hexsha
stringlengths
40
40
size
int64
2
1.02M
ext
stringclasses
10 values
lang
stringclasses
1 value
max_stars_repo_path
stringlengths
4
245
max_stars_repo_name
stringlengths
6
130
max_stars_repo_head_hexsha
stringlengths
40
40
max_stars_repo_licenses
listlengths
1
10
max_stars_count
int64
1
191k
max_stars_repo_stars_event_min_datetime
stringlengths
24
24
max_stars_repo_stars_event_max_datetime
stringlengths
24
24
max_issues_repo_path
stringlengths
4
245
max_issues_repo_name
stringlengths
6
130
max_issues_repo_head_hexsha
stringlengths
40
40
max_issues_repo_licenses
listlengths
1
10
max_issues_count
int64
1
67k
max_issues_repo_issues_event_min_datetime
stringlengths
24
24
max_issues_repo_issues_event_max_datetime
stringlengths
24
24
max_forks_repo_path
stringlengths
4
245
max_forks_repo_name
stringlengths
6
130
max_forks_repo_head_hexsha
stringlengths
40
40
max_forks_repo_licenses
listlengths
1
10
max_forks_count
int64
1
105k
max_forks_repo_forks_event_min_datetime
stringlengths
24
24
max_forks_repo_forks_event_max_datetime
stringlengths
24
24
content
stringlengths
2
1.02M
avg_line_length
float64
1
417k
max_line_length
int64
1
987k
alphanum_fraction
float64
0
1
content_no_comment
stringlengths
0
1.01M
is_comment_constant_removed
bool
1 class
is_sharp_comment_removed
bool
1 class
f705e60ad2bc78f130ddc2bedec6d3d2060f4f8b
3,309
py
Python
tests/rl_main.py
constancecrozier/CityLearn
c92f981771d29181cffce448a31d8f367a668175
[ "MIT" ]
null
null
null
tests/rl_main.py
constancecrozier/CityLearn
c92f981771d29181cffce448a31d8f367a668175
[ "MIT" ]
null
null
null
tests/rl_main.py
constancecrozier/CityLearn
c92f981771d29181cffce448a31d8f367a668175
[ "MIT" ]
null
null
null
# Run this again after editing submodules so Colab uses the updated versions from citylearn import CityLearn from citylearn import GridLearn import matplotlib.pyplot as plt from pathlib import Path from citylearn import RL_Agents_Coord, Cluster_Agents import numpy as np import csv import time import re import pandas as pd import torch from joblib import dump, load # Load environment climate_zone = 1 data_path = Path("citylearn/data/Climate_Zone_"+str(climate_zone)) building_attributes = data_path / 'building_attributes.json' weather_file = data_path / 'weather_data.csv' solar_profile = data_path / 'solar_generation_1kW.csv' building_state_actions = 'citylearn/buildings_state_action_space.json' building_id = ["Building_1","Building_2","Building_3","Building_4","Building_5","Building_6","Building_7","Building_8","Building_9"] objective_function = ['ramping','1-load_factor','average_daily_peak','peak_demand','net_electricity_consumption','quadratic','voltage_dev'] ep_period = 8760 print("Initializing the grid...") # Contain the lower and upper bounds of the states and actions, to be provided to the agent to normalize the variables between 0 and 1. # Can be obtained using observations_spaces[i].low or .high env = GridLearn(data_path, building_attributes, weather_file, solar_profile, building_id, 1, buildings_states_actions = building_state_actions, simulation_period = (0,ep_period), cost_function = objective_function, verbose=1, n_buildings_per_bus=1) # Hyperparameters batch_size = 254 bs = batch_size tau = 0.005 gamma = 0.99 lr = 0.0003 hid = [batch_size,batch_size] n_episodes = 3 n_training_eps = n_episodes - 1 if not (batch_size < ep_period * n_training_eps): print("will produce a key error because the neural nets won't be initialized yet") print("Initializing the agents...") # Instantiating the control agent(s) agents = RL_Agents_Coord(env, list(env.buildings.keys()), discount = gamma, batch_size = bs, replay_buffer_capacity = 1e5, regression_buffer_capacity = 12*ep_period, tau=tau, lr=lr, hidden_dim=hid, start_training=(ep_period+1)*(n_episodes-1), exploration_period = (ep_period+1)*(n_episodes)+1, start_regression=(ep_period+1), information_sharing = True, pca_compression = .95, action_scaling_coef=0.5, reward_scaling = 5., update_per_step = 1, iterations_as = 2) print("Starting the experiment...") # The number of episodes can be replaces by a stopping criterion (i.e. convergence of the average reward) start = time.time() for e in range(n_episodes): is_evaluating = (e > n_training_eps) # Evaluate deterministic policy after 7 epochs rewards = [] state = env.reset() done = False j = 0 print("is_deterministic", is_evaluating) action, coordination_vars = agents.select_action(state, deterministic=is_evaluating) # print(action) while not done: next_state, reward, done, _ = env.step(action) action_next, coordination_vars_next = agents.select_action(next_state, deterministic=is_evaluating) agents.add_to_buffer(state, action, reward, next_state, done, coordination_vars, coordination_vars_next) state = next_state coordination_vars = coordination_vars_next action = action_next print('Loss -',env.cost(), 'Simulation time (min) -',(time.time()-start)/60.0)
44.716216
463
0.767604
from citylearn import CityLearn from citylearn import GridLearn import matplotlib.pyplot as plt from pathlib import Path from citylearn import RL_Agents_Coord, Cluster_Agents import numpy as np import csv import time import re import pandas as pd import torch from joblib import dump, load climate_zone = 1 data_path = Path("citylearn/data/Climate_Zone_"+str(climate_zone)) building_attributes = data_path / 'building_attributes.json' weather_file = data_path / 'weather_data.csv' solar_profile = data_path / 'solar_generation_1kW.csv' building_state_actions = 'citylearn/buildings_state_action_space.json' building_id = ["Building_1","Building_2","Building_3","Building_4","Building_5","Building_6","Building_7","Building_8","Building_9"] objective_function = ['ramping','1-load_factor','average_daily_peak','peak_demand','net_electricity_consumption','quadratic','voltage_dev'] ep_period = 8760 print("Initializing the grid...") env = GridLearn(data_path, building_attributes, weather_file, solar_profile, building_id, 1, buildings_states_actions = building_state_actions, simulation_period = (0,ep_period), cost_function = objective_function, verbose=1, n_buildings_per_bus=1) batch_size = 254 bs = batch_size tau = 0.005 gamma = 0.99 lr = 0.0003 hid = [batch_size,batch_size] n_episodes = 3 n_training_eps = n_episodes - 1 if not (batch_size < ep_period * n_training_eps): print("will produce a key error because the neural nets won't be initialized yet") print("Initializing the agents...") # Instantiating the control agent(s) agents = RL_Agents_Coord(env, list(env.buildings.keys()), discount = gamma, batch_size = bs, replay_buffer_capacity = 1e5, regression_buffer_capacity = 12*ep_period, tau=tau, lr=lr, hidden_dim=hid, start_training=(ep_period+1)*(n_episodes-1), exploration_period = (ep_period+1)*(n_episodes)+1, start_regression=(ep_period+1), information_sharing = True, pca_compression = .95, action_scaling_coef=0.5, reward_scaling = 5., update_per_step = 1, iterations_as = 2) print("Starting the experiment...") # The number of episodes can be replaces by a stopping criterion (i.e. convergence of the average reward) start = time.time() for e in range(n_episodes): is_evaluating = (e > n_training_eps) # Evaluate deterministic policy after 7 epochs rewards = [] state = env.reset() done = False j = 0 print("is_deterministic", is_evaluating) action, coordination_vars = agents.select_action(state, deterministic=is_evaluating) # print(action) while not done: next_state, reward, done, _ = env.step(action) action_next, coordination_vars_next = agents.select_action(next_state, deterministic=is_evaluating) agents.add_to_buffer(state, action, reward, next_state, done, coordination_vars, coordination_vars_next) state = next_state coordination_vars = coordination_vars_next action = action_next print('Loss -',env.cost(), 'Simulation time (min) -',(time.time()-start)/60.0)
true
true
f705e793f8a0fdc1881cae42982b88bb9cb07532
16,656
py
Python
test/functional/test_framework/mininode.py
Actinium-project/acmcore-actinium-next
4b2b56d034d19ba41907c30b91948ed4bc8322ad
[ "MIT" ]
null
null
null
test/functional/test_framework/mininode.py
Actinium-project/acmcore-actinium-next
4b2b56d034d19ba41907c30b91948ed4bc8322ad
[ "MIT" ]
null
null
null
test/functional/test_framework/mininode.py
Actinium-project/acmcore-actinium-next
4b2b56d034d19ba41907c30b91948ed4bc8322ad
[ "MIT" ]
null
null
null
#!/usr/bin/env python3 # Copyright (c) 2010 ArtForz -- public domain half-a-node # Copyright (c) 2012 Jeff Garzik # Copyright (c) 2010-2017 The Bitcoin Core developers # Distributed under the MIT software license, see the accompanying # file COPYING or http://www.opensource.org/licenses/mit-license.php. """Bitcoin P2P network half-a-node. This python code was modified from ArtForz' public domain half-a-node, as found in the mini-node branch of http://github.com/jgarzik/pynode. P2PConnection: A low-level connection object to a node's P2P interface P2PInterface: A high-level interface object for communicating to a node over P2P""" import asyncore from collections import defaultdict from io import BytesIO import logging import socket import struct import sys import threading from test_framework.messages import * from test_framework.util import wait_until logger = logging.getLogger("TestFramework.mininode") MESSAGEMAP = { b"addr": msg_addr, b"block": msg_block, b"blocktxn": msg_blocktxn, b"cmpctblock": msg_cmpctblock, b"feefilter": msg_feefilter, b"getaddr": msg_getaddr, b"getblocks": msg_getblocks, b"getblocktxn": msg_getblocktxn, b"getdata": msg_getdata, b"getheaders": msg_getheaders, b"headers": msg_headers, b"inv": msg_inv, b"mempool": msg_mempool, b"ping": msg_ping, b"pong": msg_pong, b"reject": msg_reject, b"sendcmpct": msg_sendcmpct, b"sendheaders": msg_sendheaders, b"tx": msg_tx, b"verack": msg_verack, b"version": msg_version, } MAGIC_BYTES = { "mainnet": b"\xfb\xc0\xb6\xdb", # mainnet "testnet4": b"\xfd\xd2\xc8\xf1", # testnet3 "regtest": b"\xfa\xbf\xb5\xda", # regtest } class P2PConnection(asyncore.dispatcher): """A low-level connection object to a node's P2P interface. This class is responsible for: - opening and closing the TCP connection to the node - reading bytes from and writing bytes to the socket - deserializing and serializing the P2P message header - logging messages as they are sent and received This class contains no logic for handing the P2P message payloads. It must be sub-classed and the on_message() callback overridden.""" def __init__(self): # All P2PConnections must be created before starting the NetworkThread. # assert that the network thread is not running. assert not network_thread_running() super().__init__(map=mininode_socket_map) def peer_connect(self, dstaddr, dstport, net="regtest"): self.dstaddr = dstaddr self.dstport = dstport self.create_socket(socket.AF_INET, socket.SOCK_STREAM) self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) self.sendbuf = b"" self.recvbuf = b"" self.state = "connecting" self.network = net self.disconnect = False logger.info('Connecting to Actinium Node: %s:%d' % (self.dstaddr, self.dstport)) try: self.connect((dstaddr, dstport)) except: self.handle_close() def peer_disconnect(self): # Connection could have already been closed by other end. if self.state == "connected": self.disconnect_node() # Connection and disconnection methods def handle_connect(self): """asyncore callback when a connection is opened.""" if self.state != "connected": logger.debug("Connected & Listening: %s:%d" % (self.dstaddr, self.dstport)) self.state = "connected" self.on_open() def handle_close(self): """asyncore callback when a connection is closed.""" logger.debug("Closing connection to: %s:%d" % (self.dstaddr, self.dstport)) self.state = "closed" self.recvbuf = b"" self.sendbuf = b"" try: self.close() except: pass self.on_close() def disconnect_node(self): """Disconnect the p2p connection. Called by the test logic thread. Causes the p2p connection to be disconnected on the next iteration of the asyncore loop.""" self.disconnect = True # Socket read methods def handle_read(self): """asyncore callback when data is read from the socket.""" t = self.recv(8192) if len(t) > 0: self.recvbuf += t self._on_data() def _on_data(self): """Try to read P2P messages from the recv buffer. This method reads data from the buffer in a loop. It deserializes, parses and verifies the P2P header, then passes the P2P payload to the on_message callback for processing.""" try: while True: if len(self.recvbuf) < 4: return if self.recvbuf[:4] != MAGIC_BYTES[self.network]: raise ValueError("got garbage %s" % repr(self.recvbuf)) if len(self.recvbuf) < 4 + 12 + 4 + 4: return command = self.recvbuf[4:4+12].split(b"\x00", 1)[0] msglen = struct.unpack("<i", self.recvbuf[4+12:4+12+4])[0] checksum = self.recvbuf[4+12+4:4+12+4+4] if len(self.recvbuf) < 4 + 12 + 4 + 4 + msglen: return msg = self.recvbuf[4+12+4+4:4+12+4+4+msglen] th = sha256(msg) h = sha256(th) if checksum != h[:4]: raise ValueError("got bad checksum " + repr(self.recvbuf)) self.recvbuf = self.recvbuf[4+12+4+4+msglen:] if command not in MESSAGEMAP: raise ValueError("Received unknown command from %s:%d: '%s' %s" % (self.dstaddr, self.dstport, command, repr(msg))) f = BytesIO(msg) t = MESSAGEMAP[command]() t.deserialize(f) self._log_message("receive", t) self.on_message(t) except Exception as e: logger.exception('Error reading message:', repr(e)) raise def on_message(self, message): """Callback for processing a P2P payload. Must be overridden by derived class.""" raise NotImplementedError # Socket write methods def writable(self): """asyncore method to determine whether the handle_write() callback should be called on the next loop.""" with mininode_lock: pre_connection = self.state == "connecting" length = len(self.sendbuf) return (length > 0 or pre_connection) def handle_write(self): """asyncore callback when data should be written to the socket.""" with mininode_lock: # asyncore does not expose socket connection, only the first read/write # event, thus we must check connection manually here to know when we # actually connect if self.state == "connecting": self.handle_connect() if not self.writable(): return try: sent = self.send(self.sendbuf) except: self.handle_close() return self.sendbuf = self.sendbuf[sent:] def send_message(self, message, pushbuf=False): """Send a P2P message over the socket. This method takes a P2P payload, builds the P2P header and adds the message to the send buffer to be sent over the socket.""" if self.state != "connected" and not pushbuf: raise IOError('Not connected, no pushbuf') self._log_message("send", message) command = message.command data = message.serialize() tmsg = MAGIC_BYTES[self.network] tmsg += command tmsg += b"\x00" * (12 - len(command)) tmsg += struct.pack("<I", len(data)) th = sha256(data) h = sha256(th) tmsg += h[:4] tmsg += data with mininode_lock: if (len(self.sendbuf) == 0 and not pushbuf): try: sent = self.send(tmsg) self.sendbuf = tmsg[sent:] except BlockingIOError: self.sendbuf = tmsg else: self.sendbuf += tmsg # Class utility methods def _log_message(self, direction, msg): """Logs a message being sent or received over the connection.""" if direction == "send": log_message = "Send message to " elif direction == "receive": log_message = "Received message from " log_message += "%s:%d: %s" % (self.dstaddr, self.dstport, repr(msg)[:500]) if len(log_message) > 500: log_message += "... (msg truncated)" logger.debug(log_message) class P2PInterface(P2PConnection): """A high-level P2P interface class for communicating with a Bitcoin node. This class provides high-level callbacks for processing P2P message payloads, as well as convenience methods for interacting with the node over P2P. Individual testcases should subclass this and override the on_* methods if they want to alter message handling behaviour.""" def __init__(self): super().__init__() # Track number of messages of each type received and the most recent # message of each type self.message_count = defaultdict(int) self.last_message = {} # A count of the number of ping messages we've sent to the node self.ping_counter = 1 # The network services received from the peer self.nServices = 0 def peer_connect(self, *args, services=NODE_NETWORK|NODE_WITNESS, send_version=True, **kwargs): super().peer_connect(*args, **kwargs) if send_version: # Send a version msg vt = msg_version() vt.nServices = services vt.addrTo.ip = self.dstaddr vt.addrTo.port = self.dstport vt.addrFrom.ip = "0.0.0.0" vt.addrFrom.port = 0 self.send_message(vt, True) # Message receiving methods def on_message(self, message): """Receive message and dispatch message to appropriate callback. We keep a count of how many of each message type has been received and the most recent message of each type.""" with mininode_lock: try: command = message.command.decode('ascii') self.message_count[command] += 1 self.last_message[command] = message getattr(self, 'on_' + command)(message) except: print("ERROR delivering %s (%s)" % (repr(message), sys.exc_info()[0])) raise # Callback methods. Can be overridden by subclasses in individual test # cases to provide custom message handling behaviour. def on_open(self): pass def on_close(self): pass def on_addr(self, message): pass def on_block(self, message): pass def on_blocktxn(self, message): pass def on_cmpctblock(self, message): pass def on_feefilter(self, message): pass def on_getaddr(self, message): pass def on_getblocks(self, message): pass def on_getblocktxn(self, message): pass def on_getdata(self, message): pass def on_getheaders(self, message): pass def on_headers(self, message): pass def on_mempool(self, message): pass def on_pong(self, message): pass def on_reject(self, message): pass def on_sendcmpct(self, message): pass def on_sendheaders(self, message): pass def on_tx(self, message): pass def on_inv(self, message): want = msg_getdata() for i in message.inv: if i.type != 0: want.inv.append(i) if len(want.inv): self.send_message(want) def on_ping(self, message): self.send_message(msg_pong(message.nonce)) def on_verack(self, message): self.verack_received = True def on_version(self, message): assert message.nVersion >= MIN_VERSION_SUPPORTED, "Version {} received. Test framework only supports versions greater than {}".format(message.nVersion, MIN_VERSION_SUPPORTED) self.send_message(msg_verack()) self.nServices = message.nServices # Connection helper methods def wait_for_disconnect(self, timeout=60): test_function = lambda: self.state != "connected" wait_until(test_function, timeout=timeout, lock=mininode_lock) # Message receiving helper methods def wait_for_block(self, blockhash, timeout=60): test_function = lambda: self.last_message.get("block") and self.last_message["block"].block.rehash() == blockhash wait_until(test_function, timeout=timeout, lock=mininode_lock) def wait_for_getdata(self, timeout=60): test_function = lambda: self.last_message.get("getdata") wait_until(test_function, timeout=timeout, lock=mininode_lock) def wait_for_getheaders(self, timeout=60): test_function = lambda: self.last_message.get("getheaders") wait_until(test_function, timeout=timeout, lock=mininode_lock) def wait_for_inv(self, expected_inv, timeout=60): """Waits for an INV message and checks that the first inv object in the message was as expected.""" if len(expected_inv) > 1: raise NotImplementedError("wait_for_inv() will only verify the first inv object") test_function = lambda: self.last_message.get("inv") and \ self.last_message["inv"].inv[0].type == expected_inv[0].type and \ self.last_message["inv"].inv[0].hash == expected_inv[0].hash wait_until(test_function, timeout=timeout, lock=mininode_lock) def wait_for_verack(self, timeout=60): test_function = lambda: self.message_count["verack"] wait_until(test_function, timeout=timeout, lock=mininode_lock) # Message sending helper functions def send_and_ping(self, message): self.send_message(message) self.sync_with_ping() # Sync up with the node def sync_with_ping(self, timeout=60): self.send_message(msg_ping(nonce=self.ping_counter)) test_function = lambda: self.last_message.get("pong") and self.last_message["pong"].nonce == self.ping_counter wait_until(test_function, timeout=timeout, lock=mininode_lock) self.ping_counter += 1 # Keep our own socket map for asyncore, so that we can track disconnects # ourselves (to workaround an issue with closing an asyncore socket when # using select) mininode_socket_map = dict() # One lock for synchronizing all data access between the networking thread (see # NetworkThread below) and the thread running the test logic. For simplicity, # P2PConnection acquires this lock whenever delivering a message to a P2PInterface, # and whenever adding anything to the send buffer (in send_message()). This # lock should be acquired in the thread running the test logic to synchronize # access to any data shared with the P2PInterface or P2PConnection. mininode_lock = threading.RLock() class NetworkThread(threading.Thread): def __init__(self): super().__init__(name="NetworkThread") def run(self): while mininode_socket_map: # We check for whether to disconnect outside of the asyncore # loop to workaround the behavior of asyncore when using # select disconnected = [] for fd, obj in mininode_socket_map.items(): if obj.disconnect: disconnected.append(obj) [obj.handle_close() for obj in disconnected] asyncore.loop(0.1, use_poll=True, map=mininode_socket_map, count=1) logger.debug("Network thread closing") def network_thread_start(): """Start the network thread.""" # Only one network thread may run at a time assert not network_thread_running() NetworkThread().start() def network_thread_running(): """Return whether the network thread is running.""" return any([thread.name == "NetworkThread" for thread in threading.enumerate()]) def network_thread_join(timeout=10): """Wait timeout seconds for the network thread to terminate. Throw if the network thread doesn't terminate in timeout seconds.""" network_threads = [thread for thread in threading.enumerate() if thread.name == "NetworkThread"] assert len(network_threads) <= 1 for thread in network_threads: thread.join(timeout) assert not thread.is_alive()
37.598194
182
0.635447
import asyncore from collections import defaultdict from io import BytesIO import logging import socket import struct import sys import threading from test_framework.messages import * from test_framework.util import wait_until logger = logging.getLogger("TestFramework.mininode") MESSAGEMAP = { b"addr": msg_addr, b"block": msg_block, b"blocktxn": msg_blocktxn, b"cmpctblock": msg_cmpctblock, b"feefilter": msg_feefilter, b"getaddr": msg_getaddr, b"getblocks": msg_getblocks, b"getblocktxn": msg_getblocktxn, b"getdata": msg_getdata, b"getheaders": msg_getheaders, b"headers": msg_headers, b"inv": msg_inv, b"mempool": msg_mempool, b"ping": msg_ping, b"pong": msg_pong, b"reject": msg_reject, b"sendcmpct": msg_sendcmpct, b"sendheaders": msg_sendheaders, b"tx": msg_tx, b"verack": msg_verack, b"version": msg_version, } MAGIC_BYTES = { "mainnet": b"\xfb\xc0\xb6\xdb", "testnet4": b"\xfd\xd2\xc8\xf1", "regtest": b"\xfa\xbf\xb5\xda", } class P2PConnection(asyncore.dispatcher): def __init__(self): assert not network_thread_running() super().__init__(map=mininode_socket_map) def peer_connect(self, dstaddr, dstport, net="regtest"): self.dstaddr = dstaddr self.dstport = dstport self.create_socket(socket.AF_INET, socket.SOCK_STREAM) self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) self.sendbuf = b"" self.recvbuf = b"" self.state = "connecting" self.network = net self.disconnect = False logger.info('Connecting to Actinium Node: %s:%d' % (self.dstaddr, self.dstport)) try: self.connect((dstaddr, dstport)) except: self.handle_close() def peer_disconnect(self): if self.state == "connected": self.disconnect_node() def handle_connect(self): if self.state != "connected": logger.debug("Connected & Listening: %s:%d" % (self.dstaddr, self.dstport)) self.state = "connected" self.on_open() def handle_close(self): logger.debug("Closing connection to: %s:%d" % (self.dstaddr, self.dstport)) self.state = "closed" self.recvbuf = b"" self.sendbuf = b"" try: self.close() except: pass self.on_close() def disconnect_node(self): self.disconnect = True def handle_read(self): t = self.recv(8192) if len(t) > 0: self.recvbuf += t self._on_data() def _on_data(self): try: while True: if len(self.recvbuf) < 4: return if self.recvbuf[:4] != MAGIC_BYTES[self.network]: raise ValueError("got garbage %s" % repr(self.recvbuf)) if len(self.recvbuf) < 4 + 12 + 4 + 4: return command = self.recvbuf[4:4+12].split(b"\x00", 1)[0] msglen = struct.unpack("<i", self.recvbuf[4+12:4+12+4])[0] checksum = self.recvbuf[4+12+4:4+12+4+4] if len(self.recvbuf) < 4 + 12 + 4 + 4 + msglen: return msg = self.recvbuf[4+12+4+4:4+12+4+4+msglen] th = sha256(msg) h = sha256(th) if checksum != h[:4]: raise ValueError("got bad checksum " + repr(self.recvbuf)) self.recvbuf = self.recvbuf[4+12+4+4+msglen:] if command not in MESSAGEMAP: raise ValueError("Received unknown command from %s:%d: '%s' %s" % (self.dstaddr, self.dstport, command, repr(msg))) f = BytesIO(msg) t = MESSAGEMAP[command]() t.deserialize(f) self._log_message("receive", t) self.on_message(t) except Exception as e: logger.exception('Error reading message:', repr(e)) raise def on_message(self, message): raise NotImplementedError def writable(self): with mininode_lock: pre_connection = self.state == "connecting" length = len(self.sendbuf) return (length > 0 or pre_connection) def handle_write(self): with mininode_lock: if self.state == "connecting": self.handle_connect() if not self.writable(): return try: sent = self.send(self.sendbuf) except: self.handle_close() return self.sendbuf = self.sendbuf[sent:] def send_message(self, message, pushbuf=False): if self.state != "connected" and not pushbuf: raise IOError('Not connected, no pushbuf') self._log_message("send", message) command = message.command data = message.serialize() tmsg = MAGIC_BYTES[self.network] tmsg += command tmsg += b"\x00" * (12 - len(command)) tmsg += struct.pack("<I", len(data)) th = sha256(data) h = sha256(th) tmsg += h[:4] tmsg += data with mininode_lock: if (len(self.sendbuf) == 0 and not pushbuf): try: sent = self.send(tmsg) self.sendbuf = tmsg[sent:] except BlockingIOError: self.sendbuf = tmsg else: self.sendbuf += tmsg def _log_message(self, direction, msg): if direction == "send": log_message = "Send message to " elif direction == "receive": log_message = "Received message from " log_message += "%s:%d: %s" % (self.dstaddr, self.dstport, repr(msg)[:500]) if len(log_message) > 500: log_message += "... (msg truncated)" logger.debug(log_message) class P2PInterface(P2PConnection): def __init__(self): super().__init__() self.message_count = defaultdict(int) self.last_message = {} self.ping_counter = 1 # The network services received from the peer self.nServices = 0 def peer_connect(self, *args, services=NODE_NETWORK|NODE_WITNESS, send_version=True, **kwargs): super().peer_connect(*args, **kwargs) if send_version: # Send a version msg vt = msg_version() vt.nServices = services vt.addrTo.ip = self.dstaddr vt.addrTo.port = self.dstport vt.addrFrom.ip = "0.0.0.0" vt.addrFrom.port = 0 self.send_message(vt, True) # Message receiving methods def on_message(self, message): with mininode_lock: try: command = message.command.decode('ascii') self.message_count[command] += 1 self.last_message[command] = message getattr(self, 'on_' + command)(message) except: print("ERROR delivering %s (%s)" % (repr(message), sys.exc_info()[0])) raise # Callback methods. Can be overridden by subclasses in individual test # cases to provide custom message handling behaviour. def on_open(self): pass def on_close(self): pass def on_addr(self, message): pass def on_block(self, message): pass def on_blocktxn(self, message): pass def on_cmpctblock(self, message): pass def on_feefilter(self, message): pass def on_getaddr(self, message): pass def on_getblocks(self, message): pass def on_getblocktxn(self, message): pass def on_getdata(self, message): pass def on_getheaders(self, message): pass def on_headers(self, message): pass def on_mempool(self, message): pass def on_pong(self, message): pass def on_reject(self, message): pass def on_sendcmpct(self, message): pass def on_sendheaders(self, message): pass def on_tx(self, message): pass def on_inv(self, message): want = msg_getdata() for i in message.inv: if i.type != 0: want.inv.append(i) if len(want.inv): self.send_message(want) def on_ping(self, message): self.send_message(msg_pong(message.nonce)) def on_verack(self, message): self.verack_received = True def on_version(self, message): assert message.nVersion >= MIN_VERSION_SUPPORTED, "Version {} received. Test framework only supports versions greater than {}".format(message.nVersion, MIN_VERSION_SUPPORTED) self.send_message(msg_verack()) self.nServices = message.nServices # Connection helper methods def wait_for_disconnect(self, timeout=60): test_function = lambda: self.state != "connected" wait_until(test_function, timeout=timeout, lock=mininode_lock) # Message receiving helper methods def wait_for_block(self, blockhash, timeout=60): test_function = lambda: self.last_message.get("block") and self.last_message["block"].block.rehash() == blockhash wait_until(test_function, timeout=timeout, lock=mininode_lock) def wait_for_getdata(self, timeout=60): test_function = lambda: self.last_message.get("getdata") wait_until(test_function, timeout=timeout, lock=mininode_lock) def wait_for_getheaders(self, timeout=60): test_function = lambda: self.last_message.get("getheaders") wait_until(test_function, timeout=timeout, lock=mininode_lock) def wait_for_inv(self, expected_inv, timeout=60): if len(expected_inv) > 1: raise NotImplementedError("wait_for_inv() will only verify the first inv object") test_function = lambda: self.last_message.get("inv") and \ self.last_message["inv"].inv[0].type == expected_inv[0].type and \ self.last_message["inv"].inv[0].hash == expected_inv[0].hash wait_until(test_function, timeout=timeout, lock=mininode_lock) def wait_for_verack(self, timeout=60): test_function = lambda: self.message_count["verack"] wait_until(test_function, timeout=timeout, lock=mininode_lock) # Message sending helper functions def send_and_ping(self, message): self.send_message(message) self.sync_with_ping() # Sync up with the node def sync_with_ping(self, timeout=60): self.send_message(msg_ping(nonce=self.ping_counter)) test_function = lambda: self.last_message.get("pong") and self.last_message["pong"].nonce == self.ping_counter wait_until(test_function, timeout=timeout, lock=mininode_lock) self.ping_counter += 1 # Keep our own socket map for asyncore, so that we can track disconnects # ourselves (to workaround an issue with closing an asyncore socket when # using select) mininode_socket_map = dict() # One lock for synchronizing all data access between the networking thread (see # NetworkThread below) and the thread running the test logic. For simplicity, # P2PConnection acquires this lock whenever delivering a message to a P2PInterface, # and whenever adding anything to the send buffer (in send_message()). This # lock should be acquired in the thread running the test logic to synchronize # access to any data shared with the P2PInterface or P2PConnection. mininode_lock = threading.RLock() class NetworkThread(threading.Thread): def __init__(self): super().__init__(name="NetworkThread") def run(self): while mininode_socket_map: # We check for whether to disconnect outside of the asyncore # loop to workaround the behavior of asyncore when using # select disconnected = [] for fd, obj in mininode_socket_map.items(): if obj.disconnect: disconnected.append(obj) [obj.handle_close() for obj in disconnected] asyncore.loop(0.1, use_poll=True, map=mininode_socket_map, count=1) logger.debug("Network thread closing") def network_thread_start(): # Only one network thread may run at a time assert not network_thread_running() NetworkThread().start() def network_thread_running(): return any([thread.name == "NetworkThread" for thread in threading.enumerate()]) def network_thread_join(timeout=10): network_threads = [thread for thread in threading.enumerate() if thread.name == "NetworkThread"] assert len(network_threads) <= 1 for thread in network_threads: thread.join(timeout) assert not thread.is_alive()
true
true
f705e7e9ad7e6291fc8bcef3839455d8f43946dd
3,324
py
Python
mysodexo/cli.py
AndreMiras/mysodexo
3d0836c38e10d579a8758b998744d1a38ff77260
[ "MIT" ]
9
2019-11-13T08:15:51.000Z
2022-03-07T18:48:28.000Z
mysodexo/cli.py
AndreMiras/mysodexo
3d0836c38e10d579a8758b998744d1a38ff77260
[ "MIT" ]
3
2019-11-13T16:45:33.000Z
2021-10-21T10:47:54.000Z
mysodexo/cli.py
AndreMiras/mysodexo
3d0836c38e10d579a8758b998744d1a38ff77260
[ "MIT" ]
1
2020-10-10T10:22:37.000Z
2020-10-10T10:22:37.000Z
#!/usr/bin/env python3 import argparse import os import pickle from getpass import getpass from typing import Tuple import requests from appdirs import user_cache_dir from mysodexo import api from mysodexo.constants import APPLICATION_NAME, SESSION_CACHE_FILENAME def prompt_login() -> Tuple[str, str]: """Prompts user for credentials and the returns them as a tuple.""" email = input("email: ") password = getpass("password: ") return (email, password) def get_session_cache_path() -> str: return os.path.join( user_cache_dir(appname=APPLICATION_NAME), SESSION_CACHE_FILENAME ) def get_cached_session_info() -> Tuple[ requests.cookies.RequestsCookieJar, str ]: """Returns session and DNI from cache.""" session_cache_path = get_session_cache_path() with open(session_cache_path, "rb") as f: cached_session_info = pickle.load(f) cookies = cached_session_info["cookies"] dni = cached_session_info["dni"] return (cookies, dni) def cache_session_info( cookies: requests.cookies.RequestsCookieJar, dni: str ) -> None: """Stores session info to cache.""" session_cache_path = get_session_cache_path() cached_session_info = { "cookies": cookies, "dni": dni, } os.makedirs(os.path.dirname(session_cache_path), exist_ok=True) with open(session_cache_path, "wb") as f: pickle.dump(cached_session_info, f) def login() -> Tuple[requests.sessions.Session, str]: """Logins and returns session info.""" email, password = prompt_login() session, account_info = api.login(email, password) dni = account_info["dni"] return (session, dni) def process_login() -> Tuple[requests.sessions.Session, str]: """Logins and stores session info to cache.""" session, dni = login() cache_session_info(session.cookies, dni) return (session, dni) def get_session_or_login() -> Tuple[requests.sessions.Session, str]: """Retrieves session from cache or prompts login then stores session.""" try: cookies, dni = get_cached_session_info() session = requests.session() session.cookies.update(cookies) except FileNotFoundError: session, dni = process_login() return session, dni def print_balance(cards): """Prints per card balance.""" for card in cards: pan = card["pan"] details = card["_details"] balance = details["cardBalance"] print(f"{pan}: {balance}") def process_balance(): session, dni = get_session_or_login() cards = api.get_cards(session, dni) for card in cards: card_number = card["cardNumber"] details = api.get_detail_card(session, card_number) card["_details"] = details print_balance(cards) def main(): parser = argparse.ArgumentParser( description="MySodexo Command Line Interface" ) parser.add_argument( "--login", action="store_true", help="Logins and store session.", ) parser.add_argument( "--balance", action="store_true", help="Returns account balance per card", ) args = parser.parse_args() if args.login: process_login() elif args.balance: process_balance() else: parser.print_help() if __name__ == "__main__": main()
27.245902
76
0.671781
import argparse import os import pickle from getpass import getpass from typing import Tuple import requests from appdirs import user_cache_dir from mysodexo import api from mysodexo.constants import APPLICATION_NAME, SESSION_CACHE_FILENAME def prompt_login() -> Tuple[str, str]: email = input("email: ") password = getpass("password: ") return (email, password) def get_session_cache_path() -> str: return os.path.join( user_cache_dir(appname=APPLICATION_NAME), SESSION_CACHE_FILENAME ) def get_cached_session_info() -> Tuple[ requests.cookies.RequestsCookieJar, str ]: session_cache_path = get_session_cache_path() with open(session_cache_path, "rb") as f: cached_session_info = pickle.load(f) cookies = cached_session_info["cookies"] dni = cached_session_info["dni"] return (cookies, dni) def cache_session_info( cookies: requests.cookies.RequestsCookieJar, dni: str ) -> None: session_cache_path = get_session_cache_path() cached_session_info = { "cookies": cookies, "dni": dni, } os.makedirs(os.path.dirname(session_cache_path), exist_ok=True) with open(session_cache_path, "wb") as f: pickle.dump(cached_session_info, f) def login() -> Tuple[requests.sessions.Session, str]: email, password = prompt_login() session, account_info = api.login(email, password) dni = account_info["dni"] return (session, dni) def process_login() -> Tuple[requests.sessions.Session, str]: session, dni = login() cache_session_info(session.cookies, dni) return (session, dni) def get_session_or_login() -> Tuple[requests.sessions.Session, str]: try: cookies, dni = get_cached_session_info() session = requests.session() session.cookies.update(cookies) except FileNotFoundError: session, dni = process_login() return session, dni def print_balance(cards): for card in cards: pan = card["pan"] details = card["_details"] balance = details["cardBalance"] print(f"{pan}: {balance}") def process_balance(): session, dni = get_session_or_login() cards = api.get_cards(session, dni) for card in cards: card_number = card["cardNumber"] details = api.get_detail_card(session, card_number) card["_details"] = details print_balance(cards) def main(): parser = argparse.ArgumentParser( description="MySodexo Command Line Interface" ) parser.add_argument( "--login", action="store_true", help="Logins and store session.", ) parser.add_argument( "--balance", action="store_true", help="Returns account balance per card", ) args = parser.parse_args() if args.login: process_login() elif args.balance: process_balance() else: parser.print_help() if __name__ == "__main__": main()
true
true
f705e87ae9a2d445b3fb793b75ab4fa77992ea13
755
py
Python
DZ16_04/less11_5.py
denyskovalev/hillel_school_python
df75b3d6779fd03bc94278d27508cc3e461ab421
[ "MIT" ]
null
null
null
DZ16_04/less11_5.py
denyskovalev/hillel_school_python
df75b3d6779fd03bc94278d27508cc3e461ab421
[ "MIT" ]
null
null
null
DZ16_04/less11_5.py
denyskovalev/hillel_school_python
df75b3d6779fd03bc94278d27508cc3e461ab421
[ "MIT" ]
null
null
null
# Реалізувати клас Герой що має мати наступні атрибути: ім‘я, здоров‘я, ранг, сила і метод вдарити. # Метод вдарити повинен наносити шкоду противнику в розмірі сили героя. Герой має мати наступні # обмеження: здоров‘я від 0 до 100, ранг 1,2,3. Сила не більше 10% теперішнього здоров‘я героя. # Не можна бити героїв здоров‘я яких менше 5. # # Реалізувати клас маг, який може відновлювати здоров'я інших героїв, також він має ранг як герой і # може наносити удари. За відновлення здоров'я він бере гроші. ( Вам потрібно реалізувати цей функціонал ). # Герой заробляє гроші за перемогу у бою з іншим героєм, також при перемозі він забирає всі гроші суперника. # Скільки герой отримує грошей за перемогу і скільки коштує відновити здоров'я, на ваш розсуд)
58.076923
108
0.774834
# може наносити удари. За відновлення здоров'я він бере гроші. ( Вам потрібно реалізувати цей функціонал ).
true
true
f705e8be9f1c5b46fb2b7a7156603fafbace0312
14,306
py
Python
gitee/models/body19.py
pygitee/pygitee
7622314a4dbb08cf2f729b6cdd0a2887b96e394e
[ "MIT" ]
null
null
null
gitee/models/body19.py
pygitee/pygitee
7622314a4dbb08cf2f729b6cdd0a2887b96e394e
[ "MIT" ]
null
null
null
gitee/models/body19.py
pygitee/pygitee
7622314a4dbb08cf2f729b6cdd0a2887b96e394e
[ "MIT" ]
null
null
null
# coding: utf-8 import pprint import re # noqa: F401 import six class Body19(object): """NOTE: This class is auto generated by the swagger code generator program. Do not edit the class manually. """ """ Attributes: swagger_types (dict): The key is attribute name and the value is attribute type. attribute_map (dict): The key is attribute name and the value is json key in definition. """ swagger_types = { 'access_token': 'str', 'name': 'str', 'description': 'str', 'homepage': 'str', 'has_issues': 'bool', 'has_wiki': 'bool', 'can_comment': 'bool', 'issue_comment': 'bool', 'security_hole_enabled': 'bool', 'private': 'bool', 'path': 'str', 'default_branch': 'str', 'pull_requests_enabled': 'bool', 'online_edit_enabled': 'bool', 'lightweight_pr_enabled': 'bool' } attribute_map = { 'access_token': 'access_token', 'name': 'name', 'description': 'description', 'homepage': 'homepage', 'has_issues': 'has_issues', 'has_wiki': 'has_wiki', 'can_comment': 'can_comment', 'issue_comment': 'issue_comment', 'security_hole_enabled': 'security_hole_enabled', 'private': 'private', 'path': 'path', 'default_branch': 'default_branch', 'pull_requests_enabled': 'pull_requests_enabled', 'online_edit_enabled': 'online_edit_enabled', 'lightweight_pr_enabled': 'lightweight_pr_enabled' } def __init__(self, access_token=None, name=None, description=None, homepage=None, has_issues=True, has_wiki=True, can_comment=True, issue_comment=None, security_hole_enabled=None, private=None, path=None, default_branch=None, pull_requests_enabled=None, online_edit_enabled=None, lightweight_pr_enabled=None): # noqa: E501 """Body19 - a model defined in Swagger""" # noqa: E501 self._access_token = None self._name = None self._description = None self._homepage = None self._has_issues = None self._has_wiki = None self._can_comment = None self._issue_comment = None self._security_hole_enabled = None self._private = None self._path = None self._default_branch = None self._pull_requests_enabled = None self._online_edit_enabled = None self._lightweight_pr_enabled = None self.discriminator = None if access_token is not None: self.access_token = access_token self.name = name if description is not None: self.description = description if homepage is not None: self.homepage = homepage if has_issues is not None: self.has_issues = has_issues if has_wiki is not None: self.has_wiki = has_wiki if can_comment is not None: self.can_comment = can_comment if issue_comment is not None: self.issue_comment = issue_comment if security_hole_enabled is not None: self.security_hole_enabled = security_hole_enabled if private is not None: self.private = private if path is not None: self.path = path if default_branch is not None: self.default_branch = default_branch if pull_requests_enabled is not None: self.pull_requests_enabled = pull_requests_enabled if online_edit_enabled is not None: self.online_edit_enabled = online_edit_enabled if lightweight_pr_enabled is not None: self.lightweight_pr_enabled = lightweight_pr_enabled @property def access_token(self): """Gets the access_token of this Body19. # noqa: E501 用户授权码 # noqa: E501 :return: The access_token of this Body19. # noqa: E501 :rtype: str """ return self._access_token @access_token.setter def access_token(self, access_token): """Sets the access_token of this Body19. 用户授权码 # noqa: E501 :param access_token: The access_token of this Body19. # noqa: E501 :type: str """ self._access_token = access_token @property def name(self): """Gets the name of this Body19. # noqa: E501 仓库名称 # noqa: E501 :return: The name of this Body19. # noqa: E501 :rtype: str """ return self._name @name.setter def name(self, name): """Sets the name of this Body19. 仓库名称 # noqa: E501 :param name: The name of this Body19. # noqa: E501 :type: str """ if name is None: raise ValueError("Invalid value for `name`, must not be `None`") # noqa: E501 self._name = name @property def description(self): """Gets the description of this Body19. # noqa: E501 仓库描述 # noqa: E501 :return: The description of this Body19. # noqa: E501 :rtype: str """ return self._description @description.setter def description(self, description): """Sets the description of this Body19. 仓库描述 # noqa: E501 :param description: The description of this Body19. # noqa: E501 :type: str """ self._description = description @property def homepage(self): """Gets the homepage of this Body19. # noqa: E501 主页(eg: https://gitee.com) # noqa: E501 :return: The homepage of this Body19. # noqa: E501 :rtype: str """ return self._homepage @homepage.setter def homepage(self, homepage): """Sets the homepage of this Body19. 主页(eg: https://gitee.com) # noqa: E501 :param homepage: The homepage of this Body19. # noqa: E501 :type: str """ self._homepage = homepage @property def has_issues(self): """Gets the has_issues of this Body19. # noqa: E501 允许提Issue与否。默认: 允许(true) # noqa: E501 :return: The has_issues of this Body19. # noqa: E501 :rtype: bool """ return self._has_issues @has_issues.setter def has_issues(self, has_issues): """Sets the has_issues of this Body19. 允许提Issue与否。默认: 允许(true) # noqa: E501 :param has_issues: The has_issues of this Body19. # noqa: E501 :type: bool """ self._has_issues = has_issues @property def has_wiki(self): """Gets the has_wiki of this Body19. # noqa: E501 提供Wiki与否。默认: 提供(true) # noqa: E501 :return: The has_wiki of this Body19. # noqa: E501 :rtype: bool """ return self._has_wiki @has_wiki.setter def has_wiki(self, has_wiki): """Sets the has_wiki of this Body19. 提供Wiki与否。默认: 提供(true) # noqa: E501 :param has_wiki: The has_wiki of this Body19. # noqa: E501 :type: bool """ self._has_wiki = has_wiki @property def can_comment(self): """Gets the can_comment of this Body19. # noqa: E501 允许用户对仓库进行评论。默认: 允许(true) # noqa: E501 :return: The can_comment of this Body19. # noqa: E501 :rtype: bool """ return self._can_comment @can_comment.setter def can_comment(self, can_comment): """Sets the can_comment of this Body19. 允许用户对仓库进行评论。默认: 允许(true) # noqa: E501 :param can_comment: The can_comment of this Body19. # noqa: E501 :type: bool """ self._can_comment = can_comment @property def issue_comment(self): """Gets the issue_comment of this Body19. # noqa: E501 允许对“关闭”状态的 Issue 进行评论。默认: 不允许(false) # noqa: E501 :return: The issue_comment of this Body19. # noqa: E501 :rtype: bool """ return self._issue_comment @issue_comment.setter def issue_comment(self, issue_comment): """Sets the issue_comment of this Body19. 允许对“关闭”状态的 Issue 进行评论。默认: 不允许(false) # noqa: E501 :param issue_comment: The issue_comment of this Body19. # noqa: E501 :type: bool """ self._issue_comment = issue_comment @property def security_hole_enabled(self): """Gets the security_hole_enabled of this Body19. # noqa: E501 允许用户创建涉及敏感信息的 Issue。默认: 不允许(false) # noqa: E501 :return: The security_hole_enabled of this Body19. # noqa: E501 :rtype: bool """ return self._security_hole_enabled @security_hole_enabled.setter def security_hole_enabled(self, security_hole_enabled): """Sets the security_hole_enabled of this Body19. 允许用户创建涉及敏感信息的 Issue。默认: 不允许(false) # noqa: E501 :param security_hole_enabled: The security_hole_enabled of this Body19. # noqa: E501 :type: bool """ self._security_hole_enabled = security_hole_enabled @property def private(self): """Gets the private of this Body19. # noqa: E501 仓库公开或私有。 # noqa: E501 :return: The private of this Body19. # noqa: E501 :rtype: bool """ return self._private @private.setter def private(self, private): """Sets the private of this Body19. 仓库公开或私有。 # noqa: E501 :param private: The private of this Body19. # noqa: E501 :type: bool """ self._private = private @property def path(self): """Gets the path of this Body19. # noqa: E501 更新仓库路径 # noqa: E501 :return: The path of this Body19. # noqa: E501 :rtype: str """ return self._path @path.setter def path(self, path): """Sets the path of this Body19. 更新仓库路径 # noqa: E501 :param path: The path of this Body19. # noqa: E501 :type: str """ self._path = path @property def default_branch(self): """Gets the default_branch of this Body19. # noqa: E501 更新默认分支 # noqa: E501 :return: The default_branch of this Body19. # noqa: E501 :rtype: str """ return self._default_branch @default_branch.setter def default_branch(self, default_branch): """Sets the default_branch of this Body19. 更新默认分支 # noqa: E501 :param default_branch: The default_branch of this Body19. # noqa: E501 :type: str """ self._default_branch = default_branch @property def pull_requests_enabled(self): """Gets the pull_requests_enabled of this Body19. # noqa: E501 接受 pull request,协作开发 # noqa: E501 :return: The pull_requests_enabled of this Body19. # noqa: E501 :rtype: bool """ return self._pull_requests_enabled @pull_requests_enabled.setter def pull_requests_enabled(self, pull_requests_enabled): """Sets the pull_requests_enabled of this Body19. 接受 pull request,协作开发 # noqa: E501 :param pull_requests_enabled: The pull_requests_enabled of this Body19. # noqa: E501 :type: bool """ self._pull_requests_enabled = pull_requests_enabled @property def online_edit_enabled(self): """Gets the online_edit_enabled of this Body19. # noqa: E501 是否允许仓库文件在线编辑 # noqa: E501 :return: The online_edit_enabled of this Body19. # noqa: E501 :rtype: bool """ return self._online_edit_enabled @online_edit_enabled.setter def online_edit_enabled(self, online_edit_enabled): """Sets the online_edit_enabled of this Body19. 是否允许仓库文件在线编辑 # noqa: E501 :param online_edit_enabled: The online_edit_enabled of this Body19. # noqa: E501 :type: bool """ self._online_edit_enabled = online_edit_enabled @property def lightweight_pr_enabled(self): """Gets the lightweight_pr_enabled of this Body19. # noqa: E501 是否接受轻量级 pull request # noqa: E501 :return: The lightweight_pr_enabled of this Body19. # noqa: E501 :rtype: bool """ return self._lightweight_pr_enabled @lightweight_pr_enabled.setter def lightweight_pr_enabled(self, lightweight_pr_enabled): """Sets the lightweight_pr_enabled of this Body19. 是否接受轻量级 pull request # noqa: E501 :param lightweight_pr_enabled: The lightweight_pr_enabled of this Body19. # noqa: E501 :type: bool """ self._lightweight_pr_enabled = lightweight_pr_enabled def to_dict(self): """Returns the model properties as a dict""" result = {} for attr, _ in six.iteritems(self.swagger_types): value = getattr(self, attr) if isinstance(value, list): result[attr] = list(map( lambda x: x.to_dict() if hasattr(x, "to_dict") else x, value )) elif hasattr(value, "to_dict"): result[attr] = value.to_dict() elif isinstance(value, dict): result[attr] = dict(map( lambda item: (item[0], item[1].to_dict()) if hasattr(item[1], "to_dict") else item, value.items() )) else: result[attr] = value if issubclass(Body19, dict): for key, value in self.items(): result[key] = value return result def to_str(self): """Returns the string representation of the model""" return pprint.pformat(self.to_dict()) def __repr__(self): """For `print` and `pprint`""" return self.to_str() def __eq__(self, other): """Returns true if both objects are equal""" if not isinstance(other, Body19): return False return self.__dict__ == other.__dict__ def __ne__(self, other): """Returns true if both objects are not equal""" return not self == other
28.55489
117
0.592199
import pprint import re import six class Body19(object): swagger_types = { 'access_token': 'str', 'name': 'str', 'description': 'str', 'homepage': 'str', 'has_issues': 'bool', 'has_wiki': 'bool', 'can_comment': 'bool', 'issue_comment': 'bool', 'security_hole_enabled': 'bool', 'private': 'bool', 'path': 'str', 'default_branch': 'str', 'pull_requests_enabled': 'bool', 'online_edit_enabled': 'bool', 'lightweight_pr_enabled': 'bool' } attribute_map = { 'access_token': 'access_token', 'name': 'name', 'description': 'description', 'homepage': 'homepage', 'has_issues': 'has_issues', 'has_wiki': 'has_wiki', 'can_comment': 'can_comment', 'issue_comment': 'issue_comment', 'security_hole_enabled': 'security_hole_enabled', 'private': 'private', 'path': 'path', 'default_branch': 'default_branch', 'pull_requests_enabled': 'pull_requests_enabled', 'online_edit_enabled': 'online_edit_enabled', 'lightweight_pr_enabled': 'lightweight_pr_enabled' } def __init__(self, access_token=None, name=None, description=None, homepage=None, has_issues=True, has_wiki=True, can_comment=True, issue_comment=None, security_hole_enabled=None, private=None, path=None, default_branch=None, pull_requests_enabled=None, online_edit_enabled=None, lightweight_pr_enabled=None): self._access_token = None self._name = None self._description = None self._homepage = None self._has_issues = None self._has_wiki = None self._can_comment = None self._issue_comment = None self._security_hole_enabled = None self._private = None self._path = None self._default_branch = None self._pull_requests_enabled = None self._online_edit_enabled = None self._lightweight_pr_enabled = None self.discriminator = None if access_token is not None: self.access_token = access_token self.name = name if description is not None: self.description = description if homepage is not None: self.homepage = homepage if has_issues is not None: self.has_issues = has_issues if has_wiki is not None: self.has_wiki = has_wiki if can_comment is not None: self.can_comment = can_comment if issue_comment is not None: self.issue_comment = issue_comment if security_hole_enabled is not None: self.security_hole_enabled = security_hole_enabled if private is not None: self.private = private if path is not None: self.path = path if default_branch is not None: self.default_branch = default_branch if pull_requests_enabled is not None: self.pull_requests_enabled = pull_requests_enabled if online_edit_enabled is not None: self.online_edit_enabled = online_edit_enabled if lightweight_pr_enabled is not None: self.lightweight_pr_enabled = lightweight_pr_enabled @property def access_token(self): return self._access_token @access_token.setter def access_token(self, access_token): self._access_token = access_token @property def name(self): return self._name @name.setter def name(self, name): if name is None: raise ValueError("Invalid value for `name`, must not be `None`") self._name = name @property def description(self): return self._description @description.setter def description(self, description): self._description = description @property def homepage(self): return self._homepage @homepage.setter def homepage(self, homepage): self._homepage = homepage @property def has_issues(self): return self._has_issues @has_issues.setter def has_issues(self, has_issues): self._has_issues = has_issues @property def has_wiki(self): return self._has_wiki @has_wiki.setter def has_wiki(self, has_wiki): self._has_wiki = has_wiki @property def can_comment(self): return self._can_comment @can_comment.setter def can_comment(self, can_comment): self._can_comment = can_comment @property def issue_comment(self): return self._issue_comment @issue_comment.setter def issue_comment(self, issue_comment): self._issue_comment = issue_comment @property def security_hole_enabled(self): return self._security_hole_enabled @security_hole_enabled.setter def security_hole_enabled(self, security_hole_enabled): self._security_hole_enabled = security_hole_enabled @property def private(self): return self._private @private.setter def private(self, private): self._private = private @property def path(self): return self._path @path.setter def path(self, path): self._path = path @property def default_branch(self): return self._default_branch @default_branch.setter def default_branch(self, default_branch): self._default_branch = default_branch @property def pull_requests_enabled(self): return self._pull_requests_enabled @pull_requests_enabled.setter def pull_requests_enabled(self, pull_requests_enabled): self._pull_requests_enabled = pull_requests_enabled @property def online_edit_enabled(self): return self._online_edit_enabled @online_edit_enabled.setter def online_edit_enabled(self, online_edit_enabled): self._online_edit_enabled = online_edit_enabled @property def lightweight_pr_enabled(self): return self._lightweight_pr_enabled @lightweight_pr_enabled.setter def lightweight_pr_enabled(self, lightweight_pr_enabled): self._lightweight_pr_enabled = lightweight_pr_enabled def to_dict(self): result = {} for attr, _ in six.iteritems(self.swagger_types): value = getattr(self, attr) if isinstance(value, list): result[attr] = list(map( lambda x: x.to_dict() if hasattr(x, "to_dict") else x, value )) elif hasattr(value, "to_dict"): result[attr] = value.to_dict() elif isinstance(value, dict): result[attr] = dict(map( lambda item: (item[0], item[1].to_dict()) if hasattr(item[1], "to_dict") else item, value.items() )) else: result[attr] = value if issubclass(Body19, dict): for key, value in self.items(): result[key] = value return result def to_str(self): return pprint.pformat(self.to_dict()) def __repr__(self): return self.to_str() def __eq__(self, other): if not isinstance(other, Body19): return False return self.__dict__ == other.__dict__ def __ne__(self, other): return not self == other
true
true
f705e8c12e46c20739a33fd39e478184c2fdb15c
4,875
py
Python
sigma_script.py
bettybhzhou/EasyMarkit_AI
028824a0af246d232013246bf1784013921beec3
[ "MIT" ]
2
2019-04-13T23:31:10.000Z
2019-04-14T00:45:43.000Z
sigma_script.py
pak-alex/EasyMarkit_AI
028824a0af246d232013246bf1784013921beec3
[ "MIT" ]
null
null
null
sigma_script.py
pak-alex/EasyMarkit_AI
028824a0af246d232013246bf1784013921beec3
[ "MIT" ]
3
2019-04-13T23:31:16.000Z
2019-04-20T01:30:54.000Z
""" Winning Python script for EasyMarkit Hackathon by Team Sigma """ ##Team Sigma - Members: Betty Zhou, Bailey Lei, Alex Pak # Usage: python sigma_script.py data/train.csv data/test.csv # import any necessary packages here #loading libraries import argparse import os import pandas as pd import numpy as np import sklearn from sklearn.model_selection import train_test_split import lightgbm as lgb # read in command line arguments parser = argparse.ArgumentParser() parser.add_argument("train_file_path") #path of training set parser.add_argument("test_file_path") #path of test set args = parser.parse_args() def onehot_drop(df, column_name): for index in column_name: one_hot = pd.get_dummies(df[index], prefix = index) df = df.drop(index,axis = 1) df = df.join(one_hot) return df def fit_train(df): train_df = df train_clean = onehot_drop(train_df, ['type', 'province']) train_clean['cli_area'] = train_clean['cli_area'].map({'Urban':1, 'Rural':0}) train_clean['pat_area'] = train_clean['pat_area'].map({'Urban':1, 'Rural':0}) train_clean['gender'] = train_clean['gender'].map({'M':1, 'F':0}) # convert to datetime train_clean['apt_date'] = pd.to_datetime(train_df.apt_date,format='%Y-%m-%d %H:%M:%S', utc =True) train_clean['sent_time'] = pd.to_datetime(train_df.sent_time,format='%Y-%m-%d %H:%M', utc =True) train_clean['send_time'] = pd.to_datetime(train_df.send_time, format='%H:%M:%S', utc =True).dt.time # find time between reminder and appointment train_clean['sent_to_apt'] = (train_clean['apt_date'] - train_clean['sent_time']).dt.total_seconds()/3600 # attributes train_clean['apt_month'] = train_clean['apt_date'].dt.month train_clean['sent_day_of_week'] = train_clean['sent_time'].dt.day_name() # one-hot encoding train_clean = onehot_drop(train_clean, ['sent_day_of_week']) X = train_clean.iloc[:, 2:] y = train_clean.iloc[:,1] X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.8, random_state=1) X_train_drop = X_train.drop(["apt_type", "apt_date", "sent_time", "send_time", "city", "cli_zip", 'pat_id', 'family_id','clinic'], axis = 1) X_test_drop = X_test.drop(["apt_type", "apt_date", "sent_time", "send_time", "city", "cli_zip", 'pat_id', 'family_id','clinic'], axis = 1) print("Number of training examples:", len(y_train)) print("Number of test examples:", len(y_test)) lg = lgb.LGBMClassifier(silent=False, n_estimators = 2000, max_depth=100) lg_model = lg.fit(X_train_drop, y_train) print("train accuracy: ", lg.score(X_train_drop, y_train)) print("test accuracy: ", lg.score(X_test_drop, y_test)) return lg_model def predict_test(test_df, lg_model): test_clean = onehot_drop(test_df, ['type', 'province']) test_clean['cli_area'] = test_clean['cli_area'].map({'Urban':1, 'Rural':0}) test_clean['pat_area'] = test_clean['pat_area'].map({'Urban':1, 'Rural':0}) test_clean['gender'] = test_clean['gender'].map({'M':1, 'F':0}) # convert to datetime test_clean['apt_date'] = pd.to_datetime(test_df.apt_date,format='%Y-%m-%d %H:%M:%S', utc =True) test_clean['sent_time'] = pd.to_datetime(test_df.sent_time,format='%Y-%m-%d %H:%M', utc =True) test_clean['send_time'] = pd.to_datetime(test_df.send_time, format='%H:%M:%S', utc =True).dt.time # find time between reminder and appointment test_clean['sent_to_apt'] = (test_clean['apt_date'] - test_clean['sent_time']).dt.total_seconds()/3600 # attributes test_clean['apt_month'] = test_clean['apt_date'].dt.month test_clean['sent_day_of_week'] = test_clean['sent_time'].dt.day_name() # one-hot encoding test_clean = onehot_drop(test_clean, ['sent_day_of_week']) test_clean_month = onehot_drop(test_clean, ['apt_month']) test_final = test_clean.iloc[:, 1:] test_final = test_final.drop(["apt_type", "apt_date", "sent_time", "send_time", "city", "cli_zip", 'pat_id', 'family_id','clinic'], axis = 1) print("Number of test examples:", len(test_df)) print("Number of final cleaned test examples:", len(test_final)) print("test data shape: ", test_final.shape) test_clean["response"] = lg_model.predict(test_final) df = test_clean[["ReminderId","response"]] return df def write_to_csv(df): group_name = "sigma" df.to_csv(group_name + "_output.csv", index=False) print(group_name + "_output.csv output successful") def main(): # loading train and test data train_df = pd.read_csv(args.train_file_path) test_df = pd.read_csv(args.test_file_path) # pre-processing input train and test data for training model lg_model = fit_train(train_df) #predict and write to new CSV for submission df = predict_test(test_df, lg_model) write_to_csv(df) if __name__ == "__main__": main()
38.385827
145
0.690051
import argparse import os import pandas as pd import numpy as np import sklearn from sklearn.model_selection import train_test_split import lightgbm as lgb parser = argparse.ArgumentParser() parser.add_argument("train_file_path") parser.add_argument("test_file_path") args = parser.parse_args() def onehot_drop(df, column_name): for index in column_name: one_hot = pd.get_dummies(df[index], prefix = index) df = df.drop(index,axis = 1) df = df.join(one_hot) return df def fit_train(df): train_df = df train_clean = onehot_drop(train_df, ['type', 'province']) train_clean['cli_area'] = train_clean['cli_area'].map({'Urban':1, 'Rural':0}) train_clean['pat_area'] = train_clean['pat_area'].map({'Urban':1, 'Rural':0}) train_clean['gender'] = train_clean['gender'].map({'M':1, 'F':0}) train_clean['apt_date'] = pd.to_datetime(train_df.apt_date,format='%Y-%m-%d %H:%M:%S', utc =True) train_clean['sent_time'] = pd.to_datetime(train_df.sent_time,format='%Y-%m-%d %H:%M', utc =True) train_clean['send_time'] = pd.to_datetime(train_df.send_time, format='%H:%M:%S', utc =True).dt.time train_clean['sent_to_apt'] = (train_clean['apt_date'] - train_clean['sent_time']).dt.total_seconds()/3600 train_clean['apt_month'] = train_clean['apt_date'].dt.month train_clean['sent_day_of_week'] = train_clean['sent_time'].dt.day_name() train_clean = onehot_drop(train_clean, ['sent_day_of_week']) X = train_clean.iloc[:, 2:] y = train_clean.iloc[:,1] X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.8, random_state=1) X_train_drop = X_train.drop(["apt_type", "apt_date", "sent_time", "send_time", "city", "cli_zip", 'pat_id', 'family_id','clinic'], axis = 1) X_test_drop = X_test.drop(["apt_type", "apt_date", "sent_time", "send_time", "city", "cli_zip", 'pat_id', 'family_id','clinic'], axis = 1) print("Number of training examples:", len(y_train)) print("Number of test examples:", len(y_test)) lg = lgb.LGBMClassifier(silent=False, n_estimators = 2000, max_depth=100) lg_model = lg.fit(X_train_drop, y_train) print("train accuracy: ", lg.score(X_train_drop, y_train)) print("test accuracy: ", lg.score(X_test_drop, y_test)) return lg_model def predict_test(test_df, lg_model): test_clean = onehot_drop(test_df, ['type', 'province']) test_clean['cli_area'] = test_clean['cli_area'].map({'Urban':1, 'Rural':0}) test_clean['pat_area'] = test_clean['pat_area'].map({'Urban':1, 'Rural':0}) test_clean['gender'] = test_clean['gender'].map({'M':1, 'F':0}) test_clean['apt_date'] = pd.to_datetime(test_df.apt_date,format='%Y-%m-%d %H:%M:%S', utc =True) test_clean['sent_time'] = pd.to_datetime(test_df.sent_time,format='%Y-%m-%d %H:%M', utc =True) test_clean['send_time'] = pd.to_datetime(test_df.send_time, format='%H:%M:%S', utc =True).dt.time test_clean['sent_to_apt'] = (test_clean['apt_date'] - test_clean['sent_time']).dt.total_seconds()/3600 test_clean['apt_month'] = test_clean['apt_date'].dt.month test_clean['sent_day_of_week'] = test_clean['sent_time'].dt.day_name() test_clean = onehot_drop(test_clean, ['sent_day_of_week']) test_clean_month = onehot_drop(test_clean, ['apt_month']) test_final = test_clean.iloc[:, 1:] test_final = test_final.drop(["apt_type", "apt_date", "sent_time", "send_time", "city", "cli_zip", 'pat_id', 'family_id','clinic'], axis = 1) print("Number of test examples:", len(test_df)) print("Number of final cleaned test examples:", len(test_final)) print("test data shape: ", test_final.shape) test_clean["response"] = lg_model.predict(test_final) df = test_clean[["ReminderId","response"]] return df def write_to_csv(df): group_name = "sigma" df.to_csv(group_name + "_output.csv", index=False) print(group_name + "_output.csv output successful") def main(): train_df = pd.read_csv(args.train_file_path) test_df = pd.read_csv(args.test_file_path) lg_model = fit_train(train_df) df = predict_test(test_df, lg_model) write_to_csv(df) if __name__ == "__main__": main()
true
true
f705e901b5b26c21aca2813bb5122e2cd9fef07b
1,541
py
Python
hddcoin/wallet/puzzles/load_clvm.py
JakubSido/hddcoin-blockchain
7b9da03edee3512295c0f142c07c4759512ccbca
[ "Apache-2.0" ]
null
null
null
hddcoin/wallet/puzzles/load_clvm.py
JakubSido/hddcoin-blockchain
7b9da03edee3512295c0f142c07c4759512ccbca
[ "Apache-2.0" ]
null
null
null
hddcoin/wallet/puzzles/load_clvm.py
JakubSido/hddcoin-blockchain
7b9da03edee3512295c0f142c07c4759512ccbca
[ "Apache-2.0" ]
null
null
null
import pathlib import pkg_resources from clvm_tools.clvmc import compile_clvm from hddcoin.types.blockchain_format.program import Program, SerializedProgram def load_serialized_clvm(clvm_filename, package_or_requirement=__name__) -> SerializedProgram: """ This function takes a .clvm file in the given package and compiles it to a .clvm.hex file if the .hex file is missing or older than the .clvm file, then returns the contents of the .hex file as a `Program`. clvm_filename: file name package_or_requirement: usually `__name__` if the clvm file is in the same package """ hex_filename = f"{clvm_filename}.hex" try: if pkg_resources.resource_exists(package_or_requirement, clvm_filename): full_path = pathlib.Path(pkg_resources.resource_filename(package_or_requirement, clvm_filename)) output = full_path.parent / hex_filename compile_clvm(full_path, output, search_paths=[full_path.parent]) except NotImplementedError: # pyinstaller doesn't support `pkg_resources.resource_exists` # so we just fall through to loading the hex clvm pass clvm_hex = pkg_resources.resource_string(package_or_requirement, hex_filename).decode("utf8") clvm_blob = bytes.fromhex(clvm_hex) return SerializedProgram.from_bytes(clvm_blob) def load_clvm(clvm_filename, package_or_requirement=__name__) -> Program: return Program.from_bytes(bytes(load_serialized_clvm(clvm_filename, package_or_requirement=package_or_requirement)))
40.552632
120
0.761843
import pathlib import pkg_resources from clvm_tools.clvmc import compile_clvm from hddcoin.types.blockchain_format.program import Program, SerializedProgram def load_serialized_clvm(clvm_filename, package_or_requirement=__name__) -> SerializedProgram: hex_filename = f"{clvm_filename}.hex" try: if pkg_resources.resource_exists(package_or_requirement, clvm_filename): full_path = pathlib.Path(pkg_resources.resource_filename(package_or_requirement, clvm_filename)) output = full_path.parent / hex_filename compile_clvm(full_path, output, search_paths=[full_path.parent]) except NotImplementedError: # so we just fall through to loading the hex clvm pass clvm_hex = pkg_resources.resource_string(package_or_requirement, hex_filename).decode("utf8") clvm_blob = bytes.fromhex(clvm_hex) return SerializedProgram.from_bytes(clvm_blob) def load_clvm(clvm_filename, package_or_requirement=__name__) -> Program: return Program.from_bytes(bytes(load_serialized_clvm(clvm_filename, package_or_requirement=package_or_requirement)))
true
true
f705e9a1b90c904ee70c6e8914a78070d0f5ea06
101,873
py
Python
dh_abstracts/app/abstracts/views.py
cmu-lib/dhweb_app
dcfc94b8df67e2a17c882b432fdeb52cfd98bf16
[ "MIT" ]
3
2021-05-07T17:01:33.000Z
2022-01-08T07:54:22.000Z
dh_abstracts/app/abstracts/views.py
cmu-lib/dhweb_app
dcfc94b8df67e2a17c882b432fdeb52cfd98bf16
[ "MIT" ]
336
2019-12-04T23:06:08.000Z
2022-03-31T16:23:15.000Z
dh_abstracts/app/abstracts/views.py
cmu-lib/dhweb_app
dcfc94b8df67e2a17c882b432fdeb52cfd98bf16
[ "MIT" ]
null
null
null
from django.shortcuts import render from django.http import HttpResponse, JsonResponse, StreamingHttpResponse, FileResponse from django.template import loader from django.shortcuts import get_object_or_404, render, redirect from django.views import View from django.views.generic import DetailView, ListView from django.db.models import ( Count, Max, Min, Q, F, Prefetch, Subquery, OuterRef, ExpressionWrapper, FloatField, BooleanField, ) from django.db.models.functions import Concat, FirstValue, Cast from django.core import management from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator from django.db.models.functions import Coalesce from django.contrib.postgres.search import SearchRank, SearchQuery from django.contrib.postgres.aggregates import StringAgg from django.urls import reverse, reverse_lazy from django.contrib import messages from django.contrib.messages.views import SuccessMessageMixin from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin from django.contrib.auth.decorators import login_required, user_passes_test from dal.autocomplete import Select2QuerySetView from django.views.generic.edit import CreateView, DeleteView, UpdateView from django.db import transaction, IntegrityError from django.forms.models import model_to_dict from django.forms import formset_factory, inlineformset_factory, modelformset_factory from django.conf import settings from django.utils.html import format_html from django.views.decorators.cache import cache_page import glob from os.path import basename, getmtime from datetime import datetime import csv import sys from operator import attrgetter from tempfile import NamedTemporaryFile, TemporaryDirectory import zipfile from . import models from .models import ( Work, WorkType, Author, Conference, Institution, Appellation, Affiliation, ConferenceSeries, SeriesMembership, Organizer, Country, Keyword, Topic, Language, CountryLabel, Authorship, License, ) from .forms import ( WorkFilter, AuthorFilter, AuthorMergeForm, WorkForm, WorkAuthorshipForm, FullInstitutionForm, InstitutionMergeForm, AffiliationEditForm, AffiliationMergeForm, KeywordMergeForm, TagForm, TopicMergeForm, AffiliationMultiMergeForm, KeywordMultiMergeForm, ConferenceForm, ConferenceCheckoutForm, ConferenceSeriesInline, LanguageMergeForm, WorkTypeMergeForm, InstitutionMultiMergeForm, TopicMultiMergeForm, ConferenceXMLUploadForm, ) PERMISSIONS_ERROR_TEXT = ( "Please contact the lead project editors to edit this part of the database." ) def cache_for_anon(func): """ On these views, call the cache if the user is not authenticated """ def wrap(request, *args, **kwargs): if request.user.is_authenticated: return func(request, *args, **kwargs) else: return cache_page(settings.CACHES["default"]["TIMEOUT"])(func)( request, *args, **kwargs ) return wrap def user_is_staff(func): def wrap(request, *args, **kwargs): if not request.user.is_authenticated: return redirect(f"{reverse('login')}?next={request.path}") if request.user.is_staff: return func(request, *args, **kwargs) else: messages.warning(request, PERMISSIONS_ERROR_TEXT) return redirect("home_view") return wrap class StaffRequiredMixin: def dispatch(self, *args, **kwargs): if not self.request.user.is_authenticated: return redirect(f"{reverse('login')}?next={self.request.path}") if self.request.user.is_staff: return super().dispatch(*args, **kwargs) else: messages.warning(self.request, PERMISSIONS_ERROR_TEXT) return redirect("home_view") class ItemLabelAutocomplete(Select2QuerySetView): def get_selected_result_label(self, item): return self.get_result_label(item) class WorkAutocomplete(ItemLabelAutocomplete): raise_exception = True def get_queryset(self): qs = Work.objects.all() parents_only = self.forwarded.get("parents_only", None) if parents_only: qs = qs.filter(work_type__is_parent=True) conference = self.forwarded.get("conference", None) if conference: qs = qs.filter(conference=conference) if self.q: qs = qs.filter(title__icontains=self.q) return qs.all() class AppellationAutocomplete(ItemLabelAutocomplete): raise_exception = True def get_queryset(self): qs = Appellation.objects.all() if self.q: qs = qs.filter( Q(first_name__icontains=self.q) | Q(last_name__icontains=self.q) ).all() return qs class KeywordAutocomplete(ItemLabelAutocomplete): raise_exception = True def get_queryset(self): qs = Keyword.objects.annotate(n_works=Count("works")).order_by("-n_works") if self.q: qs = qs.filter(title__icontains=self.q).all() return qs def get_result_label(self, item): return f"{item} ({item.n_works} works)" class LanguageAutocomplete(ItemLabelAutocomplete): raise_exception = True def get_queryset(self): qs = Language.objects.annotate(n_works=Count("works")).order_by( "-n_works", "title" ) if self.q: qs = qs.filter(title__icontains=self.q).all() return qs def get_result_label(self, item): return f"{item} ({item.n_works} works)" class TopicAutocomplete(ItemLabelAutocomplete): raise_exception = True def get_queryset(self): qs = Topic.objects.annotate(n_works=Count("works")).order_by("-n_works") if self.q: qs = qs.filter(title__icontains=self.q).all() return qs def get_result_label(self, item): return f"{item} ({item.n_works} works)" class CountryAutocomplete(ItemLabelAutocomplete): raise_exception = True def get_queryset(self): qs = Country.objects.annotate( n_works=Count( "institutions__affiliations__asserted_by__work", distinct=True ) ).order_by("-n_works") if self.q: qs = qs.filter( Q(pref_name__icontains=self.q) | Q(names__name__icontains=self.q) ) return qs.distinct() def get_result_label(self, item): return f"{item} ({item.n_works} works)" class InstitutionAutocomplete(ItemLabelAutocomplete): raise_exception = True def get_queryset(self): qs = ( Institution.objects.annotate( n_works=Count("affiliations__asserted_by__work", distinct=True) ) .select_related("country") .order_by("-n_works") ) if self.q: qs = qs.filter(name__icontains=self.q).all() return qs def get_result_label(self, item): if item.country is not None: c_label = item.country.pref_name else: c_label = "" location_statement = ", ".join( [l for l in [item.state_province_region, c_label] if l != ""] ) return f"{item} ({item.n_works} works)<br><small text-class='muted'>{location_statement}</small>" class AffiliationAutocomplete(ItemLabelAutocomplete): raise_exception = True def get_queryset(self): qs = ( Affiliation.objects.annotate( n_works=Count("asserted_by__work", distinct=True) ) .select_related("institution", "institution__country") .order_by("-n_works") ) inst_filter = self.forwarded.get("institution", None) if inst_filter: qs = qs.filter(institution=inst_filter) if self.q: qs = qs.filter( Q(department__icontains=self.q) | Q(institution__name__icontains=self.q) ).distinct() return qs def get_result_label(self, item): if item.institution.country is not None: c_label = item.institution.country.pref_name else: c_label = "" location_statement = ", ".join( [l for l in [item.institution.state_province_region, c_label] if l != ""] ) return f"{item} ({item.n_works} works)<br><small text-class='muted'>{location_statement}</small>" class ConferenceAutocomplete(ItemLabelAutocomplete): raise_exception = True def get_queryset(self): qs = Conference.objects.annotate( main_series=StringAgg( "series_memberships__series__abbreviation", delimiter=" / ", distinct=True, ) ).order_by("year", "main_series", "short_title", "theme_title") if self.q: qs = qs.filter(search_text__icontains=self.q).distinct() return qs def get_result_label(self, item): if item.main_series: return f"{item.main_series} - {item.year} - {item.short_title}" elif item.short_title: return f"{item.year} - {item.short_title}" else: return f"{item.year} - {item.theme_title}" class AuthorAutocomplete(ItemLabelAutocomplete): raise_exception = True def get_queryset(self): qs = Author.objects.annotate( n_works=Count("authorships", distinct=True), main_last_name=Max("appellations__last_name"), main_first_name=Max("appellations__first_name"), ).order_by("main_last_name", "main_first_name", "-n_works") if self.q: qs = qs.filter(appellations_index__icontains=self.q).distinct() return qs def get_result_label(self, item): return format_html( f"{item.most_recent_appellation} ({item.n_works} works)<br><small text-class='muted'>(All names: {item.appellations_index})</small>" ) def work_view(request, work_id): related_conference = Conference.objects.annotate( n_works=Count("works", distinct=True), n_authors=Count("works__authors", distinct=True), main_series=StringAgg( "series_memberships__series__abbreviation", delimiter=" / ", distinct=True ), ).prefetch_related("series", "organizers") work = get_object_or_404( Work.objects.select_related("work_type", "full_text_license").prefetch_related( Prefetch("conference", queryset=related_conference), "keywords", "topics", "languages", Prefetch( "session_papers", queryset=Work.objects.prefetch_related( Prefetch( "authorships", queryset=Authorship.objects.select_related("appellation"), ), ), ), Prefetch( "parent_session", queryset=Work.objects.prefetch_related( Prefetch( "authorships", queryset=Authorship.objects.select_related( "author", "appellation" ), ) ), ), ), pk=work_id, ) authorships = ( Authorship.objects.filter(work_id=work_id) .order_by("authorship_order") .distinct() .select_related("work", "author", "appellation") .prefetch_related( Prefetch( "affiliations", queryset=Affiliation.objects.select_related( "institution", "institution__country" ), ) ) ) context = {"work": work, "authorships": authorships} return render(request, "work_detail.html", context) def author_view(request, author_id): author = get_object_or_404(Author, pk=author_id) sorted_authorships = ( Authorship.objects.filter(author=author) .order_by("work__conference__year") .prefetch_related( Prefetch("work", queryset=Work.objects.select_related("conference")) ) ) appellations = ( Appellation.objects.filter(asserted_by__author=author) .distinct() .annotate(latest_year=Max("asserted_by__work__conference__year")) .order_by("-latest_year") .prefetch_related(Prefetch("asserted_by", queryset=sorted_authorships)) ) affiliations = ( Affiliation.objects.filter(asserted_by__author=author) .distinct() .annotate(latest_year=Max("asserted_by__work__conference__year")) .order_by("-latest_year") .prefetch_related( Prefetch("asserted_by", queryset=sorted_authorships), Prefetch( "institution", queryset=Institution.objects.select_related("country") ), ) ) works = ( Work.objects.filter(authorships__author=author) .order_by("conference__year") .distinct() .select_related("conference", "parent_session", "work_type") .prefetch_related( Prefetch( "conference", queryset=Conference.objects.prefetch_related("series", "organizers"), ), "session_papers", "keywords", "topics", "languages", Prefetch( "authorships", queryset=Authorship.objects.select_related("appellation", "author"), ), ) ) author_admin_page = reverse("admin:abstracts_author_change", args=(author.pk,)) context = { "author": author, "works": works, "appellations": appellations, "affiliations": affiliations, "author_admin_page": author_admin_page, } return render(request, "author_detail.html", context) class AuthorSplit(DetailView, StaffRequiredMixin): model = Author template_name = "author_split.html" context_object_name = "original_author" def get_context_data(self, **kwargs): authorships = Authorship.objects.filter(author=self.get_object()).order_by( "work__conference__year" ) return {self.context_object_name: self.get_object(), "authorships": authorships} @transaction.atomic def post(self, request, *args, **kwargs): """ Create new author and transfer authorships """ authorships_to_move = request.POST.getlist("splitselect") try: new_author = Author.objects.create() Authorship.objects.filter(id__in=authorships_to_move).update( author=new_author ) # Force-update appellations self.get_object().save() new_author.save() messages.success( request, f"{len(authorships_to_move)} authorships moved to new author id {new_author.id}", ) return redirect("author_detail", new_author.id) except: messages.error(request, str(authorships_to_move)) return redirect("author_split", self.get_object().id) class XMLView(DetailView, LoginRequiredMixin): model = Work context_object_name = "work" def get(self, request, *args, **kwargs): response = HttpResponse(self.get_object().full_text, content_type="xhtml+xml") response[ "Content-Disposition" ] = f"attachment; filename={self.get_object().id}.xml" return response class AuthorList(ListView): context_object_name = "author_list" template_name = "author_list.html" paginate_by = 50 def get_queryset(self): base_result_set = Author.objects.exclude(appellations__isnull=True).annotate( n_conferences=Count("works__conference", distinct=True) ) raw_filter_form = AuthorFilter(self.request.GET) if raw_filter_form.is_valid(): filter_form = raw_filter_form.cleaned_data order_res = filter_form["ordering"] if order_res is None or order_res == "": order_res = "last_name" result_set = base_result_set.annotate( last_name=Max("appellations__last_name"), n_works=Count("authorships", distinct=True), ).order_by(order_res) author_res = filter_form["author"] if author_res is not None: result_set = result_set.filter(id=author_res.id) affiliation_res = filter_form["affiliation"] if affiliation_res is not None: result_set = result_set.filter( authorships__affiliations=affiliation_res ) institution_res = filter_form["institution"] if institution_res is not None: result_set = result_set.filter( authorships__affiliations__institution=institution_res ) country_res = filter_form["country"] if country_res is not None: result_set = result_set.filter( authorships__affiliations__institution__country=country_res ) conference_res = filter_form["conference"] if conference_res is not None: result_set = result_set.filter(works__conference=conference_res) if filter_form["singleton"]: result_set = result_set.filter(n_conferences=1) name_res = filter_form["name"] if name_res != "": result_set = result_set.filter(appellations_index__icontains=name_res) first_name_res = filter_form["first_name"] if first_name_res != "": result_set = result_set.filter( authorships__appellation__first_name__icontains=first_name_res ) last_name_res = filter_form["last_name"] if last_name_res != "": result_set = result_set.filter( authorships__appellation__last_name__icontains=last_name_res ) # Newest affiliations newest_authorship = Authorship.objects.filter( author=OuterRef("pk") ).order_by("-work__conference__year") annotated_authors = result_set.annotate( main_affiliation_department=Subquery( newest_authorship.values("affiliations__department")[:1] ), main_affiliation_institution=Subquery( newest_authorship.values("affiliations__institution__name")[:1] ), main_affiliation_institution_city=Subquery( newest_authorship.values("affiliations__institution__city")[:1] ), main_affiliation_institution_state=Subquery( newest_authorship.values( "affiliations__institution__state_province_region" )[:1] ), main_affiliation_institution_country=Subquery( newest_authorship.values( "affiliations__institution__country__pref_name" )[:1] ), most_recent_first_name=Subquery( newest_authorship.values("appellation__first_name")[:1] ), most_recent_last_name=Subquery( newest_authorship.values("appellation__last_name")[:1] ), n_works=Count("authorships", distinct=True), ) return annotated_authors else: messages.warning( self.request, "Query parameters not recognized. Check your URL and try again.", ) return base_result_set def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context["author_filter_form"] = AuthorFilter(data=self.request.GET) context["available_authors_count"] = Author.objects.count() context["redirect_url"] = reverse("author_list") return context def annotate_multiple_series(qs): return qs.annotate( n_conferences=Count("conferences", distinct=True), earliest_year=Min("conferences__year"), latest_year=Max("conferences__year"), n_complete=Count( "conferences", filter=Q(conferences__entry_status="c"), distinct=True ), n_in_progress=Count( "conferences", filter=Q(conferences__entry_status="i"), distinct=True ), n_in_review=Count( "conferences", filter=Q(conferences__entry_status="r"), distinct=True ), n_remaining=F("n_conferences") - F("n_complete") - F("n_in_progress") - F("n_in_review"), pct_complete=( Cast(F("n_complete"), FloatField()) / Cast(F("n_conferences"), FloatField()) ) * 100, pct_in_progress=( Cast(F("n_in_progress"), FloatField()) / Cast(F("n_conferences"), FloatField()) ) * 100, pct_in_review=( Cast(F("n_in_review"), FloatField()) / Cast(F("n_conferences"), FloatField()) ) * 100, pct_remaining=( Cast(F("n_remaining"), FloatField()) / Cast(F("n_conferences"), FloatField()) ) * 100, ).order_by("title") def annotate_single_series(qs): res = qs.aggregate( earliest_year=Min("year"), latest_year=Max("year"), n_conferences=Count("id", distinct=True), n_complete=Count("id", filter=Q(entry_status="c"), distinct=True), n_in_progress=Count("id", filter=Q(entry_status="i"), distinct=True), n_in_review=Count("id", filter=Q(entry_status="r"), distinct=True), ) res["n_remaining"] = ( res["n_conferences"] - res["n_complete"] - res["n_in_progress"] - res["n_in_review"] ) if res["n_conferences"] > 0: res["pct_complete"] = (res["n_complete"] / res["n_conferences"]) * 100 res["pct_in_progress"] = (res["n_in_progress"] / res["n_conferences"]) * 100 res["pct_in_review"] = (res["n_in_review"] / res["n_conferences"]) * 100 res["pct_remaining"] = (res["n_remaining"] / res["n_conferences"]) * 100 else: res["pct_complete"] = 0 res["pct_in_progress"] = 0 res["pct_in_review"] = 0 res["pct_remaining"] = 0 return res def conference_series_qs(): return annotate_multiple_series( ConferenceSeries.objects.exclude(conferences__isnull=True) ) class ConferenceSeriesList(ListView): context_object_name = "series_list" template_name = "conference_series_list.html" def get_queryset(self): base_result_set = conference_series_qs() return base_result_set def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) sa_conf = Conference.objects.filter(series__isnull=True) context["standalone_conferences"] = annotate_single_series(sa_conf) context["standalone_conference_count"] = sa_conf.count() return context class ConferenceSeriesDetail(DetailView): model = ConferenceSeries template_name = "conference_series_detail.html" context_object_name = "series" def get_member_conferences(self): return Conference.objects.filter(series_memberships__series=self.get_object()) def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context["series_progress"] = annotate_single_series( self.get_member_conferences() ) series_order_subquery = SeriesMembership.objects.filter( conference=OuterRef("pk"), series=self.get_object() ).order_by("number") context["conference_list"] = ( self.get_member_conferences() .annotate( main_series=StringAgg( "series_memberships__series__abbreviation", delimiter=" / ", distinct=True, ), n_works=Count("works", distinct=True), n_authors=Count("works__authors", distinct=True), series_order=Subquery(series_order_subquery.values("number")[:1]), ) .order_by("series_order") .prefetch_related( "series_memberships", "series_memberships__series", "organizers", "country", "hosting_institutions", "hosting_institutions__country", "documents", ) ) context["series_list"] = conference_series_qs() return context class StandaloneList(View): template_name = "conference_series_detail.html" def get_standalone_list(self): qs = ( Conference.objects.filter(series__isnull=True) .annotate( main_series=StringAgg( "series_memberships__series__abbreviation", delimiter=" / ", distinct=True, ), n_works=Count("works", distinct=True), n_authors=Count("works__authors", distinct=True), ) .order_by("year", "short_title", "theme_title") .prefetch_related( "series_memberships", "series_memberships__series", "organizers", "country", "hosting_institutions", "hosting_institutions__country", "documents", ) ) return qs def get(self, request): faux_series = { "title": "Standalone Events", "notes": "Digital humanities events not belonging to a larger series, such symposia or workshops.", "n_conferences": self.get_standalone_list().count(), } context = { "conference_list": self.get_standalone_list(), "series": faux_series, "series_list": conference_series_qs(), "series_progress": annotate_single_series(self.get_standalone_list()), } return render(request, self.template_name, context) def home_view(request): conference_count = Conference.objects.count() years_count = Conference.objects.aggregate(year_range=Max("year") - Min("year"))[ "year_range" ] work_count = Work.objects.count() author_count = Author.objects.exclude(authorships__work__isnull=True).count() institution_count = Institution.objects.count() country_count = ( Country.objects.filter( Q(institutions__affiliations__asserted_by__work__isnull=False) | Q(institutions__conferences__isnull=False) | Q(conferences__isnull=False) ) .distinct() .count() ) context = { "site": { "conference_count": conference_count, "years_count": years_count, "work_count": work_count, "author_count": author_count, "institution_count": institution_count, "country_count": country_count, } } return render(request, "index.html", context) @user_is_staff @transaction.atomic def author_merge_view(request, author_id): author = get_object_or_404(Author, pk=author_id) if request.method == "GET": """ Initial load of the merge form displays all the authorships of the current author that will be affected """ context = {"merging": author, "author_merge_form": AuthorMergeForm} return render(request, "author_merge.html", context) elif request.method == "POST": """ Posting the new author id causes all of the old author's authorships to be reassigned. """ raw_form = AuthorMergeForm(request.POST) if raw_form.is_valid(): target_author = raw_form.cleaned_data["into"] if author == target_author: """ If the user chooses the existing author, don't merge, but instead error out. """ messages.error( request, f"You cannot merge an author into themselves. Please select a different author.", ) return redirect("author_merge", author_id=author_id) else: old_author_string = str(author) merge_results = author.merge(target_author) target_author.user_last_updated = request.user target_author.save() messages.success( request, f"Author {old_author_string} has been merged into {target_author}, and the old author entry has been deleted.", ) messages.success( request, f"{merge_results['update_results']} authorships updated" ) return redirect("author_detail", author_id=target_author.pk) else: for error in raw_form.errors: messages.error(request, error) return render(request, "author_merge.html", context) def field_required(field): if field.get_internal_type() in ("CharField", "TextField") and field.blank: return False if field.null: return False return True def download_data(request): data_dictionary = [] if request.user.is_authenticated: dt_config = settings.PRIVATE_DATA_TABLE_CONFIG zip_url = reverse("private_all_tables_download") else: dt_config = settings.PUBLIC_DATA_TABLE_CONFIG zip_url = reverse("public_all_tables_download") denormalized_url = reverse("works_download") denormalized_last_updated = datetime.fromtimestamp( getmtime(f"{settings.DATA_OUTPUT_PATH}/{settings.DENORMALIZED_WORKS_NAME}.zip") ) for m in dt_config["CONFIGURATION"]: model = attrgetter(m["model"])(models) if "manual_model_description" in m: model_description = m["manual_model_description"] else: try: model_description = model.model_description except: model_description = None all_model_fields = [ { "name": f.name, "relation": f.is_relation, "help_text": f.help_text, "related_model": str(f.related_model) .replace("<class 'abstracts.models.", "") .replace("'>", ""), "type": f.get_internal_type(), "required": field_required(f), } for f in model._meta.fields if not f.one_to_many and f.name not in m["exclude_fields"] ] if m.get("include_string", False): all_model_fields.append( { "name": "label", "relation": None, "help_text": "General label for this object", "related_model": None, "type": "CharField", "required": True, } ) data_dictionary.append( { "model": m["model"], "model_description": model_description, "csv_name": m["csv_name"], "fields": all_model_fields, } ) normalized_last_updated = datetime.fromtimestamp( getmtime(f"{settings.DATA_OUTPUT_PATH}/{dt_config['DATA_ZIP_NAME']}") ) context = { "zip_url": zip_url, "denormalized_url": denormalized_url, "denormalized_last_updated": denormalized_last_updated, "normalized_last_updated": normalized_last_updated, "data_dictionary": data_dictionary, "denormalized_data_dictionary": settings.DENORMALIZED_HEADERS, } return render(request, "downloads.html", context) def download_works_csv(request): target_zip = f"{settings.DATA_OUTPUT_PATH}/{settings.DENORMALIZED_WORKS_NAME}.zip" response = FileResponse(open(target_zip, "rb")) return response def public_download_all_tables(request): target_zip = f"{settings.DATA_OUTPUT_PATH}/{settings.PUBLIC_DATA_TABLE_CONFIG['DATA_ZIP_NAME']}" response = FileResponse(open(target_zip, "rb")) return response @login_required def private_download_all_tables(request): target_zip = f"{settings.DATA_OUTPUT_PATH}/{settings.PRIVATE_DATA_TABLE_CONFIG['DATA_ZIP_NAME']}" response = FileResponse(open(target_zip, "rb")) return response @login_required def WorkCreate(request): if request.method == "GET": if "conference" in request.GET: conf = get_object_or_404(Conference, pk=int(request.GET["conference"])) work_form = WorkForm(initial={"conference": conf.pk}) else: work_form = WorkForm() if request.method == "POST": work_form = WorkForm(request.POST) if work_form.is_valid(): new_work = work_form.save() new_work.user_last_updated = request.user new_work.save() messages.success(request, f"{new_work} created.") return redirect("work_edit_authorship", work_id=new_work.pk) else: for err in work_form.errors: messages.error(request, err) context = {"work_form": work_form} return render(request, "work_create.html", context) @login_required def WorkEdit(request, work_id): work = get_object_or_404(Work, pk=work_id) if request.method == "POST": work_form = WorkForm(request.POST, instance=work) if work_form.is_valid(): work.user_last_updated = request.user work_form.save() messages.success(request, f'"{work.title}" sucessfully updated.') return redirect("work_detail", work_id=work.pk) else: for f, e in work_form.errors.items(): messages.error(request, f"{f}: {e}") work_initial_data = model_to_dict(work) context = {"work_form": WorkForm(initial=work_initial_data), "work": work} return render(request, "work_edit.html", context) @login_required @transaction.atomic def WorkEditAuthorship(request, work_id): work = get_object_or_404(Work, pk=work_id) authorships = work.authorships.all() AuthorshipWorkFormset = formset_factory( WorkAuthorshipForm, can_delete=True, extra=0 ) initial_data = [] for authorship in authorships: base_data = { "author": authorship.author, "authorship_order": authorship.authorship_order, "first_name": authorship.appellation.first_name, "last_name": authorship.appellation.last_name, "affiliations": [aff for aff in authorship.affiliations.all()], } initial_data.append(base_data) if request.method == "GET": authorships_forms = AuthorshipWorkFormset(initial=initial_data) elif request.method == "POST": authorships_forms = AuthorshipWorkFormset(request.POST) if authorships_forms.is_valid(): for d_form in authorships_forms.deleted_forms: d_form_data = d_form.cleaned_data attached_author = d_form_data["author"] Authorship.objects.filter( work=work, author=d_form_data["author"] ).delete() # Refresh the author in DB to update appellations index attached_author.save() for aform in authorships_forms: if aform not in authorships_forms.deleted_forms: aform_data = aform.cleaned_data appellation = Appellation.objects.get_or_create( first_name=aform_data["first_name"], last_name=aform_data["last_name"], )[0] affiliations = aform_data["affiliations"] authorship_order = aform_data["authorship_order"] try: if aform_data["author"] is None: author_id = Author.objects.create() else: author_id = aform_data["author"] auth = Authorship.objects.update_or_create( work=work, author=author_id, defaults={ "authorship_order": authorship_order, "appellation": appellation, "user_last_updated": request.user, }, )[0] author_id.user_last_updated = request.user author_id.save() except IntegrityError as e: messages.error( request, f"{e}: Ensure authorship order numbers are unique" ) return redirect("work_edit_authorship", work.pk) auth.affiliations.clear() if affiliations is not None: auth.affiliations.set(affiliations) messages.success( request, f'"{work.title}" authorships successfully updated.' ) if "start_new" in request.POST: return redirect( f"{reverse('work_create')}?conference={work.conference.pk}" ) return redirect("work_detail", work_id=work.pk) else: for error in authorships_forms.errors: messages.error(request, error) context = { "authorships_form": authorships_forms, "work": work, "affiliation_form": AffiliationEditForm, } return render(request, "work_edit_authorships.html", context) @login_required def AuthorInfoJSON(request, author_id): if request.method == "GET": author = get_object_or_404(Author, pk=author_id) author_aff = Affiliation.objects.filter(asserted_by__author=author).distinct() author_dict = { "first_name": author.most_recent_appellation.first_name, "last_name": author.most_recent_appellation.last_name, "work_titles": [w.title for w in author.works.all()][:4], "works_count": author.works.count(), } if author_aff is not None: author_dict["affiliations"] = [ {"name": str(aff), "id": aff.pk} for aff in author_aff ] return JsonResponse(author_dict) @login_required def AffiliationInfoJSON(request, affiliation_id): if request.method == "GET": affiliation = get_object_or_404(Affiliation, pk=affiliation_id) affiliation_dict = { "institution": { "name": str(affiliation.institution), "id": affiliation.institution.id, } } if affiliation.department is not None: affiliation_dict["department"] = affiliation.department return JsonResponse(affiliation_dict) class WorkDelete(LoginRequiredMixin, SuccessMessageMixin, DeleteView): model = Work template_name = "work_delete.html" extra_context = {"cancel_view": "work_list"} success_url = reverse_lazy("work_list") def delete(self, request, *args, **kwargs): messages.success(self.request, f"'{self.get_object().title}' deleted") return super().delete(request, *args, **kwargs) class FullWorkList(ListView): context_object_name = "work_list" template_name = "work_list.html" paginate_by = 10 def get_queryset(self): base_result_set = Work.objects.all() raw_filter_form = WorkFilter(self.request.GET) if raw_filter_form.is_valid(): result_set = base_result_set filter_form = raw_filter_form.cleaned_data work_type_res = filter_form["work_type"] if work_type_res is not None: result_set = result_set.filter(work_type=work_type_res) conference_res = filter_form["conference"] if conference_res is not None: result_set = result_set.filter(conference=conference_res) affiliation_res = filter_form["affiliation"] if len(affiliation_res) > 0: result_set = result_set.filter( authorships__affiliations__in=affiliation_res ).distinct() institution_res = filter_form["institution"] if len(institution_res) > 0: result_set = result_set.filter( authorships__affiliations__institution__in=institution_res ).distinct() author_res = filter_form["author"] if len(author_res) > 0: result_set = result_set.filter(authorships__author__in=author_res) keyword_res = filter_form["keywords"] if len(keyword_res) > 0: result_set = result_set.filter(keywords__in=keyword_res) topic_res = filter_form["topics"] if len(topic_res) > 0: result_set = result_set.filter(topics__in=topic_res) language_res = filter_form["languages"] if len(language_res) > 0: result_set = result_set.filter(languages__in=language_res) if filter_form["full_text_available"]: result_set = result_set.exclude(full_text="") if filter_form["full_text_viewable"]: result_set = result_set.exclude(full_text="").filter( full_text_license__isnull=False ) text_res = filter_form["text"] if text_res != "": text_query = SearchQuery(text_res, search_type="websearch") result_set = ( result_set.filter(search_text=text_query) .annotate( rank=SearchRank( F("search_text"), text_query, ), # Does the search text show up only in the full text? search_in_ft_only=ExpressionWrapper( ~Q(title__icontains=text_res), output_field=BooleanField() ), ) .filter(rank__gt=0.1) .order_by("-rank") ) order_res = "rank" # To find the last name of the first author, we develop a subquery that will pull the first authorship for a given work. We can then call the appellation__last_name first_author_subquery = Authorship.objects.filter( work=OuterRef("pk") ).order_by("authorship_order") order_res = filter_form["ordering"] if order_res is None or order_res == "": order_res = "year" if order_res == "year": result_set = result_set.order_by("conference__year", "title") elif order_res == "-year": result_set = result_set.order_by("-conference__year", "title") elif order_res == "title": result_set = result_set.order_by("title") elif order_res == "-title": result_set = result_set.order_by("-title") elif order_res == "last_name": result_set = result_set.annotate( first_author_last_name=Subquery( first_author_subquery.values("appellation__last_name")[:1] ) ).order_by("first_author_last_name", "title") elif order_res == "-last_name": result_set = result_set.annotate( first_author_last_name=Subquery( first_author_subquery.values("appellation__last_name")[:1] ) ).order_by("-first_author_last_name", "title") return ( result_set.select_related( "conference", "work_type", "parent_session", "full_text_license" ) .annotate( main_series=StringAgg( "conference__series_memberships__series__abbreviation", delimiter=" / ", distinct=True, ), main_institution=StringAgg( "conference__hosting_institutions__name", delimiter=" / ", distinct=True, ), ) .prefetch_related( Prefetch( "conference", queryset=Conference.objects.prefetch_related( Prefetch( "series_memberships", queryset=SeriesMembership.objects.select_related( "series" ), ), "organizers", ), ), "session_papers", Prefetch( "authorships", queryset=Authorship.objects.select_related( "appellation", "author" ), ), "keywords", "topics", "languages", ) ) else: for error in raw_filter_form.errors: messages.warning(self.request, error) return base_result_set def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) raw_filter_form = WorkFilter(self.request.GET) if raw_filter_form.is_valid(): filter_form = raw_filter_form.cleaned_data conference_res = filter_form["conference"] if conference_res is not None: conferences_data = ( Conference.objects.filter(id=conference_res.id) .annotate( n_works=Count("works", distinct=True), n_authors=Count("works__authors", distinct=True), main_series=StringAgg( "series_memberships__series__abbreviation", delimiter=" / ", distinct=True, ), ) .select_related("country") .prefetch_related( "organizers", "series_memberships", "series_memberships__series" ) .all() ) context["selected_conferences"] = conferences_data context["work_filter_form"] = WorkFilter(data=self.request.GET) context["available_works_count"] = Work.objects.count() context["filtered_works_count"] = self.get_queryset().count() context["redirect_url"] = reverse("work_list") return context class FullInstitutionList(LoginRequiredMixin, ListView): context_object_name = "institution_list" template_name = "full_institution_list.html" paginate_by = 10 def get_queryset(self): annotated_affiliations = Affiliation.objects.annotate( n_works=Count("asserted_by__work", distinct=True) ) result_set = ( Institution.objects.annotate( n_works=Count("affiliations__asserted_by__work", distinct=True) ) .prefetch_related( Prefetch("affiliations", annotated_affiliations), "country" ) .order_by("-n_works") ) if self.request.GET: raw_filter_form = FullInstitutionForm(self.request.GET) if raw_filter_form.is_valid(): filter_form = raw_filter_form.cleaned_data result_set = result_set.annotate( n_conferences=Count( "affiliations__asserted_by__work__conference", distinct=True ) ) department_res = filter_form["department"] if department_res != "": result_set = result_set.filter( affiliations__department__icontains=department_res ) affiliation_res = filter_form["affiliation"] if affiliation_res is not None: result_set = result_set.filter(affiliations=affiliation_res) institution_res = filter_form["institution"] if institution_res is not None: result_set = result_set.filter(pk=institution_res.pk) country_res = filter_form["country"] if country_res is not None: result_set = result_set.filter(country=country_res) if filter_form["no_department"]: result_set = result_set.filter(affiliations__department="") conference_res = filter_form["conference"] if conference_res is not None: result_set = result_set.filter( affiliations__asserted_by__work__conference=conference_res ).distinct() if filter_form["singleton"]: result_set = result_set.filter(n_conferences=1) if filter_form["ordering"] == "n_dsc": result_set = result_set.order_by( "-n_works", "affiliations__institution__name" ) elif filter_form["ordering"] == "n_asc": result_set = result_set.order_by( "n_works", "affiliations__institution__name" ) elif filter_form["ordering"] == "a": result_set = result_set.order_by("affiliations__institution__name") else: for f, e in raw_filter_form.errors.items(): messages.error(self.request, f"{f}: {e}") return result_set def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context["institution_filter_form"] = FullInstitutionForm( initial=self.request.GET ) context["available_institutions_count"] = Institution.objects.count() context["filtered_institutions_count"] = self.get_queryset().count() context["redirect_url"] = reverse("full_institution_list") return context class AuthorInstitutionList(FullInstitutionList): template_name = "author_institution_list.html" def get_queryset(self): base_result_set = Institution.objects.annotate( n_authors=Count("affiliations__asserted_by__author", distinct=True), n_conferences=Count( "affiliations__asserted_by__work__conference", distinct=True ), ).distinct() result_set = base_result_set if self.request.GET: raw_filter_form = FullInstitutionForm(self.request.GET) if raw_filter_form.is_valid(): filter_form = raw_filter_form.cleaned_data department_res = filter_form["department"] if department_res != "": result_set = result_set.filter( affiliations__department__icontains=department_res ) affiliation_res = filter_form["affiliation"] if affiliation_res is not None: result_set = result_set.filter(affiliations=affiliation_res) institution_res = filter_form["institution"] if institution_res is not None: result_set = result_set.filter(pk=institution_res.pk) conference_res = filter_form["conference"] if conference_res is not None: result_set = result_set.filter(works__conference=conference_res) country_res = filter_form["country"] if country_res is not None: result_set = result_set.filter(country=country_res) if filter_form["singleton"]: result_set = result_set.filter(n_conferences=1) if filter_form["no_department"]: result_set = result_set.filter(affiliations__department="") if filter_form["ordering"] == "n_dsc": result_set = result_set.order_by("-n_authors") elif filter_form["ordering"] == "n_asc": result_set = result_set.order_by("n_authors") elif filter_form["ordering"] == "a": result_set = result_set.order_by("affiliations__institution__name") else: for f, e in raw_filter_form.errors.items(): messages.error(self.request, f"{f}: {e}") result_set = base_result_set else: # Otherwise default to sorting by n_dsc result_set = result_set.order_by("-n_authors") return result_set.distinct() def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context["redirect_url"] = reverse("author_institution_list") return context class InstitutionEdit(LoginRequiredMixin, SuccessMessageMixin, UpdateView): model = Institution template_name = "generic_form.html" fields = ["name", "city", "state_province_region", "country"] extra_context = { "form_title": "Edit institution", "cancel_view": "full_institution_list", "merge_view": "institution_merge", } success_message = "%(name)s updated" success_url = reverse_lazy("full_institution_list") def form_valid(self, form): response = super(InstitutionEdit, self).form_valid(form) self.object.user_last_updated = self.request.user self.object.save() return response class InstitutionCreate(LoginRequiredMixin, SuccessMessageMixin, CreateView): model = Institution template_name = "generic_form.html" fields = ["name", "city", "state_province_region", "country"] extra_context = { "form_title": "Create institution", "cancel_view": "full_institution_list", } success_message = "%(name)s created" success_url = reverse_lazy("full_institution_list") def form_valid(self, form): response = super(InstitutionCreate, self).form_valid(form) self.object.user_last_updated = self.request.user self.object.save() return response @user_is_staff @transaction.atomic def institution_merge(request, institution_id): institution = get_object_or_404(Institution, pk=institution_id) context = {"merging": institution, "institution_merge_form": InstitutionMergeForm} if request.method == "GET": """ Initial load of the merge form displays all the authors and works associated with this institution. """ return render(request, "institution_merge.html", context) elif request.method == "POST": """ Posting the new author id causes all of the old author's authorships to be reassigned. """ raw_form = InstitutionMergeForm(request.POST) if raw_form.is_valid(): target_institution = raw_form.cleaned_data["into"] if institution == target_institution: """ If the user chooses the existing institution, don't merge, but instead error out. """ messages.error( request, f"You cannot merge an institution into itself. Please select a different institution.", ) return redirect("institution_merge", institution_id=institution_id) else: old_institution_id = str(institution) merge_results = institution.merge(target_institution) target_institution.user_last_updated = request.user target_institution.save() messages.success( request, f"Author {old_institution_id} has been merged into {target_institution}, and the old institution entry has been deleted.", ) messages.success( request, f"{merge_results['update_results']} affiliations updated" ) return redirect("institution_edit", pk=target_institution.pk) else: for error in raw_form.errors: messages.error(request, error) return render(request, "institution_merge.html", context) @user_is_staff @transaction.atomic def institution_multi_merge(request): context = {"form": InstitutionMultiMergeForm} if request.method == "POST": raw_form = InstitutionMultiMergeForm(request.POST) if raw_form.is_valid(): target_institution = raw_form.cleaned_data["into"] source_institutions = raw_form.cleaned_data["sources"].exclude( pk=target_institution.pk ) for institution in source_institutions: old_institution_id = str(institution) merge_results = institution.merge(target_institution) target_institution.user_last_updated = request.user target_institution.save() messages.success( request, f"Institution {old_institution_id} has been merged into {target_institution}, and the old institution entry has been deleted.", ) messages.success( request, f"{merge_results['update_results']} institutions updated" ) return redirect("institution_edit", pk=target_institution.pk) else: for error in raw_form.errors: messages.error(request, error) return render(request, "institution_multi_merge.html", context) class AffiliationEdit(LoginRequiredMixin, SuccessMessageMixin, UpdateView): model = Affiliation template_name = "generic_form.html" form_class = AffiliationEditForm extra_context = { "form_title": "Edit affiliation", "cancel_view": "full_institution_list", "merge_view": "affiliation_merge", } success_message = "%(department)s updated" success_url = reverse_lazy("full_institution_list") class AffiliationCreate(LoginRequiredMixin, SuccessMessageMixin, CreateView): model = Affiliation template_name = "generic_form.html" form_class = AffiliationEditForm extra_context = { "form_title": "Create affiliation", "cancel_view": "full_institution_list", } success_message = "%(department)s created" success_url = reverse_lazy("full_institution_list") def get_initial(self, **kwargs): super().get_initial(**kwargs) if "institution" in self.request.GET: self.initial = {"institution": int(self.request.GET["institution"])} return self.initial @login_required def ajax_affiliation_create(request): newaff = Affiliation.objects.get_or_create( department=request.POST["department"], institution=Institution.objects.get(pk=int(request.POST["institution"])), )[0] return JsonResponse({"name": str(newaff), "id": newaff.pk}) @user_is_staff @transaction.atomic def affiliation_merge(request, affiliation_id): affiliation = get_object_or_404(Affiliation, pk=affiliation_id) context = {"merging": affiliation, "affiliation_merge_form": AffiliationMergeForm} if request.method == "GET": """ Initial load of the merge form displays all the authors and works associated with this affiliation. """ return render(request, "affiliation_merge.html", context) elif request.method == "POST": """ Posting the new author id causes all of the old author's authorships to be reassigned. """ raw_form = AffiliationMergeForm(request.POST) if raw_form.is_valid(): target_affiliation = raw_form.cleaned_data["into"] if affiliation == target_affiliation: """ If the user chooses the existing affiliation, don't merge, but instead error out. """ messages.error( request, f"You cannot merge an affiliation into itself. Please select a different affiliation.", ) return redirect("affiliation_merge", affiliation_id=affiliation_id) else: old_affiliation_id = str(affiliation) merge_results = affiliation.merge(target_affiliation) messages.success( request, f"Affiliation {old_affiliation_id} has been merged into {target_affiliation}, and the old affiliation entry has been deleted.", ) messages.success( request, f"{merge_results['update_results']} affiliations updated" ) return redirect("affiliation_edit", pk=target_affiliation.pk) else: for error in raw_form.errors: messages.error(request, error) return render(request, "affiliation_merge.html", context) @user_is_staff @transaction.atomic def affiliation_multi_merge(request): context = {"form": AffiliationMultiMergeForm} if request.method == "POST": raw_form = AffiliationMultiMergeForm(request.POST) if raw_form.is_valid(): target_affiliation = raw_form.cleaned_data["into"] source_affiliations = raw_form.cleaned_data["sources"].exclude( pk=target_affiliation.pk ) for affiliation in source_affiliations: old_affiliation_id = str(affiliation) merge_results = affiliation.merge(target_affiliation) messages.success( request, f"Affiliation {old_affiliation_id} has been merged into {target_affiliation}, and the old affiliation entry has been deleted.", ) messages.success( request, f"{merge_results['update_results']} affiliations updated" ) return redirect("affiliation_edit", pk=target_affiliation.pk) else: for error in raw_form.errors: messages.error(request, error) return render(request, "affiliation_multi_merge.html", context) @user_is_staff @transaction.atomic def wipe_unused(request): deletion_dict = { "Author": Author.objects.exclude(authorships__isnull=False).distinct(), "Affiliation": Affiliation.objects.exclude( asserted_by__isnull=False ).distinct(), "Institution": Institution.objects.exclude( Q(affiliations__asserted_by__isnull=False) | Q(conferences__isnull=False) ).distinct(), "Keyword": Keyword.objects.exclude(works__isnull=False).distinct(), "Appellation": Appellation.objects.exclude( asserted_by__isnull=False ).distinct(), } if request.method == "POST": for k, v in deletion_dict.items(): res = v.delete() if res[0] > 0: messages.success(request, f"{k}: {res[0]} objects deleted") any_hanging_items = any([v.exists() for k, v in deletion_dict.items()]) context = {"deletions": deletion_dict, "hanging_items": any_hanging_items} return render(request, "wipe_unused.html", context) class ConferenceCreate(StaffRequiredMixin, SuccessMessageMixin, CreateView): model = Conference template_name = "conference_create.html" form_class = ConferenceForm extra_context = { "form_title": "Create conference", "cancel_view": "conference_list", } success_message = "Conference '%(year)s - %(short_title)s' created" @transaction.atomic def post(self, request, *args, **kwargs): response = super().post(request, *args, **kwargs) form_instance = self.get_form() if form_instance.is_valid(): for organizer in form_instance.cleaned_data["organizers"]: self.object.organizers.add(organizer) self.object.save() return response if "goto_abstracts" in request.POST: return redirect(reverse("work_list") + f"?conference={self.object.id}") else: for err in form_instance.errors: messages.error(request, err) return response @user_is_staff @transaction.atomic def ConferenceEdit(request, pk): conference = get_object_or_404(Conference, pk=pk) # populate the conference form, including pulling in the related organizers conference_dict = model_to_dict(conference) conference_dict["organizers"] = conference.organizers.all() form = ConferenceForm(initial=conference_dict) ConferenceSeriesFormSet = formset_factory( ConferenceSeriesInline, can_delete=True, extra=0 ) initial_series = [ {"series": memb.series, "number": memb.number} for memb in SeriesMembership.objects.filter(conference=conference).all() ] context = { "conference": conference, "form": form, # "licenses": License.objects.all(), "series_membership_form": ConferenceSeriesFormSet(initial=initial_series), "form_title": "Edit conference", "cancel_view": "conference_list", } if request.method == "POST": form = ConferenceForm(data=request.POST, instance=conference) if form.is_valid(): clean_form = form.cleaned_data conference.year = clean_form["year"] conference.short_title = clean_form["short_title"] conference.notes = clean_form["notes"] conference.url = clean_form["url"] # Clear existing relations and update according to the form conference.organizers.clear() for organizer in clean_form["organizers"]: conference.organizers.add(organizer) conference.hosting_institutions.clear() for hosting_institution in clean_form["hosting_institutions"]: conference.hosting_institutions.add(hosting_institution) conference.save() # License action license_action = clean_form["license_action"] if license_action == "": pass elif license_action == "clear": conference.works.all().update(full_text_license=None) else: license_object = License.objects.get(id=int(license_action)) conference.works.all().update(full_text_license=license_object) series_forms = ConferenceSeriesFormSet(data=request.POST) if series_forms.is_valid(): # Delete memberships first for d_form in series_forms.deleted_forms: d_form_data = d_form.cleaned_data SeriesMembership.objects.filter( conference=conference, series=d_form_data["series"], number=d_form_data["number"], ).delete() # Then update new ones for s_form in series_forms.forms: if s_form not in series_forms.deleted_forms: s_form_data = s_form.cleaned_data SeriesMembership.objects.update_or_create( conference=conference, series=s_form_data["series"], defaults={"number": s_form_data["number"]}, ) messages.success(request, f"Conference {conference} updated.") if "goto_abstracts" in request.POST: return redirect( reverse("work_list") + f"?conference={conference.id}" ) if "goto_series" in request.POST: first_series = conference.series.first() if first_series is None: return redirect("standalone_conferences") else: return redirect("conference_series_detail", pk=first_series.id) return redirect("conference_edit", pk=conference.pk) else: for f, e in series_forms.errors.items(): messages.error(request, f"{f}: {e}") else: for f, e in form.errors.items(): messages.error(request, f"{f}: {e}") return render(request, "conference_edit.html", context) class ConferenceDelete(StaffRequiredMixin, SuccessMessageMixin, DeleteView): model = Conference template_name = "conference_delete.html" extra_context = { "form_title": "Delete conference", "cancel_view": "conference_list", } success_message = "Conference deleted" success_url = reverse_lazy("conference_list") def delete(self, request, *args, **kwargs): messages.success(self.request, self.success_message) return super(ConferenceDelete, self).delete(request, *args, **kwargs) class ConferenceXMLLoad(StaffRequiredMixin, DetailView): model = Conference template_name = "conference_xml_load.html" extra_context = {"form": ConferenceXMLUploadForm()} @transaction.atomic def post(self, request, *args, **kwargs): raw_form = ConferenceXMLUploadForm(request.POST, request.FILES) conference = self.get_object() if raw_form.is_valid(): with TemporaryDirectory() as upload_dir: # Write uploaded zip to tempdir with NamedTemporaryFile(dir=upload_dir, suffix=".zip") as tei_zip: with open(tei_zip.name, "wb") as upload_zip: for chunk in request.FILES["file"]: upload_zip.write(chunk) if not zipfile.is_zipfile(tei_zip.name): messages.error(request, "That is not a valid zipfile.") return render( request, "conference_xml_load.html", { "object": self.get_object(), "form": ConferenceXMLUploadForm(), }, ) # Extract all the files within with zipfile.ZipFile(tei_zip.name) as zip_ref: zip_ref.extractall(upload_dir) # Import all XML import_results = conference.import_xml_directory(upload_dir) n_success = len(import_results["successful_files"]) n_failed = len(import_results["failed_files"]) messages.info( request, f"{n_success} of {n_success + n_failed} files valid.", ) for err in import_results["failed_files"]: messages.error( request, f"{basename(err['filepath'])}: {err['error']}" ) if n_failed == 0: messages.success(request, f"All files imported successfully.") else: messages.info( request, "Please fix errors or remove malformed files, and re-upload zip. All TEI documents must be valid in order to complete the import.", ) return render( request, "conference_xml_load.html", {"object": self.get_object(), "form": ConferenceXMLUploadForm()}, ) else: for f, e in raw_form.errors.items(): messages.error(request, f"{f}: {e}") return render( request, "conference_xml_load.html", {"object": self.get_object(), "form": ConferenceXMLUploadForm()}, ) @login_required @transaction.atomic def conference_checkout(request, conference_id): conference = get_object_or_404(Conference, pk=conference_id) if request.method == "GET": """ Load the current form and display current attached user """ context = { "conference": conference, "form": ConferenceCheckoutForm( {"entry_status": conference.entry_status, "editing_user": "self"} ), } return render(request, "conference_checkout.html", context) elif request.method == "POST": """ Get the form and update the status if the user has the authority to do so """ raw_form = ConferenceCheckoutForm(request.POST) if raw_form.is_valid(): clean_form = raw_form.cleaned_data if clean_form["entry_status"] == "c" and not request.user.is_staff: messages.error( request, "Only an administrator can mark this conference as completed.", ) return redirect("conference_checkout", conference_id=conference.id) else: if clean_form["assign_user"] == "self": conference.entry_status = clean_form["entry_status"] conference.editing_user = request.user conference.save() messages.success(request, "Conference checked out") elif clean_form["assign_user"] == "clear": conference.entry_status = clean_form["entry_status"] conference.editing_user = None conference.save() messages.success(request, "Conference cleared") return redirect(reverse("work_list") + f"?conference={conference.id}") class SeriesCreate(StaffRequiredMixin, SuccessMessageMixin, CreateView): model = ConferenceSeries template_name = "generic_form.html" extra_context = { "form_title": "Create conference series", "cancel_view": "conference_list", } fields = ["title", "abbreviation", "notes"] success_message = "Series '%(title)s' created" success_url = reverse_lazy("conference_list") class SeriesEdit(StaffRequiredMixin, SuccessMessageMixin, UpdateView): model = ConferenceSeries template_name = "generic_form.html" extra_context = { "form_title": "Update conference series", "cancel_view": "conference_list", } fields = ["title", "abbreviation", "notes"] success_message = "Series '%(title)s' updated" success_url = reverse_lazy("conference_list") class SeriesDelete(StaffRequiredMixin, SuccessMessageMixin, DeleteView): model = ConferenceSeries template_name = "generic_form.html" extra_context = { "form_title": "Delete conference series", "cancel_view": "conference_list", } success_message = "Series '%(title)s' deleted" success_url = reverse_lazy("conference_list") def delete(self, request, *args, **kwargs): messages.success(self.request, self.success_message) return super(SeriesDelete, self).delete(request, *args, **kwargs) class OrganizerCreate(StaffRequiredMixin, SuccessMessageMixin, CreateView): model = Organizer template_name = "generic_form.html" extra_context = { "form_title": "Create conference organizer", "cancel_view": "full_organizer_list", } fields = ["name", "abbreviation", "conferences_organized", "notes", "url"] success_message = "Organizer '%(name)s' created" success_url = reverse_lazy("full_organizer_list") def form_valid(self, form): response = super(OrganizerCreate, self).form_valid(form) self.object.user_last_updated = self.request.user self.object.save() return response class OrganizerEdit(StaffRequiredMixin, SuccessMessageMixin, UpdateView): model = Organizer template_name = "generic_form.html" extra_context = { "form_title": "Update conference organizer", "cancel_view": "full_organizer_list", } fields = ["name", "abbreviation", "conferences_organized", "notes", "url"] success_message = "Organizer '%(name)s' updated" success_url = reverse_lazy("full_organizer_list") def form_valid(self, form): response = super(OrganizerEdit, self).form_valid(form) self.object.user_last_updated = self.request.user self.object.save() return response class OrganizerDelete(StaffRequiredMixin, SuccessMessageMixin, DeleteView): model = Organizer template_name = "generic_form.html" extra_context = { "form_title": "Delete organizer", "cancel_view": "full_organizer_list", } success_message = "Organizer %(name)s deleted." success_url = reverse_lazy("full_organizer_list") def delete(self, request, *args, **kwargs): messages.success(self.request, self.success_message) return super(OrganizerDelete, self).delete(request, *args, **kwargs) class OrganizerList(LoginRequiredMixin, ListView): model = Organizer template_name = "full_organizer_list.html" context_object_name = "organizer_list" class KeywordCreate(LoginRequiredMixin, SuccessMessageMixin, CreateView): model = Keyword template_name = "generic_form.html" extra_context = {"form_title": "Create keyword", "cancel_view": "full_keyword_list"} fields = ["title"] success_message = "Keyword '%(title)s' created" success_url = reverse_lazy("full_keyword_list") class KeywordDelete(StaffRequiredMixin, SuccessMessageMixin, DeleteView): model = Keyword template_name = "generic_form.html" extra_context = {"form_title": "Delete keyword", "cancel_view": "full_keyword_list"} success_message = "Keyword '%(title)s' deleted" success_url = reverse_lazy("full_keyword_list") def delete(self, request, *args, **kwargs): messages.success(self.request, self.success_message) return super(KeywordDelete, self).delete(request, *args, **kwargs) class KeywordEdit(StaffRequiredMixin, SuccessMessageMixin, UpdateView): model = Keyword template_name = "generic_form.html" extra_context = { "form_title": "Update keyword", "cancel_view": "full_keyword_list", "merge_view": "keyword_merge", "delete_view": "keyword_delete", } fields = ["title"] success_message = "Keyword '%(title)s' updated" success_url = reverse_lazy("full_keyword_list") class KeywordList(LoginRequiredMixin, ListView): model = Keyword template_name = "tag_list.html" context_object_name = "tag_list" extra_context = { "tag_category": "Keywords", "tag_edit_view": "keyword_edit", "tag_create_view": "keyword_create", "tag_list_view": "full_keyword_list", "multi_merge": "keyword_multi_merge", "filter_param_name": "keywords", } def get_queryset(self): base_results_set = Keyword.objects.order_by("title") results_set = base_results_set.annotate(n_works=Count("works")) if self.request.GET: raw_filter_form = TagForm(self.request.GET) if raw_filter_form.is_valid(): filter_form = raw_filter_form.cleaned_data if filter_form["name"] != "": results_set = results_set.filter( title__icontains=filter_form["name"] ) if filter_form["ordering"] == "a": results_set = results_set.order_by("title") elif filter_form["ordering"] == "n_asc": results_set = results_set.order_by("n_works") elif filter_form["ordering"] == "n_dsc": results_set = results_set.order_by("-n_works") else: for f, e in raw_filter_form.errors.items(): messages.error(self.request, f"{f}: {e}") else: results_set = results_set.order_by("title") return results_set def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context["tag_filter_form"] = TagForm(initial=self.request.GET) context["filtered_tags_count"] = self.get_queryset().count() context["available_tags_count"] = Keyword.objects.count() return context @user_is_staff @transaction.atomic def keyword_merge(request, keyword_id): keyword = get_object_or_404(Keyword, pk=keyword_id) affected_works = Work.objects.filter(keywords=keyword).all() sample_works = affected_works[:15] count_elements = affected_works.count() - 15 context = { "merging": keyword, "tag_merge_form": KeywordMergeForm, "sample_elements": sample_works, "tag_category": "Keyword", "merge_view": "keyword_merge", } if request.method == "GET": """ Initial load of the merge form displays all the authors and works associated with this keyword. """ return render(request, "tag_merge.html", context) elif request.method == "POST": """ Posting the new author id causes all of the old author's authorships to be reassigned. """ raw_form = KeywordMergeForm(request.POST) if raw_form.is_valid(): target_keyword = raw_form.cleaned_data["into"] if keyword == target_keyword: """ If the user chooses the existing keyword, don't merge, but instead error out. """ messages.error( request, f"You cannot merge a keyword into itself. Please select a different keyword.", ) return redirect("keyword_merge", keyword_id=keyword_id) else: old_keyword_id = str(keyword) merge_results = keyword.merge(target_keyword) messages.success( request, f"Keyword {old_keyword_id} has been merged into {target_keyword}, and the old keyword entry has been deleted.", ) messages.success( request, f"{merge_results['update_results']} keywords updated" ) return redirect("keyword_edit", pk=target_keyword.pk) else: for error in raw_form.errors: messages.error(request, error) return render(request, "tag_merge.html", context) @user_is_staff @transaction.atomic def keyword_multi_merge(request): context = { "tag_merge_form": KeywordMultiMergeForm, "tag_category": "Keyword", "multi_merge_view": "keyword_multi_merge", } if request.method == "POST": """ Posting the new author id causes all of the old author's authorships to be reassigned. """ raw_form = KeywordMultiMergeForm(request.POST) if raw_form.is_valid(): target_keyword = raw_form.cleaned_data["into"] source_keywords = raw_form.cleaned_data["sources"].exclude( pk=target_keyword.pk ) for keyword in source_keywords: old_keyword_id = keyword.title merge_results = keyword.merge(target_keyword) messages.success( request, f"Keyword {old_keyword_id} has been merged into {target_keyword}, and the old keyword entry has been deleted.", ) messages.success( request, f"{merge_results['update_results']} keywords updated" ) return redirect("keyword_edit", pk=target_keyword.pk) else: for error in raw_form.errors: messages.error(request, error) return render(request, "tag_multi_merge.html", context) class TopicCreate(LoginRequiredMixin, SuccessMessageMixin, CreateView): model = Topic template_name = "generic_form.html" extra_context = {"form_title": "Create topic", "cancel_view": "full_topic_list"} fields = ["title"] success_message = "Topic '%(title)s' created" success_url = reverse_lazy("full_topic_list") class TopicDelete(StaffRequiredMixin, SuccessMessageMixin, DeleteView): model = Topic template_name = "generic_form.html" extra_context = {"form_title": "Delete topic", "cancel_view": "full_topic_list"} success_message = "Topic '%(title)s' deleted" success_url = reverse_lazy("full_topic_list") def delete(self, request, *args, **kwargs): messages.success(self.request, self.success_message) return super(TopicDelete, self).delete(request, *args, **kwargs) class TopicEdit(LoginRequiredMixin, SuccessMessageMixin, UpdateView): model = Topic template_name = "generic_form.html" extra_context = { "form_title": "Update topic", "cancel_view": "full_topic_list", "merge_view": "topic_merge", "delete_view": "topic_delete", } fields = ["title"] success_message = "Topic '%(title)s' updated" success_url = reverse_lazy("full_topic_list") class TopicList(LoginRequiredMixin, ListView): model = Topic template_name = "tag_list.html" context_object_name = "tag_list" extra_context = { "tag_category": "Topics", "tag_edit_view": "topic_edit", "tag_create_view": "topic_create", "tag_filter_form": TagForm, "tag_list_view": "full_topic_list", "multi_merge": "topic_multi_merge", "filter_param_name": "topics", } def get_queryset(self): base_results_set = Topic.objects.order_by("title") results_set = base_results_set.annotate(n_works=Count("works")) raw_filter_form = TagForm(self.request.GET) if raw_filter_form.is_valid(): filter_form = raw_filter_form.cleaned_data if filter_form["name"] != "": results_set = results_set.filter(title__icontains=filter_form["name"]) if filter_form["ordering"] == "a": results_set = results_set.order_by("title") elif filter_form["ordering"] == "n_asc": results_set = results_set.order_by("n_works") elif filter_form["ordering"] == "n_dsc": results_set = results_set.order_by("-n_works") return results_set def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context["filtered_tags_count"] = self.get_queryset().count() context["available_tags_count"] = Topic.objects.count() return context @user_is_staff @transaction.atomic def topic_merge(request, topic_id): topic = get_object_or_404(Topic, pk=topic_id) affected_elements = topic.works.all() count_elements = affected_elements.count() - 10 sample_elements = affected_elements[:10] context = { "merging": topic, "tag_merge_form": TopicMergeForm, "tag_category": "Topic", "merge_view": "topic_merge", "sample_elements": sample_elements, "count_elements": count_elements, } if request.method == "GET": """ Initial load of the merge form displays all the authors and works associated with this topic. """ return render(request, "tag_merge.html", context) elif request.method == "POST": """ Posting the new author id causes all of the old author's authorships to be reassigned. """ raw_form = TopicMergeForm(request.POST) if raw_form.is_valid(): target_topic = raw_form.cleaned_data["into"] if topic == target_topic: """ If the user chooses the existing topic, don't merge, but instead error out. """ messages.error( request, f"You cannot merge a topic into itself. Please select a different topic.", ) return redirect("topic_merge", topic_id=topic_id) else: old_topic_id = str(topic) merge_results = topic.merge(target_topic) messages.success( request, f"Topic {old_topic_id} has been merged into {target_topic}, and the old topic entry has been deleted.", ) messages.success( request, f"{merge_results['update_results']} topics updated" ) return redirect("topic_edit", pk=target_topic.pk) else: for error in raw_form.errors: messages.error(request, error) return render(request, "tag_merge.html", context) @user_is_staff @transaction.atomic def topic_multi_merge(request): context = { "tag_merge_form": TopicMultiMergeForm, "tag_category": "Topic", "multi_merge_view": "topic_multi_merge", } if request.method == "POST": """ Posting the new author id causes all of the old author's authorships to be reassigned. """ raw_form = TopicMultiMergeForm(request.POST) if raw_form.is_valid(): target_topic = raw_form.cleaned_data["into"] source_topics = raw_form.cleaned_data["sources"].exclude(pk=target_topic.pk) for topic in source_topics: old_topic_id = topic.title merge_results = topic.merge(target_topic) messages.success( request, f"Topic {old_topic_id} has been merged into {target_topic}, and the old topic entry has been deleted.", ) messages.success( request, f"{merge_results['update_results']} topics updated" ) return redirect("topic_edit", pk=target_topic.pk) else: for error in raw_form.errors: messages.error(request, error) return render(request, "tag_multi_merge.html", context) class LanguageCreate(LoginRequiredMixin, SuccessMessageMixin, CreateView): model = Language template_name = "generic_form.html" extra_context = { "form_title": "Create language", "cancel_view": "full_language_list", } fields = ["title", "code"] success_message = "Language '%(title)s' created" success_url = reverse_lazy("full_language_list") class LanguageDelete(StaffRequiredMixin, SuccessMessageMixin, DeleteView): model = Language template_name = "generic_form.html" extra_context = { "form_title": "Delete language", "cancel_view": "full_language_list", } success_message = "Language '%(title)s' deleted" success_url = reverse_lazy("full_language_list") def delete(self, request, *args, **kwargs): messages.success(self.request, self.success_message) return super(LanguageDelete, self).delete(request, *args, **kwargs) class LanguageEdit(StaffRequiredMixin, SuccessMessageMixin, UpdateView): model = Language template_name = "generic_form.html" extra_context = { "form_title": "Update language", "cancel_view": "full_language_list", "merge_view": "language_merge", "delete_view": "language_delete", } fields = ["title", "code"] success_message = "Language '%(title)s' updated" success_url = reverse_lazy("full_language_list") class LanguageList(LoginRequiredMixin, ListView): model = Language template_name = "tag_list.html" context_object_name = "tag_list" extra_context = { "tag_category": "Languages", "tag_edit_view": "language_edit", "tag_create_view": "language_create", "tag_filter_form": TagForm, "tag_list_view": "full_language_list", "filter_param_name": "languages", } def get_queryset(self): base_results_set = Language.objects.order_by("title") results_set = base_results_set.annotate(n_works=Count("works")) raw_filter_form = TagForm(self.request.GET) if raw_filter_form.is_valid(): filter_form = raw_filter_form.cleaned_data if filter_form["name"] != "": results_set = results_set.filter(title__icontains=filter_form["name"]) if filter_form["ordering"] == "a": results_set = results_set.order_by("title") elif filter_form["ordering"] == "n_asc": results_set = results_set.order_by("n_works") elif filter_form["ordering"] == "n_dsc": results_set = results_set.order_by("-n_works") return results_set def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context["filtered_tags_count"] = self.get_queryset().count() context["available_tags_count"] = Language.objects.count() return context @user_is_staff @transaction.atomic def language_merge(request, language_id): language = get_object_or_404(Language, pk=language_id) affected_elements = language.works.all() count_elements = affected_elements.count() - 10 sample_elements = affected_elements[:10] context = { "merging": language, "tag_merge_form": LanguageMergeForm, "tag_category": "Language", "merge_view": "language_merge", "sample_elements": sample_elements, "count_elements": count_elements, } if request.method == "GET": """ Initial load of the merge form displays all the authors and works associated with this language. """ return render(request, "tag_merge.html", context) elif request.method == "POST": """ Posting the new author id causes all of the old author's authorships to be reassigned. """ raw_form = LanguageMergeForm(request.POST) if raw_form.is_valid(): target_language = raw_form.cleaned_data["into"] if language == target_language: """ If the user chooses the existing language, don't merge, but instead error out. """ messages.error( request, f"You cannot merge a language into itself. Please select a different language.", ) return redirect("language_merge", language_id=language_id) else: old_language_id = str(language) merge_results = language.merge(target_language) messages.success( request, f"Language {old_language_id} has been merged into {target_language}, and the old language entry has been deleted.", ) messages.success( request, f"{merge_results['update_results']} languages updated" ) return redirect("language_edit", pk=target_language.pk) else: for error in raw_form.errors: messages.error(request, error) return render(request, "tag_merge.html", context) class WorkTypeCreate(StaffRequiredMixin, SuccessMessageMixin, CreateView): model = WorkType template_name = "generic_form.html" extra_context = { "form_title": "Create work_type", "cancel_view": "full_work_type_list", } fields = ["title", "is_parent"] success_message = "Abstract type '%(title)s' created" success_url = reverse_lazy("full_work_type_list") class WorkTypeDelete(StaffRequiredMixin, SuccessMessageMixin, DeleteView): model = WorkType template_name = "generic_form.html" extra_context = { "form_title": "Delete work_type", "cancel_view": "full_work_type_list", } success_message = "Abstract type '%(title)s' deleted" success_url = reverse_lazy("full_work_type_list") def delete(self, request, *args, **kwargs): messages.success(self.request, self.success_message) return super(WorkTypeDelete, self).delete(request, *args, **kwargs) class WorkTypeEdit(StaffRequiredMixin, SuccessMessageMixin, UpdateView): model = WorkType template_name = "generic_form.html" extra_context = { "form_title": "Update abstract type", "cancel_view": "full_work_type_list", "merge_view": "work_type_merge", "delete_view": "work_type_delete", } fields = ["title", "is_parent"] success_message = "Abstract '%(title)s' updated" success_url = reverse_lazy("full_work_type_list") class WorkTypeList(LoginRequiredMixin, ListView): model = WorkType template_name = "tag_list.html" context_object_name = "tag_list" extra_context = { "tag_category": "Abstract Types", "tag_edit_view": "work_type_edit", "tag_create_view": "work_type_create", "tag_filter_form": TagForm, "tag_list_view": "full_work_type_list", "filter_param_name": "work_type", } def get_queryset(self): base_results_set = WorkType.objects.order_by("title") results_set = base_results_set.annotate(n_works=Count("works")) raw_filter_form = TagForm(self.request.GET) if raw_filter_form.is_valid(): filter_form = raw_filter_form.cleaned_data if filter_form["name"] != "": results_set = results_set.filter(title__icontains=filter_form["name"]) if filter_form["ordering"] == "a": results_set = results_set.order_by("title") elif filter_form["ordering"] == "n_asc": results_set = results_set.order_by("n_works") elif filter_form["ordering"] == "n_dsc": results_set = results_set.order_by("-n_works") return results_set def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context["filtered_tags_count"] = self.get_queryset().count() context["available_tags_count"] = WorkType.objects.count() return context @user_is_staff @transaction.atomic def work_type_merge(request, work_type_id): work_type = get_object_or_404(WorkType, pk=work_type_id) affected_elements = work_type.works.all() count_elements = affected_elements.count() - 10 sample_elements = affected_elements[:10] context = { "merging": work_type, "tag_merge_form": WorkTypeMergeForm, "tag_category": "Abstract Type", "merge_view": "work_type_merge", "sample_elements": sample_elements, "count_elements": count_elements, } if request.method == "GET": """ Initial load of the merge form displays all the authors and works associated with this work_type. """ return render(request, "tag_merge.html", context) elif request.method == "POST": """ Posting the new author id causes all of the old author's authorships to be reassigned. """ raw_form = WorkTypeMergeForm(request.POST) if raw_form.is_valid(): target_work_type = raw_form.cleaned_data["into"] if work_type == target_work_type: """ If the user chooses the existing work_type, don't merge, but instead error out. """ messages.error( request, f"You cannot merge a work_type into itself. Please select a different work_type.", ) return redirect("work_type_merge", work_type_id=work_type_id) else: old_work_type_id = str(work_type) merge_results = work_type.merge(target_work_type) messages.success( request, f"WorkType {old_work_type_id} has been merged into {target_work_type}, and the old work_type entry has been deleted.", ) messages.success( request, f"{merge_results['update_results']} work_types updated" ) return redirect("work_type_edit", pk=target_work_type.pk) else: for error in raw_form.errors: messages.error(request, error) return render(request, "tag_merge.html", context)
36.697767
176
0.59906
from django.shortcuts import render from django.http import HttpResponse, JsonResponse, StreamingHttpResponse, FileResponse from django.template import loader from django.shortcuts import get_object_or_404, render, redirect from django.views import View from django.views.generic import DetailView, ListView from django.db.models import ( Count, Max, Min, Q, F, Prefetch, Subquery, OuterRef, ExpressionWrapper, FloatField, BooleanField, ) from django.db.models.functions import Concat, FirstValue, Cast from django.core import management from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator from django.db.models.functions import Coalesce from django.contrib.postgres.search import SearchRank, SearchQuery from django.contrib.postgres.aggregates import StringAgg from django.urls import reverse, reverse_lazy from django.contrib import messages from django.contrib.messages.views import SuccessMessageMixin from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin from django.contrib.auth.decorators import login_required, user_passes_test from dal.autocomplete import Select2QuerySetView from django.views.generic.edit import CreateView, DeleteView, UpdateView from django.db import transaction, IntegrityError from django.forms.models import model_to_dict from django.forms import formset_factory, inlineformset_factory, modelformset_factory from django.conf import settings from django.utils.html import format_html from django.views.decorators.cache import cache_page import glob from os.path import basename, getmtime from datetime import datetime import csv import sys from operator import attrgetter from tempfile import NamedTemporaryFile, TemporaryDirectory import zipfile from . import models from .models import ( Work, WorkType, Author, Conference, Institution, Appellation, Affiliation, ConferenceSeries, SeriesMembership, Organizer, Country, Keyword, Topic, Language, CountryLabel, Authorship, License, ) from .forms import ( WorkFilter, AuthorFilter, AuthorMergeForm, WorkForm, WorkAuthorshipForm, FullInstitutionForm, InstitutionMergeForm, AffiliationEditForm, AffiliationMergeForm, KeywordMergeForm, TagForm, TopicMergeForm, AffiliationMultiMergeForm, KeywordMultiMergeForm, ConferenceForm, ConferenceCheckoutForm, ConferenceSeriesInline, LanguageMergeForm, WorkTypeMergeForm, InstitutionMultiMergeForm, TopicMultiMergeForm, ConferenceXMLUploadForm, ) PERMISSIONS_ERROR_TEXT = ( "Please contact the lead project editors to edit this part of the database." ) def cache_for_anon(func): def wrap(request, *args, **kwargs): if request.user.is_authenticated: return func(request, *args, **kwargs) else: return cache_page(settings.CACHES["default"]["TIMEOUT"])(func)( request, *args, **kwargs ) return wrap def user_is_staff(func): def wrap(request, *args, **kwargs): if not request.user.is_authenticated: return redirect(f"{reverse('login')}?next={request.path}") if request.user.is_staff: return func(request, *args, **kwargs) else: messages.warning(request, PERMISSIONS_ERROR_TEXT) return redirect("home_view") return wrap class StaffRequiredMixin: def dispatch(self, *args, **kwargs): if not self.request.user.is_authenticated: return redirect(f"{reverse('login')}?next={self.request.path}") if self.request.user.is_staff: return super().dispatch(*args, **kwargs) else: messages.warning(self.request, PERMISSIONS_ERROR_TEXT) return redirect("home_view") class ItemLabelAutocomplete(Select2QuerySetView): def get_selected_result_label(self, item): return self.get_result_label(item) class WorkAutocomplete(ItemLabelAutocomplete): raise_exception = True def get_queryset(self): qs = Work.objects.all() parents_only = self.forwarded.get("parents_only", None) if parents_only: qs = qs.filter(work_type__is_parent=True) conference = self.forwarded.get("conference", None) if conference: qs = qs.filter(conference=conference) if self.q: qs = qs.filter(title__icontains=self.q) return qs.all() class AppellationAutocomplete(ItemLabelAutocomplete): raise_exception = True def get_queryset(self): qs = Appellation.objects.all() if self.q: qs = qs.filter( Q(first_name__icontains=self.q) | Q(last_name__icontains=self.q) ).all() return qs class KeywordAutocomplete(ItemLabelAutocomplete): raise_exception = True def get_queryset(self): qs = Keyword.objects.annotate(n_works=Count("works")).order_by("-n_works") if self.q: qs = qs.filter(title__icontains=self.q).all() return qs def get_result_label(self, item): return f"{item} ({item.n_works} works)" class LanguageAutocomplete(ItemLabelAutocomplete): raise_exception = True def get_queryset(self): qs = Language.objects.annotate(n_works=Count("works")).order_by( "-n_works", "title" ) if self.q: qs = qs.filter(title__icontains=self.q).all() return qs def get_result_label(self, item): return f"{item} ({item.n_works} works)" class TopicAutocomplete(ItemLabelAutocomplete): raise_exception = True def get_queryset(self): qs = Topic.objects.annotate(n_works=Count("works")).order_by("-n_works") if self.q: qs = qs.filter(title__icontains=self.q).all() return qs def get_result_label(self, item): return f"{item} ({item.n_works} works)" class CountryAutocomplete(ItemLabelAutocomplete): raise_exception = True def get_queryset(self): qs = Country.objects.annotate( n_works=Count( "institutions__affiliations__asserted_by__work", distinct=True ) ).order_by("-n_works") if self.q: qs = qs.filter( Q(pref_name__icontains=self.q) | Q(names__name__icontains=self.q) ) return qs.distinct() def get_result_label(self, item): return f"{item} ({item.n_works} works)" class InstitutionAutocomplete(ItemLabelAutocomplete): raise_exception = True def get_queryset(self): qs = ( Institution.objects.annotate( n_works=Count("affiliations__asserted_by__work", distinct=True) ) .select_related("country") .order_by("-n_works") ) if self.q: qs = qs.filter(name__icontains=self.q).all() return qs def get_result_label(self, item): if item.country is not None: c_label = item.country.pref_name else: c_label = "" location_statement = ", ".join( [l for l in [item.state_province_region, c_label] if l != ""] ) return f"{item} ({item.n_works} works)<br><small text-class='muted'>{location_statement}</small>" class AffiliationAutocomplete(ItemLabelAutocomplete): raise_exception = True def get_queryset(self): qs = ( Affiliation.objects.annotate( n_works=Count("asserted_by__work", distinct=True) ) .select_related("institution", "institution__country") .order_by("-n_works") ) inst_filter = self.forwarded.get("institution", None) if inst_filter: qs = qs.filter(institution=inst_filter) if self.q: qs = qs.filter( Q(department__icontains=self.q) | Q(institution__name__icontains=self.q) ).distinct() return qs def get_result_label(self, item): if item.institution.country is not None: c_label = item.institution.country.pref_name else: c_label = "" location_statement = ", ".join( [l for l in [item.institution.state_province_region, c_label] if l != ""] ) return f"{item} ({item.n_works} works)<br><small text-class='muted'>{location_statement}</small>" class ConferenceAutocomplete(ItemLabelAutocomplete): raise_exception = True def get_queryset(self): qs = Conference.objects.annotate( main_series=StringAgg( "series_memberships__series__abbreviation", delimiter=" / ", distinct=True, ) ).order_by("year", "main_series", "short_title", "theme_title") if self.q: qs = qs.filter(search_text__icontains=self.q).distinct() return qs def get_result_label(self, item): if item.main_series: return f"{item.main_series} - {item.year} - {item.short_title}" elif item.short_title: return f"{item.year} - {item.short_title}" else: return f"{item.year} - {item.theme_title}" class AuthorAutocomplete(ItemLabelAutocomplete): raise_exception = True def get_queryset(self): qs = Author.objects.annotate( n_works=Count("authorships", distinct=True), main_last_name=Max("appellations__last_name"), main_first_name=Max("appellations__first_name"), ).order_by("main_last_name", "main_first_name", "-n_works") if self.q: qs = qs.filter(appellations_index__icontains=self.q).distinct() return qs def get_result_label(self, item): return format_html( f"{item.most_recent_appellation} ({item.n_works} works)<br><small text-class='muted'>(All names: {item.appellations_index})</small>" ) def work_view(request, work_id): related_conference = Conference.objects.annotate( n_works=Count("works", distinct=True), n_authors=Count("works__authors", distinct=True), main_series=StringAgg( "series_memberships__series__abbreviation", delimiter=" / ", distinct=True ), ).prefetch_related("series", "organizers") work = get_object_or_404( Work.objects.select_related("work_type", "full_text_license").prefetch_related( Prefetch("conference", queryset=related_conference), "keywords", "topics", "languages", Prefetch( "session_papers", queryset=Work.objects.prefetch_related( Prefetch( "authorships", queryset=Authorship.objects.select_related("appellation"), ), ), ), Prefetch( "parent_session", queryset=Work.objects.prefetch_related( Prefetch( "authorships", queryset=Authorship.objects.select_related( "author", "appellation" ), ) ), ), ), pk=work_id, ) authorships = ( Authorship.objects.filter(work_id=work_id) .order_by("authorship_order") .distinct() .select_related("work", "author", "appellation") .prefetch_related( Prefetch( "affiliations", queryset=Affiliation.objects.select_related( "institution", "institution__country" ), ) ) ) context = {"work": work, "authorships": authorships} return render(request, "work_detail.html", context) def author_view(request, author_id): author = get_object_or_404(Author, pk=author_id) sorted_authorships = ( Authorship.objects.filter(author=author) .order_by("work__conference__year") .prefetch_related( Prefetch("work", queryset=Work.objects.select_related("conference")) ) ) appellations = ( Appellation.objects.filter(asserted_by__author=author) .distinct() .annotate(latest_year=Max("asserted_by__work__conference__year")) .order_by("-latest_year") .prefetch_related(Prefetch("asserted_by", queryset=sorted_authorships)) ) affiliations = ( Affiliation.objects.filter(asserted_by__author=author) .distinct() .annotate(latest_year=Max("asserted_by__work__conference__year")) .order_by("-latest_year") .prefetch_related( Prefetch("asserted_by", queryset=sorted_authorships), Prefetch( "institution", queryset=Institution.objects.select_related("country") ), ) ) works = ( Work.objects.filter(authorships__author=author) .order_by("conference__year") .distinct() .select_related("conference", "parent_session", "work_type") .prefetch_related( Prefetch( "conference", queryset=Conference.objects.prefetch_related("series", "organizers"), ), "session_papers", "keywords", "topics", "languages", Prefetch( "authorships", queryset=Authorship.objects.select_related("appellation", "author"), ), ) ) author_admin_page = reverse("admin:abstracts_author_change", args=(author.pk,)) context = { "author": author, "works": works, "appellations": appellations, "affiliations": affiliations, "author_admin_page": author_admin_page, } return render(request, "author_detail.html", context) class AuthorSplit(DetailView, StaffRequiredMixin): model = Author template_name = "author_split.html" context_object_name = "original_author" def get_context_data(self, **kwargs): authorships = Authorship.objects.filter(author=self.get_object()).order_by( "work__conference__year" ) return {self.context_object_name: self.get_object(), "authorships": authorships} @transaction.atomic def post(self, request, *args, **kwargs): authorships_to_move = request.POST.getlist("splitselect") try: new_author = Author.objects.create() Authorship.objects.filter(id__in=authorships_to_move).update( author=new_author ) self.get_object().save() new_author.save() messages.success( request, f"{len(authorships_to_move)} authorships moved to new author id {new_author.id}", ) return redirect("author_detail", new_author.id) except: messages.error(request, str(authorships_to_move)) return redirect("author_split", self.get_object().id) class XMLView(DetailView, LoginRequiredMixin): model = Work context_object_name = "work" def get(self, request, *args, **kwargs): response = HttpResponse(self.get_object().full_text, content_type="xhtml+xml") response[ "Content-Disposition" ] = f"attachment; filename={self.get_object().id}.xml" return response class AuthorList(ListView): context_object_name = "author_list" template_name = "author_list.html" paginate_by = 50 def get_queryset(self): base_result_set = Author.objects.exclude(appellations__isnull=True).annotate( n_conferences=Count("works__conference", distinct=True) ) raw_filter_form = AuthorFilter(self.request.GET) if raw_filter_form.is_valid(): filter_form = raw_filter_form.cleaned_data order_res = filter_form["ordering"] if order_res is None or order_res == "": order_res = "last_name" result_set = base_result_set.annotate( last_name=Max("appellations__last_name"), n_works=Count("authorships", distinct=True), ).order_by(order_res) author_res = filter_form["author"] if author_res is not None: result_set = result_set.filter(id=author_res.id) affiliation_res = filter_form["affiliation"] if affiliation_res is not None: result_set = result_set.filter( authorships__affiliations=affiliation_res ) institution_res = filter_form["institution"] if institution_res is not None: result_set = result_set.filter( authorships__affiliations__institution=institution_res ) country_res = filter_form["country"] if country_res is not None: result_set = result_set.filter( authorships__affiliations__institution__country=country_res ) conference_res = filter_form["conference"] if conference_res is not None: result_set = result_set.filter(works__conference=conference_res) if filter_form["singleton"]: result_set = result_set.filter(n_conferences=1) name_res = filter_form["name"] if name_res != "": result_set = result_set.filter(appellations_index__icontains=name_res) first_name_res = filter_form["first_name"] if first_name_res != "": result_set = result_set.filter( authorships__appellation__first_name__icontains=first_name_res ) last_name_res = filter_form["last_name"] if last_name_res != "": result_set = result_set.filter( authorships__appellation__last_name__icontains=last_name_res ) newest_authorship = Authorship.objects.filter( author=OuterRef("pk") ).order_by("-work__conference__year") annotated_authors = result_set.annotate( main_affiliation_department=Subquery( newest_authorship.values("affiliations__department")[:1] ), main_affiliation_institution=Subquery( newest_authorship.values("affiliations__institution__name")[:1] ), main_affiliation_institution_city=Subquery( newest_authorship.values("affiliations__institution__city")[:1] ), main_affiliation_institution_state=Subquery( newest_authorship.values( "affiliations__institution__state_province_region" )[:1] ), main_affiliation_institution_country=Subquery( newest_authorship.values( "affiliations__institution__country__pref_name" )[:1] ), most_recent_first_name=Subquery( newest_authorship.values("appellation__first_name")[:1] ), most_recent_last_name=Subquery( newest_authorship.values("appellation__last_name")[:1] ), n_works=Count("authorships", distinct=True), ) return annotated_authors else: messages.warning( self.request, "Query parameters not recognized. Check your URL and try again.", ) return base_result_set def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context["author_filter_form"] = AuthorFilter(data=self.request.GET) context["available_authors_count"] = Author.objects.count() context["redirect_url"] = reverse("author_list") return context def annotate_multiple_series(qs): return qs.annotate( n_conferences=Count("conferences", distinct=True), earliest_year=Min("conferences__year"), latest_year=Max("conferences__year"), n_complete=Count( "conferences", filter=Q(conferences__entry_status="c"), distinct=True ), n_in_progress=Count( "conferences", filter=Q(conferences__entry_status="i"), distinct=True ), n_in_review=Count( "conferences", filter=Q(conferences__entry_status="r"), distinct=True ), n_remaining=F("n_conferences") - F("n_complete") - F("n_in_progress") - F("n_in_review"), pct_complete=( Cast(F("n_complete"), FloatField()) / Cast(F("n_conferences"), FloatField()) ) * 100, pct_in_progress=( Cast(F("n_in_progress"), FloatField()) / Cast(F("n_conferences"), FloatField()) ) * 100, pct_in_review=( Cast(F("n_in_review"), FloatField()) / Cast(F("n_conferences"), FloatField()) ) * 100, pct_remaining=( Cast(F("n_remaining"), FloatField()) / Cast(F("n_conferences"), FloatField()) ) * 100, ).order_by("title") def annotate_single_series(qs): res = qs.aggregate( earliest_year=Min("year"), latest_year=Max("year"), n_conferences=Count("id", distinct=True), n_complete=Count("id", filter=Q(entry_status="c"), distinct=True), n_in_progress=Count("id", filter=Q(entry_status="i"), distinct=True), n_in_review=Count("id", filter=Q(entry_status="r"), distinct=True), ) res["n_remaining"] = ( res["n_conferences"] - res["n_complete"] - res["n_in_progress"] - res["n_in_review"] ) if res["n_conferences"] > 0: res["pct_complete"] = (res["n_complete"] / res["n_conferences"]) * 100 res["pct_in_progress"] = (res["n_in_progress"] / res["n_conferences"]) * 100 res["pct_in_review"] = (res["n_in_review"] / res["n_conferences"]) * 100 res["pct_remaining"] = (res["n_remaining"] / res["n_conferences"]) * 100 else: res["pct_complete"] = 0 res["pct_in_progress"] = 0 res["pct_in_review"] = 0 res["pct_remaining"] = 0 return res def conference_series_qs(): return annotate_multiple_series( ConferenceSeries.objects.exclude(conferences__isnull=True) ) class ConferenceSeriesList(ListView): context_object_name = "series_list" template_name = "conference_series_list.html" def get_queryset(self): base_result_set = conference_series_qs() return base_result_set def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) sa_conf = Conference.objects.filter(series__isnull=True) context["standalone_conferences"] = annotate_single_series(sa_conf) context["standalone_conference_count"] = sa_conf.count() return context class ConferenceSeriesDetail(DetailView): model = ConferenceSeries template_name = "conference_series_detail.html" context_object_name = "series" def get_member_conferences(self): return Conference.objects.filter(series_memberships__series=self.get_object()) def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context["series_progress"] = annotate_single_series( self.get_member_conferences() ) series_order_subquery = SeriesMembership.objects.filter( conference=OuterRef("pk"), series=self.get_object() ).order_by("number") context["conference_list"] = ( self.get_member_conferences() .annotate( main_series=StringAgg( "series_memberships__series__abbreviation", delimiter=" / ", distinct=True, ), n_works=Count("works", distinct=True), n_authors=Count("works__authors", distinct=True), series_order=Subquery(series_order_subquery.values("number")[:1]), ) .order_by("series_order") .prefetch_related( "series_memberships", "series_memberships__series", "organizers", "country", "hosting_institutions", "hosting_institutions__country", "documents", ) ) context["series_list"] = conference_series_qs() return context class StandaloneList(View): template_name = "conference_series_detail.html" def get_standalone_list(self): qs = ( Conference.objects.filter(series__isnull=True) .annotate( main_series=StringAgg( "series_memberships__series__abbreviation", delimiter=" / ", distinct=True, ), n_works=Count("works", distinct=True), n_authors=Count("works__authors", distinct=True), ) .order_by("year", "short_title", "theme_title") .prefetch_related( "series_memberships", "series_memberships__series", "organizers", "country", "hosting_institutions", "hosting_institutions__country", "documents", ) ) return qs def get(self, request): faux_series = { "title": "Standalone Events", "notes": "Digital humanities events not belonging to a larger series, such symposia or workshops.", "n_conferences": self.get_standalone_list().count(), } context = { "conference_list": self.get_standalone_list(), "series": faux_series, "series_list": conference_series_qs(), "series_progress": annotate_single_series(self.get_standalone_list()), } return render(request, self.template_name, context) def home_view(request): conference_count = Conference.objects.count() years_count = Conference.objects.aggregate(year_range=Max("year") - Min("year"))[ "year_range" ] work_count = Work.objects.count() author_count = Author.objects.exclude(authorships__work__isnull=True).count() institution_count = Institution.objects.count() country_count = ( Country.objects.filter( Q(institutions__affiliations__asserted_by__work__isnull=False) | Q(institutions__conferences__isnull=False) | Q(conferences__isnull=False) ) .distinct() .count() ) context = { "site": { "conference_count": conference_count, "years_count": years_count, "work_count": work_count, "author_count": author_count, "institution_count": institution_count, "country_count": country_count, } } return render(request, "index.html", context) @user_is_staff @transaction.atomic def author_merge_view(request, author_id): author = get_object_or_404(Author, pk=author_id) if request.method == "GET": context = {"merging": author, "author_merge_form": AuthorMergeForm} return render(request, "author_merge.html", context) elif request.method == "POST": """ Posting the new author id causes all of the old author's authorships to be reassigned. """ raw_form = AuthorMergeForm(request.POST) if raw_form.is_valid(): target_author = raw_form.cleaned_data["into"] if author == target_author: """ If the user chooses the existing author, don't merge, but instead error out. """ messages.error( request, f"You cannot merge an author into themselves. Please select a different author.", ) return redirect("author_merge", author_id=author_id) else: old_author_string = str(author) merge_results = author.merge(target_author) target_author.user_last_updated = request.user target_author.save() messages.success( request, f"Author {old_author_string} has been merged into {target_author}, and the old author entry has been deleted.", ) messages.success( request, f"{merge_results['update_results']} authorships updated" ) return redirect("author_detail", author_id=target_author.pk) else: for error in raw_form.errors: messages.error(request, error) return render(request, "author_merge.html", context) def field_required(field): if field.get_internal_type() in ("CharField", "TextField") and field.blank: return False if field.null: return False return True def download_data(request): data_dictionary = [] if request.user.is_authenticated: dt_config = settings.PRIVATE_DATA_TABLE_CONFIG zip_url = reverse("private_all_tables_download") else: dt_config = settings.PUBLIC_DATA_TABLE_CONFIG zip_url = reverse("public_all_tables_download") denormalized_url = reverse("works_download") denormalized_last_updated = datetime.fromtimestamp( getmtime(f"{settings.DATA_OUTPUT_PATH}/{settings.DENORMALIZED_WORKS_NAME}.zip") ) for m in dt_config["CONFIGURATION"]: model = attrgetter(m["model"])(models) if "manual_model_description" in m: model_description = m["manual_model_description"] else: try: model_description = model.model_description except: model_description = None all_model_fields = [ { "name": f.name, "relation": f.is_relation, "help_text": f.help_text, "related_model": str(f.related_model) .replace("<class 'abstracts.models.", "") .replace("'>", ""), "type": f.get_internal_type(), "required": field_required(f), } for f in model._meta.fields if not f.one_to_many and f.name not in m["exclude_fields"] ] if m.get("include_string", False): all_model_fields.append( { "name": "label", "relation": None, "help_text": "General label for this object", "related_model": None, "type": "CharField", "required": True, } ) data_dictionary.append( { "model": m["model"], "model_description": model_description, "csv_name": m["csv_name"], "fields": all_model_fields, } ) normalized_last_updated = datetime.fromtimestamp( getmtime(f"{settings.DATA_OUTPUT_PATH}/{dt_config['DATA_ZIP_NAME']}") ) context = { "zip_url": zip_url, "denormalized_url": denormalized_url, "denormalized_last_updated": denormalized_last_updated, "normalized_last_updated": normalized_last_updated, "data_dictionary": data_dictionary, "denormalized_data_dictionary": settings.DENORMALIZED_HEADERS, } return render(request, "downloads.html", context) def download_works_csv(request): target_zip = f"{settings.DATA_OUTPUT_PATH}/{settings.DENORMALIZED_WORKS_NAME}.zip" response = FileResponse(open(target_zip, "rb")) return response def public_download_all_tables(request): target_zip = f"{settings.DATA_OUTPUT_PATH}/{settings.PUBLIC_DATA_TABLE_CONFIG['DATA_ZIP_NAME']}" response = FileResponse(open(target_zip, "rb")) return response @login_required def private_download_all_tables(request): target_zip = f"{settings.DATA_OUTPUT_PATH}/{settings.PRIVATE_DATA_TABLE_CONFIG['DATA_ZIP_NAME']}" response = FileResponse(open(target_zip, "rb")) return response @login_required def WorkCreate(request): if request.method == "GET": if "conference" in request.GET: conf = get_object_or_404(Conference, pk=int(request.GET["conference"])) work_form = WorkForm(initial={"conference": conf.pk}) else: work_form = WorkForm() if request.method == "POST": work_form = WorkForm(request.POST) if work_form.is_valid(): new_work = work_form.save() new_work.user_last_updated = request.user new_work.save() messages.success(request, f"{new_work} created.") return redirect("work_edit_authorship", work_id=new_work.pk) else: for err in work_form.errors: messages.error(request, err) context = {"work_form": work_form} return render(request, "work_create.html", context) @login_required def WorkEdit(request, work_id): work = get_object_or_404(Work, pk=work_id) if request.method == "POST": work_form = WorkForm(request.POST, instance=work) if work_form.is_valid(): work.user_last_updated = request.user work_form.save() messages.success(request, f'"{work.title}" sucessfully updated.') return redirect("work_detail", work_id=work.pk) else: for f, e in work_form.errors.items(): messages.error(request, f"{f}: {e}") work_initial_data = model_to_dict(work) context = {"work_form": WorkForm(initial=work_initial_data), "work": work} return render(request, "work_edit.html", context) @login_required @transaction.atomic def WorkEditAuthorship(request, work_id): work = get_object_or_404(Work, pk=work_id) authorships = work.authorships.all() AuthorshipWorkFormset = formset_factory( WorkAuthorshipForm, can_delete=True, extra=0 ) initial_data = [] for authorship in authorships: base_data = { "author": authorship.author, "authorship_order": authorship.authorship_order, "first_name": authorship.appellation.first_name, "last_name": authorship.appellation.last_name, "affiliations": [aff for aff in authorship.affiliations.all()], } initial_data.append(base_data) if request.method == "GET": authorships_forms = AuthorshipWorkFormset(initial=initial_data) elif request.method == "POST": authorships_forms = AuthorshipWorkFormset(request.POST) if authorships_forms.is_valid(): for d_form in authorships_forms.deleted_forms: d_form_data = d_form.cleaned_data attached_author = d_form_data["author"] Authorship.objects.filter( work=work, author=d_form_data["author"] ).delete() attached_author.save() for aform in authorships_forms: if aform not in authorships_forms.deleted_forms: aform_data = aform.cleaned_data appellation = Appellation.objects.get_or_create( first_name=aform_data["first_name"], last_name=aform_data["last_name"], )[0] affiliations = aform_data["affiliations"] authorship_order = aform_data["authorship_order"] try: if aform_data["author"] is None: author_id = Author.objects.create() else: author_id = aform_data["author"] auth = Authorship.objects.update_or_create( work=work, author=author_id, defaults={ "authorship_order": authorship_order, "appellation": appellation, "user_last_updated": request.user, }, )[0] author_id.user_last_updated = request.user author_id.save() except IntegrityError as e: messages.error( request, f"{e}: Ensure authorship order numbers are unique" ) return redirect("work_edit_authorship", work.pk) auth.affiliations.clear() if affiliations is not None: auth.affiliations.set(affiliations) messages.success( request, f'"{work.title}" authorships successfully updated.' ) if "start_new" in request.POST: return redirect( f"{reverse('work_create')}?conference={work.conference.pk}" ) return redirect("work_detail", work_id=work.pk) else: for error in authorships_forms.errors: messages.error(request, error) context = { "authorships_form": authorships_forms, "work": work, "affiliation_form": AffiliationEditForm, } return render(request, "work_edit_authorships.html", context) @login_required def AuthorInfoJSON(request, author_id): if request.method == "GET": author = get_object_or_404(Author, pk=author_id) author_aff = Affiliation.objects.filter(asserted_by__author=author).distinct() author_dict = { "first_name": author.most_recent_appellation.first_name, "last_name": author.most_recent_appellation.last_name, "work_titles": [w.title for w in author.works.all()][:4], "works_count": author.works.count(), } if author_aff is not None: author_dict["affiliations"] = [ {"name": str(aff), "id": aff.pk} for aff in author_aff ] return JsonResponse(author_dict) @login_required def AffiliationInfoJSON(request, affiliation_id): if request.method == "GET": affiliation = get_object_or_404(Affiliation, pk=affiliation_id) affiliation_dict = { "institution": { "name": str(affiliation.institution), "id": affiliation.institution.id, } } if affiliation.department is not None: affiliation_dict["department"] = affiliation.department return JsonResponse(affiliation_dict) class WorkDelete(LoginRequiredMixin, SuccessMessageMixin, DeleteView): model = Work template_name = "work_delete.html" extra_context = {"cancel_view": "work_list"} success_url = reverse_lazy("work_list") def delete(self, request, *args, **kwargs): messages.success(self.request, f"'{self.get_object().title}' deleted") return super().delete(request, *args, **kwargs) class FullWorkList(ListView): context_object_name = "work_list" template_name = "work_list.html" paginate_by = 10 def get_queryset(self): base_result_set = Work.objects.all() raw_filter_form = WorkFilter(self.request.GET) if raw_filter_form.is_valid(): result_set = base_result_set filter_form = raw_filter_form.cleaned_data work_type_res = filter_form["work_type"] if work_type_res is not None: result_set = result_set.filter(work_type=work_type_res) conference_res = filter_form["conference"] if conference_res is not None: result_set = result_set.filter(conference=conference_res) affiliation_res = filter_form["affiliation"] if len(affiliation_res) > 0: result_set = result_set.filter( authorships__affiliations__in=affiliation_res ).distinct() institution_res = filter_form["institution"] if len(institution_res) > 0: result_set = result_set.filter( authorships__affiliations__institution__in=institution_res ).distinct() author_res = filter_form["author"] if len(author_res) > 0: result_set = result_set.filter(authorships__author__in=author_res) keyword_res = filter_form["keywords"] if len(keyword_res) > 0: result_set = result_set.filter(keywords__in=keyword_res) topic_res = filter_form["topics"] if len(topic_res) > 0: result_set = result_set.filter(topics__in=topic_res) language_res = filter_form["languages"] if len(language_res) > 0: result_set = result_set.filter(languages__in=language_res) if filter_form["full_text_available"]: result_set = result_set.exclude(full_text="") if filter_form["full_text_viewable"]: result_set = result_set.exclude(full_text="").filter( full_text_license__isnull=False ) text_res = filter_form["text"] if text_res != "": text_query = SearchQuery(text_res, search_type="websearch") result_set = ( result_set.filter(search_text=text_query) .annotate( rank=SearchRank( F("search_text"), text_query, ), search_in_ft_only=ExpressionWrapper( ~Q(title__icontains=text_res), output_field=BooleanField() ), ) .filter(rank__gt=0.1) .order_by("-rank") ) order_res = "rank" first_author_subquery = Authorship.objects.filter( work=OuterRef("pk") ).order_by("authorship_order") order_res = filter_form["ordering"] if order_res is None or order_res == "": order_res = "year" if order_res == "year": result_set = result_set.order_by("conference__year", "title") elif order_res == "-year": result_set = result_set.order_by("-conference__year", "title") elif order_res == "title": result_set = result_set.order_by("title") elif order_res == "-title": result_set = result_set.order_by("-title") elif order_res == "last_name": result_set = result_set.annotate( first_author_last_name=Subquery( first_author_subquery.values("appellation__last_name")[:1] ) ).order_by("first_author_last_name", "title") elif order_res == "-last_name": result_set = result_set.annotate( first_author_last_name=Subquery( first_author_subquery.values("appellation__last_name")[:1] ) ).order_by("-first_author_last_name", "title") return ( result_set.select_related( "conference", "work_type", "parent_session", "full_text_license" ) .annotate( main_series=StringAgg( "conference__series_memberships__series__abbreviation", delimiter=" / ", distinct=True, ), main_institution=StringAgg( "conference__hosting_institutions__name", delimiter=" / ", distinct=True, ), ) .prefetch_related( Prefetch( "conference", queryset=Conference.objects.prefetch_related( Prefetch( "series_memberships", queryset=SeriesMembership.objects.select_related( "series" ), ), "organizers", ), ), "session_papers", Prefetch( "authorships", queryset=Authorship.objects.select_related( "appellation", "author" ), ), "keywords", "topics", "languages", ) ) else: for error in raw_filter_form.errors: messages.warning(self.request, error) return base_result_set def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) raw_filter_form = WorkFilter(self.request.GET) if raw_filter_form.is_valid(): filter_form = raw_filter_form.cleaned_data conference_res = filter_form["conference"] if conference_res is not None: conferences_data = ( Conference.objects.filter(id=conference_res.id) .annotate( n_works=Count("works", distinct=True), n_authors=Count("works__authors", distinct=True), main_series=StringAgg( "series_memberships__series__abbreviation", delimiter=" / ", distinct=True, ), ) .select_related("country") .prefetch_related( "organizers", "series_memberships", "series_memberships__series" ) .all() ) context["selected_conferences"] = conferences_data context["work_filter_form"] = WorkFilter(data=self.request.GET) context["available_works_count"] = Work.objects.count() context["filtered_works_count"] = self.get_queryset().count() context["redirect_url"] = reverse("work_list") return context class FullInstitutionList(LoginRequiredMixin, ListView): context_object_name = "institution_list" template_name = "full_institution_list.html" paginate_by = 10 def get_queryset(self): annotated_affiliations = Affiliation.objects.annotate( n_works=Count("asserted_by__work", distinct=True) ) result_set = ( Institution.objects.annotate( n_works=Count("affiliations__asserted_by__work", distinct=True) ) .prefetch_related( Prefetch("affiliations", annotated_affiliations), "country" ) .order_by("-n_works") ) if self.request.GET: raw_filter_form = FullInstitutionForm(self.request.GET) if raw_filter_form.is_valid(): filter_form = raw_filter_form.cleaned_data result_set = result_set.annotate( n_conferences=Count( "affiliations__asserted_by__work__conference", distinct=True ) ) department_res = filter_form["department"] if department_res != "": result_set = result_set.filter( affiliations__department__icontains=department_res ) affiliation_res = filter_form["affiliation"] if affiliation_res is not None: result_set = result_set.filter(affiliations=affiliation_res) institution_res = filter_form["institution"] if institution_res is not None: result_set = result_set.filter(pk=institution_res.pk) country_res = filter_form["country"] if country_res is not None: result_set = result_set.filter(country=country_res) if filter_form["no_department"]: result_set = result_set.filter(affiliations__department="") conference_res = filter_form["conference"] if conference_res is not None: result_set = result_set.filter( affiliations__asserted_by__work__conference=conference_res ).distinct() if filter_form["singleton"]: result_set = result_set.filter(n_conferences=1) if filter_form["ordering"] == "n_dsc": result_set = result_set.order_by( "-n_works", "affiliations__institution__name" ) elif filter_form["ordering"] == "n_asc": result_set = result_set.order_by( "n_works", "affiliations__institution__name" ) elif filter_form["ordering"] == "a": result_set = result_set.order_by("affiliations__institution__name") else: for f, e in raw_filter_form.errors.items(): messages.error(self.request, f"{f}: {e}") return result_set def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context["institution_filter_form"] = FullInstitutionForm( initial=self.request.GET ) context["available_institutions_count"] = Institution.objects.count() context["filtered_institutions_count"] = self.get_queryset().count() context["redirect_url"] = reverse("full_institution_list") return context class AuthorInstitutionList(FullInstitutionList): template_name = "author_institution_list.html" def get_queryset(self): base_result_set = Institution.objects.annotate( n_authors=Count("affiliations__asserted_by__author", distinct=True), n_conferences=Count( "affiliations__asserted_by__work__conference", distinct=True ), ).distinct() result_set = base_result_set if self.request.GET: raw_filter_form = FullInstitutionForm(self.request.GET) if raw_filter_form.is_valid(): filter_form = raw_filter_form.cleaned_data department_res = filter_form["department"] if department_res != "": result_set = result_set.filter( affiliations__department__icontains=department_res ) affiliation_res = filter_form["affiliation"] if affiliation_res is not None: result_set = result_set.filter(affiliations=affiliation_res) institution_res = filter_form["institution"] if institution_res is not None: result_set = result_set.filter(pk=institution_res.pk) conference_res = filter_form["conference"] if conference_res is not None: result_set = result_set.filter(works__conference=conference_res) country_res = filter_form["country"] if country_res is not None: result_set = result_set.filter(country=country_res) if filter_form["singleton"]: result_set = result_set.filter(n_conferences=1) if filter_form["no_department"]: result_set = result_set.filter(affiliations__department="") if filter_form["ordering"] == "n_dsc": result_set = result_set.order_by("-n_authors") elif filter_form["ordering"] == "n_asc": result_set = result_set.order_by("n_authors") elif filter_form["ordering"] == "a": result_set = result_set.order_by("affiliations__institution__name") else: for f, e in raw_filter_form.errors.items(): messages.error(self.request, f"{f}: {e}") result_set = base_result_set else: result_set = result_set.order_by("-n_authors") return result_set.distinct() def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context["redirect_url"] = reverse("author_institution_list") return context class InstitutionEdit(LoginRequiredMixin, SuccessMessageMixin, UpdateView): model = Institution template_name = "generic_form.html" fields = ["name", "city", "state_province_region", "country"] extra_context = { "form_title": "Edit institution", "cancel_view": "full_institution_list", "merge_view": "institution_merge", } success_message = "%(name)s updated" success_url = reverse_lazy("full_institution_list") def form_valid(self, form): response = super(InstitutionEdit, self).form_valid(form) self.object.user_last_updated = self.request.user self.object.save() return response class InstitutionCreate(LoginRequiredMixin, SuccessMessageMixin, CreateView): model = Institution template_name = "generic_form.html" fields = ["name", "city", "state_province_region", "country"] extra_context = { "form_title": "Create institution", "cancel_view": "full_institution_list", } success_message = "%(name)s created" success_url = reverse_lazy("full_institution_list") def form_valid(self, form): response = super(InstitutionCreate, self).form_valid(form) self.object.user_last_updated = self.request.user self.object.save() return response @user_is_staff @transaction.atomic def institution_merge(request, institution_id): institution = get_object_or_404(Institution, pk=institution_id) context = {"merging": institution, "institution_merge_form": InstitutionMergeForm} if request.method == "GET": return render(request, "institution_merge.html", context) elif request.method == "POST": """ Posting the new author id causes all of the old author's authorships to be reassigned. """ raw_form = InstitutionMergeForm(request.POST) if raw_form.is_valid(): target_institution = raw_form.cleaned_data["into"] if institution == target_institution: """ If the user chooses the existing institution, don't merge, but instead error out. """ messages.error( request, f"You cannot merge an institution into itself. Please select a different institution.", ) return redirect("institution_merge", institution_id=institution_id) else: old_institution_id = str(institution) merge_results = institution.merge(target_institution) target_institution.user_last_updated = request.user target_institution.save() messages.success( request, f"Author {old_institution_id} has been merged into {target_institution}, and the old institution entry has been deleted.", ) messages.success( request, f"{merge_results['update_results']} affiliations updated" ) return redirect("institution_edit", pk=target_institution.pk) else: for error in raw_form.errors: messages.error(request, error) return render(request, "institution_merge.html", context) @user_is_staff @transaction.atomic def institution_multi_merge(request): context = {"form": InstitutionMultiMergeForm} if request.method == "POST": raw_form = InstitutionMultiMergeForm(request.POST) if raw_form.is_valid(): target_institution = raw_form.cleaned_data["into"] source_institutions = raw_form.cleaned_data["sources"].exclude( pk=target_institution.pk ) for institution in source_institutions: old_institution_id = str(institution) merge_results = institution.merge(target_institution) target_institution.user_last_updated = request.user target_institution.save() messages.success( request, f"Institution {old_institution_id} has been merged into {target_institution}, and the old institution entry has been deleted.", ) messages.success( request, f"{merge_results['update_results']} institutions updated" ) return redirect("institution_edit", pk=target_institution.pk) else: for error in raw_form.errors: messages.error(request, error) return render(request, "institution_multi_merge.html", context) class AffiliationEdit(LoginRequiredMixin, SuccessMessageMixin, UpdateView): model = Affiliation template_name = "generic_form.html" form_class = AffiliationEditForm extra_context = { "form_title": "Edit affiliation", "cancel_view": "full_institution_list", "merge_view": "affiliation_merge", } success_message = "%(department)s updated" success_url = reverse_lazy("full_institution_list") class AffiliationCreate(LoginRequiredMixin, SuccessMessageMixin, CreateView): model = Affiliation template_name = "generic_form.html" form_class = AffiliationEditForm extra_context = { "form_title": "Create affiliation", "cancel_view": "full_institution_list", } success_message = "%(department)s created" success_url = reverse_lazy("full_institution_list") def get_initial(self, **kwargs): super().get_initial(**kwargs) if "institution" in self.request.GET: self.initial = {"institution": int(self.request.GET["institution"])} return self.initial @login_required def ajax_affiliation_create(request): newaff = Affiliation.objects.get_or_create( department=request.POST["department"], institution=Institution.objects.get(pk=int(request.POST["institution"])), )[0] return JsonResponse({"name": str(newaff), "id": newaff.pk}) @user_is_staff @transaction.atomic def affiliation_merge(request, affiliation_id): affiliation = get_object_or_404(Affiliation, pk=affiliation_id) context = {"merging": affiliation, "affiliation_merge_form": AffiliationMergeForm} if request.method == "GET": return render(request, "affiliation_merge.html", context) elif request.method == "POST": """ Posting the new author id causes all of the old author's authorships to be reassigned. """ raw_form = AffiliationMergeForm(request.POST) if raw_form.is_valid(): target_affiliation = raw_form.cleaned_data["into"] if affiliation == target_affiliation: """ If the user chooses the existing affiliation, don't merge, but instead error out. """ messages.error( request, f"You cannot merge an affiliation into itself. Please select a different affiliation.", ) return redirect("affiliation_merge", affiliation_id=affiliation_id) else: old_affiliation_id = str(affiliation) merge_results = affiliation.merge(target_affiliation) messages.success( request, f"Affiliation {old_affiliation_id} has been merged into {target_affiliation}, and the old affiliation entry has been deleted.", ) messages.success( request, f"{merge_results['update_results']} affiliations updated" ) return redirect("affiliation_edit", pk=target_affiliation.pk) else: for error in raw_form.errors: messages.error(request, error) return render(request, "affiliation_merge.html", context) @user_is_staff @transaction.atomic def affiliation_multi_merge(request): context = {"form": AffiliationMultiMergeForm} if request.method == "POST": raw_form = AffiliationMultiMergeForm(request.POST) if raw_form.is_valid(): target_affiliation = raw_form.cleaned_data["into"] source_affiliations = raw_form.cleaned_data["sources"].exclude( pk=target_affiliation.pk ) for affiliation in source_affiliations: old_affiliation_id = str(affiliation) merge_results = affiliation.merge(target_affiliation) messages.success( request, f"Affiliation {old_affiliation_id} has been merged into {target_affiliation}, and the old affiliation entry has been deleted.", ) messages.success( request, f"{merge_results['update_results']} affiliations updated" ) return redirect("affiliation_edit", pk=target_affiliation.pk) else: for error in raw_form.errors: messages.error(request, error) return render(request, "affiliation_multi_merge.html", context) @user_is_staff @transaction.atomic def wipe_unused(request): deletion_dict = { "Author": Author.objects.exclude(authorships__isnull=False).distinct(), "Affiliation": Affiliation.objects.exclude( asserted_by__isnull=False ).distinct(), "Institution": Institution.objects.exclude( Q(affiliations__asserted_by__isnull=False) | Q(conferences__isnull=False) ).distinct(), "Keyword": Keyword.objects.exclude(works__isnull=False).distinct(), "Appellation": Appellation.objects.exclude( asserted_by__isnull=False ).distinct(), } if request.method == "POST": for k, v in deletion_dict.items(): res = v.delete() if res[0] > 0: messages.success(request, f"{k}: {res[0]} objects deleted") any_hanging_items = any([v.exists() for k, v in deletion_dict.items()]) context = {"deletions": deletion_dict, "hanging_items": any_hanging_items} return render(request, "wipe_unused.html", context) class ConferenceCreate(StaffRequiredMixin, SuccessMessageMixin, CreateView): model = Conference template_name = "conference_create.html" form_class = ConferenceForm extra_context = { "form_title": "Create conference", "cancel_view": "conference_list", } success_message = "Conference '%(year)s - %(short_title)s' created" @transaction.atomic def post(self, request, *args, **kwargs): response = super().post(request, *args, **kwargs) form_instance = self.get_form() if form_instance.is_valid(): for organizer in form_instance.cleaned_data["organizers"]: self.object.organizers.add(organizer) self.object.save() return response if "goto_abstracts" in request.POST: return redirect(reverse("work_list") + f"?conference={self.object.id}") else: for err in form_instance.errors: messages.error(request, err) return response @user_is_staff @transaction.atomic def ConferenceEdit(request, pk): conference = get_object_or_404(Conference, pk=pk) conference_dict = model_to_dict(conference) conference_dict["organizers"] = conference.organizers.all() form = ConferenceForm(initial=conference_dict) ConferenceSeriesFormSet = formset_factory( ConferenceSeriesInline, can_delete=True, extra=0 ) initial_series = [ {"series": memb.series, "number": memb.number} for memb in SeriesMembership.objects.filter(conference=conference).all() ] context = { "conference": conference, "form": form, "series_membership_form": ConferenceSeriesFormSet(initial=initial_series), "form_title": "Edit conference", "cancel_view": "conference_list", } if request.method == "POST": form = ConferenceForm(data=request.POST, instance=conference) if form.is_valid(): clean_form = form.cleaned_data conference.year = clean_form["year"] conference.short_title = clean_form["short_title"] conference.notes = clean_form["notes"] conference.url = clean_form["url"] conference.organizers.clear() for organizer in clean_form["organizers"]: conference.organizers.add(organizer) conference.hosting_institutions.clear() for hosting_institution in clean_form["hosting_institutions"]: conference.hosting_institutions.add(hosting_institution) conference.save() license_action = clean_form["license_action"] if license_action == "": pass elif license_action == "clear": conference.works.all().update(full_text_license=None) else: license_object = License.objects.get(id=int(license_action)) conference.works.all().update(full_text_license=license_object) series_forms = ConferenceSeriesFormSet(data=request.POST) if series_forms.is_valid(): for d_form in series_forms.deleted_forms: d_form_data = d_form.cleaned_data SeriesMembership.objects.filter( conference=conference, series=d_form_data["series"], number=d_form_data["number"], ).delete() for s_form in series_forms.forms: if s_form not in series_forms.deleted_forms: s_form_data = s_form.cleaned_data SeriesMembership.objects.update_or_create( conference=conference, series=s_form_data["series"], defaults={"number": s_form_data["number"]}, ) messages.success(request, f"Conference {conference} updated.") if "goto_abstracts" in request.POST: return redirect( reverse("work_list") + f"?conference={conference.id}" ) if "goto_series" in request.POST: first_series = conference.series.first() if first_series is None: return redirect("standalone_conferences") else: return redirect("conference_series_detail", pk=first_series.id) return redirect("conference_edit", pk=conference.pk) else: for f, e in series_forms.errors.items(): messages.error(request, f"{f}: {e}") else: for f, e in form.errors.items(): messages.error(request, f"{f}: {e}") return render(request, "conference_edit.html", context) class ConferenceDelete(StaffRequiredMixin, SuccessMessageMixin, DeleteView): model = Conference template_name = "conference_delete.html" extra_context = { "form_title": "Delete conference", "cancel_view": "conference_list", } success_message = "Conference deleted" success_url = reverse_lazy("conference_list") def delete(self, request, *args, **kwargs): messages.success(self.request, self.success_message) return super(ConferenceDelete, self).delete(request, *args, **kwargs) class ConferenceXMLLoad(StaffRequiredMixin, DetailView): model = Conference template_name = "conference_xml_load.html" extra_context = {"form": ConferenceXMLUploadForm()} @transaction.atomic def post(self, request, *args, **kwargs): raw_form = ConferenceXMLUploadForm(request.POST, request.FILES) conference = self.get_object() if raw_form.is_valid(): with TemporaryDirectory() as upload_dir: with NamedTemporaryFile(dir=upload_dir, suffix=".zip") as tei_zip: with open(tei_zip.name, "wb") as upload_zip: for chunk in request.FILES["file"]: upload_zip.write(chunk) if not zipfile.is_zipfile(tei_zip.name): messages.error(request, "That is not a valid zipfile.") return render( request, "conference_xml_load.html", { "object": self.get_object(), "form": ConferenceXMLUploadForm(), }, ) with zipfile.ZipFile(tei_zip.name) as zip_ref: zip_ref.extractall(upload_dir) import_results = conference.import_xml_directory(upload_dir) n_success = len(import_results["successful_files"]) n_failed = len(import_results["failed_files"]) messages.info( request, f"{n_success} of {n_success + n_failed} files valid.", ) for err in import_results["failed_files"]: messages.error( request, f"{basename(err['filepath'])}: {err['error']}" ) if n_failed == 0: messages.success(request, f"All files imported successfully.") else: messages.info( request, "Please fix errors or remove malformed files, and re-upload zip. All TEI documents must be valid in order to complete the import.", ) return render( request, "conference_xml_load.html", {"object": self.get_object(), "form": ConferenceXMLUploadForm()}, ) else: for f, e in raw_form.errors.items(): messages.error(request, f"{f}: {e}") return render( request, "conference_xml_load.html", {"object": self.get_object(), "form": ConferenceXMLUploadForm()}, ) @login_required @transaction.atomic def conference_checkout(request, conference_id): conference = get_object_or_404(Conference, pk=conference_id) if request.method == "GET": context = { "conference": conference, "form": ConferenceCheckoutForm( {"entry_status": conference.entry_status, "editing_user": "self"} ), } return render(request, "conference_checkout.html", context) elif request.method == "POST": """ Get the form and update the status if the user has the authority to do so """ raw_form = ConferenceCheckoutForm(request.POST) if raw_form.is_valid(): clean_form = raw_form.cleaned_data if clean_form["entry_status"] == "c" and not request.user.is_staff: messages.error( request, "Only an administrator can mark this conference as completed.", ) return redirect("conference_checkout", conference_id=conference.id) else: if clean_form["assign_user"] == "self": conference.entry_status = clean_form["entry_status"] conference.editing_user = request.user conference.save() messages.success(request, "Conference checked out") elif clean_form["assign_user"] == "clear": conference.entry_status = clean_form["entry_status"] conference.editing_user = None conference.save() messages.success(request, "Conference cleared") return redirect(reverse("work_list") + f"?conference={conference.id}") class SeriesCreate(StaffRequiredMixin, SuccessMessageMixin, CreateView): model = ConferenceSeries template_name = "generic_form.html" extra_context = { "form_title": "Create conference series", "cancel_view": "conference_list", } fields = ["title", "abbreviation", "notes"] success_message = "Series '%(title)s' created" success_url = reverse_lazy("conference_list") class SeriesEdit(StaffRequiredMixin, SuccessMessageMixin, UpdateView): model = ConferenceSeries template_name = "generic_form.html" extra_context = { "form_title": "Update conference series", "cancel_view": "conference_list", } fields = ["title", "abbreviation", "notes"] success_message = "Series '%(title)s' updated" success_url = reverse_lazy("conference_list") class SeriesDelete(StaffRequiredMixin, SuccessMessageMixin, DeleteView): model = ConferenceSeries template_name = "generic_form.html" extra_context = { "form_title": "Delete conference series", "cancel_view": "conference_list", } success_message = "Series '%(title)s' deleted" success_url = reverse_lazy("conference_list") def delete(self, request, *args, **kwargs): messages.success(self.request, self.success_message) return super(SeriesDelete, self).delete(request, *args, **kwargs) class OrganizerCreate(StaffRequiredMixin, SuccessMessageMixin, CreateView): model = Organizer template_name = "generic_form.html" extra_context = { "form_title": "Create conference organizer", "cancel_view": "full_organizer_list", } fields = ["name", "abbreviation", "conferences_organized", "notes", "url"] success_message = "Organizer '%(name)s' created" success_url = reverse_lazy("full_organizer_list") def form_valid(self, form): response = super(OrganizerCreate, self).form_valid(form) self.object.user_last_updated = self.request.user self.object.save() return response class OrganizerEdit(StaffRequiredMixin, SuccessMessageMixin, UpdateView): model = Organizer template_name = "generic_form.html" extra_context = { "form_title": "Update conference organizer", "cancel_view": "full_organizer_list", } fields = ["name", "abbreviation", "conferences_organized", "notes", "url"] success_message = "Organizer '%(name)s' updated" success_url = reverse_lazy("full_organizer_list") def form_valid(self, form): response = super(OrganizerEdit, self).form_valid(form) self.object.user_last_updated = self.request.user self.object.save() return response class OrganizerDelete(StaffRequiredMixin, SuccessMessageMixin, DeleteView): model = Organizer template_name = "generic_form.html" extra_context = { "form_title": "Delete organizer", "cancel_view": "full_organizer_list", } success_message = "Organizer %(name)s deleted." success_url = reverse_lazy("full_organizer_list") def delete(self, request, *args, **kwargs): messages.success(self.request, self.success_message) return super(OrganizerDelete, self).delete(request, *args, **kwargs) class OrganizerList(LoginRequiredMixin, ListView): model = Organizer template_name = "full_organizer_list.html" context_object_name = "organizer_list" class KeywordCreate(LoginRequiredMixin, SuccessMessageMixin, CreateView): model = Keyword template_name = "generic_form.html" extra_context = {"form_title": "Create keyword", "cancel_view": "full_keyword_list"} fields = ["title"] success_message = "Keyword '%(title)s' created" success_url = reverse_lazy("full_keyword_list") class KeywordDelete(StaffRequiredMixin, SuccessMessageMixin, DeleteView): model = Keyword template_name = "generic_form.html" extra_context = {"form_title": "Delete keyword", "cancel_view": "full_keyword_list"} success_message = "Keyword '%(title)s' deleted" success_url = reverse_lazy("full_keyword_list") def delete(self, request, *args, **kwargs): messages.success(self.request, self.success_message) return super(KeywordDelete, self).delete(request, *args, **kwargs) class KeywordEdit(StaffRequiredMixin, SuccessMessageMixin, UpdateView): model = Keyword template_name = "generic_form.html" extra_context = { "form_title": "Update keyword", "cancel_view": "full_keyword_list", "merge_view": "keyword_merge", "delete_view": "keyword_delete", } fields = ["title"] success_message = "Keyword '%(title)s' updated" success_url = reverse_lazy("full_keyword_list") class KeywordList(LoginRequiredMixin, ListView): model = Keyword template_name = "tag_list.html" context_object_name = "tag_list" extra_context = { "tag_category": "Keywords", "tag_edit_view": "keyword_edit", "tag_create_view": "keyword_create", "tag_list_view": "full_keyword_list", "multi_merge": "keyword_multi_merge", "filter_param_name": "keywords", } def get_queryset(self): base_results_set = Keyword.objects.order_by("title") results_set = base_results_set.annotate(n_works=Count("works")) if self.request.GET: raw_filter_form = TagForm(self.request.GET) if raw_filter_form.is_valid(): filter_form = raw_filter_form.cleaned_data if filter_form["name"] != "": results_set = results_set.filter( title__icontains=filter_form["name"] ) if filter_form["ordering"] == "a": results_set = results_set.order_by("title") elif filter_form["ordering"] == "n_asc": results_set = results_set.order_by("n_works") elif filter_form["ordering"] == "n_dsc": results_set = results_set.order_by("-n_works") else: for f, e in raw_filter_form.errors.items(): messages.error(self.request, f"{f}: {e}") else: results_set = results_set.order_by("title") return results_set def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context["tag_filter_form"] = TagForm(initial=self.request.GET) context["filtered_tags_count"] = self.get_queryset().count() context["available_tags_count"] = Keyword.objects.count() return context @user_is_staff @transaction.atomic def keyword_merge(request, keyword_id): keyword = get_object_or_404(Keyword, pk=keyword_id) affected_works = Work.objects.filter(keywords=keyword).all() sample_works = affected_works[:15] count_elements = affected_works.count() - 15 context = { "merging": keyword, "tag_merge_form": KeywordMergeForm, "sample_elements": sample_works, "tag_category": "Keyword", "merge_view": "keyword_merge", } if request.method == "GET": return render(request, "tag_merge.html", context) elif request.method == "POST": """ Posting the new author id causes all of the old author's authorships to be reassigned. """ raw_form = KeywordMergeForm(request.POST) if raw_form.is_valid(): target_keyword = raw_form.cleaned_data["into"] if keyword == target_keyword: """ If the user chooses the existing keyword, don't merge, but instead error out. """ messages.error( request, f"You cannot merge a keyword into itself. Please select a different keyword.", ) return redirect("keyword_merge", keyword_id=keyword_id) else: old_keyword_id = str(keyword) merge_results = keyword.merge(target_keyword) messages.success( request, f"Keyword {old_keyword_id} has been merged into {target_keyword}, and the old keyword entry has been deleted.", ) messages.success( request, f"{merge_results['update_results']} keywords updated" ) return redirect("keyword_edit", pk=target_keyword.pk) else: for error in raw_form.errors: messages.error(request, error) return render(request, "tag_merge.html", context) @user_is_staff @transaction.atomic def keyword_multi_merge(request): context = { "tag_merge_form": KeywordMultiMergeForm, "tag_category": "Keyword", "multi_merge_view": "keyword_multi_merge", } if request.method == "POST": raw_form = KeywordMultiMergeForm(request.POST) if raw_form.is_valid(): target_keyword = raw_form.cleaned_data["into"] source_keywords = raw_form.cleaned_data["sources"].exclude( pk=target_keyword.pk ) for keyword in source_keywords: old_keyword_id = keyword.title merge_results = keyword.merge(target_keyword) messages.success( request, f"Keyword {old_keyword_id} has been merged into {target_keyword}, and the old keyword entry has been deleted.", ) messages.success( request, f"{merge_results['update_results']} keywords updated" ) return redirect("keyword_edit", pk=target_keyword.pk) else: for error in raw_form.errors: messages.error(request, error) return render(request, "tag_multi_merge.html", context) class TopicCreate(LoginRequiredMixin, SuccessMessageMixin, CreateView): model = Topic template_name = "generic_form.html" extra_context = {"form_title": "Create topic", "cancel_view": "full_topic_list"} fields = ["title"] success_message = "Topic '%(title)s' created" success_url = reverse_lazy("full_topic_list") class TopicDelete(StaffRequiredMixin, SuccessMessageMixin, DeleteView): model = Topic template_name = "generic_form.html" extra_context = {"form_title": "Delete topic", "cancel_view": "full_topic_list"} success_message = "Topic '%(title)s' deleted" success_url = reverse_lazy("full_topic_list") def delete(self, request, *args, **kwargs): messages.success(self.request, self.success_message) return super(TopicDelete, self).delete(request, *args, **kwargs) class TopicEdit(LoginRequiredMixin, SuccessMessageMixin, UpdateView): model = Topic template_name = "generic_form.html" extra_context = { "form_title": "Update topic", "cancel_view": "full_topic_list", "merge_view": "topic_merge", "delete_view": "topic_delete", } fields = ["title"] success_message = "Topic '%(title)s' updated" success_url = reverse_lazy("full_topic_list") class TopicList(LoginRequiredMixin, ListView): model = Topic template_name = "tag_list.html" context_object_name = "tag_list" extra_context = { "tag_category": "Topics", "tag_edit_view": "topic_edit", "tag_create_view": "topic_create", "tag_filter_form": TagForm, "tag_list_view": "full_topic_list", "multi_merge": "topic_multi_merge", "filter_param_name": "topics", } def get_queryset(self): base_results_set = Topic.objects.order_by("title") results_set = base_results_set.annotate(n_works=Count("works")) raw_filter_form = TagForm(self.request.GET) if raw_filter_form.is_valid(): filter_form = raw_filter_form.cleaned_data if filter_form["name"] != "": results_set = results_set.filter(title__icontains=filter_form["name"]) if filter_form["ordering"] == "a": results_set = results_set.order_by("title") elif filter_form["ordering"] == "n_asc": results_set = results_set.order_by("n_works") elif filter_form["ordering"] == "n_dsc": results_set = results_set.order_by("-n_works") return results_set def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context["filtered_tags_count"] = self.get_queryset().count() context["available_tags_count"] = Topic.objects.count() return context @user_is_staff @transaction.atomic def topic_merge(request, topic_id): topic = get_object_or_404(Topic, pk=topic_id) affected_elements = topic.works.all() count_elements = affected_elements.count() - 10 sample_elements = affected_elements[:10] context = { "merging": topic, "tag_merge_form": TopicMergeForm, "tag_category": "Topic", "merge_view": "topic_merge", "sample_elements": sample_elements, "count_elements": count_elements, } if request.method == "GET": return render(request, "tag_merge.html", context) elif request.method == "POST": """ Posting the new author id causes all of the old author's authorships to be reassigned. """ raw_form = TopicMergeForm(request.POST) if raw_form.is_valid(): target_topic = raw_form.cleaned_data["into"] if topic == target_topic: """ If the user chooses the existing topic, don't merge, but instead error out. """ messages.error( request, f"You cannot merge a topic into itself. Please select a different topic.", ) return redirect("topic_merge", topic_id=topic_id) else: old_topic_id = str(topic) merge_results = topic.merge(target_topic) messages.success( request, f"Topic {old_topic_id} has been merged into {target_topic}, and the old topic entry has been deleted.", ) messages.success( request, f"{merge_results['update_results']} topics updated" ) return redirect("topic_edit", pk=target_topic.pk) else: for error in raw_form.errors: messages.error(request, error) return render(request, "tag_merge.html", context) @user_is_staff @transaction.atomic def topic_multi_merge(request): context = { "tag_merge_form": TopicMultiMergeForm, "tag_category": "Topic", "multi_merge_view": "topic_multi_merge", } if request.method == "POST": raw_form = TopicMultiMergeForm(request.POST) if raw_form.is_valid(): target_topic = raw_form.cleaned_data["into"] source_topics = raw_form.cleaned_data["sources"].exclude(pk=target_topic.pk) for topic in source_topics: old_topic_id = topic.title merge_results = topic.merge(target_topic) messages.success( request, f"Topic {old_topic_id} has been merged into {target_topic}, and the old topic entry has been deleted.", ) messages.success( request, f"{merge_results['update_results']} topics updated" ) return redirect("topic_edit", pk=target_topic.pk) else: for error in raw_form.errors: messages.error(request, error) return render(request, "tag_multi_merge.html", context) class LanguageCreate(LoginRequiredMixin, SuccessMessageMixin, CreateView): model = Language template_name = "generic_form.html" extra_context = { "form_title": "Create language", "cancel_view": "full_language_list", } fields = ["title", "code"] success_message = "Language '%(title)s' created" success_url = reverse_lazy("full_language_list") class LanguageDelete(StaffRequiredMixin, SuccessMessageMixin, DeleteView): model = Language template_name = "generic_form.html" extra_context = { "form_title": "Delete language", "cancel_view": "full_language_list", } success_message = "Language '%(title)s' deleted" success_url = reverse_lazy("full_language_list") def delete(self, request, *args, **kwargs): messages.success(self.request, self.success_message) return super(LanguageDelete, self).delete(request, *args, **kwargs) class LanguageEdit(StaffRequiredMixin, SuccessMessageMixin, UpdateView): model = Language template_name = "generic_form.html" extra_context = { "form_title": "Update language", "cancel_view": "full_language_list", "merge_view": "language_merge", "delete_view": "language_delete", } fields = ["title", "code"] success_message = "Language '%(title)s' updated" success_url = reverse_lazy("full_language_list") class LanguageList(LoginRequiredMixin, ListView): model = Language template_name = "tag_list.html" context_object_name = "tag_list" extra_context = { "tag_category": "Languages", "tag_edit_view": "language_edit", "tag_create_view": "language_create", "tag_filter_form": TagForm, "tag_list_view": "full_language_list", "filter_param_name": "languages", } def get_queryset(self): base_results_set = Language.objects.order_by("title") results_set = base_results_set.annotate(n_works=Count("works")) raw_filter_form = TagForm(self.request.GET) if raw_filter_form.is_valid(): filter_form = raw_filter_form.cleaned_data if filter_form["name"] != "": results_set = results_set.filter(title__icontains=filter_form["name"]) if filter_form["ordering"] == "a": results_set = results_set.order_by("title") elif filter_form["ordering"] == "n_asc": results_set = results_set.order_by("n_works") elif filter_form["ordering"] == "n_dsc": results_set = results_set.order_by("-n_works") return results_set def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context["filtered_tags_count"] = self.get_queryset().count() context["available_tags_count"] = Language.objects.count() return context @user_is_staff @transaction.atomic def language_merge(request, language_id): language = get_object_or_404(Language, pk=language_id) affected_elements = language.works.all() count_elements = affected_elements.count() - 10 sample_elements = affected_elements[:10] context = { "merging": language, "tag_merge_form": LanguageMergeForm, "tag_category": "Language", "merge_view": "language_merge", "sample_elements": sample_elements, "count_elements": count_elements, } if request.method == "GET": return render(request, "tag_merge.html", context) elif request.method == "POST": """ Posting the new author id causes all of the old author's authorships to be reassigned. """ raw_form = LanguageMergeForm(request.POST) if raw_form.is_valid(): target_language = raw_form.cleaned_data["into"] if language == target_language: """ If the user chooses the existing language, don't merge, but instead error out. """ messages.error( request, f"You cannot merge a language into itself. Please select a different language.", ) return redirect("language_merge", language_id=language_id) else: old_language_id = str(language) merge_results = language.merge(target_language) messages.success( request, f"Language {old_language_id} has been merged into {target_language}, and the old language entry has been deleted.", ) messages.success( request, f"{merge_results['update_results']} languages updated" ) return redirect("language_edit", pk=target_language.pk) else: for error in raw_form.errors: messages.error(request, error) return render(request, "tag_merge.html", context) class WorkTypeCreate(StaffRequiredMixin, SuccessMessageMixin, CreateView): model = WorkType template_name = "generic_form.html" extra_context = { "form_title": "Create work_type", "cancel_view": "full_work_type_list", } fields = ["title", "is_parent"] success_message = "Abstract type '%(title)s' created" success_url = reverse_lazy("full_work_type_list") class WorkTypeDelete(StaffRequiredMixin, SuccessMessageMixin, DeleteView): model = WorkType template_name = "generic_form.html" extra_context = { "form_title": "Delete work_type", "cancel_view": "full_work_type_list", } success_message = "Abstract type '%(title)s' deleted" success_url = reverse_lazy("full_work_type_list") def delete(self, request, *args, **kwargs): messages.success(self.request, self.success_message) return super(WorkTypeDelete, self).delete(request, *args, **kwargs) class WorkTypeEdit(StaffRequiredMixin, SuccessMessageMixin, UpdateView): model = WorkType template_name = "generic_form.html" extra_context = { "form_title": "Update abstract type", "cancel_view": "full_work_type_list", "merge_view": "work_type_merge", "delete_view": "work_type_delete", } fields = ["title", "is_parent"] success_message = "Abstract '%(title)s' updated" success_url = reverse_lazy("full_work_type_list") class WorkTypeList(LoginRequiredMixin, ListView): model = WorkType template_name = "tag_list.html" context_object_name = "tag_list" extra_context = { "tag_category": "Abstract Types", "tag_edit_view": "work_type_edit", "tag_create_view": "work_type_create", "tag_filter_form": TagForm, "tag_list_view": "full_work_type_list", "filter_param_name": "work_type", } def get_queryset(self): base_results_set = WorkType.objects.order_by("title") results_set = base_results_set.annotate(n_works=Count("works")) raw_filter_form = TagForm(self.request.GET) if raw_filter_form.is_valid(): filter_form = raw_filter_form.cleaned_data if filter_form["name"] != "": results_set = results_set.filter(title__icontains=filter_form["name"]) if filter_form["ordering"] == "a": results_set = results_set.order_by("title") elif filter_form["ordering"] == "n_asc": results_set = results_set.order_by("n_works") elif filter_form["ordering"] == "n_dsc": results_set = results_set.order_by("-n_works") return results_set def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context["filtered_tags_count"] = self.get_queryset().count() context["available_tags_count"] = WorkType.objects.count() return context @user_is_staff @transaction.atomic def work_type_merge(request, work_type_id): work_type = get_object_or_404(WorkType, pk=work_type_id) affected_elements = work_type.works.all() count_elements = affected_elements.count() - 10 sample_elements = affected_elements[:10] context = { "merging": work_type, "tag_merge_form": WorkTypeMergeForm, "tag_category": "Abstract Type", "merge_view": "work_type_merge", "sample_elements": sample_elements, "count_elements": count_elements, } if request.method == "GET": return render(request, "tag_merge.html", context) elif request.method == "POST": """ Posting the new author id causes all of the old author's authorships to be reassigned. """ raw_form = WorkTypeMergeForm(request.POST) if raw_form.is_valid(): target_work_type = raw_form.cleaned_data["into"] if work_type == target_work_type: """ If the user chooses the existing work_type, don't merge, but instead error out. """ messages.error( request, f"You cannot merge a work_type into itself. Please select a different work_type.", ) return redirect("work_type_merge", work_type_id=work_type_id) else: old_work_type_id = str(work_type) merge_results = work_type.merge(target_work_type) messages.success( request, f"WorkType {old_work_type_id} has been merged into {target_work_type}, and the old work_type entry has been deleted.", ) messages.success( request, f"{merge_results['update_results']} work_types updated" ) return redirect("work_type_edit", pk=target_work_type.pk) else: for error in raw_form.errors: messages.error(request, error) return render(request, "tag_merge.html", context)
true
true
f705eac2506b58ebfbb78d7b784fcf08c2440c41
4,823
py
Python
Aero_Garden.py
epotex/hagarden
da1aad8f7aa26a8fa0d13738bd124a8aa27319c5
[ "MIT" ]
2
2018-02-05T21:29:44.000Z
2021-12-13T16:38:24.000Z
Aero_Garden.py
epotex/hagarden
da1aad8f7aa26a8fa0d13738bd124a8aa27319c5
[ "MIT" ]
null
null
null
Aero_Garden.py
epotex/hagarden
da1aad8f7aa26a8fa0d13738bd124a8aa27319c5
[ "MIT" ]
2
2018-01-20T04:50:24.000Z
2020-01-20T18:53:27.000Z
import logging import urllib import requests import base64 """ HASS module to read Aerogarde bounty info, later there will be an option to control the light writen by @epotex """ _LOGGER = logging.getLogger(__name__) DOMAIN = 'aerogarden' agent = "BountyWiFi/1.1.13 (iPhone; iOS 10.3.2; Scale/2.00)" port = "8080" host = "http://ec2-54-86-39-88.compute-1.amazonaws.com:" #API Calls Login_call = "/api/Admin/Login" SetDictPushCount = "/api/CustomData/SetDictPushCount?userID=" QueryUserDevice = "/api/CustomData/QueryUserDevice" GetUserSetted = "/api/CustomData/GetUserSetted" QueryDeviceOnline = "/api/CustomData/QueryDeviceOnline" QueryDeviceStatus = "/api/CustomData/QueryDeviceStatus" UpdateDeviceConfig ="/api/CustomData/UpdateDeviceConfig" auth_data ="" encoded_mac = "" AERO_PARAMETERS = {} def base64decode(b): return base64.b64decode(b).decode('utf-8') def setup(hass, base_config): config = base_config.get(DOMAIN) encoded_email = urllib.parse.quote(config['mail']) encoded_password = urllib.parse.quote(config['password']) encoded_mac = urllib.parse.quote(config['aerogarden_mac_address']) auth_data = "mail=" + encoded_email + "&userPwd=" + encoded_password apiurl = str(host) + str(port) + str(Login_call) headers = { 'User-Agent': 'BountyWiFi/1.1.13 (iPhone; iOS 10.3.2; Scale/2.00)', "Content-Type": "application/x-www-form-urlencoded", "Connection": "keep-alive", "Accept": "*/*", "Accept-Encoding": "gzip, deflate" } try: r = requests.post(apiurl, data=auth_data, headers=headers) responce = r.json() userID =responce["code"] device_url = "airGuid=" + encoded_mac + "&userID=" + str(userID) apiurl = str(host) + str(port) + str(QueryDeviceStatus) r = requests.post(apiurl, data=str(device_url), headers=headers) garden_data = r.json() status = 'online' #extracted info config_id= garden_data['configID'] airGuid = garden_data['airGuid'] lightCycle = garden_data['lightCycle'] pumpCycle = garden_data['pumpCycle'] lightTemp = garden_data['lightTemp'] lightStat = garden_data['lightStat'] clock = garden_data['clock'] pumpStat = garden_data['pumpStat'] pumpHydro = garden_data['pumpHydro'] pumpRemind4Hour = garden_data['pumpRemind4Hour'] plantedType = garden_data['plantedType'] garden_name =base64decode(garden_data['plantedName']) totalDay = garden_data['totalDay'] plantedDay = garden_data['plantedDay'] nutriRemindDay = garden_data['nutriRemindDay'] alarmAllow = garden_data['alarmAllow'] plantedDate = garden_data['plantedDate'] nutrientDate = garden_data['nutrientDate'] updateDate = garden_data['updateDate'] createDate = garden_data['createDate'] swVersion = garden_data['swVersion'] hwVersion = garden_data['hwVersion'] bwVersion = garden_data['bwVersion'] oldPlantedDay = garden_data['oldPlantedDay'] deviceID = garden_data['deviceID'] deviceIP = garden_data['deviceIP'] except RequestException: _LOGGER.exception("Error communicating with AeroGarden") status = 'offline' return False #display extracted info hass.states.set('Aerogarden.garden_name',garden_name ) hass.states.set('Aerogarden.config_id',config_id ) hass.states.set('Aerogarden.airGuid',airGuid ) hass.states.set('Aerogarden.lightCycle',lightCycle ) hass.states.set('Aerogarden.pumpCycle',pumpCycle ) hass.states.set('Aerogarden.lightTemp',lightTemp ) hass.states.set('Aerogarden.lightStat',lightStat ) hass.states.set('Aerogarden.clock',clock ) hass.states.set('Aerogarden.pumpStat',pumpStat ) hass.states.set('Aerogarden.pumpHydro',pumpHydro ) hass.states.set('Aerogarden.pumpRemind4Hour',pumpRemind4Hour ) hass.states.set('Aerogarden.totalDay',totalDay ) hass.states.set('Aerogarden.plantedDay',plantedDay ) hass.states.set('Aerogarden.nutriRemindDay',nutriRemindDay ) hass.states.set('Aerogarden.alarmAllow',alarmAllow ) hass.states.set('Aerogarden.plantedDate',plantedDate ) hass.states.set('Aerogarden.nutrientDate',nutrientDate ) hass.states.set('Aerogarden.updateDate',updateDate ) hass.states.set('Aerogarden.createDate',createDate ) hass.states.set('Aerogarden.swVersion',swVersion ) hass.states.set('Aerogarden.hwVersion',hwVersion ) hass.states.set('Aerogarden.bwVersion',bwVersion ) hass.states.set('Aerogarden.oldPlantedDay',oldPlantedDay ) hass.states.set('Aerogarden.deviceID',deviceID ) hass.states.set('Aerogarden.deviceIP',deviceIP ) hass.states.set('Aerogarden.Status', status) return True
37.387597
93
0.695003
import logging import urllib import requests import base64 _LOGGER = logging.getLogger(__name__) DOMAIN = 'aerogarden' agent = "BountyWiFi/1.1.13 (iPhone; iOS 10.3.2; Scale/2.00)" port = "8080" host = "http://ec2-54-86-39-88.compute-1.amazonaws.com:" Login_call = "/api/Admin/Login" SetDictPushCount = "/api/CustomData/SetDictPushCount?userID=" QueryUserDevice = "/api/CustomData/QueryUserDevice" GetUserSetted = "/api/CustomData/GetUserSetted" QueryDeviceOnline = "/api/CustomData/QueryDeviceOnline" QueryDeviceStatus = "/api/CustomData/QueryDeviceStatus" UpdateDeviceConfig ="/api/CustomData/UpdateDeviceConfig" auth_data ="" encoded_mac = "" AERO_PARAMETERS = {} def base64decode(b): return base64.b64decode(b).decode('utf-8') def setup(hass, base_config): config = base_config.get(DOMAIN) encoded_email = urllib.parse.quote(config['mail']) encoded_password = urllib.parse.quote(config['password']) encoded_mac = urllib.parse.quote(config['aerogarden_mac_address']) auth_data = "mail=" + encoded_email + "&userPwd=" + encoded_password apiurl = str(host) + str(port) + str(Login_call) headers = { 'User-Agent': 'BountyWiFi/1.1.13 (iPhone; iOS 10.3.2; Scale/2.00)', "Content-Type": "application/x-www-form-urlencoded", "Connection": "keep-alive", "Accept": "*/*", "Accept-Encoding": "gzip, deflate" } try: r = requests.post(apiurl, data=auth_data, headers=headers) responce = r.json() userID =responce["code"] device_url = "airGuid=" + encoded_mac + "&userID=" + str(userID) apiurl = str(host) + str(port) + str(QueryDeviceStatus) r = requests.post(apiurl, data=str(device_url), headers=headers) garden_data = r.json() status = 'online' config_id= garden_data['configID'] airGuid = garden_data['airGuid'] lightCycle = garden_data['lightCycle'] pumpCycle = garden_data['pumpCycle'] lightTemp = garden_data['lightTemp'] lightStat = garden_data['lightStat'] clock = garden_data['clock'] pumpStat = garden_data['pumpStat'] pumpHydro = garden_data['pumpHydro'] pumpRemind4Hour = garden_data['pumpRemind4Hour'] plantedType = garden_data['plantedType'] garden_name =base64decode(garden_data['plantedName']) totalDay = garden_data['totalDay'] plantedDay = garden_data['plantedDay'] nutriRemindDay = garden_data['nutriRemindDay'] alarmAllow = garden_data['alarmAllow'] plantedDate = garden_data['plantedDate'] nutrientDate = garden_data['nutrientDate'] updateDate = garden_data['updateDate'] createDate = garden_data['createDate'] swVersion = garden_data['swVersion'] hwVersion = garden_data['hwVersion'] bwVersion = garden_data['bwVersion'] oldPlantedDay = garden_data['oldPlantedDay'] deviceID = garden_data['deviceID'] deviceIP = garden_data['deviceIP'] except RequestException: _LOGGER.exception("Error communicating with AeroGarden") status = 'offline' return False hass.states.set('Aerogarden.garden_name',garden_name ) hass.states.set('Aerogarden.config_id',config_id ) hass.states.set('Aerogarden.airGuid',airGuid ) hass.states.set('Aerogarden.lightCycle',lightCycle ) hass.states.set('Aerogarden.pumpCycle',pumpCycle ) hass.states.set('Aerogarden.lightTemp',lightTemp ) hass.states.set('Aerogarden.lightStat',lightStat ) hass.states.set('Aerogarden.clock',clock ) hass.states.set('Aerogarden.pumpStat',pumpStat ) hass.states.set('Aerogarden.pumpHydro',pumpHydro ) hass.states.set('Aerogarden.pumpRemind4Hour',pumpRemind4Hour ) hass.states.set('Aerogarden.totalDay',totalDay ) hass.states.set('Aerogarden.plantedDay',plantedDay ) hass.states.set('Aerogarden.nutriRemindDay',nutriRemindDay ) hass.states.set('Aerogarden.alarmAllow',alarmAllow ) hass.states.set('Aerogarden.plantedDate',plantedDate ) hass.states.set('Aerogarden.nutrientDate',nutrientDate ) hass.states.set('Aerogarden.updateDate',updateDate ) hass.states.set('Aerogarden.createDate',createDate ) hass.states.set('Aerogarden.swVersion',swVersion ) hass.states.set('Aerogarden.hwVersion',hwVersion ) hass.states.set('Aerogarden.bwVersion',bwVersion ) hass.states.set('Aerogarden.oldPlantedDay',oldPlantedDay ) hass.states.set('Aerogarden.deviceID',deviceID ) hass.states.set('Aerogarden.deviceIP',deviceIP ) hass.states.set('Aerogarden.Status', status) return True
true
true
f705eb236c6300bef2b2f2d6d212f2afda2a2e89
37,473
py
Python
caldera/app/api.py
omitroom13/caldera
7153f77941b9fe48d40850a49b8e252d251a4690
[ "Apache-2.0", "CC0-1.0" ]
null
null
null
caldera/app/api.py
omitroom13/caldera
7153f77941b9fe48d40850a49b8e252d251a4690
[ "Apache-2.0", "CC0-1.0" ]
null
null
null
caldera/app/api.py
omitroom13/caldera
7153f77941b9fe48d40850a49b8e252d251a4690
[ "Apache-2.0", "CC0-1.0" ]
null
null
null
import asyncio from datetime import datetime, timezone from functools import wraps import traceback import inspect import logging import ujson as json_module import hashlib import yaml from aiohttp import web import aiohttp import mongoengine import os from .engine.objects import Operation, Network, Domain, Log, ObservedHost, TechniqueMapping, Job, Rat, Host, \ ObservedRat, Adversary, CodedStep, ActiveConnection, Agent, AttackTechnique, AttackTactic, SiteUser, Setting, \ Opcodes, Artifactlist, ObservedFile, AttackList, JobException, ObservedSchtask, ObservedProcess, AttackGroup from .engine.objects import ObservedDomain, ObservedOSVersion, ObservedUser, ObservedShare, ObservedCredential, \ ObservedService, ObservedTimeDelta, ObservedRegKey, ObservedPersistence from . import authentication as auth from .engine.database import native_types from . import ddp from . import attack from . import util from . import interface from . import extern log = logging.getLogger(__name__) routes = [] def api(uri, methods, objects=None, get=None, auth_group=None, headers=None): """This is a decorator for web api endpoints Args: uri: The URI for the API, can contain keywords denoted by '{}' which indicate objects: a list of tuples methods: the list of HTTP methods this API accepts auth_group: the group that the token must be in for access to this API headers: A list of headers to return with the Response """ if objects is None: objects = {} if get is None: get = {} if auth_group is None: auth_group = [] if headers is None: headers = {} def decorator(f): @wraps(f) async def decorated(req, token, url_match): kwargs = {} # Map id to object for name, _class in objects.items(): if name in url_match: # If this fails and the request type is 'GET', # then an exception should be returned try: kwargs[name] = _class.objects.with_id(url_match[name]) if kwargs[name] is None: return web.HTTPBadRequest() except (mongoengine.errors.ValidationError, ): # The client has sent an invalid id in the URL return web.HTTPBadRequest() # Now set the default get parameters # For cases where we see args like ?arg1=value1&arg2&... # arg2 is set to '' # but change it to True instead trueified = {k: True if v == '' else v for k, v in req.GET.items()} for k, v in get.items(): kwargs[k] = trueified.get(k, v) sig = inspect.signature(f) if 'token' in sig.parameters: kwargs['token'] = token # Finally format the output as json (or jsonfm) results = await f(req, **kwargs) if isinstance(results, web.StreamResponse): return results else: json = json_module.dumps(native_types(results), sort_keys=True, indent=4) return web.Response(text=json, content_type='application/json', headers=headers) async def entrypoint(req): host = None try: # ensure this member is authorized token = auth.Token(req.cookies.get('AUTH')) l = [g for g in auth_group if token.in_group(g)] if len(l) == 0: raise auth.NotAuthorized() # active connections peername = req.transport.get_extra_info('peername') if peername is not None: host_ip, port = peername if req.host: local_ip = req.host.split(":")[0] if local_ip == "localhost": local_ip = "127.0.0.1" else: local_ip = "127.0.0.1" token_host = None if token.in_group('agent'): agent = Agent.objects.with_id(token.session_info['_id']) if agent is None: raise auth.NotAuthorized agent.modify(**{'alive': True}) token_host = agent.host host = ActiveConnection.objects(ip=host_ip, host=token_host, local_ip=local_ip).first() if host is None: host = ActiveConnection(ip=host_ip, host=token_host, local_ip=local_ip, connections=0).save() host.update(inc__connections=1) resp = await decorated(req, token, req.match_info) return resp except auth.NotAuthorized: return web.HTTPForbidden() except Exception: traceback.print_exc() results = {'error': 'exception in ' + f.__name__} output = json_module.dumps(results, sort_keys=True, indent=4) return web.HTTPInternalServerError(text=output, content_type='application/json') finally: if host: host.update(dec__connections=1) for method in methods: routes.append((method, uri, entrypoint)) return decorated return decorator def websocket(uri, auth_group=None): if auth_group is None: auth_group = [] def decorator(f): @wraps(f) async def entrypoint(req): try: # ensure this member is authorized token = auth.Token(req.cookies.get('AUTH')) l = [g for g in auth_group if token.in_group(g)] if len(l) == 0: raise auth.NotAuthorized() return await f(req) except auth.NotAuthorized: return web.HTTPForbidden() except Exception: traceback.print_exc() results = {'error': 'exception in ' + f.__name__} output = json_module.dumps(results, sort_keys=True, indent=4) return web.HTTPInternalServerError(text=output, content_type='application/json') routes.append(('GET', uri, entrypoint)) return entrypoint return decorator # Example usage: # GET /api/jobs # POST /api/jobs { 'action': 'install_service', 'host': 'mm198673-pc', ... } @api('/api/jobs', methods=['GET', 'POST'], get={'status': None, 'wait': False}, auth_group=['human', 'agent']) async def query_jobs(request, token, status, wait): if request.method == 'GET': query = {} if status: query['status'] = status if token.in_group('agent'): agent = Agent.objects.with_id(token.session_info['_id']) if not agent: raise auth.NotAuthorized() # are there any jobs for this agent? query.update({'agent': agent.id}) jobs = list(Job.objects(**query)) if not len(jobs) and wait is not False: # Now wait for jobs to be created try: jobs = [(await Job.wait_next(query))] except asyncio.CancelledError: return else: jobs = list(Job.objects(**query)) if not len(jobs) and wait is not False: jobs = [(await Job.wait_next(query))] return jobs elif request.method == 'POST': # only humans are allowed to create new jobs token.require_group('human') json = await request.json() return Job(**json).save().id # Example usage: # GET /api/jobs/<job> # POST /api/jobs/<job> { 'action': 'install_service', 'host': 'mm198673-pc', ... } @api('/api/jobs/{job}', methods=['GET', 'PUT', 'DELETE'], objects={'job': Job}, auth_group=['human', 'agent']) async def query_job(request, token, job): if request.method == 'GET': if token.in_group('agent'): # can only get jobs that are not completed and are for them if job['status'] in ("created", "pending") and str(job.agent.id) == token.session_info['_id']: return job else: raise auth.NotAuthorized() else: return job elif request.method == 'PUT': if token.in_group('agent'): # can only put jobs that are not completed and are for them if job.status in ("created", "pending") and str(job.agent.id) == token.session_info['_id']: json = await request.json() # whitelist legal fields if 'result' in json['action']: job['action']['result'] = json['action']['result'] if 'error' in json['action']: job['action']['error'] = json['action']['error'] if 'exception' in json['action']: job['action']['exception'] = json['action']['exception'] job['status'] = json.get('status', job.status) if job['status'] == "failed" and 'error' in job['action'] and job['action']['error'] == "no client": # Force update the clients list interface.get_clients(job.agent.host) # find the rat try: iv_name = job['action']["rats"]["args"][0] iv = Rat.objects(agent=job.agent, name=iv_name) iv.modify(**{'active': False}) except KeyError: log.warning("Could not find rat to remove for failed job") return job.save() else: raise auth.NotAuthorized() else: # human # Update the job json = await request.json() if json['create_time']: json['create_time'] = datetime.strptime(json['create_time'], "%Y-%m-%dT%H:%M:%S.%f") return job.save() elif request.method == 'DELETE': token.require_group('human') return job.delete() # Example usage: # POST /api/clients @api('/api/clients', methods=['POST'], auth_group=['agent']) async def query_clients(request, token): json = await request.json() # pid, elevated, executable_path agen = Agent.objects.with_id(token.session_info['_id']) # Get the list of known rats complete_names = {iv.name: iv for iv in Rat.objects(host=agen.host)} # Filter list for living rats known_names = {} for name, element in complete_names.items(): if element.active: known_names[name] = element # All of the currently running rats, as returned by the job active = {x['pid']: x for x in json} # Enumerate the active rats, and delete dead ones for name, iv in known_names.items(): if name not in active: iv.modify(**{'active': False}) else: a = active.pop(name) iv.update(**{'elevated': a['elevated'], 'executable': a['executable_path']}) # Any new rats need to be added for name in active: Rat(**{'agent': agen, 'host': agen.host, 'name': name, 'elevated': active[name]['elevated'], 'executable': active[name]['executable_path'], 'username': active[name]['username'].lower(), 'active': True}).save() return None # Example usage: # GET /api/networks # POST /api/networks { domain: 'mitre.org' } @api('/api/networks', methods=['GET', 'POST'], auth_group=['human']) async def query_networks(request): if request.method == 'GET': return Network.objects elif request.method == 'POST': json = await request.json() network = Network(**json).save() return network.id @api('/api/networks/{network}', methods=['GET', 'DELETE'], objects={'network': Network}, auth_group=['human']) async def query_network(request, network): if request.method == 'GET': return network elif request.method == 'DELETE': network.delete() @api('/api/heartbeat', methods=['GET'], auth_group=['agent']) async def agent_check_in(request, token): agen = Agent.objects.with_id(token.session_info['_id']) agen.modify(**{'check_in': datetime.now(timezone.utc), 'alive': True}) return True @api('/api/hosts', methods=['GET'], auth_group=['human']) async def query_hosts(request): return Host.objects @api('/api/domains', methods=['GET'], auth_group=['human']) async def query_domains(request): return Domain.objects @api('/api/domains/{domain}', methods=['GET'], objects={'domain': Domain}, auth_group=['human']) async def query_domain(request, domain): return domain @api('/api/domains/{domain}/hosts', methods=['GET'], objects={'domain': Domain}, auth_group=['human']) async def query_domainhosts(request, domain): return Host.objects(domain=domain) @api('/api/networks/{network}/hosts', methods=['GET'], objects={'network': Network}, auth_group=['human']) async def query_networkhosts(request, network): return network.hosts @api('/api/networks/{network}/hosts/{host}', methods=['GET', 'PUT', 'DELETE'], objects={'network': Network, 'host': Host}, auth_group=['human']) async def query_networkhosthosts(request, network, host): if request.method == 'GET': return host elif request.method == 'PUT': network.modify(push__hosts=host) elif request.method == 'DELETE': network.modify(pull__hosts=host) @api('/api/hosts/{host}/commands', methods=['GET', 'POST'], objects={'host': Host}, auth_group=['human']) async def query_commands(request, host): if request.method == 'GET': if 'hostname' in request.GET: hosts = Host.objects(hostname=request.GET['hostname']) return [x.host_command_result() for x in Job.objects(host__in=hosts)] else: return [x.host_command_result() for x in Job.objects(host=host)] elif request.method == 'POST': json = await request.json() return interface.agent_shell_command(host, json['command_line']).id @api('/api/hosts/{host}/commands/{job}', methods=['GET'], get={'wait': False}, objects={'host': Host, 'job': Job}, auth_group=['human']) async def query_command(request, wait, host, job): # start waiting for the job before reloading to avoid missing the update if wait is not False: try: await job.wait_till_completed() except JobException as e: log.warning(e.args) return job.host_command_result() @api('/api/rats', methods=['GET'], auth_group=['human']) async def query_ivs(request): query = {k: v for k, v in request.GET.items() if k == 'hostname'} return Rat.objects(**query) @api('/api/rats/{rat}', methods=['GET'], objects={'rat': Rat}, auth_group=['human']) async def query_iv(rat): return rat @api('/api/rats/{rat}/commands', methods=['GET', 'POST'], objects={'rat': Rat}, auth_group=['human']) async def query_ivcommands(request, rat): if request.method == 'GET': return [x.rat_command_result() for x in Job.objects(agent=rat.agent)] elif request.method == 'POST': json = await request.json() return Job.create_rat_command(rat, json["function"], **json["parameters"]).id @api('/api/rats/{rat}/commands/{job}', methods=['GET'], get={'wait': False}, objects={'rat': Rat, 'job': Job}, auth_group=['human']) async def query_ivcommand(request, wait, rat, job): # start waiting for the job before reloading to avoid missing the update if wait is not False: try: await job.wait_till_completed() except JobException as e: log.warning(e.args) return job.rat_result() @api('/api/operations', methods=['GET'], auth_group=['human']) async def query_operations(request): return Operation.objects @api('/api/opcodes', methods=['GET'], auth_group=['human']) async def get_opcodes(request): return Opcodes.arguments @api('/api/networks/{network}/operations', methods=['GET', 'POST'], objects={'network': Network}, auth_group=['human']) async def query_perations(request, network): if request.method == 'GET': return list(Operation.objects(network=network)) elif request.method == 'POST': json = await request.json() if json['start_type'] == 'existing' and 'start_rat' not in json: return None json['network'] = network json['status'] = 'start' json['status_state'] = '' json['log'] = Log().save() # Get the adversary adversary = Adversary.objects.with_id(json['adversary']) json['steps'] = [x.name for x in adversary.steps] operation = Operation(**json).save() return operation.id @api('/api/networks/{network}/operations/{operation}', methods=['GET', 'PUT', 'DELETE', 'PATCH'], get={'wait': False}, objects={'network': Network, 'operation': Operation}, auth_group=['human']) async def query_operation(request, network, operation, wait): if request.method == 'GET': if wait: wait = json_module.loads(wait) wait["id"] = operation.id log.info("Wait: {}".format(wait)) # TODO fix race condition here new = list(Operation.objects(**wait)) if len(new) == 0: del wait["id"] new = [await operation.wait(wait)] return new[0] return operation elif request.method == 'PUT': json = await request.json() json['network_id'] = network.id json['hosts'] = network.hosts return operation.update(**json) elif request.method == 'DELETE': return operation.delete() elif request.method == 'PATCH': json = await request.json() operation.update(__raw__={'$set': json}) @api('/api/agents', methods=['GET'], auth_group=['human']) async def query_agents(request): return Agent.objects @api('/api/logs', methods=['GET'], auth_group=['human']) async def query_logs(request): return Log.objects @api('/api/logs/{log}', methods=['GET'], objects={'log': Log}, auth_group=['human']) async def query_log(request, log): return log @api('/api/agents/{agent}', methods=['GET'], objects={'agent': Agent}, auth_group=['human']) async def query_agent(request, agent): return agent @api('/api/adversaries', methods=['GET', 'POST'], auth_group=['human']) async def query_adversaries(request): if request.method == 'GET': return Adversary.objects elif request.method == 'POST': json = await request.json() json['artifactlists'] = [Artifactlist.objects.with_id(x) for x in json['artifactlists']] json['steps'] = [CodedStep.objects.with_id(x) for x in json['steps']] return Adversary(**json).save().id @api('/api/adversaries/{adversary}', methods=['GET', 'PUT', 'DELETE'], objects={'adversary': Adversary}, auth_group=['human']) async def query_adversary(request, adversary): if request.method == 'GET': return adversary elif request.method == 'PUT': if (adversary.protected): new_adv = {} new_adv['name'] = adversary['name'] new_adv['steps'] = adversary['steps'] new_adv['exfil_method'] = adversary['exfil_method'] new_adv['exfil_port'] = adversary['exfil_port'] new_adv['exfil_address'] = adversary['exfil_address'] new_adv['artifactlists'] = adversary['artifactlists'] adversary = Adversary(**new_adv).save() # Update the adversary json = await request.json() json['artifactlists'] = [Artifactlist.objects.with_id(x) for x in json['artifactlists']] json['steps'] = [CodedStep.objects.with_id(x) for x in json['steps']] adversary.update(**json) return adversary.id elif request.method == 'DELETE': if not adversary.protected: return adversary.delete() @api('/api/step', methods=['GET'], auth_group=['human']) async def query_step(request): return CodedStep.objects @api('/api/site_user', methods=['GET', 'POST'], auth_group=['admin']) async def query_siteusers(request): if request.method == 'GET': return SiteUser.objects.only('username', 'groups', 'email', 'last_login') elif request.method == 'POST': json = await request.json() username = json['username'] email = json.get('email', '') password = json.get('password', None) groups = ['human'] if json.get('admin', False): groups.append('admin') return auth.register_user(username, groups, password=password, email=email).id @api('/api/site_user/{user}', methods=['GET', 'DELETE'], objects={'user': SiteUser}, auth_group=['admin']) async def query_siteuser(request, token, user): if request.method == 'GET': return user.only('username', 'groups', 'email', 'last_login') elif request.method == 'DELETE': if token.session_info['_id'] != str(user.id): return user.delete() @api('/api/site_user/{user}/admin', methods=['PUT', 'DELETE'], objects={'user': SiteUser}, auth_group=['admin']) async def query_siteuser_admin(request, token, user): if request.method == 'PUT': user.modify(push__groups='admin') elif request.method == 'DELETE': if SiteUser.objects(groups='admin').count() > 1 and token.session_info['_id'] != str(user.id): user.modify(pull__groups='admin') @api('/api/site_user/{user}/password', methods=['POST'], objects={'user': SiteUser}, auth_group=['admin', 'human']) async def query_siteuser_password(request, token, user): json = await request.json() if 'password' in json: if token.in_group('admin') or token.session_info['_id'] == str(user.id): auth.user_change_password(user, json['password']) @api('/api/site_user/{user}/email', methods=['POST'], objects={'user': SiteUser}, auth_group=['admin']) async def query_siteuser_email(request, user): json = await request.json() if 'email' in json: user.update(email=json['email']) @api('/api/save_file', methods=['POST'], auth_group=['admin']) async def save_file(request): json = await request.json() if 'edited' in json and 'file' in json: file_path = util.get_path(json['file']) if json['file'].startswith("[-d-]") or file_path is None: return core = util.encrypt_file(json['edited']) with open(file_path, 'wb') as handle: core.tofile(handle) @api('/api/list_file', methods=['GET'], auth_group=['admin']) async def list_files(request): return util.list_files() @api('/api/load_file', methods=['POST'], auth_group=['admin']) async def load_file(request): json = await request.json() if 'file' in json: file_path = util.get_path(json['file']) if json['file'].startswith("[-d-]") or json['file'] == '' or file_path is None: return if file_path.startswith('[m]'): return file_path with open(file_path, 'rb') as handle: data = handle.read() return util.decrypt_file(data) @api('/api/load_psexec', methods=['GET'], auth_group=['admin']) async def load_psexec(request): extern.load_psexec() Setting.objects.first().update(last_psexec_update=util.tz_utcnow()) @api('/api/load_attack', methods=['GET'], auth_group=['admin']) async def load_attack(request): attack.refresh_attack() Setting.objects.first().update(last_attack_update=util.tz_utcnow()) @api('/api/update_depth', methods=['POST'], auth_group=['admin']) async def update_recursion_limit(request): json = await request.json() if 'new_value' in json: Setting.objects.first().modify(recursion_limit=json['new_value']) @api('/api/group_mimic', methods=['GET'], auth_group=['admin', 'human']) async def group_coverage(request): temp_list = [] core = {} for step in CodedStep.objects: for mapping in step.mapping: temp_list.append(mapping.technique) groups = AttackGroup.objects for entry in groups: temp = {} breakdown = {} decision = [] for tech in entry.techniques: temp[tech.name] = (tech in temp_list) decision.append(tech in temp_list) breakdown['techniques'] = temp if (False not in decision) and (len(decision) > 2): breakdown['conclusion'] = 'Can Fully Emulate' else: breakdown['conclusion'] = 'Can Not Fully Emulate' core[entry.name] = breakdown return core @api('/api/steps/{step}/mapping', methods=['POST', 'DELETE'], objects={'step': CodedStep}, auth_group=['human']) async def post_step_mapping(request, step): if request.method == 'POST': json = await request.json() if 'tactics' not in json or 'technique' not in json: return tactics = json['tactics'] technique = json['technique'] try: tech = AttackTechnique.objects.with_id(technique) for tactic in tactics: tac = AttackTactic.objects.with_id(tactic) step.modify(push__mapping=TechniqueMapping(technique=tech, tactic=tac)) except (TypeError, mongoengine.errors.ValidationError): return elif request.method == 'DELETE': json = await request.json() if 'tactic' not in json or 'technique' not in json: return tactic = json['tactic'] technique = json['technique'] try: tech = AttackTechnique.objects.with_id(technique) tac = AttackTactic.objects.with_id(tactic) for mapping in step.mapping: if mapping.tactic == tac and mapping.technique == tech: step.modify(pull__mapping=mapping) except (TypeError, mongoengine.errors.ValidationError): return @api('/api/steps/{step}/mapping/load_defaults', methods=['GET'], objects={'step': CodedStep}, auth_group=['human']) async def get_step_mapping_defaults(request, step): step.update(mapping=step.default_mapping) @api('/api/attack_download.json', methods=['GET'], auth_group=['human'], headers={'Content-Disposition': 'attachment'}) async def get_all_attack_stuff(request): try: techniques = [] for technique in AttackTechnique.objects: this_technique = technique.to_dict() this_technique['tactics'] = [x.name for x in technique.tactics] del this_technique['_id'] techniques.append(this_technique) tactics = [x.to_dict() for x in AttackTactic.objects] for tactic in tactics: del tactic["_id"] return {"techniques": techniques, "tactics": tactics} except (TypeError, mongoengine.errors.ValidationError): return @api('/api/generated/{function}', methods=["POST"], auth_group=['admin']) async def generated_dispatcher(request): dispatched_function = request.match_info['function'] request_json = await request.json() job = getattr(interface, dispatched_function)(**request_json) try: await job.wait_till_completed() return job.action['result'] except JobException: return job.action['error'] @api('/api/artifactlists', methods=['GET', 'POST'], auth_group=['human']) async def get_artifactlists(request): if request.method == 'GET': return Artifactlist.objects elif request.method == 'POST': if request.content_type == "application/json": content = await request.json() elif request.content_type == "text/x-yaml": try: content = format_yaml(await request.text()) except (yaml.scanner.ScannerError, yaml.parser.ParserError): return web.Response(status=400, text="The yaml was not properly formatted") else: return web.Response(status=400) try: return Artifactlist(**content).save().id except (mongoengine.errors.FieldDoesNotExist, mongoengine.errors.ValidationError) as e: return web.Response(status=400, text=str(e)) @api('/api/artifactlists/{artifactlist}', methods=['GET', 'PUT', 'DELETE'], objects={'artifactlist': Artifactlist}, auth_group=['human']) async def query_artifactlist(request, artifactlist): if request.method == 'GET': return artifactlist elif request.method == 'PUT': if request.content_type == "application/json": content = await request.json() elif request.content_type == "text/x-yaml": try: content = format_yaml(await request.text()) except (yaml.scanner.ScannerError, yaml.parser.ParserError): return web.Response(status=400, text="The yaml was not properly formatted") else: return web.Response(status=400) try: artifactlist.update(**content) return artifactlist.id except (mongoengine.errors.FieldDoesNotExist, mongoengine.errors.ValidationError) as e: return web.Response(status=400, text=str(e)) elif request.method == 'DELETE': return artifactlist.delete() @api('/api/parse_artifactlist', methods=['POST'], auth_group=['human']) async def get_parse_artifactlist(request): try: parsed = format_yaml(await request.text()) Artifactlist(**parsed) return parsed except (yaml.scanner.ScannerError, yaml.parser.ParserError) as e: return web.Response(status=400, text="The yaml was not properly formatted: \n" + str(e.problem_mark) + '\n ' + str(e.problem)) except (mongoengine.errors.FieldDoesNotExist, mongoengine.errors.ValidationError) as e: return web.Response(status=400, text=str(e)) def format_yaml(yaml_content): parsed = yaml.load(yaml_content) cleaned = {} for k, v in parsed.items(): if isinstance(v, list) and len(v) == 1 and v[0] is None: cleaned[k] = [] else: cleaned[k] = v return cleaned @api('/api/bsf/{log}', methods=['GET'], objects={'log': Log}, auth_group=['human'], headers={'Content-Disposition': 'attachment; filename=\"bsf.json\"'}) async def query_bsf(request, log): return log["event_stream"] @api('/api/observed/credentials', methods=['GET'], auth_group=['human']) async def query_credentials(request): return ObservedCredential.objects @api('/api/observed/credentials/{credential}', methods=['GET'], objects={'credential': ObservedCredential}, auth_group=['human']) async def query_credential(request, token, credential): return credential @api('/api/observed/users', methods=['GET'], auth_group=['human']) async def query_users(request): return ObservedUser.objects @api('/api/observed/users/{user}', methods=['GET'], objects={'user': ObservedUser}, auth_group=['human']) async def query_user(request, token, user): return user @api('/api/observed/shares', methods=['GET'], auth_group=['human']) async def query_shares(request): return ObservedShare.objects @api('/api/observed/shares/{share}', methods=['GET'], objects={'share': ObservedShare}, auth_group=['human']) async def query_share(request, token, share): return share @api('/api/observed/files', methods=['GET'], auth_group=['human']) async def query_files(request): return ObservedFile.objects @api('/api/observed/files/{file}', methods=['GET'], objects={'file': ObservedFile}, auth_group=['human']) async def query_file(request, token, file): return file @api('/api/observed/domains', methods=['GET'], auth_group=['human']) async def query_domains(request): return ObservedDomain.objects @api('/api/observed/domains/{domain}', methods=['GET'], objects={'domain': ObservedDomain}, auth_group=['human']) async def query_domain(request, token, domain): return domain @api('/api/observed/os_versions', methods=['GET'], auth_group=['human']) async def query_os_versions(request): return ObservedOsversion.objects @api('/api/observed/os_versions/{os_version}', methods=['GET'], objects={'os_version': ObservedOSVersion}, auth_group=['human']) async def query_os_version(request, token, os_version): return os_version @api('/api/observed/hosts', methods=['GET'], auth_group=['human']) async def query_hosts(request): return ObservedHost.objects @api('/api/observed/hosts/{host}', methods=['GET'], objects={'host': ObservedHost}, auth_group=['human']) async def query_host(request, token, host): return host @api('/api/observed/schtasks', methods=['GET'], auth_group=['human']) async def query_schtasks(request): return ObservedSchtask.objects @api('/api/observed/schtasks/{schtask}', methods=['GET'], objects={'schtask': ObservedSchtask}, auth_group=['human']) async def query_schtask(request, token, schtask): return schtask @api('/api/observed/services', methods=['GET'], auth_group=['human']) async def query_timedeltas(request): return ObservedService.objects @api('/api/observed/services/{service}', methods=['GET'], objects={'service': ObservedService}, auth_group=['human']) async def query_timedelta(request, token, service): return service @api('/api/observed/timedeltas', methods=['GET'], auth_group=['human']) async def query_timedeltas(request): return ObservedTimeDelta.objects @api('/api/observed/timedeltas/{timedelta}', methods=['GET'], objects={'timedelta': ObservedTimeDelta}, auth_group=['human']) async def query_timedelta(request, token, timedelta): return timedelta @api('/api/observed/rats', methods=['GET'], auth_group=['human']) async def query_rats(request): return ObservedRat.objects @api('/api/observed/rats/{rat}', methods=['GET'], objects={'rat': ObservedRat}, auth_group=['human']) async def query_rat(request, token, rat): return rat @api('/api/observed/registry_keys', methods=['GET'], auth_group=['human']) async def query_registry_keys(request): return ObservedRegKey.objects @api('/api/observed/registry_keys/{registry_key}', methods=['GET'], objects={'registry_key': ObservedRegKey}, auth_group=['human']) async def query_regkey(request, token, registry_key): return registry_key @api('/api/observed/persistence', methods=['GET'], auth_group=['human']) async def query_persistence_all(request): return ObservedPersistence.objects @api('/api/observed/persistence/{persistence}', methods=['GET'], objects={'persistence': ObservedPersistence}, auth_group=['human']) async def query_persistence(request, token, persistence): return persistence @api('/api/observed/processes', methods=['GET'], auth_group=['human']) async def query_processes(request): return ObservedProcess.objects @api('/api/observed/processes/{process}', methods=['GET'], objects={'process': ObservedProcess}, auth_group=['human']) async def query_process(request, token, process): return process @api('/api/step', methods=['GET'], auth_group=['human']) async def query_step(request): return CodedStep.objects @websocket('/websocket', auth_group=["human"]) async def wb_operation(request): ws = web.WebSocketResponse() await ws.prepare(request) def write_websocket(data): if not ws.closed: ws.send_bytes(data) else: raise RuntimeError srv = ddp.DDPServer(write_websocket) srv.register_collection("operation", Operation) srv.register_collection("domain", Domain) srv.register_collection("host", Host) srv.register_collection("network", Network) srv.register_collection("rat", Rat) srv.register_collection("observed_rat", ObservedRat) srv.register_collection("observed_host", ObservedHost) srv.register_collection("observed_file", ObservedFile) srv.register_collection("observed_schtask", ObservedSchtask) srv.register_collection("job", Job) srv.register_collection("log", Log) srv.register_collection("adversary", Adversary) srv.register_collection("step", CodedStep) srv.register_collection("active_connection", ActiveConnection) srv.register_collection("agent", Agent) srv.register_collection("attack_technique", AttackTechnique) srv.register_collection("attack_tactic", AttackTactic) srv.register_collection("attack_list", AttackList) srv.register_collection("attack_group", AttackGroup) srv.register_collection("setting", Setting) srv.register_collection("artifactlist", Artifactlist) request.app['websockets'].append(ws) try: async for msg in ws: if msg.type == aiohttp.WSMsgType.TEXT or msg.type == aiohttp.WSMsgType.BINARY: srv.parse_message(msg.data) elif msg.type == aiohttp.WSMsgType.ERROR: log.debug('ws connection closed with exception {}'.format(ws.exception())) finally: request.app['websockets'].remove(ws) log.debug('websocket connection closed') return ws def init(app): # setup the generated endpoints for method, uri, func in routes: app.router.add_route(method, uri, func)
37.889788
137
0.621221
import asyncio from datetime import datetime, timezone from functools import wraps import traceback import inspect import logging import ujson as json_module import hashlib import yaml from aiohttp import web import aiohttp import mongoengine import os from .engine.objects import Operation, Network, Domain, Log, ObservedHost, TechniqueMapping, Job, Rat, Host, \ ObservedRat, Adversary, CodedStep, ActiveConnection, Agent, AttackTechnique, AttackTactic, SiteUser, Setting, \ Opcodes, Artifactlist, ObservedFile, AttackList, JobException, ObservedSchtask, ObservedProcess, AttackGroup from .engine.objects import ObservedDomain, ObservedOSVersion, ObservedUser, ObservedShare, ObservedCredential, \ ObservedService, ObservedTimeDelta, ObservedRegKey, ObservedPersistence from . import authentication as auth from .engine.database import native_types from . import ddp from . import attack from . import util from . import interface from . import extern log = logging.getLogger(__name__) routes = [] def api(uri, methods, objects=None, get=None, auth_group=None, headers=None): if objects is None: objects = {} if get is None: get = {} if auth_group is None: auth_group = [] if headers is None: headers = {} def decorator(f): @wraps(f) async def decorated(req, token, url_match): kwargs = {} for name, _class in objects.items(): if name in url_match: try: kwargs[name] = _class.objects.with_id(url_match[name]) if kwargs[name] is None: return web.HTTPBadRequest() except (mongoengine.errors.ValidationError, ): return web.HTTPBadRequest() trueified = {k: True if v == '' else v for k, v in req.GET.items()} for k, v in get.items(): kwargs[k] = trueified.get(k, v) sig = inspect.signature(f) if 'token' in sig.parameters: kwargs['token'] = token results = await f(req, **kwargs) if isinstance(results, web.StreamResponse): return results else: json = json_module.dumps(native_types(results), sort_keys=True, indent=4) return web.Response(text=json, content_type='application/json', headers=headers) async def entrypoint(req): host = None try: token = auth.Token(req.cookies.get('AUTH')) l = [g for g in auth_group if token.in_group(g)] if len(l) == 0: raise auth.NotAuthorized() peername = req.transport.get_extra_info('peername') if peername is not None: host_ip, port = peername if req.host: local_ip = req.host.split(":")[0] if local_ip == "localhost": local_ip = "127.0.0.1" else: local_ip = "127.0.0.1" token_host = None if token.in_group('agent'): agent = Agent.objects.with_id(token.session_info['_id']) if agent is None: raise auth.NotAuthorized agent.modify(**{'alive': True}) token_host = agent.host host = ActiveConnection.objects(ip=host_ip, host=token_host, local_ip=local_ip).first() if host is None: host = ActiveConnection(ip=host_ip, host=token_host, local_ip=local_ip, connections=0).save() host.update(inc__connections=1) resp = await decorated(req, token, req.match_info) return resp except auth.NotAuthorized: return web.HTTPForbidden() except Exception: traceback.print_exc() results = {'error': 'exception in ' + f.__name__} output = json_module.dumps(results, sort_keys=True, indent=4) return web.HTTPInternalServerError(text=output, content_type='application/json') finally: if host: host.update(dec__connections=1) for method in methods: routes.append((method, uri, entrypoint)) return decorated return decorator def websocket(uri, auth_group=None): if auth_group is None: auth_group = [] def decorator(f): @wraps(f) async def entrypoint(req): try: token = auth.Token(req.cookies.get('AUTH')) l = [g for g in auth_group if token.in_group(g)] if len(l) == 0: raise auth.NotAuthorized() return await f(req) except auth.NotAuthorized: return web.HTTPForbidden() except Exception: traceback.print_exc() results = {'error': 'exception in ' + f.__name__} output = json_module.dumps(results, sort_keys=True, indent=4) return web.HTTPInternalServerError(text=output, content_type='application/json') routes.append(('GET', uri, entrypoint)) return entrypoint return decorator @api('/api/jobs', methods=['GET', 'POST'], get={'status': None, 'wait': False}, auth_group=['human', 'agent']) async def query_jobs(request, token, status, wait): if request.method == 'GET': query = {} if status: query['status'] = status if token.in_group('agent'): agent = Agent.objects.with_id(token.session_info['_id']) if not agent: raise auth.NotAuthorized() query.update({'agent': agent.id}) jobs = list(Job.objects(**query)) if not len(jobs) and wait is not False: try: jobs = [(await Job.wait_next(query))] except asyncio.CancelledError: return else: jobs = list(Job.objects(**query)) if not len(jobs) and wait is not False: jobs = [(await Job.wait_next(query))] return jobs elif request.method == 'POST': token.require_group('human') json = await request.json() return Job(**json).save().id @api('/api/jobs/{job}', methods=['GET', 'PUT', 'DELETE'], objects={'job': Job}, auth_group=['human', 'agent']) async def query_job(request, token, job): if request.method == 'GET': if token.in_group('agent'): if job['status'] in ("created", "pending") and str(job.agent.id) == token.session_info['_id']: return job else: raise auth.NotAuthorized() else: return job elif request.method == 'PUT': if token.in_group('agent'): if job.status in ("created", "pending") and str(job.agent.id) == token.session_info['_id']: json = await request.json() if 'result' in json['action']: job['action']['result'] = json['action']['result'] if 'error' in json['action']: job['action']['error'] = json['action']['error'] if 'exception' in json['action']: job['action']['exception'] = json['action']['exception'] job['status'] = json.get('status', job.status) if job['status'] == "failed" and 'error' in job['action'] and job['action']['error'] == "no client": interface.get_clients(job.agent.host) try: iv_name = job['action']["rats"]["args"][0] iv = Rat.objects(agent=job.agent, name=iv_name) iv.modify(**{'active': False}) except KeyError: log.warning("Could not find rat to remove for failed job") return job.save() else: raise auth.NotAuthorized() else: json = await request.json() if json['create_time']: json['create_time'] = datetime.strptime(json['create_time'], "%Y-%m-%dT%H:%M:%S.%f") return job.save() elif request.method == 'DELETE': token.require_group('human') return job.delete() @api('/api/clients', methods=['POST'], auth_group=['agent']) async def query_clients(request, token): json = await request.json() agen = Agent.objects.with_id(token.session_info['_id']) complete_names = {iv.name: iv for iv in Rat.objects(host=agen.host)} known_names = {} for name, element in complete_names.items(): if element.active: known_names[name] = element active = {x['pid']: x for x in json} for name, iv in known_names.items(): if name not in active: iv.modify(**{'active': False}) else: a = active.pop(name) iv.update(**{'elevated': a['elevated'], 'executable': a['executable_path']}) for name in active: Rat(**{'agent': agen, 'host': agen.host, 'name': name, 'elevated': active[name]['elevated'], 'executable': active[name]['executable_path'], 'username': active[name]['username'].lower(), 'active': True}).save() return None @api('/api/networks', methods=['GET', 'POST'], auth_group=['human']) async def query_networks(request): if request.method == 'GET': return Network.objects elif request.method == 'POST': json = await request.json() network = Network(**json).save() return network.id @api('/api/networks/{network}', methods=['GET', 'DELETE'], objects={'network': Network}, auth_group=['human']) async def query_network(request, network): if request.method == 'GET': return network elif request.method == 'DELETE': network.delete() @api('/api/heartbeat', methods=['GET'], auth_group=['agent']) async def agent_check_in(request, token): agen = Agent.objects.with_id(token.session_info['_id']) agen.modify(**{'check_in': datetime.now(timezone.utc), 'alive': True}) return True @api('/api/hosts', methods=['GET'], auth_group=['human']) async def query_hosts(request): return Host.objects @api('/api/domains', methods=['GET'], auth_group=['human']) async def query_domains(request): return Domain.objects @api('/api/domains/{domain}', methods=['GET'], objects={'domain': Domain}, auth_group=['human']) async def query_domain(request, domain): return domain @api('/api/domains/{domain}/hosts', methods=['GET'], objects={'domain': Domain}, auth_group=['human']) async def query_domainhosts(request, domain): return Host.objects(domain=domain) @api('/api/networks/{network}/hosts', methods=['GET'], objects={'network': Network}, auth_group=['human']) async def query_networkhosts(request, network): return network.hosts @api('/api/networks/{network}/hosts/{host}', methods=['GET', 'PUT', 'DELETE'], objects={'network': Network, 'host': Host}, auth_group=['human']) async def query_networkhosthosts(request, network, host): if request.method == 'GET': return host elif request.method == 'PUT': network.modify(push__hosts=host) elif request.method == 'DELETE': network.modify(pull__hosts=host) @api('/api/hosts/{host}/commands', methods=['GET', 'POST'], objects={'host': Host}, auth_group=['human']) async def query_commands(request, host): if request.method == 'GET': if 'hostname' in request.GET: hosts = Host.objects(hostname=request.GET['hostname']) return [x.host_command_result() for x in Job.objects(host__in=hosts)] else: return [x.host_command_result() for x in Job.objects(host=host)] elif request.method == 'POST': json = await request.json() return interface.agent_shell_command(host, json['command_line']).id @api('/api/hosts/{host}/commands/{job}', methods=['GET'], get={'wait': False}, objects={'host': Host, 'job': Job}, auth_group=['human']) async def query_command(request, wait, host, job): if wait is not False: try: await job.wait_till_completed() except JobException as e: log.warning(e.args) return job.host_command_result() @api('/api/rats', methods=['GET'], auth_group=['human']) async def query_ivs(request): query = {k: v for k, v in request.GET.items() if k == 'hostname'} return Rat.objects(**query) @api('/api/rats/{rat}', methods=['GET'], objects={'rat': Rat}, auth_group=['human']) async def query_iv(rat): return rat @api('/api/rats/{rat}/commands', methods=['GET', 'POST'], objects={'rat': Rat}, auth_group=['human']) async def query_ivcommands(request, rat): if request.method == 'GET': return [x.rat_command_result() for x in Job.objects(agent=rat.agent)] elif request.method == 'POST': json = await request.json() return Job.create_rat_command(rat, json["function"], **json["parameters"]).id @api('/api/rats/{rat}/commands/{job}', methods=['GET'], get={'wait': False}, objects={'rat': Rat, 'job': Job}, auth_group=['human']) async def query_ivcommand(request, wait, rat, job): if wait is not False: try: await job.wait_till_completed() except JobException as e: log.warning(e.args) return job.rat_result() @api('/api/operations', methods=['GET'], auth_group=['human']) async def query_operations(request): return Operation.objects @api('/api/opcodes', methods=['GET'], auth_group=['human']) async def get_opcodes(request): return Opcodes.arguments @api('/api/networks/{network}/operations', methods=['GET', 'POST'], objects={'network': Network}, auth_group=['human']) async def query_perations(request, network): if request.method == 'GET': return list(Operation.objects(network=network)) elif request.method == 'POST': json = await request.json() if json['start_type'] == 'existing' and 'start_rat' not in json: return None json['network'] = network json['status'] = 'start' json['status_state'] = '' json['log'] = Log().save() adversary = Adversary.objects.with_id(json['adversary']) json['steps'] = [x.name for x in adversary.steps] operation = Operation(**json).save() return operation.id @api('/api/networks/{network}/operations/{operation}', methods=['GET', 'PUT', 'DELETE', 'PATCH'], get={'wait': False}, objects={'network': Network, 'operation': Operation}, auth_group=['human']) async def query_operation(request, network, operation, wait): if request.method == 'GET': if wait: wait = json_module.loads(wait) wait["id"] = operation.id log.info("Wait: {}".format(wait)) new = list(Operation.objects(**wait)) if len(new) == 0: del wait["id"] new = [await operation.wait(wait)] return new[0] return operation elif request.method == 'PUT': json = await request.json() json['network_id'] = network.id json['hosts'] = network.hosts return operation.update(**json) elif request.method == 'DELETE': return operation.delete() elif request.method == 'PATCH': json = await request.json() operation.update(__raw__={'$set': json}) @api('/api/agents', methods=['GET'], auth_group=['human']) async def query_agents(request): return Agent.objects @api('/api/logs', methods=['GET'], auth_group=['human']) async def query_logs(request): return Log.objects @api('/api/logs/{log}', methods=['GET'], objects={'log': Log}, auth_group=['human']) async def query_log(request, log): return log @api('/api/agents/{agent}', methods=['GET'], objects={'agent': Agent}, auth_group=['human']) async def query_agent(request, agent): return agent @api('/api/adversaries', methods=['GET', 'POST'], auth_group=['human']) async def query_adversaries(request): if request.method == 'GET': return Adversary.objects elif request.method == 'POST': json = await request.json() json['artifactlists'] = [Artifactlist.objects.with_id(x) for x in json['artifactlists']] json['steps'] = [CodedStep.objects.with_id(x) for x in json['steps']] return Adversary(**json).save().id @api('/api/adversaries/{adversary}', methods=['GET', 'PUT', 'DELETE'], objects={'adversary': Adversary}, auth_group=['human']) async def query_adversary(request, adversary): if request.method == 'GET': return adversary elif request.method == 'PUT': if (adversary.protected): new_adv = {} new_adv['name'] = adversary['name'] new_adv['steps'] = adversary['steps'] new_adv['exfil_method'] = adversary['exfil_method'] new_adv['exfil_port'] = adversary['exfil_port'] new_adv['exfil_address'] = adversary['exfil_address'] new_adv['artifactlists'] = adversary['artifactlists'] adversary = Adversary(**new_adv).save() json = await request.json() json['artifactlists'] = [Artifactlist.objects.with_id(x) for x in json['artifactlists']] json['steps'] = [CodedStep.objects.with_id(x) for x in json['steps']] adversary.update(**json) return adversary.id elif request.method == 'DELETE': if not adversary.protected: return adversary.delete() @api('/api/step', methods=['GET'], auth_group=['human']) async def query_step(request): return CodedStep.objects @api('/api/site_user', methods=['GET', 'POST'], auth_group=['admin']) async def query_siteusers(request): if request.method == 'GET': return SiteUser.objects.only('username', 'groups', 'email', 'last_login') elif request.method == 'POST': json = await request.json() username = json['username'] email = json.get('email', '') password = json.get('password', None) groups = ['human'] if json.get('admin', False): groups.append('admin') return auth.register_user(username, groups, password=password, email=email).id @api('/api/site_user/{user}', methods=['GET', 'DELETE'], objects={'user': SiteUser}, auth_group=['admin']) async def query_siteuser(request, token, user): if request.method == 'GET': return user.only('username', 'groups', 'email', 'last_login') elif request.method == 'DELETE': if token.session_info['_id'] != str(user.id): return user.delete() @api('/api/site_user/{user}/admin', methods=['PUT', 'DELETE'], objects={'user': SiteUser}, auth_group=['admin']) async def query_siteuser_admin(request, token, user): if request.method == 'PUT': user.modify(push__groups='admin') elif request.method == 'DELETE': if SiteUser.objects(groups='admin').count() > 1 and token.session_info['_id'] != str(user.id): user.modify(pull__groups='admin') @api('/api/site_user/{user}/password', methods=['POST'], objects={'user': SiteUser}, auth_group=['admin', 'human']) async def query_siteuser_password(request, token, user): json = await request.json() if 'password' in json: if token.in_group('admin') or token.session_info['_id'] == str(user.id): auth.user_change_password(user, json['password']) @api('/api/site_user/{user}/email', methods=['POST'], objects={'user': SiteUser}, auth_group=['admin']) async def query_siteuser_email(request, user): json = await request.json() if 'email' in json: user.update(email=json['email']) @api('/api/save_file', methods=['POST'], auth_group=['admin']) async def save_file(request): json = await request.json() if 'edited' in json and 'file' in json: file_path = util.get_path(json['file']) if json['file'].startswith("[-d-]") or file_path is None: return core = util.encrypt_file(json['edited']) with open(file_path, 'wb') as handle: core.tofile(handle) @api('/api/list_file', methods=['GET'], auth_group=['admin']) async def list_files(request): return util.list_files() @api('/api/load_file', methods=['POST'], auth_group=['admin']) async def load_file(request): json = await request.json() if 'file' in json: file_path = util.get_path(json['file']) if json['file'].startswith("[-d-]") or json['file'] == '' or file_path is None: return if file_path.startswith('[m]'): return file_path with open(file_path, 'rb') as handle: data = handle.read() return util.decrypt_file(data) @api('/api/load_psexec', methods=['GET'], auth_group=['admin']) async def load_psexec(request): extern.load_psexec() Setting.objects.first().update(last_psexec_update=util.tz_utcnow()) @api('/api/load_attack', methods=['GET'], auth_group=['admin']) async def load_attack(request): attack.refresh_attack() Setting.objects.first().update(last_attack_update=util.tz_utcnow()) @api('/api/update_depth', methods=['POST'], auth_group=['admin']) async def update_recursion_limit(request): json = await request.json() if 'new_value' in json: Setting.objects.first().modify(recursion_limit=json['new_value']) @api('/api/group_mimic', methods=['GET'], auth_group=['admin', 'human']) async def group_coverage(request): temp_list = [] core = {} for step in CodedStep.objects: for mapping in step.mapping: temp_list.append(mapping.technique) groups = AttackGroup.objects for entry in groups: temp = {} breakdown = {} decision = [] for tech in entry.techniques: temp[tech.name] = (tech in temp_list) decision.append(tech in temp_list) breakdown['techniques'] = temp if (False not in decision) and (len(decision) > 2): breakdown['conclusion'] = 'Can Fully Emulate' else: breakdown['conclusion'] = 'Can Not Fully Emulate' core[entry.name] = breakdown return core @api('/api/steps/{step}/mapping', methods=['POST', 'DELETE'], objects={'step': CodedStep}, auth_group=['human']) async def post_step_mapping(request, step): if request.method == 'POST': json = await request.json() if 'tactics' not in json or 'technique' not in json: return tactics = json['tactics'] technique = json['technique'] try: tech = AttackTechnique.objects.with_id(technique) for tactic in tactics: tac = AttackTactic.objects.with_id(tactic) step.modify(push__mapping=TechniqueMapping(technique=tech, tactic=tac)) except (TypeError, mongoengine.errors.ValidationError): return elif request.method == 'DELETE': json = await request.json() if 'tactic' not in json or 'technique' not in json: return tactic = json['tactic'] technique = json['technique'] try: tech = AttackTechnique.objects.with_id(technique) tac = AttackTactic.objects.with_id(tactic) for mapping in step.mapping: if mapping.tactic == tac and mapping.technique == tech: step.modify(pull__mapping=mapping) except (TypeError, mongoengine.errors.ValidationError): return @api('/api/steps/{step}/mapping/load_defaults', methods=['GET'], objects={'step': CodedStep}, auth_group=['human']) async def get_step_mapping_defaults(request, step): step.update(mapping=step.default_mapping) @api('/api/attack_download.json', methods=['GET'], auth_group=['human'], headers={'Content-Disposition': 'attachment'}) async def get_all_attack_stuff(request): try: techniques = [] for technique in AttackTechnique.objects: this_technique = technique.to_dict() this_technique['tactics'] = [x.name for x in technique.tactics] del this_technique['_id'] techniques.append(this_technique) tactics = [x.to_dict() for x in AttackTactic.objects] for tactic in tactics: del tactic["_id"] return {"techniques": techniques, "tactics": tactics} except (TypeError, mongoengine.errors.ValidationError): return @api('/api/generated/{function}', methods=["POST"], auth_group=['admin']) async def generated_dispatcher(request): dispatched_function = request.match_info['function'] request_json = await request.json() job = getattr(interface, dispatched_function)(**request_json) try: await job.wait_till_completed() return job.action['result'] except JobException: return job.action['error'] @api('/api/artifactlists', methods=['GET', 'POST'], auth_group=['human']) async def get_artifactlists(request): if request.method == 'GET': return Artifactlist.objects elif request.method == 'POST': if request.content_type == "application/json": content = await request.json() elif request.content_type == "text/x-yaml": try: content = format_yaml(await request.text()) except (yaml.scanner.ScannerError, yaml.parser.ParserError): return web.Response(status=400, text="The yaml was not properly formatted") else: return web.Response(status=400) try: return Artifactlist(**content).save().id except (mongoengine.errors.FieldDoesNotExist, mongoengine.errors.ValidationError) as e: return web.Response(status=400, text=str(e)) @api('/api/artifactlists/{artifactlist}', methods=['GET', 'PUT', 'DELETE'], objects={'artifactlist': Artifactlist}, auth_group=['human']) async def query_artifactlist(request, artifactlist): if request.method == 'GET': return artifactlist elif request.method == 'PUT': if request.content_type == "application/json": content = await request.json() elif request.content_type == "text/x-yaml": try: content = format_yaml(await request.text()) except (yaml.scanner.ScannerError, yaml.parser.ParserError): return web.Response(status=400, text="The yaml was not properly formatted") else: return web.Response(status=400) try: artifactlist.update(**content) return artifactlist.id except (mongoengine.errors.FieldDoesNotExist, mongoengine.errors.ValidationError) as e: return web.Response(status=400, text=str(e)) elif request.method == 'DELETE': return artifactlist.delete() @api('/api/parse_artifactlist', methods=['POST'], auth_group=['human']) async def get_parse_artifactlist(request): try: parsed = format_yaml(await request.text()) Artifactlist(**parsed) return parsed except (yaml.scanner.ScannerError, yaml.parser.ParserError) as e: return web.Response(status=400, text="The yaml was not properly formatted: \n" + str(e.problem_mark) + '\n ' + str(e.problem)) except (mongoengine.errors.FieldDoesNotExist, mongoengine.errors.ValidationError) as e: return web.Response(status=400, text=str(e)) def format_yaml(yaml_content): parsed = yaml.load(yaml_content) cleaned = {} for k, v in parsed.items(): if isinstance(v, list) and len(v) == 1 and v[0] is None: cleaned[k] = [] else: cleaned[k] = v return cleaned @api('/api/bsf/{log}', methods=['GET'], objects={'log': Log}, auth_group=['human'], headers={'Content-Disposition': 'attachment; filename=\"bsf.json\"'}) async def query_bsf(request, log): return log["event_stream"] @api('/api/observed/credentials', methods=['GET'], auth_group=['human']) async def query_credentials(request): return ObservedCredential.objects @api('/api/observed/credentials/{credential}', methods=['GET'], objects={'credential': ObservedCredential}, auth_group=['human']) async def query_credential(request, token, credential): return credential @api('/api/observed/users', methods=['GET'], auth_group=['human']) async def query_users(request): return ObservedUser.objects @api('/api/observed/users/{user}', methods=['GET'], objects={'user': ObservedUser}, auth_group=['human']) async def query_user(request, token, user): return user @api('/api/observed/shares', methods=['GET'], auth_group=['human']) async def query_shares(request): return ObservedShare.objects @api('/api/observed/shares/{share}', methods=['GET'], objects={'share': ObservedShare}, auth_group=['human']) async def query_share(request, token, share): return share @api('/api/observed/files', methods=['GET'], auth_group=['human']) async def query_files(request): return ObservedFile.objects @api('/api/observed/files/{file}', methods=['GET'], objects={'file': ObservedFile}, auth_group=['human']) async def query_file(request, token, file): return file @api('/api/observed/domains', methods=['GET'], auth_group=['human']) async def query_domains(request): return ObservedDomain.objects @api('/api/observed/domains/{domain}', methods=['GET'], objects={'domain': ObservedDomain}, auth_group=['human']) async def query_domain(request, token, domain): return domain @api('/api/observed/os_versions', methods=['GET'], auth_group=['human']) async def query_os_versions(request): return ObservedOsversion.objects @api('/api/observed/os_versions/{os_version}', methods=['GET'], objects={'os_version': ObservedOSVersion}, auth_group=['human']) async def query_os_version(request, token, os_version): return os_version @api('/api/observed/hosts', methods=['GET'], auth_group=['human']) async def query_hosts(request): return ObservedHost.objects @api('/api/observed/hosts/{host}', methods=['GET'], objects={'host': ObservedHost}, auth_group=['human']) async def query_host(request, token, host): return host @api('/api/observed/schtasks', methods=['GET'], auth_group=['human']) async def query_schtasks(request): return ObservedSchtask.objects @api('/api/observed/schtasks/{schtask}', methods=['GET'], objects={'schtask': ObservedSchtask}, auth_group=['human']) async def query_schtask(request, token, schtask): return schtask @api('/api/observed/services', methods=['GET'], auth_group=['human']) async def query_timedeltas(request): return ObservedService.objects @api('/api/observed/services/{service}', methods=['GET'], objects={'service': ObservedService}, auth_group=['human']) async def query_timedelta(request, token, service): return service @api('/api/observed/timedeltas', methods=['GET'], auth_group=['human']) async def query_timedeltas(request): return ObservedTimeDelta.objects @api('/api/observed/timedeltas/{timedelta}', methods=['GET'], objects={'timedelta': ObservedTimeDelta}, auth_group=['human']) async def query_timedelta(request, token, timedelta): return timedelta @api('/api/observed/rats', methods=['GET'], auth_group=['human']) async def query_rats(request): return ObservedRat.objects @api('/api/observed/rats/{rat}', methods=['GET'], objects={'rat': ObservedRat}, auth_group=['human']) async def query_rat(request, token, rat): return rat @api('/api/observed/registry_keys', methods=['GET'], auth_group=['human']) async def query_registry_keys(request): return ObservedRegKey.objects @api('/api/observed/registry_keys/{registry_key}', methods=['GET'], objects={'registry_key': ObservedRegKey}, auth_group=['human']) async def query_regkey(request, token, registry_key): return registry_key @api('/api/observed/persistence', methods=['GET'], auth_group=['human']) async def query_persistence_all(request): return ObservedPersistence.objects @api('/api/observed/persistence/{persistence}', methods=['GET'], objects={'persistence': ObservedPersistence}, auth_group=['human']) async def query_persistence(request, token, persistence): return persistence @api('/api/observed/processes', methods=['GET'], auth_group=['human']) async def query_processes(request): return ObservedProcess.objects @api('/api/observed/processes/{process}', methods=['GET'], objects={'process': ObservedProcess}, auth_group=['human']) async def query_process(request, token, process): return process @api('/api/step', methods=['GET'], auth_group=['human']) async def query_step(request): return CodedStep.objects @websocket('/websocket', auth_group=["human"]) async def wb_operation(request): ws = web.WebSocketResponse() await ws.prepare(request) def write_websocket(data): if not ws.closed: ws.send_bytes(data) else: raise RuntimeError srv = ddp.DDPServer(write_websocket) srv.register_collection("operation", Operation) srv.register_collection("domain", Domain) srv.register_collection("host", Host) srv.register_collection("network", Network) srv.register_collection("rat", Rat) srv.register_collection("observed_rat", ObservedRat) srv.register_collection("observed_host", ObservedHost) srv.register_collection("observed_file", ObservedFile) srv.register_collection("observed_schtask", ObservedSchtask) srv.register_collection("job", Job) srv.register_collection("log", Log) srv.register_collection("adversary", Adversary) srv.register_collection("step", CodedStep) srv.register_collection("active_connection", ActiveConnection) srv.register_collection("agent", Agent) srv.register_collection("attack_technique", AttackTechnique) srv.register_collection("attack_tactic", AttackTactic) srv.register_collection("attack_list", AttackList) srv.register_collection("attack_group", AttackGroup) srv.register_collection("setting", Setting) srv.register_collection("artifactlist", Artifactlist) request.app['websockets'].append(ws) try: async for msg in ws: if msg.type == aiohttp.WSMsgType.TEXT or msg.type == aiohttp.WSMsgType.BINARY: srv.parse_message(msg.data) elif msg.type == aiohttp.WSMsgType.ERROR: log.debug('ws connection closed with exception {}'.format(ws.exception())) finally: request.app['websockets'].remove(ws) log.debug('websocket connection closed') return ws def init(app): for method, uri, func in routes: app.router.add_route(method, uri, func)
true
true
f705ecdb7259ffcb800671f6f82c2aa41da950a6
480
py
Python
10_mobilenetv3/06_mobilenet_v3_large_224_dm07/01_float32/02_weight_quantization.py
khanfarhan10/PINTO_model_zoo
4cad2e506d8c0fb604aa7b5f84115a840ab59ba1
[ "MIT" ]
1,529
2019-12-11T13:36:23.000Z
2022-03-31T18:38:27.000Z
10_mobilenetv3/06_mobilenet_v3_large_224_dm07/01_float32/02_weight_quantization.py
khanfarhan10/PINTO_model_zoo
4cad2e506d8c0fb604aa7b5f84115a840ab59ba1
[ "MIT" ]
200
2020-01-06T09:24:42.000Z
2022-03-31T17:29:08.000Z
10_mobilenetv3/06_mobilenet_v3_large_224_dm07/01_float32/02_weight_quantization.py
khanfarhan10/PINTO_model_zoo
4cad2e506d8c0fb604aa7b5f84115a840ab59ba1
[ "MIT" ]
288
2020-02-21T14:56:02.000Z
2022-03-30T03:00:35.000Z
import tensorflow as tf tf.compat.v1.enable_eager_execution() # Weight Quantization - Input/Output=float32 converter = tf.lite.TFLiteConverter.from_saved_model('./saved_model') converter.optimizations = [tf.lite.Optimize.OPTIMIZE_FOR_SIZE] tflite_quant_model = converter.convert() with open('./mobilenet_v3_large_224_dm07_weight_quant.tflite', 'wb') as w: w.write(tflite_quant_model) print("Weight Quantization complete! - mobilenet_v3_large_224_dm07_weight_quant.tflite")
36.923077
88
0.816667
import tensorflow as tf tf.compat.v1.enable_eager_execution() converter = tf.lite.TFLiteConverter.from_saved_model('./saved_model') converter.optimizations = [tf.lite.Optimize.OPTIMIZE_FOR_SIZE] tflite_quant_model = converter.convert() with open('./mobilenet_v3_large_224_dm07_weight_quant.tflite', 'wb') as w: w.write(tflite_quant_model) print("Weight Quantization complete! - mobilenet_v3_large_224_dm07_weight_quant.tflite")
true
true
f705f177770c12c0106c8231abaa004e1def0c23
1,728
py
Python
src/simulator/workload/schemes.py
pskopnik/htc-cache-simulator
ee502db3f1c2b99ffe05ee609a18069b583798da
[ "MIT" ]
1
2020-12-15T16:09:31.000Z
2020-12-15T16:09:31.000Z
src/simulator/workload/schemes.py
pskopnik/htc-cache-system-simulator
ee502db3f1c2b99ffe05ee609a18069b583798da
[ "MIT" ]
null
null
null
src/simulator/workload/schemes.py
pskopnik/htc-cache-system-simulator
ee502db3f1c2b99ffe05ee609a18069b583798da
[ "MIT" ]
null
null
null
from typing import List from . import AccessRequest, FileID, PartSpec, PartsGenerator class NonCorrelatedSchemesGenerator(object): def __init__(self, number: int, fraction: float) -> None: self._number: int = number self._fraction: float = fraction self._parts_number: int = 2 ** number @property def number(self) -> int: return self._number @property def fraction(self) -> float: return self._fraction def parts(self, index: int, total_bytes: int) -> List[PartSpec]: scheme_parts_number = 2 ** (self._number - 1) parts: List[PartSpec] = [] for i in range(scheme_parts_number): # Insert 1 bit at index into binary representation of i part_index = (((i << 1 >> index) | 1) << index) | (i & ((1 << index) - 1)) containing_schemes = bin(part_index).count('1') part_bytes = round(total_bytes * ( self._fraction ** containing_schemes * (1 - self._fraction) ** (self._number - containing_schemes) )) parts.append((part_index, part_bytes)) return parts def access_request(self, index: int, file: FileID, total_bytes: int) -> AccessRequest: return AccessRequest(file, self.parts(index, total_bytes)) class WithIndex(PartsGenerator): def __init__(self, generator: 'NonCorrelatedSchemesGenerator', index: int) -> None: self._generator: NonCorrelatedSchemesGenerator = generator self._index: int = index def parts(self, total_bytes: int) -> List[PartSpec]: return self._generator.parts(self._index, total_bytes) def access_request(self, file: FileID, total_bytes: int) -> AccessRequest: return self._generator.access_request(self._index, file, total_bytes) def with_index(self, index: int) -> WithIndex: return self.WithIndex(self, index)
30.315789
87
0.712963
from typing import List from . import AccessRequest, FileID, PartSpec, PartsGenerator class NonCorrelatedSchemesGenerator(object): def __init__(self, number: int, fraction: float) -> None: self._number: int = number self._fraction: float = fraction self._parts_number: int = 2 ** number @property def number(self) -> int: return self._number @property def fraction(self) -> float: return self._fraction def parts(self, index: int, total_bytes: int) -> List[PartSpec]: scheme_parts_number = 2 ** (self._number - 1) parts: List[PartSpec] = [] for i in range(scheme_parts_number): part_index = (((i << 1 >> index) | 1) << index) | (i & ((1 << index) - 1)) containing_schemes = bin(part_index).count('1') part_bytes = round(total_bytes * ( self._fraction ** containing_schemes * (1 - self._fraction) ** (self._number - containing_schemes) )) parts.append((part_index, part_bytes)) return parts def access_request(self, index: int, file: FileID, total_bytes: int) -> AccessRequest: return AccessRequest(file, self.parts(index, total_bytes)) class WithIndex(PartsGenerator): def __init__(self, generator: 'NonCorrelatedSchemesGenerator', index: int) -> None: self._generator: NonCorrelatedSchemesGenerator = generator self._index: int = index def parts(self, total_bytes: int) -> List[PartSpec]: return self._generator.parts(self._index, total_bytes) def access_request(self, file: FileID, total_bytes: int) -> AccessRequest: return self._generator.access_request(self._index, file, total_bytes) def with_index(self, index: int) -> WithIndex: return self.WithIndex(self, index)
true
true
f705f24007c4d4c5b29712c358e78098ecba7ffc
434
py
Python
src/sentry/api/serializers/models/monitor.py
tomaszek1989/sentry
4486b70326fff96ab5148bb230c4287aa125e8a9
[ "BSD-3-Clause" ]
null
null
null
src/sentry/api/serializers/models/monitor.py
tomaszek1989/sentry
4486b70326fff96ab5148bb230c4287aa125e8a9
[ "BSD-3-Clause" ]
null
null
null
src/sentry/api/serializers/models/monitor.py
tomaszek1989/sentry
4486b70326fff96ab5148bb230c4287aa125e8a9
[ "BSD-3-Clause" ]
null
null
null
from __future__ import absolute_import import six from sentry.api.serializers import Serializer, register from sentry.models import Monitor @register(Monitor) class MonitorSerializer(Serializer): def serialize(self, obj, attrs, user): return { 'id': six.text_type(obj.guid), 'status': obj.get_status_display(), 'name': obj.name, 'dateCreated': obj.date_added, }
24.111111
55
0.656682
from __future__ import absolute_import import six from sentry.api.serializers import Serializer, register from sentry.models import Monitor @register(Monitor) class MonitorSerializer(Serializer): def serialize(self, obj, attrs, user): return { 'id': six.text_type(obj.guid), 'status': obj.get_status_display(), 'name': obj.name, 'dateCreated': obj.date_added, }
true
true
f705f26c35efa64bd18d29acc429de3a676fe490
236
py
Python
test/fixtures/python/get_with_data.py
dandycheung/curlconverter
5f7fb52258f2fb161dab4e7e6194e76a85d5a9a6
[ "MIT" ]
536
2021-10-06T17:21:25.000Z
2022-03-31T13:05:48.000Z
test/fixtures/python/get_with_data.py
dandycheung/curlconverter
5f7fb52258f2fb161dab4e7e6194e76a85d5a9a6
[ "MIT" ]
74
2021-10-08T13:57:14.000Z
2022-03-31T06:55:39.000Z
test/fixtures/python/get_with_data.py
dandycheung/curlconverter
5f7fb52258f2fb161dab4e7e6194e76a85d5a9a6
[ "MIT" ]
104
2021-10-06T19:36:15.000Z
2022-03-31T07:34:04.000Z
import requests headers = { 'X-Api-Key': '123456789', } params = { 'test': '2', 'limit': '100', 'w': '4', } response = requests.get('http://localhost:28139/synthetics/api/v3/monitors', params=params, headers=headers)
16.857143
108
0.610169
import requests headers = { 'X-Api-Key': '123456789', } params = { 'test': '2', 'limit': '100', 'w': '4', } response = requests.get('http://localhost:28139/synthetics/api/v3/monitors', params=params, headers=headers)
true
true
f705f28a7f85059eb3592621cd35b9680052d1b6
4,839
py
Python
main.py
santomet/OpenSupportTool-daemon
09bfe96925416ccd16b76b8f69acb4b6b030825c
[ "MIT" ]
null
null
null
main.py
santomet/OpenSupportTool-daemon
09bfe96925416ccd16b76b8f69acb4b6b030825c
[ "MIT" ]
null
null
null
main.py
santomet/OpenSupportTool-daemon
09bfe96925416ccd16b76b8f69acb4b6b030825c
[ "MIT" ]
null
null
null
import time from datetime import datetime from requests import Response import json import os import TunnelSSH import Helpers import SettingsStorage import sys def create_tunnel(tun: dict): timeout_time: datetime = datetime.fromisoformat(tun["timeout_time"]) if timeout_time <= datetime.utcnow(): Helpers.log_that("Tunnel already not valid") return if tun["connection_type"] == Helpers.ConnectionTypeEnum.ssh_tunnel: TunnelSSH.create_ssh_tunnel(tun) def destroy_tunnel(tun: dict): if tun["connection_type"] == Helpers.ConnectionTypeEnum.ssh_tunnel: TunnelSSH.destroy_ssh_tunnel(tun) def destroy_expired_tunnels(): for tun in SettingsStorage.datajson["tunnels"]: timeout_time: datetime = datetime.fromisoformat(tun["timeout_time"]) if timeout_time <= datetime.utcnow(): Helpers.log_that("A tunnel has expired, destroy") destroy_tunnel(tun) def act_on_tunnel(tun: dict): Helpers.log_that(tun) tunnel_id: int = tun["id"] type: Helpers.ConnectionTypeEnum = tun["connection_type"] state: Helpers.ConnectionStateEnum = tun["connection_state"] port_to_tunnel: int = tun["port_to_tunnel"] timeout_time: datetime = tun["timeout_time"] temporaray_pubkey: str = tun["temporary_pubkey_for_agent_ssh"] remote_ssh_server: str = tun["remote_ssh_server"] remote_ssh_fingerprint: str = tun["remote_ssh_fingerprint"] remote_ssh_username: str = tun["remote_ssh_fingerprint"] reverse_port: int = tun["reverse_port"] remote_ssh_port: int = tun["remote_ssh_port"] temporary_tunnel_privkey: str = tun["temporary_tunnel_privkey"] if state == Helpers.ConnectionStateEnum.connected: # first check what should we do: Helpers.log_that("Requesting connection that should already be connected, ignore") return elif state == Helpers.ConnectionStateEnum.requested: Helpers.log_that("Requesting new connection, act upon that!") create_tunnel(tun) elif state == Helpers.ConnectionStateEnum.disconnect_requested: Helpers.log_that("Requesting to destroy the connection id {}".format(tunnel_id)) destroy_tunnel(tun) def parse_success_resp(resp: Response): j: dict = resp.json() keys = j.keys() if "message" in keys and len(j["message"]) > 0: Helpers.log_that(j["message"]) if "tunnels_requesting_action" in keys and len(j["tunnels_requesting_action"]) > 0: Helpers.log_that("There are {} tunnels requesting action:".format(len(j["tunnels_requesting_action"]))) for tun in j["tunnels_requesting_action"]: act_on_tunnel(tun) def main(): # Our small local "db" consisting of Tunnels which are active while True: # Do this all the time try: # First check if this is installed, if not, send the installation data if not SettingsStorage.is_installed: resp: Response = Helpers.ReqSession.post(SettingsStorage.server_url + "/agents/agent_install", json=Helpers.get_install_json()) if resp.status_code == 200: SettingsStorage.is_installed = True SettingsStorage.datajson["is_installed"] = True Helpers.log_that("Successfully Installed!") else: msg = "" if "detail" in resp.json().keys(): msg = resp.json()["detail"] Helpers.log_that( "Error when trying to install the agent. Code {}, with message {}".format(str(resp.status_code), msg)) # First check if we have any Tunnel that should be disconnected TBD destroy_expired_tunnels() Helpers.remove_expired_ssh_auth_keys() resp: Response = Helpers.ReqSession.post(SettingsStorage.server_url + "/agents/query", json=Helpers.get_query_json()) if resp.status_code == 200: parse_success_resp(resp) else: msg = "" if "detail" in resp.json().keys(): msg = resp.json()["detail"] Helpers.log_that( "Error when querying the API. Code {}, with message {}".format(str(resp.status_code), msg)) except ValueError as e: Helpers.log_that("Could not process some value" + str(e.args)) except Exception as e: Helpers.log_that("Could not connect to server " + str(e.args)) datafile = open(os.path.join(sys.path[0], "data.json"), "w") json.dump(SettingsStorage.datajson, datafile) datafile.close() time.sleep(SettingsStorage.interval_seconds) if __name__ == "__main__": # execute only if run as a script main()
39.663934
129
0.646001
import time from datetime import datetime from requests import Response import json import os import TunnelSSH import Helpers import SettingsStorage import sys def create_tunnel(tun: dict): timeout_time: datetime = datetime.fromisoformat(tun["timeout_time"]) if timeout_time <= datetime.utcnow(): Helpers.log_that("Tunnel already not valid") return if tun["connection_type"] == Helpers.ConnectionTypeEnum.ssh_tunnel: TunnelSSH.create_ssh_tunnel(tun) def destroy_tunnel(tun: dict): if tun["connection_type"] == Helpers.ConnectionTypeEnum.ssh_tunnel: TunnelSSH.destroy_ssh_tunnel(tun) def destroy_expired_tunnels(): for tun in SettingsStorage.datajson["tunnels"]: timeout_time: datetime = datetime.fromisoformat(tun["timeout_time"]) if timeout_time <= datetime.utcnow(): Helpers.log_that("A tunnel has expired, destroy") destroy_tunnel(tun) def act_on_tunnel(tun: dict): Helpers.log_that(tun) tunnel_id: int = tun["id"] type: Helpers.ConnectionTypeEnum = tun["connection_type"] state: Helpers.ConnectionStateEnum = tun["connection_state"] port_to_tunnel: int = tun["port_to_tunnel"] timeout_time: datetime = tun["timeout_time"] temporaray_pubkey: str = tun["temporary_pubkey_for_agent_ssh"] remote_ssh_server: str = tun["remote_ssh_server"] remote_ssh_fingerprint: str = tun["remote_ssh_fingerprint"] remote_ssh_username: str = tun["remote_ssh_fingerprint"] reverse_port: int = tun["reverse_port"] remote_ssh_port: int = tun["remote_ssh_port"] temporary_tunnel_privkey: str = tun["temporary_tunnel_privkey"] if state == Helpers.ConnectionStateEnum.connected: Helpers.log_that("Requesting connection that should already be connected, ignore") return elif state == Helpers.ConnectionStateEnum.requested: Helpers.log_that("Requesting new connection, act upon that!") create_tunnel(tun) elif state == Helpers.ConnectionStateEnum.disconnect_requested: Helpers.log_that("Requesting to destroy the connection id {}".format(tunnel_id)) destroy_tunnel(tun) def parse_success_resp(resp: Response): j: dict = resp.json() keys = j.keys() if "message" in keys and len(j["message"]) > 0: Helpers.log_that(j["message"]) if "tunnels_requesting_action" in keys and len(j["tunnels_requesting_action"]) > 0: Helpers.log_that("There are {} tunnels requesting action:".format(len(j["tunnels_requesting_action"]))) for tun in j["tunnels_requesting_action"]: act_on_tunnel(tun) def main(): while True: try: if not SettingsStorage.is_installed: resp: Response = Helpers.ReqSession.post(SettingsStorage.server_url + "/agents/agent_install", json=Helpers.get_install_json()) if resp.status_code == 200: SettingsStorage.is_installed = True SettingsStorage.datajson["is_installed"] = True Helpers.log_that("Successfully Installed!") else: msg = "" if "detail" in resp.json().keys(): msg = resp.json()["detail"] Helpers.log_that( "Error when trying to install the agent. Code {}, with message {}".format(str(resp.status_code), msg)) destroy_expired_tunnels() Helpers.remove_expired_ssh_auth_keys() resp: Response = Helpers.ReqSession.post(SettingsStorage.server_url + "/agents/query", json=Helpers.get_query_json()) if resp.status_code == 200: parse_success_resp(resp) else: msg = "" if "detail" in resp.json().keys(): msg = resp.json()["detail"] Helpers.log_that( "Error when querying the API. Code {}, with message {}".format(str(resp.status_code), msg)) except ValueError as e: Helpers.log_that("Could not process some value" + str(e.args)) except Exception as e: Helpers.log_that("Could not connect to server " + str(e.args)) datafile = open(os.path.join(sys.path[0], "data.json"), "w") json.dump(SettingsStorage.datajson, datafile) datafile.close() time.sleep(SettingsStorage.interval_seconds) if __name__ == "__main__": main()
true
true
f705f318155534a100cd8e18c7b7f820f08fda0e
132
py
Python
cotede/humanqc/__init__.py
jessicaaustin/CoTeDe
0ca2a1c71de980d91262fd36fd5d8ab8cc09f019
[ "BSD-3-Clause" ]
35
2015-04-20T07:28:18.000Z
2022-02-23T08:26:28.000Z
cotede/humanqc/__init__.py
jessicaaustin/CoTeDe
0ca2a1c71de980d91262fd36fd5d8ab8cc09f019
[ "BSD-3-Clause" ]
55
2015-03-29T06:59:25.000Z
2021-04-20T17:38:09.000Z
cotede/humanqc/__init__.py
jessicaaustin/CoTeDe
0ca2a1c71de980d91262fd36fd5d8ab8cc09f019
[ "BSD-3-Clause" ]
12
2016-03-22T21:14:11.000Z
2022-01-03T04:43:36.000Z
#!/usr/bin/env python # Licensed under a 3-clause BSD style license - see LICENSE.rst from cotede.humanqc.humaneval import HumanQC
26.4
63
0.780303
from cotede.humanqc.humaneval import HumanQC
true
true
f705f3880d6c09e4f9449cdd939c9a729476b1c2
5,493
py
Python
senlin_tempest_plugin/functional/test_scaling_policy.py
tengqm/senlin-tempest-plugin
4c1901c1e78cac057ab73c51b44f046656dc3667
[ "Apache-2.0" ]
null
null
null
senlin_tempest_plugin/functional/test_scaling_policy.py
tengqm/senlin-tempest-plugin
4c1901c1e78cac057ab73c51b44f046656dc3667
[ "Apache-2.0" ]
null
null
null
senlin_tempest_plugin/functional/test_scaling_policy.py
tengqm/senlin-tempest-plugin
4c1901c1e78cac057ab73c51b44f046656dc3667
[ "Apache-2.0" ]
null
null
null
# Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain # a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. from tempest.lib import decorators from senlin_tempest_plugin.common import constants from senlin_tempest_plugin.common import utils from senlin_tempest_plugin.functional import base class TestScalingPolicy(base.BaseSenlinFunctionalTest): def setUp(self): super(TestScalingPolicy, self).setUp() self.profile_id = utils.create_a_profile(self) self.addCleanup(utils.delete_a_profile, self, self.profile_id) self.cluster_id = utils.create_a_cluster(self, self.profile_id, min_size=0, max_size=5, desired_capacity=1) self.addCleanup(utils.delete_a_cluster, self, self.cluster_id) @decorators.attr(type=['functional']) @decorators.idempotent_id('6b513a5d-75b6-447a-b95d-e17b84ac9ee8') def test_scaling_policy(self): # Create a scaling policy targets on CLUSTER_SCALE_OUT action spec = constants.spec_scaling_policy spec['properties'] = { 'event': 'CLUSTER_SCALE_OUT', 'adjustment': { 'type': 'CHANGE_IN_CAPACITY', 'number': 2, 'min_step': 1, 'best_effort': True } } policy_id = utils.create_a_policy(self, spec) scaleout_policy = utils.get_a_policy(self, policy_id) self.addCleanup(utils.delete_a_policy, self, scaleout_policy['id']) # Create a scaling policy targets on CLUSTER_SCALE_IN action spec['properties'] = { 'event': 'CLUSTER_SCALE_IN', 'adjustment': { 'type': 'CHANGE_IN_PERCENTAGE', 'number': 50, 'min_step': 2, 'best_effort': False } } policy_id = utils.create_a_policy(self, spec) scalein_policy = utils.get_a_policy(self, policy_id) self.addCleanup(utils.delete_a_policy, self, scalein_policy['id']) # Attach scale in/out policies to cluster for policy in [scaleout_policy, scalein_policy]: utils.cluster_attach_policy(self, self.cluster_id, policy['id']) self.addCleanup(utils.cluster_detach_policy, self, self.cluster_id, policy['id']) # Scale out cluster without count specified utils.cluster_scale_out(self, self.cluster_id) # Verify scale out result cluster = utils.get_a_cluster(self, self.cluster_id) self.assertEqual('ACTIVE', cluster['status']) self.assertEqual(3, cluster['desired_capacity']) self.assertEqual(3, len(cluster['nodes'])) # Scale out cluster with count set to 1 utils.cluster_scale_out(self, self.cluster_id, count=1) # Verify scale out result cluster = utils.get_a_cluster(self, self.cluster_id) self.assertEqual('ACTIVE', cluster['status']) self.assertEqual(4, cluster['desired_capacity']) self.assertEqual(4, len(cluster['nodes'])) # Keep scaling out cluster with count set to 2 to # verify best_effort parameter utils.cluster_scale_out(self, self.cluster_id, count=2) # Verify scale out result cluster = utils.get_a_cluster(self, self.cluster_id) self.assertEqual('ACTIVE', cluster['status']) self.assertEqual(5, cluster['desired_capacity']) self.assertEqual(5, len(cluster['nodes'])) # Scale in cluster without count specified utils.cluster_scale_in(self, self.cluster_id) # Verify scale in result cluster = utils.get_a_cluster(self, self.cluster_id) self.assertEqual('ACTIVE', cluster['status']) self.assertEqual(3, cluster['desired_capacity']) self.assertEqual(3, len(cluster['nodes'])) # Scale in cluster without count specified to # verify min_step parameter utils.cluster_scale_in(self, self.cluster_id) # Verify scale in result cluster = utils.get_a_cluster(self, self.cluster_id) self.assertEqual('ACTIVE', cluster['status']) self.assertEqual(1, cluster['desired_capacity']) self.assertEqual(1, len(cluster['nodes'])) # Keep scaling in cluster with count set to 2 to # verify best_effort parameter res = utils.cluster_scale_in(self, self.cluster_id, count=2, expected_status='FAILED') # Verify action result and action failure reason cluster = utils.get_a_cluster(self, self.cluster_id) self.assertEqual('ACTIVE', cluster['status']) self.assertEqual(1, cluster['desired_capacity']) self.assertEqual(1, len(cluster['nodes'])) reason = ("Policy check failure: Failed policy '%s': The target " "capacity (-1) is less than the cluster's " "min_size (0).") % scalein_policy['name'] self.assertEqual(reason, res)
42.253846
76
0.64209
from tempest.lib import decorators from senlin_tempest_plugin.common import constants from senlin_tempest_plugin.common import utils from senlin_tempest_plugin.functional import base class TestScalingPolicy(base.BaseSenlinFunctionalTest): def setUp(self): super(TestScalingPolicy, self).setUp() self.profile_id = utils.create_a_profile(self) self.addCleanup(utils.delete_a_profile, self, self.profile_id) self.cluster_id = utils.create_a_cluster(self, self.profile_id, min_size=0, max_size=5, desired_capacity=1) self.addCleanup(utils.delete_a_cluster, self, self.cluster_id) @decorators.attr(type=['functional']) @decorators.idempotent_id('6b513a5d-75b6-447a-b95d-e17b84ac9ee8') def test_scaling_policy(self): spec = constants.spec_scaling_policy spec['properties'] = { 'event': 'CLUSTER_SCALE_OUT', 'adjustment': { 'type': 'CHANGE_IN_CAPACITY', 'number': 2, 'min_step': 1, 'best_effort': True } } policy_id = utils.create_a_policy(self, spec) scaleout_policy = utils.get_a_policy(self, policy_id) self.addCleanup(utils.delete_a_policy, self, scaleout_policy['id']) spec['properties'] = { 'event': 'CLUSTER_SCALE_IN', 'adjustment': { 'type': 'CHANGE_IN_PERCENTAGE', 'number': 50, 'min_step': 2, 'best_effort': False } } policy_id = utils.create_a_policy(self, spec) scalein_policy = utils.get_a_policy(self, policy_id) self.addCleanup(utils.delete_a_policy, self, scalein_policy['id']) for policy in [scaleout_policy, scalein_policy]: utils.cluster_attach_policy(self, self.cluster_id, policy['id']) self.addCleanup(utils.cluster_detach_policy, self, self.cluster_id, policy['id']) utils.cluster_scale_out(self, self.cluster_id) cluster = utils.get_a_cluster(self, self.cluster_id) self.assertEqual('ACTIVE', cluster['status']) self.assertEqual(3, cluster['desired_capacity']) self.assertEqual(3, len(cluster['nodes'])) utils.cluster_scale_out(self, self.cluster_id, count=1) cluster = utils.get_a_cluster(self, self.cluster_id) self.assertEqual('ACTIVE', cluster['status']) self.assertEqual(4, cluster['desired_capacity']) self.assertEqual(4, len(cluster['nodes'])) utils.cluster_scale_out(self, self.cluster_id, count=2) cluster = utils.get_a_cluster(self, self.cluster_id) self.assertEqual('ACTIVE', cluster['status']) self.assertEqual(5, cluster['desired_capacity']) self.assertEqual(5, len(cluster['nodes'])) utils.cluster_scale_in(self, self.cluster_id) cluster = utils.get_a_cluster(self, self.cluster_id) self.assertEqual('ACTIVE', cluster['status']) self.assertEqual(3, cluster['desired_capacity']) self.assertEqual(3, len(cluster['nodes'])) utils.cluster_scale_in(self, self.cluster_id) cluster = utils.get_a_cluster(self, self.cluster_id) self.assertEqual('ACTIVE', cluster['status']) self.assertEqual(1, cluster['desired_capacity']) self.assertEqual(1, len(cluster['nodes'])) res = utils.cluster_scale_in(self, self.cluster_id, count=2, expected_status='FAILED') cluster = utils.get_a_cluster(self, self.cluster_id) self.assertEqual('ACTIVE', cluster['status']) self.assertEqual(1, cluster['desired_capacity']) self.assertEqual(1, len(cluster['nodes'])) reason = ("Policy check failure: Failed policy '%s': The target " "capacity (-1) is less than the cluster's " "min_size (0).") % scalein_policy['name'] self.assertEqual(reason, res)
true
true
f705f391ab810c9189b100ed39229631d36e2a29
213,437
py
Python
core/domain/exp_domain_test.py
yashdusing/oppia
c0218e13ba29f9bc25cc5ec6f7f13108ee4fdb9a
[ "Apache-2.0" ]
null
null
null
core/domain/exp_domain_test.py
yashdusing/oppia
c0218e13ba29f9bc25cc5ec6f7f13108ee4fdb9a
[ "Apache-2.0" ]
null
null
null
core/domain/exp_domain_test.py
yashdusing/oppia
c0218e13ba29f9bc25cc5ec6f7f13108ee4fdb9a
[ "Apache-2.0" ]
null
null
null
# coding: utf-8 # # Copyright 2014 The Oppia 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 exploration domain objects and methods defined on them.""" import copy import os import re from core.domain import exp_domain from core.domain import exp_fetchers from core.domain import exp_services from core.domain import html_validation_service from core.domain import param_domain from core.domain import state_domain from core.platform import models from core.tests import test_utils import feconf import utils (exp_models,) = models.Registry.import_models([models.NAMES.exploration]) def mock_get_filename_with_dimensions(filename, unused_exp_id): return html_validation_service.regenerate_image_filename_using_dimensions( filename, 490, 120) class ExplorationChangeTests(test_utils.GenericTestBase): def test_exp_change_object_with_missing_cmd(self): with self.assertRaisesRegexp( utils.ValidationError, 'Missing cmd key in change dict'): exp_domain.ExplorationChange({'invalid': 'data'}) def test_exp_change_object_with_invalid_cmd(self): with self.assertRaisesRegexp( utils.ValidationError, 'Command invalid is not allowed'): exp_domain.ExplorationChange({'cmd': 'invalid'}) def test_exp_change_object_with_missing_attribute_in_cmd(self): with self.assertRaisesRegexp( utils.ValidationError, ( 'The following required attributes are missing: ' 'new_value')): exp_domain.ExplorationChange({ 'cmd': 'edit_state_property', 'property_name': 'content', 'old_value': 'old_value' }) def test_exp_change_object_with_extra_attribute_in_cmd(self): with self.assertRaisesRegexp( utils.ValidationError, ( 'The following extra attributes are present: invalid')): exp_domain.ExplorationChange({ 'cmd': 'rename_state', 'old_state_name': 'old_state_name', 'new_state_name': 'new_state_name', 'invalid': 'invalid' }) def test_exp_change_object_with_invalid_exploration_property(self): with self.assertRaisesRegexp( utils.ValidationError, ( 'Value for property_name in cmd edit_exploration_property: ' 'invalid is not allowed')): exp_domain.ExplorationChange({ 'cmd': 'edit_exploration_property', 'property_name': 'invalid', 'old_value': 'old_value', 'new_value': 'new_value', }) def test_exp_change_object_with_invalid_state_property(self): with self.assertRaisesRegexp( utils.ValidationError, ( 'Value for property_name in cmd edit_state_property: ' 'invalid is not allowed')): exp_domain.ExplorationChange({ 'cmd': 'edit_state_property', 'state_name': 'state_name', 'property_name': 'invalid', 'old_value': 'old_value', 'new_value': 'new_value', }) def test_exp_change_object_with_create_new(self): exp_change_object = exp_domain.ExplorationChange({ 'cmd': 'create_new', 'category': 'category', 'title': 'title' }) self.assertEqual(exp_change_object.cmd, 'create_new') self.assertEqual(exp_change_object.category, 'category') self.assertEqual(exp_change_object.title, 'title') def test_exp_change_object_with_add_state(self): exp_change_object = exp_domain.ExplorationChange({ 'cmd': 'add_state', 'state_name': 'state_name', }) self.assertEqual(exp_change_object.cmd, 'add_state') self.assertEqual(exp_change_object.state_name, 'state_name') def test_exp_change_object_with_rename_state(self): exp_change_object = exp_domain.ExplorationChange({ 'cmd': 'rename_state', 'old_state_name': 'old_state_name', 'new_state_name': 'new_state_name' }) self.assertEqual(exp_change_object.cmd, 'rename_state') self.assertEqual(exp_change_object.old_state_name, 'old_state_name') self.assertEqual(exp_change_object.new_state_name, 'new_state_name') def test_exp_change_object_with_delete_state(self): exp_change_object = exp_domain.ExplorationChange({ 'cmd': 'delete_state', 'state_name': 'state_name', }) self.assertEqual(exp_change_object.cmd, 'delete_state') self.assertEqual(exp_change_object.state_name, 'state_name') def test_exp_change_object_with_edit_state_property(self): exp_change_object = exp_domain.ExplorationChange({ 'cmd': 'edit_state_property', 'state_name': 'state_name', 'property_name': 'content', 'new_value': 'new_value', 'old_value': 'old_value' }) self.assertEqual(exp_change_object.cmd, 'edit_state_property') self.assertEqual(exp_change_object.state_name, 'state_name') self.assertEqual(exp_change_object.property_name, 'content') self.assertEqual(exp_change_object.new_value, 'new_value') self.assertEqual(exp_change_object.old_value, 'old_value') def test_exp_change_object_with_edit_exploration_property(self): exp_change_object = exp_domain.ExplorationChange({ 'cmd': 'edit_exploration_property', 'property_name': 'title', 'new_value': 'new_value', 'old_value': 'old_value' }) self.assertEqual(exp_change_object.cmd, 'edit_exploration_property') self.assertEqual(exp_change_object.property_name, 'title') self.assertEqual(exp_change_object.new_value, 'new_value') self.assertEqual(exp_change_object.old_value, 'old_value') def test_exp_change_object_with_migrate_states_schema_to_latest_version( self): exp_change_object = exp_domain.ExplorationChange({ 'cmd': 'migrate_states_schema_to_latest_version', 'from_version': 'from_version', 'to_version': 'to_version', }) self.assertEqual( exp_change_object.cmd, 'migrate_states_schema_to_latest_version') self.assertEqual(exp_change_object.from_version, 'from_version') self.assertEqual(exp_change_object.to_version, 'to_version') def test_exp_change_object_with_revert_commit(self): exp_change_object = exp_domain.ExplorationChange({ 'cmd': exp_models.ExplorationModel.CMD_REVERT_COMMIT, 'version_number': 'version_number' }) self.assertEqual( exp_change_object.cmd, exp_models.ExplorationModel.CMD_REVERT_COMMIT) self.assertEqual(exp_change_object.version_number, 'version_number') def test_to_dict(self): exp_change_dict = { 'cmd': 'create_new', 'title': 'title', 'category': 'category' } exp_change_object = exp_domain.ExplorationChange(exp_change_dict) self.assertEqual(exp_change_object.to_dict(), exp_change_dict) class ExplorationVersionsDiffDomainUnitTests(test_utils.GenericTestBase): """Test the exploration versions difference domain object.""" def setUp(self): super(ExplorationVersionsDiffDomainUnitTests, self).setUp() self.exp_id = 'exp_id1' test_exp_filepath = os.path.join( feconf.TESTS_DATA_DIR, 'string_classifier_test.yaml') yaml_content = utils.get_file_contents(test_exp_filepath) assets_list = [] exp_services.save_new_exploration_from_yaml_and_assets( feconf.SYSTEM_COMMITTER_ID, yaml_content, self.exp_id, assets_list) self.exploration = exp_fetchers.get_exploration_by_id(self.exp_id) def test_correct_creation_of_version_diffs(self): # Rename a state. self.exploration.rename_state('Home', 'Renamed state') change_list = [exp_domain.ExplorationChange({ 'cmd': 'rename_state', 'old_state_name': 'Home', 'new_state_name': 'Renamed state' })] exp_versions_diff = exp_domain.ExplorationVersionsDiff(change_list) self.assertEqual(exp_versions_diff.added_state_names, []) self.assertEqual(exp_versions_diff.deleted_state_names, []) self.assertEqual( exp_versions_diff.old_to_new_state_names, { 'Home': 'Renamed state' }) self.exploration.version += 1 # Add a state. self.exploration.add_states(['New state']) self.exploration.states['New state'] = copy.deepcopy( self.exploration.states['Renamed state']) change_list = [exp_domain.ExplorationChange({ 'cmd': 'add_state', 'state_name': 'New state', })] exp_versions_diff = exp_domain.ExplorationVersionsDiff(change_list) self.assertEqual(exp_versions_diff.added_state_names, ['New state']) self.assertEqual(exp_versions_diff.deleted_state_names, []) self.assertEqual(exp_versions_diff.old_to_new_state_names, {}) self.exploration.version += 1 # Delete state. self.exploration.delete_state('New state') change_list = [exp_domain.ExplorationChange({ 'cmd': 'delete_state', 'state_name': 'New state' })] exp_versions_diff = exp_domain.ExplorationVersionsDiff(change_list) self.assertEqual(exp_versions_diff.added_state_names, []) self.assertEqual(exp_versions_diff.deleted_state_names, ['New state']) self.assertEqual(exp_versions_diff.old_to_new_state_names, {}) self.exploration.version += 1 # Test addition and multiple renames. self.exploration.add_states(['New state']) self.exploration.states['New state'] = copy.deepcopy( self.exploration.states['Renamed state']) self.exploration.rename_state('New state', 'New state2') self.exploration.rename_state('New state2', 'New state3') change_list = [exp_domain.ExplorationChange({ 'cmd': 'add_state', 'state_name': 'New state', }), exp_domain.ExplorationChange({ 'cmd': 'rename_state', 'old_state_name': 'New state', 'new_state_name': 'New state2' }), exp_domain.ExplorationChange({ 'cmd': 'rename_state', 'old_state_name': 'New state2', 'new_state_name': 'New state3' })] exp_versions_diff = exp_domain.ExplorationVersionsDiff(change_list) self.assertEqual(exp_versions_diff.added_state_names, ['New state3']) self.assertEqual(exp_versions_diff.deleted_state_names, []) self.assertEqual(exp_versions_diff.old_to_new_state_names, {}) self.exploration.version += 1 # Test addition, rename and deletion. self.exploration.add_states(['New state 2']) self.exploration.rename_state('New state 2', 'Renamed state 2') self.exploration.delete_state('Renamed state 2') change_list = [exp_domain.ExplorationChange({ 'cmd': 'add_state', 'state_name': 'New state 2' }), exp_domain.ExplorationChange({ 'cmd': 'rename_state', 'old_state_name': 'New state 2', 'new_state_name': 'Renamed state 2' }), exp_domain.ExplorationChange({ 'cmd': 'delete_state', 'state_name': 'Renamed state 2' })] exp_versions_diff = exp_domain.ExplorationVersionsDiff(change_list) self.assertEqual(exp_versions_diff.added_state_names, []) self.assertEqual(exp_versions_diff.deleted_state_names, []) self.assertEqual(exp_versions_diff.old_to_new_state_names, {}) self.exploration.version += 1 # Test multiple renames and deletion. self.exploration.rename_state('New state3', 'Renamed state 3') self.exploration.rename_state('Renamed state 3', 'Renamed state 4') self.exploration.delete_state('Renamed state 4') change_list = [exp_domain.ExplorationChange({ 'cmd': 'rename_state', 'old_state_name': 'New state3', 'new_state_name': 'Renamed state 3' }), exp_domain.ExplorationChange({ 'cmd': 'rename_state', 'old_state_name': 'Renamed state 3', 'new_state_name': 'Renamed state 4' }), exp_domain.ExplorationChange({ 'cmd': 'delete_state', 'state_name': 'Renamed state 4' })] exp_versions_diff = exp_domain.ExplorationVersionsDiff(change_list) self.assertEqual(exp_versions_diff.added_state_names, []) self.assertEqual( exp_versions_diff.deleted_state_names, ['New state3']) self.assertEqual(exp_versions_diff.old_to_new_state_names, {}) self.exploration.version += 1 def test_cannot_create_exploration_change_with_invalid_change_dict(self): with self.assertRaisesRegexp( Exception, 'Missing cmd key in change dict'): exp_domain.ExplorationChange({ 'invalid_cmd': 'invalid' }) def test_cannot_create_exploration_change_with_invalid_cmd(self): with self.assertRaisesRegexp( Exception, 'Command invalid_cmd is not allowed'): exp_domain.ExplorationChange({ 'cmd': 'invalid_cmd' }) def test_cannot_create_exploration_change_with_invalid_state_property(self): exp_change = exp_domain.ExplorationChange({ 'cmd': exp_domain.CMD_EDIT_STATE_PROPERTY, 'property_name': exp_domain.STATE_PROPERTY_INTERACTION_ID, 'state_name': '', 'new_value': '' }) self.assertTrue(isinstance(exp_change, exp_domain.ExplorationChange)) with self.assertRaisesRegexp( Exception, 'Value for property_name in cmd edit_state_property: ' 'invalid_property is not allowed'): exp_domain.ExplorationChange({ 'cmd': exp_domain.CMD_EDIT_STATE_PROPERTY, 'property_name': 'invalid_property', 'state_name': '', 'new_value': '' }) def test_cannot_create_exploration_change_with_invalid_exploration_property( self): exp_change = exp_domain.ExplorationChange({ 'cmd': exp_domain.CMD_EDIT_EXPLORATION_PROPERTY, 'property_name': 'title', 'new_value': '' }) self.assertTrue(isinstance(exp_change, exp_domain.ExplorationChange)) with self.assertRaisesRegexp( Exception, 'Value for property_name in cmd edit_exploration_property: ' 'invalid_property is not allowed'): exp_domain.ExplorationChange({ 'cmd': exp_domain.CMD_EDIT_EXPLORATION_PROPERTY, 'property_name': 'invalid_property', 'new_value': '' }) def test_revert_exploration_commit(self): exp_change = exp_domain.ExplorationChange({ 'cmd': exp_models.ExplorationModel.CMD_REVERT_COMMIT, 'version_number': 1 }) self.assertEqual(exp_change.version_number, 1) exp_change = exp_domain.ExplorationChange({ 'cmd': exp_models.ExplorationModel.CMD_REVERT_COMMIT, 'version_number': 2 }) self.assertEqual(exp_change.version_number, 2) class ExpVersionReferenceTests(test_utils.GenericTestBase): def test_create_exp_version_reference_object(self): exp_version_reference = exp_domain.ExpVersionReference('exp_id', 1) self.assertEqual( exp_version_reference.to_dict(), { 'exp_id': 'exp_id', 'version': 1 }) def test_validate_exp_version(self): with self.assertRaisesRegexp( Exception, 'Expected version to be an int, received invalid_version'): exp_domain.ExpVersionReference('exp_id', 'invalid_version') def test_validate_exp_id(self): with self.assertRaisesRegexp( Exception, 'Expected exp_id to be a str, received 0'): exp_domain.ExpVersionReference(0, 1) class ExplorationDomainUnitTests(test_utils.GenericTestBase): """Test the exploration domain object.""" # TODO(bhenning): The validation tests below should be split into separate # unit tests. Also, all validation errors should be covered in the tests. def test_validation(self): """Test validation of explorations.""" exploration = exp_domain.Exploration.create_default_exploration('eid') exploration.init_state_name = '' exploration.states = {} exploration.title = 'Hello #' self._assert_validation_error(exploration, 'Invalid character #') exploration.title = 'Title' exploration.category = 'Category' # Note: If '/' ever becomes a valid state name, ensure that the rule # editor frontend tenplate is fixed -- it currently uses '/' as a # sentinel for an invalid state name. bad_state = state_domain.State.create_default_state('/') exploration.states = {'/': bad_state} self._assert_validation_error( exploration, 'Invalid character / in a state name') new_state = state_domain.State.create_default_state('ABC') new_state.update_interaction_id('TextInput') # The 'states' property must be a non-empty dict of states. exploration.states = {} self._assert_validation_error( exploration, 'exploration has no states') exploration.states = {'A string #': new_state} self._assert_validation_error( exploration, 'Invalid character # in a state name') exploration.states = {'A string _': new_state} self._assert_validation_error( exploration, 'Invalid character _ in a state name') exploration.states = {'ABC': new_state} self._assert_validation_error( exploration, 'has no initial state name') exploration.init_state_name = 'initname' self._assert_validation_error( exploration, r'There is no state in \[\'ABC\'\] corresponding to ' 'the exploration\'s initial state name initname.') # Test whether a default outcome to a non-existing state is invalid. exploration.states = {exploration.init_state_name: new_state} self._assert_validation_error( exploration, 'destination ABC is not a valid') # Restore a valid exploration. init_state = exploration.states[exploration.init_state_name] default_outcome_dict = init_state.interaction.default_outcome.to_dict() default_outcome_dict['dest'] = exploration.init_state_name init_state.update_interaction_default_outcome(default_outcome_dict) exploration.validate() # Ensure an invalid destination can also be detected for answer groups. # Note: The state must keep its default_outcome, otherwise it will # trigger a validation error for non-terminal states needing to have a # default outcome. To validate the outcome of the answer group, this # default outcome must point to a valid state. init_state = exploration.states[exploration.init_state_name] default_outcome = init_state.interaction.default_outcome default_outcome.dest = exploration.init_state_name old_answer_groups = copy.deepcopy(init_state.interaction.answer_groups) old_answer_groups.append({ 'outcome': { 'dest': exploration.init_state_name, 'feedback': { 'content_id': 'feedback_1', 'html': '<p>Feedback</p>' }, 'labelled_as_correct': False, 'param_changes': [], 'refresher_exploration_id': None, 'missing_prerequisite_skill_id': None }, 'rule_specs': [{ 'inputs': { 'x': 'Test' }, 'rule_type': 'Contains' }], 'training_data': [], 'tagged_skill_misconception_id': None }) init_state.update_interaction_answer_groups(old_answer_groups) exploration.validate() interaction = init_state.interaction answer_groups = interaction.answer_groups answer_group = answer_groups[0] answer_group.outcome.dest = 'DEF' self._assert_validation_error( exploration, 'destination DEF is not a valid') # Restore a valid exploration. exploration.states[exploration.init_state_name].update_interaction_id( 'TextInput') answer_group.outcome.dest = exploration.init_state_name exploration.validate() # Validate RuleSpec. rule_spec = answer_group.rule_specs[0] rule_spec.inputs = {} self._assert_validation_error( exploration, 'RuleSpec \'Contains\' is missing inputs') rule_spec.inputs = 'Inputs string' self._assert_validation_error( exploration, 'Expected inputs to be a dict') rule_spec.inputs = {'x': 'Test'} rule_spec.rule_type = 'FakeRuleType' self._assert_validation_error(exploration, 'Unrecognized rule type') rule_spec.inputs = {'x': 15} rule_spec.rule_type = 'Contains' with self.assertRaisesRegexp( Exception, 'Expected unicode string, received 15' ): exploration.validate() rule_spec.inputs = {'x': '{{ExampleParam}}'} self._assert_validation_error( exploration, 'RuleSpec \'Contains\' has an input with name \'x\' which refers ' 'to an unknown parameter within the exploration: ExampleParam') # Restore a valid exploration. exploration.param_specs['ExampleParam'] = param_domain.ParamSpec( 'UnicodeString') exploration.validate() # Validate Outcome. outcome = answer_group.outcome destination = exploration.init_state_name outcome.dest = None self._assert_validation_error( exploration, 'Every outcome should have a destination.') # Try setting the outcome destination to something other than a string. outcome.dest = 15 self._assert_validation_error( exploration, 'Expected outcome dest to be a string') outcome.dest = destination outcome.feedback = state_domain.SubtitledHtml('feedback_1', '') exploration.validate() outcome.labelled_as_correct = 'hello' self._assert_validation_error( exploration, 'The "labelled_as_correct" field should be a boolean') # Test that labelled_as_correct must be False for self-loops, and that # this causes a strict validation failure but not a normal validation # failure. outcome.labelled_as_correct = True with self.assertRaisesRegexp( Exception, 'is labelled correct but is a self-loop.' ): exploration.validate(strict=True) exploration.validate() outcome.labelled_as_correct = False exploration.validate() outcome.param_changes = 'Changes' self._assert_validation_error( exploration, 'Expected outcome param_changes to be a list') outcome.param_changes = [param_domain.ParamChange( 0, 'generator_id', {})] self._assert_validation_error( exploration, 'Expected param_change name to be a string, received 0') outcome.param_changes = [] exploration.validate() outcome.refresher_exploration_id = 12345 self._assert_validation_error( exploration, 'Expected outcome refresher_exploration_id to be a string') outcome.refresher_exploration_id = None exploration.validate() outcome.refresher_exploration_id = 'valid_string' exploration.validate() outcome.missing_prerequisite_skill_id = 12345 self._assert_validation_error( exploration, 'Expected outcome missing_prerequisite_skill_id to be a string') outcome.missing_prerequisite_skill_id = None exploration.validate() outcome.missing_prerequisite_skill_id = 'valid_string' exploration.validate() # Test that refresher_exploration_id must be None for non-self-loops. new_state_name = 'New state' exploration.add_states([new_state_name]) outcome.dest = new_state_name outcome.refresher_exploration_id = 'another_string' self._assert_validation_error( exploration, 'has a refresher exploration ID, but is not a self-loop') outcome.refresher_exploration_id = None exploration.validate() exploration.delete_state(new_state_name) # Validate InteractionInstance. interaction.id = 15 self._assert_validation_error( exploration, 'Expected interaction id to be a string') interaction.id = 'SomeInteractionTypeThatDoesNotExist' self._assert_validation_error(exploration, 'Invalid interaction id') interaction.id = 'TextInput' exploration.validate() interaction.customization_args = [] self._assert_validation_error( exploration, 'Expected customization args to be a dict') interaction.customization_args = {15: ''} self._assert_validation_error( exploration, 'Invalid customization arg name') interaction.customization_args = {'placeholder': ''} exploration.validate() interaction.answer_groups = {} self._assert_validation_error( exploration, 'Expected answer groups to be a list') interaction.answer_groups = answer_groups interaction.id = 'EndExploration' self._assert_validation_error( exploration, 'Terminal interactions must not have a default outcome.') interaction.id = 'TextInput' init_state.update_interaction_default_outcome(None) self._assert_validation_error( exploration, 'Non-terminal interactions must have a default outcome.') interaction.id = 'EndExploration' self._assert_validation_error( exploration, 'Terminal interactions must not have any answer groups.') # A terminal interaction without a default outcome or answer group is # valid. This resets the exploration back to a valid state. init_state.update_interaction_answer_groups([]) exploration.validate() # Restore a valid exploration. interaction.id = 'TextInput' answer_groups_list = [ answer_group.to_dict() for answer_group in answer_groups] init_state.update_interaction_answer_groups(answer_groups_list) init_state.update_interaction_default_outcome(default_outcome.to_dict()) exploration.validate() init_state.update_interaction_solution({ 'answer_is_exclusive': True, 'correct_answer': 'hello_world!', 'explanation': { 'content_id': 'solution', 'html': 'hello_world is a string' } }) self._assert_validation_error( exploration, re.escape('Hint(s) must be specified if solution is specified')) init_state.update_interaction_solution(None) interaction.hints = {} self._assert_validation_error( exploration, 'Expected hints to be a list') interaction.hints = [] # Validate AnswerGroup. answer_groups_dict = { 'outcome': { 'dest': exploration.init_state_name, 'feedback': { 'content_id': 'feedback_1', 'html': 'Feedback' }, 'labelled_as_correct': False, 'param_changes': [], 'refresher_exploration_id': None, 'missing_prerequisite_skill_id': None }, 'rule_specs': [{ 'inputs': { 'x': 'Test' }, 'rule_type': 'Contains' }], 'training_data': [], 'tagged_skill_misconception_id': 1 } init_state.update_interaction_answer_groups([answer_groups_dict]) self._assert_validation_error( exploration, 'Expected tagged skill misconception id to be a str, received 1') answer_groups_dict = { 'outcome': { 'dest': exploration.init_state_name, 'feedback': { 'content_id': 'feedback_1', 'html': 'Feedback' }, 'labelled_as_correct': False, 'param_changes': [], 'refresher_exploration_id': None, 'missing_prerequisite_skill_id': None }, 'rule_specs': [{ 'inputs': { 'x': 'Test' }, 'rule_type': 'Contains' }], 'training_data': [], 'tagged_skill_misconception_id': 'invalid_tagged_skill_misconception_id' } init_state.update_interaction_answer_groups([answer_groups_dict]) self._assert_validation_error( exploration, 'Expected the format of tagged skill misconception id ' 'to be <skill_id>-<misconception_id>, received ' 'invalid_tagged_skill_misconception_id') init_state.interaction.answer_groups[0].rule_specs = {} self._assert_validation_error( exploration, 'Expected answer group rules to be a list') first_answer_group = init_state.interaction.answer_groups[0] first_answer_group.tagged_skill_misconception_id = None first_answer_group.rule_specs = [] self._assert_validation_error( exploration, 'There must be at least one rule or training data for each' ' answer group.') exploration.states = { exploration.init_state_name: ( state_domain.State.create_default_state( exploration.init_state_name)) } exploration.states[exploration.init_state_name].update_interaction_id( 'TextInput') exploration.validate() exploration.language_code = 'fake_code' self._assert_validation_error(exploration, 'Invalid language_code') exploration.language_code = 'English' self._assert_validation_error(exploration, 'Invalid language_code') exploration.language_code = 'en' exploration.validate() exploration.param_specs = 'A string' self._assert_validation_error(exploration, 'param_specs to be a dict') exploration.param_specs = { '@': param_domain.ParamSpec.from_dict({ 'obj_type': 'UnicodeString' }) } self._assert_validation_error( exploration, 'Only parameter names with characters') exploration.param_specs = { 'notAParamSpec': param_domain.ParamSpec.from_dict( {'obj_type': 'UnicodeString'}) } exploration.validate() def test_tag_validation(self): """Test validation of exploration tags.""" exploration = exp_domain.Exploration.create_default_exploration('eid') exploration.objective = 'Objective' init_state = exploration.states[exploration.init_state_name] init_state.update_interaction_id('EndExploration') init_state.update_interaction_default_outcome(None) exploration.validate() exploration.tags = 'this should be a list' self._assert_validation_error( exploration, 'Expected \'tags\' to be a list') exploration.tags = [123] self._assert_validation_error(exploration, 'to be a string') exploration.tags = ['abc', 123] self._assert_validation_error(exploration, 'to be a string') exploration.tags = [''] self._assert_validation_error(exploration, 'Tags should be non-empty') exploration.tags = ['123'] self._assert_validation_error( exploration, 'should only contain lowercase letters and spaces') exploration.tags = ['ABC'] self._assert_validation_error( exploration, 'should only contain lowercase letters and spaces') exploration.tags = [' a b'] self._assert_validation_error( exploration, 'Tags should not start or end with whitespace') exploration.tags = ['a b '] self._assert_validation_error( exploration, 'Tags should not start or end with whitespace') exploration.tags = ['a b'] self._assert_validation_error( exploration, 'Adjacent whitespace in tags should be collapsed') exploration.tags = ['abc', 'abc'] self._assert_validation_error( exploration, 'Some tags duplicate each other') exploration.tags = ['computer science', 'analysis', 'a b c'] exploration.validate() def test_title_category_and_objective_validation(self): """Test that titles, categories and objectives are validated only in 'strict' mode. """ self.save_new_valid_exploration( 'exp_id', 'user@example.com', title='', category='', objective='', end_state_name='End') exploration = exp_fetchers.get_exploration_by_id('exp_id') exploration.validate() with self.assertRaisesRegexp( utils.ValidationError, 'title must be specified' ): exploration.validate(strict=True) exploration.title = 'A title' with self.assertRaisesRegexp( utils.ValidationError, 'category must be specified' ): exploration.validate(strict=True) exploration.category = 'A category' with self.assertRaisesRegexp( utils.ValidationError, 'objective must be specified' ): exploration.validate(strict=True) exploration.objective = 'An objective' exploration.validate(strict=True) def test_get_trainable_states_dict(self): """Test the get_trainable_states_dict() method.""" exp_id = 'exp_id1' test_exp_filepath = os.path.join( feconf.TESTS_DATA_DIR, 'string_classifier_test.yaml') yaml_content = utils.get_file_contents(test_exp_filepath) assets_list = [] exp_services.save_new_exploration_from_yaml_and_assets( feconf.SYSTEM_COMMITTER_ID, yaml_content, exp_id, assets_list) exploration_model = exp_models.ExplorationModel.get( exp_id, strict=False) old_states = exp_fetchers.get_exploration_from_model( exploration_model).states exploration = exp_fetchers.get_exploration_by_id(exp_id) # Rename a state to add it in unchanged answer group. exploration.rename_state('Home', 'Renamed state') change_list = [exp_domain.ExplorationChange({ 'cmd': 'rename_state', 'old_state_name': 'Home', 'new_state_name': 'Renamed state' })] expected_dict = { 'state_names_with_changed_answer_groups': [], 'state_names_with_unchanged_answer_groups': ['Renamed state'] } exp_versions_diff = exp_domain.ExplorationVersionsDiff(change_list) actual_dict = exploration.get_trainable_states_dict( old_states, exp_versions_diff) self.assertEqual(actual_dict, expected_dict) # Modify answer groups to trigger change in answer groups. state = exploration.states['Renamed state'] exploration.states['Renamed state'].interaction.answer_groups.insert( 3, state.interaction.answer_groups[3]) answer_groups = [] for answer_group in state.interaction.answer_groups: answer_groups.append(answer_group.to_dict()) change_list = [exp_domain.ExplorationChange({ 'cmd': 'edit_state_property', 'state_name': 'Renamed state', 'property_name': 'answer_groups', 'new_value': answer_groups })] expected_dict = { 'state_names_with_changed_answer_groups': ['Renamed state'], 'state_names_with_unchanged_answer_groups': [] } exp_versions_diff = exp_domain.ExplorationVersionsDiff(change_list) actual_dict = exploration.get_trainable_states_dict( old_states, exp_versions_diff) self.assertEqual(actual_dict, expected_dict) # Add new state to trigger change in answer groups. exploration.add_states(['New state']) exploration.states['New state'] = copy.deepcopy( exploration.states['Renamed state']) change_list = [exp_domain.ExplorationChange({ 'cmd': 'add_state', 'state_name': 'New state', })] expected_dict = { 'state_names_with_changed_answer_groups': [ 'New state', 'Renamed state'], 'state_names_with_unchanged_answer_groups': [] } exp_versions_diff = exp_domain.ExplorationVersionsDiff(change_list) actual_dict = exploration.get_trainable_states_dict( old_states, exp_versions_diff) self.assertEqual(actual_dict, expected_dict) # Delete state. exploration.delete_state('New state') change_list = [exp_domain.ExplorationChange({ 'cmd': 'delete_state', 'state_name': 'New state' })] expected_dict = { 'state_names_with_changed_answer_groups': ['Renamed state'], 'state_names_with_unchanged_answer_groups': [] } exp_versions_diff = exp_domain.ExplorationVersionsDiff(change_list) actual_dict = exploration.get_trainable_states_dict( old_states, exp_versions_diff) self.assertEqual(actual_dict, expected_dict) # Test addition and multiple renames. exploration.add_states(['New state']) exploration.states['New state'] = copy.deepcopy( exploration.states['Renamed state']) exploration.rename_state('New state', 'New state2') exploration.rename_state('New state2', 'New state3') change_list = [exp_domain.ExplorationChange({ 'cmd': 'add_state', 'state_name': 'New state', }), exp_domain.ExplorationChange({ 'cmd': 'rename_state', 'old_state_name': 'New state', 'new_state_name': 'New state2' }), exp_domain.ExplorationChange({ 'cmd': 'rename_state', 'old_state_name': 'New state2', 'new_state_name': 'New state3' })] expected_dict = { 'state_names_with_changed_answer_groups': [ 'Renamed state', 'New state3'], 'state_names_with_unchanged_answer_groups': [] } exp_versions_diff = exp_domain.ExplorationVersionsDiff(change_list) actual_dict = exploration.get_trainable_states_dict( old_states, exp_versions_diff) self.assertEqual(actual_dict, expected_dict) def test_is_demo_property(self): """Test the is_demo property.""" demo = exp_domain.Exploration.create_default_exploration('0') self.assertEqual(demo.is_demo, True) notdemo1 = exp_domain.Exploration.create_default_exploration('a') self.assertEqual(notdemo1.is_demo, False) notdemo2 = exp_domain.Exploration.create_default_exploration('abcd') self.assertEqual(notdemo2.is_demo, False) def test_has_state_name(self): """Test for has_state_name.""" demo = exp_domain.Exploration.create_default_exploration('0') state_names = demo.states.keys() self.assertEqual(state_names, ['Introduction']) self.assertEqual(demo.has_state_name('Introduction'), True) self.assertEqual(demo.has_state_name('Fake state name'), False) def test_exploration_export_import(self): """Test that to_dict and from_dict preserve all data within an exploration. """ demo = exp_domain.Exploration.create_default_exploration('0') demo_dict = demo.to_dict() exp_from_dict = exp_domain.Exploration.from_dict(demo_dict) self.assertEqual(exp_from_dict.to_dict(), demo_dict) def test_interaction_with_none_id_is_not_terminal(self): """Test that an interaction with an id of None leads to is_terminal being false. """ # Default exploration has a default interaction with an ID of None. demo = exp_domain.Exploration.create_default_exploration('0') init_state = demo.states[feconf.DEFAULT_INIT_STATE_NAME] self.assertFalse(init_state.interaction.is_terminal) def test_cannot_create_demo_exp_with_invalid_param_changes(self): demo_exp = exp_domain.Exploration.create_default_exploration('0') demo_dict = demo_exp.to_dict() new_state = state_domain.State.create_default_state('new_state_name') new_state.param_changes = [param_domain.ParamChange.from_dict({ 'customization_args': { 'list_of_values': ['1', '2'], 'parse_with_jinja': False }, 'name': 'myParam', 'generator_id': 'RandomSelector' })] demo_dict['states']['new_state_name'] = new_state.to_dict() demo_dict['param_specs'] = { 'ParamSpec': {'obj_type': 'UnicodeString'} } with self.assertRaisesRegexp( Exception, 'Parameter myParam was used in a state but not ' 'declared in the exploration param_specs.'): exp_domain.Exploration.from_dict(demo_dict) def test_validate_exploration_category(self): exploration = self.save_new_valid_exploration( 'exp_id', 'user@example.com', title='', category='', objective='', end_state_name='End') exploration.validate() exploration.category = 1 with self.assertRaisesRegexp( Exception, 'Expected category to be a string, received 1'): exploration.validate() def test_validate_exploration_objective(self): exploration = self.save_new_valid_exploration( 'exp_id', 'user@example.com', title='', category='', objective='', end_state_name='End') exploration.validate() exploration.objective = 1 with self.assertRaisesRegexp( Exception, 'Expected objective to be a string, received 1'): exploration.validate() def test_validate_exploration_blurb(self): exploration = self.save_new_valid_exploration( 'exp_id', 'user@example.com', title='', category='', objective='', end_state_name='End') exploration.validate() exploration.blurb = 1 with self.assertRaisesRegexp( Exception, 'Expected blurb to be a string, received 1'): exploration.validate() def test_validate_exploration_language_code(self): exploration = self.save_new_valid_exploration( 'exp_id', 'user@example.com', title='', category='', objective='', end_state_name='End') exploration.validate() exploration.language_code = 1 with self.assertRaisesRegexp( Exception, 'Expected language_code to be a string, received 1'): exploration.validate() def test_validate_exploration_author_notes(self): exploration = self.save_new_valid_exploration( 'exp_id', 'user@example.com', title='', category='', objective='', end_state_name='End') exploration.validate() exploration.author_notes = 1 with self.assertRaisesRegexp( Exception, 'Expected author_notes to be a string, received 1'): exploration.validate() def test_validate_exploration_states(self): exploration = self.save_new_valid_exploration( 'exp_id', 'user@example.com', title='', category='', objective='', end_state_name='End') exploration.validate() exploration.states = 1 with self.assertRaisesRegexp( Exception, 'Expected states to be a dict, received 1'): exploration.validate() def test_validate_exploration_outcome_dest(self): exploration = self.save_new_valid_exploration( 'exp_id', 'user@example.com', title='', category='', objective='', end_state_name='End') exploration.validate() exploration.init_state.interaction.default_outcome.dest = None with self.assertRaisesRegexp( Exception, 'Every outcome should have a destination.'): exploration.validate() def test_validate_exploration_outcome_dest_type(self): exploration = self.save_new_valid_exploration( 'exp_id', 'user@example.com', title='', category='', objective='', end_state_name='End') exploration.validate() exploration.init_state.interaction.default_outcome.dest = 1 with self.assertRaisesRegexp( Exception, 'Expected outcome dest to be a string, received 1'): exploration.validate() def test_validate_exploration_states_schema_version(self): exploration = self.save_new_valid_exploration( 'exp_id', 'user@example.com', title='', category='', objective='', end_state_name='End') exploration.validate() exploration.states_schema_version = None with self.assertRaisesRegexp( Exception, 'This exploration has no states schema version.'): exploration.validate() def test_validate_exploration_auto_tts_enabled(self): exploration = self.save_new_valid_exploration( 'exp_id', 'user@example.com', title='', category='', objective='', end_state_name='End') exploration.validate() exploration.auto_tts_enabled = 1 with self.assertRaisesRegexp( Exception, 'Expected auto_tts_enabled to be a bool, received 1'): exploration.validate() def test_validate_exploration_correctness_feedback_enabled(self): exploration = self.save_new_valid_exploration( 'exp_id', 'user@example.com', title='', category='', objective='', end_state_name='End') exploration.validate() exploration.correctness_feedback_enabled = 1 with self.assertRaisesRegexp( Exception, 'Expected correctness_feedback_enabled to be a bool, received 1'): exploration.validate() def test_validate_exploration_param_specs(self): exploration = self.save_new_valid_exploration( 'exp_id', 'user@example.com', title='', category='', objective='', end_state_name='End') exploration.validate() exploration.param_specs = { 1: param_domain.ParamSpec.from_dict( {'obj_type': 'UnicodeString'}) } with self.assertRaisesRegexp( Exception, 'Expected parameter name to be a string, received 1'): exploration.validate() def test_validate_exploration_param_changes_type(self): exploration = self.save_new_valid_exploration( 'exp_id', 'user@example.com', title='', category='', objective='', end_state_name='End') exploration.validate() exploration.param_changes = 1 with self.assertRaisesRegexp( Exception, 'Expected param_changes to be a list, received 1'): exploration.validate() def test_validate_exploration_param_name(self): exploration = self.save_new_valid_exploration( 'exp_id', 'user@example.com', title='', category='', objective='', end_state_name='End') exploration.validate() exploration.param_changes = [param_domain.ParamChange.from_dict({ 'customization_args': { 'list_of_values': ['1', '2'], 'parse_with_jinja': False }, 'name': 'invalid', 'generator_id': 'RandomSelector' })] with self.assertRaisesRegexp( Exception, 'No parameter named \'invalid\' exists in this ' 'exploration'): exploration.validate() def test_validate_exploration_reserved_param_name(self): exploration = self.save_new_valid_exploration( 'exp_id', 'user@example.com', title='', category='', objective='', end_state_name='End') exploration.validate() exploration.param_changes = [param_domain.ParamChange.from_dict({ 'customization_args': { 'list_of_values': ['1', '2'], 'parse_with_jinja': False }, 'name': 'all', 'generator_id': 'RandomSelector' })] with self.assertRaisesRegexp( Exception, 'The exploration-level parameter with name \'all\' is ' 'reserved. Please choose a different name.'): exploration.validate() def test_validate_exploration_is_non_self_loop(self): exploration = self.save_new_valid_exploration( 'exp_id', 'user@example.com', title='', category='', objective='', end_state_name='End') exploration.validate() exploration.add_states(['DEF']) default_outcome_dict = { 'dest': 'DEF', 'feedback': { 'content_id': 'default_outcome', 'html': '<p>Default outcome for state1</p>' }, 'param_changes': [], 'labelled_as_correct': False, 'refresher_exploration_id': 'refresher_exploration_id', 'missing_prerequisite_skill_id': None } exploration.init_state.update_interaction_default_outcome( default_outcome_dict) with self.assertRaisesRegexp( Exception, 'The default outcome for state Introduction has a refresher ' 'exploration ID, but is not a self-loop.'): exploration.validate() def test_validate_exploration_answer_group_parameter(self): exploration = self.save_new_valid_exploration( 'exp_id', 'user@example.com', title='', category='', objective='', end_state_name='End') exploration.validate() param_changes = [{ 'customization_args': { 'list_of_values': ['1', '2'], 'parse_with_jinja': False }, 'name': 'ParamChange', 'generator_id': 'RandomSelector' }] answer_groups = [{ 'outcome': { 'dest': exploration.init_state_name, 'feedback': { 'content_id': 'feedback_1', 'html': 'Feedback' }, 'labelled_as_correct': False, 'param_changes': param_changes, 'refresher_exploration_id': None, 'missing_prerequisite_skill_id': None }, 'rule_specs': [{ 'inputs': { 'x': 'Test' }, 'rule_type': 'Contains' }], 'training_data': [], 'tagged_skill_misconception_id': None }] exploration.init_state.update_interaction_answer_groups(answer_groups) with self.assertRaisesRegexp( Exception, 'The parameter ParamChange was used in an answer group, ' 'but it does not exist in this exploration'): exploration.validate() def test_verify_all_states_reachable(self): exploration = self.save_new_valid_exploration( 'exp_id', 'owner_id') exploration.validate() exploration.add_states(['End']) end_state = exploration.states['End'] end_state.update_interaction_id('EndExploration') end_state.update_interaction_default_outcome(None) with self.assertRaisesRegexp( Exception, 'Please fix the following issues before saving this exploration: ' '1. The following states are not reachable from the initial state: ' 'End 2. It is impossible to complete the exploration from the ' 'following states: Introduction'): exploration.validate(strict=True) def test_update_init_state_name_with_invalid_state(self): exploration = self.save_new_valid_exploration( 'exp_id', 'user@example.com', title='title', category='category', objective='objective', end_state_name='End') exploration.update_init_state_name('End') self.assertEqual(exploration.init_state_name, 'End') with self.assertRaisesRegexp( Exception, 'Invalid new initial state name: invalid_state;'): exploration.update_init_state_name('invalid_state') def test_rename_state_with_invalid_state(self): exploration = self.save_new_valid_exploration( 'exp_id', 'user@example.com', title='title', category='category', objective='objective', end_state_name='End') self.assertTrue(exploration.states.get('End')) self.assertFalse(exploration.states.get('new state name')) exploration.rename_state('End', 'new state name') self.assertFalse(exploration.states.get('End')) self.assertTrue(exploration.states.get('new state name')) with self.assertRaisesRegexp( Exception, 'State invalid_state does not exist'): exploration.rename_state('invalid_state', 'new state name') def test_default_outcome_is_labelled_incorrect_for_self_loop(self): exploration = self.save_new_valid_exploration( 'exp_id', 'user@example.com', title='title', category='category', objective='objective', end_state_name='End') exploration.validate(strict=True) (exploration.init_state.interaction.default_outcome .labelled_as_correct) = True (exploration.init_state.interaction.default_outcome .dest) = exploration.init_state_name with self.assertRaisesRegexp( Exception, 'The default outcome for state Introduction is labelled ' 'correct but is a self-loop'): exploration.validate(strict=True) class ExplorationSummaryTests(test_utils.GenericTestBase): def setUp(self): super(ExplorationSummaryTests, self).setUp() self.signup(self.OWNER_EMAIL, self.OWNER_USERNAME) owner_id = self.get_user_id_from_email(self.OWNER_EMAIL) exploration = exp_domain.Exploration.create_default_exploration('eid') exp_services.save_new_exploration(owner_id, exploration) self.exp_summary = exp_fetchers.get_exploration_summary_by_id('eid') def test_validation_passes_with_valid_properties(self): self.exp_summary.validate() def test_validation_fails_with_invalid_title(self): self.exp_summary.title = 0 with self.assertRaisesRegexp( utils.ValidationError, 'Expected title to be a string, received 0'): self.exp_summary.validate() def test_validation_fails_with_invalid_category(self): self.exp_summary.category = 0 with self.assertRaisesRegexp( utils.ValidationError, 'Expected category to be a string, received 0'): self.exp_summary.validate() def test_validation_fails_with_invalid_objective(self): self.exp_summary.objective = 0 with self.assertRaisesRegexp( utils.ValidationError, 'Expected objective to be a string, received 0'): self.exp_summary.validate() def test_validation_fails_with_invalid_language_code(self): self.exp_summary.language_code = 0 with self.assertRaisesRegexp( utils.ValidationError, 'Expected language_code to be a string, received 0'): self.exp_summary.validate() def test_validation_fails_with_unallowed_language_code(self): self.exp_summary.language_code = 'invalid' with self.assertRaisesRegexp( utils.ValidationError, 'Invalid language_code: invalid'): self.exp_summary.validate() def test_validation_fails_with_invalid_tags(self): self.exp_summary.tags = 'tags' with self.assertRaisesRegexp( utils.ValidationError, 'Expected \'tags\' to be a list, received tags'): self.exp_summary.validate() def test_validation_fails_with_invalid_tag_in_tags(self): self.exp_summary.tags = ['tag', 2] with self.assertRaisesRegexp( utils.ValidationError, 'Expected each tag in \'tags\' to be a string, received \'2\''): self.exp_summary.validate() def test_validation_fails_with_empty_tag_in_tags(self): self.exp_summary.tags = ['', 'abc'] with self.assertRaisesRegexp( utils.ValidationError, 'Tags should be non-empty'): self.exp_summary.validate() def test_validation_fails_with_unallowed_characters_in_tag(self): self.exp_summary.tags = ['123', 'abc'] with self.assertRaisesRegexp( utils.ValidationError, ( 'Tags should only contain lowercase ' 'letters and spaces, received \'123\'')): self.exp_summary.validate() def test_validation_fails_with_whitespace_in_tag_start(self): self.exp_summary.tags = [' ab', 'abc'] with self.assertRaisesRegexp( utils.ValidationError, 'Tags should not start or end with whitespace, received \' ab\''): self.exp_summary.validate() def test_validation_fails_with_whitespace_in_tag_end(self): self.exp_summary.tags = ['ab ', 'abc'] with self.assertRaisesRegexp( utils.ValidationError, 'Tags should not start or end with whitespace, received \'ab \''): self.exp_summary.validate() def test_validation_fails_with_adjacent_whitespace_in_tag(self): self.exp_summary.tags = ['a b', 'abc'] with self.assertRaisesRegexp( utils.ValidationError, ( 'Adjacent whitespace in tags should ' 'be collapsed, received \'a b\'')): self.exp_summary.validate() def test_validation_fails_with_duplicate_tags(self): self.exp_summary.tags = ['abc', 'abc', 'ab'] with self.assertRaisesRegexp( utils.ValidationError, 'Some tags duplicate each other'): self.exp_summary.validate() def test_validation_fails_with_invalid_rating_type(self): self.exp_summary.ratings = 0 with self.assertRaisesRegexp( utils.ValidationError, 'Expected ratings to be a dict, received 0'): self.exp_summary.validate() def test_validation_fails_with_invalid_rating_keys(self): self.exp_summary.ratings = {'1': 0, '10': 1} with self.assertRaisesRegexp( utils.ValidationError, 'Expected ratings to have keys: 1, 2, 3, 4, 5, received 1, 10'): self.exp_summary.validate() def test_validation_fails_with_invalid_value_type_for_ratings(self): self.exp_summary.ratings = {'1': 0, '2': 'one', '3': 0, '4': 0, '5': 0} with self.assertRaisesRegexp( utils.ValidationError, 'Expected value to be int, received one'): self.exp_summary.validate() def test_validation_fails_with_invalid_value_for_ratings(self): self.exp_summary.ratings = {'1': 0, '2': -1, '3': 0, '4': 0, '5': 0} with self.assertRaisesRegexp( utils.ValidationError, 'Expected value to be non-negative, received -1'): self.exp_summary.validate() def test_validation_fails_with_invalid_scaled_average_rating(self): self.exp_summary.scaled_average_rating = 'one' with self.assertRaisesRegexp( utils.ValidationError, 'Expected scaled_average_rating to be float, received one'): self.exp_summary.validate() def test_validation_fails_with_invalid_status(self): self.exp_summary.status = 0 with self.assertRaisesRegexp( utils.ValidationError, 'Expected status to be string, received 0'): self.exp_summary.validate() def test_validation_fails_with_invalid_community_owned(self): self.exp_summary.community_owned = '1' with self.assertRaisesRegexp( utils.ValidationError, 'Expected community_owned to be bool, received 1'): self.exp_summary.validate() def test_validation_fails_with_invalid_contributors_summary(self): self.exp_summary.contributors_summary = 0 with self.assertRaisesRegexp( utils.ValidationError, 'Expected contributors_summary to be dict, received 0'): self.exp_summary.validate() def test_validation_fails_with_invalid_owner_ids_type(self): self.exp_summary.owner_ids = 0 with self.assertRaisesRegexp( utils.ValidationError, 'Expected owner_ids to be list, received 0'): self.exp_summary.validate() def test_validation_fails_with_invalid_owner_id_in_owner_ids(self): self.exp_summary.owner_ids = ['1', 2, '3'] with self.assertRaisesRegexp( utils.ValidationError, 'Expected each id in owner_ids to be string, received 2'): self.exp_summary.validate() def test_validation_fails_with_invalid_editor_ids_type(self): self.exp_summary.editor_ids = 0 with self.assertRaisesRegexp( utils.ValidationError, 'Expected editor_ids to be list, received 0'): self.exp_summary.validate() def test_validation_fails_with_invalid_editor_id_in_editor_ids(self): self.exp_summary.editor_ids = ['1', 2, '3'] with self.assertRaisesRegexp( utils.ValidationError, 'Expected each id in editor_ids to be string, received 2'): self.exp_summary.validate() def test_validation_fails_with_invalid_voice_artist_ids_type(self): self.exp_summary.voice_artist_ids = 0 with self.assertRaisesRegexp( utils.ValidationError, 'Expected voice_artist_ids to be list, received 0'): self.exp_summary.validate() def test_validation_fails_with_invalid_voice_artist_id_in_voice_artists_ids( self): self.exp_summary.voice_artist_ids = ['1', 2, '3'] with self.assertRaisesRegexp( utils.ValidationError, 'Expected each id in voice_artist_ids to be string, received 2'): self.exp_summary.validate() def test_validation_fails_with_invalid_viewer_ids_type(self): self.exp_summary.viewer_ids = 0 with self.assertRaisesRegexp( utils.ValidationError, 'Expected viewer_ids to be list, received 0'): self.exp_summary.validate() def test_validation_fails_with_invalid_viewer_id_in_viewer_ids(self): self.exp_summary.viewer_ids = ['1', 2, '3'] with self.assertRaisesRegexp( utils.ValidationError, 'Expected each id in viewer_ids to be string, received 2'): self.exp_summary.validate() def test_validation_fails_with_invalid_contributor_ids_type(self): self.exp_summary.contributor_ids = 0 with self.assertRaisesRegexp( utils.ValidationError, 'Expected contributor_ids to be list, received 0'): self.exp_summary.validate() def test_validation_fails_with_invalid_contributor_id_in_contributor_ids( self): self.exp_summary.contributor_ids = ['1', 2, '3'] with self.assertRaisesRegexp( utils.ValidationError, 'Expected each id in contributor_ids to be string, received 2'): self.exp_summary.validate() class YamlCreationUnitTests(test_utils.GenericTestBase): """Test creation of explorations from YAML files.""" EXP_ID = 'An exploration_id' def test_yaml_import_and_export(self): """Test the from_yaml() and to_yaml() methods.""" exploration = exp_domain.Exploration.create_default_exploration( self.EXP_ID, title='Title', category='Category') exploration.add_states(['New state']) self.assertEqual(len(exploration.states), 2) exploration.validate() yaml_content = exploration.to_yaml() self.assertEqual(yaml_content, self.SAMPLE_YAML_CONTENT) exploration2 = exp_domain.Exploration.from_yaml('exp2', yaml_content) self.assertEqual(len(exploration2.states), 2) yaml_content_2 = exploration2.to_yaml() self.assertEqual(yaml_content_2, yaml_content) # Verify SAMPLE_UNTITLED_YAML_CONTENT can be converted to an exploration # without error. exp_domain.Exploration.from_untitled_yaml( 'exp4', 'Title', 'Category', self.SAMPLE_UNTITLED_YAML_CONTENT) with self.assertRaises(Exception): exp_domain.Exploration.from_yaml('exp3', 'No_initial_state_name') with self.assertRaises(Exception): exp_domain.Exploration.from_yaml( 'exp4', 'Invalid\ninit_state_name:\nMore stuff') with self.assertRaises(Exception): exp_domain.Exploration.from_yaml( 'exp4', 'State1:\n(\nInvalid yaml') with self.assertRaisesRegexp( Exception, 'Expected a YAML version >= 10, received: 9' ): exp_domain.Exploration.from_yaml( 'exp4', self.SAMPLE_UNTITLED_YAML_CONTENT) with self.assertRaisesRegexp( Exception, 'Expected a YAML version <= 9' ): exp_domain.Exploration.from_untitled_yaml( 'exp4', 'Title', 'Category', self.SAMPLE_YAML_CONTENT) class SchemaMigrationMethodsUnitTests(test_utils.GenericTestBase): """Tests the presence of appropriate schema migration methods in the Exploration domain object class. """ def test_correct_states_schema_conversion_methods_exist(self): """Test that the right states schema conversion methods exist.""" current_states_schema_version = ( feconf.CURRENT_STATE_SCHEMA_VERSION) for version_num in range(current_states_schema_version): self.assertTrue(hasattr( exp_domain.Exploration, '_convert_states_v%s_dict_to_v%s_dict' % ( version_num, version_num + 1))) self.assertFalse(hasattr( exp_domain.Exploration, '_convert_states_v%s_dict_to_v%s_dict' % ( current_states_schema_version, current_states_schema_version + 1))) def test_correct_exploration_schema_conversion_methods_exist(self): """Test that the right exploration schema conversion methods exist.""" current_exp_schema_version = ( exp_domain.Exploration.CURRENT_EXP_SCHEMA_VERSION) for version_num in range(1, current_exp_schema_version): self.assertTrue(hasattr( exp_domain.Exploration, '_convert_v%s_dict_to_v%s_dict' % ( version_num, version_num + 1))) self.assertFalse(hasattr( exp_domain.Exploration, '_convert_v%s_dict_to_v%s_dict' % ( current_exp_schema_version, current_exp_schema_version + 1))) class SchemaMigrationUnitTests(test_utils.GenericTestBase): """Test migration methods for yaml content.""" YAML_CONTENT_V1 = ("""default_skin: conversation_v1 param_changes: [] param_specs: {} schema_version: 1 states: - content: - type: text value: '' name: (untitled state) param_changes: [] widget: customization_args: {} handlers: - name: submit rule_specs: - definition: inputs: x: InputString name: Equals rule_type: atomic dest: END feedback: - Correct! param_changes: [] - definition: rule_type: default dest: (untitled state) feedback: [] param_changes: [] sticky: false widget_id: TextInput - content: - type: text value: '' name: New state param_changes: [] widget: customization_args: {} handlers: - name: submit rule_specs: - definition: rule_type: default dest: END feedback: [] param_changes: [] sticky: false widget_id: TextInput """) YAML_CONTENT_V2 = ("""default_skin: conversation_v1 init_state_name: (untitled state) param_changes: [] param_specs: {} schema_version: 2 states: (untitled state): content: - type: text value: '' param_changes: [] widget: customization_args: {} handlers: - name: submit rule_specs: - definition: inputs: x: InputString name: Equals rule_type: atomic dest: END feedback: - Correct! param_changes: [] - definition: rule_type: default dest: (untitled state) feedback: [] param_changes: [] sticky: false widget_id: TextInput New state: content: - type: text value: '' param_changes: [] widget: customization_args: {} handlers: - name: submit rule_specs: - definition: rule_type: default dest: END feedback: [] param_changes: [] sticky: false widget_id: TextInput """) YAML_CONTENT_V3 = ("""author_notes: '' blurb: '' default_skin: conversation_v1 init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 3 skill_tags: [] states: (untitled state): content: - type: text value: '' param_changes: [] widget: customization_args: placeholder: value: '' rows: value: 1 handlers: - name: submit rule_specs: - definition: inputs: x: InputString name: Equals rule_type: atomic dest: END feedback: - Correct! param_changes: [] - definition: rule_type: default dest: (untitled state) feedback: [] param_changes: [] sticky: false widget_id: TextInput New state: content: - type: text value: '' param_changes: [] widget: customization_args: placeholder: value: '' rows: value: 1 handlers: - name: submit rule_specs: - definition: rule_type: default dest: END feedback: [] param_changes: [] sticky: false widget_id: TextInput """) YAML_CONTENT_V4 = ("""author_notes: '' blurb: '' default_skin: conversation_v1 init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 4 skill_tags: [] states: (untitled state): content: - type: text value: '' interaction: customization_args: placeholder: value: '' rows: value: 1 handlers: - name: submit rule_specs: - definition: inputs: x: InputString name: Equals rule_type: atomic dest: END feedback: - Correct! param_changes: [] - definition: rule_type: default dest: (untitled state) feedback: [] param_changes: [] id: TextInput param_changes: [] New state: content: - type: text value: '' interaction: customization_args: placeholder: value: '' rows: value: 1 handlers: - name: submit rule_specs: - definition: rule_type: default dest: END feedback: [] param_changes: [] id: TextInput param_changes: [] """) YAML_CONTENT_V5 = ("""author_notes: '' blurb: '' default_skin: conversation_v1 init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 5 skin_customizations: panels_contents: {} states: (untitled state): content: - type: text value: '' interaction: customization_args: placeholder: value: '' rows: value: 1 handlers: - name: submit rule_specs: - definition: inputs: x: InputString name: Equals rule_type: atomic dest: END feedback: - Correct! param_changes: [] - definition: rule_type: default dest: (untitled state) feedback: [] param_changes: [] id: TextInput param_changes: [] New state: content: - type: text value: '' interaction: customization_args: placeholder: value: '' rows: value: 1 handlers: - name: submit rule_specs: - definition: rule_type: default dest: END feedback: [] param_changes: [] id: TextInput param_changes: [] widget: customization_args: {} handlers: - name: submit rule_specs: - definition: rule_type: default dest: END feedback: [] param_changes: [] sticky: false widget_id: TextInput END: content: - type: text value: Congratulations, you have finished! interaction: customization_args: recommendedExplorationIds: value: [] handlers: - name: submit rule_specs: - definition: rule_type: default dest: END feedback: [] param_changes: [] id: EndExploration triggers: [] param_changes: [] tags: [] """) YAML_CONTENT_V6 = ("""author_notes: '' blurb: '' default_skin: conversation_v1 init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 6 skin_customizations: panels_contents: {} states: (untitled state): content: - type: text value: '' interaction: customization_args: placeholder: value: '' rows: value: 1 handlers: - name: submit rule_specs: - definition: inputs: x: InputString name: Equals rule_type: atomic dest: END feedback: - Correct! param_changes: [] - definition: rule_type: default dest: (untitled state) feedback: [] param_changes: [] id: TextInput triggers: [] param_changes: [] END: content: - type: text value: Congratulations, you have finished! interaction: customization_args: recommendedExplorationIds: value: [] handlers: - name: submit rule_specs: - definition: rule_type: default dest: END feedback: [] param_changes: [] id: EndExploration triggers: [] param_changes: [] New state: content: - type: text value: '' interaction: customization_args: placeholder: value: '' rows: value: 1 handlers: - name: submit rule_specs: - definition: rule_type: default dest: END feedback: [] param_changes: [] id: TextInput triggers: [] param_changes: [] states_schema_version: 3 tags: [] """) YAML_CONTENT_V7 = ("""author_notes: '' blurb: '' default_skin: conversation_v1 init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 7 skin_customizations: panels_contents: {} states: (untitled state): content: - type: text value: '' interaction: answer_groups: - outcome: dest: END feedback: - Correct! param_changes: [] rule_specs: - inputs: x: InputString rule_type: Equals customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: [] param_changes: [] id: TextInput triggers: [] param_changes: [] END: content: - type: text value: Congratulations, you have finished! interaction: answer_groups: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null id: EndExploration triggers: [] param_changes: [] New state: content: - type: text value: '' interaction: answer_groups: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: [] param_changes: [] id: TextInput triggers: [] param_changes: [] states_schema_version: 4 tags: [] """) YAML_CONTENT_V8 = ("""author_notes: '' blurb: '' default_skin: conversation_v1 init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 8 skin_customizations: panels_contents: {} states: (untitled state): content: - type: text value: '' interaction: answer_groups: - outcome: dest: END feedback: - Correct! param_changes: [] rule_specs: - inputs: x: InputString rule_type: Equals customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: [] param_changes: [] fallbacks: [] id: TextInput param_changes: [] END: content: - type: text value: Congratulations, you have finished! interaction: answer_groups: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null fallbacks: [] id: EndExploration param_changes: [] New state: content: - type: text value: '' interaction: answer_groups: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: [] param_changes: [] fallbacks: [] id: TextInput param_changes: [] states_schema_version: 5 tags: [] """) YAML_CONTENT_V9 = ("""author_notes: '' blurb: '' default_skin: conversation_v1 init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 9 skin_customizations: panels_contents: {} states: (untitled state): content: - type: text value: '' interaction: answer_groups: - outcome: dest: END feedback: - Correct! param_changes: [] rule_specs: - inputs: x: InputString rule_type: Equals confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: [] param_changes: [] fallbacks: [] id: TextInput param_changes: [] END: content: - type: text value: Congratulations, you have finished! interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null fallbacks: [] id: EndExploration param_changes: [] New state: content: - type: text value: '' interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 language: value: '' default_outcome: dest: END feedback: [] param_changes: [] fallbacks: [] id: CodeRepl param_changes: [] states_schema_version: 6 tags: [] """) YAML_CONTENT_V10 = ("""author_notes: '' blurb: '' category: Category init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 10 skin_customizations: panels_contents: bottom: [] states: (untitled state): content: - type: text value: '' interaction: answer_groups: - outcome: dest: END feedback: - Correct! param_changes: [] rule_specs: - inputs: x: InputString rule_type: Equals confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: [] param_changes: [] fallbacks: [] id: TextInput param_changes: [] END: content: - type: text value: Congratulations, you have finished! interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null fallbacks: [] id: EndExploration param_changes: [] New state: content: - type: text value: '' interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: [] param_changes: [] fallbacks: [] id: TextInput param_changes: [] states_schema_version: 7 tags: [] title: Title """) YAML_CONTENT_V11 = ("""author_notes: '' blurb: '' category: Category init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 11 skin_customizations: panels_contents: bottom: [] states: (untitled state): classifier_model_id: null content: - type: text value: '' interaction: answer_groups: - outcome: dest: END feedback: - Correct! param_changes: [] rule_specs: - inputs: x: InputString rule_type: Equals confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: [] param_changes: [] fallbacks: [] id: TextInput param_changes: [] END: classifier_model_id: null content: - type: text value: Congratulations, you have finished! interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null fallbacks: [] id: EndExploration param_changes: [] New state: classifier_model_id: null content: - type: text value: '' interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: [] param_changes: [] fallbacks: [] id: TextInput param_changes: [] states_schema_version: 8 tags: [] title: Title """) YAML_CONTENT_V12 = ("""author_notes: '' blurb: '' category: Category init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 12 skin_customizations: panels_contents: bottom: [] states: (untitled state): classifier_model_id: null content: - type: text value: '' interaction: answer_groups: - correct: false outcome: dest: END feedback: - Correct! param_changes: [] rule_specs: - inputs: x: InputString rule_type: Equals confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: [] param_changes: [] fallbacks: [] id: TextInput param_changes: [] END: classifier_model_id: null content: - type: text value: Congratulations, you have finished! interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null fallbacks: [] id: EndExploration param_changes: [] New state: classifier_model_id: null content: - type: text value: '' interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: [] param_changes: [] fallbacks: - outcome: dest: END feedback: - Correct! id: TextInput param_changes: [] states_schema_version: 9 tags: [] title: Title """) YAML_CONTENT_V13 = ("""author_notes: '' blurb: '' category: Category init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 13 skin_customizations: panels_contents: bottom: [] states: (untitled state): classifier_model_id: null content: - type: text value: '' interaction: answer_groups: - correct: false outcome: dest: END feedback: - Correct! param_changes: [] rule_specs: - inputs: x: InputString rule_type: Equals confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: [] param_changes: [] fallbacks: [] hints: [] id: TextInput solution: {} param_changes: [] END: classifier_model_id: null content: - type: text value: Congratulations, you have finished! interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null fallbacks: [] hints: [] id: EndExploration solution: {} param_changes: [] New state: classifier_model_id: null content: - type: text value: '' interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: [] param_changes: [] fallbacks: [] hints: [] id: TextInput solution: {} param_changes: [] states_schema_version: 10 tags: [] title: Title """) YAML_CONTENT_V14 = ("""author_notes: '' blurb: '' category: Category init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 14 skin_customizations: panels_contents: bottom: [] states: (untitled state): classifier_model_id: null content: audio_translations: [] html: '' interaction: answer_groups: - correct: false outcome: dest: END feedback: - Correct! param_changes: [] rule_specs: - inputs: x: InputString rule_type: Equals confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: [] param_changes: [] fallbacks: [] hints: [] id: TextInput solution: {} param_changes: [] END: classifier_model_id: null content: audio_translations: [] html: Congratulations, you have finished! interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null fallbacks: [] hints: [] id: EndExploration solution: {} param_changes: [] New state: classifier_model_id: null content: audio_translations: [] html: '' interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: [] param_changes: [] fallbacks: [] hints: [] id: TextInput solution: {} param_changes: [] states_schema_version: 11 tags: [] title: Title """) YAML_CONTENT_V15 = ("""author_notes: '' blurb: '' category: Category init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 15 skin_customizations: panels_contents: bottom: [] states: (untitled state): classifier_model_id: null content: audio_translations: {} html: '' interaction: answer_groups: - correct: false outcome: dest: END feedback: - Correct! param_changes: [] rule_specs: - inputs: x: InputString rule_type: Equals confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: [] param_changes: [] fallbacks: [] hints: [] id: TextInput solution: {} param_changes: [] END: classifier_model_id: null content: audio_translations: {} html: Congratulations, you have finished! interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null fallbacks: [] hints: [] id: EndExploration solution: {} param_changes: [] New state: classifier_model_id: null content: audio_translations: {} html: '' interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: [] param_changes: [] fallbacks: [] hints: [] id: TextInput solution: {} param_changes: [] states_schema_version: 12 tags: [] title: Title """) YAML_CONTENT_V16 = ("""author_notes: '' blurb: '' category: Category init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 16 skin_customizations: panels_contents: bottom: [] states: (untitled state): classifier_model_id: null content: audio_translations: {} html: '' interaction: answer_groups: - correct: false outcome: dest: END feedback: - Correct! param_changes: [] rule_specs: - inputs: x: InputString rule_type: Equals confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: [] param_changes: [] hints: [] id: TextInput solution: null param_changes: [] END: classifier_model_id: null content: audio_translations: {} html: Congratulations, you have finished! interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null hints: [] id: EndExploration solution: null param_changes: [] New state: classifier_model_id: null content: audio_translations: {} html: '' interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: [] param_changes: [] hints: [] id: TextInput solution: null param_changes: [] states_schema_version: 13 tags: [] title: Title """) YAML_CONTENT_V17 = ("""author_notes: '' blurb: '' category: Category init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 17 states: (untitled state): classifier_model_id: null content: audio_translations: {} html: '' interaction: answer_groups: - correct: false outcome: dest: END feedback: - Correct! param_changes: [] rule_specs: - inputs: x: InputString rule_type: Equals confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: [] param_changes: [] hints: [] id: TextInput solution: null param_changes: [] END: classifier_model_id: null content: audio_translations: {} html: Congratulations, you have finished! interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null hints: [] id: EndExploration solution: null param_changes: [] New state: classifier_model_id: null content: audio_translations: {} html: '' interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: [] param_changes: [] hints: [] id: TextInput solution: null param_changes: [] states_schema_version: 13 tags: [] title: Title """) YAML_CONTENT_V18 = ("""author_notes: '' auto_tts_enabled: true blurb: '' category: Category init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 18 states: (untitled state): classifier_model_id: null content: audio_translations: {} html: '' interaction: answer_groups: - correct: false outcome: dest: END feedback: - Correct! param_changes: [] rule_specs: - inputs: x: InputString rule_type: Equals confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: [] param_changes: [] hints: [] id: TextInput solution: null param_changes: [] END: classifier_model_id: null content: audio_translations: {} html: Congratulations, you have finished! interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null hints: [] id: EndExploration solution: null param_changes: [] New state: classifier_model_id: null content: audio_translations: {} html: '' interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: [] param_changes: [] hints: - hint_text: '' id: TextInput solution: explanation: '' answer_is_exclusive: False correct_answer: Answer param_changes: [] states_schema_version: 13 tags: [] title: Title """) YAML_CONTENT_V19 = ("""author_notes: '' auto_tts_enabled: true blurb: '' category: Category init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 19 states: (untitled state): classifier_model_id: null content: audio_translations: {} html: '' interaction: answer_groups: - correct: false outcome: dest: END feedback: audio_translations: {} html: Correct! param_changes: [] rule_specs: - inputs: x: InputString rule_type: Equals confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: audio_translations: {} html: '' param_changes: [] hints: [] id: TextInput solution: null param_changes: [] END: classifier_model_id: null content: audio_translations: {} html: Congratulations, you have finished! interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null hints: [] id: EndExploration solution: null param_changes: [] New state: classifier_model_id: null content: audio_translations: {} html: '' interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: audio_translations: {} html: '' param_changes: [] hints: [] id: TextInput solution: null param_changes: [] states_schema_version: 14 tags: [] title: Title """) YAML_CONTENT_V20 = ("""author_notes: '' auto_tts_enabled: true blurb: '' category: Category correctness_feedback_enabled: false init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 20 states: (untitled state): classifier_model_id: null content: audio_translations: {} html: '' interaction: answer_groups: - labelled_as_correct: false outcome: dest: END feedback: audio_translations: {} html: Correct! param_changes: [] rule_specs: - inputs: x: InputString rule_type: Equals confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: audio_translations: {} html: '' param_changes: [] hints: [] id: TextInput solution: null param_changes: [] END: classifier_model_id: null content: audio_translations: {} html: Congratulations, you have finished! interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null hints: [] id: EndExploration solution: null param_changes: [] New state: classifier_model_id: null content: audio_translations: {} html: '' interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: audio_translations: {} html: '' param_changes: [] hints: [] id: TextInput solution: null param_changes: [] states_schema_version: 15 tags: [] title: Title """) YAML_CONTENT_V21 = ("""author_notes: '' auto_tts_enabled: true blurb: '' category: Category correctness_feedback_enabled: false init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 21 states: (untitled state): classifier_model_id: null content: audio_translations: {} html: '' interaction: answer_groups: - labelled_as_correct: false outcome: dest: END feedback: audio_translations: {} html: Correct! param_changes: [] refresher_exploration_id: null rule_specs: - inputs: x: InputString rule_type: Equals confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: audio_translations: {} html: '' param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] END: classifier_model_id: null content: audio_translations: {} html: Congratulations, you have finished! interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null hints: [] id: EndExploration solution: null param_changes: [] New state: classifier_model_id: null content: audio_translations: {} html: '' interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: audio_translations: {} html: '' param_changes: [] refresher_exploration_id: null hints: [] id: FractionInput solution: null param_changes: [] states_schema_version: 16 tags: [] title: Title """) YAML_CONTENT_V22 = ("""author_notes: '' auto_tts_enabled: true blurb: '' category: Category correctness_feedback_enabled: false init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 22 states: (untitled state): classifier_model_id: null content: audio_translations: {} html: '' interaction: answer_groups: - outcome: dest: END feedback: audio_translations: {} html: Correct! labelled_as_correct: false param_changes: [] refresher_exploration_id: null rule_specs: - inputs: x: InputString rule_type: Equals confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: audio_translations: {} html: '' labelled_as_correct: false param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] END: classifier_model_id: null content: audio_translations: {} html: Congratulations, you have finished! interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null hints: [] id: EndExploration solution: null param_changes: [] New state: classifier_model_id: null content: audio_translations: {} html: '' interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: audio_translations: {} html: '' labelled_as_correct: false param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] states_schema_version: 17 tags: [] title: Title """) YAML_CONTENT_V23 = ("""author_notes: '' auto_tts_enabled: true blurb: '' category: Category correctness_feedback_enabled: false init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 23 states: (untitled state): classifier_model_id: null content: audio_translations: {} html: '' interaction: answer_groups: - outcome: dest: END feedback: audio_translations: {} html: Correct! labelled_as_correct: false param_changes: [] refresher_exploration_id: null rule_specs: - inputs: x: InputString rule_type: Equals confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: audio_translations: {} html: '' labelled_as_correct: false param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] END: classifier_model_id: null content: audio_translations: {} html: Congratulations, you have finished! interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null hints: [] id: EndExploration solution: null param_changes: [] New state: classifier_model_id: null content: audio_translations: {} html: '' interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: audio_translations: {} html: '' labelled_as_correct: false param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] states_schema_version: 18 tags: [] title: Title """) YAML_CONTENT_V24 = ("""author_notes: '' auto_tts_enabled: true blurb: '' category: Category correctness_feedback_enabled: false init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 24 states: (untitled state): classifier_model_id: null content: audio_translations: {} html: '' interaction: answer_groups: - outcome: dest: END feedback: audio_translations: {} html: Correct! labelled_as_correct: false param_changes: [] refresher_exploration_id: null rule_specs: - inputs: x: InputString rule_type: Equals training_data: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: audio_translations: {} html: '' labelled_as_correct: false param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] END: classifier_model_id: null content: audio_translations: {} html: Congratulations, you have finished! interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null hints: [] id: EndExploration solution: null param_changes: [] New state: classifier_model_id: null content: audio_translations: {} html: '' interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: audio_translations: {} html: '' labelled_as_correct: false param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] states_schema_version: 19 tags: [] title: Title """) YAML_CONTENT_V25 = ("""author_notes: '' auto_tts_enabled: true blurb: '' category: Category correctness_feedback_enabled: false init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 25 states: (untitled state): classifier_model_id: null content: audio_translations: {} html: '' interaction: answer_groups: - outcome: dest: END feedback: audio_translations: {} html: Correct! labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null rule_specs: - inputs: x: InputString rule_type: Equals tagged_misconception_id: null training_data: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: audio_translations: {} html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] END: classifier_model_id: null content: audio_translations: {} html: Congratulations, you have finished! interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null hints: [] id: EndExploration solution: null param_changes: [] New state: classifier_model_id: null content: audio_translations: {} html: '' interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: audio_translations: {} html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] states_schema_version: 20 tags: [] title: Title """) YAML_CONTENT_V26 = ("""author_notes: '' auto_tts_enabled: true blurb: '' category: Category correctness_feedback_enabled: false init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 26 states: (untitled state): classifier_model_id: null content: content_id: content html: '' content_ids_to_audio_translations: content: {} default_outcome: {} feedback_1: {} interaction: answer_groups: - outcome: dest: END feedback: content_id: feedback_1 html: Correct! labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null rule_specs: - inputs: x: InputString rule_type: Equals tagged_misconception_id: null training_data: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] END: classifier_model_id: null content: content_id: content html: Congratulations, you have finished! content_ids_to_audio_translations: content: {} interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null hints: [] id: EndExploration solution: null param_changes: [] New state: classifier_model_id: null content: content_id: content html: '' content_ids_to_audio_translations: content: {} default_outcome: {} interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] states_schema_version: 21 tags: [] title: Title """) YAML_CONTENT_V27 = ("""author_notes: '' auto_tts_enabled: true blurb: '' category: Category correctness_feedback_enabled: false init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 27 states: (untitled state): classifier_model_id: null content: content_id: content html: '' content_ids_to_audio_translations: content: {} default_outcome: {} feedback_1: {} interaction: answer_groups: - outcome: dest: END feedback: content_id: feedback_1 html: <p>Correct!</p> labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null rule_specs: - inputs: x: InputString rule_type: Equals tagged_misconception_id: null training_data: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] END: classifier_model_id: null content: content_id: content html: <p>Congratulations, you have finished!</p> content_ids_to_audio_translations: content: {} interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null hints: [] id: EndExploration solution: null param_changes: [] New state: classifier_model_id: null content: content_id: content html: '' content_ids_to_audio_translations: content: {} default_outcome: {} interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] states_schema_version: 22 tags: [] title: Title """) YAML_CONTENT_V28 = ("""author_notes: '' auto_tts_enabled: true blurb: '' category: Category correctness_feedback_enabled: false init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 28 states: (untitled state): classifier_model_id: null content: content_id: content html: '' content_ids_to_audio_translations: content: {} default_outcome: {} feedback_1: {} interaction: answer_groups: - outcome: dest: END feedback: content_id: feedback_1 html: <p>Correct!</p> labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null rule_specs: - inputs: x: InputString rule_type: Equals tagged_misconception_id: null training_data: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] END: classifier_model_id: null content: content_id: content html: <p>Congratulations, you have finished!</p> content_ids_to_audio_translations: content: {} interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null hints: [] id: EndExploration solution: null param_changes: [] New state: classifier_model_id: null content: content_id: content html: '' content_ids_to_audio_translations: content: {} default_outcome: {} interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] states_schema_version: 23 tags: [] title: Title """) YAML_CONTENT_V29 = ("""author_notes: '' auto_tts_enabled: true blurb: '' category: Category correctness_feedback_enabled: false init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 29 states: (untitled state): classifier_model_id: null content: content_id: content html: '' content_ids_to_audio_translations: content: {} default_outcome: {} feedback_1: {} interaction: answer_groups: - outcome: dest: END feedback: content_id: feedback_1 html: <p>Correct!</p> labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null rule_specs: - inputs: x: InputString rule_type: Equals tagged_misconception_id: null training_data: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] END: classifier_model_id: null content: content_id: content html: <p>Congratulations, you have finished!</p> content_ids_to_audio_translations: content: {} interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null hints: [] id: EndExploration solution: null param_changes: [] New state: classifier_model_id: null content: content_id: content html: '' content_ids_to_audio_translations: content: {} default_outcome: {} interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 imageAndRegions: value: imagePath: s1ImagePath.png default_outcome: dest: END feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: ImageClickInput solution: null param_changes: [] states_schema_version: 24 tags: [] title: Title """) YAML_CONTENT_V30 = ("""author_notes: '' auto_tts_enabled: true blurb: '' category: Category correctness_feedback_enabled: false init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 30 states: (untitled state): classifier_model_id: null content: content_id: content html: '' content_ids_to_audio_translations: content: {} default_outcome: {} feedback_1: {} interaction: answer_groups: - outcome: dest: END feedback: content_id: feedback_1 html: <p>Correct!</p> labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null rule_specs: - inputs: x: InputString rule_type: Equals tagged_misconception_id: null training_data: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] END: classifier_model_id: null content: content_id: content html: <p>Congratulations, you have finished!</p> content_ids_to_audio_translations: content: {} interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null hints: [] id: EndExploration solution: null param_changes: [] New state: classifier_model_id: null content: content_id: content html: '' content_ids_to_audio_translations: content: {} default_outcome: {} interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] states_schema_version: 25 tags: [] title: Title """) YAML_CONTENT_V31 = ("""author_notes: '' auto_tts_enabled: true blurb: '' category: Category correctness_feedback_enabled: false init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 31 states: (untitled state): classifier_model_id: null content: content_id: content html: '' content_ids_to_audio_translations: content: {} default_outcome: {} feedback_1: {} interaction: answer_groups: - outcome: dest: END feedback: content_id: feedback_1 html: <p>Correct!</p> labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null rule_specs: - inputs: x: InputString rule_type: Equals tagged_misconception_id: null training_data: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] END: classifier_model_id: null content: content_id: content html: <p>Congratulations, you have finished!</p> content_ids_to_audio_translations: content: {} interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null hints: [] id: EndExploration solution: null param_changes: [] New state: classifier_model_id: null content: content_id: content html: '' content_ids_to_audio_translations: content: {} default_outcome: {} new_content: {} interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] states_schema_version: 26 tags: [] title: Title """) YAML_CONTENT_V32 = ("""author_notes: '' auto_tts_enabled: true blurb: '' category: Category correctness_feedback_enabled: false init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 32 states: (untitled state): classifier_model_id: null content: content_id: content html: '' content_ids_to_audio_translations: content: {} default_outcome: {} feedback_1: {} interaction: answer_groups: - outcome: dest: END feedback: content_id: feedback_1 html: <p>Correct!</p> labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null rule_specs: - inputs: x: InputString rule_type: Equals tagged_misconception_id: null training_data: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] written_translations: translations_mapping: content: {} default_outcome: {} feedback_1: {} END: classifier_model_id: null content: content_id: content html: <p>Congratulations, you have finished!</p> content_ids_to_audio_translations: content: {} interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null hints: [] id: EndExploration solution: null param_changes: [] written_translations: translations_mapping: content: {} New state: classifier_model_id: null content: content_id: content html: '' content_ids_to_audio_translations: content: {} default_outcome: {} interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] written_translations: translations_mapping: content: {} default_outcome: {} states_schema_version: 27 tags: [] title: Title """) YAML_CONTENT_V33 = ("""author_notes: '' auto_tts_enabled: true blurb: '' category: Category correctness_feedback_enabled: false init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 33 states: (untitled state): classifier_model_id: null content: content_id: content html: '' interaction: answer_groups: - outcome: dest: END feedback: content_id: feedback_1 html: <p>Correct!</p> labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null rule_specs: - inputs: x: InputString rule_type: Equals tagged_misconception_id: null training_data: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} default_outcome: {} feedback_1: {} written_translations: translations_mapping: content: {} default_outcome: {} feedback_1: {} END: classifier_model_id: null content: content_id: content html: <p>Congratulations, you have finished!</p> interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null hints: [] id: EndExploration solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} written_translations: translations_mapping: content: {} New state: classifier_model_id: null content: content_id: content html: '' interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} default_outcome: {} written_translations: translations_mapping: content: {} default_outcome: {} states_schema_version: 28 tags: [] title: Title """) YAML_CONTENT_V34 = ("""author_notes: '' auto_tts_enabled: true blurb: '' category: Category correctness_feedback_enabled: false init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 34 states: (untitled state): classifier_model_id: null content: content_id: content html: '' interaction: answer_groups: - outcome: dest: END feedback: content_id: feedback_1 html: <p>Correct!</p> labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null rule_specs: - inputs: x: InputString rule_type: Equals tagged_misconception_id: null training_data: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} default_outcome: {} feedback_1: {} solicit_answer_details: false written_translations: translations_mapping: content: {} default_outcome: {} feedback_1: {} END: classifier_model_id: null content: content_id: content html: <p>Congratulations, you have finished!</p> interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null hints: [] id: EndExploration solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} solicit_answer_details: false written_translations: translations_mapping: content: {} New state: classifier_model_id: null content: content_id: content html: '' interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} default_outcome: {} solicit_answer_details: false written_translations: translations_mapping: content: {} default_outcome: {} states_schema_version: 29 tags: [] title: Title """) YAML_CONTENT_V35 = ("""author_notes: '' auto_tts_enabled: true blurb: '' category: Category correctness_feedback_enabled: false init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 35 states: (untitled state): classifier_model_id: null content: content_id: content html: '' interaction: answer_groups: - outcome: dest: END feedback: content_id: feedback_1 html: <p>Correct!</p> labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null rule_specs: - inputs: x: InputString rule_type: Equals tagged_skill_misconception_id: null training_data: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} default_outcome: {} feedback_1: {} solicit_answer_details: false written_translations: translations_mapping: content: {} default_outcome: {} feedback_1: {} END: classifier_model_id: null content: content_id: content html: <p>Congratulations, you have finished!</p> interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null hints: [] id: EndExploration solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} solicit_answer_details: false written_translations: translations_mapping: content: {} New state: classifier_model_id: null content: content_id: content html: '' interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} default_outcome: {} solicit_answer_details: false written_translations: translations_mapping: content: {} default_outcome: {} states_schema_version: 30 tags: [] title: Title """) _LATEST_YAML_CONTENT = YAML_CONTENT_V35 def test_load_from_v1(self): """Test direct loading from a v1 yaml file.""" exploration = exp_domain.Exploration.from_untitled_yaml( 'eid', 'Title', 'Category', self.YAML_CONTENT_V1) self.assertEqual(exploration.to_yaml(), self._LATEST_YAML_CONTENT) def test_load_from_v2(self): """Test direct loading from a v2 yaml file.""" exploration = exp_domain.Exploration.from_untitled_yaml( 'eid', 'Title', 'Category', self.YAML_CONTENT_V2) self.assertEqual(exploration.to_yaml(), self._LATEST_YAML_CONTENT) def test_load_from_v3(self): """Test direct loading from a v3 yaml file.""" exploration = exp_domain.Exploration.from_untitled_yaml( 'eid', 'Title', 'Category', self.YAML_CONTENT_V3) self.assertEqual(exploration.to_yaml(), self._LATEST_YAML_CONTENT) def test_load_from_v4(self): """Test direct loading from a v4 yaml file.""" exploration = exp_domain.Exploration.from_untitled_yaml( 'eid', 'Title', 'Category', self.YAML_CONTENT_V4) self.assertEqual(exploration.to_yaml(), self._LATEST_YAML_CONTENT) def test_load_from_v5(self): """Test direct loading from a v5 yaml file.""" exploration = exp_domain.Exploration.from_untitled_yaml( 'eid', 'Title', 'Category', self.YAML_CONTENT_V5) self.assertEqual(exploration.to_yaml(), self._LATEST_YAML_CONTENT) def test_load_from_v6(self): """Test direct loading from a v6 yaml file.""" exploration = exp_domain.Exploration.from_untitled_yaml( 'eid', 'Title', 'Category', self.YAML_CONTENT_V6) self.assertEqual(exploration.to_yaml(), self._LATEST_YAML_CONTENT) def test_cannot_load_from_v6_with_invalid_handler_name(self): invalid_yaml_content_v6 = ("""author_notes: '' blurb: '' default_skin: conversation_v1 init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 6 skin_customizations: panels_contents: {} states: (untitled state): content: - type: text value: '' interaction: customization_args: placeholder: value: '' rows: value: 1 handlers: - name: submit rule_specs: - definition: inputs: x: InputString name: Equals rule_type: atomic dest: END feedback: - Correct! param_changes: [] - definition: rule_type: default dest: (untitled state) feedback: [] param_changes: [] id: TextInput triggers: [] param_changes: [] END: content: - type: text value: Congratulations, you have finished! interaction: customization_args: recommendedExplorationIds: value: [] handlers: - name: submit rule_specs: - definition: rule_type: default dest: END feedback: [] param_changes: [] id: EndExploration triggers: [] param_changes: [] New state: content: - type: text value: '' interaction: customization_args: placeholder: value: '' rows: value: 1 handlers: - name: invalid_handler_name rule_specs: - definition: rule_type: default dest: END feedback: [] param_changes: [] id: TextInput triggers: [] param_changes: [] states_schema_version: 3 tags: [] """) with self.assertRaisesRegexp( Exception, 'Error: Can only convert rules with a name ' '\'submit\' in states v3 to v4 conversion process. '): exp_domain.Exploration.from_untitled_yaml( 'eid', 'Title', 'Category', invalid_yaml_content_v6) def test_cannot_load_from_v6_with_invalid_rule(self): invalid_yaml_content_v6 = ("""author_notes: '' blurb: '' default_skin: conversation_v1 init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 6 skin_customizations: panels_contents: {} states: (untitled state): content: - type: text value: '' interaction: customization_args: placeholder: value: '' rows: value: 1 handlers: - name: submit rule_specs: - definition: inputs: x: InputString name: Equals rule_type: invalid_rule dest: END feedback: - Correct! param_changes: [] - definition: rule_type: default dest: (untitled state) feedback: [] param_changes: [] id: TextInput triggers: [] param_changes: [] END: content: - type: text value: Congratulations, you have finished! interaction: customization_args: recommendedExplorationIds: value: [] handlers: - name: submit rule_specs: - definition: rule_type: default dest: END feedback: [] param_changes: [] id: EndExploration triggers: [] param_changes: [] New state: content: - type: text value: '' interaction: customization_args: placeholder: value: '' rows: value: 1 handlers: - name: submit rule_specs: - definition: rule_type: default dest: END feedback: [] param_changes: [] id: TextInput triggers: [] param_changes: [] states_schema_version: 3 tags: [] """) with self.assertRaisesRegexp( Exception, 'Error: Can only convert default and atomic ' 'rules in states v3 to v4 conversion process.'): exp_domain.Exploration.from_untitled_yaml( 'eid', 'Title', 'Category', invalid_yaml_content_v6) def test_cannot_load_from_v6_with_invalid_subject(self): invalid_yaml_content_v6 = ("""author_notes: '' blurb: '' default_skin: conversation_v1 init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 6 skin_customizations: panels_contents: {} states: (untitled state): content: - type: text value: '' interaction: customization_args: placeholder: value: '' rows: value: 1 handlers: - name: submit rule_specs: - definition: inputs: x: InputString name: Equals rule_type: atomic dest: END feedback: - Correct! param_changes: [] - definition: rule_type: default dest: (untitled state) feedback: [] param_changes: [] id: TextInput triggers: [] param_changes: [] END: content: - type: text value: Congratulations, you have finished! interaction: customization_args: recommendedExplorationIds: value: [] handlers: - name: submit rule_specs: - definition: rule_type: default dest: END feedback: [] param_changes: [] id: EndExploration triggers: [] param_changes: [] New state: content: - type: text value: '' interaction: customization_args: placeholder: value: '' rows: value: 1 handlers: - name: submit rule_specs: - definition: rule_type: default subject: invalid_subject dest: END feedback: [] param_changes: [] id: TextInput triggers: [] param_changes: [] states_schema_version: 3 tags: [] """) with self.assertRaisesRegexp( Exception, 'Error: Can only convert rules with an \'answer\' ' 'subject in states v3 to v4 conversion process.'): exp_domain.Exploration.from_untitled_yaml( 'eid', 'Title', 'Category', invalid_yaml_content_v6) def test_cannot_load_from_v6_with_invalid_interaction_id(self): invalid_yaml_content_v6 = ("""author_notes: '' blurb: '' default_skin: conversation_v1 init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 6 skin_customizations: panels_contents: {} states: (untitled state): content: - type: text value: '' interaction: customization_args: placeholder: value: '' rows: value: 1 handlers: - name: submit rule_specs: - definition: inputs: x: InputString name: Equals rule_type: atomic dest: END feedback: - Correct! param_changes: [] - definition: rule_type: default dest: (untitled state) feedback: [] param_changes: [] id: TextInput triggers: [] param_changes: [] END: content: - type: text value: Congratulations, you have finished! interaction: customization_args: recommendedExplorationIds: value: [] handlers: - name: submit rule_specs: - definition: rule_type: default dest: END feedback: [] param_changes: [] id: EndExploration triggers: [] param_changes: [] New state: content: - type: text value: '' interaction: customization_args: placeholder: value: '' rows: value: 1 handlers: - name: submit rule_specs: - definition: rule_type: default dest: END feedback: [] param_changes: [] id: invalid_id triggers: [] param_changes: [] states_schema_version: 3 tags: [] """) with self.assertRaisesRegexp( Exception, 'Trying to migrate exploration containing non-existent ' 'interaction ID'): exp_domain.Exploration.from_untitled_yaml( 'eid', 'Title', 'Category', invalid_yaml_content_v6) def test_load_from_v7(self): """Test direct loading from a v7 yaml file.""" exploration = exp_domain.Exploration.from_untitled_yaml( 'eid', 'Title', 'Category', self.YAML_CONTENT_V7) self.assertEqual(exploration.to_yaml(), self._LATEST_YAML_CONTENT) def test_load_from_v8(self): """Test direct loading from a v8 yaml file.""" exploration = exp_domain.Exploration.from_untitled_yaml( 'eid', 'Title', 'Category', self.YAML_CONTENT_V8) self.assertEqual(exploration.to_yaml(), self._LATEST_YAML_CONTENT) def test_load_from_v9(self): """Test direct loading from a v9 yaml file.""" latest_yaml_content = ("""author_notes: '' auto_tts_enabled: true blurb: '' category: Category correctness_feedback_enabled: false init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 35 states: (untitled state): classifier_model_id: null content: content_id: content html: '' interaction: answer_groups: - outcome: dest: END feedback: content_id: feedback_1 html: <p>Correct!</p> labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null rule_specs: - inputs: x: InputString rule_type: Equals tagged_skill_misconception_id: null training_data: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} default_outcome: {} feedback_1: {} solicit_answer_details: false written_translations: translations_mapping: content: {} default_outcome: {} feedback_1: {} END: classifier_model_id: null content: content_id: content html: <p>Congratulations, you have finished!</p> interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null hints: [] id: EndExploration solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} solicit_answer_details: false written_translations: translations_mapping: content: {} New state: classifier_model_id: null content: content_id: content html: '' interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: language: value: python placeholder: value: '' postCode: value: '' preCode: value: '' rows: value: 1 default_outcome: dest: END feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: CodeRepl solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} default_outcome: {} solicit_answer_details: false written_translations: translations_mapping: content: {} default_outcome: {} states_schema_version: 30 tags: [] title: Title """) exploration = exp_domain.Exploration.from_untitled_yaml( 'eid', 'Title', 'Category', self.YAML_CONTENT_V9) self.assertEqual(exploration.to_yaml(), latest_yaml_content) def test_load_from_v10(self): """Test direct loading from a v10 yaml file.""" exploration = exp_domain.Exploration.from_yaml( 'eid', self.YAML_CONTENT_V10) self.assertEqual(exploration.to_yaml(), self._LATEST_YAML_CONTENT) def test_load_from_v11(self): """Test direct loading from a v11 yaml file.""" exploration = exp_domain.Exploration.from_yaml( 'eid', self.YAML_CONTENT_V11) self.assertEqual(exploration.to_yaml(), self._LATEST_YAML_CONTENT) def test_load_from_v12(self): """Test direct loading from a v12 yaml file.""" latest_yaml_content = ("""author_notes: '' auto_tts_enabled: true blurb: '' category: Category correctness_feedback_enabled: false init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 35 states: (untitled state): classifier_model_id: null content: content_id: content html: '' interaction: answer_groups: - outcome: dest: END feedback: content_id: feedback_1 html: <p>Correct!</p> labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null rule_specs: - inputs: x: InputString rule_type: Equals tagged_skill_misconception_id: null training_data: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} default_outcome: {} feedback_1: {} solicit_answer_details: false written_translations: translations_mapping: content: {} default_outcome: {} feedback_1: {} END: classifier_model_id: null content: content_id: content html: <p>Congratulations, you have finished!</p> interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null hints: [] id: EndExploration solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} solicit_answer_details: false written_translations: translations_mapping: content: {} New state: classifier_model_id: null content: content_id: content html: '' interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: - hint_content: content_id: hint_1 html: <p>Correct!</p> id: TextInput solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} default_outcome: {} hint_1: {} solicit_answer_details: false written_translations: translations_mapping: content: {} default_outcome: {} hint_1: {} states_schema_version: 30 tags: [] title: Title """) exploration = exp_domain.Exploration.from_yaml( 'eid', self.YAML_CONTENT_V12) self.assertEqual(exploration.to_yaml(), latest_yaml_content) def test_load_from_v13(self): """Test direct loading from a v13 yaml file.""" exploration = exp_domain.Exploration.from_yaml( 'eid', self.YAML_CONTENT_V13) self.assertEqual(exploration.to_yaml(), self._LATEST_YAML_CONTENT) def test_load_from_v14(self): """Test direct loading from a v14 yaml file.""" exploration = exp_domain.Exploration.from_yaml( 'eid', self.YAML_CONTENT_V14) self.assertEqual(exploration.to_yaml(), self._LATEST_YAML_CONTENT) def test_load_from_v15(self): """Test direct loading from a v15 yaml file.""" exploration = exp_domain.Exploration.from_yaml( 'eid', self.YAML_CONTENT_V15) self.assertEqual(exploration.to_yaml(), self._LATEST_YAML_CONTENT) def test_load_from_v16(self): """Test direct loading from a v16 yaml file.""" exploration = exp_domain.Exploration.from_yaml( 'eid', self.YAML_CONTENT_V16) self.assertEqual(exploration.to_yaml(), self._LATEST_YAML_CONTENT) def test_load_from_v17(self): """Test direct loading from a v17 yaml file.""" exploration = exp_domain.Exploration.from_yaml( 'eid', self.YAML_CONTENT_V17) self.assertEqual(exploration.to_yaml(), self._LATEST_YAML_CONTENT) def test_load_from_v18(self): """Test direct loading from a v18 yaml file.""" latest_yaml_content = ("""author_notes: '' auto_tts_enabled: true blurb: '' category: Category correctness_feedback_enabled: false init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 35 states: (untitled state): classifier_model_id: null content: content_id: content html: '' interaction: answer_groups: - outcome: dest: END feedback: content_id: feedback_1 html: <p>Correct!</p> labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null rule_specs: - inputs: x: InputString rule_type: Equals tagged_skill_misconception_id: null training_data: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} default_outcome: {} feedback_1: {} solicit_answer_details: false written_translations: translations_mapping: content: {} default_outcome: {} feedback_1: {} END: classifier_model_id: null content: content_id: content html: <p>Congratulations, you have finished!</p> interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null hints: [] id: EndExploration solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} solicit_answer_details: false written_translations: translations_mapping: content: {} New state: classifier_model_id: null content: content_id: content html: '' interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: - hint_content: content_id: hint_1 html: '' id: TextInput solution: answer_is_exclusive: false correct_answer: Answer explanation: content_id: solution html: '' param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} default_outcome: {} hint_1: {} solution: {} solicit_answer_details: false written_translations: translations_mapping: content: {} default_outcome: {} hint_1: {} solution: {} states_schema_version: 30 tags: [] title: Title """) exploration = exp_domain.Exploration.from_yaml( 'eid', self.YAML_CONTENT_V18) self.assertEqual(exploration.to_yaml(), latest_yaml_content) def test_load_from_v19(self): """Test direct loading from a v19 yaml file.""" exploration = exp_domain.Exploration.from_yaml( 'eid', self.YAML_CONTENT_V19) self.assertEqual(exploration.to_yaml(), self._LATEST_YAML_CONTENT) def test_load_from_v20(self): """Test direct loading from a v20 yaml file.""" exploration = exp_domain.Exploration.from_yaml( 'eid', self.YAML_CONTENT_V20) self.assertEqual(exploration.to_yaml(), self._LATEST_YAML_CONTENT) def test_load_from_v21(self): """Test direct loading from a v21 yaml file.""" latest_yaml_content = ("""author_notes: '' auto_tts_enabled: true blurb: '' category: Category correctness_feedback_enabled: false init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 35 states: (untitled state): classifier_model_id: null content: content_id: content html: '' interaction: answer_groups: - outcome: dest: END feedback: content_id: feedback_1 html: <p>Correct!</p> labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null rule_specs: - inputs: x: InputString rule_type: Equals tagged_skill_misconception_id: null training_data: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} default_outcome: {} feedback_1: {} solicit_answer_details: false written_translations: translations_mapping: content: {} default_outcome: {} feedback_1: {} END: classifier_model_id: null content: content_id: content html: <p>Congratulations, you have finished!</p> interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null hints: [] id: EndExploration solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} solicit_answer_details: false written_translations: translations_mapping: content: {} New state: classifier_model_id: null content: content_id: content html: '' interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: allowImproperFraction: value: true allowNonzeroIntegerPart: value: true customPlaceholder: value: '' placeholder: value: '' requireSimplestForm: value: false rows: value: 1 default_outcome: dest: END feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: FractionInput solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} default_outcome: {} solicit_answer_details: false written_translations: translations_mapping: content: {} default_outcome: {} states_schema_version: 30 tags: [] title: Title """) exploration = exp_domain.Exploration.from_yaml( 'eid', self.YAML_CONTENT_V21) self.assertEqual(exploration.to_yaml(), latest_yaml_content) def test_load_from_v22(self): """Test direct loading from a v22 yaml file.""" exploration = exp_domain.Exploration.from_yaml( 'eid', self.YAML_CONTENT_V22) self.assertEqual(exploration.to_yaml(), self._LATEST_YAML_CONTENT) def test_load_from_v23(self): """Test direct loading from a v23 yaml file.""" exploration = exp_domain.Exploration.from_yaml( 'eid', self.YAML_CONTENT_V23) self.assertEqual(exploration.to_yaml(), self._LATEST_YAML_CONTENT) def test_load_from_v24(self): """Test direct loading from a v24 yaml file.""" exploration = exp_domain.Exploration.from_yaml( 'eid', self.YAML_CONTENT_V24) self.assertEqual(exploration.to_yaml(), self._LATEST_YAML_CONTENT) def test_load_from_v25(self): """Test direct loading from a v25 yaml file.""" exploration = exp_domain.Exploration.from_yaml( 'eid', self.YAML_CONTENT_V25) self.assertEqual(exploration.to_yaml(), self._LATEST_YAML_CONTENT) def test_load_from_v26(self): """Test direct loading from a v26 yaml file.""" exploration = exp_domain.Exploration.from_yaml( 'eid', self.YAML_CONTENT_V26) self.assertEqual(exploration.to_yaml(), self._LATEST_YAML_CONTENT) def test_load_from_v27(self): """Test direct loading from a v27 yaml file.""" exploration = exp_domain.Exploration.from_yaml( 'eid', self.YAML_CONTENT_V27) self.assertEqual(exploration.to_yaml(), self._LATEST_YAML_CONTENT) def test_load_from_v28(self): """Test direct loading from a v28 yaml file.""" exploration = exp_domain.Exploration.from_yaml( 'eid', self.YAML_CONTENT_V28) self.assertEqual(exploration.to_yaml(), self._LATEST_YAML_CONTENT) def test_load_from_v29(self): """Test direct loading from a v29 yaml file.""" latest_yaml_content = ("""author_notes: '' auto_tts_enabled: true blurb: '' category: Category correctness_feedback_enabled: false init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 35 states: (untitled state): classifier_model_id: null content: content_id: content html: '' interaction: answer_groups: - outcome: dest: END feedback: content_id: feedback_1 html: <p>Correct!</p> labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null rule_specs: - inputs: x: InputString rule_type: Equals tagged_skill_misconception_id: null training_data: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} default_outcome: {} feedback_1: {} solicit_answer_details: false written_translations: translations_mapping: content: {} default_outcome: {} feedback_1: {} END: classifier_model_id: null content: content_id: content html: <p>Congratulations, you have finished!</p> interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null hints: [] id: EndExploration solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} solicit_answer_details: false written_translations: translations_mapping: content: {} New state: classifier_model_id: null content: content_id: content html: '' interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: highlightRegionsOnHover: value: false imageAndRegions: value: imagePath: s1ImagePath_height_120_width_120.png placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: ImageClickInput solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} default_outcome: {} solicit_answer_details: false written_translations: translations_mapping: content: {} default_outcome: {} states_schema_version: 30 tags: [] title: Title """) exploration = exp_domain.Exploration.from_yaml( 'eid', self.YAML_CONTENT_V29) self.assertEqual(exploration.to_yaml(), latest_yaml_content) def test_load_from_v30(self): """Test direct loading from a v30 yaml file.""" exploration = exp_domain.Exploration.from_yaml( 'eid', self.YAML_CONTENT_V30) self.assertEqual(exploration.to_yaml(), self._LATEST_YAML_CONTENT) def test_load_from_v31(self): """Test direct loading from a v31 yaml file.""" exploration = exp_domain.Exploration.from_yaml( 'eid', self.YAML_CONTENT_V31) self.assertEqual(exploration.to_yaml(), self._LATEST_YAML_CONTENT) def test_load_from_v32(self): """Test direct loading from a v32 yaml file.""" exploration = exp_domain.Exploration.from_yaml( 'eid', self.YAML_CONTENT_V32) self.assertEqual(exploration.to_yaml(), self._LATEST_YAML_CONTENT) def test_load_from_v33(self): """Test direct loading from a v33 yaml file.""" exploration = exp_domain.Exploration.from_yaml( 'eid', self.YAML_CONTENT_V33) self.assertEqual(exploration.to_yaml(), self._LATEST_YAML_CONTENT) def test_cannot_load_from_yaml_with_no_schema_version(self): sample_yaml_content = ("""author_notes: '' blurb: '' default_skin: conversation_v1 init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} skin_customizations: panels_contents: {} states: (untitled state): content: - type: text value: '' interaction: customization_args: placeholder: value: '' rows: value: 1 handlers: - name: submit rule_specs: - definition: inputs: x: InputString name: Equals rule_type: atomic dest: END feedback: - Correct! param_changes: [] - definition: rule_type: default dest: (untitled state) feedback: [] param_changes: [] id: TextInput param_changes: [] New state: content: - type: text value: '' interaction: customization_args: placeholder: value: '' rows: value: 1 handlers: - name: submit rule_specs: - definition: rule_type: default dest: END feedback: [] param_changes: [] id: TextInput param_changes: [] widget: customization_args: {} handlers: - name: submit rule_specs: - definition: rule_type: default dest: END feedback: [] param_changes: [] sticky: false widget_id: TextInput END: content: - type: text value: Congratulations, you have finished! interaction: customization_args: recommendedExplorationIds: value: [] handlers: - name: submit rule_specs: - definition: rule_type: default dest: END feedback: [] param_changes: [] id: EndExploration triggers: [] param_changes: [] tags: [] """) with self.assertRaisesRegexp( Exception, 'Invalid YAML file: no schema version specified.'): exp_domain.Exploration.from_untitled_yaml( 'eid', 'Title', 'Category', sample_yaml_content) def test_cannot_load_from_yaml_with_invalid_schema_version(self): sample_yaml_content = ("""author_notes: '' blurb: '' default_skin: conversation_v1 init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 0 skin_customizations: panels_contents: {} states: (untitled state): content: - type: text value: '' interaction: customization_args: placeholder: value: '' rows: value: 1 handlers: - name: submit rule_specs: - definition: inputs: x: InputString name: Equals rule_type: atomic dest: END feedback: - Correct! param_changes: [] - definition: rule_type: default dest: (untitled state) feedback: [] param_changes: [] id: TextInput param_changes: [] New state: content: - type: text value: '' interaction: customization_args: placeholder: value: '' rows: value: 1 handlers: - name: submit rule_specs: - definition: rule_type: default dest: END feedback: [] param_changes: [] id: TextInput param_changes: [] widget: customization_args: {} handlers: - name: submit rule_specs: - definition: rule_type: default dest: END feedback: [] param_changes: [] sticky: false widget_id: TextInput END: content: - type: text value: Congratulations, you have finished! interaction: customization_args: recommendedExplorationIds: value: [] handlers: - name: submit rule_specs: - definition: rule_type: default dest: END feedback: [] param_changes: [] id: EndExploration triggers: [] param_changes: [] tags: [] """) with self.assertRaisesRegexp( Exception, 'Sorry, we can only process v1 to v%s exploration YAML files ' 'at present.' % exp_domain.Exploration.CURRENT_EXP_SCHEMA_VERSION): exp_domain.Exploration.from_untitled_yaml( 'eid', 'Title', 'Category', sample_yaml_content) class HTMLMigrationUnitTests(test_utils.GenericTestBase): """Test HTML migration.""" YAML_CONTENT_V26_TEXTANGULAR = ("""author_notes: '' auto_tts_enabled: true blurb: '' category: category correctness_feedback_enabled: false init_state_name: Introduction language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 26 states: Introduction: classifier_model_id: null content: content_id: content html: '' content_ids_to_audio_translations: content: {} default_outcome: {} interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: {} default_outcome: dest: Introduction feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: null solution: null param_changes: [] state1: classifier_model_id: null content: content_id: content html: <blockquote><p>Hello, this is state1</p></blockquote> content_ids_to_audio_translations: content: {} default_outcome: {} solution: {} interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: state2 feedback: content_id: default_outcome html: Default <p>outcome</p> for state1 labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: answer_is_exclusive: true correct_answer: Answer1 explanation: content_id: solution html: This is <i>solution</i> for state1 param_changes: [] state2: classifier_model_id: null content: content_id: content html: <p>Hello, </p>this <i>is </i>state2 content_ids_to_audio_translations: content: {} default_outcome: {} feedback_1: {} feedback_2: {} hint_1: {} hint_2: {} interaction: answer_groups: - outcome: dest: state1 feedback: content_id: feedback_1 html: <div>Outcome1 for state2</div> labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null rule_specs: - inputs: x: 0 rule_type: Equals - inputs: x: 1 rule_type: Equals tagged_misconception_id: null training_data: [] - outcome: dest: state3 feedback: content_id: feedback_2 html: <pre>Outcome2 <br>for state2</pre> labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null rule_specs: - inputs: x: 0 rule_type: Equals tagged_misconception_id: null training_data: [] confirmed_unclassified_answers: [] customization_args: choices: value: - <p>This is </p>value1 <br>for MultipleChoice - This is value2<span> for <br>MultipleChoice</span> default_outcome: dest: state2 feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: - hint_content: content_id: hint_1 html: <p>Hello, this is<div> html1<b> for </b></div>state2</p> - hint_content: content_id: hint_2 html: Here is link 2 <oppia-noninteractive-link text-with-value="&amp;quot;discussion forum&amp;quot;" url-with-value="&amp;quot;https://groups.google.com/ forum/?fromgroups#!forum/oppia&amp;quot;"> </oppia-noninteractive-link> id: MultipleChoiceInput solution: null param_changes: [] state3: classifier_model_id: null content: content_id: content html: <p>Hello, this is state3</p> content_ids_to_audio_translations: content: {} default_outcome: {} feedback_1: {} interaction: answer_groups: - outcome: dest: state1 feedback: content_id: feedback_1 html: Here is the image1 <i><oppia-noninteractive-image caption-with-value="&amp;quot;&amp;quot;" filepath-with-value="&amp;quot;startBlue.png&amp;quot;" alt-with-value="&amp;quot;&amp;quot;"> </oppia-noninteractive-image></i>Here is the image2 <div><oppia-noninteractive-image caption-with-value="&amp;quot;&amp;quot;" filepath-with-value="&amp;quot;startBlue.png&amp;quot;" alt-with-value="&amp;quot;&amp;quot;"> </oppia-noninteractive-image></div> labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null rule_specs: - inputs: x: - This <span>is value1 for </span>ItemSelectionInput rule_type: Equals - inputs: x: - This is value3 for ItemSelectionInput rule_type: Equals tagged_misconception_id: null training_data: [] confirmed_unclassified_answers: [] customization_args: choices: value: - This <span>is value1 for </span>ItemSelection - This <code>is value2</code> for ItemSelection - This is value3 for ItemSelection maxAllowableSelectionCount: value: 1 minAllowableSelectionCount: value: 1 default_outcome: dest: state3 feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: ItemSelectionInput solution: null param_changes: [] states_schema_version: 21 tags: [] title: title """) # pylint: disable=line-too-long YAML_CONTENT_V35_IMAGE_DIMENSIONS = ("""author_notes: '' auto_tts_enabled: true blurb: '' category: category correctness_feedback_enabled: false init_state_name: Introduction language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 35 states: Introduction: classifier_model_id: null content: content_id: content html: '' interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: {} default_outcome: dest: Introduction feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: null solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} default_outcome: {} solicit_answer_details: false written_translations: translations_mapping: content: {} default_outcome: {} state1: classifier_model_id: null content: content_id: content html: <blockquote><p>Hello, this is state1</p></blockquote> interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: state2 feedback: content_id: default_outcome html: <p>Default </p><p>outcome</p><p> for state1</p> labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: answer_is_exclusive: true correct_answer: Answer1 explanation: content_id: solution html: <p>This is <em>solution</em> for state1</p> param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} default_outcome: {} solution: {} solicit_answer_details: false written_translations: translations_mapping: content: {} default_outcome: {} solution: {} state2: classifier_model_id: null content: content_id: content html: <p>Hello, </p><p>this <em>is </em>state2</p> interaction: answer_groups: - outcome: dest: state1 feedback: content_id: feedback_1 html: <p>Outcome1 for state2</p> labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null rule_specs: - inputs: x: 0 rule_type: Equals - inputs: x: 1 rule_type: Equals tagged_skill_misconception_id: null training_data: [] - outcome: dest: state3 feedback: content_id: feedback_2 html: "<pre>Outcome2 \\nfor state2</pre>" labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null rule_specs: - inputs: x: 0 rule_type: Equals tagged_skill_misconception_id: null training_data: [] confirmed_unclassified_answers: [] customization_args: choices: value: - <p>This is </p><p>value1 <br>for MultipleChoice</p> - <p>This is value2 for <br>MultipleChoice</p> default_outcome: dest: state2 feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: - hint_content: content_id: hint_1 html: <p>Hello, this is</p><p> html1<strong> for </strong></p><p>state2</p> - hint_content: content_id: hint_2 html: <p>Here is link 2 <oppia-noninteractive-link text-with-value="&amp;quot;discussion forum&amp;quot;" url-with-value="&amp;quot;https://groups.google.com/ forum/?fromgroups#!forum/oppia&amp;quot;"> </oppia-noninteractive-link></p> id: MultipleChoiceInput solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} default_outcome: {} feedback_1: {} feedback_2: {} hint_1: {} hint_2: {} solicit_answer_details: false written_translations: translations_mapping: content: {} default_outcome: {} feedback_1: {} feedback_2: {} hint_1: {} hint_2: {} state3: classifier_model_id: null content: content_id: content html: <p>Hello, this is state3</p> interaction: answer_groups: - outcome: dest: state1 feedback: content_id: feedback_1 html: <p>Here is the image1 </p><oppia-noninteractive-image alt-with-value="&amp;quot;&amp;quot;" caption-with-value="&amp;quot;&amp;quot;" filepath-with-value="&amp;quot;startBlue_height_490_width_120.png&amp;quot;"> </oppia-noninteractive-image><p>Here is the image2 </p><oppia-noninteractive-image alt-with-value="&amp;quot;&amp;quot;" caption-with-value="&amp;quot;&amp;quot;" filepath-with-value="&amp;quot;startBlue_height_490_width_120.png&amp;quot;"> </oppia-noninteractive-image> labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null rule_specs: - inputs: x: - <p>This is value1 for ItemSelectionInput</p> rule_type: Equals - inputs: x: - <p>This is value3 for ItemSelectionInput</p> rule_type: Equals tagged_skill_misconception_id: null training_data: [] confirmed_unclassified_answers: [] customization_args: choices: value: - <p>This is value1 for ItemSelection</p> - <p>This is value2 for ItemSelection</p> - <p>This is value3 for ItemSelection</p> maxAllowableSelectionCount: value: 1 minAllowableSelectionCount: value: 1 default_outcome: dest: state3 feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: ItemSelectionInput solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} default_outcome: {} feedback_1: {} solicit_answer_details: false written_translations: translations_mapping: content: {} default_outcome: {} feedback_1: {} states_schema_version: 30 tags: [] title: title """) YAML_CONTENT_V27_WITHOUT_IMAGE_CAPTION = ("""author_notes: '' auto_tts_enabled: true blurb: '' category: Category correctness_feedback_enabled: false init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 27 states: (untitled state): classifier_model_id: null content: content_id: content html: <p><oppia-noninteractive-image filepath-with-value="&amp;quot;random.png&amp;quot;"></oppia-noninteractive-image>Hello this is test case to check image tag inside p tag</p> content_ids_to_audio_translations: content: {} default_outcome: {} feedback_1: {} interaction: answer_groups: - outcome: dest: END feedback: content_id: feedback_1 html: <p>Correct!</p> labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null rule_specs: - inputs: x: InputString rule_type: Equals tagged_misconception_id: null training_data: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] END: classifier_model_id: null content: content_id: content html: <p>Congratulations, you have finished!</p> content_ids_to_audio_translations: content: {} interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null hints: [] id: EndExploration solution: null param_changes: [] New state: classifier_model_id: null content: content_id: content html: '' content_ids_to_audio_translations: content: {} default_outcome: {} interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] states_schema_version: 22 tags: [] title: Title """) YAML_CONTENT_V35_WITH_IMAGE_CAPTION = ("""author_notes: '' auto_tts_enabled: true blurb: '' category: Category correctness_feedback_enabled: false init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 35 states: (untitled state): classifier_model_id: null content: content_id: content html: <oppia-noninteractive-image caption-with-value="&amp;quot;&amp;quot;" filepath-with-value="&amp;quot;random_height_490_width_120.png&amp;quot;"></oppia-noninteractive-image><p>Hello this is test case to check image tag inside p tag</p> interaction: answer_groups: - outcome: dest: END feedback: content_id: feedback_1 html: <p>Correct!</p> labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null rule_specs: - inputs: x: InputString rule_type: Equals tagged_skill_misconception_id: null training_data: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} default_outcome: {} feedback_1: {} solicit_answer_details: false written_translations: translations_mapping: content: {} default_outcome: {} feedback_1: {} END: classifier_model_id: null content: content_id: content html: <p>Congratulations, you have finished!</p> interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null hints: [] id: EndExploration solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} solicit_answer_details: false written_translations: translations_mapping: content: {} New state: classifier_model_id: null content: content_id: content html: '' interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} default_outcome: {} solicit_answer_details: false written_translations: translations_mapping: content: {} default_outcome: {} states_schema_version: 30 tags: [] title: Title """) # pylint: enable=line-too-long def test_load_from_v26_textangular(self): """Test direct loading from a v26 yaml file.""" mock_get_filename_with_dimensions_context = self.swap( html_validation_service, 'get_filename_with_dimensions', mock_get_filename_with_dimensions) with mock_get_filename_with_dimensions_context: exploration = exp_domain.Exploration.from_yaml( 'eid', self.YAML_CONTENT_V26_TEXTANGULAR) self.assertEqual( exploration.to_yaml(), self.YAML_CONTENT_V35_IMAGE_DIMENSIONS) def test_load_from_v27_without_image_caption(self): """Test direct loading from a v27 yaml file.""" mock_get_filename_with_dimensions_context = self.swap( html_validation_service, 'get_filename_with_dimensions', mock_get_filename_with_dimensions) with mock_get_filename_with_dimensions_context: exploration = exp_domain.Exploration.from_yaml( 'eid', self.YAML_CONTENT_V27_WITHOUT_IMAGE_CAPTION) self.assertEqual( exploration.to_yaml(), self.YAML_CONTENT_V35_WITH_IMAGE_CAPTION) class ConversionUnitTests(test_utils.GenericTestBase): """Test conversion methods.""" def test_convert_exploration_to_player_dict(self): exp_title = 'Title' second_state_name = 'first state' exploration = exp_domain.Exploration.create_default_exploration( 'eid', title=exp_title, category='Category') exploration.add_states([second_state_name]) def _get_default_state_dict(content_str, dest_name): """Gets the default state dict of the exploration.""" return { 'classifier_model_id': None, 'content': { 'content_id': 'content', 'html': content_str, }, 'recorded_voiceovers': { 'voiceovers_mapping': { 'content': {}, 'default_outcome': {} } }, 'solicit_answer_details': False, 'written_translations': { 'translations_mapping': { 'content': {}, 'default_outcome': {} } }, 'interaction': { 'answer_groups': [], 'confirmed_unclassified_answers': [], 'customization_args': {}, 'default_outcome': { 'dest': dest_name, 'feedback': { 'content_id': feconf.DEFAULT_OUTCOME_CONTENT_ID, 'html': '' }, 'labelled_as_correct': False, 'param_changes': [], 'refresher_exploration_id': None, 'missing_prerequisite_skill_id': None }, 'hints': [], 'id': None, 'solution': None, }, 'param_changes': [], } self.assertEqual(exploration.to_player_dict(), { 'init_state_name': feconf.DEFAULT_INIT_STATE_NAME, 'title': exp_title, 'objective': feconf.DEFAULT_EXPLORATION_OBJECTIVE, 'states': { feconf.DEFAULT_INIT_STATE_NAME: _get_default_state_dict( feconf.DEFAULT_INIT_STATE_CONTENT_STR, feconf.DEFAULT_INIT_STATE_NAME), second_state_name: _get_default_state_dict( '', second_state_name), }, 'param_changes': [], 'param_specs': {}, 'language_code': 'en', 'correctness_feedback_enabled': False, }) class StateOperationsUnitTests(test_utils.GenericTestBase): """Test methods operating on states.""" def test_delete_state(self): """Test deletion of states.""" exploration = exp_domain.Exploration.create_default_exploration('eid') exploration.add_states(['first state']) with self.assertRaisesRegexp( ValueError, 'Cannot delete initial state' ): exploration.delete_state(exploration.init_state_name) exploration.add_states(['second state']) exploration.delete_state('second state') with self.assertRaisesRegexp(ValueError, 'fake state does not exist'): exploration.delete_state('fake state') class HtmlCollectionTests(test_utils.GenericTestBase): """Test method to obtain all html strings.""" def test_all_html_strings_are_collected(self): exploration = exp_domain.Exploration.create_default_exploration( 'eid', title='title', category='category') exploration.add_states(['state1', 'state2', 'state3', 'state4']) state1 = exploration.states['state1'] state2 = exploration.states['state2'] state3 = exploration.states['state3'] state4 = exploration.states['state4'] content1_dict = { 'content_id': 'content', 'html': '<blockquote>Hello, this is state1</blockquote>' } content2_dict = { 'content_id': 'content', 'html': '<pre>Hello, this is state2</pre>' } content3_dict = { 'content_id': 'content', 'html': '<p>Hello, this is state3</p>' } content4_dict = { 'content_id': 'content', 'html': '<p>Hello, this is state4</p>' } state1.update_content( state_domain.SubtitledHtml.from_dict(content1_dict)) state2.update_content( state_domain.SubtitledHtml.from_dict(content2_dict)) state3.update_content( state_domain.SubtitledHtml.from_dict(content3_dict)) state4.update_content( state_domain.SubtitledHtml.from_dict(content4_dict)) state1.update_interaction_id('TextInput') state2.update_interaction_id('MultipleChoiceInput') state3.update_interaction_id('ItemSelectionInput') state4.update_interaction_id('DragAndDropSortInput') customization_args_dict1 = { 'placeholder': {'value': ''}, 'rows': {'value': 1} } customization_args_dict2 = { 'choices': {'value': [ '<p>This is value1 for MultipleChoice</p>', '<p>This is value2 for MultipleChoice</p>' ]} } customization_args_dict3 = { 'choices': {'value': [ '<p>This is value1 for ItemSelection</p>', '<p>This is value2 for ItemSelection</p>', '<p>This is value3 for ItemSelection</p>' ]} } customization_args_dict4 = { 'choices': {'value': [ '<p>This is value1 for DragAndDropSortInput</p>', '<p>This is value2 for DragAndDropSortInput</p>', ]} } state1.update_interaction_customization_args(customization_args_dict1) state2.update_interaction_customization_args(customization_args_dict2) state3.update_interaction_customization_args(customization_args_dict3) state4.update_interaction_customization_args(customization_args_dict4) default_outcome_dict1 = { 'dest': 'state2', 'feedback': { 'content_id': 'default_outcome', 'html': '<p>Default outcome for state1</p>' }, 'param_changes': [], 'labelled_as_correct': False, 'refresher_exploration_id': None, 'missing_prerequisite_skill_id': None } state1.update_interaction_default_outcome(default_outcome_dict1) hint_list2 = [{ 'hint_content': { 'content_id': 'hint_1', 'html': '<p>Hello, this is html1 for state2</p>' } }, { 'hint_content': { 'content_id': 'hint_2', 'html': '<p>Hello, this is html2 for state2</p>' } }] state2.update_interaction_hints(hint_list2) solution_dict1 = { 'interaction_id': '', 'answer_is_exclusive': True, 'correct_answer': 'Answer1', 'explanation': { 'content_id': 'solution', 'html': '<p>This is solution for state1</p>' } } state1.update_interaction_solution(solution_dict1) answer_group_list2 = [{ 'rule_specs': [{ 'rule_type': 'Equals', 'inputs': {'x': 0} }, { 'rule_type': 'Equals', 'inputs': {'x': 1} }], 'outcome': { 'dest': 'state1', 'feedback': { 'content_id': 'feedback_1', 'html': '<p>Outcome1 for state2</p>' }, 'param_changes': [], 'labelled_as_correct': False, 'refresher_exploration_id': None, 'missing_prerequisite_skill_id': None }, 'training_data': [], 'tagged_skill_misconception_id': None }, { 'rule_specs': [{ 'rule_type': 'Equals', 'inputs': {'x': 0} }], 'outcome': { 'dest': 'state3', 'feedback': { 'content_id': 'feedback_2', 'html': '<p>Outcome2 for state2</p>' }, 'param_changes': [], 'labelled_as_correct': False, 'refresher_exploration_id': None, 'missing_prerequisite_skill_id': None }, 'training_data': [], 'tagged_skill_misconception_id': None }] answer_group_list3 = [{ 'rule_specs': [{ 'rule_type': 'Equals', 'inputs': {'x': [ '<p>This is value1 for ItemSelectionInput</p>' ]} }, { 'rule_type': 'Equals', 'inputs': {'x': [ '<p>This is value3 for ItemSelectionInput</p>' ]} }], 'outcome': { 'dest': 'state1', 'feedback': { 'content_id': 'feedback_1', 'html': '<p>Outcome for state3</p>' }, 'param_changes': [], 'labelled_as_correct': False, 'refresher_exploration_id': None, 'missing_prerequisite_skill_id': None }, 'training_data': [], 'tagged_skill_misconception_id': None }] state2.update_interaction_answer_groups(answer_group_list2) state3.update_interaction_answer_groups(answer_group_list3) expected_html_list = [ '', '', '<pre>Hello, this is state2</pre>', '<p>Outcome1 for state2</p>', '<p>Outcome2 for state2</p>', '', '<p>Hello, this is html1 for state2</p>', '<p>Hello, this is html2 for state2</p>', '<p>This is value1 for MultipleChoice</p>', '<p>This is value2 for MultipleChoice</p>', '<blockquote>Hello, this is state1</blockquote>', '<p>Default outcome for state1</p>', '<p>This is solution for state1</p>', '<p>Hello, this is state3</p>', '<p>Outcome for state3</p>', '<p>This is value1 for ItemSelectionInput</p>', '<p>This is value3 for ItemSelectionInput</p>', '', '<p>This is value1 for ItemSelection</p>', '<p>This is value2 for ItemSelection</p>', '<p>This is value3 for ItemSelection</p>', '<p>Hello, this is state4</p>', '', '<p>This is value1 for DragAndDropSortInput</p>', '<p>This is value2 for DragAndDropSortInput</p>' ] actual_outcome_list = exploration.get_all_html_content_strings() self.assertEqual(actual_outcome_list, expected_html_list)
28.745724
135
0.603415
import copy import os import re from core.domain import exp_domain from core.domain import exp_fetchers from core.domain import exp_services from core.domain import html_validation_service from core.domain import param_domain from core.domain import state_domain from core.platform import models from core.tests import test_utils import feconf import utils (exp_models,) = models.Registry.import_models([models.NAMES.exploration]) def mock_get_filename_with_dimensions(filename, unused_exp_id): return html_validation_service.regenerate_image_filename_using_dimensions( filename, 490, 120) class ExplorationChangeTests(test_utils.GenericTestBase): def test_exp_change_object_with_missing_cmd(self): with self.assertRaisesRegexp( utils.ValidationError, 'Missing cmd key in change dict'): exp_domain.ExplorationChange({'invalid': 'data'}) def test_exp_change_object_with_invalid_cmd(self): with self.assertRaisesRegexp( utils.ValidationError, 'Command invalid is not allowed'): exp_domain.ExplorationChange({'cmd': 'invalid'}) def test_exp_change_object_with_missing_attribute_in_cmd(self): with self.assertRaisesRegexp( utils.ValidationError, ( 'The following required attributes are missing: ' 'new_value')): exp_domain.ExplorationChange({ 'cmd': 'edit_state_property', 'property_name': 'content', 'old_value': 'old_value' }) def test_exp_change_object_with_extra_attribute_in_cmd(self): with self.assertRaisesRegexp( utils.ValidationError, ( 'The following extra attributes are present: invalid')): exp_domain.ExplorationChange({ 'cmd': 'rename_state', 'old_state_name': 'old_state_name', 'new_state_name': 'new_state_name', 'invalid': 'invalid' }) def test_exp_change_object_with_invalid_exploration_property(self): with self.assertRaisesRegexp( utils.ValidationError, ( 'Value for property_name in cmd edit_exploration_property: ' 'invalid is not allowed')): exp_domain.ExplorationChange({ 'cmd': 'edit_exploration_property', 'property_name': 'invalid', 'old_value': 'old_value', 'new_value': 'new_value', }) def test_exp_change_object_with_invalid_state_property(self): with self.assertRaisesRegexp( utils.ValidationError, ( 'Value for property_name in cmd edit_state_property: ' 'invalid is not allowed')): exp_domain.ExplorationChange({ 'cmd': 'edit_state_property', 'state_name': 'state_name', 'property_name': 'invalid', 'old_value': 'old_value', 'new_value': 'new_value', }) def test_exp_change_object_with_create_new(self): exp_change_object = exp_domain.ExplorationChange({ 'cmd': 'create_new', 'category': 'category', 'title': 'title' }) self.assertEqual(exp_change_object.cmd, 'create_new') self.assertEqual(exp_change_object.category, 'category') self.assertEqual(exp_change_object.title, 'title') def test_exp_change_object_with_add_state(self): exp_change_object = exp_domain.ExplorationChange({ 'cmd': 'add_state', 'state_name': 'state_name', }) self.assertEqual(exp_change_object.cmd, 'add_state') self.assertEqual(exp_change_object.state_name, 'state_name') def test_exp_change_object_with_rename_state(self): exp_change_object = exp_domain.ExplorationChange({ 'cmd': 'rename_state', 'old_state_name': 'old_state_name', 'new_state_name': 'new_state_name' }) self.assertEqual(exp_change_object.cmd, 'rename_state') self.assertEqual(exp_change_object.old_state_name, 'old_state_name') self.assertEqual(exp_change_object.new_state_name, 'new_state_name') def test_exp_change_object_with_delete_state(self): exp_change_object = exp_domain.ExplorationChange({ 'cmd': 'delete_state', 'state_name': 'state_name', }) self.assertEqual(exp_change_object.cmd, 'delete_state') self.assertEqual(exp_change_object.state_name, 'state_name') def test_exp_change_object_with_edit_state_property(self): exp_change_object = exp_domain.ExplorationChange({ 'cmd': 'edit_state_property', 'state_name': 'state_name', 'property_name': 'content', 'new_value': 'new_value', 'old_value': 'old_value' }) self.assertEqual(exp_change_object.cmd, 'edit_state_property') self.assertEqual(exp_change_object.state_name, 'state_name') self.assertEqual(exp_change_object.property_name, 'content') self.assertEqual(exp_change_object.new_value, 'new_value') self.assertEqual(exp_change_object.old_value, 'old_value') def test_exp_change_object_with_edit_exploration_property(self): exp_change_object = exp_domain.ExplorationChange({ 'cmd': 'edit_exploration_property', 'property_name': 'title', 'new_value': 'new_value', 'old_value': 'old_value' }) self.assertEqual(exp_change_object.cmd, 'edit_exploration_property') self.assertEqual(exp_change_object.property_name, 'title') self.assertEqual(exp_change_object.new_value, 'new_value') self.assertEqual(exp_change_object.old_value, 'old_value') def test_exp_change_object_with_migrate_states_schema_to_latest_version( self): exp_change_object = exp_domain.ExplorationChange({ 'cmd': 'migrate_states_schema_to_latest_version', 'from_version': 'from_version', 'to_version': 'to_version', }) self.assertEqual( exp_change_object.cmd, 'migrate_states_schema_to_latest_version') self.assertEqual(exp_change_object.from_version, 'from_version') self.assertEqual(exp_change_object.to_version, 'to_version') def test_exp_change_object_with_revert_commit(self): exp_change_object = exp_domain.ExplorationChange({ 'cmd': exp_models.ExplorationModel.CMD_REVERT_COMMIT, 'version_number': 'version_number' }) self.assertEqual( exp_change_object.cmd, exp_models.ExplorationModel.CMD_REVERT_COMMIT) self.assertEqual(exp_change_object.version_number, 'version_number') def test_to_dict(self): exp_change_dict = { 'cmd': 'create_new', 'title': 'title', 'category': 'category' } exp_change_object = exp_domain.ExplorationChange(exp_change_dict) self.assertEqual(exp_change_object.to_dict(), exp_change_dict) class ExplorationVersionsDiffDomainUnitTests(test_utils.GenericTestBase): def setUp(self): super(ExplorationVersionsDiffDomainUnitTests, self).setUp() self.exp_id = 'exp_id1' test_exp_filepath = os.path.join( feconf.TESTS_DATA_DIR, 'string_classifier_test.yaml') yaml_content = utils.get_file_contents(test_exp_filepath) assets_list = [] exp_services.save_new_exploration_from_yaml_and_assets( feconf.SYSTEM_COMMITTER_ID, yaml_content, self.exp_id, assets_list) self.exploration = exp_fetchers.get_exploration_by_id(self.exp_id) def test_correct_creation_of_version_diffs(self): self.exploration.rename_state('Home', 'Renamed state') change_list = [exp_domain.ExplorationChange({ 'cmd': 'rename_state', 'old_state_name': 'Home', 'new_state_name': 'Renamed state' })] exp_versions_diff = exp_domain.ExplorationVersionsDiff(change_list) self.assertEqual(exp_versions_diff.added_state_names, []) self.assertEqual(exp_versions_diff.deleted_state_names, []) self.assertEqual( exp_versions_diff.old_to_new_state_names, { 'Home': 'Renamed state' }) self.exploration.version += 1 self.exploration.add_states(['New state']) self.exploration.states['New state'] = copy.deepcopy( self.exploration.states['Renamed state']) change_list = [exp_domain.ExplorationChange({ 'cmd': 'add_state', 'state_name': 'New state', })] exp_versions_diff = exp_domain.ExplorationVersionsDiff(change_list) self.assertEqual(exp_versions_diff.added_state_names, ['New state']) self.assertEqual(exp_versions_diff.deleted_state_names, []) self.assertEqual(exp_versions_diff.old_to_new_state_names, {}) self.exploration.version += 1 self.exploration.delete_state('New state') change_list = [exp_domain.ExplorationChange({ 'cmd': 'delete_state', 'state_name': 'New state' })] exp_versions_diff = exp_domain.ExplorationVersionsDiff(change_list) self.assertEqual(exp_versions_diff.added_state_names, []) self.assertEqual(exp_versions_diff.deleted_state_names, ['New state']) self.assertEqual(exp_versions_diff.old_to_new_state_names, {}) self.exploration.version += 1 self.exploration.add_states(['New state']) self.exploration.states['New state'] = copy.deepcopy( self.exploration.states['Renamed state']) self.exploration.rename_state('New state', 'New state2') self.exploration.rename_state('New state2', 'New state3') change_list = [exp_domain.ExplorationChange({ 'cmd': 'add_state', 'state_name': 'New state', }), exp_domain.ExplorationChange({ 'cmd': 'rename_state', 'old_state_name': 'New state', 'new_state_name': 'New state2' }), exp_domain.ExplorationChange({ 'cmd': 'rename_state', 'old_state_name': 'New state2', 'new_state_name': 'New state3' })] exp_versions_diff = exp_domain.ExplorationVersionsDiff(change_list) self.assertEqual(exp_versions_diff.added_state_names, ['New state3']) self.assertEqual(exp_versions_diff.deleted_state_names, []) self.assertEqual(exp_versions_diff.old_to_new_state_names, {}) self.exploration.version += 1 self.exploration.add_states(['New state 2']) self.exploration.rename_state('New state 2', 'Renamed state 2') self.exploration.delete_state('Renamed state 2') change_list = [exp_domain.ExplorationChange({ 'cmd': 'add_state', 'state_name': 'New state 2' }), exp_domain.ExplorationChange({ 'cmd': 'rename_state', 'old_state_name': 'New state 2', 'new_state_name': 'Renamed state 2' }), exp_domain.ExplorationChange({ 'cmd': 'delete_state', 'state_name': 'Renamed state 2' })] exp_versions_diff = exp_domain.ExplorationVersionsDiff(change_list) self.assertEqual(exp_versions_diff.added_state_names, []) self.assertEqual(exp_versions_diff.deleted_state_names, []) self.assertEqual(exp_versions_diff.old_to_new_state_names, {}) self.exploration.version += 1 self.exploration.rename_state('New state3', 'Renamed state 3') self.exploration.rename_state('Renamed state 3', 'Renamed state 4') self.exploration.delete_state('Renamed state 4') change_list = [exp_domain.ExplorationChange({ 'cmd': 'rename_state', 'old_state_name': 'New state3', 'new_state_name': 'Renamed state 3' }), exp_domain.ExplorationChange({ 'cmd': 'rename_state', 'old_state_name': 'Renamed state 3', 'new_state_name': 'Renamed state 4' }), exp_domain.ExplorationChange({ 'cmd': 'delete_state', 'state_name': 'Renamed state 4' })] exp_versions_diff = exp_domain.ExplorationVersionsDiff(change_list) self.assertEqual(exp_versions_diff.added_state_names, []) self.assertEqual( exp_versions_diff.deleted_state_names, ['New state3']) self.assertEqual(exp_versions_diff.old_to_new_state_names, {}) self.exploration.version += 1 def test_cannot_create_exploration_change_with_invalid_change_dict(self): with self.assertRaisesRegexp( Exception, 'Missing cmd key in change dict'): exp_domain.ExplorationChange({ 'invalid_cmd': 'invalid' }) def test_cannot_create_exploration_change_with_invalid_cmd(self): with self.assertRaisesRegexp( Exception, 'Command invalid_cmd is not allowed'): exp_domain.ExplorationChange({ 'cmd': 'invalid_cmd' }) def test_cannot_create_exploration_change_with_invalid_state_property(self): exp_change = exp_domain.ExplorationChange({ 'cmd': exp_domain.CMD_EDIT_STATE_PROPERTY, 'property_name': exp_domain.STATE_PROPERTY_INTERACTION_ID, 'state_name': '', 'new_value': '' }) self.assertTrue(isinstance(exp_change, exp_domain.ExplorationChange)) with self.assertRaisesRegexp( Exception, 'Value for property_name in cmd edit_state_property: ' 'invalid_property is not allowed'): exp_domain.ExplorationChange({ 'cmd': exp_domain.CMD_EDIT_STATE_PROPERTY, 'property_name': 'invalid_property', 'state_name': '', 'new_value': '' }) def test_cannot_create_exploration_change_with_invalid_exploration_property( self): exp_change = exp_domain.ExplorationChange({ 'cmd': exp_domain.CMD_EDIT_EXPLORATION_PROPERTY, 'property_name': 'title', 'new_value': '' }) self.assertTrue(isinstance(exp_change, exp_domain.ExplorationChange)) with self.assertRaisesRegexp( Exception, 'Value for property_name in cmd edit_exploration_property: ' 'invalid_property is not allowed'): exp_domain.ExplorationChange({ 'cmd': exp_domain.CMD_EDIT_EXPLORATION_PROPERTY, 'property_name': 'invalid_property', 'new_value': '' }) def test_revert_exploration_commit(self): exp_change = exp_domain.ExplorationChange({ 'cmd': exp_models.ExplorationModel.CMD_REVERT_COMMIT, 'version_number': 1 }) self.assertEqual(exp_change.version_number, 1) exp_change = exp_domain.ExplorationChange({ 'cmd': exp_models.ExplorationModel.CMD_REVERT_COMMIT, 'version_number': 2 }) self.assertEqual(exp_change.version_number, 2) class ExpVersionReferenceTests(test_utils.GenericTestBase): def test_create_exp_version_reference_object(self): exp_version_reference = exp_domain.ExpVersionReference('exp_id', 1) self.assertEqual( exp_version_reference.to_dict(), { 'exp_id': 'exp_id', 'version': 1 }) def test_validate_exp_version(self): with self.assertRaisesRegexp( Exception, 'Expected version to be an int, received invalid_version'): exp_domain.ExpVersionReference('exp_id', 'invalid_version') def test_validate_exp_id(self): with self.assertRaisesRegexp( Exception, 'Expected exp_id to be a str, received 0'): exp_domain.ExpVersionReference(0, 1) class ExplorationDomainUnitTests(test_utils.GenericTestBase): def test_validation(self): exploration = exp_domain.Exploration.create_default_exploration('eid') exploration.init_state_name = '' exploration.states = {} exploration.title = 'Hello #' self._assert_validation_error(exploration, 'Invalid character #') exploration.title = 'Title' exploration.category = 'Category' bad_state = state_domain.State.create_default_state('/') exploration.states = {'/': bad_state} self._assert_validation_error( exploration, 'Invalid character / in a state name') new_state = state_domain.State.create_default_state('ABC') new_state.update_interaction_id('TextInput') exploration.states = {} self._assert_validation_error( exploration, 'exploration has no states') exploration.states = {'A string #': new_state} self._assert_validation_error( exploration, 'Invalid character # in a state name') exploration.states = {'A string _': new_state} self._assert_validation_error( exploration, 'Invalid character _ in a state name') exploration.states = {'ABC': new_state} self._assert_validation_error( exploration, 'has no initial state name') exploration.init_state_name = 'initname' self._assert_validation_error( exploration, r'There is no state in \[\'ABC\'\] corresponding to ' 'the exploration\'s initial state name initname.') # Test whether a default outcome to a non-existing state is invalid. exploration.states = {exploration.init_state_name: new_state} self._assert_validation_error( exploration, 'destination ABC is not a valid') # Restore a valid exploration. init_state = exploration.states[exploration.init_state_name] default_outcome_dict = init_state.interaction.default_outcome.to_dict() default_outcome_dict['dest'] = exploration.init_state_name init_state.update_interaction_default_outcome(default_outcome_dict) exploration.validate() # Ensure an invalid destination can also be detected for answer groups. # Note: The state must keep its default_outcome, otherwise it will # trigger a validation error for non-terminal states needing to have a # default outcome. To validate the outcome of the answer group, this # default outcome must point to a valid state. init_state = exploration.states[exploration.init_state_name] default_outcome = init_state.interaction.default_outcome default_outcome.dest = exploration.init_state_name old_answer_groups = copy.deepcopy(init_state.interaction.answer_groups) old_answer_groups.append({ 'outcome': { 'dest': exploration.init_state_name, 'feedback': { 'content_id': 'feedback_1', 'html': '<p>Feedback</p>' }, 'labelled_as_correct': False, 'param_changes': [], 'refresher_exploration_id': None, 'missing_prerequisite_skill_id': None }, 'rule_specs': [{ 'inputs': { 'x': 'Test' }, 'rule_type': 'Contains' }], 'training_data': [], 'tagged_skill_misconception_id': None }) init_state.update_interaction_answer_groups(old_answer_groups) exploration.validate() interaction = init_state.interaction answer_groups = interaction.answer_groups answer_group = answer_groups[0] answer_group.outcome.dest = 'DEF' self._assert_validation_error( exploration, 'destination DEF is not a valid') # Restore a valid exploration. exploration.states[exploration.init_state_name].update_interaction_id( 'TextInput') answer_group.outcome.dest = exploration.init_state_name exploration.validate() # Validate RuleSpec. rule_spec = answer_group.rule_specs[0] rule_spec.inputs = {} self._assert_validation_error( exploration, 'RuleSpec \'Contains\' is missing inputs') rule_spec.inputs = 'Inputs string' self._assert_validation_error( exploration, 'Expected inputs to be a dict') rule_spec.inputs = {'x': 'Test'} rule_spec.rule_type = 'FakeRuleType' self._assert_validation_error(exploration, 'Unrecognized rule type') rule_spec.inputs = {'x': 15} rule_spec.rule_type = 'Contains' with self.assertRaisesRegexp( Exception, 'Expected unicode string, received 15' ): exploration.validate() rule_spec.inputs = {'x': '{{ExampleParam}}'} self._assert_validation_error( exploration, 'RuleSpec \'Contains\' has an input with name \'x\' which refers ' 'to an unknown parameter within the exploration: ExampleParam') # Restore a valid exploration. exploration.param_specs['ExampleParam'] = param_domain.ParamSpec( 'UnicodeString') exploration.validate() # Validate Outcome. outcome = answer_group.outcome destination = exploration.init_state_name outcome.dest = None self._assert_validation_error( exploration, 'Every outcome should have a destination.') # Try setting the outcome destination to something other than a string. outcome.dest = 15 self._assert_validation_error( exploration, 'Expected outcome dest to be a string') outcome.dest = destination outcome.feedback = state_domain.SubtitledHtml('feedback_1', '') exploration.validate() outcome.labelled_as_correct = 'hello' self._assert_validation_error( exploration, 'The "labelled_as_correct" field should be a boolean') # Test that labelled_as_correct must be False for self-loops, and that # this causes a strict validation failure but not a normal validation # failure. outcome.labelled_as_correct = True with self.assertRaisesRegexp( Exception, 'is labelled correct but is a self-loop.' ): exploration.validate(strict=True) exploration.validate() outcome.labelled_as_correct = False exploration.validate() outcome.param_changes = 'Changes' self._assert_validation_error( exploration, 'Expected outcome param_changes to be a list') outcome.param_changes = [param_domain.ParamChange( 0, 'generator_id', {})] self._assert_validation_error( exploration, 'Expected param_change name to be a string, received 0') outcome.param_changes = [] exploration.validate() outcome.refresher_exploration_id = 12345 self._assert_validation_error( exploration, 'Expected outcome refresher_exploration_id to be a string') outcome.refresher_exploration_id = None exploration.validate() outcome.refresher_exploration_id = 'valid_string' exploration.validate() outcome.missing_prerequisite_skill_id = 12345 self._assert_validation_error( exploration, 'Expected outcome missing_prerequisite_skill_id to be a string') outcome.missing_prerequisite_skill_id = None exploration.validate() outcome.missing_prerequisite_skill_id = 'valid_string' exploration.validate() # Test that refresher_exploration_id must be None for non-self-loops. new_state_name = 'New state' exploration.add_states([new_state_name]) outcome.dest = new_state_name outcome.refresher_exploration_id = 'another_string' self._assert_validation_error( exploration, 'has a refresher exploration ID, but is not a self-loop') outcome.refresher_exploration_id = None exploration.validate() exploration.delete_state(new_state_name) # Validate InteractionInstance. interaction.id = 15 self._assert_validation_error( exploration, 'Expected interaction id to be a string') interaction.id = 'SomeInteractionTypeThatDoesNotExist' self._assert_validation_error(exploration, 'Invalid interaction id') interaction.id = 'TextInput' exploration.validate() interaction.customization_args = [] self._assert_validation_error( exploration, 'Expected customization args to be a dict') interaction.customization_args = {15: ''} self._assert_validation_error( exploration, 'Invalid customization arg name') interaction.customization_args = {'placeholder': ''} exploration.validate() interaction.answer_groups = {} self._assert_validation_error( exploration, 'Expected answer groups to be a list') interaction.answer_groups = answer_groups interaction.id = 'EndExploration' self._assert_validation_error( exploration, 'Terminal interactions must not have a default outcome.') interaction.id = 'TextInput' init_state.update_interaction_default_outcome(None) self._assert_validation_error( exploration, 'Non-terminal interactions must have a default outcome.') interaction.id = 'EndExploration' self._assert_validation_error( exploration, 'Terminal interactions must not have any answer groups.') # A terminal interaction without a default outcome or answer group is # valid. This resets the exploration back to a valid state. init_state.update_interaction_answer_groups([]) exploration.validate() # Restore a valid exploration. interaction.id = 'TextInput' answer_groups_list = [ answer_group.to_dict() for answer_group in answer_groups] init_state.update_interaction_answer_groups(answer_groups_list) init_state.update_interaction_default_outcome(default_outcome.to_dict()) exploration.validate() init_state.update_interaction_solution({ 'answer_is_exclusive': True, 'correct_answer': 'hello_world!', 'explanation': { 'content_id': 'solution', 'html': 'hello_world is a string' } }) self._assert_validation_error( exploration, re.escape('Hint(s) must be specified if solution is specified')) init_state.update_interaction_solution(None) interaction.hints = {} self._assert_validation_error( exploration, 'Expected hints to be a list') interaction.hints = [] # Validate AnswerGroup. answer_groups_dict = { 'outcome': { 'dest': exploration.init_state_name, 'feedback': { 'content_id': 'feedback_1', 'html': 'Feedback' }, 'labelled_as_correct': False, 'param_changes': [], 'refresher_exploration_id': None, 'missing_prerequisite_skill_id': None }, 'rule_specs': [{ 'inputs': { 'x': 'Test' }, 'rule_type': 'Contains' }], 'training_data': [], 'tagged_skill_misconception_id': 1 } init_state.update_interaction_answer_groups([answer_groups_dict]) self._assert_validation_error( exploration, 'Expected tagged skill misconception id to be a str, received 1') answer_groups_dict = { 'outcome': { 'dest': exploration.init_state_name, 'feedback': { 'content_id': 'feedback_1', 'html': 'Feedback' }, 'labelled_as_correct': False, 'param_changes': [], 'refresher_exploration_id': None, 'missing_prerequisite_skill_id': None }, 'rule_specs': [{ 'inputs': { 'x': 'Test' }, 'rule_type': 'Contains' }], 'training_data': [], 'tagged_skill_misconception_id': 'invalid_tagged_skill_misconception_id' } init_state.update_interaction_answer_groups([answer_groups_dict]) self._assert_validation_error( exploration, 'Expected the format of tagged skill misconception id ' 'to be <skill_id>-<misconception_id>, received ' 'invalid_tagged_skill_misconception_id') init_state.interaction.answer_groups[0].rule_specs = {} self._assert_validation_error( exploration, 'Expected answer group rules to be a list') first_answer_group = init_state.interaction.answer_groups[0] first_answer_group.tagged_skill_misconception_id = None first_answer_group.rule_specs = [] self._assert_validation_error( exploration, 'There must be at least one rule or training data for each' ' answer group.') exploration.states = { exploration.init_state_name: ( state_domain.State.create_default_state( exploration.init_state_name)) } exploration.states[exploration.init_state_name].update_interaction_id( 'TextInput') exploration.validate() exploration.language_code = 'fake_code' self._assert_validation_error(exploration, 'Invalid language_code') exploration.language_code = 'English' self._assert_validation_error(exploration, 'Invalid language_code') exploration.language_code = 'en' exploration.validate() exploration.param_specs = 'A string' self._assert_validation_error(exploration, 'param_specs to be a dict') exploration.param_specs = { '@': param_domain.ParamSpec.from_dict({ 'obj_type': 'UnicodeString' }) } self._assert_validation_error( exploration, 'Only parameter names with characters') exploration.param_specs = { 'notAParamSpec': param_domain.ParamSpec.from_dict( {'obj_type': 'UnicodeString'}) } exploration.validate() def test_tag_validation(self): exploration = exp_domain.Exploration.create_default_exploration('eid') exploration.objective = 'Objective' init_state = exploration.states[exploration.init_state_name] init_state.update_interaction_id('EndExploration') init_state.update_interaction_default_outcome(None) exploration.validate() exploration.tags = 'this should be a list' self._assert_validation_error( exploration, 'Expected \'tags\' to be a list') exploration.tags = [123] self._assert_validation_error(exploration, 'to be a string') exploration.tags = ['abc', 123] self._assert_validation_error(exploration, 'to be a string') exploration.tags = [''] self._assert_validation_error(exploration, 'Tags should be non-empty') exploration.tags = ['123'] self._assert_validation_error( exploration, 'should only contain lowercase letters and spaces') exploration.tags = ['ABC'] self._assert_validation_error( exploration, 'should only contain lowercase letters and spaces') exploration.tags = [' a b'] self._assert_validation_error( exploration, 'Tags should not start or end with whitespace') exploration.tags = ['a b '] self._assert_validation_error( exploration, 'Tags should not start or end with whitespace') exploration.tags = ['a b'] self._assert_validation_error( exploration, 'Adjacent whitespace in tags should be collapsed') exploration.tags = ['abc', 'abc'] self._assert_validation_error( exploration, 'Some tags duplicate each other') exploration.tags = ['computer science', 'analysis', 'a b c'] exploration.validate() def test_title_category_and_objective_validation(self): self.save_new_valid_exploration( 'exp_id', 'user@example.com', title='', category='', objective='', end_state_name='End') exploration = exp_fetchers.get_exploration_by_id('exp_id') exploration.validate() with self.assertRaisesRegexp( utils.ValidationError, 'title must be specified' ): exploration.validate(strict=True) exploration.title = 'A title' with self.assertRaisesRegexp( utils.ValidationError, 'category must be specified' ): exploration.validate(strict=True) exploration.category = 'A category' with self.assertRaisesRegexp( utils.ValidationError, 'objective must be specified' ): exploration.validate(strict=True) exploration.objective = 'An objective' exploration.validate(strict=True) def test_get_trainable_states_dict(self): exp_id = 'exp_id1' test_exp_filepath = os.path.join( feconf.TESTS_DATA_DIR, 'string_classifier_test.yaml') yaml_content = utils.get_file_contents(test_exp_filepath) assets_list = [] exp_services.save_new_exploration_from_yaml_and_assets( feconf.SYSTEM_COMMITTER_ID, yaml_content, exp_id, assets_list) exploration_model = exp_models.ExplorationModel.get( exp_id, strict=False) old_states = exp_fetchers.get_exploration_from_model( exploration_model).states exploration = exp_fetchers.get_exploration_by_id(exp_id) # Rename a state to add it in unchanged answer group. exploration.rename_state('Home', 'Renamed state') change_list = [exp_domain.ExplorationChange({ 'cmd': 'rename_state', 'old_state_name': 'Home', 'new_state_name': 'Renamed state' })] expected_dict = { 'state_names_with_changed_answer_groups': [], 'state_names_with_unchanged_answer_groups': ['Renamed state'] } exp_versions_diff = exp_domain.ExplorationVersionsDiff(change_list) actual_dict = exploration.get_trainable_states_dict( old_states, exp_versions_diff) self.assertEqual(actual_dict, expected_dict) # Modify answer groups to trigger change in answer groups. state = exploration.states['Renamed state'] exploration.states['Renamed state'].interaction.answer_groups.insert( 3, state.interaction.answer_groups[3]) answer_groups = [] for answer_group in state.interaction.answer_groups: answer_groups.append(answer_group.to_dict()) change_list = [exp_domain.ExplorationChange({ 'cmd': 'edit_state_property', 'state_name': 'Renamed state', 'property_name': 'answer_groups', 'new_value': answer_groups })] expected_dict = { 'state_names_with_changed_answer_groups': ['Renamed state'], 'state_names_with_unchanged_answer_groups': [] } exp_versions_diff = exp_domain.ExplorationVersionsDiff(change_list) actual_dict = exploration.get_trainable_states_dict( old_states, exp_versions_diff) self.assertEqual(actual_dict, expected_dict) # Add new state to trigger change in answer groups. exploration.add_states(['New state']) exploration.states['New state'] = copy.deepcopy( exploration.states['Renamed state']) change_list = [exp_domain.ExplorationChange({ 'cmd': 'add_state', 'state_name': 'New state', })] expected_dict = { 'state_names_with_changed_answer_groups': [ 'New state', 'Renamed state'], 'state_names_with_unchanged_answer_groups': [] } exp_versions_diff = exp_domain.ExplorationVersionsDiff(change_list) actual_dict = exploration.get_trainable_states_dict( old_states, exp_versions_diff) self.assertEqual(actual_dict, expected_dict) # Delete state. exploration.delete_state('New state') change_list = [exp_domain.ExplorationChange({ 'cmd': 'delete_state', 'state_name': 'New state' })] expected_dict = { 'state_names_with_changed_answer_groups': ['Renamed state'], 'state_names_with_unchanged_answer_groups': [] } exp_versions_diff = exp_domain.ExplorationVersionsDiff(change_list) actual_dict = exploration.get_trainable_states_dict( old_states, exp_versions_diff) self.assertEqual(actual_dict, expected_dict) # Test addition and multiple renames. exploration.add_states(['New state']) exploration.states['New state'] = copy.deepcopy( exploration.states['Renamed state']) exploration.rename_state('New state', 'New state2') exploration.rename_state('New state2', 'New state3') change_list = [exp_domain.ExplorationChange({ 'cmd': 'add_state', 'state_name': 'New state', }), exp_domain.ExplorationChange({ 'cmd': 'rename_state', 'old_state_name': 'New state', 'new_state_name': 'New state2' }), exp_domain.ExplorationChange({ 'cmd': 'rename_state', 'old_state_name': 'New state2', 'new_state_name': 'New state3' })] expected_dict = { 'state_names_with_changed_answer_groups': [ 'Renamed state', 'New state3'], 'state_names_with_unchanged_answer_groups': [] } exp_versions_diff = exp_domain.ExplorationVersionsDiff(change_list) actual_dict = exploration.get_trainable_states_dict( old_states, exp_versions_diff) self.assertEqual(actual_dict, expected_dict) def test_is_demo_property(self): demo = exp_domain.Exploration.create_default_exploration('0') self.assertEqual(demo.is_demo, True) notdemo1 = exp_domain.Exploration.create_default_exploration('a') self.assertEqual(notdemo1.is_demo, False) notdemo2 = exp_domain.Exploration.create_default_exploration('abcd') self.assertEqual(notdemo2.is_demo, False) def test_has_state_name(self): demo = exp_domain.Exploration.create_default_exploration('0') state_names = demo.states.keys() self.assertEqual(state_names, ['Introduction']) self.assertEqual(demo.has_state_name('Introduction'), True) self.assertEqual(demo.has_state_name('Fake state name'), False) def test_exploration_export_import(self): demo = exp_domain.Exploration.create_default_exploration('0') demo_dict = demo.to_dict() exp_from_dict = exp_domain.Exploration.from_dict(demo_dict) self.assertEqual(exp_from_dict.to_dict(), demo_dict) def test_interaction_with_none_id_is_not_terminal(self): # Default exploration has a default interaction with an ID of None. demo = exp_domain.Exploration.create_default_exploration('0') init_state = demo.states[feconf.DEFAULT_INIT_STATE_NAME] self.assertFalse(init_state.interaction.is_terminal) def test_cannot_create_demo_exp_with_invalid_param_changes(self): demo_exp = exp_domain.Exploration.create_default_exploration('0') demo_dict = demo_exp.to_dict() new_state = state_domain.State.create_default_state('new_state_name') new_state.param_changes = [param_domain.ParamChange.from_dict({ 'customization_args': { 'list_of_values': ['1', '2'], 'parse_with_jinja': False }, 'name': 'myParam', 'generator_id': 'RandomSelector' })] demo_dict['states']['new_state_name'] = new_state.to_dict() demo_dict['param_specs'] = { 'ParamSpec': {'obj_type': 'UnicodeString'} } with self.assertRaisesRegexp( Exception, 'Parameter myParam was used in a state but not ' 'declared in the exploration param_specs.'): exp_domain.Exploration.from_dict(demo_dict) def test_validate_exploration_category(self): exploration = self.save_new_valid_exploration( 'exp_id', 'user@example.com', title='', category='', objective='', end_state_name='End') exploration.validate() exploration.category = 1 with self.assertRaisesRegexp( Exception, 'Expected category to be a string, received 1'): exploration.validate() def test_validate_exploration_objective(self): exploration = self.save_new_valid_exploration( 'exp_id', 'user@example.com', title='', category='', objective='', end_state_name='End') exploration.validate() exploration.objective = 1 with self.assertRaisesRegexp( Exception, 'Expected objective to be a string, received 1'): exploration.validate() def test_validate_exploration_blurb(self): exploration = self.save_new_valid_exploration( 'exp_id', 'user@example.com', title='', category='', objective='', end_state_name='End') exploration.validate() exploration.blurb = 1 with self.assertRaisesRegexp( Exception, 'Expected blurb to be a string, received 1'): exploration.validate() def test_validate_exploration_language_code(self): exploration = self.save_new_valid_exploration( 'exp_id', 'user@example.com', title='', category='', objective='', end_state_name='End') exploration.validate() exploration.language_code = 1 with self.assertRaisesRegexp( Exception, 'Expected language_code to be a string, received 1'): exploration.validate() def test_validate_exploration_author_notes(self): exploration = self.save_new_valid_exploration( 'exp_id', 'user@example.com', title='', category='', objective='', end_state_name='End') exploration.validate() exploration.author_notes = 1 with self.assertRaisesRegexp( Exception, 'Expected author_notes to be a string, received 1'): exploration.validate() def test_validate_exploration_states(self): exploration = self.save_new_valid_exploration( 'exp_id', 'user@example.com', title='', category='', objective='', end_state_name='End') exploration.validate() exploration.states = 1 with self.assertRaisesRegexp( Exception, 'Expected states to be a dict, received 1'): exploration.validate() def test_validate_exploration_outcome_dest(self): exploration = self.save_new_valid_exploration( 'exp_id', 'user@example.com', title='', category='', objective='', end_state_name='End') exploration.validate() exploration.init_state.interaction.default_outcome.dest = None with self.assertRaisesRegexp( Exception, 'Every outcome should have a destination.'): exploration.validate() def test_validate_exploration_outcome_dest_type(self): exploration = self.save_new_valid_exploration( 'exp_id', 'user@example.com', title='', category='', objective='', end_state_name='End') exploration.validate() exploration.init_state.interaction.default_outcome.dest = 1 with self.assertRaisesRegexp( Exception, 'Expected outcome dest to be a string, received 1'): exploration.validate() def test_validate_exploration_states_schema_version(self): exploration = self.save_new_valid_exploration( 'exp_id', 'user@example.com', title='', category='', objective='', end_state_name='End') exploration.validate() exploration.states_schema_version = None with self.assertRaisesRegexp( Exception, 'This exploration has no states schema version.'): exploration.validate() def test_validate_exploration_auto_tts_enabled(self): exploration = self.save_new_valid_exploration( 'exp_id', 'user@example.com', title='', category='', objective='', end_state_name='End') exploration.validate() exploration.auto_tts_enabled = 1 with self.assertRaisesRegexp( Exception, 'Expected auto_tts_enabled to be a bool, received 1'): exploration.validate() def test_validate_exploration_correctness_feedback_enabled(self): exploration = self.save_new_valid_exploration( 'exp_id', 'user@example.com', title='', category='', objective='', end_state_name='End') exploration.validate() exploration.correctness_feedback_enabled = 1 with self.assertRaisesRegexp( Exception, 'Expected correctness_feedback_enabled to be a bool, received 1'): exploration.validate() def test_validate_exploration_param_specs(self): exploration = self.save_new_valid_exploration( 'exp_id', 'user@example.com', title='', category='', objective='', end_state_name='End') exploration.validate() exploration.param_specs = { 1: param_domain.ParamSpec.from_dict( {'obj_type': 'UnicodeString'}) } with self.assertRaisesRegexp( Exception, 'Expected parameter name to be a string, received 1'): exploration.validate() def test_validate_exploration_param_changes_type(self): exploration = self.save_new_valid_exploration( 'exp_id', 'user@example.com', title='', category='', objective='', end_state_name='End') exploration.validate() exploration.param_changes = 1 with self.assertRaisesRegexp( Exception, 'Expected param_changes to be a list, received 1'): exploration.validate() def test_validate_exploration_param_name(self): exploration = self.save_new_valid_exploration( 'exp_id', 'user@example.com', title='', category='', objective='', end_state_name='End') exploration.validate() exploration.param_changes = [param_domain.ParamChange.from_dict({ 'customization_args': { 'list_of_values': ['1', '2'], 'parse_with_jinja': False }, 'name': 'invalid', 'generator_id': 'RandomSelector' })] with self.assertRaisesRegexp( Exception, 'No parameter named \'invalid\' exists in this ' 'exploration'): exploration.validate() def test_validate_exploration_reserved_param_name(self): exploration = self.save_new_valid_exploration( 'exp_id', 'user@example.com', title='', category='', objective='', end_state_name='End') exploration.validate() exploration.param_changes = [param_domain.ParamChange.from_dict({ 'customization_args': { 'list_of_values': ['1', '2'], 'parse_with_jinja': False }, 'name': 'all', 'generator_id': 'RandomSelector' })] with self.assertRaisesRegexp( Exception, 'The exploration-level parameter with name \'all\' is ' 'reserved. Please choose a different name.'): exploration.validate() def test_validate_exploration_is_non_self_loop(self): exploration = self.save_new_valid_exploration( 'exp_id', 'user@example.com', title='', category='', objective='', end_state_name='End') exploration.validate() exploration.add_states(['DEF']) default_outcome_dict = { 'dest': 'DEF', 'feedback': { 'content_id': 'default_outcome', 'html': '<p>Default outcome for state1</p>' }, 'param_changes': [], 'labelled_as_correct': False, 'refresher_exploration_id': 'refresher_exploration_id', 'missing_prerequisite_skill_id': None } exploration.init_state.update_interaction_default_outcome( default_outcome_dict) with self.assertRaisesRegexp( Exception, 'The default outcome for state Introduction has a refresher ' 'exploration ID, but is not a self-loop.'): exploration.validate() def test_validate_exploration_answer_group_parameter(self): exploration = self.save_new_valid_exploration( 'exp_id', 'user@example.com', title='', category='', objective='', end_state_name='End') exploration.validate() param_changes = [{ 'customization_args': { 'list_of_values': ['1', '2'], 'parse_with_jinja': False }, 'name': 'ParamChange', 'generator_id': 'RandomSelector' }] answer_groups = [{ 'outcome': { 'dest': exploration.init_state_name, 'feedback': { 'content_id': 'feedback_1', 'html': 'Feedback' }, 'labelled_as_correct': False, 'param_changes': param_changes, 'refresher_exploration_id': None, 'missing_prerequisite_skill_id': None }, 'rule_specs': [{ 'inputs': { 'x': 'Test' }, 'rule_type': 'Contains' }], 'training_data': [], 'tagged_skill_misconception_id': None }] exploration.init_state.update_interaction_answer_groups(answer_groups) with self.assertRaisesRegexp( Exception, 'The parameter ParamChange was used in an answer group, ' 'but it does not exist in this exploration'): exploration.validate() def test_verify_all_states_reachable(self): exploration = self.save_new_valid_exploration( 'exp_id', 'owner_id') exploration.validate() exploration.add_states(['End']) end_state = exploration.states['End'] end_state.update_interaction_id('EndExploration') end_state.update_interaction_default_outcome(None) with self.assertRaisesRegexp( Exception, 'Please fix the following issues before saving this exploration: ' '1. The following states are not reachable from the initial state: ' 'End 2. It is impossible to complete the exploration from the ' 'following states: Introduction'): exploration.validate(strict=True) def test_update_init_state_name_with_invalid_state(self): exploration = self.save_new_valid_exploration( 'exp_id', 'user@example.com', title='title', category='category', objective='objective', end_state_name='End') exploration.update_init_state_name('End') self.assertEqual(exploration.init_state_name, 'End') with self.assertRaisesRegexp( Exception, 'Invalid new initial state name: invalid_state;'): exploration.update_init_state_name('invalid_state') def test_rename_state_with_invalid_state(self): exploration = self.save_new_valid_exploration( 'exp_id', 'user@example.com', title='title', category='category', objective='objective', end_state_name='End') self.assertTrue(exploration.states.get('End')) self.assertFalse(exploration.states.get('new state name')) exploration.rename_state('End', 'new state name') self.assertFalse(exploration.states.get('End')) self.assertTrue(exploration.states.get('new state name')) with self.assertRaisesRegexp( Exception, 'State invalid_state does not exist'): exploration.rename_state('invalid_state', 'new state name') def test_default_outcome_is_labelled_incorrect_for_self_loop(self): exploration = self.save_new_valid_exploration( 'exp_id', 'user@example.com', title='title', category='category', objective='objective', end_state_name='End') exploration.validate(strict=True) (exploration.init_state.interaction.default_outcome .labelled_as_correct) = True (exploration.init_state.interaction.default_outcome .dest) = exploration.init_state_name with self.assertRaisesRegexp( Exception, 'The default outcome for state Introduction is labelled ' 'correct but is a self-loop'): exploration.validate(strict=True) class ExplorationSummaryTests(test_utils.GenericTestBase): def setUp(self): super(ExplorationSummaryTests, self).setUp() self.signup(self.OWNER_EMAIL, self.OWNER_USERNAME) owner_id = self.get_user_id_from_email(self.OWNER_EMAIL) exploration = exp_domain.Exploration.create_default_exploration('eid') exp_services.save_new_exploration(owner_id, exploration) self.exp_summary = exp_fetchers.get_exploration_summary_by_id('eid') def test_validation_passes_with_valid_properties(self): self.exp_summary.validate() def test_validation_fails_with_invalid_title(self): self.exp_summary.title = 0 with self.assertRaisesRegexp( utils.ValidationError, 'Expected title to be a string, received 0'): self.exp_summary.validate() def test_validation_fails_with_invalid_category(self): self.exp_summary.category = 0 with self.assertRaisesRegexp( utils.ValidationError, 'Expected category to be a string, received 0'): self.exp_summary.validate() def test_validation_fails_with_invalid_objective(self): self.exp_summary.objective = 0 with self.assertRaisesRegexp( utils.ValidationError, 'Expected objective to be a string, received 0'): self.exp_summary.validate() def test_validation_fails_with_invalid_language_code(self): self.exp_summary.language_code = 0 with self.assertRaisesRegexp( utils.ValidationError, 'Expected language_code to be a string, received 0'): self.exp_summary.validate() def test_validation_fails_with_unallowed_language_code(self): self.exp_summary.language_code = 'invalid' with self.assertRaisesRegexp( utils.ValidationError, 'Invalid language_code: invalid'): self.exp_summary.validate() def test_validation_fails_with_invalid_tags(self): self.exp_summary.tags = 'tags' with self.assertRaisesRegexp( utils.ValidationError, 'Expected \'tags\' to be a list, received tags'): self.exp_summary.validate() def test_validation_fails_with_invalid_tag_in_tags(self): self.exp_summary.tags = ['tag', 2] with self.assertRaisesRegexp( utils.ValidationError, 'Expected each tag in \'tags\' to be a string, received \'2\''): self.exp_summary.validate() def test_validation_fails_with_empty_tag_in_tags(self): self.exp_summary.tags = ['', 'abc'] with self.assertRaisesRegexp( utils.ValidationError, 'Tags should be non-empty'): self.exp_summary.validate() def test_validation_fails_with_unallowed_characters_in_tag(self): self.exp_summary.tags = ['123', 'abc'] with self.assertRaisesRegexp( utils.ValidationError, ( 'Tags should only contain lowercase ' 'letters and spaces, received \'123\'')): self.exp_summary.validate() def test_validation_fails_with_whitespace_in_tag_start(self): self.exp_summary.tags = [' ab', 'abc'] with self.assertRaisesRegexp( utils.ValidationError, 'Tags should not start or end with whitespace, received \' ab\''): self.exp_summary.validate() def test_validation_fails_with_whitespace_in_tag_end(self): self.exp_summary.tags = ['ab ', 'abc'] with self.assertRaisesRegexp( utils.ValidationError, 'Tags should not start or end with whitespace, received \'ab \''): self.exp_summary.validate() def test_validation_fails_with_adjacent_whitespace_in_tag(self): self.exp_summary.tags = ['a b', 'abc'] with self.assertRaisesRegexp( utils.ValidationError, ( 'Adjacent whitespace in tags should ' 'be collapsed, received \'a b\'')): self.exp_summary.validate() def test_validation_fails_with_duplicate_tags(self): self.exp_summary.tags = ['abc', 'abc', 'ab'] with self.assertRaisesRegexp( utils.ValidationError, 'Some tags duplicate each other'): self.exp_summary.validate() def test_validation_fails_with_invalid_rating_type(self): self.exp_summary.ratings = 0 with self.assertRaisesRegexp( utils.ValidationError, 'Expected ratings to be a dict, received 0'): self.exp_summary.validate() def test_validation_fails_with_invalid_rating_keys(self): self.exp_summary.ratings = {'1': 0, '10': 1} with self.assertRaisesRegexp( utils.ValidationError, 'Expected ratings to have keys: 1, 2, 3, 4, 5, received 1, 10'): self.exp_summary.validate() def test_validation_fails_with_invalid_value_type_for_ratings(self): self.exp_summary.ratings = {'1': 0, '2': 'one', '3': 0, '4': 0, '5': 0} with self.assertRaisesRegexp( utils.ValidationError, 'Expected value to be int, received one'): self.exp_summary.validate() def test_validation_fails_with_invalid_value_for_ratings(self): self.exp_summary.ratings = {'1': 0, '2': -1, '3': 0, '4': 0, '5': 0} with self.assertRaisesRegexp( utils.ValidationError, 'Expected value to be non-negative, received -1'): self.exp_summary.validate() def test_validation_fails_with_invalid_scaled_average_rating(self): self.exp_summary.scaled_average_rating = 'one' with self.assertRaisesRegexp( utils.ValidationError, 'Expected scaled_average_rating to be float, received one'): self.exp_summary.validate() def test_validation_fails_with_invalid_status(self): self.exp_summary.status = 0 with self.assertRaisesRegexp( utils.ValidationError, 'Expected status to be string, received 0'): self.exp_summary.validate() def test_validation_fails_with_invalid_community_owned(self): self.exp_summary.community_owned = '1' with self.assertRaisesRegexp( utils.ValidationError, 'Expected community_owned to be bool, received 1'): self.exp_summary.validate() def test_validation_fails_with_invalid_contributors_summary(self): self.exp_summary.contributors_summary = 0 with self.assertRaisesRegexp( utils.ValidationError, 'Expected contributors_summary to be dict, received 0'): self.exp_summary.validate() def test_validation_fails_with_invalid_owner_ids_type(self): self.exp_summary.owner_ids = 0 with self.assertRaisesRegexp( utils.ValidationError, 'Expected owner_ids to be list, received 0'): self.exp_summary.validate() def test_validation_fails_with_invalid_owner_id_in_owner_ids(self): self.exp_summary.owner_ids = ['1', 2, '3'] with self.assertRaisesRegexp( utils.ValidationError, 'Expected each id in owner_ids to be string, received 2'): self.exp_summary.validate() def test_validation_fails_with_invalid_editor_ids_type(self): self.exp_summary.editor_ids = 0 with self.assertRaisesRegexp( utils.ValidationError, 'Expected editor_ids to be list, received 0'): self.exp_summary.validate() def test_validation_fails_with_invalid_editor_id_in_editor_ids(self): self.exp_summary.editor_ids = ['1', 2, '3'] with self.assertRaisesRegexp( utils.ValidationError, 'Expected each id in editor_ids to be string, received 2'): self.exp_summary.validate() def test_validation_fails_with_invalid_voice_artist_ids_type(self): self.exp_summary.voice_artist_ids = 0 with self.assertRaisesRegexp( utils.ValidationError, 'Expected voice_artist_ids to be list, received 0'): self.exp_summary.validate() def test_validation_fails_with_invalid_voice_artist_id_in_voice_artists_ids( self): self.exp_summary.voice_artist_ids = ['1', 2, '3'] with self.assertRaisesRegexp( utils.ValidationError, 'Expected each id in voice_artist_ids to be string, received 2'): self.exp_summary.validate() def test_validation_fails_with_invalid_viewer_ids_type(self): self.exp_summary.viewer_ids = 0 with self.assertRaisesRegexp( utils.ValidationError, 'Expected viewer_ids to be list, received 0'): self.exp_summary.validate() def test_validation_fails_with_invalid_viewer_id_in_viewer_ids(self): self.exp_summary.viewer_ids = ['1', 2, '3'] with self.assertRaisesRegexp( utils.ValidationError, 'Expected each id in viewer_ids to be string, received 2'): self.exp_summary.validate() def test_validation_fails_with_invalid_contributor_ids_type(self): self.exp_summary.contributor_ids = 0 with self.assertRaisesRegexp( utils.ValidationError, 'Expected contributor_ids to be list, received 0'): self.exp_summary.validate() def test_validation_fails_with_invalid_contributor_id_in_contributor_ids( self): self.exp_summary.contributor_ids = ['1', 2, '3'] with self.assertRaisesRegexp( utils.ValidationError, 'Expected each id in contributor_ids to be string, received 2'): self.exp_summary.validate() class YamlCreationUnitTests(test_utils.GenericTestBase): EXP_ID = 'An exploration_id' def test_yaml_import_and_export(self): exploration = exp_domain.Exploration.create_default_exploration( self.EXP_ID, title='Title', category='Category') exploration.add_states(['New state']) self.assertEqual(len(exploration.states), 2) exploration.validate() yaml_content = exploration.to_yaml() self.assertEqual(yaml_content, self.SAMPLE_YAML_CONTENT) exploration2 = exp_domain.Exploration.from_yaml('exp2', yaml_content) self.assertEqual(len(exploration2.states), 2) yaml_content_2 = exploration2.to_yaml() self.assertEqual(yaml_content_2, yaml_content) # Verify SAMPLE_UNTITLED_YAML_CONTENT can be converted to an exploration # without error. exp_domain.Exploration.from_untitled_yaml( 'exp4', 'Title', 'Category', self.SAMPLE_UNTITLED_YAML_CONTENT) with self.assertRaises(Exception): exp_domain.Exploration.from_yaml('exp3', 'No_initial_state_name') with self.assertRaises(Exception): exp_domain.Exploration.from_yaml( 'exp4', 'Invalid\ninit_state_name:\nMore stuff') with self.assertRaises(Exception): exp_domain.Exploration.from_yaml( 'exp4', 'State1:\n(\nInvalid yaml') with self.assertRaisesRegexp( Exception, 'Expected a YAML version >= 10, received: 9' ): exp_domain.Exploration.from_yaml( 'exp4', self.SAMPLE_UNTITLED_YAML_CONTENT) with self.assertRaisesRegexp( Exception, 'Expected a YAML version <= 9' ): exp_domain.Exploration.from_untitled_yaml( 'exp4', 'Title', 'Category', self.SAMPLE_YAML_CONTENT) class SchemaMigrationMethodsUnitTests(test_utils.GenericTestBase): def test_correct_states_schema_conversion_methods_exist(self): current_states_schema_version = ( feconf.CURRENT_STATE_SCHEMA_VERSION) for version_num in range(current_states_schema_version): self.assertTrue(hasattr( exp_domain.Exploration, '_convert_states_v%s_dict_to_v%s_dict' % ( version_num, version_num + 1))) self.assertFalse(hasattr( exp_domain.Exploration, '_convert_states_v%s_dict_to_v%s_dict' % ( current_states_schema_version, current_states_schema_version + 1))) def test_correct_exploration_schema_conversion_methods_exist(self): current_exp_schema_version = ( exp_domain.Exploration.CURRENT_EXP_SCHEMA_VERSION) for version_num in range(1, current_exp_schema_version): self.assertTrue(hasattr( exp_domain.Exploration, '_convert_v%s_dict_to_v%s_dict' % ( version_num, version_num + 1))) self.assertFalse(hasattr( exp_domain.Exploration, '_convert_v%s_dict_to_v%s_dict' % ( current_exp_schema_version, current_exp_schema_version + 1))) class SchemaMigrationUnitTests(test_utils.GenericTestBase): YAML_CONTENT_V1 = ("""default_skin: conversation_v1 param_changes: [] param_specs: {} schema_version: 1 states: - content: - type: text value: '' name: (untitled state) param_changes: [] widget: customization_args: {} handlers: - name: submit rule_specs: - definition: inputs: x: InputString name: Equals rule_type: atomic dest: END feedback: - Correct! param_changes: [] - definition: rule_type: default dest: (untitled state) feedback: [] param_changes: [] sticky: false widget_id: TextInput - content: - type: text value: '' name: New state param_changes: [] widget: customization_args: {} handlers: - name: submit rule_specs: - definition: rule_type: default dest: END feedback: [] param_changes: [] sticky: false widget_id: TextInput """) YAML_CONTENT_V2 = ("""default_skin: conversation_v1 init_state_name: (untitled state) param_changes: [] param_specs: {} schema_version: 2 states: (untitled state): content: - type: text value: '' param_changes: [] widget: customization_args: {} handlers: - name: submit rule_specs: - definition: inputs: x: InputString name: Equals rule_type: atomic dest: END feedback: - Correct! param_changes: [] - definition: rule_type: default dest: (untitled state) feedback: [] param_changes: [] sticky: false widget_id: TextInput New state: content: - type: text value: '' param_changes: [] widget: customization_args: {} handlers: - name: submit rule_specs: - definition: rule_type: default dest: END feedback: [] param_changes: [] sticky: false widget_id: TextInput """) YAML_CONTENT_V3 = ("""author_notes: '' blurb: '' default_skin: conversation_v1 init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 3 skill_tags: [] states: (untitled state): content: - type: text value: '' param_changes: [] widget: customization_args: placeholder: value: '' rows: value: 1 handlers: - name: submit rule_specs: - definition: inputs: x: InputString name: Equals rule_type: atomic dest: END feedback: - Correct! param_changes: [] - definition: rule_type: default dest: (untitled state) feedback: [] param_changes: [] sticky: false widget_id: TextInput New state: content: - type: text value: '' param_changes: [] widget: customization_args: placeholder: value: '' rows: value: 1 handlers: - name: submit rule_specs: - definition: rule_type: default dest: END feedback: [] param_changes: [] sticky: false widget_id: TextInput """) YAML_CONTENT_V4 = ("""author_notes: '' blurb: '' default_skin: conversation_v1 init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 4 skill_tags: [] states: (untitled state): content: - type: text value: '' interaction: customization_args: placeholder: value: '' rows: value: 1 handlers: - name: submit rule_specs: - definition: inputs: x: InputString name: Equals rule_type: atomic dest: END feedback: - Correct! param_changes: [] - definition: rule_type: default dest: (untitled state) feedback: [] param_changes: [] id: TextInput param_changes: [] New state: content: - type: text value: '' interaction: customization_args: placeholder: value: '' rows: value: 1 handlers: - name: submit rule_specs: - definition: rule_type: default dest: END feedback: [] param_changes: [] id: TextInput param_changes: [] """) YAML_CONTENT_V5 = ("""author_notes: '' blurb: '' default_skin: conversation_v1 init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 5 skin_customizations: panels_contents: {} states: (untitled state): content: - type: text value: '' interaction: customization_args: placeholder: value: '' rows: value: 1 handlers: - name: submit rule_specs: - definition: inputs: x: InputString name: Equals rule_type: atomic dest: END feedback: - Correct! param_changes: [] - definition: rule_type: default dest: (untitled state) feedback: [] param_changes: [] id: TextInput param_changes: [] New state: content: - type: text value: '' interaction: customization_args: placeholder: value: '' rows: value: 1 handlers: - name: submit rule_specs: - definition: rule_type: default dest: END feedback: [] param_changes: [] id: TextInput param_changes: [] widget: customization_args: {} handlers: - name: submit rule_specs: - definition: rule_type: default dest: END feedback: [] param_changes: [] sticky: false widget_id: TextInput END: content: - type: text value: Congratulations, you have finished! interaction: customization_args: recommendedExplorationIds: value: [] handlers: - name: submit rule_specs: - definition: rule_type: default dest: END feedback: [] param_changes: [] id: EndExploration triggers: [] param_changes: [] tags: [] """) YAML_CONTENT_V6 = ("""author_notes: '' blurb: '' default_skin: conversation_v1 init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 6 skin_customizations: panels_contents: {} states: (untitled state): content: - type: text value: '' interaction: customization_args: placeholder: value: '' rows: value: 1 handlers: - name: submit rule_specs: - definition: inputs: x: InputString name: Equals rule_type: atomic dest: END feedback: - Correct! param_changes: [] - definition: rule_type: default dest: (untitled state) feedback: [] param_changes: [] id: TextInput triggers: [] param_changes: [] END: content: - type: text value: Congratulations, you have finished! interaction: customization_args: recommendedExplorationIds: value: [] handlers: - name: submit rule_specs: - definition: rule_type: default dest: END feedback: [] param_changes: [] id: EndExploration triggers: [] param_changes: [] New state: content: - type: text value: '' interaction: customization_args: placeholder: value: '' rows: value: 1 handlers: - name: submit rule_specs: - definition: rule_type: default dest: END feedback: [] param_changes: [] id: TextInput triggers: [] param_changes: [] states_schema_version: 3 tags: [] """) YAML_CONTENT_V7 = ("""author_notes: '' blurb: '' default_skin: conversation_v1 init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 7 skin_customizations: panels_contents: {} states: (untitled state): content: - type: text value: '' interaction: answer_groups: - outcome: dest: END feedback: - Correct! param_changes: [] rule_specs: - inputs: x: InputString rule_type: Equals customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: [] param_changes: [] id: TextInput triggers: [] param_changes: [] END: content: - type: text value: Congratulations, you have finished! interaction: answer_groups: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null id: EndExploration triggers: [] param_changes: [] New state: content: - type: text value: '' interaction: answer_groups: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: [] param_changes: [] id: TextInput triggers: [] param_changes: [] states_schema_version: 4 tags: [] """) YAML_CONTENT_V8 = ("""author_notes: '' blurb: '' default_skin: conversation_v1 init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 8 skin_customizations: panels_contents: {} states: (untitled state): content: - type: text value: '' interaction: answer_groups: - outcome: dest: END feedback: - Correct! param_changes: [] rule_specs: - inputs: x: InputString rule_type: Equals customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: [] param_changes: [] fallbacks: [] id: TextInput param_changes: [] END: content: - type: text value: Congratulations, you have finished! interaction: answer_groups: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null fallbacks: [] id: EndExploration param_changes: [] New state: content: - type: text value: '' interaction: answer_groups: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: [] param_changes: [] fallbacks: [] id: TextInput param_changes: [] states_schema_version: 5 tags: [] """) YAML_CONTENT_V9 = ("""author_notes: '' blurb: '' default_skin: conversation_v1 init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 9 skin_customizations: panels_contents: {} states: (untitled state): content: - type: text value: '' interaction: answer_groups: - outcome: dest: END feedback: - Correct! param_changes: [] rule_specs: - inputs: x: InputString rule_type: Equals confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: [] param_changes: [] fallbacks: [] id: TextInput param_changes: [] END: content: - type: text value: Congratulations, you have finished! interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null fallbacks: [] id: EndExploration param_changes: [] New state: content: - type: text value: '' interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 language: value: '' default_outcome: dest: END feedback: [] param_changes: [] fallbacks: [] id: CodeRepl param_changes: [] states_schema_version: 6 tags: [] """) YAML_CONTENT_V10 = ("""author_notes: '' blurb: '' category: Category init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 10 skin_customizations: panels_contents: bottom: [] states: (untitled state): content: - type: text value: '' interaction: answer_groups: - outcome: dest: END feedback: - Correct! param_changes: [] rule_specs: - inputs: x: InputString rule_type: Equals confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: [] param_changes: [] fallbacks: [] id: TextInput param_changes: [] END: content: - type: text value: Congratulations, you have finished! interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null fallbacks: [] id: EndExploration param_changes: [] New state: content: - type: text value: '' interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: [] param_changes: [] fallbacks: [] id: TextInput param_changes: [] states_schema_version: 7 tags: [] title: Title """) YAML_CONTENT_V11 = ("""author_notes: '' blurb: '' category: Category init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 11 skin_customizations: panels_contents: bottom: [] states: (untitled state): classifier_model_id: null content: - type: text value: '' interaction: answer_groups: - outcome: dest: END feedback: - Correct! param_changes: [] rule_specs: - inputs: x: InputString rule_type: Equals confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: [] param_changes: [] fallbacks: [] id: TextInput param_changes: [] END: classifier_model_id: null content: - type: text value: Congratulations, you have finished! interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null fallbacks: [] id: EndExploration param_changes: [] New state: classifier_model_id: null content: - type: text value: '' interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: [] param_changes: [] fallbacks: [] id: TextInput param_changes: [] states_schema_version: 8 tags: [] title: Title """) YAML_CONTENT_V12 = ("""author_notes: '' blurb: '' category: Category init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 12 skin_customizations: panels_contents: bottom: [] states: (untitled state): classifier_model_id: null content: - type: text value: '' interaction: answer_groups: - correct: false outcome: dest: END feedback: - Correct! param_changes: [] rule_specs: - inputs: x: InputString rule_type: Equals confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: [] param_changes: [] fallbacks: [] id: TextInput param_changes: [] END: classifier_model_id: null content: - type: text value: Congratulations, you have finished! interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null fallbacks: [] id: EndExploration param_changes: [] New state: classifier_model_id: null content: - type: text value: '' interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: [] param_changes: [] fallbacks: - outcome: dest: END feedback: - Correct! id: TextInput param_changes: [] states_schema_version: 9 tags: [] title: Title """) YAML_CONTENT_V13 = ("""author_notes: '' blurb: '' category: Category init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 13 skin_customizations: panels_contents: bottom: [] states: (untitled state): classifier_model_id: null content: - type: text value: '' interaction: answer_groups: - correct: false outcome: dest: END feedback: - Correct! param_changes: [] rule_specs: - inputs: x: InputString rule_type: Equals confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: [] param_changes: [] fallbacks: [] hints: [] id: TextInput solution: {} param_changes: [] END: classifier_model_id: null content: - type: text value: Congratulations, you have finished! interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null fallbacks: [] hints: [] id: EndExploration solution: {} param_changes: [] New state: classifier_model_id: null content: - type: text value: '' interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: [] param_changes: [] fallbacks: [] hints: [] id: TextInput solution: {} param_changes: [] states_schema_version: 10 tags: [] title: Title """) YAML_CONTENT_V14 = ("""author_notes: '' blurb: '' category: Category init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 14 skin_customizations: panels_contents: bottom: [] states: (untitled state): classifier_model_id: null content: audio_translations: [] html: '' interaction: answer_groups: - correct: false outcome: dest: END feedback: - Correct! param_changes: [] rule_specs: - inputs: x: InputString rule_type: Equals confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: [] param_changes: [] fallbacks: [] hints: [] id: TextInput solution: {} param_changes: [] END: classifier_model_id: null content: audio_translations: [] html: Congratulations, you have finished! interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null fallbacks: [] hints: [] id: EndExploration solution: {} param_changes: [] New state: classifier_model_id: null content: audio_translations: [] html: '' interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: [] param_changes: [] fallbacks: [] hints: [] id: TextInput solution: {} param_changes: [] states_schema_version: 11 tags: [] title: Title """) YAML_CONTENT_V15 = ("""author_notes: '' blurb: '' category: Category init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 15 skin_customizations: panels_contents: bottom: [] states: (untitled state): classifier_model_id: null content: audio_translations: {} html: '' interaction: answer_groups: - correct: false outcome: dest: END feedback: - Correct! param_changes: [] rule_specs: - inputs: x: InputString rule_type: Equals confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: [] param_changes: [] fallbacks: [] hints: [] id: TextInput solution: {} param_changes: [] END: classifier_model_id: null content: audio_translations: {} html: Congratulations, you have finished! interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null fallbacks: [] hints: [] id: EndExploration solution: {} param_changes: [] New state: classifier_model_id: null content: audio_translations: {} html: '' interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: [] param_changes: [] fallbacks: [] hints: [] id: TextInput solution: {} param_changes: [] states_schema_version: 12 tags: [] title: Title """) YAML_CONTENT_V16 = ("""author_notes: '' blurb: '' category: Category init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 16 skin_customizations: panels_contents: bottom: [] states: (untitled state): classifier_model_id: null content: audio_translations: {} html: '' interaction: answer_groups: - correct: false outcome: dest: END feedback: - Correct! param_changes: [] rule_specs: - inputs: x: InputString rule_type: Equals confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: [] param_changes: [] hints: [] id: TextInput solution: null param_changes: [] END: classifier_model_id: null content: audio_translations: {} html: Congratulations, you have finished! interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null hints: [] id: EndExploration solution: null param_changes: [] New state: classifier_model_id: null content: audio_translations: {} html: '' interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: [] param_changes: [] hints: [] id: TextInput solution: null param_changes: [] states_schema_version: 13 tags: [] title: Title """) YAML_CONTENT_V17 = ("""author_notes: '' blurb: '' category: Category init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 17 states: (untitled state): classifier_model_id: null content: audio_translations: {} html: '' interaction: answer_groups: - correct: false outcome: dest: END feedback: - Correct! param_changes: [] rule_specs: - inputs: x: InputString rule_type: Equals confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: [] param_changes: [] hints: [] id: TextInput solution: null param_changes: [] END: classifier_model_id: null content: audio_translations: {} html: Congratulations, you have finished! interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null hints: [] id: EndExploration solution: null param_changes: [] New state: classifier_model_id: null content: audio_translations: {} html: '' interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: [] param_changes: [] hints: [] id: TextInput solution: null param_changes: [] states_schema_version: 13 tags: [] title: Title """) YAML_CONTENT_V18 = ("""author_notes: '' auto_tts_enabled: true blurb: '' category: Category init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 18 states: (untitled state): classifier_model_id: null content: audio_translations: {} html: '' interaction: answer_groups: - correct: false outcome: dest: END feedback: - Correct! param_changes: [] rule_specs: - inputs: x: InputString rule_type: Equals confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: [] param_changes: [] hints: [] id: TextInput solution: null param_changes: [] END: classifier_model_id: null content: audio_translations: {} html: Congratulations, you have finished! interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null hints: [] id: EndExploration solution: null param_changes: [] New state: classifier_model_id: null content: audio_translations: {} html: '' interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: [] param_changes: [] hints: - hint_text: '' id: TextInput solution: explanation: '' answer_is_exclusive: False correct_answer: Answer param_changes: [] states_schema_version: 13 tags: [] title: Title """) YAML_CONTENT_V19 = ("""author_notes: '' auto_tts_enabled: true blurb: '' category: Category init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 19 states: (untitled state): classifier_model_id: null content: audio_translations: {} html: '' interaction: answer_groups: - correct: false outcome: dest: END feedback: audio_translations: {} html: Correct! param_changes: [] rule_specs: - inputs: x: InputString rule_type: Equals confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: audio_translations: {} html: '' param_changes: [] hints: [] id: TextInput solution: null param_changes: [] END: classifier_model_id: null content: audio_translations: {} html: Congratulations, you have finished! interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null hints: [] id: EndExploration solution: null param_changes: [] New state: classifier_model_id: null content: audio_translations: {} html: '' interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: audio_translations: {} html: '' param_changes: [] hints: [] id: TextInput solution: null param_changes: [] states_schema_version: 14 tags: [] title: Title """) YAML_CONTENT_V20 = ("""author_notes: '' auto_tts_enabled: true blurb: '' category: Category correctness_feedback_enabled: false init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 20 states: (untitled state): classifier_model_id: null content: audio_translations: {} html: '' interaction: answer_groups: - labelled_as_correct: false outcome: dest: END feedback: audio_translations: {} html: Correct! param_changes: [] rule_specs: - inputs: x: InputString rule_type: Equals confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: audio_translations: {} html: '' param_changes: [] hints: [] id: TextInput solution: null param_changes: [] END: classifier_model_id: null content: audio_translations: {} html: Congratulations, you have finished! interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null hints: [] id: EndExploration solution: null param_changes: [] New state: classifier_model_id: null content: audio_translations: {} html: '' interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: audio_translations: {} html: '' param_changes: [] hints: [] id: TextInput solution: null param_changes: [] states_schema_version: 15 tags: [] title: Title """) YAML_CONTENT_V21 = ("""author_notes: '' auto_tts_enabled: true blurb: '' category: Category correctness_feedback_enabled: false init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 21 states: (untitled state): classifier_model_id: null content: audio_translations: {} html: '' interaction: answer_groups: - labelled_as_correct: false outcome: dest: END feedback: audio_translations: {} html: Correct! param_changes: [] refresher_exploration_id: null rule_specs: - inputs: x: InputString rule_type: Equals confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: audio_translations: {} html: '' param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] END: classifier_model_id: null content: audio_translations: {} html: Congratulations, you have finished! interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null hints: [] id: EndExploration solution: null param_changes: [] New state: classifier_model_id: null content: audio_translations: {} html: '' interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: audio_translations: {} html: '' param_changes: [] refresher_exploration_id: null hints: [] id: FractionInput solution: null param_changes: [] states_schema_version: 16 tags: [] title: Title """) YAML_CONTENT_V22 = ("""author_notes: '' auto_tts_enabled: true blurb: '' category: Category correctness_feedback_enabled: false init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 22 states: (untitled state): classifier_model_id: null content: audio_translations: {} html: '' interaction: answer_groups: - outcome: dest: END feedback: audio_translations: {} html: Correct! labelled_as_correct: false param_changes: [] refresher_exploration_id: null rule_specs: - inputs: x: InputString rule_type: Equals confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: audio_translations: {} html: '' labelled_as_correct: false param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] END: classifier_model_id: null content: audio_translations: {} html: Congratulations, you have finished! interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null hints: [] id: EndExploration solution: null param_changes: [] New state: classifier_model_id: null content: audio_translations: {} html: '' interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: audio_translations: {} html: '' labelled_as_correct: false param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] states_schema_version: 17 tags: [] title: Title """) YAML_CONTENT_V23 = ("""author_notes: '' auto_tts_enabled: true blurb: '' category: Category correctness_feedback_enabled: false init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 23 states: (untitled state): classifier_model_id: null content: audio_translations: {} html: '' interaction: answer_groups: - outcome: dest: END feedback: audio_translations: {} html: Correct! labelled_as_correct: false param_changes: [] refresher_exploration_id: null rule_specs: - inputs: x: InputString rule_type: Equals confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: audio_translations: {} html: '' labelled_as_correct: false param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] END: classifier_model_id: null content: audio_translations: {} html: Congratulations, you have finished! interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null hints: [] id: EndExploration solution: null param_changes: [] New state: classifier_model_id: null content: audio_translations: {} html: '' interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: audio_translations: {} html: '' labelled_as_correct: false param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] states_schema_version: 18 tags: [] title: Title """) YAML_CONTENT_V24 = ("""author_notes: '' auto_tts_enabled: true blurb: '' category: Category correctness_feedback_enabled: false init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 24 states: (untitled state): classifier_model_id: null content: audio_translations: {} html: '' interaction: answer_groups: - outcome: dest: END feedback: audio_translations: {} html: Correct! labelled_as_correct: false param_changes: [] refresher_exploration_id: null rule_specs: - inputs: x: InputString rule_type: Equals training_data: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: audio_translations: {} html: '' labelled_as_correct: false param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] END: classifier_model_id: null content: audio_translations: {} html: Congratulations, you have finished! interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null hints: [] id: EndExploration solution: null param_changes: [] New state: classifier_model_id: null content: audio_translations: {} html: '' interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: audio_translations: {} html: '' labelled_as_correct: false param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] states_schema_version: 19 tags: [] title: Title """) YAML_CONTENT_V25 = ("""author_notes: '' auto_tts_enabled: true blurb: '' category: Category correctness_feedback_enabled: false init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 25 states: (untitled state): classifier_model_id: null content: audio_translations: {} html: '' interaction: answer_groups: - outcome: dest: END feedback: audio_translations: {} html: Correct! labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null rule_specs: - inputs: x: InputString rule_type: Equals tagged_misconception_id: null training_data: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: audio_translations: {} html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] END: classifier_model_id: null content: audio_translations: {} html: Congratulations, you have finished! interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null hints: [] id: EndExploration solution: null param_changes: [] New state: classifier_model_id: null content: audio_translations: {} html: '' interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: audio_translations: {} html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] states_schema_version: 20 tags: [] title: Title """) YAML_CONTENT_V26 = ("""author_notes: '' auto_tts_enabled: true blurb: '' category: Category correctness_feedback_enabled: false init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 26 states: (untitled state): classifier_model_id: null content: content_id: content html: '' content_ids_to_audio_translations: content: {} default_outcome: {} feedback_1: {} interaction: answer_groups: - outcome: dest: END feedback: content_id: feedback_1 html: Correct! labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null rule_specs: - inputs: x: InputString rule_type: Equals tagged_misconception_id: null training_data: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] END: classifier_model_id: null content: content_id: content html: Congratulations, you have finished! content_ids_to_audio_translations: content: {} interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null hints: [] id: EndExploration solution: null param_changes: [] New state: classifier_model_id: null content: content_id: content html: '' content_ids_to_audio_translations: content: {} default_outcome: {} interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] states_schema_version: 21 tags: [] title: Title """) YAML_CONTENT_V27 = ("""author_notes: '' auto_tts_enabled: true blurb: '' category: Category correctness_feedback_enabled: false init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 27 states: (untitled state): classifier_model_id: null content: content_id: content html: '' content_ids_to_audio_translations: content: {} default_outcome: {} feedback_1: {} interaction: answer_groups: - outcome: dest: END feedback: content_id: feedback_1 html: <p>Correct!</p> labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null rule_specs: - inputs: x: InputString rule_type: Equals tagged_misconception_id: null training_data: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] END: classifier_model_id: null content: content_id: content html: <p>Congratulations, you have finished!</p> content_ids_to_audio_translations: content: {} interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null hints: [] id: EndExploration solution: null param_changes: [] New state: classifier_model_id: null content: content_id: content html: '' content_ids_to_audio_translations: content: {} default_outcome: {} interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] states_schema_version: 22 tags: [] title: Title """) YAML_CONTENT_V28 = ("""author_notes: '' auto_tts_enabled: true blurb: '' category: Category correctness_feedback_enabled: false init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 28 states: (untitled state): classifier_model_id: null content: content_id: content html: '' content_ids_to_audio_translations: content: {} default_outcome: {} feedback_1: {} interaction: answer_groups: - outcome: dest: END feedback: content_id: feedback_1 html: <p>Correct!</p> labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null rule_specs: - inputs: x: InputString rule_type: Equals tagged_misconception_id: null training_data: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] END: classifier_model_id: null content: content_id: content html: <p>Congratulations, you have finished!</p> content_ids_to_audio_translations: content: {} interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null hints: [] id: EndExploration solution: null param_changes: [] New state: classifier_model_id: null content: content_id: content html: '' content_ids_to_audio_translations: content: {} default_outcome: {} interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] states_schema_version: 23 tags: [] title: Title """) YAML_CONTENT_V29 = ("""author_notes: '' auto_tts_enabled: true blurb: '' category: Category correctness_feedback_enabled: false init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 29 states: (untitled state): classifier_model_id: null content: content_id: content html: '' content_ids_to_audio_translations: content: {} default_outcome: {} feedback_1: {} interaction: answer_groups: - outcome: dest: END feedback: content_id: feedback_1 html: <p>Correct!</p> labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null rule_specs: - inputs: x: InputString rule_type: Equals tagged_misconception_id: null training_data: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] END: classifier_model_id: null content: content_id: content html: <p>Congratulations, you have finished!</p> content_ids_to_audio_translations: content: {} interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null hints: [] id: EndExploration solution: null param_changes: [] New state: classifier_model_id: null content: content_id: content html: '' content_ids_to_audio_translations: content: {} default_outcome: {} interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 imageAndRegions: value: imagePath: s1ImagePath.png default_outcome: dest: END feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: ImageClickInput solution: null param_changes: [] states_schema_version: 24 tags: [] title: Title """) YAML_CONTENT_V30 = ("""author_notes: '' auto_tts_enabled: true blurb: '' category: Category correctness_feedback_enabled: false init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 30 states: (untitled state): classifier_model_id: null content: content_id: content html: '' content_ids_to_audio_translations: content: {} default_outcome: {} feedback_1: {} interaction: answer_groups: - outcome: dest: END feedback: content_id: feedback_1 html: <p>Correct!</p> labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null rule_specs: - inputs: x: InputString rule_type: Equals tagged_misconception_id: null training_data: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] END: classifier_model_id: null content: content_id: content html: <p>Congratulations, you have finished!</p> content_ids_to_audio_translations: content: {} interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null hints: [] id: EndExploration solution: null param_changes: [] New state: classifier_model_id: null content: content_id: content html: '' content_ids_to_audio_translations: content: {} default_outcome: {} interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] states_schema_version: 25 tags: [] title: Title """) YAML_CONTENT_V31 = ("""author_notes: '' auto_tts_enabled: true blurb: '' category: Category correctness_feedback_enabled: false init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 31 states: (untitled state): classifier_model_id: null content: content_id: content html: '' content_ids_to_audio_translations: content: {} default_outcome: {} feedback_1: {} interaction: answer_groups: - outcome: dest: END feedback: content_id: feedback_1 html: <p>Correct!</p> labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null rule_specs: - inputs: x: InputString rule_type: Equals tagged_misconception_id: null training_data: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] END: classifier_model_id: null content: content_id: content html: <p>Congratulations, you have finished!</p> content_ids_to_audio_translations: content: {} interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null hints: [] id: EndExploration solution: null param_changes: [] New state: classifier_model_id: null content: content_id: content html: '' content_ids_to_audio_translations: content: {} default_outcome: {} new_content: {} interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] states_schema_version: 26 tags: [] title: Title """) YAML_CONTENT_V32 = ("""author_notes: '' auto_tts_enabled: true blurb: '' category: Category correctness_feedback_enabled: false init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 32 states: (untitled state): classifier_model_id: null content: content_id: content html: '' content_ids_to_audio_translations: content: {} default_outcome: {} feedback_1: {} interaction: answer_groups: - outcome: dest: END feedback: content_id: feedback_1 html: <p>Correct!</p> labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null rule_specs: - inputs: x: InputString rule_type: Equals tagged_misconception_id: null training_data: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] written_translations: translations_mapping: content: {} default_outcome: {} feedback_1: {} END: classifier_model_id: null content: content_id: content html: <p>Congratulations, you have finished!</p> content_ids_to_audio_translations: content: {} interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null hints: [] id: EndExploration solution: null param_changes: [] written_translations: translations_mapping: content: {} New state: classifier_model_id: null content: content_id: content html: '' content_ids_to_audio_translations: content: {} default_outcome: {} interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] written_translations: translations_mapping: content: {} default_outcome: {} states_schema_version: 27 tags: [] title: Title """) YAML_CONTENT_V33 = ("""author_notes: '' auto_tts_enabled: true blurb: '' category: Category correctness_feedback_enabled: false init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 33 states: (untitled state): classifier_model_id: null content: content_id: content html: '' interaction: answer_groups: - outcome: dest: END feedback: content_id: feedback_1 html: <p>Correct!</p> labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null rule_specs: - inputs: x: InputString rule_type: Equals tagged_misconception_id: null training_data: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} default_outcome: {} feedback_1: {} written_translations: translations_mapping: content: {} default_outcome: {} feedback_1: {} END: classifier_model_id: null content: content_id: content html: <p>Congratulations, you have finished!</p> interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null hints: [] id: EndExploration solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} written_translations: translations_mapping: content: {} New state: classifier_model_id: null content: content_id: content html: '' interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} default_outcome: {} written_translations: translations_mapping: content: {} default_outcome: {} states_schema_version: 28 tags: [] title: Title """) YAML_CONTENT_V34 = ("""author_notes: '' auto_tts_enabled: true blurb: '' category: Category correctness_feedback_enabled: false init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 34 states: (untitled state): classifier_model_id: null content: content_id: content html: '' interaction: answer_groups: - outcome: dest: END feedback: content_id: feedback_1 html: <p>Correct!</p> labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null rule_specs: - inputs: x: InputString rule_type: Equals tagged_misconception_id: null training_data: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} default_outcome: {} feedback_1: {} solicit_answer_details: false written_translations: translations_mapping: content: {} default_outcome: {} feedback_1: {} END: classifier_model_id: null content: content_id: content html: <p>Congratulations, you have finished!</p> interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null hints: [] id: EndExploration solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} solicit_answer_details: false written_translations: translations_mapping: content: {} New state: classifier_model_id: null content: content_id: content html: '' interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} default_outcome: {} solicit_answer_details: false written_translations: translations_mapping: content: {} default_outcome: {} states_schema_version: 29 tags: [] title: Title """) YAML_CONTENT_V35 = ("""author_notes: '' auto_tts_enabled: true blurb: '' category: Category correctness_feedback_enabled: false init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 35 states: (untitled state): classifier_model_id: null content: content_id: content html: '' interaction: answer_groups: - outcome: dest: END feedback: content_id: feedback_1 html: <p>Correct!</p> labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null rule_specs: - inputs: x: InputString rule_type: Equals tagged_skill_misconception_id: null training_data: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} default_outcome: {} feedback_1: {} solicit_answer_details: false written_translations: translations_mapping: content: {} default_outcome: {} feedback_1: {} END: classifier_model_id: null content: content_id: content html: <p>Congratulations, you have finished!</p> interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null hints: [] id: EndExploration solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} solicit_answer_details: false written_translations: translations_mapping: content: {} New state: classifier_model_id: null content: content_id: content html: '' interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} default_outcome: {} solicit_answer_details: false written_translations: translations_mapping: content: {} default_outcome: {} states_schema_version: 30 tags: [] title: Title """) _LATEST_YAML_CONTENT = YAML_CONTENT_V35 def test_load_from_v1(self): exploration = exp_domain.Exploration.from_untitled_yaml( 'eid', 'Title', 'Category', self.YAML_CONTENT_V1) self.assertEqual(exploration.to_yaml(), self._LATEST_YAML_CONTENT) def test_load_from_v2(self): exploration = exp_domain.Exploration.from_untitled_yaml( 'eid', 'Title', 'Category', self.YAML_CONTENT_V2) self.assertEqual(exploration.to_yaml(), self._LATEST_YAML_CONTENT) def test_load_from_v3(self): exploration = exp_domain.Exploration.from_untitled_yaml( 'eid', 'Title', 'Category', self.YAML_CONTENT_V3) self.assertEqual(exploration.to_yaml(), self._LATEST_YAML_CONTENT) def test_load_from_v4(self): exploration = exp_domain.Exploration.from_untitled_yaml( 'eid', 'Title', 'Category', self.YAML_CONTENT_V4) self.assertEqual(exploration.to_yaml(), self._LATEST_YAML_CONTENT) def test_load_from_v5(self): exploration = exp_domain.Exploration.from_untitled_yaml( 'eid', 'Title', 'Category', self.YAML_CONTENT_V5) self.assertEqual(exploration.to_yaml(), self._LATEST_YAML_CONTENT) def test_load_from_v6(self): exploration = exp_domain.Exploration.from_untitled_yaml( 'eid', 'Title', 'Category', self.YAML_CONTENT_V6) self.assertEqual(exploration.to_yaml(), self._LATEST_YAML_CONTENT) def test_cannot_load_from_v6_with_invalid_handler_name(self): invalid_yaml_content_v6 = ("""author_notes: '' blurb: '' default_skin: conversation_v1 init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 6 skin_customizations: panels_contents: {} states: (untitled state): content: - type: text value: '' interaction: customization_args: placeholder: value: '' rows: value: 1 handlers: - name: submit rule_specs: - definition: inputs: x: InputString name: Equals rule_type: atomic dest: END feedback: - Correct! param_changes: [] - definition: rule_type: default dest: (untitled state) feedback: [] param_changes: [] id: TextInput triggers: [] param_changes: [] END: content: - type: text value: Congratulations, you have finished! interaction: customization_args: recommendedExplorationIds: value: [] handlers: - name: submit rule_specs: - definition: rule_type: default dest: END feedback: [] param_changes: [] id: EndExploration triggers: [] param_changes: [] New state: content: - type: text value: '' interaction: customization_args: placeholder: value: '' rows: value: 1 handlers: - name: invalid_handler_name rule_specs: - definition: rule_type: default dest: END feedback: [] param_changes: [] id: TextInput triggers: [] param_changes: [] states_schema_version: 3 tags: [] """) with self.assertRaisesRegexp( Exception, 'Error: Can only convert rules with a name ' '\'submit\' in states v3 to v4 conversion process. '): exp_domain.Exploration.from_untitled_yaml( 'eid', 'Title', 'Category', invalid_yaml_content_v6) def test_cannot_load_from_v6_with_invalid_rule(self): invalid_yaml_content_v6 = ("""author_notes: '' blurb: '' default_skin: conversation_v1 init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 6 skin_customizations: panels_contents: {} states: (untitled state): content: - type: text value: '' interaction: customization_args: placeholder: value: '' rows: value: 1 handlers: - name: submit rule_specs: - definition: inputs: x: InputString name: Equals rule_type: invalid_rule dest: END feedback: - Correct! param_changes: [] - definition: rule_type: default dest: (untitled state) feedback: [] param_changes: [] id: TextInput triggers: [] param_changes: [] END: content: - type: text value: Congratulations, you have finished! interaction: customization_args: recommendedExplorationIds: value: [] handlers: - name: submit rule_specs: - definition: rule_type: default dest: END feedback: [] param_changes: [] id: EndExploration triggers: [] param_changes: [] New state: content: - type: text value: '' interaction: customization_args: placeholder: value: '' rows: value: 1 handlers: - name: submit rule_specs: - definition: rule_type: default dest: END feedback: [] param_changes: [] id: TextInput triggers: [] param_changes: [] states_schema_version: 3 tags: [] """) with self.assertRaisesRegexp( Exception, 'Error: Can only convert default and atomic ' 'rules in states v3 to v4 conversion process.'): exp_domain.Exploration.from_untitled_yaml( 'eid', 'Title', 'Category', invalid_yaml_content_v6) def test_cannot_load_from_v6_with_invalid_subject(self): invalid_yaml_content_v6 = ("""author_notes: '' blurb: '' default_skin: conversation_v1 init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 6 skin_customizations: panels_contents: {} states: (untitled state): content: - type: text value: '' interaction: customization_args: placeholder: value: '' rows: value: 1 handlers: - name: submit rule_specs: - definition: inputs: x: InputString name: Equals rule_type: atomic dest: END feedback: - Correct! param_changes: [] - definition: rule_type: default dest: (untitled state) feedback: [] param_changes: [] id: TextInput triggers: [] param_changes: [] END: content: - type: text value: Congratulations, you have finished! interaction: customization_args: recommendedExplorationIds: value: [] handlers: - name: submit rule_specs: - definition: rule_type: default dest: END feedback: [] param_changes: [] id: EndExploration triggers: [] param_changes: [] New state: content: - type: text value: '' interaction: customization_args: placeholder: value: '' rows: value: 1 handlers: - name: submit rule_specs: - definition: rule_type: default subject: invalid_subject dest: END feedback: [] param_changes: [] id: TextInput triggers: [] param_changes: [] states_schema_version: 3 tags: [] """) with self.assertRaisesRegexp( Exception, 'Error: Can only convert rules with an \'answer\' ' 'subject in states v3 to v4 conversion process.'): exp_domain.Exploration.from_untitled_yaml( 'eid', 'Title', 'Category', invalid_yaml_content_v6) def test_cannot_load_from_v6_with_invalid_interaction_id(self): invalid_yaml_content_v6 = ("""author_notes: '' blurb: '' default_skin: conversation_v1 init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 6 skin_customizations: panels_contents: {} states: (untitled state): content: - type: text value: '' interaction: customization_args: placeholder: value: '' rows: value: 1 handlers: - name: submit rule_specs: - definition: inputs: x: InputString name: Equals rule_type: atomic dest: END feedback: - Correct! param_changes: [] - definition: rule_type: default dest: (untitled state) feedback: [] param_changes: [] id: TextInput triggers: [] param_changes: [] END: content: - type: text value: Congratulations, you have finished! interaction: customization_args: recommendedExplorationIds: value: [] handlers: - name: submit rule_specs: - definition: rule_type: default dest: END feedback: [] param_changes: [] id: EndExploration triggers: [] param_changes: [] New state: content: - type: text value: '' interaction: customization_args: placeholder: value: '' rows: value: 1 handlers: - name: submit rule_specs: - definition: rule_type: default dest: END feedback: [] param_changes: [] id: invalid_id triggers: [] param_changes: [] states_schema_version: 3 tags: [] """) with self.assertRaisesRegexp( Exception, 'Trying to migrate exploration containing non-existent ' 'interaction ID'): exp_domain.Exploration.from_untitled_yaml( 'eid', 'Title', 'Category', invalid_yaml_content_v6) def test_load_from_v7(self): exploration = exp_domain.Exploration.from_untitled_yaml( 'eid', 'Title', 'Category', self.YAML_CONTENT_V7) self.assertEqual(exploration.to_yaml(), self._LATEST_YAML_CONTENT) def test_load_from_v8(self): exploration = exp_domain.Exploration.from_untitled_yaml( 'eid', 'Title', 'Category', self.YAML_CONTENT_V8) self.assertEqual(exploration.to_yaml(), self._LATEST_YAML_CONTENT) def test_load_from_v9(self): latest_yaml_content = ("""author_notes: '' auto_tts_enabled: true blurb: '' category: Category correctness_feedback_enabled: false init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 35 states: (untitled state): classifier_model_id: null content: content_id: content html: '' interaction: answer_groups: - outcome: dest: END feedback: content_id: feedback_1 html: <p>Correct!</p> labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null rule_specs: - inputs: x: InputString rule_type: Equals tagged_skill_misconception_id: null training_data: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} default_outcome: {} feedback_1: {} solicit_answer_details: false written_translations: translations_mapping: content: {} default_outcome: {} feedback_1: {} END: classifier_model_id: null content: content_id: content html: <p>Congratulations, you have finished!</p> interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null hints: [] id: EndExploration solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} solicit_answer_details: false written_translations: translations_mapping: content: {} New state: classifier_model_id: null content: content_id: content html: '' interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: language: value: python placeholder: value: '' postCode: value: '' preCode: value: '' rows: value: 1 default_outcome: dest: END feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: CodeRepl solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} default_outcome: {} solicit_answer_details: false written_translations: translations_mapping: content: {} default_outcome: {} states_schema_version: 30 tags: [] title: Title """) exploration = exp_domain.Exploration.from_untitled_yaml( 'eid', 'Title', 'Category', self.YAML_CONTENT_V9) self.assertEqual(exploration.to_yaml(), latest_yaml_content) def test_load_from_v10(self): exploration = exp_domain.Exploration.from_yaml( 'eid', self.YAML_CONTENT_V10) self.assertEqual(exploration.to_yaml(), self._LATEST_YAML_CONTENT) def test_load_from_v11(self): exploration = exp_domain.Exploration.from_yaml( 'eid', self.YAML_CONTENT_V11) self.assertEqual(exploration.to_yaml(), self._LATEST_YAML_CONTENT) def test_load_from_v12(self): latest_yaml_content = ("""author_notes: '' auto_tts_enabled: true blurb: '' category: Category correctness_feedback_enabled: false init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 35 states: (untitled state): classifier_model_id: null content: content_id: content html: '' interaction: answer_groups: - outcome: dest: END feedback: content_id: feedback_1 html: <p>Correct!</p> labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null rule_specs: - inputs: x: InputString rule_type: Equals tagged_skill_misconception_id: null training_data: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} default_outcome: {} feedback_1: {} solicit_answer_details: false written_translations: translations_mapping: content: {} default_outcome: {} feedback_1: {} END: classifier_model_id: null content: content_id: content html: <p>Congratulations, you have finished!</p> interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null hints: [] id: EndExploration solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} solicit_answer_details: false written_translations: translations_mapping: content: {} New state: classifier_model_id: null content: content_id: content html: '' interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: - hint_content: content_id: hint_1 html: <p>Correct!</p> id: TextInput solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} default_outcome: {} hint_1: {} solicit_answer_details: false written_translations: translations_mapping: content: {} default_outcome: {} hint_1: {} states_schema_version: 30 tags: [] title: Title """) exploration = exp_domain.Exploration.from_yaml( 'eid', self.YAML_CONTENT_V12) self.assertEqual(exploration.to_yaml(), latest_yaml_content) def test_load_from_v13(self): exploration = exp_domain.Exploration.from_yaml( 'eid', self.YAML_CONTENT_V13) self.assertEqual(exploration.to_yaml(), self._LATEST_YAML_CONTENT) def test_load_from_v14(self): exploration = exp_domain.Exploration.from_yaml( 'eid', self.YAML_CONTENT_V14) self.assertEqual(exploration.to_yaml(), self._LATEST_YAML_CONTENT) def test_load_from_v15(self): exploration = exp_domain.Exploration.from_yaml( 'eid', self.YAML_CONTENT_V15) self.assertEqual(exploration.to_yaml(), self._LATEST_YAML_CONTENT) def test_load_from_v16(self): exploration = exp_domain.Exploration.from_yaml( 'eid', self.YAML_CONTENT_V16) self.assertEqual(exploration.to_yaml(), self._LATEST_YAML_CONTENT) def test_load_from_v17(self): exploration = exp_domain.Exploration.from_yaml( 'eid', self.YAML_CONTENT_V17) self.assertEqual(exploration.to_yaml(), self._LATEST_YAML_CONTENT) def test_load_from_v18(self): latest_yaml_content = ("""author_notes: '' auto_tts_enabled: true blurb: '' category: Category correctness_feedback_enabled: false init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 35 states: (untitled state): classifier_model_id: null content: content_id: content html: '' interaction: answer_groups: - outcome: dest: END feedback: content_id: feedback_1 html: <p>Correct!</p> labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null rule_specs: - inputs: x: InputString rule_type: Equals tagged_skill_misconception_id: null training_data: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} default_outcome: {} feedback_1: {} solicit_answer_details: false written_translations: translations_mapping: content: {} default_outcome: {} feedback_1: {} END: classifier_model_id: null content: content_id: content html: <p>Congratulations, you have finished!</p> interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null hints: [] id: EndExploration solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} solicit_answer_details: false written_translations: translations_mapping: content: {} New state: classifier_model_id: null content: content_id: content html: '' interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: - hint_content: content_id: hint_1 html: '' id: TextInput solution: answer_is_exclusive: false correct_answer: Answer explanation: content_id: solution html: '' param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} default_outcome: {} hint_1: {} solution: {} solicit_answer_details: false written_translations: translations_mapping: content: {} default_outcome: {} hint_1: {} solution: {} states_schema_version: 30 tags: [] title: Title """) exploration = exp_domain.Exploration.from_yaml( 'eid', self.YAML_CONTENT_V18) self.assertEqual(exploration.to_yaml(), latest_yaml_content) def test_load_from_v19(self): exploration = exp_domain.Exploration.from_yaml( 'eid', self.YAML_CONTENT_V19) self.assertEqual(exploration.to_yaml(), self._LATEST_YAML_CONTENT) def test_load_from_v20(self): exploration = exp_domain.Exploration.from_yaml( 'eid', self.YAML_CONTENT_V20) self.assertEqual(exploration.to_yaml(), self._LATEST_YAML_CONTENT) def test_load_from_v21(self): latest_yaml_content = ("""author_notes: '' auto_tts_enabled: true blurb: '' category: Category correctness_feedback_enabled: false init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 35 states: (untitled state): classifier_model_id: null content: content_id: content html: '' interaction: answer_groups: - outcome: dest: END feedback: content_id: feedback_1 html: <p>Correct!</p> labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null rule_specs: - inputs: x: InputString rule_type: Equals tagged_skill_misconception_id: null training_data: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} default_outcome: {} feedback_1: {} solicit_answer_details: false written_translations: translations_mapping: content: {} default_outcome: {} feedback_1: {} END: classifier_model_id: null content: content_id: content html: <p>Congratulations, you have finished!</p> interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null hints: [] id: EndExploration solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} solicit_answer_details: false written_translations: translations_mapping: content: {} New state: classifier_model_id: null content: content_id: content html: '' interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: allowImproperFraction: value: true allowNonzeroIntegerPart: value: true customPlaceholder: value: '' placeholder: value: '' requireSimplestForm: value: false rows: value: 1 default_outcome: dest: END feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: FractionInput solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} default_outcome: {} solicit_answer_details: false written_translations: translations_mapping: content: {} default_outcome: {} states_schema_version: 30 tags: [] title: Title """) exploration = exp_domain.Exploration.from_yaml( 'eid', self.YAML_CONTENT_V21) self.assertEqual(exploration.to_yaml(), latest_yaml_content) def test_load_from_v22(self): exploration = exp_domain.Exploration.from_yaml( 'eid', self.YAML_CONTENT_V22) self.assertEqual(exploration.to_yaml(), self._LATEST_YAML_CONTENT) def test_load_from_v23(self): exploration = exp_domain.Exploration.from_yaml( 'eid', self.YAML_CONTENT_V23) self.assertEqual(exploration.to_yaml(), self._LATEST_YAML_CONTENT) def test_load_from_v24(self): exploration = exp_domain.Exploration.from_yaml( 'eid', self.YAML_CONTENT_V24) self.assertEqual(exploration.to_yaml(), self._LATEST_YAML_CONTENT) def test_load_from_v25(self): exploration = exp_domain.Exploration.from_yaml( 'eid', self.YAML_CONTENT_V25) self.assertEqual(exploration.to_yaml(), self._LATEST_YAML_CONTENT) def test_load_from_v26(self): exploration = exp_domain.Exploration.from_yaml( 'eid', self.YAML_CONTENT_V26) self.assertEqual(exploration.to_yaml(), self._LATEST_YAML_CONTENT) def test_load_from_v27(self): exploration = exp_domain.Exploration.from_yaml( 'eid', self.YAML_CONTENT_V27) self.assertEqual(exploration.to_yaml(), self._LATEST_YAML_CONTENT) def test_load_from_v28(self): exploration = exp_domain.Exploration.from_yaml( 'eid', self.YAML_CONTENT_V28) self.assertEqual(exploration.to_yaml(), self._LATEST_YAML_CONTENT) def test_load_from_v29(self): latest_yaml_content = ("""author_notes: '' auto_tts_enabled: true blurb: '' category: Category correctness_feedback_enabled: false init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 35 states: (untitled state): classifier_model_id: null content: content_id: content html: '' interaction: answer_groups: - outcome: dest: END feedback: content_id: feedback_1 html: <p>Correct!</p> labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null rule_specs: - inputs: x: InputString rule_type: Equals tagged_skill_misconception_id: null training_data: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} default_outcome: {} feedback_1: {} solicit_answer_details: false written_translations: translations_mapping: content: {} default_outcome: {} feedback_1: {} END: classifier_model_id: null content: content_id: content html: <p>Congratulations, you have finished!</p> interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null hints: [] id: EndExploration solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} solicit_answer_details: false written_translations: translations_mapping: content: {} New state: classifier_model_id: null content: content_id: content html: '' interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: highlightRegionsOnHover: value: false imageAndRegions: value: imagePath: s1ImagePath_height_120_width_120.png placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: ImageClickInput solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} default_outcome: {} solicit_answer_details: false written_translations: translations_mapping: content: {} default_outcome: {} states_schema_version: 30 tags: [] title: Title """) exploration = exp_domain.Exploration.from_yaml( 'eid', self.YAML_CONTENT_V29) self.assertEqual(exploration.to_yaml(), latest_yaml_content) def test_load_from_v30(self): exploration = exp_domain.Exploration.from_yaml( 'eid', self.YAML_CONTENT_V30) self.assertEqual(exploration.to_yaml(), self._LATEST_YAML_CONTENT) def test_load_from_v31(self): exploration = exp_domain.Exploration.from_yaml( 'eid', self.YAML_CONTENT_V31) self.assertEqual(exploration.to_yaml(), self._LATEST_YAML_CONTENT) def test_load_from_v32(self): exploration = exp_domain.Exploration.from_yaml( 'eid', self.YAML_CONTENT_V32) self.assertEqual(exploration.to_yaml(), self._LATEST_YAML_CONTENT) def test_load_from_v33(self): exploration = exp_domain.Exploration.from_yaml( 'eid', self.YAML_CONTENT_V33) self.assertEqual(exploration.to_yaml(), self._LATEST_YAML_CONTENT) def test_cannot_load_from_yaml_with_no_schema_version(self): sample_yaml_content = ("""author_notes: '' blurb: '' default_skin: conversation_v1 init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} skin_customizations: panels_contents: {} states: (untitled state): content: - type: text value: '' interaction: customization_args: placeholder: value: '' rows: value: 1 handlers: - name: submit rule_specs: - definition: inputs: x: InputString name: Equals rule_type: atomic dest: END feedback: - Correct! param_changes: [] - definition: rule_type: default dest: (untitled state) feedback: [] param_changes: [] id: TextInput param_changes: [] New state: content: - type: text value: '' interaction: customization_args: placeholder: value: '' rows: value: 1 handlers: - name: submit rule_specs: - definition: rule_type: default dest: END feedback: [] param_changes: [] id: TextInput param_changes: [] widget: customization_args: {} handlers: - name: submit rule_specs: - definition: rule_type: default dest: END feedback: [] param_changes: [] sticky: false widget_id: TextInput END: content: - type: text value: Congratulations, you have finished! interaction: customization_args: recommendedExplorationIds: value: [] handlers: - name: submit rule_specs: - definition: rule_type: default dest: END feedback: [] param_changes: [] id: EndExploration triggers: [] param_changes: [] tags: [] """) with self.assertRaisesRegexp( Exception, 'Invalid YAML file: no schema version specified.'): exp_domain.Exploration.from_untitled_yaml( 'eid', 'Title', 'Category', sample_yaml_content) def test_cannot_load_from_yaml_with_invalid_schema_version(self): sample_yaml_content = ("""author_notes: '' blurb: '' default_skin: conversation_v1 init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 0 skin_customizations: panels_contents: {} states: (untitled state): content: - type: text value: '' interaction: customization_args: placeholder: value: '' rows: value: 1 handlers: - name: submit rule_specs: - definition: inputs: x: InputString name: Equals rule_type: atomic dest: END feedback: - Correct! param_changes: [] - definition: rule_type: default dest: (untitled state) feedback: [] param_changes: [] id: TextInput param_changes: [] New state: content: - type: text value: '' interaction: customization_args: placeholder: value: '' rows: value: 1 handlers: - name: submit rule_specs: - definition: rule_type: default dest: END feedback: [] param_changes: [] id: TextInput param_changes: [] widget: customization_args: {} handlers: - name: submit rule_specs: - definition: rule_type: default dest: END feedback: [] param_changes: [] sticky: false widget_id: TextInput END: content: - type: text value: Congratulations, you have finished! interaction: customization_args: recommendedExplorationIds: value: [] handlers: - name: submit rule_specs: - definition: rule_type: default dest: END feedback: [] param_changes: [] id: EndExploration triggers: [] param_changes: [] tags: [] """) with self.assertRaisesRegexp( Exception, 'Sorry, we can only process v1 to v%s exploration YAML files ' 'at present.' % exp_domain.Exploration.CURRENT_EXP_SCHEMA_VERSION): exp_domain.Exploration.from_untitled_yaml( 'eid', 'Title', 'Category', sample_yaml_content) class HTMLMigrationUnitTests(test_utils.GenericTestBase): YAML_CONTENT_V26_TEXTANGULAR = ("""author_notes: '' auto_tts_enabled: true blurb: '' category: category correctness_feedback_enabled: false init_state_name: Introduction language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 26 states: Introduction: classifier_model_id: null content: content_id: content html: '' content_ids_to_audio_translations: content: {} default_outcome: {} interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: {} default_outcome: dest: Introduction feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: null solution: null param_changes: [] state1: classifier_model_id: null content: content_id: content html: <blockquote><p>Hello, this is state1</p></blockquote> content_ids_to_audio_translations: content: {} default_outcome: {} solution: {} interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: state2 feedback: content_id: default_outcome html: Default <p>outcome</p> for state1 labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: answer_is_exclusive: true correct_answer: Answer1 explanation: content_id: solution html: This is <i>solution</i> for state1 param_changes: [] state2: classifier_model_id: null content: content_id: content html: <p>Hello, </p>this <i>is </i>state2 content_ids_to_audio_translations: content: {} default_outcome: {} feedback_1: {} feedback_2: {} hint_1: {} hint_2: {} interaction: answer_groups: - outcome: dest: state1 feedback: content_id: feedback_1 html: <div>Outcome1 for state2</div> labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null rule_specs: - inputs: x: 0 rule_type: Equals - inputs: x: 1 rule_type: Equals tagged_misconception_id: null training_data: [] - outcome: dest: state3 feedback: content_id: feedback_2 html: <pre>Outcome2 <br>for state2</pre> labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null rule_specs: - inputs: x: 0 rule_type: Equals tagged_misconception_id: null training_data: [] confirmed_unclassified_answers: [] customization_args: choices: value: - <p>This is </p>value1 <br>for MultipleChoice - This is value2<span> for <br>MultipleChoice</span> default_outcome: dest: state2 feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: - hint_content: content_id: hint_1 html: <p>Hello, this is<div> html1<b> for </b></div>state2</p> - hint_content: content_id: hint_2 html: Here is link 2 <oppia-noninteractive-link text-with-value="&amp;quot;discussion forum&amp;quot;" url-with-value="&amp;quot;https://groups.google.com/ forum/?fromgroups#!forum/oppia&amp;quot;"> </oppia-noninteractive-link> id: MultipleChoiceInput solution: null param_changes: [] state3: classifier_model_id: null content: content_id: content html: <p>Hello, this is state3</p> content_ids_to_audio_translations: content: {} default_outcome: {} feedback_1: {} interaction: answer_groups: - outcome: dest: state1 feedback: content_id: feedback_1 html: Here is the image1 <i><oppia-noninteractive-image caption-with-value="&amp;quot;&amp;quot;" filepath-with-value="&amp;quot;startBlue.png&amp;quot;" alt-with-value="&amp;quot;&amp;quot;"> </oppia-noninteractive-image></i>Here is the image2 <div><oppia-noninteractive-image caption-with-value="&amp;quot;&amp;quot;" filepath-with-value="&amp;quot;startBlue.png&amp;quot;" alt-with-value="&amp;quot;&amp;quot;"> </oppia-noninteractive-image></div> labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null rule_specs: - inputs: x: - This <span>is value1 for </span>ItemSelectionInput rule_type: Equals - inputs: x: - This is value3 for ItemSelectionInput rule_type: Equals tagged_misconception_id: null training_data: [] confirmed_unclassified_answers: [] customization_args: choices: value: - This <span>is value1 for </span>ItemSelection - This <code>is value2</code> for ItemSelection - This is value3 for ItemSelection maxAllowableSelectionCount: value: 1 minAllowableSelectionCount: value: 1 default_outcome: dest: state3 feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: ItemSelectionInput solution: null param_changes: [] states_schema_version: 21 tags: [] title: title """) # pylint: disable=line-too-long YAML_CONTENT_V35_IMAGE_DIMENSIONS = ("""author_notes: '' auto_tts_enabled: true blurb: '' category: category correctness_feedback_enabled: false init_state_name: Introduction language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 35 states: Introduction: classifier_model_id: null content: content_id: content html: '' interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: {} default_outcome: dest: Introduction feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: null solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} default_outcome: {} solicit_answer_details: false written_translations: translations_mapping: content: {} default_outcome: {} state1: classifier_model_id: null content: content_id: content html: <blockquote><p>Hello, this is state1</p></blockquote> interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: state2 feedback: content_id: default_outcome html: <p>Default </p><p>outcome</p><p> for state1</p> labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: answer_is_exclusive: true correct_answer: Answer1 explanation: content_id: solution html: <p>This is <em>solution</em> for state1</p> param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} default_outcome: {} solution: {} solicit_answer_details: false written_translations: translations_mapping: content: {} default_outcome: {} solution: {} state2: classifier_model_id: null content: content_id: content html: <p>Hello, </p><p>this <em>is </em>state2</p> interaction: answer_groups: - outcome: dest: state1 feedback: content_id: feedback_1 html: <p>Outcome1 for state2</p> labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null rule_specs: - inputs: x: 0 rule_type: Equals - inputs: x: 1 rule_type: Equals tagged_skill_misconception_id: null training_data: [] - outcome: dest: state3 feedback: content_id: feedback_2 html: "<pre>Outcome2 \\nfor state2</pre>" labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null rule_specs: - inputs: x: 0 rule_type: Equals tagged_skill_misconception_id: null training_data: [] confirmed_unclassified_answers: [] customization_args: choices: value: - <p>This is </p><p>value1 <br>for MultipleChoice</p> - <p>This is value2 for <br>MultipleChoice</p> default_outcome: dest: state2 feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: - hint_content: content_id: hint_1 html: <p>Hello, this is</p><p> html1<strong> for </strong></p><p>state2</p> - hint_content: content_id: hint_2 html: <p>Here is link 2 <oppia-noninteractive-link text-with-value="&amp;quot;discussion forum&amp;quot;" url-with-value="&amp;quot;https://groups.google.com/ forum/?fromgroups#!forum/oppia&amp;quot;"> </oppia-noninteractive-link></p> id: MultipleChoiceInput solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} default_outcome: {} feedback_1: {} feedback_2: {} hint_1: {} hint_2: {} solicit_answer_details: false written_translations: translations_mapping: content: {} default_outcome: {} feedback_1: {} feedback_2: {} hint_1: {} hint_2: {} state3: classifier_model_id: null content: content_id: content html: <p>Hello, this is state3</p> interaction: answer_groups: - outcome: dest: state1 feedback: content_id: feedback_1 html: <p>Here is the image1 </p><oppia-noninteractive-image alt-with-value="&amp;quot;&amp;quot;" caption-with-value="&amp;quot;&amp;quot;" filepath-with-value="&amp;quot;startBlue_height_490_width_120.png&amp;quot;"> </oppia-noninteractive-image><p>Here is the image2 </p><oppia-noninteractive-image alt-with-value="&amp;quot;&amp;quot;" caption-with-value="&amp;quot;&amp;quot;" filepath-with-value="&amp;quot;startBlue_height_490_width_120.png&amp;quot;"> </oppia-noninteractive-image> labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null rule_specs: - inputs: x: - <p>This is value1 for ItemSelectionInput</p> rule_type: Equals - inputs: x: - <p>This is value3 for ItemSelectionInput</p> rule_type: Equals tagged_skill_misconception_id: null training_data: [] confirmed_unclassified_answers: [] customization_args: choices: value: - <p>This is value1 for ItemSelection</p> - <p>This is value2 for ItemSelection</p> - <p>This is value3 for ItemSelection</p> maxAllowableSelectionCount: value: 1 minAllowableSelectionCount: value: 1 default_outcome: dest: state3 feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: ItemSelectionInput solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} default_outcome: {} feedback_1: {} solicit_answer_details: false written_translations: translations_mapping: content: {} default_outcome: {} feedback_1: {} states_schema_version: 30 tags: [] title: title """) YAML_CONTENT_V27_WITHOUT_IMAGE_CAPTION = ("""author_notes: '' auto_tts_enabled: true blurb: '' category: Category correctness_feedback_enabled: false init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 27 states: (untitled state): classifier_model_id: null content: content_id: content html: <p><oppia-noninteractive-image filepath-with-value="&amp;quot;random.png&amp;quot;"></oppia-noninteractive-image>Hello this is test case to check image tag inside p tag</p> content_ids_to_audio_translations: content: {} default_outcome: {} feedback_1: {} interaction: answer_groups: - outcome: dest: END feedback: content_id: feedback_1 html: <p>Correct!</p> labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null rule_specs: - inputs: x: InputString rule_type: Equals tagged_misconception_id: null training_data: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] END: classifier_model_id: null content: content_id: content html: <p>Congratulations, you have finished!</p> content_ids_to_audio_translations: content: {} interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null hints: [] id: EndExploration solution: null param_changes: [] New state: classifier_model_id: null content: content_id: content html: '' content_ids_to_audio_translations: content: {} default_outcome: {} interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] states_schema_version: 22 tags: [] title: Title """) YAML_CONTENT_V35_WITH_IMAGE_CAPTION = ("""author_notes: '' auto_tts_enabled: true blurb: '' category: Category correctness_feedback_enabled: false init_state_name: (untitled state) language_code: en objective: '' param_changes: [] param_specs: {} schema_version: 35 states: (untitled state): classifier_model_id: null content: content_id: content html: <oppia-noninteractive-image caption-with-value="&amp;quot;&amp;quot;" filepath-with-value="&amp;quot;random_height_490_width_120.png&amp;quot;"></oppia-noninteractive-image><p>Hello this is test case to check image tag inside p tag</p> interaction: answer_groups: - outcome: dest: END feedback: content_id: feedback_1 html: <p>Correct!</p> labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null rule_specs: - inputs: x: InputString rule_type: Equals tagged_skill_misconception_id: null training_data: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: (untitled state) feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} default_outcome: {} feedback_1: {} solicit_answer_details: false written_translations: translations_mapping: content: {} default_outcome: {} feedback_1: {} END: classifier_model_id: null content: content_id: content html: <p>Congratulations, you have finished!</p> interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: recommendedExplorationIds: value: [] default_outcome: null hints: [] id: EndExploration solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} solicit_answer_details: false written_translations: translations_mapping: content: {} New state: classifier_model_id: null content: content_id: content html: '' interaction: answer_groups: [] confirmed_unclassified_answers: [] customization_args: placeholder: value: '' rows: value: 1 default_outcome: dest: END feedback: content_id: default_outcome html: '' labelled_as_correct: false missing_prerequisite_skill_id: null param_changes: [] refresher_exploration_id: null hints: [] id: TextInput solution: null param_changes: [] recorded_voiceovers: voiceovers_mapping: content: {} default_outcome: {} solicit_answer_details: false written_translations: translations_mapping: content: {} default_outcome: {} states_schema_version: 30 tags: [] title: Title """) # pylint: enable=line-too-long def test_load_from_v26_textangular(self): mock_get_filename_with_dimensions_context = self.swap( html_validation_service, 'get_filename_with_dimensions', mock_get_filename_with_dimensions) with mock_get_filename_with_dimensions_context: exploration = exp_domain.Exploration.from_yaml( 'eid', self.YAML_CONTENT_V26_TEXTANGULAR) self.assertEqual( exploration.to_yaml(), self.YAML_CONTENT_V35_IMAGE_DIMENSIONS) def test_load_from_v27_without_image_caption(self): mock_get_filename_with_dimensions_context = self.swap( html_validation_service, 'get_filename_with_dimensions', mock_get_filename_with_dimensions) with mock_get_filename_with_dimensions_context: exploration = exp_domain.Exploration.from_yaml( 'eid', self.YAML_CONTENT_V27_WITHOUT_IMAGE_CAPTION) self.assertEqual( exploration.to_yaml(), self.YAML_CONTENT_V35_WITH_IMAGE_CAPTION) class ConversionUnitTests(test_utils.GenericTestBase): def test_convert_exploration_to_player_dict(self): exp_title = 'Title' second_state_name = 'first state' exploration = exp_domain.Exploration.create_default_exploration( 'eid', title=exp_title, category='Category') exploration.add_states([second_state_name]) def _get_default_state_dict(content_str, dest_name): return { 'classifier_model_id': None, 'content': { 'content_id': 'content', 'html': content_str, }, 'recorded_voiceovers': { 'voiceovers_mapping': { 'content': {}, 'default_outcome': {} } }, 'solicit_answer_details': False, 'written_translations': { 'translations_mapping': { 'content': {}, 'default_outcome': {} } }, 'interaction': { 'answer_groups': [], 'confirmed_unclassified_answers': [], 'customization_args': {}, 'default_outcome': { 'dest': dest_name, 'feedback': { 'content_id': feconf.DEFAULT_OUTCOME_CONTENT_ID, 'html': '' }, 'labelled_as_correct': False, 'param_changes': [], 'refresher_exploration_id': None, 'missing_prerequisite_skill_id': None }, 'hints': [], 'id': None, 'solution': None, }, 'param_changes': [], } self.assertEqual(exploration.to_player_dict(), { 'init_state_name': feconf.DEFAULT_INIT_STATE_NAME, 'title': exp_title, 'objective': feconf.DEFAULT_EXPLORATION_OBJECTIVE, 'states': { feconf.DEFAULT_INIT_STATE_NAME: _get_default_state_dict( feconf.DEFAULT_INIT_STATE_CONTENT_STR, feconf.DEFAULT_INIT_STATE_NAME), second_state_name: _get_default_state_dict( '', second_state_name), }, 'param_changes': [], 'param_specs': {}, 'language_code': 'en', 'correctness_feedback_enabled': False, }) class StateOperationsUnitTests(test_utils.GenericTestBase): def test_delete_state(self): exploration = exp_domain.Exploration.create_default_exploration('eid') exploration.add_states(['first state']) with self.assertRaisesRegexp( ValueError, 'Cannot delete initial state' ): exploration.delete_state(exploration.init_state_name) exploration.add_states(['second state']) exploration.delete_state('second state') with self.assertRaisesRegexp(ValueError, 'fake state does not exist'): exploration.delete_state('fake state') class HtmlCollectionTests(test_utils.GenericTestBase): def test_all_html_strings_are_collected(self): exploration = exp_domain.Exploration.create_default_exploration( 'eid', title='title', category='category') exploration.add_states(['state1', 'state2', 'state3', 'state4']) state1 = exploration.states['state1'] state2 = exploration.states['state2'] state3 = exploration.states['state3'] state4 = exploration.states['state4'] content1_dict = { 'content_id': 'content', 'html': '<blockquote>Hello, this is state1</blockquote>' } content2_dict = { 'content_id': 'content', 'html': '<pre>Hello, this is state2</pre>' } content3_dict = { 'content_id': 'content', 'html': '<p>Hello, this is state3</p>' } content4_dict = { 'content_id': 'content', 'html': '<p>Hello, this is state4</p>' } state1.update_content( state_domain.SubtitledHtml.from_dict(content1_dict)) state2.update_content( state_domain.SubtitledHtml.from_dict(content2_dict)) state3.update_content( state_domain.SubtitledHtml.from_dict(content3_dict)) state4.update_content( state_domain.SubtitledHtml.from_dict(content4_dict)) state1.update_interaction_id('TextInput') state2.update_interaction_id('MultipleChoiceInput') state3.update_interaction_id('ItemSelectionInput') state4.update_interaction_id('DragAndDropSortInput') customization_args_dict1 = { 'placeholder': {'value': ''}, 'rows': {'value': 1} } customization_args_dict2 = { 'choices': {'value': [ '<p>This is value1 for MultipleChoice</p>', '<p>This is value2 for MultipleChoice</p>' ]} } customization_args_dict3 = { 'choices': {'value': [ '<p>This is value1 for ItemSelection</p>', '<p>This is value2 for ItemSelection</p>', '<p>This is value3 for ItemSelection</p>' ]} } customization_args_dict4 = { 'choices': {'value': [ '<p>This is value1 for DragAndDropSortInput</p>', '<p>This is value2 for DragAndDropSortInput</p>', ]} } state1.update_interaction_customization_args(customization_args_dict1) state2.update_interaction_customization_args(customization_args_dict2) state3.update_interaction_customization_args(customization_args_dict3) state4.update_interaction_customization_args(customization_args_dict4) default_outcome_dict1 = { 'dest': 'state2', 'feedback': { 'content_id': 'default_outcome', 'html': '<p>Default outcome for state1</p>' }, 'param_changes': [], 'labelled_as_correct': False, 'refresher_exploration_id': None, 'missing_prerequisite_skill_id': None } state1.update_interaction_default_outcome(default_outcome_dict1) hint_list2 = [{ 'hint_content': { 'content_id': 'hint_1', 'html': '<p>Hello, this is html1 for state2</p>' } }, { 'hint_content': { 'content_id': 'hint_2', 'html': '<p>Hello, this is html2 for state2</p>' } }] state2.update_interaction_hints(hint_list2) solution_dict1 = { 'interaction_id': '', 'answer_is_exclusive': True, 'correct_answer': 'Answer1', 'explanation': { 'content_id': 'solution', 'html': '<p>This is solution for state1</p>' } } state1.update_interaction_solution(solution_dict1) answer_group_list2 = [{ 'rule_specs': [{ 'rule_type': 'Equals', 'inputs': {'x': 0} }, { 'rule_type': 'Equals', 'inputs': {'x': 1} }], 'outcome': { 'dest': 'state1', 'feedback': { 'content_id': 'feedback_1', 'html': '<p>Outcome1 for state2</p>' }, 'param_changes': [], 'labelled_as_correct': False, 'refresher_exploration_id': None, 'missing_prerequisite_skill_id': None }, 'training_data': [], 'tagged_skill_misconception_id': None }, { 'rule_specs': [{ 'rule_type': 'Equals', 'inputs': {'x': 0} }], 'outcome': { 'dest': 'state3', 'feedback': { 'content_id': 'feedback_2', 'html': '<p>Outcome2 for state2</p>' }, 'param_changes': [], 'labelled_as_correct': False, 'refresher_exploration_id': None, 'missing_prerequisite_skill_id': None }, 'training_data': [], 'tagged_skill_misconception_id': None }] answer_group_list3 = [{ 'rule_specs': [{ 'rule_type': 'Equals', 'inputs': {'x': [ '<p>This is value1 for ItemSelectionInput</p>' ]} }, { 'rule_type': 'Equals', 'inputs': {'x': [ '<p>This is value3 for ItemSelectionInput</p>' ]} }], 'outcome': { 'dest': 'state1', 'feedback': { 'content_id': 'feedback_1', 'html': '<p>Outcome for state3</p>' }, 'param_changes': [], 'labelled_as_correct': False, 'refresher_exploration_id': None, 'missing_prerequisite_skill_id': None }, 'training_data': [], 'tagged_skill_misconception_id': None }] state2.update_interaction_answer_groups(answer_group_list2) state3.update_interaction_answer_groups(answer_group_list3) expected_html_list = [ '', '', '<pre>Hello, this is state2</pre>', '<p>Outcome1 for state2</p>', '<p>Outcome2 for state2</p>', '', '<p>Hello, this is html1 for state2</p>', '<p>Hello, this is html2 for state2</p>', '<p>This is value1 for MultipleChoice</p>', '<p>This is value2 for MultipleChoice</p>', '<blockquote>Hello, this is state1</blockquote>', '<p>Default outcome for state1</p>', '<p>This is solution for state1</p>', '<p>Hello, this is state3</p>', '<p>Outcome for state3</p>', '<p>This is value1 for ItemSelectionInput</p>', '<p>This is value3 for ItemSelectionInput</p>', '', '<p>This is value1 for ItemSelection</p>', '<p>This is value2 for ItemSelection</p>', '<p>This is value3 for ItemSelection</p>', '<p>Hello, this is state4</p>', '', '<p>This is value1 for DragAndDropSortInput</p>', '<p>This is value2 for DragAndDropSortInput</p>' ] actual_outcome_list = exploration.get_all_html_content_strings() self.assertEqual(actual_outcome_list, expected_html_list)
true
true
f705f423ca61244d0ab1343bbd0dea58537140db
2,397
py
Python
src/tempgen/tests/test_tempgen.py
k5md/Templated-Generator
f3cd2bc6c7de6f68fa1e5835471e8eaa9163d530
[ "MIT" ]
null
null
null
src/tempgen/tests/test_tempgen.py
k5md/Templated-Generator
f3cd2bc6c7de6f68fa1e5835471e8eaa9163d530
[ "MIT" ]
null
null
null
src/tempgen/tests/test_tempgen.py
k5md/Templated-Generator
f3cd2bc6c7de6f68fa1e5835471e8eaa9163d530
[ "MIT" ]
null
null
null
import pytest import os import os from collections import defaultdict from syrupy.extensions.single_file import SingleFileSnapshotExtension from tempgen.module import Tempgen from tempgen.parsers import Parsers from tempgen.transforms import Transforms from tempgen.tests.helpers import ext_serializer_map tests_dir = os.path.dirname(os.path.abspath(__file__)) fixture_dir = os.path.join(tests_dir, 'fixtures') fixture_name = 'test_template' generated_name = 'generated' transforms = Transforms().name_transform_map.keys() extensions = Parsers().ext_parser_map.keys() serializers = ext_serializer_map @pytest.fixture(autouse=True) def tempgen_instance(): return Tempgen() @pytest.fixture def tempgen_instances(request): return [Tempgen() for _ in range(request.param)] @pytest.mark.parametrize('extension', extensions) def test_load_template(extension, tempgen_instance, snapshot): template = os.path.join(fixture_dir, fixture_name + extension) tempgen_instance.load_template(template) assert template in tempgen_instance.get_templates() assert tempgen_instance.get_fields() == snapshot @pytest.mark.parametrize('extension', extensions) def test_save_result(extension, tempgen_instance, snapshot): template = os.path.join(fixture_dir, fixture_name + extension) tempgen_instance.load_template(template) replacements = { key: value['value'] for key, value in tempgen_instance.get_fields().items() } replacements['doer'] = 'Петров П.П.' replacements['itn'] = '987654321098' save_path = os.path.join(fixture_dir, generated_name) tempgen_instance.save_result(template, save_path, replacements) assert ext_serializer_map[extension](save_path + extension) == snapshot os.remove(save_path + extension) @pytest.mark.parametrize('extension', extensions) @pytest.mark.parametrize('transform', transforms) @pytest.mark.parametrize('tempgen_instances', [2], indirect=['tempgen_instances']) def test_independence(extension, transform, tempgen_instances): instance_0, instance_1 = tempgen_instances assert instance_0.parsers != instance_1.parsers assert instance_0.transforms != instance_1.transforms instance_0.parsers[extension].parse = lambda *args, **kwargs: ({}) instance_0.transforms[transform] = lambda x: x assert instance_0.parsers != instance_1.parsers assert instance_0.transforms != instance_1.transforms
41.327586
98
0.783479
import pytest import os import os from collections import defaultdict from syrupy.extensions.single_file import SingleFileSnapshotExtension from tempgen.module import Tempgen from tempgen.parsers import Parsers from tempgen.transforms import Transforms from tempgen.tests.helpers import ext_serializer_map tests_dir = os.path.dirname(os.path.abspath(__file__)) fixture_dir = os.path.join(tests_dir, 'fixtures') fixture_name = 'test_template' generated_name = 'generated' transforms = Transforms().name_transform_map.keys() extensions = Parsers().ext_parser_map.keys() serializers = ext_serializer_map @pytest.fixture(autouse=True) def tempgen_instance(): return Tempgen() @pytest.fixture def tempgen_instances(request): return [Tempgen() for _ in range(request.param)] @pytest.mark.parametrize('extension', extensions) def test_load_template(extension, tempgen_instance, snapshot): template = os.path.join(fixture_dir, fixture_name + extension) tempgen_instance.load_template(template) assert template in tempgen_instance.get_templates() assert tempgen_instance.get_fields() == snapshot @pytest.mark.parametrize('extension', extensions) def test_save_result(extension, tempgen_instance, snapshot): template = os.path.join(fixture_dir, fixture_name + extension) tempgen_instance.load_template(template) replacements = { key: value['value'] for key, value in tempgen_instance.get_fields().items() } replacements['doer'] = 'Петров П.П.' replacements['itn'] = '987654321098' save_path = os.path.join(fixture_dir, generated_name) tempgen_instance.save_result(template, save_path, replacements) assert ext_serializer_map[extension](save_path + extension) == snapshot os.remove(save_path + extension) @pytest.mark.parametrize('extension', extensions) @pytest.mark.parametrize('transform', transforms) @pytest.mark.parametrize('tempgen_instances', [2], indirect=['tempgen_instances']) def test_independence(extension, transform, tempgen_instances): instance_0, instance_1 = tempgen_instances assert instance_0.parsers != instance_1.parsers assert instance_0.transforms != instance_1.transforms instance_0.parsers[extension].parse = lambda *args, **kwargs: ({}) instance_0.transforms[transform] = lambda x: x assert instance_0.parsers != instance_1.parsers assert instance_0.transforms != instance_1.transforms
true
true
f705f497e8cf2e3770451fa8033e79140ae27952
1,635
py
Python
0x06-python-classes/6-square.py
ricardo1470/holbertonschool-higher_level_programming
aab73c8efee665b0215958ee7b338871f13634bc
[ "CNRI-Python" ]
null
null
null
0x06-python-classes/6-square.py
ricardo1470/holbertonschool-higher_level_programming
aab73c8efee665b0215958ee7b338871f13634bc
[ "CNRI-Python" ]
null
null
null
0x06-python-classes/6-square.py
ricardo1470/holbertonschool-higher_level_programming
aab73c8efee665b0215958ee7b338871f13634bc
[ "CNRI-Python" ]
null
null
null
#!/usr/bin/python3 """Create class""" class Square: """Square class""" def __init__(self, size=0, position=(0, 0)): """Initialize Square""" self.__size = size self.position = position """if type(size) is not int: raise TypeError("size must be an integer") if size < 0: raise ValueError("size must be >= 0")""" def area(self, area=0): """defines area""" return(self.__size * self.__size) @property def size(self): """ define size""" return self.__size @size.setter def size(self, value): """Define area""" if type(value) is not int: raise TypeError("size must be an integer") if value < 0: raise ValueError("size must be >= 0") self.__size = value def my_print(self): """print Square""" if self.__size: for i in range(self.__position[1]): print() for j in range(self.__size): print('{}{}'.format(' ' * self.position[0], '#' * self.__size)) else: print() @property def position(self): """ position""" return self.__position @position.setter def position(self, value): if (not isinstance(value, tuple) or len(value) != 2 or not isinstance(value[0], int) or not isinstance(value[1], int) or value[0] < 0 or value[1] < 0): raise TypeError("position must be a tuple of 2 positive integers") self.__position = value
26.803279
79
0.511927
class Square: def __init__(self, size=0, position=(0, 0)): self.__size = size self.position = position def area(self, area=0): return(self.__size * self.__size) @property def size(self): return self.__size @size.setter def size(self, value): if type(value) is not int: raise TypeError("size must be an integer") if value < 0: raise ValueError("size must be >= 0") self.__size = value def my_print(self): if self.__size: for i in range(self.__position[1]): print() for j in range(self.__size): print('{}{}'.format(' ' * self.position[0], '#' * self.__size)) else: print() @property def position(self): return self.__position @position.setter def position(self, value): if (not isinstance(value, tuple) or len(value) != 2 or not isinstance(value[0], int) or not isinstance(value[1], int) or value[0] < 0 or value[1] < 0): raise TypeError("position must be a tuple of 2 positive integers") self.__position = value
true
true
f705f4a2e63a2fa8ac258a2512c5917660abddae
8,792
py
Python
f2_testbench/analyzers_mixin.py
TheSDK-blocks/f2_testbench
6a263dbab6de1da980619c0ecf6d181342c7592b
[ "Apache-2.0", "BSD-3-Clause" ]
null
null
null
f2_testbench/analyzers_mixin.py
TheSDK-blocks/f2_testbench
6a263dbab6de1da980619c0ecf6d181342c7592b
[ "Apache-2.0", "BSD-3-Clause" ]
null
null
null
f2_testbench/analyzers_mixin.py
TheSDK-blocks/f2_testbench
6a263dbab6de1da980619c0ecf6d181342c7592b
[ "Apache-2.0", "BSD-3-Clause" ]
null
null
null
#This is a mixin class for signal analyzers to be used by f2_system class #Todo: Analyzers should be a independent class #Last modification by Marko Kosunen, marko.kosunen@aalto.fi, 30.07.2018 18:09 import numpy as np import scipy.signal as sig import matplotlib as mpl mpl.use('Agg') #To enble plotting without X import matplotlib.pyplot as plt class analyzers_mixin: #Define signal analyzer methods def oscilloscope(self,argdict): ymax=argdict['ymax'] ymin=argdict['ymin'] timex=argdict['timex'] sigin=argdict['sigin'] tstr=argdict['tstr'] printstr=argdict['printstr'] msg="Generating %s" %(printstr) self.print_log(type='I', msg=msg) figure=plt.figure() h=plt.subplot(); hfont = {'fontname':'Sans'} plt.plot(timex, sigin, linewidth=2) plt.ylim((ymin, ymax)); plt.xlim((np.amin(timex), np.amax(timex))); #plt.plot(self.x,self.a,label='Blowing in the wind',linewidth=2); #plt.plot(self.x,self.b,label='Blowing in the wind',linewidth=2); tstr=argdict['tstr'] plt.suptitle(tstr,fontsize=20); plt.ylabel('Out', **hfont,fontsize=18); plt.xlabel('Sample (n)', **hfont,fontsize=18); h.tick_params(labelsize=14) plt.grid(True); printstr=argdict['printstr'] figure.savefig(printstr, format='eps', dpi=300); plt.close("all") def constellation(self,argdict): ymax=argdict['ymax'] ymin=argdict['ymin'] I=argdict['I'] Q=argdict['Q'] tstr=argdict['tstr'] printstr=argdict['printstr'] msg="Generating %s" %(printstr) self.print_log(type='I', msg=msg) figure=plt.figure() h=plt.subplot(); hfont = {'fontname':'Sans'} plt.plot(I, Q, linestyle='None', marker='x') plt.ylim((ymin, ymax)); plt.ylim((1.1*np.amin(Q), 1.1*np.amax(Q))); plt.xlim((1.1*np.amin(I), 1.1*np.amax(I))); #plt.plot(self.x,self.a,label='Blowing in the wind',linewidth=2); #plt.plot(self.x,self.b,label='Blowing in the wind',linewidth=2); tstr=argdict['tstr'] plt.suptitle(tstr,fontsize=20); plt.ylabel('Q', **hfont,fontsize=18); plt.xlabel('I', **hfont,fontsize=18); h.tick_params(labelsize=14) plt.grid(True); printstr=argdict['printstr'] figure.savefig(printstr, format='eps', dpi=300); plt.close("all") def spectrum_analyzer(self, **kwargs): #Example argdict #argdict={'sig':self.signal_gen._Z.Data[i,:,0],'ymax':3, 'ymin':spectrumfloorideal,'nperseg':1024, # 'tstr' : "Tx, User:%i" %(i),'printstr':"%s/F2_system_Tx_antennas_Spectrum_Rs_%i_k:%i.eps" %(self.picpath, self.Rs, i)} ymax=kwargs.get('ymax',3) ymin=kwargs.get('ymin',-80) nperseg=kwargs.get('nperseg',1024) #Samples for the Welch periodogram seqment fs=kwargs.get('Rs',self.Rs) freqx=np.arange(nperseg)/nperseg*fs/1e6 freqx.shape=(-1,1) sigin=kwargs['sigin'] sigin.shape=(-1,1) tstr=kwargs['tstr'] printstr=kwargs['printstr'] msg="Generating %s" %(printstr) self.print_log(type='I', msg=msg) figure=plt.figure() h=plt.subplot(); hfont = {'fontname':'Sans'} fs, spe=sig.welch(sigin,fs=self.Rs,nperseg=nperseg,return_onesided=False,scaling='spectrum',axis=0) spelog=10*np.log10(np.abs(spe)/np.amax(np.abs(spe))) plt.plot(freqx,spelog, linewidth=2 ) #plt.setp(markerline,'markerfacecolor', 'b','linewidth',2) #plt.setp(stemlines, 'linestyle','solid','color','b', 'linewidth', 2) #plt.ylim((np.amin([self.a,self.b]), np.amax([self.a,self.b]))); plt.ylim((ymin, ymax)); plt.xlim((np.amin(freqx), np.amax(freqx))); #plt.plot(self.x,self.a,label='Blowing in the wind',linewidth=2); #plt.plot(self.x,self.b,label='Blowing in the wind',linewidth=2); plt.suptitle(tstr,fontsize=20); plt.ylabel('Normalized Spectrum', **hfont,fontsize=18); plt.xlabel('Frequency (MHz)', **hfont,fontsize=18); h.tick_params(labelsize=14) #for axis in ['top','bottom','left','right']: #h.spines[axis].set_linewidth(2) #lgd=plt.legend(loc='upper right', fontsize=14); ##lgd.set_fontsize(12); plt.grid(True); figure.savefig(printstr, format='eps', dpi=300); plt.close("all") def logic_analyzer(self,argdict): ymax=argdict['ymax'] ymin=argdict['ymin'] timex=argdict['timex'] sigin=argdict['sigin'] tstr = argdict['tstr'] printstr=argdict['printstr'] msg="Generating %s" %(printstr) self.print_log(type='I', msg=msg) figure=plt.figure() h=plt.subplot(); hfont = {'fontname':'Sans'} markerline, stemlines, baseline = plt.stem(timex, sigin, '-.') plt.setp(markerline,'markerfacecolor', 'b','linewidth',2) plt.setp(stemlines, 'linestyle','solid','color','b', 'linewidth', 2) plt.ylim((ymin, ymax)); plt.xlim((np.amin(timex), np.amax(timex))); plt.suptitle(tstr,fontsize=20); plt.ylabel('Out', **hfont,fontsize=18); plt.xlabel('Sample (n)', **hfont,fontsize=18); h.tick_params(labelsize=14) plt.grid(True); figure.savefig(printstr, format='eps', dpi=300); def evm_calculator(self,argdict): reference=argdict['ref'] received=argdict['signal'] #Shape the vectors: time is row observation is colum #if received.shape[0]<received.shape[1]: # received=np.transpose(received) reference.shape=(-1,1) received.shape=(-1,1) #RMS for Scaling rmsref=np.std(reference) rmsreceived=np.std(received) EVM=10*np.log10(np.mean(np.mean(np.abs(received/rmsreceived*rmsref-reference)**2,axis=0)/np.mean(np.abs(reference)**2,axis=0))) self.print_log(type='I', msg="Estimated EVM is %0.2f dB" %(EVM)) return EVM def ber_calculator(self,argdict): reference=argdict['ref'] received=argdict['signal'] #Shape the vectors: time is row observation is colum #if received.shape[0]<received.shape[1]: # received=np.transpose(received) #reference.shape=received.shape reference.shape=(-1,1) received.shape=(-1,1) #Discard samples rounded away in transmission #if received.shape[1] < reference.shape[1]: # reference=reference[:,0:received.shape[1]] errors=np.sum(np.sum(np.abs(received-reference),axis=0))/(received.shape[0]*received.shape[1]) errors=np.sum(np.sum(np.abs(received-reference),axis=0)) bits=(received.shape[0]*received.shape[1]) self.print_log(type='I', msg="Received %i errors in %i bits" %(int(errors), int(bits))) BER=errors/bits self.print_log(type='I', msg="Resulting BER is %0.3g" %(BER)) return BER #From Kosta. def plot_generic(x, y_list, title_str, legend_list, xlabel_str, ylabel_str, xscale, yscale, plot_style_str='o-', xlim=[], ylim=[]): if (xscale, yscale) == ('linear', 'linear'): plot_type_str = 'plot' elif (xscale, yscale) == ('log', 'linear'): plot_type_str = 'semilogx' elif (xscale, yscale) == ('linear', 'log'): plot_type_str = 'semilogy' elif (xscale, yscale) == ('log', 'log'): plot_type_str = 'loglog' else: raise Exception('xscale = %s, yscale = %s, both should be linear or log!!' % (xscale, yscale)) fig, ax = plt.subplots() # default is 1,1,1 if (isinstance(x[0], list)) and (len(x) == len(y_list)): # several plots with different x values for x, y in zip(x, y_list): exec('ax.' + plot_type_str + '(x, y, plot_style_str, linewidth=linewidth)') else: if (isinstance(y_list[0], list)): # several plots with the same x values for y in y_list: exec('ax.' + plot_type_str + '(x, y, plot_style_str, linewidth=linewidth)') else: # single plot only exec('ax.' + plot_type_str + '(x, y_list, plot_style_str, linewidth=linewidth)') if xlim != []: plt.xlim(xlim) if ylim != []: plt.ylim(ylim) ax.set_xlabel(xlabel_str, fontsize=fontsize) plt.ylabel(ylabel_str, fontsize=fontsize) if title_str == []: loc_y = 1.05 else: plt.title(title_str, fontsize=fontsize) loc_y = 1 if legend_list != []: plt.legend(legend_list, loc=(0, loc_y)) plt.grid(True, which='both') ax.tick_params(axis='both', which='major', labelsize=fontsize) plt.show()
40.330275
135
0.59702
import numpy as np import scipy.signal as sig import matplotlib as mpl mpl.use('Agg') import matplotlib.pyplot as plt class analyzers_mixin: def oscilloscope(self,argdict): ymax=argdict['ymax'] ymin=argdict['ymin'] timex=argdict['timex'] sigin=argdict['sigin'] tstr=argdict['tstr'] printstr=argdict['printstr'] msg="Generating %s" %(printstr) self.print_log(type='I', msg=msg) figure=plt.figure() h=plt.subplot(); hfont = {'fontname':'Sans'} plt.plot(timex, sigin, linewidth=2) plt.ylim((ymin, ymax)); plt.xlim((np.amin(timex), np.amax(timex))); tstr=argdict['tstr'] plt.suptitle(tstr,fontsize=20); plt.ylabel('Out', **hfont,fontsize=18); plt.xlabel('Sample (n)', **hfont,fontsize=18); h.tick_params(labelsize=14) plt.grid(True); printstr=argdict['printstr'] figure.savefig(printstr, format='eps', dpi=300); plt.close("all") def constellation(self,argdict): ymax=argdict['ymax'] ymin=argdict['ymin'] I=argdict['I'] Q=argdict['Q'] tstr=argdict['tstr'] printstr=argdict['printstr'] msg="Generating %s" %(printstr) self.print_log(type='I', msg=msg) figure=plt.figure() h=plt.subplot(); hfont = {'fontname':'Sans'} plt.plot(I, Q, linestyle='None', marker='x') plt.ylim((ymin, ymax)); plt.ylim((1.1*np.amin(Q), 1.1*np.amax(Q))); plt.xlim((1.1*np.amin(I), 1.1*np.amax(I))); tstr=argdict['tstr'] plt.suptitle(tstr,fontsize=20); plt.ylabel('Q', **hfont,fontsize=18); plt.xlabel('I', **hfont,fontsize=18); h.tick_params(labelsize=14) plt.grid(True); printstr=argdict['printstr'] figure.savefig(printstr, format='eps', dpi=300); plt.close("all") def spectrum_analyzer(self, **kwargs): ymax=kwargs.get('ymax',3) ymin=kwargs.get('ymin',-80) nperseg=kwargs.get('nperseg',1024) fs=kwargs.get('Rs',self.Rs) freqx=np.arange(nperseg)/nperseg*fs/1e6 freqx.shape=(-1,1) sigin=kwargs['sigin'] sigin.shape=(-1,1) tstr=kwargs['tstr'] printstr=kwargs['printstr'] msg="Generating %s" %(printstr) self.print_log(type='I', msg=msg) figure=plt.figure() h=plt.subplot(); hfont = {'fontname':'Sans'} fs, spe=sig.welch(sigin,fs=self.Rs,nperseg=nperseg,return_onesided=False,scaling='spectrum',axis=0) spelog=10*np.log10(np.abs(spe)/np.amax(np.abs(spe))) plt.plot(freqx,spelog, linewidth=2 ) plt.ylim((ymin, ymax)); plt.xlim((np.amin(freqx), np.amax(freqx))); plt.suptitle(tstr,fontsize=20); plt.ylabel('Normalized Spectrum', **hfont,fontsize=18); plt.xlabel('Frequency (MHz)', **hfont,fontsize=18); h.tick_params(labelsize=14) plt.grid(True); figure.savefig(printstr, format='eps', dpi=300); plt.close("all") def logic_analyzer(self,argdict): ymax=argdict['ymax'] ymin=argdict['ymin'] timex=argdict['timex'] sigin=argdict['sigin'] tstr = argdict['tstr'] printstr=argdict['printstr'] msg="Generating %s" %(printstr) self.print_log(type='I', msg=msg) figure=plt.figure() h=plt.subplot(); hfont = {'fontname':'Sans'} markerline, stemlines, baseline = plt.stem(timex, sigin, '-.') plt.setp(markerline,'markerfacecolor', 'b','linewidth',2) plt.setp(stemlines, 'linestyle','solid','color','b', 'linewidth', 2) plt.ylim((ymin, ymax)); plt.xlim((np.amin(timex), np.amax(timex))); plt.suptitle(tstr,fontsize=20); plt.ylabel('Out', **hfont,fontsize=18); plt.xlabel('Sample (n)', **hfont,fontsize=18); h.tick_params(labelsize=14) plt.grid(True); figure.savefig(printstr, format='eps', dpi=300); def evm_calculator(self,argdict): reference=argdict['ref'] received=argdict['signal'] reference.shape=(-1,1) received.shape=(-1,1) rmsref=np.std(reference) rmsreceived=np.std(received) EVM=10*np.log10(np.mean(np.mean(np.abs(received/rmsreceived*rmsref-reference)**2,axis=0)/np.mean(np.abs(reference)**2,axis=0))) self.print_log(type='I', msg="Estimated EVM is %0.2f dB" %(EVM)) return EVM def ber_calculator(self,argdict): reference=argdict['ref'] received=argdict['signal'] reference.shape=(-1,1) received.shape=(-1,1) errors=np.sum(np.sum(np.abs(received-reference),axis=0))/(received.shape[0]*received.shape[1]) errors=np.sum(np.sum(np.abs(received-reference),axis=0)) bits=(received.shape[0]*received.shape[1]) self.print_log(type='I', msg="Received %i errors in %i bits" %(int(errors), int(bits))) BER=errors/bits self.print_log(type='I', msg="Resulting BER is %0.3g" %(BER)) return BER def plot_generic(x, y_list, title_str, legend_list, xlabel_str, ylabel_str, xscale, yscale, plot_style_str='o-', xlim=[], ylim=[]): if (xscale, yscale) == ('linear', 'linear'): plot_type_str = 'plot' elif (xscale, yscale) == ('log', 'linear'): plot_type_str = 'semilogx' elif (xscale, yscale) == ('linear', 'log'): plot_type_str = 'semilogy' elif (xscale, yscale) == ('log', 'log'): plot_type_str = 'loglog' else: raise Exception('xscale = %s, yscale = %s, both should be linear or log!!' % (xscale, yscale)) fig, ax = plt.subplots() if (isinstance(x[0], list)) and (len(x) == len(y_list)): for x, y in zip(x, y_list): exec('ax.' + plot_type_str + '(x, y, plot_style_str, linewidth=linewidth)') else: if (isinstance(y_list[0], list)): for y in y_list: exec('ax.' + plot_type_str + '(x, y, plot_style_str, linewidth=linewidth)') else: exec('ax.' + plot_type_str + '(x, y_list, plot_style_str, linewidth=linewidth)') if xlim != []: plt.xlim(xlim) if ylim != []: plt.ylim(ylim) ax.set_xlabel(xlabel_str, fontsize=fontsize) plt.ylabel(ylabel_str, fontsize=fontsize) if title_str == []: loc_y = 1.05 else: plt.title(title_str, fontsize=fontsize) loc_y = 1 if legend_list != []: plt.legend(legend_list, loc=(0, loc_y)) plt.grid(True, which='both') ax.tick_params(axis='both', which='major', labelsize=fontsize) plt.show()
true
true
f705f4ae86e20d5a772a5f5decb9d89639e2d559
4,579
py
Python
McsPyDataTools/McsPy/McsCMOS.py
multichannelsystems/McsPyDataTools
45777d5955043cc6849ea2f01ea442aa19141edd
[ "BSD-Source-Code" ]
5
2019-01-02T09:02:07.000Z
2022-02-11T16:49:38.000Z
McsPyDataTools/McsPy/McsCMOS.py
multichannelsystems/McsPyDataTools
45777d5955043cc6849ea2f01ea442aa19141edd
[ "BSD-Source-Code" ]
5
2019-06-25T12:34:59.000Z
2022-02-11T16:22:09.000Z
McsPyDataTools/McsPy/McsCMOS.py
multichannelsystems/McsPyDataTools
45777d5955043cc6849ea2f01ea442aa19141edd
[ "BSD-Source-Code" ]
8
2019-09-27T13:37:02.000Z
2022-03-25T07:37:25.000Z
""" McsCMOS ~~~~~~~ Wrapper and Helper to access MCS CMOS Data within H5 Files :copyright: (c) 2018 by Multi Channel Systems MCS GmbH :license: see LICENSE for more details """ import h5py import numpy as np class CMOSData(h5py.File): """ Wrapper for a HDF5 File containing CMOS Data """ def __init__(self, path): """ Creates a CMOSData file and links it to a H5 File :param path: Path to a H5 File containing CMOS Data :type path: string """ super(CMOSData, self).__init__(path, mode='r') # -- map raw data -- self.raw_data= self['/Data/Recording_0/FrameStream/Stream_0/FrameDataEntity_0/FrameData'] self.conv_factors= self['/Data/Recording_0/FrameStream/Stream_0/FrameDataEntity_0/ConversionFactors'] # - map proxy data - self.conv_data = CMOSConvProxy(self) # -- map meta -- self.meta={} # - from InfoFrame info_frame= self['/Data/Recording_0/FrameStream/Stream_0/InfoFrame'] for key in info_frame.dtype.names: if hasattr(info_frame[key][0], "decode"): self.meta[key]=info_frame[key][0].decode('utf-8') else: self.meta[key]=info_frame[key][0] if("Tick" in self.meta): self.meta["FrameRate"] = 10.0**6/self.meta["Tick"] # - from File for key, value in self.attrs.items(): if hasattr(value, "decode"): self.meta[key]= value.decode('utf-8') else: self.meta[key]= value # - from Data Group for key, value in self['/Data'].attrs.items(): if hasattr(value, "decode"): self.meta[key]= value.decode('utf-8') else: self.meta[key]= value # -- map events -- if("EventStream" in self["Data/Recording_0/"].keys()): event_group = self["Data/Recording_0/EventStream/Stream_0/"] event_info = self["Data/Recording_0/EventStream/Stream_0/InfoEvent"] self.events={} self.event_frames={} for key in event_group.keys(): if "EventEntity" in key: info = event_info["Label"][event_info["EventID"]==int(key.split("_")[1])][0] self.events[info] = event_group[key][0, 0] self.event_frames[info] = event_group[key][0, 0]/self.meta["Tick"] class CMOSConvProxy: """ Private Class, should be embedded within a CMOSData Object. A proxy that transparently converts raw data to calibrated data. """ def __init__(self, parent): """ Creates a new CMOSConvProxy :param parent: Object that can provide raw_data and conv_factors :type parent: CMOSData """ self._parent = parent self.dtype = np.int32 def __getitem__(self, slices): """ Sliced access to converted data :param slices: Data-slices to retrieve :returns: converted data """ raw_data = self._parent.raw_data.__getitem__(slices) conv_fasctors = self._parent.conv_factors.__getitem__((slices[0], slices[1])) return (raw_data*conv_fasctors).astype(self.dtype) @property def shape(self): """ Shape of the data """ return self._parent.raw_data.shape class CMOSSpikes(h5py.File): """ Wrapper for a HDF5 File containing CMOS Spike Data. Spike Information is accessible through the .spike Member, Waveform Information (if available) through the .waveforms Member. """ def __init__(self, path): super(CMOSSpikes, self).__init__(path) # -- Check for right structure -- if("data" in self.keys() and "spikes" in self['data'].keys()): # -- Map Spike-Data to RecordArray self.spikes = np.core.records.fromarrays(self['data/spikes'][:,:], names='time, col, row', formats = 'int64, int64, int64') # -- Map Waveforms to Array if("waveforms" in self['data'].keys()): self.waveforms = self['data/waveforms'][:,:].transpose() else: raise IOError(path+ " has no valid CMOSSpikeFile Structure")
34.689394
110
0.544879
import h5py import numpy as np class CMOSData(h5py.File): def __init__(self, path): super(CMOSData, self).__init__(path, mode='r') self.raw_data= self['/Data/Recording_0/FrameStream/Stream_0/FrameDataEntity_0/FrameData'] self.conv_factors= self['/Data/Recording_0/FrameStream/Stream_0/FrameDataEntity_0/ConversionFactors'] self.conv_data = CMOSConvProxy(self) self.meta={} info_frame= self['/Data/Recording_0/FrameStream/Stream_0/InfoFrame'] for key in info_frame.dtype.names: if hasattr(info_frame[key][0], "decode"): self.meta[key]=info_frame[key][0].decode('utf-8') else: self.meta[key]=info_frame[key][0] if("Tick" in self.meta): self.meta["FrameRate"] = 10.0**6/self.meta["Tick"] for key, value in self.attrs.items(): if hasattr(value, "decode"): self.meta[key]= value.decode('utf-8') else: self.meta[key]= value for key, value in self['/Data'].attrs.items(): if hasattr(value, "decode"): self.meta[key]= value.decode('utf-8') else: self.meta[key]= value if("EventStream" in self["Data/Recording_0/"].keys()): event_group = self["Data/Recording_0/EventStream/Stream_0/"] event_info = self["Data/Recording_0/EventStream/Stream_0/InfoEvent"] self.events={} self.event_frames={} for key in event_group.keys(): if "EventEntity" in key: info = event_info["Label"][event_info["EventID"]==int(key.split("_")[1])][0] self.events[info] = event_group[key][0, 0] self.event_frames[info] = event_group[key][0, 0]/self.meta["Tick"] class CMOSConvProxy: def __init__(self, parent): self._parent = parent self.dtype = np.int32 def __getitem__(self, slices): raw_data = self._parent.raw_data.__getitem__(slices) conv_fasctors = self._parent.conv_factors.__getitem__((slices[0], slices[1])) return (raw_data*conv_fasctors).astype(self.dtype) @property def shape(self): return self._parent.raw_data.shape class CMOSSpikes(h5py.File): def __init__(self, path): super(CMOSSpikes, self).__init__(path) if("data" in self.keys() and "spikes" in self['data'].keys()): self.spikes = np.core.records.fromarrays(self['data/spikes'][:,:], names='time, col, row', formats = 'int64, int64, int64') if("waveforms" in self['data'].keys()): self.waveforms = self['data/waveforms'][:,:].transpose() else: raise IOError(path+ " has no valid CMOSSpikeFile Structure")
true
true
f705f652c38abcd0301c2a6ba9960c81d7a2d03b
502
py
Python
plotly/validators/bar/hoverlabel/_namelengthsrc.py
gnestor/plotly.py
a8ae062795ddbf9867b8578fe6d9e244948c15ff
[ "MIT" ]
12
2020-04-18T18:10:22.000Z
2021-12-06T10:11:15.000Z
plotly/validators/bar/hoverlabel/_namelengthsrc.py
gnestor/plotly.py
a8ae062795ddbf9867b8578fe6d9e244948c15ff
[ "MIT" ]
27
2020-04-28T21:23:12.000Z
2021-06-25T15:36:38.000Z
plotly/validators/bar/hoverlabel/_namelengthsrc.py
gnestor/plotly.py
a8ae062795ddbf9867b8578fe6d9e244948c15ff
[ "MIT" ]
6
2020-04-18T23:07:08.000Z
2021-11-18T07:53:06.000Z
import _plotly_utils.basevalidators class NamelengthsrcValidator(_plotly_utils.basevalidators.SrcValidator): def __init__( self, plotly_name='namelengthsrc', parent_name='bar.hoverlabel', **kwargs ): super(NamelengthsrcValidator, self).__init__( plotly_name=plotly_name, parent_name=parent_name, edit_type=kwargs.pop('edit_type', 'none'), role=kwargs.pop('role', 'info'), **kwargs )
26.421053
72
0.615538
import _plotly_utils.basevalidators class NamelengthsrcValidator(_plotly_utils.basevalidators.SrcValidator): def __init__( self, plotly_name='namelengthsrc', parent_name='bar.hoverlabel', **kwargs ): super(NamelengthsrcValidator, self).__init__( plotly_name=plotly_name, parent_name=parent_name, edit_type=kwargs.pop('edit_type', 'none'), role=kwargs.pop('role', 'info'), **kwargs )
true
true
f705f70fc4ddea80296fbe30a9b5a4ea75f4a1af
241
py
Python
src/core/utils.py
ghrecommender/ghrecommender-backend
c888bbc2e3369a581e2ae84890cb6d502c837db6
[ "MIT" ]
26
2017-09-26T01:26:30.000Z
2021-09-30T14:38:14.000Z
src/core/utils.py
ghrecommender/ghrecommender-backend
c888bbc2e3369a581e2ae84890cb6d502c837db6
[ "MIT" ]
7
2017-09-29T14:47:42.000Z
2021-06-10T17:26:19.000Z
src/core/utils.py
ghrecommender/ghrecommender-backend
c888bbc2e3369a581e2ae84890cb6d502c837db6
[ "MIT" ]
2
2017-11-15T10:45:11.000Z
2020-02-26T18:03:48.000Z
from rest_framework_extensions.key_constructor.constructors import DefaultKeyConstructor from rest_framework_extensions.key_constructor.constructors import bits class UserKeyConstructor(DefaultKeyConstructor): user = bits.UserKeyBit()
34.428571
88
0.875519
from rest_framework_extensions.key_constructor.constructors import DefaultKeyConstructor from rest_framework_extensions.key_constructor.constructors import bits class UserKeyConstructor(DefaultKeyConstructor): user = bits.UserKeyBit()
true
true
f705f7908cac086343ab21b33ea3c415c5273107
306
py
Python
DRL/log_analysis/numpy2png.py
EXYNOS-999/AWS_JPL_DRL
ea9df7f293058b0ca2dc63753e68182fcc5380f5
[ "Apache-2.0" ]
null
null
null
DRL/log_analysis/numpy2png.py
EXYNOS-999/AWS_JPL_DRL
ea9df7f293058b0ca2dc63753e68182fcc5380f5
[ "Apache-2.0" ]
1
2020-01-08T06:52:03.000Z
2020-01-08T07:05:44.000Z
DRL/log_analysis/numpy2png.py
EXYNOS-999/AWS_JPL_DRL
ea9df7f293058b0ca2dc63753e68182fcc5380f5
[ "Apache-2.0" ]
null
null
null
import numpy as np from PIL import Image import sys def numpy_to_png(source, dest): image = Image.fromarray(np.load(source)) image.save(dest,"PNG") if __name__ == "__main__": source = sys.argv[1] dest = source.split('.npy')[0] + '.png' print(source, " to ", dest) numpy_to_png(source, dest)
20.4
42
0.676471
import numpy as np from PIL import Image import sys def numpy_to_png(source, dest): image = Image.fromarray(np.load(source)) image.save(dest,"PNG") if __name__ == "__main__": source = sys.argv[1] dest = source.split('.npy')[0] + '.png' print(source, " to ", dest) numpy_to_png(source, dest)
true
true
f705f7eac68ee6fa86ef486da4d36936d4a08a45
12,880
py
Python
h/accounts/schemas.py
openstax/hypothesis-server
9610a7cd190cce933d93cdc4bda2dc907a2869e4
[ "MIT" ]
1
2018-03-09T02:15:16.000Z
2018-03-09T02:15:16.000Z
h/accounts/schemas.py
openstax/hypothesis-server
9610a7cd190cce933d93cdc4bda2dc907a2869e4
[ "MIT" ]
16
2018-03-14T21:23:46.000Z
2019-04-29T18:55:28.000Z
h/accounts/schemas.py
openstax/hypothesis-server
9610a7cd190cce933d93cdc4bda2dc907a2869e4
[ "MIT" ]
null
null
null
# -*- coding: utf-8 -*- from codecs import open import logging import colander import deform from itsdangerous import BadData, SignatureExpired from h import i18n, models, validators from h.accounts import util from h.services.user import UserNotActivated from h.models.user import ( EMAIL_MAX_LENGTH, USERNAME_MAX_LENGTH, USERNAME_MIN_LENGTH, USERNAME_PATTERN, ) from h.schemas.base import CSRFSchema, JSONSchema _ = i18n.TranslationString log = logging.getLogger(__name__) PASSWORD_MIN_LENGTH = 2 # FIXME: this is ridiculous USERNAME_BLACKLIST = None def get_blacklist(): global USERNAME_BLACKLIST if USERNAME_BLACKLIST is None: # Try to load the blacklist file from disk. If, for whatever reason, we # can't load the file, then don't crash out, just log a warning about # the problem. try: with open('h/accounts/blacklist', encoding='utf-8') as fp: blacklist = fp.readlines() except (IOError, ValueError): log.exception('unable to load blacklist') blacklist = [] USERNAME_BLACKLIST = set(l.strip().lower() for l in blacklist) return USERNAME_BLACKLIST def unique_email(node, value): '''Colander validator that ensures no user with this email exists.''' request = node.bindings['request'] user = models.User.get_by_email(request.db, value, request.authority) if user and user.userid != request.authenticated_userid: msg = _("Sorry, an account with this email address already exists.") raise colander.Invalid(node, msg) def unique_username(node, value): '''Colander validator that ensures the username does not exist.''' request = node.bindings['request'] user = models.User.get_by_username(request.db, value, request.authority) if user: msg = _("This username is already taken.") raise colander.Invalid(node, msg) def email_node(**kwargs): """Return a Colander schema node for a new user email.""" return colander.SchemaNode( colander.String(), validator=colander.All( validators.Length(max=EMAIL_MAX_LENGTH), validators.Email(), unique_email, ), widget=deform.widget.TextInputWidget(template='emailinput'), **kwargs) def unblacklisted_username(node, value, blacklist=None): '''Colander validator that ensures the username is not blacklisted.''' if blacklist is None: blacklist = get_blacklist() if value.lower() in blacklist: # We raise a generic "user with this name already exists" error so as # not to make explicit the presence of a blacklist. msg = _("Sorry, an account with this username already exists. " "Please enter another one.") raise colander.Invalid(node, msg) def password_node(**kwargs): """Return a Colander schema node for an existing user password.""" kwargs.setdefault('widget', deform.widget.PasswordWidget()) return colander.SchemaNode( colander.String(), **kwargs) def new_password_node(**kwargs): """Return a Colander schema node for a new user password.""" kwargs.setdefault('widget', deform.widget.PasswordWidget()) return colander.SchemaNode( colander.String(), validator=validators.Length(min=PASSWORD_MIN_LENGTH), **kwargs) class LoginSchema(CSRFSchema): username = colander.SchemaNode( colander.String(), title=_('Username / email'), widget=deform.widget.TextInputWidget(autofocus=True), ) password = colander.SchemaNode( colander.String(), title=_('Password'), widget=deform.widget.PasswordWidget() ) def validator(self, node, value): super(LoginSchema, self).validator(node, value) request = node.bindings['request'] username = value.get('username') password = value.get('password') user_service = request.find_service(name='user') user_password_service = request.find_service(name='user_password') try: user = user_service.fetch_for_login(username_or_email=username) except UserNotActivated: err = colander.Invalid(node) err['username'] = _("Please check your email and open the link " "to activate your account.") raise err if user is None: err = colander.Invalid(node) err['username'] = _('User does not exist.') raise err if not user_password_service.check_password(user, password): err = colander.Invalid(node) err['password'] = _('Wrong password.') raise err value['user'] = user class ForgotPasswordSchema(CSRFSchema): email = colander.SchemaNode( colander.String(), validator=colander.All(validators.Email()), title=_('Email address'), widget=deform.widget.TextInputWidget(template='emailinput', autofocus=True), ) def validator(self, node, value): super(ForgotPasswordSchema, self).validator(node, value) request = node.bindings['request'] email = value.get('email') user = models.User.get_by_email(request.db, email, request.authority) if user is None: err = colander.Invalid(node) err['email'] = _('Unknown email address.') raise err value['user'] = user class RegisterSchema(CSRFSchema): username = colander.SchemaNode( colander.String(), validator=colander.All( validators.Length(min=USERNAME_MIN_LENGTH, max=USERNAME_MAX_LENGTH), colander.Regex( USERNAME_PATTERN, msg=_("Must have only letters, numbers, periods, and " "underscores.")), unique_username, unblacklisted_username, ), title=_('Username'), hint=_('Must be between {min} and {max} characters, containing only ' 'letters, numbers, periods, and underscores.').format( min=USERNAME_MIN_LENGTH, max=USERNAME_MAX_LENGTH ), widget=deform.widget.TextInputWidget(autofocus=True), ) email = email_node(title=_('Email address')) password = new_password_node(title=_('Password')) class ResetCode(colander.SchemaType): """Schema type transforming a reset code to a user and back.""" def serialize(self, node, appstruct): if appstruct is colander.null: return colander.null if not isinstance(appstruct, models.User): raise colander.Invalid(node, '%r is not a User' % appstruct) request = node.bindings['request'] serializer = request.registry.password_reset_serializer return serializer.dumps(appstruct.username) def deserialize(self, node, cstruct): if cstruct is colander.null: return colander.null request = node.bindings['request'] serializer = request.registry.password_reset_serializer try: (username, timestamp) = serializer.loads(cstruct, max_age=72*3600, return_timestamp=True) except SignatureExpired: raise colander.Invalid(node, _('Reset code has expired. Please reset your password again')) except BadData: raise colander.Invalid(node, _('Wrong reset code.')) user = models.User.get_by_username(request.db, username, request.authority) if user is None: raise colander.Invalid(node, _('Your reset code is not valid')) if user.password_updated is not None and timestamp < user.password_updated: raise colander.Invalid(node, _('This reset code has already been used.')) return user class ResetPasswordSchema(CSRFSchema): # N.B. this is the field into which the user puts their reset code, but we # call it `user` because when validated, it will return a `User` object. user = colander.SchemaNode( ResetCode(), title=_('Reset code'), hint=_('This will be emailed to you.'), widget=deform.widget.TextInputWidget(disable_autocomplete=True)) password = new_password_node( title=_('New password'), widget=deform.widget.PasswordWidget(disable_autocomplete=True)) class EmailChangeSchema(CSRFSchema): email = email_node(title=_('Email address')) # No validators: all validation is done on the email field password = password_node(title=_('Confirm password'), hide_until_form_active=True) def validator(self, node, value): super(EmailChangeSchema, self).validator(node, value) exc = colander.Invalid(node) request = node.bindings['request'] svc = request.find_service(name='user_password') user = request.user if not svc.check_password(user, value.get('password')): exc['password'] = _('Wrong password.') if exc.children: raise exc class PasswordChangeSchema(CSRFSchema): password = password_node(title=_('Current password'), inactive_label=_('Password')) new_password = password_node(title=_('New password'), hide_until_form_active=True) # No validators: all validation is done on the new_password field and we # merely assert that the confirmation field is the same. new_password_confirm = colander.SchemaNode( colander.String(), title=_('Confirm new password'), widget=deform.widget.PasswordWidget(), hide_until_form_active=True) def validator(self, node, value): super(PasswordChangeSchema, self).validator(node, value) exc = colander.Invalid(node) request = node.bindings['request'] svc = request.find_service(name='user_password') user = request.user if value.get('new_password') != value.get('new_password_confirm'): exc['new_password_confirm'] = _('The passwords must match') if not svc.check_password(user, value.get('password')): exc['password'] = _('Wrong password.') if exc.children: raise exc def validate_url(node, cstruct): try: util.validate_url(cstruct) except ValueError as exc: raise colander.Invalid(node, str(exc)) def validate_orcid(node, cstruct): try: util.validate_orcid(cstruct) except ValueError as exc: raise colander.Invalid(node, str(exc)) class EditProfileSchema(CSRFSchema): display_name = colander.SchemaNode( colander.String(), missing=None, validator=validators.Length(max=30), title=_('Display name')) description = colander.SchemaNode( colander.String(), missing=None, validator=validators.Length(max=250), widget=deform.widget.TextAreaWidget( max_length=250, rows=4, ), title=_('Description')) location = colander.SchemaNode( colander.String(), missing=None, validator=validators.Length(max=100), title=_('Location')) link = colander.SchemaNode( colander.String(), missing=None, validator=colander.All( validators.Length(max=250), validate_url), title=_('Link')) orcid = colander.SchemaNode( colander.String(), missing=None, validator=validate_orcid, title=_('ORCID'), hint=_('ORCID provides a persistent identifier for researchers (see orcid.org).')) class NotificationsSchema(CSRFSchema): types = (('reply', _('Email me when someone replies to one of my annotations.'),),) notifications = colander.SchemaNode( colander.Set(), widget=deform.widget.CheckboxChoiceWidget( omit_label=True, values=types), ) class CreateUserAPISchema(JSONSchema): """Validate a user JSON object.""" schema = { 'type': 'object', 'properties': { 'authority': { 'type': 'string', 'format': 'hostname', }, 'username': { 'type': 'string', 'minLength': 3, 'maxLength': 30, 'pattern': '^[A-Za-z0-9._]+$', }, 'email': { 'type': 'string', 'format': 'email', }, }, 'required': [ 'authority', 'username', 'email', ], } def includeme(config): pass
32.941176
103
0.615062
from codecs import open import logging import colander import deform from itsdangerous import BadData, SignatureExpired from h import i18n, models, validators from h.accounts import util from h.services.user import UserNotActivated from h.models.user import ( EMAIL_MAX_LENGTH, USERNAME_MAX_LENGTH, USERNAME_MIN_LENGTH, USERNAME_PATTERN, ) from h.schemas.base import CSRFSchema, JSONSchema _ = i18n.TranslationString log = logging.getLogger(__name__) PASSWORD_MIN_LENGTH = 2 USERNAME_BLACKLIST = None def get_blacklist(): global USERNAME_BLACKLIST if USERNAME_BLACKLIST is None: try: with open('h/accounts/blacklist', encoding='utf-8') as fp: blacklist = fp.readlines() except (IOError, ValueError): log.exception('unable to load blacklist') blacklist = [] USERNAME_BLACKLIST = set(l.strip().lower() for l in blacklist) return USERNAME_BLACKLIST def unique_email(node, value): request = node.bindings['request'] user = models.User.get_by_email(request.db, value, request.authority) if user and user.userid != request.authenticated_userid: msg = _("Sorry, an account with this email address already exists.") raise colander.Invalid(node, msg) def unique_username(node, value): request = node.bindings['request'] user = models.User.get_by_username(request.db, value, request.authority) if user: msg = _("This username is already taken.") raise colander.Invalid(node, msg) def email_node(**kwargs): return colander.SchemaNode( colander.String(), validator=colander.All( validators.Length(max=EMAIL_MAX_LENGTH), validators.Email(), unique_email, ), widget=deform.widget.TextInputWidget(template='emailinput'), **kwargs) def unblacklisted_username(node, value, blacklist=None): if blacklist is None: blacklist = get_blacklist() if value.lower() in blacklist: msg = _("Sorry, an account with this username already exists. " "Please enter another one.") raise colander.Invalid(node, msg) def password_node(**kwargs): kwargs.setdefault('widget', deform.widget.PasswordWidget()) return colander.SchemaNode( colander.String(), **kwargs) def new_password_node(**kwargs): kwargs.setdefault('widget', deform.widget.PasswordWidget()) return colander.SchemaNode( colander.String(), validator=validators.Length(min=PASSWORD_MIN_LENGTH), **kwargs) class LoginSchema(CSRFSchema): username = colander.SchemaNode( colander.String(), title=_('Username / email'), widget=deform.widget.TextInputWidget(autofocus=True), ) password = colander.SchemaNode( colander.String(), title=_('Password'), widget=deform.widget.PasswordWidget() ) def validator(self, node, value): super(LoginSchema, self).validator(node, value) request = node.bindings['request'] username = value.get('username') password = value.get('password') user_service = request.find_service(name='user') user_password_service = request.find_service(name='user_password') try: user = user_service.fetch_for_login(username_or_email=username) except UserNotActivated: err = colander.Invalid(node) err['username'] = _("Please check your email and open the link " "to activate your account.") raise err if user is None: err = colander.Invalid(node) err['username'] = _('User does not exist.') raise err if not user_password_service.check_password(user, password): err = colander.Invalid(node) err['password'] = _('Wrong password.') raise err value['user'] = user class ForgotPasswordSchema(CSRFSchema): email = colander.SchemaNode( colander.String(), validator=colander.All(validators.Email()), title=_('Email address'), widget=deform.widget.TextInputWidget(template='emailinput', autofocus=True), ) def validator(self, node, value): super(ForgotPasswordSchema, self).validator(node, value) request = node.bindings['request'] email = value.get('email') user = models.User.get_by_email(request.db, email, request.authority) if user is None: err = colander.Invalid(node) err['email'] = _('Unknown email address.') raise err value['user'] = user class RegisterSchema(CSRFSchema): username = colander.SchemaNode( colander.String(), validator=colander.All( validators.Length(min=USERNAME_MIN_LENGTH, max=USERNAME_MAX_LENGTH), colander.Regex( USERNAME_PATTERN, msg=_("Must have only letters, numbers, periods, and " "underscores.")), unique_username, unblacklisted_username, ), title=_('Username'), hint=_('Must be between {min} and {max} characters, containing only ' 'letters, numbers, periods, and underscores.').format( min=USERNAME_MIN_LENGTH, max=USERNAME_MAX_LENGTH ), widget=deform.widget.TextInputWidget(autofocus=True), ) email = email_node(title=_('Email address')) password = new_password_node(title=_('Password')) class ResetCode(colander.SchemaType): def serialize(self, node, appstruct): if appstruct is colander.null: return colander.null if not isinstance(appstruct, models.User): raise colander.Invalid(node, '%r is not a User' % appstruct) request = node.bindings['request'] serializer = request.registry.password_reset_serializer return serializer.dumps(appstruct.username) def deserialize(self, node, cstruct): if cstruct is colander.null: return colander.null request = node.bindings['request'] serializer = request.registry.password_reset_serializer try: (username, timestamp) = serializer.loads(cstruct, max_age=72*3600, return_timestamp=True) except SignatureExpired: raise colander.Invalid(node, _('Reset code has expired. Please reset your password again')) except BadData: raise colander.Invalid(node, _('Wrong reset code.')) user = models.User.get_by_username(request.db, username, request.authority) if user is None: raise colander.Invalid(node, _('Your reset code is not valid')) if user.password_updated is not None and timestamp < user.password_updated: raise colander.Invalid(node, _('This reset code has already been used.')) return user class ResetPasswordSchema(CSRFSchema): user = colander.SchemaNode( ResetCode(), title=_('Reset code'), hint=_('This will be emailed to you.'), widget=deform.widget.TextInputWidget(disable_autocomplete=True)) password = new_password_node( title=_('New password'), widget=deform.widget.PasswordWidget(disable_autocomplete=True)) class EmailChangeSchema(CSRFSchema): email = email_node(title=_('Email address')) password = password_node(title=_('Confirm password'), hide_until_form_active=True) def validator(self, node, value): super(EmailChangeSchema, self).validator(node, value) exc = colander.Invalid(node) request = node.bindings['request'] svc = request.find_service(name='user_password') user = request.user if not svc.check_password(user, value.get('password')): exc['password'] = _('Wrong password.') if exc.children: raise exc class PasswordChangeSchema(CSRFSchema): password = password_node(title=_('Current password'), inactive_label=_('Password')) new_password = password_node(title=_('New password'), hide_until_form_active=True) new_password_confirm = colander.SchemaNode( colander.String(), title=_('Confirm new password'), widget=deform.widget.PasswordWidget(), hide_until_form_active=True) def validator(self, node, value): super(PasswordChangeSchema, self).validator(node, value) exc = colander.Invalid(node) request = node.bindings['request'] svc = request.find_service(name='user_password') user = request.user if value.get('new_password') != value.get('new_password_confirm'): exc['new_password_confirm'] = _('The passwords must match') if not svc.check_password(user, value.get('password')): exc['password'] = _('Wrong password.') if exc.children: raise exc def validate_url(node, cstruct): try: util.validate_url(cstruct) except ValueError as exc: raise colander.Invalid(node, str(exc)) def validate_orcid(node, cstruct): try: util.validate_orcid(cstruct) except ValueError as exc: raise colander.Invalid(node, str(exc)) class EditProfileSchema(CSRFSchema): display_name = colander.SchemaNode( colander.String(), missing=None, validator=validators.Length(max=30), title=_('Display name')) description = colander.SchemaNode( colander.String(), missing=None, validator=validators.Length(max=250), widget=deform.widget.TextAreaWidget( max_length=250, rows=4, ), title=_('Description')) location = colander.SchemaNode( colander.String(), missing=None, validator=validators.Length(max=100), title=_('Location')) link = colander.SchemaNode( colander.String(), missing=None, validator=colander.All( validators.Length(max=250), validate_url), title=_('Link')) orcid = colander.SchemaNode( colander.String(), missing=None, validator=validate_orcid, title=_('ORCID'), hint=_('ORCID provides a persistent identifier for researchers (see orcid.org).')) class NotificationsSchema(CSRFSchema): types = (('reply', _('Email me when someone replies to one of my annotations.'),),) notifications = colander.SchemaNode( colander.Set(), widget=deform.widget.CheckboxChoiceWidget( omit_label=True, values=types), ) class CreateUserAPISchema(JSONSchema): schema = { 'type': 'object', 'properties': { 'authority': { 'type': 'string', 'format': 'hostname', }, 'username': { 'type': 'string', 'minLength': 3, 'maxLength': 30, 'pattern': '^[A-Za-z0-9._]+$', }, 'email': { 'type': 'string', 'format': 'email', }, }, 'required': [ 'authority', 'username', 'email', ], } def includeme(config): pass
true
true
f705f844ab6c8fc39a4b3639c4a016021d2926c3
16,056
py
Python
Tools/Scripts/webkitpy/w3c/test_converter_unittest.py
primiano/blink-gitcs
0b5424070e3006102e0036deea1e2e263b871eaa
[ "BSD-3-Clause" ]
1
2017-08-25T05:15:52.000Z
2017-08-25T05:15:52.000Z
Tools/Scripts/webkitpy/w3c/test_converter_unittest.py
primiano/blink-gitcs
0b5424070e3006102e0036deea1e2e263b871eaa
[ "BSD-3-Clause" ]
null
null
null
Tools/Scripts/webkitpy/w3c/test_converter_unittest.py
primiano/blink-gitcs
0b5424070e3006102e0036deea1e2e263b871eaa
[ "BSD-3-Clause" ]
null
null
null
# Copyright (C) 2013 Adobe Systems Incorporated. All rights reserved. # # 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. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER "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 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 os import re import unittest from webkitpy.common.host import Host from webkitpy.common.system.outputcapture import OutputCapture from webkitpy.common.webkit_finder import WebKitFinder from webkitpy.thirdparty.BeautifulSoup import BeautifulSoup from webkitpy.w3c.test_converter import _W3CTestConverter DUMMY_FILENAME = 'dummy.html' DUMMY_PATH = 'dummy/testharness/path' class W3CTestConverterTest(unittest.TestCase): # FIXME: When we move to using a MockHost, this method should be removed, since # then we can just pass in a dummy dir path def fake_dir_path(self, dirname): filesystem = Host().filesystem webkit_root = WebKitFinder(filesystem).webkit_base() return filesystem.abspath(filesystem.join(webkit_root, "LayoutTests", "css", dirname)) def test_read_prefixed_property_list(self): """ Tests that the current list of properties requiring the -webkit- prefix load correctly """ # FIXME: We should be passing in a MockHost here ... converter = _W3CTestConverter(DUMMY_PATH, DUMMY_FILENAME, None) prop_list = converter.prefixed_properties self.assertTrue(prop_list, 'No prefixed properties found') def test_convert_for_webkit_nothing_to_convert(self): """ Tests convert_for_webkit() using a basic test that has nothing to convert """ test_html = """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>CSS Test: DESCRIPTION OF TEST</title> <link rel="author" title="NAME_OF_AUTHOR" href="mailto:EMAIL OR http://CONTACT_PAGE"/> <link rel="help" href="RELEVANT_SPEC_SECTION"/> <meta name="assert" content="TEST ASSERTION"/> <style type="text/css"><![CDATA[ CSS FOR TEST ]]></style> </head> <body> CONTENT OF TEST </body> </html> """ converter = _W3CTestConverter(DUMMY_PATH, DUMMY_FILENAME, None) oc = OutputCapture() oc.capture_output() try: converter.feed(test_html) converter.close() converted = converter.output() finally: oc.restore_output() self.verify_no_conversion_happened(converted, test_html) def test_convert_for_webkit_harness_only(self): """ Tests convert_for_webkit() using a basic JS test that uses testharness.js only and has no prefixed properties """ test_html = """<head> <link href="/resources/testharness.css" rel="stylesheet" type="text/css"> <script src="/resources/testharness.js"></script> </head> """ fake_dir_path = self.fake_dir_path("harnessonly") converter = _W3CTestConverter(fake_dir_path, DUMMY_FILENAME, None) converter.feed(test_html) converter.close() converted = converter.output() self.verify_conversion_happened(converted) self.verify_test_harness_paths(converter, converted[1], fake_dir_path, 1, 1) self.verify_prefixed_properties(converted, []) def test_convert_for_webkit_properties_only(self): """ Tests convert_for_webkit() using a test that has 2 prefixed properties: 1 in a style block + 1 inline style """ test_html = """<html> <head> <link href="/resources/testharness.css" rel="stylesheet" type="text/css"> <script src="/resources/testharness.js"></script> <style type="text/css"> #block1 { @test0@: propvalue; } </style> </head> <body> <div id="elem1" style="@test1@: propvalue;"></div> </body> </html> """ fake_dir_path = self.fake_dir_path('harnessandprops') converter = _W3CTestConverter(fake_dir_path, DUMMY_FILENAME, None) test_content = self.generate_test_content(converter.prefixed_properties, 1, test_html) oc = OutputCapture() oc.capture_output() try: converter.feed(test_content[1]) converter.close() converted = converter.output() finally: oc.restore_output() self.verify_conversion_happened(converted) self.verify_test_harness_paths(converter, converted[1], fake_dir_path, 1, 1) self.verify_prefixed_properties(converted, test_content[0]) def test_convert_for_webkit_harness_and_properties(self): """ Tests convert_for_webkit() using a basic JS test that uses testharness.js and testharness.css and has 4 prefixed properties: 3 in a style block + 1 inline style """ test_html = """<html> <head> <link href="/resources/testharness.css" rel="stylesheet" type="text/css"> <script src="/resources/testharness.js"></script> <style type="text/css"> #block1 { @test0@: propvalue; } #block2 { @test1@: propvalue; } #block3 { @test2@: propvalue; } </style> </head> <body> <div id="elem1" style="@test3@: propvalue;"></div> </body> </html> """ fake_dir_path = self.fake_dir_path('harnessandprops') converter = _W3CTestConverter(fake_dir_path, DUMMY_FILENAME, None) oc = OutputCapture() oc.capture_output() try: test_content = self.generate_test_content(converter.prefixed_properties, 2, test_html) converter.feed(test_content[1]) converter.close() converted = converter.output() finally: oc.restore_output() self.verify_conversion_happened(converted) self.verify_test_harness_paths(converter, converted[1], fake_dir_path, 1, 1) self.verify_prefixed_properties(converted, test_content[0]) def test_convert_test_harness_paths(self): """ Tests convert_testharness_paths() with a test that uses all three testharness files """ test_html = """<head> <link href="/resources/testharness.css" rel="stylesheet" type="text/css"> <script src="/resources/testharness.js"></script> <script src="/resources/testharnessreport.js"></script> </head> """ fake_dir_path = self.fake_dir_path('testharnesspaths') converter = _W3CTestConverter(fake_dir_path, DUMMY_FILENAME, None) oc = OutputCapture() oc.capture_output() try: converter.feed(test_html) converter.close() converted = converter.output() finally: oc.restore_output() self.verify_conversion_happened(converted) self.verify_test_harness_paths(converter, converted[1], fake_dir_path, 2, 1) def test_convert_vendor_prefix_js_paths(self): test_html = """<head> <script src="/common/vendor-prefix.js"> </head> """ fake_dir_path = self.fake_dir_path('adapterjspaths') converter = _W3CTestConverter(fake_dir_path, DUMMY_FILENAME, None) oc = OutputCapture() oc.capture_output() try: converter.feed(test_html) converter.close() converted = converter.output() finally: oc.restore_output() new_html = BeautifulSoup(converted[1]) # Verify the original paths are gone, and the new paths are present. orig_path_pattern = re.compile('\"/common/vendor-prefix.js') self.assertEquals(len(new_html.findAll(src=orig_path_pattern)), 0, 'vendor-prefix.js path was not converted') resources_dir = converter.path_from_webkit_root("LayoutTests", "resources") new_relpath = os.path.relpath(resources_dir, fake_dir_path) relpath_pattern = re.compile(new_relpath) self.assertEquals(len(new_html.findAll(src=relpath_pattern)), 1, 'vendor-prefix.js relative path not correct') def test_convert_prefixed_properties(self): """ Tests convert_prefixed_properties() file that has 20 properties requiring the -webkit- prefix: 10 in one style block + 5 in another style block + 5 inline styles, including one with multiple prefixed properties. The properties in the test content are in all sorts of wack formatting. """ test_html = """<html> <style type="text/css"><![CDATA[ .block1 { width: 300px; height: 300px } .block2 { @test0@: propvalue; } .block3{@test1@: propvalue;} .block4 { @test2@:propvalue; } .block5{ @test3@ :propvalue; } #block6 { @test4@ : propvalue; } #block7 { @test5@: propvalue; } #block8 { @test6@: propvalue; } #block9:pseudo { @test7@: propvalue; @test8@: propvalue propvalue propvalue; } ]]></style> </head> <body> <div id="elem1" style="@test9@: propvalue;"></div> <div id="elem2" style="propname: propvalue; @test10@ : propvalue; propname:propvalue;"></div> <div id="elem2" style="@test11@: propvalue; @test12@ : propvalue; @test13@ :propvalue;"></div> <div id="elem3" style="@test14@:propvalue"></div> </body> <style type="text/css"><![CDATA[ .block10{ @test15@: propvalue; } .block11{ @test16@: propvalue; } .block12{ @test17@: propvalue; } #block13:pseudo { @test18@: propvalue; @test19@: propvalue; } ]]></style> </html> """ converter = _W3CTestConverter(DUMMY_PATH, DUMMY_FILENAME, None) test_content = self.generate_test_content(converter.prefixed_properties, 20, test_html) oc = OutputCapture() oc.capture_output() try: converter.feed(test_content[1]) converter.close() converted = converter.output() finally: oc.restore_output() self.verify_conversion_happened(converted) self.verify_prefixed_properties(converted, test_content[0]) def test_hides_all_instructions_for_manual_testers(self): test_html = """<body> <h1 class="instructions">Hello manual tester!</h1> <p class="instructions some_other_class">This is how you run this test.</p> <p style="willbeoverwritten" class="instructions">...</p> <doesntmatterwhichtagitis class="some_other_class instructions">...</p> <p>Legit content may contain the instructions string</p> </body> """ expected_test_html = """<body> <h1 class="instructions" style="display:none">Hello manual tester!</h1> <p class="instructions some_other_class" style="display:none">This is how you run this test.</p> <p class="instructions" style="display:none">...</p> <doesntmatterwhichtagitis class="some_other_class instructions" style="display:none">...</p> <p>Legit content may contain the instructions string</p> </body> """ converter = _W3CTestConverter(DUMMY_PATH, DUMMY_FILENAME, None) oc = OutputCapture() oc.capture_output() try: converter.feed(test_html) converter.close() converted = converter.output() finally: oc.restore_output() self.assertEqual(converted[1], expected_test_html) def test_convert_attributes_if_needed(self): """ Tests convert_attributes_if_needed() using a reference file that has some relative src paths """ test_html = """<html> <head> <script src="../../some-script.js"></script> <style src="../../../some-style.css"></style> </head> <body> <img src="../../../../some-image.jpg"> </body> </html> """ test_reference_support_info = {'reference_relpath': '../', 'files': ['../../some-script.js', '../../../some-style.css', '../../../../some-image.jpg'], 'elements': ['script', 'style', 'img']} converter = _W3CTestConverter(DUMMY_PATH, DUMMY_FILENAME, test_reference_support_info) oc = OutputCapture() oc.capture_output() try: converter.feed(test_html) converter.close() converted = converter.output() finally: oc.restore_output() self.verify_conversion_happened(converted) self.verify_reference_relative_paths(converted, test_reference_support_info) def verify_conversion_happened(self, converted): self.assertTrue(converted, "conversion didn't happen") def verify_no_conversion_happened(self, converted, original): self.assertEqual(converted[1], original, 'test should not have been converted') def verify_test_harness_paths(self, converter, converted, test_path, num_src_paths, num_href_paths): if isinstance(converted, basestring): converted = BeautifulSoup(converted) resources_dir = converter.path_from_webkit_root("LayoutTests", "resources") # Verify the original paths are gone, and the new paths are present. orig_path_pattern = re.compile('\"/resources/testharness') self.assertEquals(len(converted.findAll(src=orig_path_pattern)), 0, 'testharness src path was not converted') self.assertEquals(len(converted.findAll(href=orig_path_pattern)), 0, 'testharness href path was not converted') new_relpath = os.path.relpath(resources_dir, test_path) relpath_pattern = re.compile(new_relpath) self.assertEquals(len(converted.findAll(src=relpath_pattern)), num_src_paths, 'testharness src relative path not correct') self.assertEquals(len(converted.findAll(href=relpath_pattern)), num_href_paths, 'testharness href relative path not correct') def verify_prefixed_properties(self, converted, test_properties): self.assertEqual(len(set(converted[0])), len(set(test_properties)), 'Incorrect number of properties converted') for test_prop in test_properties: self.assertTrue((test_prop in converted[1]), 'Property ' + test_prop + ' not found in converted doc') def verify_reference_relative_paths(self, converted, reference_support_info): idx = 0 for path in reference_support_info['files']: expected_path = re.sub(reference_support_info['reference_relpath'], '', path, 1) element = reference_support_info['elements'][idx] expected_tag = '<' + element + ' src=\"' + expected_path + '\">' self.assertTrue(expected_tag in converted[1], 'relative path ' + path + ' was not converted correcty') idx += 1 def generate_test_content(self, full_property_list, num_test_properties, html): """Inserts properties requiring a -webkit- prefix into the content, replacing \'@testXX@\' with a property.""" test_properties = [] count = 0 while count < num_test_properties: test_properties.append(full_property_list[count]) count += 1 # Replace the tokens in the testhtml with the test properties. Walk backward # through the list to replace the double-digit tokens first index = len(test_properties) - 1 while index >= 0: # Use the unprefixed version test_prop = test_properties[index].replace('-webkit-', '') # Replace the token html = html.replace('@test' + str(index) + '@', test_prop) index -= 1 return (test_properties, html)
37.690141
198
0.682237
import os import re import unittest from webkitpy.common.host import Host from webkitpy.common.system.outputcapture import OutputCapture from webkitpy.common.webkit_finder import WebKitFinder from webkitpy.thirdparty.BeautifulSoup import BeautifulSoup from webkitpy.w3c.test_converter import _W3CTestConverter DUMMY_FILENAME = 'dummy.html' DUMMY_PATH = 'dummy/testharness/path' class W3CTestConverterTest(unittest.TestCase): def fake_dir_path(self, dirname): filesystem = Host().filesystem webkit_root = WebKitFinder(filesystem).webkit_base() return filesystem.abspath(filesystem.join(webkit_root, "LayoutTests", "css", dirname)) def test_read_prefixed_property_list(self): converter = _W3CTestConverter(DUMMY_PATH, DUMMY_FILENAME, None) prop_list = converter.prefixed_properties self.assertTrue(prop_list, 'No prefixed properties found') def test_convert_for_webkit_nothing_to_convert(self): test_html = """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>CSS Test: DESCRIPTION OF TEST</title> <link rel="author" title="NAME_OF_AUTHOR" href="mailto:EMAIL OR http://CONTACT_PAGE"/> <link rel="help" href="RELEVANT_SPEC_SECTION"/> <meta name="assert" content="TEST ASSERTION"/> <style type="text/css"><![CDATA[ CSS FOR TEST ]]></style> </head> <body> CONTENT OF TEST </body> </html> """ converter = _W3CTestConverter(DUMMY_PATH, DUMMY_FILENAME, None) oc = OutputCapture() oc.capture_output() try: converter.feed(test_html) converter.close() converted = converter.output() finally: oc.restore_output() self.verify_no_conversion_happened(converted, test_html) def test_convert_for_webkit_harness_only(self): test_html = """<head> <link href="/resources/testharness.css" rel="stylesheet" type="text/css"> <script src="/resources/testharness.js"></script> </head> """ fake_dir_path = self.fake_dir_path("harnessonly") converter = _W3CTestConverter(fake_dir_path, DUMMY_FILENAME, None) converter.feed(test_html) converter.close() converted = converter.output() self.verify_conversion_happened(converted) self.verify_test_harness_paths(converter, converted[1], fake_dir_path, 1, 1) self.verify_prefixed_properties(converted, []) def test_convert_for_webkit_properties_only(self): test_html = """<html> <head> <link href="/resources/testharness.css" rel="stylesheet" type="text/css"> <script src="/resources/testharness.js"></script> <style type="text/css"> #block1 { @test0@: propvalue; } </style> </head> <body> <div id="elem1" style="@test1@: propvalue;"></div> </body> </html> """ fake_dir_path = self.fake_dir_path('harnessandprops') converter = _W3CTestConverter(fake_dir_path, DUMMY_FILENAME, None) test_content = self.generate_test_content(converter.prefixed_properties, 1, test_html) oc = OutputCapture() oc.capture_output() try: converter.feed(test_content[1]) converter.close() converted = converter.output() finally: oc.restore_output() self.verify_conversion_happened(converted) self.verify_test_harness_paths(converter, converted[1], fake_dir_path, 1, 1) self.verify_prefixed_properties(converted, test_content[0]) def test_convert_for_webkit_harness_and_properties(self): test_html = """<html> <head> <link href="/resources/testharness.css" rel="stylesheet" type="text/css"> <script src="/resources/testharness.js"></script> <style type="text/css"> #block1 { @test0@: propvalue; } #block2 { @test1@: propvalue; } #block3 { @test2@: propvalue; } </style> </head> <body> <div id="elem1" style="@test3@: propvalue;"></div> </body> </html> """ fake_dir_path = self.fake_dir_path('harnessandprops') converter = _W3CTestConverter(fake_dir_path, DUMMY_FILENAME, None) oc = OutputCapture() oc.capture_output() try: test_content = self.generate_test_content(converter.prefixed_properties, 2, test_html) converter.feed(test_content[1]) converter.close() converted = converter.output() finally: oc.restore_output() self.verify_conversion_happened(converted) self.verify_test_harness_paths(converter, converted[1], fake_dir_path, 1, 1) self.verify_prefixed_properties(converted, test_content[0]) def test_convert_test_harness_paths(self): test_html = """<head> <link href="/resources/testharness.css" rel="stylesheet" type="text/css"> <script src="/resources/testharness.js"></script> <script src="/resources/testharnessreport.js"></script> </head> """ fake_dir_path = self.fake_dir_path('testharnesspaths') converter = _W3CTestConverter(fake_dir_path, DUMMY_FILENAME, None) oc = OutputCapture() oc.capture_output() try: converter.feed(test_html) converter.close() converted = converter.output() finally: oc.restore_output() self.verify_conversion_happened(converted) self.verify_test_harness_paths(converter, converted[1], fake_dir_path, 2, 1) def test_convert_vendor_prefix_js_paths(self): test_html = """<head> <script src="/common/vendor-prefix.js"> </head> """ fake_dir_path = self.fake_dir_path('adapterjspaths') converter = _W3CTestConverter(fake_dir_path, DUMMY_FILENAME, None) oc = OutputCapture() oc.capture_output() try: converter.feed(test_html) converter.close() converted = converter.output() finally: oc.restore_output() new_html = BeautifulSoup(converted[1]) orig_path_pattern = re.compile('\"/common/vendor-prefix.js') self.assertEquals(len(new_html.findAll(src=orig_path_pattern)), 0, 'vendor-prefix.js path was not converted') resources_dir = converter.path_from_webkit_root("LayoutTests", "resources") new_relpath = os.path.relpath(resources_dir, fake_dir_path) relpath_pattern = re.compile(new_relpath) self.assertEquals(len(new_html.findAll(src=relpath_pattern)), 1, 'vendor-prefix.js relative path not correct') def test_convert_prefixed_properties(self): test_html = """<html> <style type="text/css"><![CDATA[ .block1 { width: 300px; height: 300px } .block2 { @test0@: propvalue; } .block3{@test1@: propvalue;} .block4 { @test2@:propvalue; } .block5{ @test3@ :propvalue; } #block6 { @test4@ : propvalue; } #block7 { @test5@: propvalue; } #block8 { @test6@: propvalue; } #block9:pseudo { @test7@: propvalue; @test8@: propvalue propvalue propvalue; } ]]></style> </head> <body> <div id="elem1" style="@test9@: propvalue;"></div> <div id="elem2" style="propname: propvalue; @test10@ : propvalue; propname:propvalue;"></div> <div id="elem2" style="@test11@: propvalue; @test12@ : propvalue; @test13@ :propvalue;"></div> <div id="elem3" style="@test14@:propvalue"></div> </body> <style type="text/css"><![CDATA[ .block10{ @test15@: propvalue; } .block11{ @test16@: propvalue; } .block12{ @test17@: propvalue; } #block13:pseudo { @test18@: propvalue; @test19@: propvalue; } ]]></style> </html> """ converter = _W3CTestConverter(DUMMY_PATH, DUMMY_FILENAME, None) test_content = self.generate_test_content(converter.prefixed_properties, 20, test_html) oc = OutputCapture() oc.capture_output() try: converter.feed(test_content[1]) converter.close() converted = converter.output() finally: oc.restore_output() self.verify_conversion_happened(converted) self.verify_prefixed_properties(converted, test_content[0]) def test_hides_all_instructions_for_manual_testers(self): test_html = """<body> <h1 class="instructions">Hello manual tester!</h1> <p class="instructions some_other_class">This is how you run this test.</p> <p style="willbeoverwritten" class="instructions">...</p> <doesntmatterwhichtagitis class="some_other_class instructions">...</p> <p>Legit content may contain the instructions string</p> </body> """ expected_test_html = """<body> <h1 class="instructions" style="display:none">Hello manual tester!</h1> <p class="instructions some_other_class" style="display:none">This is how you run this test.</p> <p class="instructions" style="display:none">...</p> <doesntmatterwhichtagitis class="some_other_class instructions" style="display:none">...</p> <p>Legit content may contain the instructions string</p> </body> """ converter = _W3CTestConverter(DUMMY_PATH, DUMMY_FILENAME, None) oc = OutputCapture() oc.capture_output() try: converter.feed(test_html) converter.close() converted = converter.output() finally: oc.restore_output() self.assertEqual(converted[1], expected_test_html) def test_convert_attributes_if_needed(self): test_html = """<html> <head> <script src="../../some-script.js"></script> <style src="../../../some-style.css"></style> </head> <body> <img src="../../../../some-image.jpg"> </body> </html> """ test_reference_support_info = {'reference_relpath': '../', 'files': ['../../some-script.js', '../../../some-style.css', '../../../../some-image.jpg'], 'elements': ['script', 'style', 'img']} converter = _W3CTestConverter(DUMMY_PATH, DUMMY_FILENAME, test_reference_support_info) oc = OutputCapture() oc.capture_output() try: converter.feed(test_html) converter.close() converted = converter.output() finally: oc.restore_output() self.verify_conversion_happened(converted) self.verify_reference_relative_paths(converted, test_reference_support_info) def verify_conversion_happened(self, converted): self.assertTrue(converted, "conversion didn't happen") def verify_no_conversion_happened(self, converted, original): self.assertEqual(converted[1], original, 'test should not have been converted') def verify_test_harness_paths(self, converter, converted, test_path, num_src_paths, num_href_paths): if isinstance(converted, basestring): converted = BeautifulSoup(converted) resources_dir = converter.path_from_webkit_root("LayoutTests", "resources") # Verify the original paths are gone, and the new paths are present. orig_path_pattern = re.compile('\"/resources/testharness') self.assertEquals(len(converted.findAll(src=orig_path_pattern)), 0, 'testharness src path was not converted') self.assertEquals(len(converted.findAll(href=orig_path_pattern)), 0, 'testharness href path was not converted') new_relpath = os.path.relpath(resources_dir, test_path) relpath_pattern = re.compile(new_relpath) self.assertEquals(len(converted.findAll(src=relpath_pattern)), num_src_paths, 'testharness src relative path not correct') self.assertEquals(len(converted.findAll(href=relpath_pattern)), num_href_paths, 'testharness href relative path not correct') def verify_prefixed_properties(self, converted, test_properties): self.assertEqual(len(set(converted[0])), len(set(test_properties)), 'Incorrect number of properties converted') for test_prop in test_properties: self.assertTrue((test_prop in converted[1]), 'Property ' + test_prop + ' not found in converted doc') def verify_reference_relative_paths(self, converted, reference_support_info): idx = 0 for path in reference_support_info['files']: expected_path = re.sub(reference_support_info['reference_relpath'], '', path, 1) element = reference_support_info['elements'][idx] expected_tag = '<' + element + ' src=\"' + expected_path + '\">' self.assertTrue(expected_tag in converted[1], 'relative path ' + path + ' was not converted correcty') idx += 1 def generate_test_content(self, full_property_list, num_test_properties, html): test_properties = [] count = 0 while count < num_test_properties: test_properties.append(full_property_list[count]) count += 1 # Replace the tokens in the testhtml with the test properties. Walk backward # through the list to replace the double-digit tokens first index = len(test_properties) - 1 while index >= 0: # Use the unprefixed version test_prop = test_properties[index].replace('-webkit-', '') # Replace the token html = html.replace('@test' + str(index) + '@', test_prop) index -= 1 return (test_properties, html)
true
true
f705f9212f5668c98d6b0949e6d5a1272d11e8d2
21,361
py
Python
sdk/network/azure-mgmt-network/azure/mgmt/network/v2020_03_01/operations/_security_rules_operations.py
vbarbaresi/azure-sdk-for-python
397ba46c51d001ff89c66b170f5576cf8f49c05f
[ "MIT" ]
8
2021-01-13T23:44:08.000Z
2021-03-17T10:13:36.000Z
sdk/network/azure-mgmt-network/azure/mgmt/network/v2020_03_01/operations/_security_rules_operations.py
vbarbaresi/azure-sdk-for-python
397ba46c51d001ff89c66b170f5576cf8f49c05f
[ "MIT" ]
null
null
null
sdk/network/azure-mgmt-network/azure/mgmt/network/v2020_03_01/operations/_security_rules_operations.py
vbarbaresi/azure-sdk-for-python
397ba46c51d001ff89c66b170f5576cf8f49c05f
[ "MIT" ]
null
null
null
# coding=utf-8 # -------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for license information. # Code generated by Microsoft (R) AutoRest Code Generator. # Changes may cause incorrect behavior and will be lost if the code is regenerated. # -------------------------------------------------------------------------- from typing import TYPE_CHECKING import warnings from azure.core.exceptions import ClientAuthenticationError, HttpResponseError, ResourceExistsError, ResourceNotFoundError, map_error from azure.core.paging import ItemPaged from azure.core.pipeline import PipelineResponse from azure.core.pipeline.transport import HttpRequest, HttpResponse from azure.core.polling import LROPoller, NoPolling, PollingMethod from azure.mgmt.core.exceptions import ARMErrorFormat from azure.mgmt.core.polling.arm_polling import ARMPolling from .. import models if TYPE_CHECKING: # pylint: disable=unused-import,ungrouped-imports from typing import Any, Callable, Dict, Generic, Iterable, Optional, TypeVar, Union T = TypeVar('T') ClsType = Optional[Callable[[PipelineResponse[HttpRequest, HttpResponse], T, Dict[str, Any]], Any]] class SecurityRulesOperations(object): """SecurityRulesOperations operations. You should not instantiate this class directly. Instead, you should create a Client instance that instantiates it for you and attaches it as an attribute. :ivar models: Alias to model classes used in this operation group. :type models: ~azure.mgmt.network.v2020_03_01.models :param client: Client for service requests. :param config: Configuration of service client. :param serializer: An object model serializer. :param deserializer: An object model deserializer. """ models = models def __init__(self, client, config, serializer, deserializer): self._client = client self._serialize = serializer self._deserialize = deserializer self._config = config def _delete_initial( self, resource_group_name, # type: str network_security_group_name, # type: str security_rule_name, # type: str **kwargs # type: Any ): # type: (...) -> None cls = kwargs.pop('cls', None) # type: ClsType[None] error_map = { 401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError } error_map.update(kwargs.pop('error_map', {})) api_version = "2020-03-01" accept = "application/json" # Construct URL url = self._delete_initial.metadata['url'] # type: ignore path_format_arguments = { 'resourceGroupName': self._serialize.url("resource_group_name", resource_group_name, 'str'), 'networkSecurityGroupName': self._serialize.url("network_security_group_name", network_security_group_name, 'str'), 'securityRuleName': self._serialize.url("security_rule_name", security_rule_name, 'str'), 'subscriptionId': self._serialize.url("self._config.subscription_id", self._config.subscription_id, 'str'), } url = self._client.format_url(url, **path_format_arguments) # Construct parameters query_parameters = {} # type: Dict[str, Any] query_parameters['api-version'] = self._serialize.query("api_version", api_version, 'str') # Construct headers header_parameters = {} # type: Dict[str, Any] header_parameters['Accept'] = self._serialize.header("accept", accept, 'str') request = self._client.delete(url, query_parameters, header_parameters) pipeline_response = self._client._pipeline.run(request, stream=False, **kwargs) response = pipeline_response.http_response if response.status_code not in [200, 202, 204]: map_error(status_code=response.status_code, response=response, error_map=error_map) raise HttpResponseError(response=response, error_format=ARMErrorFormat) if cls: return cls(pipeline_response, None, {}) _delete_initial.metadata = {'url': '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkSecurityGroups/{networkSecurityGroupName}/securityRules/{securityRuleName}'} # type: ignore def begin_delete( self, resource_group_name, # type: str network_security_group_name, # type: str security_rule_name, # type: str **kwargs # type: Any ): # type: (...) -> LROPoller[None] """Deletes the specified network security rule. :param resource_group_name: The name of the resource group. :type resource_group_name: str :param network_security_group_name: The name of the network security group. :type network_security_group_name: str :param security_rule_name: The name of the security rule. :type security_rule_name: str :keyword callable cls: A custom type or function that will be passed the direct response :keyword str continuation_token: A continuation token to restart a poller from a saved state. :keyword polling: True for ARMPolling, False for no polling, or a polling object for personal polling strategy :paramtype polling: bool or ~azure.core.polling.PollingMethod :keyword int polling_interval: Default waiting time between two polls for LRO operations if no Retry-After header is present. :return: An instance of LROPoller that returns either None or the result of cls(response) :rtype: ~azure.core.polling.LROPoller[None] :raises ~azure.core.exceptions.HttpResponseError: """ polling = kwargs.pop('polling', True) # type: Union[bool, PollingMethod] cls = kwargs.pop('cls', None) # type: ClsType[None] lro_delay = kwargs.pop( 'polling_interval', self._config.polling_interval ) cont_token = kwargs.pop('continuation_token', None) # type: Optional[str] if cont_token is None: raw_result = self._delete_initial( resource_group_name=resource_group_name, network_security_group_name=network_security_group_name, security_rule_name=security_rule_name, cls=lambda x,y,z: x, **kwargs ) kwargs.pop('error_map', None) kwargs.pop('content_type', None) def get_long_running_output(pipeline_response): if cls: return cls(pipeline_response, None, {}) if polling is True: polling_method = ARMPolling(lro_delay, lro_options={'final-state-via': 'location'}, **kwargs) elif polling is False: polling_method = NoPolling() else: polling_method = polling if cont_token: return LROPoller.from_continuation_token( polling_method=polling_method, continuation_token=cont_token, client=self._client, deserialization_callback=get_long_running_output ) else: return LROPoller(self._client, raw_result, get_long_running_output, polling_method) begin_delete.metadata = {'url': '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkSecurityGroups/{networkSecurityGroupName}/securityRules/{securityRuleName}'} # type: ignore def get( self, resource_group_name, # type: str network_security_group_name, # type: str security_rule_name, # type: str **kwargs # type: Any ): # type: (...) -> "models.SecurityRule" """Get the specified network security rule. :param resource_group_name: The name of the resource group. :type resource_group_name: str :param network_security_group_name: The name of the network security group. :type network_security_group_name: str :param security_rule_name: The name of the security rule. :type security_rule_name: str :keyword callable cls: A custom type or function that will be passed the direct response :return: SecurityRule, or the result of cls(response) :rtype: ~azure.mgmt.network.v2020_03_01.models.SecurityRule :raises: ~azure.core.exceptions.HttpResponseError """ cls = kwargs.pop('cls', None) # type: ClsType["models.SecurityRule"] error_map = { 401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError } error_map.update(kwargs.pop('error_map', {})) api_version = "2020-03-01" accept = "application/json" # Construct URL url = self.get.metadata['url'] # type: ignore path_format_arguments = { 'resourceGroupName': self._serialize.url("resource_group_name", resource_group_name, 'str'), 'networkSecurityGroupName': self._serialize.url("network_security_group_name", network_security_group_name, 'str'), 'securityRuleName': self._serialize.url("security_rule_name", security_rule_name, 'str'), 'subscriptionId': self._serialize.url("self._config.subscription_id", self._config.subscription_id, 'str'), } url = self._client.format_url(url, **path_format_arguments) # Construct parameters query_parameters = {} # type: Dict[str, Any] query_parameters['api-version'] = self._serialize.query("api_version", api_version, 'str') # Construct headers header_parameters = {} # type: Dict[str, Any] header_parameters['Accept'] = self._serialize.header("accept", accept, 'str') request = self._client.get(url, query_parameters, header_parameters) pipeline_response = self._client._pipeline.run(request, stream=False, **kwargs) response = pipeline_response.http_response if response.status_code not in [200]: map_error(status_code=response.status_code, response=response, error_map=error_map) raise HttpResponseError(response=response, error_format=ARMErrorFormat) deserialized = self._deserialize('SecurityRule', pipeline_response) if cls: return cls(pipeline_response, deserialized, {}) return deserialized get.metadata = {'url': '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkSecurityGroups/{networkSecurityGroupName}/securityRules/{securityRuleName}'} # type: ignore def _create_or_update_initial( self, resource_group_name, # type: str network_security_group_name, # type: str security_rule_name, # type: str security_rule_parameters, # type: "models.SecurityRule" **kwargs # type: Any ): # type: (...) -> "models.SecurityRule" cls = kwargs.pop('cls', None) # type: ClsType["models.SecurityRule"] error_map = { 401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError } error_map.update(kwargs.pop('error_map', {})) api_version = "2020-03-01" content_type = kwargs.pop("content_type", "application/json") accept = "application/json" # Construct URL url = self._create_or_update_initial.metadata['url'] # type: ignore path_format_arguments = { 'resourceGroupName': self._serialize.url("resource_group_name", resource_group_name, 'str'), 'networkSecurityGroupName': self._serialize.url("network_security_group_name", network_security_group_name, 'str'), 'securityRuleName': self._serialize.url("security_rule_name", security_rule_name, 'str'), 'subscriptionId': self._serialize.url("self._config.subscription_id", self._config.subscription_id, 'str'), } url = self._client.format_url(url, **path_format_arguments) # Construct parameters query_parameters = {} # type: Dict[str, Any] query_parameters['api-version'] = self._serialize.query("api_version", api_version, 'str') # Construct headers header_parameters = {} # type: Dict[str, Any] header_parameters['Content-Type'] = self._serialize.header("content_type", content_type, 'str') header_parameters['Accept'] = self._serialize.header("accept", accept, 'str') body_content_kwargs = {} # type: Dict[str, Any] body_content = self._serialize.body(security_rule_parameters, 'SecurityRule') body_content_kwargs['content'] = body_content request = self._client.put(url, query_parameters, header_parameters, **body_content_kwargs) pipeline_response = self._client._pipeline.run(request, stream=False, **kwargs) response = pipeline_response.http_response if response.status_code not in [200, 201]: map_error(status_code=response.status_code, response=response, error_map=error_map) raise HttpResponseError(response=response, error_format=ARMErrorFormat) if response.status_code == 200: deserialized = self._deserialize('SecurityRule', pipeline_response) if response.status_code == 201: deserialized = self._deserialize('SecurityRule', pipeline_response) if cls: return cls(pipeline_response, deserialized, {}) return deserialized _create_or_update_initial.metadata = {'url': '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkSecurityGroups/{networkSecurityGroupName}/securityRules/{securityRuleName}'} # type: ignore def begin_create_or_update( self, resource_group_name, # type: str network_security_group_name, # type: str security_rule_name, # type: str security_rule_parameters, # type: "models.SecurityRule" **kwargs # type: Any ): # type: (...) -> LROPoller["models.SecurityRule"] """Creates or updates a security rule in the specified network security group. :param resource_group_name: The name of the resource group. :type resource_group_name: str :param network_security_group_name: The name of the network security group. :type network_security_group_name: str :param security_rule_name: The name of the security rule. :type security_rule_name: str :param security_rule_parameters: Parameters supplied to the create or update network security rule operation. :type security_rule_parameters: ~azure.mgmt.network.v2020_03_01.models.SecurityRule :keyword callable cls: A custom type or function that will be passed the direct response :keyword str continuation_token: A continuation token to restart a poller from a saved state. :keyword polling: True for ARMPolling, False for no polling, or a polling object for personal polling strategy :paramtype polling: bool or ~azure.core.polling.PollingMethod :keyword int polling_interval: Default waiting time between two polls for LRO operations if no Retry-After header is present. :return: An instance of LROPoller that returns either SecurityRule or the result of cls(response) :rtype: ~azure.core.polling.LROPoller[~azure.mgmt.network.v2020_03_01.models.SecurityRule] :raises ~azure.core.exceptions.HttpResponseError: """ polling = kwargs.pop('polling', True) # type: Union[bool, PollingMethod] cls = kwargs.pop('cls', None) # type: ClsType["models.SecurityRule"] lro_delay = kwargs.pop( 'polling_interval', self._config.polling_interval ) cont_token = kwargs.pop('continuation_token', None) # type: Optional[str] if cont_token is None: raw_result = self._create_or_update_initial( resource_group_name=resource_group_name, network_security_group_name=network_security_group_name, security_rule_name=security_rule_name, security_rule_parameters=security_rule_parameters, cls=lambda x,y,z: x, **kwargs ) kwargs.pop('error_map', None) kwargs.pop('content_type', None) def get_long_running_output(pipeline_response): deserialized = self._deserialize('SecurityRule', pipeline_response) if cls: return cls(pipeline_response, deserialized, {}) return deserialized if polling is True: polling_method = ARMPolling(lro_delay, lro_options={'final-state-via': 'azure-async-operation'}, **kwargs) elif polling is False: polling_method = NoPolling() else: polling_method = polling if cont_token: return LROPoller.from_continuation_token( polling_method=polling_method, continuation_token=cont_token, client=self._client, deserialization_callback=get_long_running_output ) else: return LROPoller(self._client, raw_result, get_long_running_output, polling_method) begin_create_or_update.metadata = {'url': '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkSecurityGroups/{networkSecurityGroupName}/securityRules/{securityRuleName}'} # type: ignore def list( self, resource_group_name, # type: str network_security_group_name, # type: str **kwargs # type: Any ): # type: (...) -> Iterable["models.SecurityRuleListResult"] """Gets all security rules in a network security group. :param resource_group_name: The name of the resource group. :type resource_group_name: str :param network_security_group_name: The name of the network security group. :type network_security_group_name: str :keyword callable cls: A custom type or function that will be passed the direct response :return: An iterator like instance of either SecurityRuleListResult or the result of cls(response) :rtype: ~azure.core.paging.ItemPaged[~azure.mgmt.network.v2020_03_01.models.SecurityRuleListResult] :raises: ~azure.core.exceptions.HttpResponseError """ cls = kwargs.pop('cls', None) # type: ClsType["models.SecurityRuleListResult"] error_map = { 401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError } error_map.update(kwargs.pop('error_map', {})) api_version = "2020-03-01" accept = "application/json" def prepare_request(next_link=None): # Construct headers header_parameters = {} # type: Dict[str, Any] header_parameters['Accept'] = self._serialize.header("accept", accept, 'str') if not next_link: # Construct URL url = self.list.metadata['url'] # type: ignore path_format_arguments = { 'resourceGroupName': self._serialize.url("resource_group_name", resource_group_name, 'str'), 'networkSecurityGroupName': self._serialize.url("network_security_group_name", network_security_group_name, 'str'), 'subscriptionId': self._serialize.url("self._config.subscription_id", self._config.subscription_id, 'str'), } url = self._client.format_url(url, **path_format_arguments) # Construct parameters query_parameters = {} # type: Dict[str, Any] query_parameters['api-version'] = self._serialize.query("api_version", api_version, 'str') request = self._client.get(url, query_parameters, header_parameters) else: url = next_link query_parameters = {} # type: Dict[str, Any] request = self._client.get(url, query_parameters, header_parameters) return request def extract_data(pipeline_response): deserialized = self._deserialize('SecurityRuleListResult', pipeline_response) list_of_elem = deserialized.value if cls: list_of_elem = cls(list_of_elem) return deserialized.next_link or None, iter(list_of_elem) def get_next(next_link=None): request = prepare_request(next_link) pipeline_response = self._client._pipeline.run(request, stream=False, **kwargs) response = pipeline_response.http_response if response.status_code not in [200]: map_error(status_code=response.status_code, response=response, error_map=error_map) raise HttpResponseError(response=response, error_format=ARMErrorFormat) return pipeline_response return ItemPaged( get_next, extract_data ) list.metadata = {'url': '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkSecurityGroups/{networkSecurityGroupName}/securityRules'} # type: ignore
49.908879
244
0.671317
from typing import TYPE_CHECKING import warnings from azure.core.exceptions import ClientAuthenticationError, HttpResponseError, ResourceExistsError, ResourceNotFoundError, map_error from azure.core.paging import ItemPaged from azure.core.pipeline import PipelineResponse from azure.core.pipeline.transport import HttpRequest, HttpResponse from azure.core.polling import LROPoller, NoPolling, PollingMethod from azure.mgmt.core.exceptions import ARMErrorFormat from azure.mgmt.core.polling.arm_polling import ARMPolling from .. import models if TYPE_CHECKING: from typing import Any, Callable, Dict, Generic, Iterable, Optional, TypeVar, Union T = TypeVar('T') ClsType = Optional[Callable[[PipelineResponse[HttpRequest, HttpResponse], T, Dict[str, Any]], Any]] class SecurityRulesOperations(object): models = models def __init__(self, client, config, serializer, deserializer): self._client = client self._serialize = serializer self._deserialize = deserializer self._config = config def _delete_initial( self, resource_group_name, network_security_group_name, security_rule_name, **kwargs ): cls = kwargs.pop('cls', None) error_map = { 401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError } error_map.update(kwargs.pop('error_map', {})) api_version = "2020-03-01" accept = "application/json" url = self._delete_initial.metadata['url'] path_format_arguments = { 'resourceGroupName': self._serialize.url("resource_group_name", resource_group_name, 'str'), 'networkSecurityGroupName': self._serialize.url("network_security_group_name", network_security_group_name, 'str'), 'securityRuleName': self._serialize.url("security_rule_name", security_rule_name, 'str'), 'subscriptionId': self._serialize.url("self._config.subscription_id", self._config.subscription_id, 'str'), } url = self._client.format_url(url, **path_format_arguments) query_parameters = {} query_parameters['api-version'] = self._serialize.query("api_version", api_version, 'str') header_parameters = {} header_parameters['Accept'] = self._serialize.header("accept", accept, 'str') request = self._client.delete(url, query_parameters, header_parameters) pipeline_response = self._client._pipeline.run(request, stream=False, **kwargs) response = pipeline_response.http_response if response.status_code not in [200, 202, 204]: map_error(status_code=response.status_code, response=response, error_map=error_map) raise HttpResponseError(response=response, error_format=ARMErrorFormat) if cls: return cls(pipeline_response, None, {}) _delete_initial.metadata = {'url': '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkSecurityGroups/{networkSecurityGroupName}/securityRules/{securityRuleName}'} def begin_delete( self, resource_group_name, network_security_group_name, security_rule_name, **kwargs ): polling = kwargs.pop('polling', True) cls = kwargs.pop('cls', None) lro_delay = kwargs.pop( 'polling_interval', self._config.polling_interval ) cont_token = kwargs.pop('continuation_token', None) if cont_token is None: raw_result = self._delete_initial( resource_group_name=resource_group_name, network_security_group_name=network_security_group_name, security_rule_name=security_rule_name, cls=lambda x,y,z: x, **kwargs ) kwargs.pop('error_map', None) kwargs.pop('content_type', None) def get_long_running_output(pipeline_response): if cls: return cls(pipeline_response, None, {}) if polling is True: polling_method = ARMPolling(lro_delay, lro_options={'final-state-via': 'location'}, **kwargs) elif polling is False: polling_method = NoPolling() else: polling_method = polling if cont_token: return LROPoller.from_continuation_token( polling_method=polling_method, continuation_token=cont_token, client=self._client, deserialization_callback=get_long_running_output ) else: return LROPoller(self._client, raw_result, get_long_running_output, polling_method) begin_delete.metadata = {'url': '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkSecurityGroups/{networkSecurityGroupName}/securityRules/{securityRuleName}'} def get( self, resource_group_name, network_security_group_name, security_rule_name, **kwargs ): cls = kwargs.pop('cls', None) error_map = { 401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError } error_map.update(kwargs.pop('error_map', {})) api_version = "2020-03-01" accept = "application/json" url = self.get.metadata['url'] path_format_arguments = { 'resourceGroupName': self._serialize.url("resource_group_name", resource_group_name, 'str'), 'networkSecurityGroupName': self._serialize.url("network_security_group_name", network_security_group_name, 'str'), 'securityRuleName': self._serialize.url("security_rule_name", security_rule_name, 'str'), 'subscriptionId': self._serialize.url("self._config.subscription_id", self._config.subscription_id, 'str'), } url = self._client.format_url(url, **path_format_arguments) query_parameters = {} query_parameters['api-version'] = self._serialize.query("api_version", api_version, 'str') header_parameters = {} header_parameters['Accept'] = self._serialize.header("accept", accept, 'str') request = self._client.get(url, query_parameters, header_parameters) pipeline_response = self._client._pipeline.run(request, stream=False, **kwargs) response = pipeline_response.http_response if response.status_code not in [200]: map_error(status_code=response.status_code, response=response, error_map=error_map) raise HttpResponseError(response=response, error_format=ARMErrorFormat) deserialized = self._deserialize('SecurityRule', pipeline_response) if cls: return cls(pipeline_response, deserialized, {}) return deserialized get.metadata = {'url': '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkSecurityGroups/{networkSecurityGroupName}/securityRules/{securityRuleName}'} def _create_or_update_initial( self, resource_group_name, network_security_group_name, security_rule_name, security_rule_parameters, **kwargs ): cls = kwargs.pop('cls', None) error_map = { 401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError } error_map.update(kwargs.pop('error_map', {})) api_version = "2020-03-01" content_type = kwargs.pop("content_type", "application/json") accept = "application/json" url = self._create_or_update_initial.metadata['url'] path_format_arguments = { 'resourceGroupName': self._serialize.url("resource_group_name", resource_group_name, 'str'), 'networkSecurityGroupName': self._serialize.url("network_security_group_name", network_security_group_name, 'str'), 'securityRuleName': self._serialize.url("security_rule_name", security_rule_name, 'str'), 'subscriptionId': self._serialize.url("self._config.subscription_id", self._config.subscription_id, 'str'), } url = self._client.format_url(url, **path_format_arguments) query_parameters = {} query_parameters['api-version'] = self._serialize.query("api_version", api_version, 'str') header_parameters = {} header_parameters['Content-Type'] = self._serialize.header("content_type", content_type, 'str') header_parameters['Accept'] = self._serialize.header("accept", accept, 'str') body_content_kwargs = {} body_content = self._serialize.body(security_rule_parameters, 'SecurityRule') body_content_kwargs['content'] = body_content request = self._client.put(url, query_parameters, header_parameters, **body_content_kwargs) pipeline_response = self._client._pipeline.run(request, stream=False, **kwargs) response = pipeline_response.http_response if response.status_code not in [200, 201]: map_error(status_code=response.status_code, response=response, error_map=error_map) raise HttpResponseError(response=response, error_format=ARMErrorFormat) if response.status_code == 200: deserialized = self._deserialize('SecurityRule', pipeline_response) if response.status_code == 201: deserialized = self._deserialize('SecurityRule', pipeline_response) if cls: return cls(pipeline_response, deserialized, {}) return deserialized _create_or_update_initial.metadata = {'url': '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkSecurityGroups/{networkSecurityGroupName}/securityRules/{securityRuleName}'} def begin_create_or_update( self, resource_group_name, network_security_group_name, security_rule_name, security_rule_parameters, **kwargs ): polling = kwargs.pop('polling', True) cls = kwargs.pop('cls', None) lro_delay = kwargs.pop( 'polling_interval', self._config.polling_interval ) cont_token = kwargs.pop('continuation_token', None) if cont_token is None: raw_result = self._create_or_update_initial( resource_group_name=resource_group_name, network_security_group_name=network_security_group_name, security_rule_name=security_rule_name, security_rule_parameters=security_rule_parameters, cls=lambda x,y,z: x, **kwargs ) kwargs.pop('error_map', None) kwargs.pop('content_type', None) def get_long_running_output(pipeline_response): deserialized = self._deserialize('SecurityRule', pipeline_response) if cls: return cls(pipeline_response, deserialized, {}) return deserialized if polling is True: polling_method = ARMPolling(lro_delay, lro_options={'final-state-via': 'azure-async-operation'}, **kwargs) elif polling is False: polling_method = NoPolling() else: polling_method = polling if cont_token: return LROPoller.from_continuation_token( polling_method=polling_method, continuation_token=cont_token, client=self._client, deserialization_callback=get_long_running_output ) else: return LROPoller(self._client, raw_result, get_long_running_output, polling_method) begin_create_or_update.metadata = {'url': '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkSecurityGroups/{networkSecurityGroupName}/securityRules/{securityRuleName}'} def list( self, resource_group_name, network_security_group_name, **kwargs ): cls = kwargs.pop('cls', None) error_map = { 401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError } error_map.update(kwargs.pop('error_map', {})) api_version = "2020-03-01" accept = "application/json" def prepare_request(next_link=None): header_parameters = {} header_parameters['Accept'] = self._serialize.header("accept", accept, 'str') if not next_link: url = self.list.metadata['url'] path_format_arguments = { 'resourceGroupName': self._serialize.url("resource_group_name", resource_group_name, 'str'), 'networkSecurityGroupName': self._serialize.url("network_security_group_name", network_security_group_name, 'str'), 'subscriptionId': self._serialize.url("self._config.subscription_id", self._config.subscription_id, 'str'), } url = self._client.format_url(url, **path_format_arguments) query_parameters = {} query_parameters['api-version'] = self._serialize.query("api_version", api_version, 'str') request = self._client.get(url, query_parameters, header_parameters) else: url = next_link query_parameters = {} request = self._client.get(url, query_parameters, header_parameters) return request def extract_data(pipeline_response): deserialized = self._deserialize('SecurityRuleListResult', pipeline_response) list_of_elem = deserialized.value if cls: list_of_elem = cls(list_of_elem) return deserialized.next_link or None, iter(list_of_elem) def get_next(next_link=None): request = prepare_request(next_link) pipeline_response = self._client._pipeline.run(request, stream=False, **kwargs) response = pipeline_response.http_response if response.status_code not in [200]: map_error(status_code=response.status_code, response=response, error_map=error_map) raise HttpResponseError(response=response, error_format=ARMErrorFormat) return pipeline_response return ItemPaged( get_next, extract_data ) list.metadata = {'url': '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkSecurityGroups/{networkSecurityGroupName}/securityRules'}
true
true
f705f9481fb22578b16c347722b71f3a9d96e730
13,644
py
Python
app.py
okdefinet/oknodes
c74797f7d3b6a9ec690f0129ea5313d1afc2ae66
[ "MIT" ]
null
null
null
app.py
okdefinet/oknodes
c74797f7d3b6a9ec690f0129ea5313d1afc2ae66
[ "MIT" ]
null
null
null
app.py
okdefinet/oknodes
c74797f7d3b6a9ec690f0129ea5313d1afc2ae66
[ "MIT" ]
null
null
null
""" Open Nodes web server Copyright (c) 2018 Opennodes / Blake Bjorn Anderson 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 gzip import json import os import sys from io import BytesIO from flask import Flask, render_template, request, redirect, flash, Response from flask_sqlalchemy import SQLAlchemy from geoip2.errors import AddressNotFoundError from sqlalchemy import and_ from config import load_config, DefaultFlaskConfig from crawler import init_geoip, connect from models import * import pandas as pd from autodoc import Autodoc app = Flask(__name__) auto = Autodoc(app) app.config.from_object(DefaultFlaskConfig()) app.config.from_object('flask_config') db = SQLAlchemy(app) CONF = load_config() COUNTRY, CITY, ASN = init_geoip() @app.route('/') @app.route('/networks/<network_name>', methods=['GET']) def network_dashboard(network_name=None): if not network_name in ("okcash", "testnet", None): flash("Invalid network") return redirect("/") with open("static/network_summaries.json", 'r') as f: summaries = json.load(f) if network_name: age_min = summaries[network_name]['age_min'] age_max = summaries[network_name]['age_max'] else: age_min = min((summaries[network]['age_min'] for network in CONF['networks'])) age_max = max((summaries[network]['age_max'] for network in CONF['networks'])) return render_template("network_dashboard.html", network=network_name, include_client=False if network_name is not None else False, include_user_agent=True if network_name is not None else False, include_network=True if network_name is None else False, include_version=True if network_name is not None else False, include_active=True if CONF['export_inactive_nodes'] else False, age_min=age_min * 1000.0, age_max=age_max * 1000.0) def gzip_response(input_str, pre_compressed): response = Response() if not pre_compressed: buffer = BytesIO() gzip_file = gzip.GzipFile(mode='wb', fileobj=buffer) gzip_file.write(input_str if isinstance(input_str, bytes) else input_str.encode()) gzip_file.close() response.data = buffer.getvalue() else: response.data = input_str response.headers['Content-Encoding'] = 'gzip' response.headers['Vary'] = 'Accept-Encoding' response.headers['Content-Length'] = len(response.data) return response @app.route('/api/get_networks', methods=['POST']) @auto.doc() def get_networks(): """ Returns a list of all available network names :return: JSON string, ex. "['okcash','testnet']" """ return json.dumps([x[0] for x in db.session.query(Node.network).distinct().all()]) @app.route('/api/gzip_file/<filename>', methods=['GET']) @auto.doc() def gzip_static_file(filename): """ Returns a crawl result as a gzipped response :param filename: file_network.ext - file is 'data' or 'history', ext is either .json, .csv, .txt (data.ext returns data for all crawled networks) :return: gzip encoded html response """ valid_files = ["custom.geo.json"] for coin in ("", "_groestlcoin", "_testnet"): for suff in ("", "_unique"): for ext in (".csv", ".json", ".txt"): valid_files.append("data" + coin + suff + ext) valid_files.append("history" + coin + '.json') if filename not in valid_files: return redirect("/", code=404) with open(os.path.join("static", filename), "r") as f: return gzip_response(f.read(), False) def deconstruct_address_string(inp): assert isinstance(inp, str) resp = {} aliases = {'ok': 'okcash', 'tok': 'testnet'} inp = inp.lower() network = inp.split(":")[0] if network: inp = ":".join(inp.split(":")[1:]) network = aliases[network] if network in aliases else network network = network if network in CONF['networks'] else None if not network: network = "okcash" resp['warning'] = "Network not recognized, using OK" if ":" in inp: port = inp.split(":")[-1] try: port = int(port) inp = ":".join(inp.split(":")[:-1]) except ValueError: resp['warning'] = "port not recognized, using default" port = int(CONF['networks'][network]['port']) else: port = int(CONF['networks'][network]['port']) return network, inp, port, resp @app.route('/api/check_node', methods=['POST']) @auto.doc() def check_node(): """ Checks the current status of a node. This is a live result, so response times will be longer - to view a saved result see /api/check_historic_node. :param node: connection string, e.g. ok:127.0.0.1:6970 - port is optional if it is the network default :param to_services (integer, optional): outgoing services to broadcast, default=0 :param from_services (integer, optional): outgoing services to broadcast, default=0 :param version (integer, optional): version code to broadcast, default varies by network :param user_agent (string, optional): user agent to broadcast, default="/oknodes:0.1/" :param height (integer, optional): block height to broadcast during handshake. default=network median :param p2p_nodes (bool, optional): issues a getaddr call and list of connected nodes, default=False :return: json dict {"result":{"user_agent":"/oktoshi:5.0.0.2/", "version":" .... }, "nodes":[["127.0.0.1:6970, 157532132191], ...]} """ dat = request.form node = dat.get("node") network, address, port, resp = deconstruct_address_string(node) network_data = CONF['networks'][network] if dat.get("height"): network_data['height'] = dat.get("height") else: with open("static/network_summaries.json", 'r') as f: network_data['height'] = int(json.load(f)[network]['med']) network_data['protocol_version'] = dat.get("version") or network_data['protocol_version'] result = connect(network, address, port, to_services=dat.get("to_services") or network_data['services'], network_data=network_data, user_agent=dat.get("user_agent") or None, p2p_nodes=False, explicit_p2p=dat.get("p2p_nodes") or False, from_services=dat.get('from_services') or None, keepalive=False) resp['result'] = result[0] resp['nodes'] = result[1] resp['result'] = geocode(resp['result']) return to_json(resp) @app.route('/api/check_historic_node', methods=['POST', 'GET']) @auto.doc() def check_historic_node(): """ Checks the status of a node based on the last crawl result see /api/check_historical_node :param node: connection string, e.g. ok:127.0.0.1:6970 - port is optional if it is the network default :return: json dict {"result":{"user_agent":"/oktoshi:5.0.0.2/", "version":" .... }} """ if request.method == "POST": dat = request.form else: dat = request.args node = dat.get("node") network, address, port, resp = deconstruct_address_string(node) if network not in CONF['networks']: return json.dumps({'error': "network not recognized"}) result = db.session.query(Node).get((network, address, port)) resp['result'] = "None" if result is None else result.to_dict() return to_json(resp) @app.route("/about") def about(): return render_template("about.html") @app.route("/api_docs") def api_docs(): return auto.html() @app.route('/api/get_nodes', methods=['POST']) @auto.doc() def get_node_list(): """ Gets a list of all nodes visible during the past 30 days :param network (optional): Filters the result set based on the given network :return: json array [{"address":"127.0.0.1" ... }, {"address":"0.0.0.0", "port:6970}] """ q = db.session.query(Node.network, Node.address, Node.port, Node.user_agent, Node.version, Node.first_seen, Node.last_seen, Node.last_checked, Node.country, Node.city, Node.asn, Node.aso).filter( Node.seen) if request.args.get("network") is not None: network = request.args.get("network") if network not in CONF['networks']: return {"error": "network must be one of " + ", ".join(CONF['networks'])} q = q.filter(Node.network == network) return pd.read_sql(q.statement, q.session.bind).to_json(orient='records') @app.route('/api/node_history', methods=['POST']) @auto.doc() def get_node_history(): """ Returns the data associated with a node, and all crawler visitations on record :param node: connection string, e.g. ok:127.0.0.1:6970 - port is optional if it is the network default. :return: json dict {"node":{"user_agent":"/oktoshi/", "last_seen": ... }, "history":{"timestamp":157032190321,"height":56000, "success":1 ...}} """ node = request.form.get("node") network, address, port, resp = deconstruct_address_string(node) if network not in CONF['networks']: return json.dumps({'error': "network not recognized"}) default_port = int(CONF['networks'][network]['port']) resp = {} try: port = int(port) if port is not None else default_port except ValueError: resp['warning'] = "port not recognized, using default" port = default_port n = db.session.query(Node.network, Node.address, Node.port, Node.user_agent, Node.version, Node.first_seen, Node.last_seen, Node.last_checked, Node.country, Node.city, Node.asn, Node.aso) \ .filter(and_(Node.network == network, Node.address == address, Node.port == port)).one() q = db.session.query(NodeVisitation.timestamp, NodeVisitation.height, NodeVisitation.success) \ .join(Node, and_(Node.network == NodeVisitation.network, Node.address == NodeVisitation.address, Node.port == NodeVisitation.port)) \ .filter(and_(Node.network == network, Node.address == address, Node.port == port)) \ .order_by(NodeVisitation.timestamp.desc()) df = pd.read_sql(q.statement, q.session.bind) df['timestamp'] = df['timestamp'].astype(pd.np.int64) // 10 ** 9 resp.update({"node": {"network": n.network, 'address': n.address, "port": n.port, "user_agent": n.user_agent, "version": n.version, "first_seen": n.first_seen, "last_seen": n.last_seen, "last_checked": n.last_checked, "country": n.country, "city": n.city, "asn": n.asn, "aso": n.aso}, "history": df.to_dict(orient='records')}) return to_json(resp) def geocode(result): if result and result['address'].endswith('.onion'): aso, asn, country, city = "Anonymous", "Anonymous", "Anonymous", "Anonymous" elif result: try: aso = ASN.asn(result['address']).autonomous_system_organization asn = ASN.asn(result['address']).autonomous_system_number except AddressNotFoundError: aso = None asn = None try: country = COUNTRY.country(result['address']).country.name except AddressNotFoundError: country = None try: city = CITY.city(result['address']).city.name except AddressNotFoundError: city = None else: return result result['aso'] = aso result['asn'] = asn result['country'] = country result['city'] = city return result def clean_dates(d): for i in d: if isinstance(d[i], datetime.datetime): d[i] = d[i].timestamp() if isinstance(d[i], dict): d[i] = clean_dates(d[i]) return d def to_json(d): """ Sanitizes a dictionary - converts datetime.datetime instances to timestamps :param d: dictionary :return: json string """ d = clean_dates(d) return json.dumps(d) def main(): app.run("0.0.0.0", debug=False if "--prod" in sys.argv else True, port=8888 if "--prod" in sys.argv else 5000) # app.run("0.0.0.0", debug=False if "--prod" in sys.argv else True, port=443 if "--prod" in sys.argv else 5000, ssl_context=('/etc/letsencrypt/live/nodes.okcash.org/fullchain.pem', '/etc/letsencrypt/live/nodes.okcash.org/privkey.pem')) if __name__ == '__main__': main()
37.483516
239
0.639915
import gzip import json import os import sys from io import BytesIO from flask import Flask, render_template, request, redirect, flash, Response from flask_sqlalchemy import SQLAlchemy from geoip2.errors import AddressNotFoundError from sqlalchemy import and_ from config import load_config, DefaultFlaskConfig from crawler import init_geoip, connect from models import * import pandas as pd from autodoc import Autodoc app = Flask(__name__) auto = Autodoc(app) app.config.from_object(DefaultFlaskConfig()) app.config.from_object('flask_config') db = SQLAlchemy(app) CONF = load_config() COUNTRY, CITY, ASN = init_geoip() @app.route('/') @app.route('/networks/<network_name>', methods=['GET']) def network_dashboard(network_name=None): if not network_name in ("okcash", "testnet", None): flash("Invalid network") return redirect("/") with open("static/network_summaries.json", 'r') as f: summaries = json.load(f) if network_name: age_min = summaries[network_name]['age_min'] age_max = summaries[network_name]['age_max'] else: age_min = min((summaries[network]['age_min'] for network in CONF['networks'])) age_max = max((summaries[network]['age_max'] for network in CONF['networks'])) return render_template("network_dashboard.html", network=network_name, include_client=False if network_name is not None else False, include_user_agent=True if network_name is not None else False, include_network=True if network_name is None else False, include_version=True if network_name is not None else False, include_active=True if CONF['export_inactive_nodes'] else False, age_min=age_min * 1000.0, age_max=age_max * 1000.0) def gzip_response(input_str, pre_compressed): response = Response() if not pre_compressed: buffer = BytesIO() gzip_file = gzip.GzipFile(mode='wb', fileobj=buffer) gzip_file.write(input_str if isinstance(input_str, bytes) else input_str.encode()) gzip_file.close() response.data = buffer.getvalue() else: response.data = input_str response.headers['Content-Encoding'] = 'gzip' response.headers['Vary'] = 'Accept-Encoding' response.headers['Content-Length'] = len(response.data) return response @app.route('/api/get_networks', methods=['POST']) @auto.doc() def get_networks(): return json.dumps([x[0] for x in db.session.query(Node.network).distinct().all()]) @app.route('/api/gzip_file/<filename>', methods=['GET']) @auto.doc() def gzip_static_file(filename): valid_files = ["custom.geo.json"] for coin in ("", "_groestlcoin", "_testnet"): for suff in ("", "_unique"): for ext in (".csv", ".json", ".txt"): valid_files.append("data" + coin + suff + ext) valid_files.append("history" + coin + '.json') if filename not in valid_files: return redirect("/", code=404) with open(os.path.join("static", filename), "r") as f: return gzip_response(f.read(), False) def deconstruct_address_string(inp): assert isinstance(inp, str) resp = {} aliases = {'ok': 'okcash', 'tok': 'testnet'} inp = inp.lower() network = inp.split(":")[0] if network: inp = ":".join(inp.split(":")[1:]) network = aliases[network] if network in aliases else network network = network if network in CONF['networks'] else None if not network: network = "okcash" resp['warning'] = "Network not recognized, using OK" if ":" in inp: port = inp.split(":")[-1] try: port = int(port) inp = ":".join(inp.split(":")[:-1]) except ValueError: resp['warning'] = "port not recognized, using default" port = int(CONF['networks'][network]['port']) else: port = int(CONF['networks'][network]['port']) return network, inp, port, resp @app.route('/api/check_node', methods=['POST']) @auto.doc() def check_node(): dat = request.form node = dat.get("node") network, address, port, resp = deconstruct_address_string(node) network_data = CONF['networks'][network] if dat.get("height"): network_data['height'] = dat.get("height") else: with open("static/network_summaries.json", 'r') as f: network_data['height'] = int(json.load(f)[network]['med']) network_data['protocol_version'] = dat.get("version") or network_data['protocol_version'] result = connect(network, address, port, to_services=dat.get("to_services") or network_data['services'], network_data=network_data, user_agent=dat.get("user_agent") or None, p2p_nodes=False, explicit_p2p=dat.get("p2p_nodes") or False, from_services=dat.get('from_services') or None, keepalive=False) resp['result'] = result[0] resp['nodes'] = result[1] resp['result'] = geocode(resp['result']) return to_json(resp) @app.route('/api/check_historic_node', methods=['POST', 'GET']) @auto.doc() def check_historic_node(): if request.method == "POST": dat = request.form else: dat = request.args node = dat.get("node") network, address, port, resp = deconstruct_address_string(node) if network not in CONF['networks']: return json.dumps({'error': "network not recognized"}) result = db.session.query(Node).get((network, address, port)) resp['result'] = "None" if result is None else result.to_dict() return to_json(resp) @app.route("/about") def about(): return render_template("about.html") @app.route("/api_docs") def api_docs(): return auto.html() @app.route('/api/get_nodes', methods=['POST']) @auto.doc() def get_node_list(): q = db.session.query(Node.network, Node.address, Node.port, Node.user_agent, Node.version, Node.first_seen, Node.last_seen, Node.last_checked, Node.country, Node.city, Node.asn, Node.aso).filter( Node.seen) if request.args.get("network") is not None: network = request.args.get("network") if network not in CONF['networks']: return {"error": "network must be one of " + ", ".join(CONF['networks'])} q = q.filter(Node.network == network) return pd.read_sql(q.statement, q.session.bind).to_json(orient='records') @app.route('/api/node_history', methods=['POST']) @auto.doc() def get_node_history(): node = request.form.get("node") network, address, port, resp = deconstruct_address_string(node) if network not in CONF['networks']: return json.dumps({'error': "network not recognized"}) default_port = int(CONF['networks'][network]['port']) resp = {} try: port = int(port) if port is not None else default_port except ValueError: resp['warning'] = "port not recognized, using default" port = default_port n = db.session.query(Node.network, Node.address, Node.port, Node.user_agent, Node.version, Node.first_seen, Node.last_seen, Node.last_checked, Node.country, Node.city, Node.asn, Node.aso) \ .filter(and_(Node.network == network, Node.address == address, Node.port == port)).one() q = db.session.query(NodeVisitation.timestamp, NodeVisitation.height, NodeVisitation.success) \ .join(Node, and_(Node.network == NodeVisitation.network, Node.address == NodeVisitation.address, Node.port == NodeVisitation.port)) \ .filter(and_(Node.network == network, Node.address == address, Node.port == port)) \ .order_by(NodeVisitation.timestamp.desc()) df = pd.read_sql(q.statement, q.session.bind) df['timestamp'] = df['timestamp'].astype(pd.np.int64) // 10 ** 9 resp.update({"node": {"network": n.network, 'address': n.address, "port": n.port, "user_agent": n.user_agent, "version": n.version, "first_seen": n.first_seen, "last_seen": n.last_seen, "last_checked": n.last_checked, "country": n.country, "city": n.city, "asn": n.asn, "aso": n.aso}, "history": df.to_dict(orient='records')}) return to_json(resp) def geocode(result): if result and result['address'].endswith('.onion'): aso, asn, country, city = "Anonymous", "Anonymous", "Anonymous", "Anonymous" elif result: try: aso = ASN.asn(result['address']).autonomous_system_organization asn = ASN.asn(result['address']).autonomous_system_number except AddressNotFoundError: aso = None asn = None try: country = COUNTRY.country(result['address']).country.name except AddressNotFoundError: country = None try: city = CITY.city(result['address']).city.name except AddressNotFoundError: city = None else: return result result['aso'] = aso result['asn'] = asn result['country'] = country result['city'] = city return result def clean_dates(d): for i in d: if isinstance(d[i], datetime.datetime): d[i] = d[i].timestamp() if isinstance(d[i], dict): d[i] = clean_dates(d[i]) return d def to_json(d): d = clean_dates(d) return json.dumps(d) def main(): app.run("0.0.0.0", debug=False if "--prod" in sys.argv else True, port=8888 if "--prod" in sys.argv else 5000) if __name__ == '__main__': main()
true
true
f705fac03817f888ee9d6bdb2cb40d1264f925df
11,104
py
Python
ec.py
ryancdotorg/undercrypto2014
b8c8f8c0c989889cdef4a2eb5791250533629e3d
[ "BSD-2-Clause" ]
3
2015-04-02T14:16:50.000Z
2022-02-01T03:33:34.000Z
ec.py
ryancdotorg/undercrypto2014
b8c8f8c0c989889cdef4a2eb5791250533629e3d
[ "BSD-2-Clause" ]
null
null
null
ec.py
ryancdotorg/undercrypto2014
b8c8f8c0c989889cdef4a2eb5791250533629e3d
[ "BSD-2-Clause" ]
1
2021-01-02T20:07:38.000Z
2021-01-02T20:07:38.000Z
#!/usr/bin/python import hashlib, re, sys, os, base64, time, random, hmac # stripped down from https://github.com/vbuterin/pybitcointools/blob/master/bitcoin/main.py ### Elliptic curve parameters P = 2**256-2**32-2**9-2**8-2**7-2**6-2**4-1 N = 115792089237316195423570985008687907852837564279074904382605163141518161494337 A = 0 B = 7 H = 1 Gx = 55066263022277343669578718895168534326250603453777594175500187360389116729240 Gy = 32670510020758816978083085130507043184471273380659243275938904335757337482424 G = (Gx,Gy) ### Extended Euclidean Algorithm def inv(a,n): lm, hm = 1,0 low, high = a%n,n while low > 1: r = high/low nm, new = hm-lm*r, high-low*r lm, low, hm, high = nm, new, lm, low return lm % n ### Base switching def get_code_string(base): if base == 2: return '01' elif base == 10: return '0123456789' elif base == 16: return "0123456789abcdef" elif base == 58: return "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" elif base == 256: return ''.join([chr(x) for x in range(256)]) else: raise ValueError("Invalid base!") def encode(val,base,minlen=0): base, minlen = int(base), int(minlen) code_string = get_code_string(base) result = "" while val > 0: result = code_string[val % base] + result val /= base if len(result) < minlen: result = code_string[0]*(minlen-len(result))+result return result def decode(string,base): base = int(base) code_string = get_code_string(base) result = 0 if base == 16: string = string.lower() while len(string) > 0: result *= base result += code_string.find(string[0]) string = string[1:] return result def changebase(string,frm,to,minlen=0): return encode(decode(string,frm),to,minlen) ### Elliptic Curve functions def isinf(p): return p[0] == 0 and p[1] == 0 def base10_add(a,b): if isinf(a): return b[0],b[1] if isinf(b): return a[0],a[1] if a[0] == b[0]: if a[1] == b[1]: return base10_double((a[0],a[1])) else: return (0,0) m = ((b[1]-a[1]) * inv(b[0]-a[0],P)) % P x = (m*m-a[0]-b[0]) % P y = (m*(a[0]-x)-a[1]) % P return (x,y) def base10_double(a): if isinf(a): return (0,0) m = ((3*a[0]*a[0]+A)*inv(2*a[1],P)) % P x = (m*m-2*a[0]) % P y = (m*(a[0]-x)-a[1]) % P return (x,y) def base10_multiply(a,n): if isinf(a) or n == 0: return (0,0) if n == 1: return a if n < 0 or n >= N: return base10_multiply(a,n%N) if (n%2) == 0: return base10_double(base10_multiply(a,n/2)) if (n%2) == 1: return base10_add(base10_double(base10_multiply(a,n/2)),a) # Functions for handling pubkey and privkey formats def get_pubkey_format(pub): if isinstance(pub,(tuple,list)): return 'decimal' elif len(pub) == 65 and pub[0] == '\x04': return 'bin' elif len(pub) == 130 and pub[0:2] == '04': return 'hex' elif len(pub) == 33 and pub[0] in ['\x02','\x03']: return 'bin_compressed' elif len(pub) == 66 and pub[0:2] in ['02','03']: return 'hex_compressed' elif len(pub) == 64: return 'bin_electrum' elif len(pub) == 128: return 'hex_electrum' else: raise Exception("Pubkey not in recognized format") def encode_pubkey(pub,formt): if not isinstance(pub,(tuple,list)): pub = decode_pubkey(pub) if formt == 'decimal': return pub elif formt == 'bin': return '\x04' + encode(pub[0],256,32) + encode(pub[1],256,32) elif formt == 'bin_compressed': return chr(2+(pub[1]%2)) + encode(pub[0],256,32) elif formt == 'hex': return '04' + encode(pub[0],16,64) + encode(pub[1],16,64) elif formt == 'hex_compressed': return '0'+str(2+(pub[1]%2)) + encode(pub[0],16,64) elif formt == 'bin_electrum': return encode(pub[0],256,32) + encode(pub[1],256,32) elif formt == 'hex_electrum': return encode(pub[0],16,64) + encode(pub[1],16,64) else: raise Exception("Invalid format!") def decode_pubkey(pub,formt=None): if not formt: formt = get_pubkey_format(pub) if formt == 'decimal': return pub elif formt == 'bin': return (decode(pub[1:33],256),decode(pub[33:65],256)) elif formt == 'bin_compressed': x = decode(pub[1:33],256) beta = pow(x*x*x+7,(P+1)/4,P) y = (P-beta) if ((beta + ord(pub[0])) % 2) else beta return (x,y) elif formt == 'hex': return (decode(pub[2:66],16),decode(pub[66:130],16)) elif formt == 'hex_compressed': return decode_pubkey(pub.decode('hex'),'bin_compressed') elif formt == 'bin_electrum': return (decode(pub[:32],256),decode(pub[32:64],256)) elif formt == 'hex_electrum': return (decode(pub[:64],16),decode(pub[64:128],16)) else: raise Exception("Invalid format!") def get_privkey_format(priv): if isinstance(priv,(int,long)): return 'decimal' elif len(priv) == 32: return 'bin' elif len(priv) == 33: return 'bin_compressed' elif len(priv) == 64: return 'hex' elif len(priv) == 66: return 'hex_compressed' else: bin_p = b58check_to_bin(priv) if len(bin_p) == 32: return 'wif' elif len(bin_p) == 33: return 'wif_compressed' else: raise Exception("WIF does not represent privkey") def encode_privkey(priv,formt): if not isinstance(priv,(int,long)): return encode_privkey(decode_privkey(priv),formt) if formt == 'decimal': return priv elif formt == 'bin': return encode(priv,256,32) elif formt == 'bin_compressed': return encode(priv,256,32)+'\x01' elif formt == 'hex': return encode(priv,16,64) elif formt == 'hex_compressed': return encode(priv,16,64)+'01' else: raise Exception("Invalid format!") def decode_privkey(priv,formt=None): if not formt: formt = get_privkey_format(priv) if formt == 'decimal': return priv elif formt == 'bin': return decode(priv,256) elif formt == 'bin_compressed': return decode(priv[:32],256) elif formt == 'hex': return decode(priv,16) elif formt == 'hex_compressed': return decode(priv[:64],16) else: raise Exception("Invalid format!") def add_pubkeys(p1,p2): f1,f2 = get_pubkey_format(p1), get_pubkey_format(p2) return encode_pubkey(base10_add(decode_pubkey(p1,f1),decode_pubkey(p2,f2)),f1) def add_privkeys(p1,p2): f1,f2 = get_privkey_format(p1), get_privkey_format(p2) return encode_privkey((decode_privkey(p1,f1) + decode_privkey(p2,f2)) % N,f1) def multiply(pubkey,privkey): f1,f2 = get_pubkey_format(pubkey), get_privkey_format(privkey) pubkey, privkey = decode_pubkey(pubkey,f1), decode_privkey(privkey,f2) # http://safecurves.cr.yp.to/twist.html if not isinf(pubkey) and (pubkey[0]**3+7-pubkey[1]*pubkey[1]) % P != 0: raise Exception("Point not on curve") return encode_pubkey(base10_multiply(pubkey,privkey),f1) def divide(pubkey,privkey): factor = inv(decode_privkey(privkey),N) return multiply(pubkey,factor) def compress(pubkey): f = get_pubkey_format(pubkey) if 'compressed' in f: return pubkey elif f == 'bin': return encode_pubkey(decode_pubkey(pubkey,f),'bin_compressed') elif f == 'hex' or f == 'decimal': return encode_pubkey(decode_pubkey(pubkey,f),'hex_compressed') def decompress(pubkey): f = get_pubkey_format(pubkey) if 'compressed' not in f: return pubkey elif f == 'bin_compressed': return encode_pubkey(decode_pubkey(pubkey,f),'bin') elif f == 'hex_compressed' or f == 'decimal': return encode_pubkey(decode_pubkey(pubkey,f),'hex') def privkey_to_pubkey(privkey): f = get_privkey_format(privkey) privkey = decode_privkey(privkey,f) if privkey == 0 or privkey >= N: raise Exception("Invalid privkey") if f in ['bin','bin_compressed','hex','hex_compressed','decimal']: return encode_pubkey(base10_multiply(G,privkey),f) else: return encode_pubkey(base10_multiply(G,privkey),f.replace('wif','hex')) privtopub = privkey_to_pubkey def privkey_to_address(priv,magicbyte=0): return pubkey_to_address(privkey_to_pubkey(priv),magicbyte) privtoaddr = privkey_to_address def neg_pubkey(pubkey): f = get_pubkey_format(pubkey) pubkey = decode_pubkey(pubkey,f) return encode_pubkey((pubkey[0],(P-pubkey[1]) % P),f) def neg_privkey(privkey): f = get_privkey_format(privkey) privkey = decode_privkey(privkey,f) return encode_privkey((N - privkey) % N,f) def subtract_pubkeys(p1, p2): f1,f2 = get_pubkey_format(p1), get_pubkey_format(p2) k2 = decode_pubkey(p2,f2) return encode_pubkey(base10_add(decode_pubkey(p1,f1),(k2[0],(P - k2[1]) % P)),f1) def subtract_privkeys(p1, p2): f1,f2 = get_privkey_format(p1), get_privkey_format(p2) k2 = decode_privkey(p2,f2) return encode_privkey((decode_privkey(p1,f1) - k2) % N,f1) ### Hashes def bin_sha256(string): return hashlib.sha256(string).digest() def sha256(string): return bin_sha256(string).encode('hex') def hash_to_int(x): if len(x) in [40,64]: return decode(x,16) else: return decode(x,256) def num_to_var_int(x): x = int(x) if x < 253: return chr(x) elif x < 65536: return chr(253) + encode(x,256,2)[::-1] elif x < 4294967296: return chr(254) + encode(x,256,4)[::-1] else: return chr(255) + encode(x,256,8)[::-1] ### Encodings ### EDCSA def encode_sig(v,r,s): vb, rb, sb = chr(v), encode(r,256), encode(s,256) return base64.b64encode(vb+'\x00'*(32-len(rb))+rb+'\x00'*(32-len(sb))+sb) def decode_sig(sig): bytez = base64.b64decode(sig) return ord(bytez[0]), decode(bytez[1:33],256), decode(bytez[33:],256) # https://tools.ietf.org/html/rfc6979#section-3.2 def deterministic_generate_k(msghash,priv): v = '\x01' * 32 k = '\x00' * 32 priv = encode_privkey(priv,'bin') msghash = encode(hash_to_int(msghash),256,32) k = hmac.new(k, v+'\x00'+priv+msghash, hashlib.sha256).digest() v = hmac.new(k, v, hashlib.sha256).digest() k = hmac.new(k, v+'\x01'+priv+msghash, hashlib.sha256).digest() v = hmac.new(k, v, hashlib.sha256).digest() return decode(hmac.new(k, v, hashlib.sha256).digest(),256) def ecdsa_raw_sign(msghash,priv): z = hash_to_int(msghash) k = deterministic_generate_k(msghash,priv) r,y = base10_multiply(G,k) s = inv(k,N) * (z + r*decode_privkey(priv)) % N return 27+(y%2),r,s def ecdsa_sign(msg,priv): return encode_sig(*ecdsa_raw_sign(electrum_sig_hash(msg),priv)) def ecdsa_raw_verify(msghash,vrs,pub): v,r,s = vrs w = inv(s,N) z = hash_to_int(msghash) u1, u2 = z*w % N, r*w % N x,y = base10_add(base10_multiply(G,u1), base10_multiply(decode_pubkey(pub),u2)) return r == x def ecdsa_verify(msg,sig,pub): return ecdsa_raw_verify(electrum_sig_hash(msg),decode_sig(sig),pub) def ecdsa_raw_recover(msghash,vrs): v,r,s = vrs x = r beta = pow(x*x*x+7,(P+1)/4,P) y = beta if v%2 ^ beta%2 else (P - beta) z = hash_to_int(msghash) Qr = base10_add(neg_pubkey(base10_multiply(G,z)),base10_multiply((x,y),s)) Q = base10_multiply(Qr,inv(r,N)) if ecdsa_raw_verify(msghash,vrs,Q): return encode_pubkey(Q,'hex') return False def ecdsa_recover(msg,sig): return ecdsa_raw_recover(electrum_sig_hash(msg),decode_sig(sig))
34.5919
91
0.659492
import hashlib, re, sys, os, base64, time, random, hmac P = 2**256-2**32-2**9-2**8-2**7-2**6-2**4-1 N = 115792089237316195423570985008687907852837564279074904382605163141518161494337 A = 0 B = 7 H = 1 Gx = 55066263022277343669578718895168534326250603453777594175500187360389116729240 Gy = 32670510020758816978083085130507043184471273380659243275938904335757337482424 G = (Gx,Gy) def inv(a,n): lm, hm = 1,0 low, high = a%n,n while low > 1: r = high/low nm, new = hm-lm*r, high-low*r lm, low, hm, high = nm, new, lm, low return lm % n def get_code_string(base): if base == 2: return '01' elif base == 10: return '0123456789' elif base == 16: return "0123456789abcdef" elif base == 58: return "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" elif base == 256: return ''.join([chr(x) for x in range(256)]) else: raise ValueError("Invalid base!") def encode(val,base,minlen=0): base, minlen = int(base), int(minlen) code_string = get_code_string(base) result = "" while val > 0: result = code_string[val % base] + result val /= base if len(result) < minlen: result = code_string[0]*(minlen-len(result))+result return result def decode(string,base): base = int(base) code_string = get_code_string(base) result = 0 if base == 16: string = string.lower() while len(string) > 0: result *= base result += code_string.find(string[0]) string = string[1:] return result def changebase(string,frm,to,minlen=0): return encode(decode(string,frm),to,minlen) def isinf(p): return p[0] == 0 and p[1] == 0 def base10_add(a,b): if isinf(a): return b[0],b[1] if isinf(b): return a[0],a[1] if a[0] == b[0]: if a[1] == b[1]: return base10_double((a[0],a[1])) else: return (0,0) m = ((b[1]-a[1]) * inv(b[0]-a[0],P)) % P x = (m*m-a[0]-b[0]) % P y = (m*(a[0]-x)-a[1]) % P return (x,y) def base10_double(a): if isinf(a): return (0,0) m = ((3*a[0]*a[0]+A)*inv(2*a[1],P)) % P x = (m*m-2*a[0]) % P y = (m*(a[0]-x)-a[1]) % P return (x,y) def base10_multiply(a,n): if isinf(a) or n == 0: return (0,0) if n == 1: return a if n < 0 or n >= N: return base10_multiply(a,n%N) if (n%2) == 0: return base10_double(base10_multiply(a,n/2)) if (n%2) == 1: return base10_add(base10_double(base10_multiply(a,n/2)),a) def get_pubkey_format(pub): if isinstance(pub,(tuple,list)): return 'decimal' elif len(pub) == 65 and pub[0] == '\x04': return 'bin' elif len(pub) == 130 and pub[0:2] == '04': return 'hex' elif len(pub) == 33 and pub[0] in ['\x02','\x03']: return 'bin_compressed' elif len(pub) == 66 and pub[0:2] in ['02','03']: return 'hex_compressed' elif len(pub) == 64: return 'bin_electrum' elif len(pub) == 128: return 'hex_electrum' else: raise Exception("Pubkey not in recognized format") def encode_pubkey(pub,formt): if not isinstance(pub,(tuple,list)): pub = decode_pubkey(pub) if formt == 'decimal': return pub elif formt == 'bin': return '\x04' + encode(pub[0],256,32) + encode(pub[1],256,32) elif formt == 'bin_compressed': return chr(2+(pub[1]%2)) + encode(pub[0],256,32) elif formt == 'hex': return '04' + encode(pub[0],16,64) + encode(pub[1],16,64) elif formt == 'hex_compressed': return '0'+str(2+(pub[1]%2)) + encode(pub[0],16,64) elif formt == 'bin_electrum': return encode(pub[0],256,32) + encode(pub[1],256,32) elif formt == 'hex_electrum': return encode(pub[0],16,64) + encode(pub[1],16,64) else: raise Exception("Invalid format!") def decode_pubkey(pub,formt=None): if not formt: formt = get_pubkey_format(pub) if formt == 'decimal': return pub elif formt == 'bin': return (decode(pub[1:33],256),decode(pub[33:65],256)) elif formt == 'bin_compressed': x = decode(pub[1:33],256) beta = pow(x*x*x+7,(P+1)/4,P) y = (P-beta) if ((beta + ord(pub[0])) % 2) else beta return (x,y) elif formt == 'hex': return (decode(pub[2:66],16),decode(pub[66:130],16)) elif formt == 'hex_compressed': return decode_pubkey(pub.decode('hex'),'bin_compressed') elif formt == 'bin_electrum': return (decode(pub[:32],256),decode(pub[32:64],256)) elif formt == 'hex_electrum': return (decode(pub[:64],16),decode(pub[64:128],16)) else: raise Exception("Invalid format!") def get_privkey_format(priv): if isinstance(priv,(int,long)): return 'decimal' elif len(priv) == 32: return 'bin' elif len(priv) == 33: return 'bin_compressed' elif len(priv) == 64: return 'hex' elif len(priv) == 66: return 'hex_compressed' else: bin_p = b58check_to_bin(priv) if len(bin_p) == 32: return 'wif' elif len(bin_p) == 33: return 'wif_compressed' else: raise Exception("WIF does not represent privkey") def encode_privkey(priv,formt): if not isinstance(priv,(int,long)): return encode_privkey(decode_privkey(priv),formt) if formt == 'decimal': return priv elif formt == 'bin': return encode(priv,256,32) elif formt == 'bin_compressed': return encode(priv,256,32)+'\x01' elif formt == 'hex': return encode(priv,16,64) elif formt == 'hex_compressed': return encode(priv,16,64)+'01' else: raise Exception("Invalid format!") def decode_privkey(priv,formt=None): if not formt: formt = get_privkey_format(priv) if formt == 'decimal': return priv elif formt == 'bin': return decode(priv,256) elif formt == 'bin_compressed': return decode(priv[:32],256) elif formt == 'hex': return decode(priv,16) elif formt == 'hex_compressed': return decode(priv[:64],16) else: raise Exception("Invalid format!") def add_pubkeys(p1,p2): f1,f2 = get_pubkey_format(p1), get_pubkey_format(p2) return encode_pubkey(base10_add(decode_pubkey(p1,f1),decode_pubkey(p2,f2)),f1) def add_privkeys(p1,p2): f1,f2 = get_privkey_format(p1), get_privkey_format(p2) return encode_privkey((decode_privkey(p1,f1) + decode_privkey(p2,f2)) % N,f1) def multiply(pubkey,privkey): f1,f2 = get_pubkey_format(pubkey), get_privkey_format(privkey) pubkey, privkey = decode_pubkey(pubkey,f1), decode_privkey(privkey,f2) if not isinf(pubkey) and (pubkey[0]**3+7-pubkey[1]*pubkey[1]) % P != 0: raise Exception("Point not on curve") return encode_pubkey(base10_multiply(pubkey,privkey),f1) def divide(pubkey,privkey): factor = inv(decode_privkey(privkey),N) return multiply(pubkey,factor) def compress(pubkey): f = get_pubkey_format(pubkey) if 'compressed' in f: return pubkey elif f == 'bin': return encode_pubkey(decode_pubkey(pubkey,f),'bin_compressed') elif f == 'hex' or f == 'decimal': return encode_pubkey(decode_pubkey(pubkey,f),'hex_compressed') def decompress(pubkey): f = get_pubkey_format(pubkey) if 'compressed' not in f: return pubkey elif f == 'bin_compressed': return encode_pubkey(decode_pubkey(pubkey,f),'bin') elif f == 'hex_compressed' or f == 'decimal': return encode_pubkey(decode_pubkey(pubkey,f),'hex') def privkey_to_pubkey(privkey): f = get_privkey_format(privkey) privkey = decode_privkey(privkey,f) if privkey == 0 or privkey >= N: raise Exception("Invalid privkey") if f in ['bin','bin_compressed','hex','hex_compressed','decimal']: return encode_pubkey(base10_multiply(G,privkey),f) else: return encode_pubkey(base10_multiply(G,privkey),f.replace('wif','hex')) privtopub = privkey_to_pubkey def privkey_to_address(priv,magicbyte=0): return pubkey_to_address(privkey_to_pubkey(priv),magicbyte) privtoaddr = privkey_to_address def neg_pubkey(pubkey): f = get_pubkey_format(pubkey) pubkey = decode_pubkey(pubkey,f) return encode_pubkey((pubkey[0],(P-pubkey[1]) % P),f) def neg_privkey(privkey): f = get_privkey_format(privkey) privkey = decode_privkey(privkey,f) return encode_privkey((N - privkey) % N,f) def subtract_pubkeys(p1, p2): f1,f2 = get_pubkey_format(p1), get_pubkey_format(p2) k2 = decode_pubkey(p2,f2) return encode_pubkey(base10_add(decode_pubkey(p1,f1),(k2[0],(P - k2[1]) % P)),f1) def subtract_privkeys(p1, p2): f1,f2 = get_privkey_format(p1), get_privkey_format(p2) k2 = decode_privkey(p2,f2) return encode_privkey((decode_privkey(p1,f1) - k2) % N,f1) def bin_sha256(string): return hashlib.sha256(string).digest() def sha256(string): return bin_sha256(string).encode('hex') def hash_to_int(x): if len(x) in [40,64]: return decode(x,16) else: return decode(x,256) def num_to_var_int(x): x = int(x) if x < 253: return chr(x) elif x < 65536: return chr(253) + encode(x,256,2)[::-1] elif x < 4294967296: return chr(254) + encode(x,256,4)[::-1] else: return chr(255) + encode(x,256,8)[::-1] def encode_sig(v,r,s): vb, rb, sb = chr(v), encode(r,256), encode(s,256) return base64.b64encode(vb+'\x00'*(32-len(rb))+rb+'\x00'*(32-len(sb))+sb) def decode_sig(sig): bytez = base64.b64decode(sig) return ord(bytez[0]), decode(bytez[1:33],256), decode(bytez[33:],256) def deterministic_generate_k(msghash,priv): v = '\x01' * 32 k = '\x00' * 32 priv = encode_privkey(priv,'bin') msghash = encode(hash_to_int(msghash),256,32) k = hmac.new(k, v+'\x00'+priv+msghash, hashlib.sha256).digest() v = hmac.new(k, v, hashlib.sha256).digest() k = hmac.new(k, v+'\x01'+priv+msghash, hashlib.sha256).digest() v = hmac.new(k, v, hashlib.sha256).digest() return decode(hmac.new(k, v, hashlib.sha256).digest(),256) def ecdsa_raw_sign(msghash,priv): z = hash_to_int(msghash) k = deterministic_generate_k(msghash,priv) r,y = base10_multiply(G,k) s = inv(k,N) * (z + r*decode_privkey(priv)) % N return 27+(y%2),r,s def ecdsa_sign(msg,priv): return encode_sig(*ecdsa_raw_sign(electrum_sig_hash(msg),priv)) def ecdsa_raw_verify(msghash,vrs,pub): v,r,s = vrs w = inv(s,N) z = hash_to_int(msghash) u1, u2 = z*w % N, r*w % N x,y = base10_add(base10_multiply(G,u1), base10_multiply(decode_pubkey(pub),u2)) return r == x def ecdsa_verify(msg,sig,pub): return ecdsa_raw_verify(electrum_sig_hash(msg),decode_sig(sig),pub) def ecdsa_raw_recover(msghash,vrs): v,r,s = vrs x = r beta = pow(x*x*x+7,(P+1)/4,P) y = beta if v%2 ^ beta%2 else (P - beta) z = hash_to_int(msghash) Qr = base10_add(neg_pubkey(base10_multiply(G,z)),base10_multiply((x,y),s)) Q = base10_multiply(Qr,inv(r,N)) if ecdsa_raw_verify(msghash,vrs,Q): return encode_pubkey(Q,'hex') return False def ecdsa_recover(msg,sig): return ecdsa_raw_recover(electrum_sig_hash(msg),decode_sig(sig))
true
true
f705facbfc717526b824ea5eecc1a85c798bafbf
437
py
Python
packaging/setup/plugins/ovirt-engine-setup/vmconsole_proxy_helper/__init__.py
jihwahn1018/ovirt-engine
5c8a3d9a9637eefb28e4accc3cbd2b7f530d5ec9
[ "Apache-2.0" ]
347
2015-01-20T14:13:21.000Z
2022-03-31T17:53:11.000Z
packaging/setup/plugins/ovirt-engine-setup/vmconsole_proxy_helper/__init__.py
jihwahn1018/ovirt-engine
5c8a3d9a9637eefb28e4accc3cbd2b7f530d5ec9
[ "Apache-2.0" ]
128
2015-05-22T19:14:32.000Z
2022-03-31T08:11:18.000Z
packaging/setup/plugins/ovirt-engine-setup/vmconsole_proxy_helper/__init__.py
jihwahn1018/ovirt-engine
5c8a3d9a9637eefb28e4accc3cbd2b7f530d5ec9
[ "Apache-2.0" ]
202
2015-01-04T06:20:49.000Z
2022-03-08T15:30:08.000Z
# # ovirt-engine-setup -- ovirt engine setup # # Copyright oVirt Authors # SPDX-License-Identifier: Apache-2.0 # # """ovirt-host-setup vmconsole_proxy plugin.""" from otopi import util from . import config from . import pki from . import system @util.export def createPlugins(context): config.Plugin(context=context) pki.Plugin(context=context) system.Plugin(context=context) # vim: expandtab tabstop=4 shiftwidth=4
15.607143
46
0.732265
from otopi import util from . import config from . import pki from . import system @util.export def createPlugins(context): config.Plugin(context=context) pki.Plugin(context=context) system.Plugin(context=context)
true
true
f705fc436eda67eb2850c5e8b81bdabc6b0593a9
1,227
py
Python
st2client/st2client/models/policy.py
totalkyos/stack-storm
b89bc648d53dae03c7484d22abd771edfe45bbb8
[ "Apache-2.0" ]
1
2020-11-21T10:11:25.000Z
2020-11-21T10:11:25.000Z
st2client/st2client/models/policy.py
totalkyos/stack-storm
b89bc648d53dae03c7484d22abd771edfe45bbb8
[ "Apache-2.0" ]
1
2015-06-08T15:27:11.000Z
2015-06-08T15:27:11.000Z
st2client/st2client/models/policy.py
totalkyos/stack-storm
b89bc648d53dae03c7484d22abd771edfe45bbb8
[ "Apache-2.0" ]
13
2017-01-12T11:07:20.000Z
2019-04-19T09:55:49.000Z
# Licensed to the StackStorm, Inc ('StackStorm') 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. import logging from st2client.models import core LOG = logging.getLogger(__name__) class PolicyType(core.Resource): _alias = 'Policy-Type' _display_name = 'Policy type' _plural = 'PolicyTypes' _plural_display_name = 'Policy types' _repr_attributes = ['ref', 'enabled', 'description'] class Policy(core.Resource): _plural = 'Policies' _repr_attributes = ['name', 'pack', 'enabled', 'policy_type', 'resource_ref']
35.057143
81
0.746536
import logging from st2client.models import core LOG = logging.getLogger(__name__) class PolicyType(core.Resource): _alias = 'Policy-Type' _display_name = 'Policy type' _plural = 'PolicyTypes' _plural_display_name = 'Policy types' _repr_attributes = ['ref', 'enabled', 'description'] class Policy(core.Resource): _plural = 'Policies' _repr_attributes = ['name', 'pack', 'enabled', 'policy_type', 'resource_ref']
true
true
f705fc879c5006503f618f69c586ef4592be2ca0
8,442
py
Python
kivycalendar/calendar_ui.py
yumoqing/kivycalendar
6297e5094d994b8eca49515c0d61328c0545d5ef
[ "MIT" ]
1
2019-12-09T16:49:16.000Z
2019-12-09T16:49:16.000Z
kivycalendar/calendar_ui.py
yumoqing/kivycalendar
6297e5094d994b8eca49515c0d61328c0545d5ef
[ "MIT" ]
null
null
null
kivycalendar/calendar_ui.py
yumoqing/kivycalendar
6297e5094d994b8eca49515c0d61328c0545d5ef
[ "MIT" ]
1
2019-12-09T16:50:03.000Z
2019-12-09T16:50:03.000Z
#!/usr/bin/python # -*- coding: utf-8 -*- ########################################################### # KivyCalendar (X11/MIT License) # Calendar & Date picker widgets for Kivy (http://kivy.org) # https://bitbucket.org/xxblx/kivycalendar # # Oleg Kozlov (xxblx), 2015 # https://xxblx.bitbucket.org/ ########################################################### from kivy.lang import Builder from kivy.uix.screenmanager import ScreenManager, Screen from kivy.uix.popup import Popup from kivy.uix.relativelayout import RelativeLayout from kivy.uix.gridlayout import GridLayout from kivy.uix.button import Button from kivy.uix.togglebutton import ToggleButton from kivy.uix.textinput import TextInput from kivy.uix.label import Label from kivy.core.window import Window from kivy.factory import Factory from kivy.properties import NumericProperty, ReferenceListProperty from kivyblocks.i18n import I18n from . import calendar_data as cal_data ########################################################### Builder.load_string(""" <ArrowButton>: background_normal: "" background_down: "" background_color: 1, 1, 1, 0 size_hint: .1, .1 <MonthYearLabel>: pos_hint: {"top": 1, "center_x": .5} size_hint: None, 0.1 halign: "center" <MonthsManager>: pos_hint: {"top": .9} size_hint: 1, .9 <ButtonsGrid>: cols: 7 rows: 7 size_hint: 1, 1 pos_hint: {"top": 1} <DayAbbrLabel>: text_size: self.size[0], None halign: "center" <DayAbbrWeekendLabel>: color: 1, 0, 0, 1 <DayButton>: group: "day_num" <DayNumWeekendButton>: background_color: 1, 0, 0, 1 """) ########################################################### class DatePicker(TextInput): """ Date picker is a textinput, if it focused shows popup with calendar which allows you to define the popup dimensions using pHint_x, pHint_y, and the pHint lists, for example in kv: DatePicker: pHint: 0.7,0.4 would result in a size_hint of 0.7,0.4 being used to create the popup """ def __init__(self, touch_switch=False, value=None, *args, **kwargs): super(DatePicker, self).__init__(*args, **kwargs) self.touch_switch = touch_switch self.init_ui(value) def getValue(self): return self.text def setValue(self, sdate): self.text = sdate d = [int(i) for i in sdate.split('-')] d.reverse() self.cal.active_date = d def init_ui(self, value): if not value: value = cal_data.today_date() d = [int(i) for i in value.split('.')] value = '%04d-%02d-%02d' % (d[2],d[1],d[0]) # Calendar self.cal = CalendarWidget(as_popup=True, touch_switch=self.touch_switch) self.setValue(value) # Popup self.popup = Popup(content=self.cal, on_dismiss=self.update_value, size_hint=(0.7,0.7), title="") self.cal.parent_popup = self.popup self.bind(focus=self.show_popup) def show_popup(self, isnt, val): """ Open popup if textinput focused, and regardless update the popup size_hint """ if val: # Automatically dismiss the keyboard # that results from the textInput Window.release_all_keyboards() self.popup.open() def update_value(self, inst): """ Update textinput value on popup close """ d = self.cal.active_date self.text = "%04d-%02d-%02d" % (d[2],d[1],d[0]) self.focus = False class CalendarWidget(RelativeLayout): """ Basic calendar widget """ def __init__(self, as_popup=False, touch_switch=False, *args, **kwargs): super(CalendarWidget, self).__init__(*args, **kwargs) self.i18n = I18n() self.as_popup = as_popup self.touch_switch = touch_switch self.prepare_data() self.init_ui() def init_ui(self): self.left_arrow = ArrowButton(text="<", on_press=self.go_prev, pos_hint={"top": 1, "left": 0}) self.right_arrow = ArrowButton(text=">", on_press=self.go_next, pos_hint={"top": 1, "right": 1}) self.add_widget(self.left_arrow) self.add_widget(self.right_arrow) # Title self.title_label = MonthYearLabel(text=self.title) self.add_widget(self.title_label) # ScreenManager self.sm = MonthsManager() self.add_widget(self.sm) self.create_month_scr(self.quarter[1], toogle_today=True) def create_month_scr(self, month, toogle_today=False): """ Screen with calendar for one month """ scr = Screen() m = self.month_names_eng[self.active_date[1] - 1] scr.name = "%s-%s" % (m, self.active_date[2]) # like march-2015 # Grid for days grid_layout = ButtonsGrid() scr.add_widget(grid_layout) # Days abbrs for i in range(7): if i >= 5: # weekends #l = DayAbbrWeekendLabel(text=self.days_abrs[i]) l = Factory.Text(text=self.days_abrs[i], i18n=True) else: # work days #l = DayAbbrLabel(text=self.days_abrs[i]) l = Factory.Text(text=self.days_abrs[i], i18n=True) grid_layout.add_widget(l) # Buttons with days numbers for week in month: for day in week: if day[1] >= 5: # weekends tbtn = DayNumWeekendButton(text=str(day[0])) else: # work days tbtn = DayNumButton(text=str(day[0])) tbtn.bind(on_press=self.get_btn_value) if toogle_today: # Down today button if day[0] == self.active_date[0] and day[2] == 1: tbtn.state = "down" # Disable buttons with days from other months if day[2] == 0: tbtn.disabled = True grid_layout.add_widget(tbtn) self.sm.add_widget(scr) def prepare_data(self): """ Prepare data for showing on widget loading """ # Get days abbrs and month names lists self.month_names = cal_data.get_month_names() self.month_names_eng = cal_data.get_month_names_eng() self.days_abrs = cal_data.get_days_abbrs() # Today date self.active_date = cal_data.today_date_list() # Set title self.title = "%s - %s" % (self.i18n(self.month_names[self.active_date[1] - 1]), self.active_date[2]) # Quarter where current month in the self.quarter[1] self.get_quarter() def get_quarter(self): """ Get caledar and months/years nums for quarter """ self.quarter_nums = cal_data.calc_quarter(self.active_date[2], self.active_date[1]) self.quarter = cal_data.get_quarter(self.active_date[2], self.active_date[1]) def get_btn_value(self, inst): """ Get day value from pressed button """ self.active_date[0] = int(inst.text) if self.as_popup: self.parent_popup.dismiss() def go_prev(self, inst): """ Go to screen with previous month """ # Change active date self.active_date = [self.active_date[0], self.quarter_nums[0][1], self.quarter_nums[0][0]] # Name of prev screen n = self.quarter_nums[0][1] - 1 prev_scr_name = "%s-%s" % (self.month_names_eng[n], self.quarter_nums[0][0]) # If it's doen't exitst, create it if not self.sm.has_screen(prev_scr_name): self.create_month_scr(self.quarter[0]) self.sm.current = prev_scr_name self.sm.transition.direction = "right" self.get_quarter() self.title = "%s - %s" % (self.i18n(self.month_names[self.active_date[1] - 1]), self.active_date[2]) self.title_label.text = self.title def go_next(self, inst): """ Go to screen with next month """ # Change active date self.active_date = [self.active_date[0], self.quarter_nums[2][1], self.quarter_nums[2][0]] # Name of prev screen n = self.quarter_nums[2][1] - 1 next_scr_name = "%s-%s" % (self.month_names_eng[n], self.quarter_nums[2][0]) # If it's doen't exitst, create it if not self.sm.has_screen(next_scr_name): self.create_month_scr(self.quarter[2]) self.sm.current = next_scr_name self.sm.transition.direction = "left" self.get_quarter() self.title = "%s - %s" % (self.i18n(self.month_names[self.active_date[1] - 1]), self.active_date[2]) self.title_label.text = self.title def on_touch_move(self, touch): """ Switch months pages by touch move """ if self.touch_switch: # Left - prev if touch.dpos[0] < -30: self.go_prev(None) # Right - next elif touch.dpos[0] > 30: self.go_next(None) class ArrowButton(Button): pass class MonthYearLabel(Label): pass class MonthsManager(ScreenManager): pass class ButtonsGrid(GridLayout): pass class DayAbbrLabel(Label): pass class DayAbbrWeekendLabel(DayAbbrLabel): pass class DayButton(ToggleButton): pass class DayNumButton(DayButton): pass class DayNumWeekendButton(DayButton): pass
26.38125
82
0.659086
from kivy.lang import Builder from kivy.uix.screenmanager import ScreenManager, Screen from kivy.uix.popup import Popup from kivy.uix.relativelayout import RelativeLayout from kivy.uix.gridlayout import GridLayout from kivy.uix.button import Button from kivy.uix.togglebutton import ToggleButton from kivy.uix.textinput import TextInput from kivy.uix.label import Label from kivy.core.window import Window from kivy.factory import Factory from kivy.properties import NumericProperty, ReferenceListProperty from kivyblocks.i18n import I18n from . import calendar_data as cal_data Builder.load_string(""" <ArrowButton>: background_normal: "" background_down: "" background_color: 1, 1, 1, 0 size_hint: .1, .1 <MonthYearLabel>: pos_hint: {"top": 1, "center_x": .5} size_hint: None, 0.1 halign: "center" <MonthsManager>: pos_hint: {"top": .9} size_hint: 1, .9 <ButtonsGrid>: cols: 7 rows: 7 size_hint: 1, 1 pos_hint: {"top": 1} <DayAbbrLabel>: text_size: self.size[0], None halign: "center" <DayAbbrWeekendLabel>: color: 1, 0, 0, 1 <DayButton>: group: "day_num" <DayNumWeekendButton>: background_color: 1, 0, 0, 1 """) class DatePicker(TextInput): def __init__(self, touch_switch=False, value=None, *args, **kwargs): super(DatePicker, self).__init__(*args, **kwargs) self.touch_switch = touch_switch self.init_ui(value) def getValue(self): return self.text def setValue(self, sdate): self.text = sdate d = [int(i) for i in sdate.split('-')] d.reverse() self.cal.active_date = d def init_ui(self, value): if not value: value = cal_data.today_date() d = [int(i) for i in value.split('.')] value = '%04d-%02d-%02d' % (d[2],d[1],d[0]) self.cal = CalendarWidget(as_popup=True, touch_switch=self.touch_switch) self.setValue(value) self.popup = Popup(content=self.cal, on_dismiss=self.update_value, size_hint=(0.7,0.7), title="") self.cal.parent_popup = self.popup self.bind(focus=self.show_popup) def show_popup(self, isnt, val): if val: Window.release_all_keyboards() self.popup.open() def update_value(self, inst): d = self.cal.active_date self.text = "%04d-%02d-%02d" % (d[2],d[1],d[0]) self.focus = False class CalendarWidget(RelativeLayout): def __init__(self, as_popup=False, touch_switch=False, *args, **kwargs): super(CalendarWidget, self).__init__(*args, **kwargs) self.i18n = I18n() self.as_popup = as_popup self.touch_switch = touch_switch self.prepare_data() self.init_ui() def init_ui(self): self.left_arrow = ArrowButton(text="<", on_press=self.go_prev, pos_hint={"top": 1, "left": 0}) self.right_arrow = ArrowButton(text=">", on_press=self.go_next, pos_hint={"top": 1, "right": 1}) self.add_widget(self.left_arrow) self.add_widget(self.right_arrow) self.title_label = MonthYearLabel(text=self.title) self.add_widget(self.title_label) self.sm = MonthsManager() self.add_widget(self.sm) self.create_month_scr(self.quarter[1], toogle_today=True) def create_month_scr(self, month, toogle_today=False): scr = Screen() m = self.month_names_eng[self.active_date[1] - 1] scr.name = "%s-%s" % (m, self.active_date[2]) grid_layout = ButtonsGrid() scr.add_widget(grid_layout) for i in range(7): if i >= 5: l = Factory.Text(text=self.days_abrs[i], i18n=True) else: l = Factory.Text(text=self.days_abrs[i], i18n=True) grid_layout.add_widget(l) for week in month: for day in week: if day[1] >= 5: tbtn = DayNumWeekendButton(text=str(day[0])) else: tbtn = DayNumButton(text=str(day[0])) tbtn.bind(on_press=self.get_btn_value) if toogle_today: if day[0] == self.active_date[0] and day[2] == 1: tbtn.state = "down" if day[2] == 0: tbtn.disabled = True grid_layout.add_widget(tbtn) self.sm.add_widget(scr) def prepare_data(self): self.month_names = cal_data.get_month_names() self.month_names_eng = cal_data.get_month_names_eng() self.days_abrs = cal_data.get_days_abbrs() self.active_date = cal_data.today_date_list() self.title = "%s - %s" % (self.i18n(self.month_names[self.active_date[1] - 1]), self.active_date[2]) self.get_quarter() def get_quarter(self): self.quarter_nums = cal_data.calc_quarter(self.active_date[2], self.active_date[1]) self.quarter = cal_data.get_quarter(self.active_date[2], self.active_date[1]) def get_btn_value(self, inst): self.active_date[0] = int(inst.text) if self.as_popup: self.parent_popup.dismiss() def go_prev(self, inst): self.active_date = [self.active_date[0], self.quarter_nums[0][1], self.quarter_nums[0][0]] n = self.quarter_nums[0][1] - 1 prev_scr_name = "%s-%s" % (self.month_names_eng[n], self.quarter_nums[0][0]) if not self.sm.has_screen(prev_scr_name): self.create_month_scr(self.quarter[0]) self.sm.current = prev_scr_name self.sm.transition.direction = "right" self.get_quarter() self.title = "%s - %s" % (self.i18n(self.month_names[self.active_date[1] - 1]), self.active_date[2]) self.title_label.text = self.title def go_next(self, inst): self.active_date = [self.active_date[0], self.quarter_nums[2][1], self.quarter_nums[2][0]] n = self.quarter_nums[2][1] - 1 next_scr_name = "%s-%s" % (self.month_names_eng[n], self.quarter_nums[2][0]) if not self.sm.has_screen(next_scr_name): self.create_month_scr(self.quarter[2]) self.sm.current = next_scr_name self.sm.transition.direction = "left" self.get_quarter() self.title = "%s - %s" % (self.i18n(self.month_names[self.active_date[1] - 1]), self.active_date[2]) self.title_label.text = self.title def on_touch_move(self, touch): if self.touch_switch: if touch.dpos[0] < -30: self.go_prev(None) elif touch.dpos[0] > 30: self.go_next(None) class ArrowButton(Button): pass class MonthYearLabel(Label): pass class MonthsManager(ScreenManager): pass class ButtonsGrid(GridLayout): pass class DayAbbrLabel(Label): pass class DayAbbrWeekendLabel(DayAbbrLabel): pass class DayButton(ToggleButton): pass class DayNumButton(DayButton): pass class DayNumWeekendButton(DayButton): pass
true
true
f705fe7872d172d8f85abd1a9f7b4972525638ec
498
py
Python
wystia/utils/response.py
rnag/wystia
dfe21764a54d2737814157072c3262aa7b1cec7d
[ "MIT" ]
1
2022-02-02T21:22:20.000Z
2022-02-02T21:22:20.000Z
wystia/utils/response.py
rnag/wystia
dfe21764a54d2737814157072c3262aa7b1cec7d
[ "MIT" ]
9
2021-06-17T15:11:31.000Z
2021-12-01T18:49:13.000Z
wystia/utils/response.py
rnag/wystia
dfe21764a54d2737814157072c3262aa7b1cec7d
[ "MIT" ]
null
null
null
""" Utilities for API Gateway response formatting """ import json def format_response(data, status=200): return { 'body': json.dumps(data), 'headers': { 'Content-Type': 'application/json' }, 'statusCode': int(status) } def format_error(msg, code='BadRequest', status=400): data = { 'success': False, 'error': { 'code': code, 'message': msg } } return format_response(data, status)
18.444444
53
0.536145
import json def format_response(data, status=200): return { 'body': json.dumps(data), 'headers': { 'Content-Type': 'application/json' }, 'statusCode': int(status) } def format_error(msg, code='BadRequest', status=400): data = { 'success': False, 'error': { 'code': code, 'message': msg } } return format_response(data, status)
true
true
f705febbae4ab8b3666a1d72a4151694f25b9165
78,540
py
Python
nova/virt/vmwareapi/vmops.py
armstrong-liu/nova
df4a0cb8fadbb9ff4fb96b97c12d7d909e1c7df0
[ "Apache-2.0" ]
null
null
null
nova/virt/vmwareapi/vmops.py
armstrong-liu/nova
df4a0cb8fadbb9ff4fb96b97c12d7d909e1c7df0
[ "Apache-2.0" ]
9
2015-05-20T11:20:17.000Z
2017-07-27T08:21:33.000Z
nova/virt/vmwareapi/vmops.py
armstrong-liu/nova
df4a0cb8fadbb9ff4fb96b97c12d7d909e1c7df0
[ "Apache-2.0" ]
13
2015-05-05T09:34:04.000Z
2017-11-08T02:03:46.000Z
# Copyright (c) 2013 Hewlett-Packard Development Company, L.P. # Copyright (c) 2012 VMware, Inc. # Copyright (c) 2011 Citrix Systems, Inc. # Copyright 2011 OpenStack Foundation # # 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. """ Class for VM tasks like spawn, snapshot, suspend, resume etc. """ import collections import copy import os import time import decorator from oslo.config import cfg from oslo.vmware import exceptions as vexc from nova.api.metadata import base as instance_metadata from nova import compute from nova.compute import power_state from nova.compute import task_states from nova.compute import vm_states from nova.console import type as ctype from nova import context as nova_context from nova import exception from nova.i18n import _, _LE, _LW from nova import objects from nova.openstack.common import excutils from nova.openstack.common import lockutils from nova.openstack.common import log as logging from nova.openstack.common import units from nova.openstack.common import uuidutils from nova import utils from nova.virt import configdrive from nova.virt import diagnostics from nova.virt import driver from nova.virt.vmwareapi import constants from nova.virt.vmwareapi import ds_util from nova.virt.vmwareapi import error_util from nova.virt.vmwareapi import imagecache from nova.virt.vmwareapi import vif as vmwarevif from nova.virt.vmwareapi import vim_util from nova.virt.vmwareapi import vm_util from nova.virt.vmwareapi import vmware_images CONF = cfg.CONF CONF.import_opt('image_cache_subdirectory_name', 'nova.virt.imagecache') CONF.import_opt('remove_unused_base_images', 'nova.virt.imagecache') CONF.import_opt('vnc_enabled', 'nova.vnc') CONF.import_opt('my_ip', 'nova.netconf') LOG = logging.getLogger(__name__) VMWARE_POWER_STATES = { 'poweredOff': power_state.SHUTDOWN, 'poweredOn': power_state.RUNNING, 'suspended': power_state.SUSPENDED} RESIZE_TOTAL_STEPS = 4 DcInfo = collections.namedtuple('DcInfo', ['ref', 'name', 'vmFolder']) class VirtualMachineInstanceConfigInfo(object): """Parameters needed to create and configure a new instance.""" def __init__(self, instance, instance_name, image_info, datastore, dc_info, image_cache): # Some methods called during spawn take the instance parameter purely # for logging purposes. # TODO(vui) Clean them up, so we no longer need to keep this variable self.instance = instance # Get the instance name. In some cases this may differ from the 'uuid', # for example when the spawn of a rescue instance takes place. self.instance_name = instance_name or instance.uuid self.ii = image_info self.root_gb = instance.root_gb self.datastore = datastore self.dc_info = dc_info self._image_cache = image_cache @property def cache_image_folder(self): if self.ii.image_id is None: return return self._image_cache.get_image_cache_folder( self.datastore, self.ii.image_id) @property def cache_image_path(self): if self.ii.image_id is None: return cached_image_file_name = "%s.%s" % (self.ii.image_id, self.ii.file_type) return self.cache_image_folder.join(cached_image_file_name) # Note(vui): See https://bugs.launchpad.net/nova/+bug/1363349 # for cases where mocking time.sleep() can have unintended effects on code # not under test. For now, unblock the affected test cases by providing # a wrapper function to work around needing to mock time.sleep() def _time_sleep_wrapper(delay): time.sleep(delay) @decorator.decorator def retry_if_task_in_progress(f, *args, **kwargs): retries = max(CONF.vmware.api_retry_count, 1) delay = 1 for attempt in range(1, retries + 1): if attempt != 1: _time_sleep_wrapper(delay) delay = min(2 * delay, 60) try: f(*args, **kwargs) return except error_util.TaskInProgress: pass class VMwareVMOps(object): """Management class for VM-related tasks.""" def __init__(self, session, virtapi, volumeops, cluster=None, datastore_regex=None): """Initializer.""" self.compute_api = compute.API() self._session = session self._virtapi = virtapi self._volumeops = volumeops self._cluster = cluster self._datastore_regex = datastore_regex # Ensure that the base folder is unique per compute node if CONF.remove_unused_base_images: self._base_folder = '%s%s' % (CONF.my_ip, CONF.image_cache_subdirectory_name) else: # Aging disable ensures backward compatibility self._base_folder = CONF.image_cache_subdirectory_name self._tmp_folder = 'vmware_temp' self._default_root_device = 'vda' self._rescue_suffix = '-rescue' self._migrate_suffix = '-orig' self._datastore_dc_mapping = {} self._datastore_browser_mapping = {} self._imagecache = imagecache.ImageCacheManager(self._session, self._base_folder) def _extend_virtual_disk(self, instance, requested_size, name, dc_ref): service_content = self._session._get_vim().service_content LOG.debug("Extending root virtual disk to %s", requested_size) vmdk_extend_task = self._session._call_method( self._session._get_vim(), "ExtendVirtualDisk_Task", service_content.virtualDiskManager, name=name, datacenter=dc_ref, newCapacityKb=requested_size, eagerZero=False) try: self._session._wait_for_task(vmdk_extend_task) except Exception as e: with excutils.save_and_reraise_exception(): LOG.error(_('Extending virtual disk failed with error: %s'), e, instance=instance) # Clean up files created during the extend operation files = [name.replace(".vmdk", "-flat.vmdk"), name] for file in files: ds_path = ds_util.DatastorePath.parse(file) self._delete_datastore_file(instance, ds_path, dc_ref) LOG.debug("Extended root virtual disk") def _delete_datastore_file(self, instance, datastore_path, dc_ref): try: ds_util.file_delete(self._session, datastore_path, dc_ref) except (vexc.CannotDeleteFileException, vexc.FileFaultException, vexc.FileLockedException, vexc.FileNotFoundException): LOG.debug("Unable to delete %(ds)s. There may be more than " "one process or thread trying to delete the file", {'ds': datastore_path}, exc_info=True) def _extend_if_required(self, dc_info, image_info, instance, root_vmdk_path): """Increase the size of the root vmdk if necessary.""" if instance.root_gb > image_info.file_size_in_gb: size_in_kb = instance.root_gb * units.Mi self._extend_virtual_disk(instance, size_in_kb, root_vmdk_path, dc_info.ref) def _configure_config_drive(self, instance, vm_ref, dc_info, datastore, injected_files, admin_password): session_vim = self._session._get_vim() cookies = session_vim.client.options.transport.cookiejar uploaded_iso_path = self._create_config_drive(instance, injected_files, admin_password, datastore.name, dc_info.name, instance['uuid'], cookies) uploaded_iso_path = datastore.build_path(uploaded_iso_path) self._attach_cdrom_to_vm( vm_ref, instance, datastore.ref, str(uploaded_iso_path)) def build_virtual_machine(self, instance, instance_name, image_info, dc_info, datastore, network_info): node_mo_id = vm_util.get_mo_id_from_instance(instance) res_pool_ref = vm_util.get_res_pool_ref(self._session, self._cluster, node_mo_id) vif_infos = vmwarevif.get_vif_info(self._session, self._cluster, utils.is_neutron(), image_info.vif_model, network_info) allocations = self._get_cpu_allocations(instance.instance_type_id) # Get the create vm config spec client_factory = self._session._get_vim().client.factory config_spec = vm_util.get_vm_create_spec(client_factory, instance, instance_name, datastore.name, vif_infos, image_info.os_type, allocations=allocations) # Create the VM vm_ref = vm_util.create_vm(self._session, instance, dc_info.vmFolder, config_spec, res_pool_ref) return vm_ref def _get_cpu_allocations(self, instance_type_id): # Read flavors for allocations flavor = objects.Flavor.get_by_id( nova_context.get_admin_context(read_deleted='yes'), instance_type_id) allocations = {} for (key, type) in (('cpu_limit', int), ('cpu_reservation', int), ('cpu_shares_level', str), ('cpu_shares_share', int)): value = flavor.extra_specs.get('quota:' + key) if value: allocations[key] = type(value) return allocations def _fetch_image_as_file(self, context, vi, image_ds_loc): """Download image as an individual file to host via HTTP PUT.""" session = self._session session_vim = session._get_vim() cookies = session_vim.client.options.transport.cookiejar LOG.debug("Downloading image file data %(image_id)s to " "%(file_path)s on the data store " "%(datastore_name)s", {'image_id': vi.ii.image_id, 'file_path': image_ds_loc, 'datastore_name': vi.datastore.name}, instance=vi.instance) vmware_images.fetch_image( context, vi.instance, session._host, vi.dc_info.name, vi.datastore.name, image_ds_loc.rel_path, cookies=cookies) def _prepare_sparse_image(self, vi): tmp_dir_loc = vi.datastore.build_path( self._tmp_folder, uuidutils.generate_uuid()) tmp_image_ds_loc = tmp_dir_loc.join( vi.ii.image_id, "tmp-sparse.vmdk") return tmp_dir_loc, tmp_image_ds_loc def _prepare_flat_image(self, vi): tmp_dir_loc = vi.datastore.build_path( self._tmp_folder, uuidutils.generate_uuid()) tmp_image_ds_loc = tmp_dir_loc.join( vi.ii.image_id, vi.cache_image_path.basename) ds_util.mkdir(self._session, tmp_image_ds_loc.parent, vi.dc_info.ref) vm_util.create_virtual_disk( self._session, vi.dc_info.ref, vi.ii.adapter_type, vi.ii.disk_type, str(tmp_image_ds_loc), vi.ii.file_size_in_kb) flat_vmdk_name = vi.cache_image_path.basename.replace('.vmdk', '-flat.vmdk') flat_vmdk_ds_loc = tmp_dir_loc.join(vi.ii.image_id, flat_vmdk_name) self._delete_datastore_file(vi.instance, str(flat_vmdk_ds_loc), vi.dc_info.ref) return tmp_dir_loc, flat_vmdk_ds_loc def _prepare_iso_image(self, vi): tmp_dir_loc = vi.datastore.build_path( self._tmp_folder, uuidutils.generate_uuid()) tmp_image_ds_loc = tmp_dir_loc.join( vi.ii.image_id, vi.cache_image_path.basename) return tmp_dir_loc, tmp_image_ds_loc def _move_to_cache(self, dc_ref, src_folder_ds_path, dst_folder_ds_path): try: ds_util.file_move(self._session, dc_ref, src_folder_ds_path, dst_folder_ds_path) except vexc.FileAlreadyExistsException: # Folder move has failed. This may be due to the fact that a # process or thread has already completed the operation. # Since image caching is synchronized, this can only happen # due to action external to the process. # In the event of a FileAlreadyExists we continue, # all other exceptions will be raised. LOG.warning(_LW("Destination %s already exists! Concurrent moves " "can lead to unexpected results."), dst_folder_ds_path) def _cache_sparse_image(self, vi, tmp_image_ds_loc): tmp_dir_loc = tmp_image_ds_loc.parent.parent converted_image_ds_loc = tmp_dir_loc.join( vi.ii.image_id, vi.cache_image_path.basename) # converts fetched image to preallocated disk vm_util.copy_virtual_disk( self._session, vi.dc_info.ref, str(tmp_image_ds_loc), str(converted_image_ds_loc)) self._delete_datastore_file(vi.instance, str(tmp_image_ds_loc), vi.dc_info.ref) self._move_to_cache(vi.dc_info.ref, tmp_image_ds_loc.parent, vi.cache_image_folder) def _cache_flat_image(self, vi, tmp_image_ds_loc): self._move_to_cache(vi.dc_info.ref, tmp_image_ds_loc.parent, vi.cache_image_folder) def _cache_iso_image(self, vi, tmp_image_ds_loc): self._move_to_cache(vi.dc_info.ref, tmp_image_ds_loc.parent, vi.cache_image_folder) def _get_vm_config_info(self, instance, image_info, instance_name=None): """Captures all relevant information from the spawn parameters.""" if (instance.root_gb != 0 and image_info.file_size_in_gb > instance.root_gb): reason = _("Image disk size greater than requested disk size") raise exception.InstanceUnacceptable(instance_id=instance.uuid, reason=reason) datastore = ds_util.get_datastore( self._session, self._cluster, self._datastore_regex) dc_info = self.get_datacenter_ref_and_name(datastore.ref) return VirtualMachineInstanceConfigInfo(instance, instance_name, image_info, datastore, dc_info, self._imagecache) def _get_image_callbacks(self, vi): disk_type = vi.ii.disk_type image_fetch = self._fetch_image_as_file if vi.ii.is_iso: image_prepare = self._prepare_iso_image image_cache = self._cache_iso_image elif disk_type == constants.DISK_TYPE_SPARSE: image_prepare = self._prepare_sparse_image image_cache = self._cache_sparse_image elif disk_type in constants.SUPPORTED_FLAT_VARIANTS: image_prepare = self._prepare_flat_image image_cache = self._cache_flat_image else: reason = _("disk type '%s' not supported") % disk_type raise exception.InvalidDiskInfo(reason=reason) return image_prepare, image_fetch, image_cache def _fetch_image_if_missing(self, context, vi): image_prepare, image_fetch, image_cache = self._get_image_callbacks(vi) LOG.debug("Processing image %s", vi.ii.image_id) with lockutils.lock(str(vi.cache_image_path), lock_file_prefix='nova-vmware-fetch_image'): self.check_cache_folder(vi.datastore.name, vi.datastore.ref) ds_browser = self._get_ds_browser(vi.datastore.ref) if not ds_util.file_exists(self._session, ds_browser, vi.cache_image_folder, vi.cache_image_path.basename): LOG.debug("Preparing fetch location") tmp_dir_loc, tmp_image_ds_loc = image_prepare(vi) LOG.debug("Fetch image to %s", tmp_image_ds_loc) image_fetch(context, vi, tmp_image_ds_loc) LOG.debug("Caching image") image_cache(vi, tmp_image_ds_loc) LOG.debug("Cleaning up location %s", str(tmp_dir_loc)) self._delete_datastore_file(vi.instance, str(tmp_dir_loc), vi.dc_info.ref) def spawn(self, context, instance, image_meta, injected_files, admin_password, network_info, block_device_info=None, instance_name=None, power_on=True): client_factory = self._session._get_vim().client.factory image_info = vmware_images.VMwareImage.from_image(instance.image_ref, image_meta) vi = self._get_vm_config_info(instance, image_info, instance_name) # Creates the virtual machine. The virtual machine reference returned # is unique within Virtual Center. vm_ref = self.build_virtual_machine(instance, vi.instance_name, image_info, vi.dc_info, vi.datastore, network_info) # Cache the vm_ref. This saves a remote call to the VC. This uses the # instance_name. This covers all use cases including rescue and resize. vm_util.vm_ref_cache_update(vi.instance_name, vm_ref) # Set the machine.id parameter of the instance to inject # the NIC configuration inside the VM if CONF.flat_injected: self._set_machine_id(client_factory, instance, network_info) # Set the vnc configuration of the instance, vnc port starts from 5900 if CONF.vnc_enabled: self._get_and_set_vnc_config(client_factory, instance) block_device_mapping = [] if block_device_info is not None: block_device_mapping = driver.block_device_info_get_mapping( block_device_info) # NOTE(mdbooth): the logic here is that we ignore the image if there # are block device mappings. This behaviour is incorrect, and a bug in # the driver. We should be able to accept an image and block device # mappings. if len(block_device_mapping) > 0: msg = "Block device information present: %s" % block_device_info # NOTE(mriedem): block_device_info can contain an auth_password # so we have to scrub the message before logging it. LOG.debug(logging.mask_password(msg), instance=instance) for root_disk in block_device_mapping: connection_info = root_disk['connection_info'] # TODO(hartsocks): instance is unnecessary, remove it # we still use instance in many locations for no other purpose # than logging, can we simplify this? self._volumeops.attach_root_volume(connection_info, instance, self._default_root_device, vi.datastore.ref) else: self._imagecache.enlist_image( image_info.image_id, vi.datastore, vi.dc_info.ref) self._fetch_image_if_missing(context, vi) if image_info.is_iso: self._use_iso_image(vm_ref, vi) elif image_info.linked_clone: self._use_disk_image_as_linked_clone(vm_ref, vi) else: self._use_disk_image_as_full_clone(vm_ref, vi) if configdrive.required_by(instance): self._configure_config_drive( instance, vm_ref, vi.dc_info, vi.datastore, injected_files, admin_password) if power_on: vm_util.power_on_instance(self._session, instance, vm_ref=vm_ref) def _create_config_drive(self, instance, injected_files, admin_password, data_store_name, dc_name, upload_folder, cookies): if CONF.config_drive_format != 'iso9660': reason = (_('Invalid config_drive_format "%s"') % CONF.config_drive_format) raise exception.InstancePowerOnFailure(reason=reason) LOG.info(_('Using config drive for instance'), instance=instance) extra_md = {} if admin_password: extra_md['admin_pass'] = admin_password inst_md = instance_metadata.InstanceMetadata(instance, content=injected_files, extra_md=extra_md) try: with configdrive.ConfigDriveBuilder(instance_md=inst_md) as cdb: with utils.tempdir() as tmp_path: tmp_file = os.path.join(tmp_path, 'configdrive.iso') cdb.make_drive(tmp_file) upload_iso_path = "%s/configdrive.iso" % ( upload_folder) vmware_images.upload_iso_to_datastore( tmp_file, instance, host=self._session._host, data_center_name=dc_name, datastore_name=data_store_name, cookies=cookies, file_path=upload_iso_path) return upload_iso_path except Exception as e: with excutils.save_and_reraise_exception(): LOG.error(_('Creating config drive failed with error: %s'), e, instance=instance) def _attach_cdrom_to_vm(self, vm_ref, instance, datastore, file_path): """Attach cdrom to VM by reconfiguration.""" client_factory = self._session._get_vim().client.factory devices = self._session._call_method(vim_util, "get_dynamic_property", vm_ref, "VirtualMachine", "config.hardware.device") (controller_key, unit_number, controller_spec) = vm_util.allocate_controller_key_and_unit_number( client_factory, devices, 'ide') cdrom_attach_config_spec = vm_util.get_cdrom_attach_config_spec( client_factory, datastore, file_path, controller_key, unit_number) if controller_spec: cdrom_attach_config_spec.deviceChange.append(controller_spec) LOG.debug("Reconfiguring VM instance to attach cdrom %s", file_path, instance=instance) vm_util.reconfigure_vm(self._session, vm_ref, cdrom_attach_config_spec) LOG.debug("Reconfigured VM instance to attach cdrom %s", file_path, instance=instance) def _create_vm_snapshot(self, instance, vm_ref): LOG.debug("Creating Snapshot of the VM instance", instance=instance) snapshot_task = self._session._call_method( self._session._get_vim(), "CreateSnapshot_Task", vm_ref, name="%s-snapshot" % instance.uuid, description="Taking Snapshot of the VM", memory=False, quiesce=True) self._session._wait_for_task(snapshot_task) LOG.debug("Created Snapshot of the VM instance", instance=instance) task_info = self._session._call_method(vim_util, "get_dynamic_property", snapshot_task, "Task", "info") snapshot = task_info.result return snapshot @retry_if_task_in_progress def _delete_vm_snapshot(self, instance, vm_ref, snapshot): LOG.debug("Deleting Snapshot of the VM instance", instance=instance) delete_snapshot_task = self._session._call_method( self._session._get_vim(), "RemoveSnapshot_Task", snapshot, removeChildren=False, consolidate=True) self._session._wait_for_task(delete_snapshot_task) LOG.debug("Deleted Snapshot of the VM instance", instance=instance) def snapshot(self, context, instance, image_id, update_task_state): """Create snapshot from a running VM instance. Steps followed are: 1. Get the name of the vmdk file which the VM points to right now. Can be a chain of snapshots, so we need to know the last in the chain. 2. Create the snapshot. A new vmdk is created which the VM points to now. The earlier vmdk becomes read-only. 3. Call CopyVirtualDisk which coalesces the disk chain to form a single vmdk, rather a .vmdk metadata file and a -flat.vmdk disk data file. 4. Now upload the -flat.vmdk file to the image store. 5. Delete the coalesced .vmdk and -flat.vmdk created. """ vm_ref = vm_util.get_vm_ref(self._session, instance) service_content = self._session._get_vim().service_content def _get_vm_and_vmdk_attribs(): # Get the vmdk file name that the VM is pointing to hw_devices = self._session._call_method(vim_util, "get_dynamic_property", vm_ref, "VirtualMachine", "config.hardware.device") (vmdk_file_path_before_snapshot, adapter_type, disk_type) = vm_util.get_vmdk_path_and_adapter_type( hw_devices, uuid=instance.uuid) if not vmdk_file_path_before_snapshot: LOG.debug("No root disk defined. Unable to snapshot.") raise error_util.NoRootDiskDefined() datastore_name = ds_util.DatastorePath.parse( vmdk_file_path_before_snapshot).datastore os_type = self._session._call_method(vim_util, "get_dynamic_property", vm_ref, "VirtualMachine", "summary.config.guestId") return (vmdk_file_path_before_snapshot, adapter_type, disk_type, datastore_name, os_type) (vmdk_file_path_before_snapshot, adapter_type, disk_type, datastore_name, os_type) = _get_vm_and_vmdk_attribs() snapshot = self._create_vm_snapshot(instance, vm_ref) update_task_state(task_state=task_states.IMAGE_PENDING_UPLOAD) def _check_if_tmp_folder_exists(): # Copy the contents of the VM that were there just before the # snapshot was taken ds_ref_ret = self._session._call_method( vim_util, "get_dynamic_property", vm_ref, "VirtualMachine", "datastore") if ds_ref_ret is None: raise exception.DatastoreNotFound() ds_ref = ds_ref_ret.ManagedObjectReference[0] self.check_temp_folder(datastore_name, ds_ref) return ds_ref ds_ref = _check_if_tmp_folder_exists() # Generate a random vmdk file name to which the coalesced vmdk content # will be copied to. A random name is chosen so that we don't have # name clashes. random_name = uuidutils.generate_uuid() dest_vmdk_file_path = ds_util.DatastorePath( datastore_name, self._tmp_folder, "%s.vmdk" % random_name) dest_vmdk_data_file_path = ds_util.DatastorePath( datastore_name, self._tmp_folder, "%s-flat.vmdk" % random_name) dc_info = self.get_datacenter_ref_and_name(ds_ref) def _copy_vmdk_content(): # Consolidate the snapshotted disk to a temporary vmdk. LOG.debug('Copying snapshotted disk %s.', vmdk_file_path_before_snapshot, instance=instance) copy_disk_task = self._session._call_method( self._session._get_vim(), "CopyVirtualDisk_Task", service_content.virtualDiskManager, sourceName=vmdk_file_path_before_snapshot, sourceDatacenter=dc_info.ref, destName=str(dest_vmdk_file_path), destDatacenter=dc_info.ref, force=False) self._session._wait_for_task(copy_disk_task) LOG.debug('Copied snapshotted disk %s.', vmdk_file_path_before_snapshot, instance=instance) _copy_vmdk_content() self._delete_vm_snapshot(instance, vm_ref, snapshot) cookies = self._session._get_vim().client.options.transport.cookiejar def _upload_vmdk_to_image_repository(): # Upload the contents of -flat.vmdk file which has the disk data. LOG.debug("Uploading image %s", image_id, instance=instance) vmware_images.upload_image( context, image_id, instance, os_type=os_type, disk_type=constants.DEFAULT_DISK_TYPE, adapter_type=adapter_type, image_version=1, host=self._session._host, data_center_name=dc_info.name, datastore_name=datastore_name, cookies=cookies, file_path="%s/%s-flat.vmdk" % (self._tmp_folder, random_name)) LOG.debug("Uploaded image %s", image_id, instance=instance) update_task_state(task_state=task_states.IMAGE_UPLOADING, expected_state=task_states.IMAGE_PENDING_UPLOAD) _upload_vmdk_to_image_repository() def _clean_temp_data(): """Delete temporary vmdk files generated in image handling operations. """ # The data file is the one occupying space, and likelier to see # deletion problems, so prioritize its deletion first. In the # unlikely event that its deletion fails, the small descriptor file # is retained too by design since it makes little sense to remove # it when the data disk it refers to still lingers. for f in dest_vmdk_data_file_path, dest_vmdk_file_path: self._delete_datastore_file(instance, f, dc_info.ref) _clean_temp_data() def reboot(self, instance, network_info): """Reboot a VM instance.""" vm_ref = vm_util.get_vm_ref(self._session, instance) lst_properties = ["summary.guest.toolsStatus", "runtime.powerState", "summary.guest.toolsRunningStatus"] props = self._session._call_method(vim_util, "get_object_properties", None, vm_ref, "VirtualMachine", lst_properties) query = vm_util.get_values_from_object_properties(self._session, props) pwr_state = query['runtime.powerState'] tools_status = query['summary.guest.toolsStatus'] tools_running_status = query['summary.guest.toolsRunningStatus'] # Raise an exception if the VM is not powered On. if pwr_state not in ["poweredOn"]: reason = _("instance is not powered on") raise exception.InstanceRebootFailure(reason=reason) # If latest vmware tools are installed in the VM, and that the tools # are running, then only do a guest reboot. Otherwise do a hard reset. if (tools_status == "toolsOk" and tools_running_status == "guestToolsRunning"): LOG.debug("Rebooting guest OS of VM", instance=instance) self._session._call_method(self._session._get_vim(), "RebootGuest", vm_ref) LOG.debug("Rebooted guest OS of VM", instance=instance) else: LOG.debug("Doing hard reboot of VM", instance=instance) reset_task = self._session._call_method(self._session._get_vim(), "ResetVM_Task", vm_ref) self._session._wait_for_task(reset_task) LOG.debug("Did hard reboot of VM", instance=instance) def _destroy_instance(self, instance, destroy_disks=True, instance_name=None): # Destroy a VM instance # Get the instance name. In some cases this may differ from the 'uuid', # for example when the spawn of a rescue instance takes place. if instance_name is None: instance_name = instance['uuid'] try: vm_ref = vm_util.get_vm_ref_from_name(self._session, instance_name) if vm_ref is None: LOG.warning(_('Instance does not exist on backend'), instance=instance) return lst_properties = ["config.files.vmPathName", "runtime.powerState", "datastore"] props = self._session._call_method(vim_util, "get_object_properties", None, vm_ref, "VirtualMachine", lst_properties) query = vm_util.get_values_from_object_properties( self._session, props) pwr_state = query['runtime.powerState'] vm_config_pathname = query['config.files.vmPathName'] vm_ds_path = None if vm_config_pathname: vm_ds_path = ds_util.DatastorePath.parse(vm_config_pathname) # Power off the VM if it is in PoweredOn state. if pwr_state == "poweredOn": vm_util.power_off_instance(self._session, instance, vm_ref) # Un-register the VM try: LOG.debug("Unregistering the VM", instance=instance) self._session._call_method(self._session._get_vim(), "UnregisterVM", vm_ref) LOG.debug("Unregistered the VM", instance=instance) except Exception as excep: LOG.warn(_("In vmwareapi:vmops:_destroy_instance, got this " "exception while un-registering the VM: %s"), excep) # Delete the folder holding the VM related content on # the datastore. if destroy_disks and vm_ds_path: try: dir_ds_compliant_path = vm_ds_path.parent LOG.debug("Deleting contents of the VM from " "datastore %(datastore_name)s", {'datastore_name': vm_ds_path.datastore}, instance=instance) ds_ref_ret = query['datastore'] ds_ref = ds_ref_ret.ManagedObjectReference[0] dc_info = self.get_datacenter_ref_and_name(ds_ref) ds_util.file_delete(self._session, dir_ds_compliant_path, dc_info.ref) LOG.debug("Deleted contents of the VM from " "datastore %(datastore_name)s", {'datastore_name': vm_ds_path.datastore}, instance=instance) except Exception: LOG.warn(_("In vmwareapi:vmops:_destroy_instance, " "exception while deleting the VM contents from " "the disk"), exc_info=True) except Exception as exc: LOG.exception(exc, instance=instance) finally: vm_util.vm_ref_cache_delete(instance_name) def destroy(self, instance, destroy_disks=True): """Destroy a VM instance. Steps followed for each VM are: 1. Power off, if it is in poweredOn state. 2. Un-register. 3. Delete the contents of the folder holding the VM related data. """ # If there is a rescue VM then we need to destroy that one too. LOG.debug("Destroying instance", instance=instance) if instance['vm_state'] == vm_states.RESCUED: LOG.debug("Rescue VM configured", instance=instance) try: self.unrescue(instance, power_on=False) LOG.debug("Rescue VM destroyed", instance=instance) except Exception: rescue_name = instance['uuid'] + self._rescue_suffix self._destroy_instance(instance, destroy_disks=destroy_disks, instance_name=rescue_name) # NOTE(arnaud): Destroy uuid-orig and uuid VMs iff it is not # triggered by the revert resize api call. This prevents # the uuid-orig VM to be deleted to be able to associate it later. if instance.task_state != task_states.RESIZE_REVERTING: # When VM deletion is triggered in middle of VM resize before VM # arrive RESIZED state, uuid-orig VM need to deleted to avoid # VM leak. Within method _destroy_instance it will check vmref # exist or not before attempt deletion. resize_orig_vmname = instance['uuid'] + self._migrate_suffix vm_orig_ref = vm_util.get_vm_ref_from_name(self._session, resize_orig_vmname) if vm_orig_ref: self._destroy_instance(instance, destroy_disks=destroy_disks, instance_name=resize_orig_vmname) self._destroy_instance(instance, destroy_disks=destroy_disks) LOG.debug("Instance destroyed", instance=instance) def pause(self, instance): msg = _("pause not supported for vmwareapi") raise NotImplementedError(msg) def unpause(self, instance): msg = _("unpause not supported for vmwareapi") raise NotImplementedError(msg) def suspend(self, instance): """Suspend the specified instance.""" vm_ref = vm_util.get_vm_ref(self._session, instance) pwr_state = self._session._call_method(vim_util, "get_dynamic_property", vm_ref, "VirtualMachine", "runtime.powerState") # Only PoweredOn VMs can be suspended. if pwr_state == "poweredOn": LOG.debug("Suspending the VM", instance=instance) suspend_task = self._session._call_method(self._session._get_vim(), "SuspendVM_Task", vm_ref) self._session._wait_for_task(suspend_task) LOG.debug("Suspended the VM", instance=instance) # Raise Exception if VM is poweredOff elif pwr_state == "poweredOff": reason = _("instance is powered off and cannot be suspended.") raise exception.InstanceSuspendFailure(reason=reason) else: LOG.debug("VM was already in suspended state. So returning " "without doing anything", instance=instance) def resume(self, instance): """Resume the specified instance.""" vm_ref = vm_util.get_vm_ref(self._session, instance) pwr_state = self._session._call_method(vim_util, "get_dynamic_property", vm_ref, "VirtualMachine", "runtime.powerState") if pwr_state.lower() == "suspended": LOG.debug("Resuming the VM", instance=instance) suspend_task = self._session._call_method( self._session._get_vim(), "PowerOnVM_Task", vm_ref) self._session._wait_for_task(suspend_task) LOG.debug("Resumed the VM", instance=instance) else: reason = _("instance is not in a suspended state") raise exception.InstanceResumeFailure(reason=reason) def rescue(self, context, instance, network_info, image_meta): """Rescue the specified instance. - shutdown the instance VM. - spawn a rescue VM (the vm name-label will be instance-N-rescue). """ vm_ref = vm_util.get_vm_ref(self._session, instance) self.power_off(instance) r_instance = copy.deepcopy(instance) instance_name = r_instance.uuid + self._rescue_suffix self.spawn(context, r_instance, image_meta, None, None, network_info, instance_name=instance_name, power_on=False) # Attach vmdk to the rescue VM hardware_devices = self._session._call_method(vim_util, "get_dynamic_property", vm_ref, "VirtualMachine", "config.hardware.device") (vmdk_path, adapter_type, disk_type) = vm_util.get_vmdk_path_and_adapter_type( hardware_devices, uuid=instance.uuid) rescue_vm_ref = vm_util.get_vm_ref_from_name(self._session, instance_name) self._volumeops.attach_disk_to_vm( rescue_vm_ref, r_instance, adapter_type, disk_type, vmdk_path) vm_util.power_on_instance(self._session, r_instance, vm_ref=rescue_vm_ref) def unrescue(self, instance, power_on=True): """Unrescue the specified instance.""" # Get the original vmdk_path vm_ref = vm_util.get_vm_ref(self._session, instance) hardware_devices = self._session._call_method(vim_util, "get_dynamic_property", vm_ref, "VirtualMachine", "config.hardware.device") (vmdk_path, adapter_type, disk_type) = vm_util.get_vmdk_path_and_adapter_type( hardware_devices, uuid=instance.uuid) r_instance = copy.deepcopy(instance) instance_name = r_instance.uuid + self._rescue_suffix # detach the original instance disk from the rescue disk vm_rescue_ref = vm_util.get_vm_ref_from_name(self._session, instance_name) hardware_devices = self._session._call_method(vim_util, "get_dynamic_property", vm_rescue_ref, "VirtualMachine", "config.hardware.device") device = vm_util.get_vmdk_volume_disk(hardware_devices, path=vmdk_path) vm_util.power_off_instance(self._session, r_instance, vm_rescue_ref) self._volumeops.detach_disk_from_vm(vm_rescue_ref, r_instance, device) self._destroy_instance(r_instance, instance_name=instance_name) if power_on: vm_util.power_on_instance(self._session, instance, vm_ref=vm_ref) def power_off(self, instance): """Power off the specified instance. :param instance: nova.objects.instance.Instance """ vm_util.power_off_instance(self._session, instance) def power_on(self, instance): vm_util.power_on_instance(self._session, instance) def _get_orig_vm_name_label(self, instance): return instance.uuid + '-orig' def _update_instance_progress(self, context, instance, step, total_steps): """Update instance progress percent to reflect current step number """ # Divide the action's workflow into discrete steps and "bump" the # instance's progress field as each step is completed. # # For a first cut this should be fine, however, for large VM images, # the clone disk step begins to dominate the equation. A # better approximation would use the percentage of the VM image that # has been streamed to the destination host. progress = round(float(step) / total_steps * 100) instance_uuid = instance.uuid LOG.debug("Updating instance '%(instance_uuid)s' progress to" " %(progress)d", {'instance_uuid': instance_uuid, 'progress': progress}, instance=instance) instance.progress = progress instance.save() def migrate_disk_and_power_off(self, context, instance, dest, flavor): """Transfers the disk of a running instance in multiple phases, turning off the instance before the end. """ # Checks if the migration needs a disk resize down. if flavor['root_gb'] < instance['root_gb']: reason = _("Unable to shrink disk.") raise exception.InstanceFaultRollback( exception.ResizeError(reason=reason)) # 0. Zero out the progress to begin self._update_instance_progress(context, instance, step=0, total_steps=RESIZE_TOTAL_STEPS) vm_ref = vm_util.get_vm_ref(self._session, instance) # Read the host_ref for the destination. If this is None then the # VC will decide on placement host_ref = self._get_host_ref_from_name(dest) # 1. Power off the instance self.power_off(instance) self._update_instance_progress(context, instance, step=1, total_steps=RESIZE_TOTAL_STEPS) # 2. Disassociate the linked vsphere VM from the instance vm_util.disassociate_vmref_from_instance(self._session, instance, vm_ref, suffix=self._migrate_suffix) self._update_instance_progress(context, instance, step=2, total_steps=RESIZE_TOTAL_STEPS) ds_ref = ds_util.get_datastore( self._session, self._cluster, datastore_regex=self._datastore_regex).ref dc_info = self.get_datacenter_ref_and_name(ds_ref) # 3. Clone the VM for instance vm_util.clone_vmref_for_instance(self._session, instance, vm_ref, host_ref, ds_ref, dc_info.vmFolder) self._update_instance_progress(context, instance, step=3, total_steps=RESIZE_TOTAL_STEPS) def confirm_migration(self, migration, instance, network_info): """Confirms a resize, destroying the source VM.""" # Destroy the original VM. The vm_ref needs to be searched using the # instance.uuid + self._migrate_suffix as the identifier. We will # not get the vm when searched using the instanceUuid but rather will # be found using the uuid buried in the extraConfig vm_ref = vm_util.search_vm_ref_by_identifier(self._session, instance.uuid + self._migrate_suffix) if vm_ref is None: LOG.debug("instance not present", instance=instance) return try: LOG.debug("Destroying the VM", instance=instance) destroy_task = self._session._call_method( self._session._get_vim(), "Destroy_Task", vm_ref) self._session._wait_for_task(destroy_task) LOG.debug("Destroyed the VM", instance=instance) except Exception as excep: LOG.warn(_("In vmwareapi:vmops:confirm_migration, got this " "exception while destroying the VM: %s"), excep) def finish_revert_migration(self, context, instance, network_info, block_device_info, power_on=True): """Finish reverting a resize.""" vm_util.associate_vmref_for_instance(self._session, instance, suffix=self._migrate_suffix) if power_on: vm_util.power_on_instance(self._session, instance) def finish_migration(self, context, migration, instance, disk_info, network_info, image_meta, resize_instance=False, block_device_info=None, power_on=True): """Completes a resize, turning on the migrated instance.""" vm_ref = vm_util.get_vm_ref(self._session, instance) if resize_instance: client_factory = self._session._get_vim().client.factory vm_resize_spec = vm_util.get_vm_resize_spec(client_factory, instance) vm_util.reconfigure_vm(self._session, vm_ref, vm_resize_spec) # Resize the disk (if larger) old_root_gb = instance.system_metadata['old_instance_type_root_gb'] if instance['root_gb'] > int(old_root_gb): root_disk_in_kb = instance['root_gb'] * units.Mi vmdk_path = vm_util.get_vmdk_path(self._session, vm_ref, instance) data_store_ref = ds_util.get_datastore(self._session, self._cluster, datastore_regex=self._datastore_regex).ref dc_info = self.get_datacenter_ref_and_name(data_store_ref) self._extend_virtual_disk(instance, root_disk_in_kb, vmdk_path, dc_info.ref) # TODO(ericwb): add extend for ephemeral disk # 4. Start VM if power_on: vm_util.power_on_instance(self._session, instance, vm_ref=vm_ref) self._update_instance_progress(context, instance, step=4, total_steps=RESIZE_TOTAL_STEPS) def live_migration(self, context, instance_ref, dest, post_method, recover_method, block_migration=False): """Spawning live_migration operation for distributing high-load.""" vm_ref = vm_util.get_vm_ref(self._session, instance_ref) host_ref = self._get_host_ref_from_name(dest) if host_ref is None: raise exception.HostNotFound(host=dest) LOG.debug("Migrating VM to host %s", dest, instance=instance_ref) try: vm_migrate_task = self._session._call_method( self._session._get_vim(), "MigrateVM_Task", vm_ref, host=host_ref, priority="defaultPriority") self._session._wait_for_task(vm_migrate_task) except Exception: with excutils.save_and_reraise_exception(): recover_method(context, instance_ref, dest, block_migration) post_method(context, instance_ref, dest, block_migration) LOG.debug("Migrated VM to host %s", dest, instance=instance_ref) def poll_rebooting_instances(self, timeout, instances): """Poll for rebooting instances.""" ctxt = nova_context.get_admin_context() instances_info = dict(instance_count=len(instances), timeout=timeout) if instances_info["instance_count"] > 0: LOG.info(_("Found %(instance_count)d hung reboots " "older than %(timeout)d seconds") % instances_info) for instance in instances: LOG.info(_("Automatically hard rebooting"), instance=instance) self.compute_api.reboot(ctxt, instance, "HARD") def get_info(self, instance): """Return data about the VM instance.""" vm_ref = vm_util.get_vm_ref(self._session, instance) lst_properties = ["summary.config.numCpu", "summary.config.memorySizeMB", "runtime.powerState"] vm_props = self._session._call_method(vim_util, "get_object_properties", None, vm_ref, "VirtualMachine", lst_properties) query = vm_util.get_values_from_object_properties( self._session, vm_props) max_mem = int(query['summary.config.memorySizeMB']) * 1024 return {'state': VMWARE_POWER_STATES[query['runtime.powerState']], 'max_mem': max_mem, 'mem': max_mem, 'num_cpu': int(query['summary.config.numCpu']), 'cpu_time': 0} def _get_diagnostics(self, instance): """Return data about VM diagnostics.""" vm_ref = vm_util.get_vm_ref(self._session, instance) lst_properties = ["summary.config", "summary.quickStats", "summary.runtime"] vm_props = self._session._call_method(vim_util, "get_object_properties", None, vm_ref, "VirtualMachine", lst_properties) query = vm_util.get_values_from_object_properties(self._session, vm_props) data = {} # All of values received are objects. Convert them to dictionaries for value in query.values(): prop_dict = vim_util.object_to_dict(value, list_depth=1) data.update(prop_dict) return data def get_diagnostics(self, instance): """Return data about VM diagnostics.""" data = self._get_diagnostics(instance) # Add a namespace to all of the diagnostsics return dict([('vmware:' + k, v) for k, v in data.items()]) def get_instance_diagnostics(self, instance): """Return data about VM diagnostics.""" data = self._get_diagnostics(instance) state = data.get('powerState') if state: state = power_state.STATE_MAP[VMWARE_POWER_STATES[state]] uptime = data.get('uptimeSeconds', 0) config_drive = configdrive.required_by(instance) diags = diagnostics.Diagnostics(state=state, driver='vmwareapi', config_drive=config_drive, hypervisor_os='esxi', uptime=uptime) diags.memory_details.maximum = data.get('memorySizeMB', 0) diags.memory_details.used = data.get('guestMemoryUsage', 0) # TODO(garyk): add in cpu, nic and disk stats return diags def _get_vnc_console_connection(self, instance): """Return connection info for a vnc console.""" vm_ref = vm_util.get_vm_ref(self._session, instance) opt_value = self._session._call_method(vim_util, 'get_dynamic_property', vm_ref, 'VirtualMachine', vm_util.VNC_CONFIG_KEY) if opt_value: port = int(opt_value.value) else: raise exception.ConsoleTypeUnavailable(console_type='vnc') return {'port': port, 'internal_access_path': None} @staticmethod def _get_machine_id_str(network_info): machine_id_str = '' for vif in network_info: # TODO(vish): add support for dns2 # TODO(sateesh): add support for injection of ipv6 configuration network = vif['network'] ip_v4 = netmask_v4 = gateway_v4 = broadcast_v4 = dns = None subnets_v4 = [s for s in network['subnets'] if s['version'] == 4] if len(subnets_v4) > 0: if len(subnets_v4[0]['ips']) > 0: ip_v4 = subnets_v4[0]['ips'][0] if len(subnets_v4[0]['dns']) > 0: dns = subnets_v4[0]['dns'][0]['address'] netmask_v4 = str(subnets_v4[0].as_netaddr().netmask) gateway_v4 = subnets_v4[0]['gateway']['address'] broadcast_v4 = str(subnets_v4[0].as_netaddr().broadcast) interface_str = ";".join([vif['address'], ip_v4 and ip_v4['address'] or '', netmask_v4 or '', gateway_v4 or '', broadcast_v4 or '', dns or '']) machine_id_str = machine_id_str + interface_str + '#' return machine_id_str def _set_machine_id(self, client_factory, instance, network_info): """Set the machine id of the VM for guest tools to pick up and reconfigure the network interfaces. """ vm_ref = vm_util.get_vm_ref(self._session, instance) machine_id_change_spec = vm_util.get_machine_id_change_spec( client_factory, self._get_machine_id_str(network_info)) LOG.debug("Reconfiguring VM instance to set the machine id", instance=instance) vm_util.reconfigure_vm(self._session, vm_ref, machine_id_change_spec) LOG.debug("Reconfigured VM instance to set the machine id", instance=instance) @utils.synchronized('vmware.get_and_set_vnc_port') def _get_and_set_vnc_config(self, client_factory, instance): """Set the vnc configuration of the VM.""" port = vm_util.get_vnc_port(self._session) vm_ref = vm_util.get_vm_ref(self._session, instance) vnc_config_spec = vm_util.get_vnc_config_spec( client_factory, port) LOG.debug("Reconfiguring VM instance to enable vnc on " "port - %(port)s", {'port': port}, instance=instance) vm_util.reconfigure_vm(self._session, vm_ref, vnc_config_spec) LOG.debug("Reconfigured VM instance to enable vnc on " "port - %(port)s", {'port': port}, instance=instance) def _get_ds_browser(self, ds_ref): ds_browser = self._datastore_browser_mapping.get(ds_ref.value) if not ds_browser: ds_browser = self._session._call_method( vim_util, "get_dynamic_property", ds_ref, "Datastore", "browser") self._datastore_browser_mapping[ds_ref.value] = ds_browser return ds_browser def _get_host_ref_from_name(self, host_name): """Get reference to the host with the name specified.""" host_objs = self._session._call_method(vim_util, "get_objects", "HostSystem", ["name"]) vm_util._cancel_retrieve_if_necessary(self._session, host_objs) for host in host_objs: if hasattr(host, 'propSet'): if host.propSet[0].val == host_name: return host.obj return None def _get_vmfolder_ref(self): """Get the Vm folder ref from the datacenter.""" dc_objs = self._session._call_method(vim_util, "get_objects", "Datacenter", ["vmFolder"]) vm_util._cancel_retrieve_if_necessary(self._session, dc_objs) # There is only one default datacenter in a standalone ESX host vm_folder_ref = dc_objs.objects[0].propSet[0].val return vm_folder_ref def _create_folder_if_missing(self, ds_name, ds_ref, folder): """Create a folder if it does not exist. Currently there are two folder that are required on the datastore - base folder - the folder to store cached images - temp folder - the folder used for snapshot management and image uploading This method is aimed to be used for the management of those folders to ensure that they are created if they are missing. The ds_util method mkdir will be used to check if the folder exists. If this throws and exception 'FileAlreadyExistsException' then the folder already exists on the datastore. """ path = ds_util.DatastorePath(ds_name, folder) dc_info = self.get_datacenter_ref_and_name(ds_ref) try: ds_util.mkdir(self._session, path, dc_info.ref) LOG.debug("Folder %s created.", path) except vexc.FileAlreadyExistsException: # NOTE(hartsocks): if the folder already exists, that # just means the folder was prepped by another process. pass def check_cache_folder(self, ds_name, ds_ref): """Check that the cache folder exists.""" self._create_folder_if_missing(ds_name, ds_ref, self._base_folder) def check_temp_folder(self, ds_name, ds_ref): """Check that the temp folder exists.""" self._create_folder_if_missing(ds_name, ds_ref, self._tmp_folder) def _check_if_folder_file_exists(self, ds_browser, ds_ref, ds_name, folder_name, file_name): # Ensure that the cache folder exists self.check_cache_folder(ds_name, ds_ref) # Check if the file exists or not. folder_ds_path = ds_util.DatastorePath(ds_name, folder_name) return ds_util.file_exists( self._session, ds_browser, folder_ds_path, file_name) def inject_network_info(self, instance, network_info): """inject network info for specified instance.""" # Set the machine.id parameter of the instance to inject # the NIC configuration inside the VM client_factory = self._session._get_vim().client.factory self._set_machine_id(client_factory, instance, network_info) def manage_image_cache(self, context, instances): if not CONF.remove_unused_base_images: LOG.debug("Image aging disabled. Aging will not be done.") return datastores = ds_util.get_available_datastores(self._session, self._cluster, self._datastore_regex) datastores_info = [] for ds in datastores: dc_info = self.get_datacenter_ref_and_name(ds.ref) datastores_info.append((ds, dc_info)) self._imagecache.update(context, instances, datastores_info) def _get_valid_vms_from_retrieve_result(self, retrieve_result): """Returns list of valid vms from RetrieveResult object.""" lst_vm_names = [] while retrieve_result: token = vm_util._get_token(retrieve_result) for vm in retrieve_result.objects: vm_name = None conn_state = None for prop in vm.propSet: if prop.name == "name": vm_name = prop.val elif prop.name == "runtime.connectionState": conn_state = prop.val # Ignoring the orphaned or inaccessible VMs if conn_state not in ["orphaned", "inaccessible"]: lst_vm_names.append(vm_name) if token: retrieve_result = self._session._call_method(vim_util, "continue_to_get_objects", token) else: break return lst_vm_names def instance_exists(self, instance): try: vm_util.get_vm_ref(self._session, instance) return True except exception.InstanceNotFound: return False def attach_interface(self, instance, image_meta, vif): """Attach an interface to the instance.""" vif_model = image_meta.get("hw_vif_model", constants.DEFAULT_VIF_MODEL) vif_model = vm_util.convert_vif_model(vif_model) vif_info = vmwarevif.get_vif_dict(self._session, self._cluster, vif_model, utils.is_neutron(), vif) vm_ref = vm_util.get_vm_ref(self._session, instance) # Ensure that there is not a race with the port index management with lockutils.lock(instance.uuid, lock_file_prefix='nova-vmware-hot-plug'): port_index = vm_util.get_attach_port_index(self._session, vm_ref) client_factory = self._session._get_vim().client.factory attach_config_spec = vm_util.get_network_attach_config_spec( client_factory, vif_info, port_index) LOG.debug("Reconfiguring VM to attach interface", instance=instance) try: vm_util.reconfigure_vm(self._session, vm_ref, attach_config_spec) except Exception as e: LOG.error(_LE('Attaching network adapter failed. Exception: ' ' %s'), e, instance=instance) raise exception.InterfaceAttachFailed( instance_uuid=instance['uuid']) LOG.debug("Reconfigured VM to attach interface", instance=instance) def detach_interface(self, instance, vif): """Detach an interface from the instance.""" vm_ref = vm_util.get_vm_ref(self._session, instance) # Ensure that there is not a race with the port index management with lockutils.lock(instance.uuid, lock_file_prefix='nova-vmware-hot-plug'): port_index = vm_util.get_vm_detach_port_index(self._session, vm_ref, vif['id']) if port_index is None: msg = _("No device with interface-id %s exists on " "VM") % vif['id'] raise exception.NotFound(msg) hardware_devices = self._session._call_method(vim_util, "get_dynamic_property", vm_ref, "VirtualMachine", "config.hardware.device") device = vmwarevif.get_network_device(hardware_devices, vif['address']) if device is None: msg = _("No device with MAC address %s exists on the " "VM") % vif['address'] raise exception.NotFound(msg) client_factory = self._session._get_vim().client.factory detach_config_spec = vm_util.get_network_detach_config_spec( client_factory, device, port_index) LOG.debug("Reconfiguring VM to detach interface", instance=instance) try: vm_util.reconfigure_vm(self._session, vm_ref, detach_config_spec) except Exception as e: LOG.error(_LE('Detaching network adapter failed. Exception: ' '%s'), e, instance=instance) raise exception.InterfaceDetachFailed( instance_uuid=instance['uuid']) LOG.debug("Reconfigured VM to detach interface", instance=instance) def _use_disk_image_as_full_clone(self, vm_ref, vi): """Uses cached image disk by copying it into the VM directory.""" instance_folder = vi.instance_name root_disk_name = "%s.vmdk" % vi.instance_name root_disk_ds_loc = vi.datastore.build_path(instance_folder, root_disk_name) vm_util.copy_virtual_disk( self._session, vi.dc_info.ref, str(vi.cache_image_path), str(root_disk_ds_loc)) self._extend_if_required( vi.dc_info, vi.ii, vi.instance, str(root_disk_ds_loc)) self._volumeops.attach_disk_to_vm( vm_ref, vi.instance, vi.ii.adapter_type, vi.ii.disk_type, str(root_disk_ds_loc), vi.root_gb * units.Mi, False) def _sized_image_exists(self, sized_disk_ds_loc, ds_ref): ds_browser = self._get_ds_browser(ds_ref) return ds_util.file_exists( self._session, ds_browser, sized_disk_ds_loc.parent, sized_disk_ds_loc.basename) def _use_disk_image_as_linked_clone(self, vm_ref, vi): """Uses cached image as parent of a COW child in the VM directory.""" sized_image_disk_name = "%s.vmdk" % vi.ii.image_id if vi.root_gb > 0: sized_image_disk_name = "%s.%s.vmdk" % (vi.ii.image_id, vi.root_gb) sized_disk_ds_loc = vi.cache_image_folder.join(sized_image_disk_name) # Ensure only a single thread extends the image at once. # We do this by taking a lock on the name of the extended # image. This allows multiple threads to create resized # copies simultaneously, as long as they are different # sizes. Threads attempting to create the same resized copy # will be serialized, with only the first actually creating # the copy. # # Note that the object is in a per-nova cache directory, # so inter-nova locking is not a concern. Consequently we # can safely use simple thread locks. with lockutils.lock(str(sized_disk_ds_loc), lock_file_prefix='nova-vmware-image'): if not self._sized_image_exists(sized_disk_ds_loc, vi.datastore.ref): LOG.debug("Copying root disk of size %sGb", vi.root_gb) try: vm_util.copy_virtual_disk( self._session, vi.dc_info.ref, str(vi.cache_image_path), str(sized_disk_ds_loc)) except Exception as e: LOG.warning(_("Root disk file creation " "failed - %s"), e) with excutils.save_and_reraise_exception(): LOG.error(_LE('Failed to copy cached ' 'image %(source)s to ' '%(dest)s for resize: ' '%(error)s'), {'source': vi.cache_image_path, 'dest': sized_disk_ds_loc, 'error': e.message}) try: ds_util.file_delete(self._session, sized_disk_ds_loc, vi.dc_info.ref) except vexc.FileNotFoundException: # File was never created: cleanup not # required pass # Resize the copy to the appropriate size. No need # for cleanup up here, as _extend_virtual_disk # already does it self._extend_if_required( vi.dc_info, vi.ii, vi.instance, str(sized_disk_ds_loc)) # Associate the sized image disk to the VM by attaching to the VM a # COW child of said disk. self._volumeops.attach_disk_to_vm( vm_ref, vi.instance, vi.ii.adapter_type, vi.ii.disk_type, str(sized_disk_ds_loc), vi.root_gb * units.Mi, vi.ii.linked_clone) def _use_iso_image(self, vm_ref, vi): """Uses cached image as a bootable virtual cdrom.""" self._attach_cdrom_to_vm( vm_ref, vi.instance, vi.datastore.ref, str(vi.cache_image_path)) # Optionally create and attach blank disk if vi.root_gb > 0: instance_folder = vi.instance_name root_disk_name = "%s.vmdk" % vi.instance_name root_disk_ds_loc = vi.datastore.build_path(instance_folder, root_disk_name) # It is pointless to COW a blank disk linked_clone = False vm_util.create_virtual_disk( self._session, vi.dc_info.ref, vi.ii.adapter_type, vi.ii.disk_type, str(root_disk_ds_loc), vi.root_gb * units.Mi) self._volumeops.attach_disk_to_vm( vm_ref, vi.instance, vi.ii.adapter_type, vi.ii.disk_type, str(root_disk_ds_loc), vi.root_gb * units.Mi, linked_clone) def _update_datacenter_cache_from_objects(self, dcs): """Updates the datastore/datacenter cache.""" while dcs: token = vm_util._get_token(dcs) for dco in dcs.objects: dc_ref = dco.obj ds_refs = [] prop_dict = vm_util.propset_dict(dco.propSet) name = prop_dict.get('name') vmFolder = prop_dict.get('vmFolder') datastore_refs = prop_dict.get('datastore') if datastore_refs: datastore_refs = datastore_refs.ManagedObjectReference for ds in datastore_refs: ds_refs.append(ds.value) else: LOG.debug("Datacenter %s doesn't have any datastore " "associated with it, ignoring it", name) for ds_ref in ds_refs: self._datastore_dc_mapping[ds_ref] = DcInfo(ref=dc_ref, name=name, vmFolder=vmFolder) if token: dcs = self._session._call_method(vim_util, "continue_to_get_objects", token) else: break def get_datacenter_ref_and_name(self, ds_ref): """Get the datacenter name and the reference.""" dc_info = self._datastore_dc_mapping.get(ds_ref.value) if not dc_info: dcs = self._session._call_method(vim_util, "get_objects", "Datacenter", ["name", "datastore", "vmFolder"]) self._update_datacenter_cache_from_objects(dcs) dc_info = self._datastore_dc_mapping.get(ds_ref.value) return dc_info def list_instances(self): """Lists the VM instances that are registered with vCenter cluster.""" properties = ['name', 'runtime.connectionState'] LOG.debug("Getting list of instances from cluster %s", self._cluster) vms = [] root_res_pool = self._session._call_method( vim_util, "get_dynamic_property", self._cluster, 'ClusterComputeResource', 'resourcePool') if root_res_pool: vms = self._session._call_method( vim_util, 'get_inner_objects', root_res_pool, 'vm', 'VirtualMachine', properties) lst_vm_names = self._get_valid_vms_from_retrieve_result(vms) LOG.debug("Got total of %s instances", str(len(lst_vm_names))) return lst_vm_names def get_vnc_console(self, instance): """Return connection info for a vnc console using vCenter logic.""" # vCenter does not run virtual machines and does not run # a VNC proxy. Instead, you need to tell OpenStack to talk # directly to the ESX host running the VM you are attempting # to connect to via VNC. vnc_console = self._get_vnc_console_connection(instance) host_name = vm_util.get_host_name_for_vm( self._session, instance) vnc_console['host'] = host_name # NOTE: VM can move hosts in some situations. Debug for admins. LOG.debug("VM %(uuid)s is currently on host %(host_name)s", {'uuid': instance.name, 'host_name': host_name}, instance=instance) return ctype.ConsoleVNC(**vnc_console)
46.945607
79
0.58108
import collections import copy import os import time import decorator from oslo.config import cfg from oslo.vmware import exceptions as vexc from nova.api.metadata import base as instance_metadata from nova import compute from nova.compute import power_state from nova.compute import task_states from nova.compute import vm_states from nova.console import type as ctype from nova import context as nova_context from nova import exception from nova.i18n import _, _LE, _LW from nova import objects from nova.openstack.common import excutils from nova.openstack.common import lockutils from nova.openstack.common import log as logging from nova.openstack.common import units from nova.openstack.common import uuidutils from nova import utils from nova.virt import configdrive from nova.virt import diagnostics from nova.virt import driver from nova.virt.vmwareapi import constants from nova.virt.vmwareapi import ds_util from nova.virt.vmwareapi import error_util from nova.virt.vmwareapi import imagecache from nova.virt.vmwareapi import vif as vmwarevif from nova.virt.vmwareapi import vim_util from nova.virt.vmwareapi import vm_util from nova.virt.vmwareapi import vmware_images CONF = cfg.CONF CONF.import_opt('image_cache_subdirectory_name', 'nova.virt.imagecache') CONF.import_opt('remove_unused_base_images', 'nova.virt.imagecache') CONF.import_opt('vnc_enabled', 'nova.vnc') CONF.import_opt('my_ip', 'nova.netconf') LOG = logging.getLogger(__name__) VMWARE_POWER_STATES = { 'poweredOff': power_state.SHUTDOWN, 'poweredOn': power_state.RUNNING, 'suspended': power_state.SUSPENDED} RESIZE_TOTAL_STEPS = 4 DcInfo = collections.namedtuple('DcInfo', ['ref', 'name', 'vmFolder']) class VirtualMachineInstanceConfigInfo(object): def __init__(self, instance, instance_name, image_info, datastore, dc_info, image_cache): self.instance = instance self.instance_name = instance_name or instance.uuid self.ii = image_info self.root_gb = instance.root_gb self.datastore = datastore self.dc_info = dc_info self._image_cache = image_cache @property def cache_image_folder(self): if self.ii.image_id is None: return return self._image_cache.get_image_cache_folder( self.datastore, self.ii.image_id) @property def cache_image_path(self): if self.ii.image_id is None: return cached_image_file_name = "%s.%s" % (self.ii.image_id, self.ii.file_type) return self.cache_image_folder.join(cached_image_file_name) def _time_sleep_wrapper(delay): time.sleep(delay) @decorator.decorator def retry_if_task_in_progress(f, *args, **kwargs): retries = max(CONF.vmware.api_retry_count, 1) delay = 1 for attempt in range(1, retries + 1): if attempt != 1: _time_sleep_wrapper(delay) delay = min(2 * delay, 60) try: f(*args, **kwargs) return except error_util.TaskInProgress: pass class VMwareVMOps(object): def __init__(self, session, virtapi, volumeops, cluster=None, datastore_regex=None): self.compute_api = compute.API() self._session = session self._virtapi = virtapi self._volumeops = volumeops self._cluster = cluster self._datastore_regex = datastore_regex if CONF.remove_unused_base_images: self._base_folder = '%s%s' % (CONF.my_ip, CONF.image_cache_subdirectory_name) else: self._base_folder = CONF.image_cache_subdirectory_name self._tmp_folder = 'vmware_temp' self._default_root_device = 'vda' self._rescue_suffix = '-rescue' self._migrate_suffix = '-orig' self._datastore_dc_mapping = {} self._datastore_browser_mapping = {} self._imagecache = imagecache.ImageCacheManager(self._session, self._base_folder) def _extend_virtual_disk(self, instance, requested_size, name, dc_ref): service_content = self._session._get_vim().service_content LOG.debug("Extending root virtual disk to %s", requested_size) vmdk_extend_task = self._session._call_method( self._session._get_vim(), "ExtendVirtualDisk_Task", service_content.virtualDiskManager, name=name, datacenter=dc_ref, newCapacityKb=requested_size, eagerZero=False) try: self._session._wait_for_task(vmdk_extend_task) except Exception as e: with excutils.save_and_reraise_exception(): LOG.error(_('Extending virtual disk failed with error: %s'), e, instance=instance) files = [name.replace(".vmdk", "-flat.vmdk"), name] for file in files: ds_path = ds_util.DatastorePath.parse(file) self._delete_datastore_file(instance, ds_path, dc_ref) LOG.debug("Extended root virtual disk") def _delete_datastore_file(self, instance, datastore_path, dc_ref): try: ds_util.file_delete(self._session, datastore_path, dc_ref) except (vexc.CannotDeleteFileException, vexc.FileFaultException, vexc.FileLockedException, vexc.FileNotFoundException): LOG.debug("Unable to delete %(ds)s. There may be more than " "one process or thread trying to delete the file", {'ds': datastore_path}, exc_info=True) def _extend_if_required(self, dc_info, image_info, instance, root_vmdk_path): if instance.root_gb > image_info.file_size_in_gb: size_in_kb = instance.root_gb * units.Mi self._extend_virtual_disk(instance, size_in_kb, root_vmdk_path, dc_info.ref) def _configure_config_drive(self, instance, vm_ref, dc_info, datastore, injected_files, admin_password): session_vim = self._session._get_vim() cookies = session_vim.client.options.transport.cookiejar uploaded_iso_path = self._create_config_drive(instance, injected_files, admin_password, datastore.name, dc_info.name, instance['uuid'], cookies) uploaded_iso_path = datastore.build_path(uploaded_iso_path) self._attach_cdrom_to_vm( vm_ref, instance, datastore.ref, str(uploaded_iso_path)) def build_virtual_machine(self, instance, instance_name, image_info, dc_info, datastore, network_info): node_mo_id = vm_util.get_mo_id_from_instance(instance) res_pool_ref = vm_util.get_res_pool_ref(self._session, self._cluster, node_mo_id) vif_infos = vmwarevif.get_vif_info(self._session, self._cluster, utils.is_neutron(), image_info.vif_model, network_info) allocations = self._get_cpu_allocations(instance.instance_type_id) client_factory = self._session._get_vim().client.factory config_spec = vm_util.get_vm_create_spec(client_factory, instance, instance_name, datastore.name, vif_infos, image_info.os_type, allocations=allocations) vm_ref = vm_util.create_vm(self._session, instance, dc_info.vmFolder, config_spec, res_pool_ref) return vm_ref def _get_cpu_allocations(self, instance_type_id): flavor = objects.Flavor.get_by_id( nova_context.get_admin_context(read_deleted='yes'), instance_type_id) allocations = {} for (key, type) in (('cpu_limit', int), ('cpu_reservation', int), ('cpu_shares_level', str), ('cpu_shares_share', int)): value = flavor.extra_specs.get('quota:' + key) if value: allocations[key] = type(value) return allocations def _fetch_image_as_file(self, context, vi, image_ds_loc): session = self._session session_vim = session._get_vim() cookies = session_vim.client.options.transport.cookiejar LOG.debug("Downloading image file data %(image_id)s to " "%(file_path)s on the data store " "%(datastore_name)s", {'image_id': vi.ii.image_id, 'file_path': image_ds_loc, 'datastore_name': vi.datastore.name}, instance=vi.instance) vmware_images.fetch_image( context, vi.instance, session._host, vi.dc_info.name, vi.datastore.name, image_ds_loc.rel_path, cookies=cookies) def _prepare_sparse_image(self, vi): tmp_dir_loc = vi.datastore.build_path( self._tmp_folder, uuidutils.generate_uuid()) tmp_image_ds_loc = tmp_dir_loc.join( vi.ii.image_id, "tmp-sparse.vmdk") return tmp_dir_loc, tmp_image_ds_loc def _prepare_flat_image(self, vi): tmp_dir_loc = vi.datastore.build_path( self._tmp_folder, uuidutils.generate_uuid()) tmp_image_ds_loc = tmp_dir_loc.join( vi.ii.image_id, vi.cache_image_path.basename) ds_util.mkdir(self._session, tmp_image_ds_loc.parent, vi.dc_info.ref) vm_util.create_virtual_disk( self._session, vi.dc_info.ref, vi.ii.adapter_type, vi.ii.disk_type, str(tmp_image_ds_loc), vi.ii.file_size_in_kb) flat_vmdk_name = vi.cache_image_path.basename.replace('.vmdk', '-flat.vmdk') flat_vmdk_ds_loc = tmp_dir_loc.join(vi.ii.image_id, flat_vmdk_name) self._delete_datastore_file(vi.instance, str(flat_vmdk_ds_loc), vi.dc_info.ref) return tmp_dir_loc, flat_vmdk_ds_loc def _prepare_iso_image(self, vi): tmp_dir_loc = vi.datastore.build_path( self._tmp_folder, uuidutils.generate_uuid()) tmp_image_ds_loc = tmp_dir_loc.join( vi.ii.image_id, vi.cache_image_path.basename) return tmp_dir_loc, tmp_image_ds_loc def _move_to_cache(self, dc_ref, src_folder_ds_path, dst_folder_ds_path): try: ds_util.file_move(self._session, dc_ref, src_folder_ds_path, dst_folder_ds_path) except vexc.FileAlreadyExistsException: LOG.warning(_LW("Destination %s already exists! Concurrent moves " "can lead to unexpected results."), dst_folder_ds_path) def _cache_sparse_image(self, vi, tmp_image_ds_loc): tmp_dir_loc = tmp_image_ds_loc.parent.parent converted_image_ds_loc = tmp_dir_loc.join( vi.ii.image_id, vi.cache_image_path.basename) vm_util.copy_virtual_disk( self._session, vi.dc_info.ref, str(tmp_image_ds_loc), str(converted_image_ds_loc)) self._delete_datastore_file(vi.instance, str(tmp_image_ds_loc), vi.dc_info.ref) self._move_to_cache(vi.dc_info.ref, tmp_image_ds_loc.parent, vi.cache_image_folder) def _cache_flat_image(self, vi, tmp_image_ds_loc): self._move_to_cache(vi.dc_info.ref, tmp_image_ds_loc.parent, vi.cache_image_folder) def _cache_iso_image(self, vi, tmp_image_ds_loc): self._move_to_cache(vi.dc_info.ref, tmp_image_ds_loc.parent, vi.cache_image_folder) def _get_vm_config_info(self, instance, image_info, instance_name=None): if (instance.root_gb != 0 and image_info.file_size_in_gb > instance.root_gb): reason = _("Image disk size greater than requested disk size") raise exception.InstanceUnacceptable(instance_id=instance.uuid, reason=reason) datastore = ds_util.get_datastore( self._session, self._cluster, self._datastore_regex) dc_info = self.get_datacenter_ref_and_name(datastore.ref) return VirtualMachineInstanceConfigInfo(instance, instance_name, image_info, datastore, dc_info, self._imagecache) def _get_image_callbacks(self, vi): disk_type = vi.ii.disk_type image_fetch = self._fetch_image_as_file if vi.ii.is_iso: image_prepare = self._prepare_iso_image image_cache = self._cache_iso_image elif disk_type == constants.DISK_TYPE_SPARSE: image_prepare = self._prepare_sparse_image image_cache = self._cache_sparse_image elif disk_type in constants.SUPPORTED_FLAT_VARIANTS: image_prepare = self._prepare_flat_image image_cache = self._cache_flat_image else: reason = _("disk type '%s' not supported") % disk_type raise exception.InvalidDiskInfo(reason=reason) return image_prepare, image_fetch, image_cache def _fetch_image_if_missing(self, context, vi): image_prepare, image_fetch, image_cache = self._get_image_callbacks(vi) LOG.debug("Processing image %s", vi.ii.image_id) with lockutils.lock(str(vi.cache_image_path), lock_file_prefix='nova-vmware-fetch_image'): self.check_cache_folder(vi.datastore.name, vi.datastore.ref) ds_browser = self._get_ds_browser(vi.datastore.ref) if not ds_util.file_exists(self._session, ds_browser, vi.cache_image_folder, vi.cache_image_path.basename): LOG.debug("Preparing fetch location") tmp_dir_loc, tmp_image_ds_loc = image_prepare(vi) LOG.debug("Fetch image to %s", tmp_image_ds_loc) image_fetch(context, vi, tmp_image_ds_loc) LOG.debug("Caching image") image_cache(vi, tmp_image_ds_loc) LOG.debug("Cleaning up location %s", str(tmp_dir_loc)) self._delete_datastore_file(vi.instance, str(tmp_dir_loc), vi.dc_info.ref) def spawn(self, context, instance, image_meta, injected_files, admin_password, network_info, block_device_info=None, instance_name=None, power_on=True): client_factory = self._session._get_vim().client.factory image_info = vmware_images.VMwareImage.from_image(instance.image_ref, image_meta) vi = self._get_vm_config_info(instance, image_info, instance_name) vm_ref = self.build_virtual_machine(instance, vi.instance_name, image_info, vi.dc_info, vi.datastore, network_info) vm_util.vm_ref_cache_update(vi.instance_name, vm_ref) if CONF.flat_injected: self._set_machine_id(client_factory, instance, network_info) if CONF.vnc_enabled: self._get_and_set_vnc_config(client_factory, instance) block_device_mapping = [] if block_device_info is not None: block_device_mapping = driver.block_device_info_get_mapping( block_device_info) if len(block_device_mapping) > 0: msg = "Block device information present: %s" % block_device_info LOG.debug(logging.mask_password(msg), instance=instance) for root_disk in block_device_mapping: connection_info = root_disk['connection_info'] self._volumeops.attach_root_volume(connection_info, instance, self._default_root_device, vi.datastore.ref) else: self._imagecache.enlist_image( image_info.image_id, vi.datastore, vi.dc_info.ref) self._fetch_image_if_missing(context, vi) if image_info.is_iso: self._use_iso_image(vm_ref, vi) elif image_info.linked_clone: self._use_disk_image_as_linked_clone(vm_ref, vi) else: self._use_disk_image_as_full_clone(vm_ref, vi) if configdrive.required_by(instance): self._configure_config_drive( instance, vm_ref, vi.dc_info, vi.datastore, injected_files, admin_password) if power_on: vm_util.power_on_instance(self._session, instance, vm_ref=vm_ref) def _create_config_drive(self, instance, injected_files, admin_password, data_store_name, dc_name, upload_folder, cookies): if CONF.config_drive_format != 'iso9660': reason = (_('Invalid config_drive_format "%s"') % CONF.config_drive_format) raise exception.InstancePowerOnFailure(reason=reason) LOG.info(_('Using config drive for instance'), instance=instance) extra_md = {} if admin_password: extra_md['admin_pass'] = admin_password inst_md = instance_metadata.InstanceMetadata(instance, content=injected_files, extra_md=extra_md) try: with configdrive.ConfigDriveBuilder(instance_md=inst_md) as cdb: with utils.tempdir() as tmp_path: tmp_file = os.path.join(tmp_path, 'configdrive.iso') cdb.make_drive(tmp_file) upload_iso_path = "%s/configdrive.iso" % ( upload_folder) vmware_images.upload_iso_to_datastore( tmp_file, instance, host=self._session._host, data_center_name=dc_name, datastore_name=data_store_name, cookies=cookies, file_path=upload_iso_path) return upload_iso_path except Exception as e: with excutils.save_and_reraise_exception(): LOG.error(_('Creating config drive failed with error: %s'), e, instance=instance) def _attach_cdrom_to_vm(self, vm_ref, instance, datastore, file_path): client_factory = self._session._get_vim().client.factory devices = self._session._call_method(vim_util, "get_dynamic_property", vm_ref, "VirtualMachine", "config.hardware.device") (controller_key, unit_number, controller_spec) = vm_util.allocate_controller_key_and_unit_number( client_factory, devices, 'ide') cdrom_attach_config_spec = vm_util.get_cdrom_attach_config_spec( client_factory, datastore, file_path, controller_key, unit_number) if controller_spec: cdrom_attach_config_spec.deviceChange.append(controller_spec) LOG.debug("Reconfiguring VM instance to attach cdrom %s", file_path, instance=instance) vm_util.reconfigure_vm(self._session, vm_ref, cdrom_attach_config_spec) LOG.debug("Reconfigured VM instance to attach cdrom %s", file_path, instance=instance) def _create_vm_snapshot(self, instance, vm_ref): LOG.debug("Creating Snapshot of the VM instance", instance=instance) snapshot_task = self._session._call_method( self._session._get_vim(), "CreateSnapshot_Task", vm_ref, name="%s-snapshot" % instance.uuid, description="Taking Snapshot of the VM", memory=False, quiesce=True) self._session._wait_for_task(snapshot_task) LOG.debug("Created Snapshot of the VM instance", instance=instance) task_info = self._session._call_method(vim_util, "get_dynamic_property", snapshot_task, "Task", "info") snapshot = task_info.result return snapshot @retry_if_task_in_progress def _delete_vm_snapshot(self, instance, vm_ref, snapshot): LOG.debug("Deleting Snapshot of the VM instance", instance=instance) delete_snapshot_task = self._session._call_method( self._session._get_vim(), "RemoveSnapshot_Task", snapshot, removeChildren=False, consolidate=True) self._session._wait_for_task(delete_snapshot_task) LOG.debug("Deleted Snapshot of the VM instance", instance=instance) def snapshot(self, context, instance, image_id, update_task_state): vm_ref = vm_util.get_vm_ref(self._session, instance) service_content = self._session._get_vim().service_content def _get_vm_and_vmdk_attribs(): hw_devices = self._session._call_method(vim_util, "get_dynamic_property", vm_ref, "VirtualMachine", "config.hardware.device") (vmdk_file_path_before_snapshot, adapter_type, disk_type) = vm_util.get_vmdk_path_and_adapter_type( hw_devices, uuid=instance.uuid) if not vmdk_file_path_before_snapshot: LOG.debug("No root disk defined. Unable to snapshot.") raise error_util.NoRootDiskDefined() datastore_name = ds_util.DatastorePath.parse( vmdk_file_path_before_snapshot).datastore os_type = self._session._call_method(vim_util, "get_dynamic_property", vm_ref, "VirtualMachine", "summary.config.guestId") return (vmdk_file_path_before_snapshot, adapter_type, disk_type, datastore_name, os_type) (vmdk_file_path_before_snapshot, adapter_type, disk_type, datastore_name, os_type) = _get_vm_and_vmdk_attribs() snapshot = self._create_vm_snapshot(instance, vm_ref) update_task_state(task_state=task_states.IMAGE_PENDING_UPLOAD) def _check_if_tmp_folder_exists(): ds_ref_ret = self._session._call_method( vim_util, "get_dynamic_property", vm_ref, "VirtualMachine", "datastore") if ds_ref_ret is None: raise exception.DatastoreNotFound() ds_ref = ds_ref_ret.ManagedObjectReference[0] self.check_temp_folder(datastore_name, ds_ref) return ds_ref ds_ref = _check_if_tmp_folder_exists() # name clashes. random_name = uuidutils.generate_uuid() dest_vmdk_file_path = ds_util.DatastorePath( datastore_name, self._tmp_folder, "%s.vmdk" % random_name) dest_vmdk_data_file_path = ds_util.DatastorePath( datastore_name, self._tmp_folder, "%s-flat.vmdk" % random_name) dc_info = self.get_datacenter_ref_and_name(ds_ref) def _copy_vmdk_content(): # Consolidate the snapshotted disk to a temporary vmdk. LOG.debug('Copying snapshotted disk %s.', vmdk_file_path_before_snapshot, instance=instance) copy_disk_task = self._session._call_method( self._session._get_vim(), "CopyVirtualDisk_Task", service_content.virtualDiskManager, sourceName=vmdk_file_path_before_snapshot, sourceDatacenter=dc_info.ref, destName=str(dest_vmdk_file_path), destDatacenter=dc_info.ref, force=False) self._session._wait_for_task(copy_disk_task) LOG.debug('Copied snapshotted disk %s.', vmdk_file_path_before_snapshot, instance=instance) _copy_vmdk_content() self._delete_vm_snapshot(instance, vm_ref, snapshot) cookies = self._session._get_vim().client.options.transport.cookiejar def _upload_vmdk_to_image_repository(): # Upload the contents of -flat.vmdk file which has the disk data. LOG.debug("Uploading image %s", image_id, instance=instance) vmware_images.upload_image( context, image_id, instance, os_type=os_type, disk_type=constants.DEFAULT_DISK_TYPE, adapter_type=adapter_type, image_version=1, host=self._session._host, data_center_name=dc_info.name, datastore_name=datastore_name, cookies=cookies, file_path="%s/%s-flat.vmdk" % (self._tmp_folder, random_name)) LOG.debug("Uploaded image %s", image_id, instance=instance) update_task_state(task_state=task_states.IMAGE_UPLOADING, expected_state=task_states.IMAGE_PENDING_UPLOAD) _upload_vmdk_to_image_repository() def _clean_temp_data(): # The data file is the one occupying space, and likelier to see # deletion problems, so prioritize its deletion first. In the # unlikely event that its deletion fails, the small descriptor file # is retained too by design since it makes little sense to remove # it when the data disk it refers to still lingers. for f in dest_vmdk_data_file_path, dest_vmdk_file_path: self._delete_datastore_file(instance, f, dc_info.ref) _clean_temp_data() def reboot(self, instance, network_info): vm_ref = vm_util.get_vm_ref(self._session, instance) lst_properties = ["summary.guest.toolsStatus", "runtime.powerState", "summary.guest.toolsRunningStatus"] props = self._session._call_method(vim_util, "get_object_properties", None, vm_ref, "VirtualMachine", lst_properties) query = vm_util.get_values_from_object_properties(self._session, props) pwr_state = query['runtime.powerState'] tools_status = query['summary.guest.toolsStatus'] tools_running_status = query['summary.guest.toolsRunningStatus'] # Raise an exception if the VM is not powered On. if pwr_state not in ["poweredOn"]: reason = _("instance is not powered on") raise exception.InstanceRebootFailure(reason=reason) # If latest vmware tools are installed in the VM, and that the tools # are running, then only do a guest reboot. Otherwise do a hard reset. if (tools_status == "toolsOk" and tools_running_status == "guestToolsRunning"): LOG.debug("Rebooting guest OS of VM", instance=instance) self._session._call_method(self._session._get_vim(), "RebootGuest", vm_ref) LOG.debug("Rebooted guest OS of VM", instance=instance) else: LOG.debug("Doing hard reboot of VM", instance=instance) reset_task = self._session._call_method(self._session._get_vim(), "ResetVM_Task", vm_ref) self._session._wait_for_task(reset_task) LOG.debug("Did hard reboot of VM", instance=instance) def _destroy_instance(self, instance, destroy_disks=True, instance_name=None): # Destroy a VM instance # Get the instance name. In some cases this may differ from the 'uuid', # for example when the spawn of a rescue instance takes place. if instance_name is None: instance_name = instance['uuid'] try: vm_ref = vm_util.get_vm_ref_from_name(self._session, instance_name) if vm_ref is None: LOG.warning(_('Instance does not exist on backend'), instance=instance) return lst_properties = ["config.files.vmPathName", "runtime.powerState", "datastore"] props = self._session._call_method(vim_util, "get_object_properties", None, vm_ref, "VirtualMachine", lst_properties) query = vm_util.get_values_from_object_properties( self._session, props) pwr_state = query['runtime.powerState'] vm_config_pathname = query['config.files.vmPathName'] vm_ds_path = None if vm_config_pathname: vm_ds_path = ds_util.DatastorePath.parse(vm_config_pathname) # Power off the VM if it is in PoweredOn state. if pwr_state == "poweredOn": vm_util.power_off_instance(self._session, instance, vm_ref) # Un-register the VM try: LOG.debug("Unregistering the VM", instance=instance) self._session._call_method(self._session._get_vim(), "UnregisterVM", vm_ref) LOG.debug("Unregistered the VM", instance=instance) except Exception as excep: LOG.warn(_("In vmwareapi:vmops:_destroy_instance, got this " "exception while un-registering the VM: %s"), excep) # Delete the folder holding the VM related content on # the datastore. if destroy_disks and vm_ds_path: try: dir_ds_compliant_path = vm_ds_path.parent LOG.debug("Deleting contents of the VM from " "datastore %(datastore_name)s", {'datastore_name': vm_ds_path.datastore}, instance=instance) ds_ref_ret = query['datastore'] ds_ref = ds_ref_ret.ManagedObjectReference[0] dc_info = self.get_datacenter_ref_and_name(ds_ref) ds_util.file_delete(self._session, dir_ds_compliant_path, dc_info.ref) LOG.debug("Deleted contents of the VM from " "datastore %(datastore_name)s", {'datastore_name': vm_ds_path.datastore}, instance=instance) except Exception: LOG.warn(_("In vmwareapi:vmops:_destroy_instance, " "exception while deleting the VM contents from " "the disk"), exc_info=True) except Exception as exc: LOG.exception(exc, instance=instance) finally: vm_util.vm_ref_cache_delete(instance_name) def destroy(self, instance, destroy_disks=True): # If there is a rescue VM then we need to destroy that one too. LOG.debug("Destroying instance", instance=instance) if instance['vm_state'] == vm_states.RESCUED: LOG.debug("Rescue VM configured", instance=instance) try: self.unrescue(instance, power_on=False) LOG.debug("Rescue VM destroyed", instance=instance) except Exception: rescue_name = instance['uuid'] + self._rescue_suffix self._destroy_instance(instance, destroy_disks=destroy_disks, instance_name=rescue_name) # NOTE(arnaud): Destroy uuid-orig and uuid VMs iff it is not # triggered by the revert resize api call. This prevents # the uuid-orig VM to be deleted to be able to associate it later. if instance.task_state != task_states.RESIZE_REVERTING: # When VM deletion is triggered in middle of VM resize before VM # arrive RESIZED state, uuid-orig VM need to deleted to avoid # VM leak. Within method _destroy_instance it will check vmref # exist or not before attempt deletion. resize_orig_vmname = instance['uuid'] + self._migrate_suffix vm_orig_ref = vm_util.get_vm_ref_from_name(self._session, resize_orig_vmname) if vm_orig_ref: self._destroy_instance(instance, destroy_disks=destroy_disks, instance_name=resize_orig_vmname) self._destroy_instance(instance, destroy_disks=destroy_disks) LOG.debug("Instance destroyed", instance=instance) def pause(self, instance): msg = _("pause not supported for vmwareapi") raise NotImplementedError(msg) def unpause(self, instance): msg = _("unpause not supported for vmwareapi") raise NotImplementedError(msg) def suspend(self, instance): vm_ref = vm_util.get_vm_ref(self._session, instance) pwr_state = self._session._call_method(vim_util, "get_dynamic_property", vm_ref, "VirtualMachine", "runtime.powerState") # Only PoweredOn VMs can be suspended. if pwr_state == "poweredOn": LOG.debug("Suspending the VM", instance=instance) suspend_task = self._session._call_method(self._session._get_vim(), "SuspendVM_Task", vm_ref) self._session._wait_for_task(suspend_task) LOG.debug("Suspended the VM", instance=instance) # Raise Exception if VM is poweredOff elif pwr_state == "poweredOff": reason = _("instance is powered off and cannot be suspended.") raise exception.InstanceSuspendFailure(reason=reason) else: LOG.debug("VM was already in suspended state. So returning " "without doing anything", instance=instance) def resume(self, instance): vm_ref = vm_util.get_vm_ref(self._session, instance) pwr_state = self._session._call_method(vim_util, "get_dynamic_property", vm_ref, "VirtualMachine", "runtime.powerState") if pwr_state.lower() == "suspended": LOG.debug("Resuming the VM", instance=instance) suspend_task = self._session._call_method( self._session._get_vim(), "PowerOnVM_Task", vm_ref) self._session._wait_for_task(suspend_task) LOG.debug("Resumed the VM", instance=instance) else: reason = _("instance is not in a suspended state") raise exception.InstanceResumeFailure(reason=reason) def rescue(self, context, instance, network_info, image_meta): vm_ref = vm_util.get_vm_ref(self._session, instance) self.power_off(instance) r_instance = copy.deepcopy(instance) instance_name = r_instance.uuid + self._rescue_suffix self.spawn(context, r_instance, image_meta, None, None, network_info, instance_name=instance_name, power_on=False) # Attach vmdk to the rescue VM hardware_devices = self._session._call_method(vim_util, "get_dynamic_property", vm_ref, "VirtualMachine", "config.hardware.device") (vmdk_path, adapter_type, disk_type) = vm_util.get_vmdk_path_and_adapter_type( hardware_devices, uuid=instance.uuid) rescue_vm_ref = vm_util.get_vm_ref_from_name(self._session, instance_name) self._volumeops.attach_disk_to_vm( rescue_vm_ref, r_instance, adapter_type, disk_type, vmdk_path) vm_util.power_on_instance(self._session, r_instance, vm_ref=rescue_vm_ref) def unrescue(self, instance, power_on=True): # Get the original vmdk_path vm_ref = vm_util.get_vm_ref(self._session, instance) hardware_devices = self._session._call_method(vim_util, "get_dynamic_property", vm_ref, "VirtualMachine", "config.hardware.device") (vmdk_path, adapter_type, disk_type) = vm_util.get_vmdk_path_and_adapter_type( hardware_devices, uuid=instance.uuid) r_instance = copy.deepcopy(instance) instance_name = r_instance.uuid + self._rescue_suffix # detach the original instance disk from the rescue disk vm_rescue_ref = vm_util.get_vm_ref_from_name(self._session, instance_name) hardware_devices = self._session._call_method(vim_util, "get_dynamic_property", vm_rescue_ref, "VirtualMachine", "config.hardware.device") device = vm_util.get_vmdk_volume_disk(hardware_devices, path=vmdk_path) vm_util.power_off_instance(self._session, r_instance, vm_rescue_ref) self._volumeops.detach_disk_from_vm(vm_rescue_ref, r_instance, device) self._destroy_instance(r_instance, instance_name=instance_name) if power_on: vm_util.power_on_instance(self._session, instance, vm_ref=vm_ref) def power_off(self, instance): vm_util.power_off_instance(self._session, instance) def power_on(self, instance): vm_util.power_on_instance(self._session, instance) def _get_orig_vm_name_label(self, instance): return instance.uuid + '-orig' def _update_instance_progress(self, context, instance, step, total_steps): # Divide the action's workflow into discrete steps and "bump" the # # For a first cut this should be fine, however, for large VM images, # the clone disk step begins to dominate the equation. A # better approximation would use the percentage of the VM image that # has been streamed to the destination host. progress = round(float(step) / total_steps * 100) instance_uuid = instance.uuid LOG.debug("Updating instance '%(instance_uuid)s' progress to" " %(progress)d", {'instance_uuid': instance_uuid, 'progress': progress}, instance=instance) instance.progress = progress instance.save() def migrate_disk_and_power_off(self, context, instance, dest, flavor): # Checks if the migration needs a disk resize down. if flavor['root_gb'] < instance['root_gb']: reason = _("Unable to shrink disk.") raise exception.InstanceFaultRollback( exception.ResizeError(reason=reason)) # 0. Zero out the progress to begin self._update_instance_progress(context, instance, step=0, total_steps=RESIZE_TOTAL_STEPS) vm_ref = vm_util.get_vm_ref(self._session, instance) # Read the host_ref for the destination. If this is None then the # VC will decide on placement host_ref = self._get_host_ref_from_name(dest) # 1. Power off the instance self.power_off(instance) self._update_instance_progress(context, instance, step=1, total_steps=RESIZE_TOTAL_STEPS) # 2. Disassociate the linked vsphere VM from the instance vm_util.disassociate_vmref_from_instance(self._session, instance, vm_ref, suffix=self._migrate_suffix) self._update_instance_progress(context, instance, step=2, total_steps=RESIZE_TOTAL_STEPS) ds_ref = ds_util.get_datastore( self._session, self._cluster, datastore_regex=self._datastore_regex).ref dc_info = self.get_datacenter_ref_and_name(ds_ref) # 3. Clone the VM for instance vm_util.clone_vmref_for_instance(self._session, instance, vm_ref, host_ref, ds_ref, dc_info.vmFolder) self._update_instance_progress(context, instance, step=3, total_steps=RESIZE_TOTAL_STEPS) def confirm_migration(self, migration, instance, network_info): # Destroy the original VM. The vm_ref needs to be searched using the # instance.uuid + self._migrate_suffix as the identifier. We will # not get the vm when searched using the instanceUuid but rather will # be found using the uuid buried in the extraConfig vm_ref = vm_util.search_vm_ref_by_identifier(self._session, instance.uuid + self._migrate_suffix) if vm_ref is None: LOG.debug("instance not present", instance=instance) return try: LOG.debug("Destroying the VM", instance=instance) destroy_task = self._session._call_method( self._session._get_vim(), "Destroy_Task", vm_ref) self._session._wait_for_task(destroy_task) LOG.debug("Destroyed the VM", instance=instance) except Exception as excep: LOG.warn(_("In vmwareapi:vmops:confirm_migration, got this " "exception while destroying the VM: %s"), excep) def finish_revert_migration(self, context, instance, network_info, block_device_info, power_on=True): vm_util.associate_vmref_for_instance(self._session, instance, suffix=self._migrate_suffix) if power_on: vm_util.power_on_instance(self._session, instance) def finish_migration(self, context, migration, instance, disk_info, network_info, image_meta, resize_instance=False, block_device_info=None, power_on=True): vm_ref = vm_util.get_vm_ref(self._session, instance) if resize_instance: client_factory = self._session._get_vim().client.factory vm_resize_spec = vm_util.get_vm_resize_spec(client_factory, instance) vm_util.reconfigure_vm(self._session, vm_ref, vm_resize_spec) # Resize the disk (if larger) old_root_gb = instance.system_metadata['old_instance_type_root_gb'] if instance['root_gb'] > int(old_root_gb): root_disk_in_kb = instance['root_gb'] * units.Mi vmdk_path = vm_util.get_vmdk_path(self._session, vm_ref, instance) data_store_ref = ds_util.get_datastore(self._session, self._cluster, datastore_regex=self._datastore_regex).ref dc_info = self.get_datacenter_ref_and_name(data_store_ref) self._extend_virtual_disk(instance, root_disk_in_kb, vmdk_path, dc_info.ref) # TODO(ericwb): add extend for ephemeral disk # 4. Start VM if power_on: vm_util.power_on_instance(self._session, instance, vm_ref=vm_ref) self._update_instance_progress(context, instance, step=4, total_steps=RESIZE_TOTAL_STEPS) def live_migration(self, context, instance_ref, dest, post_method, recover_method, block_migration=False): vm_ref = vm_util.get_vm_ref(self._session, instance_ref) host_ref = self._get_host_ref_from_name(dest) if host_ref is None: raise exception.HostNotFound(host=dest) LOG.debug("Migrating VM to host %s", dest, instance=instance_ref) try: vm_migrate_task = self._session._call_method( self._session._get_vim(), "MigrateVM_Task", vm_ref, host=host_ref, priority="defaultPriority") self._session._wait_for_task(vm_migrate_task) except Exception: with excutils.save_and_reraise_exception(): recover_method(context, instance_ref, dest, block_migration) post_method(context, instance_ref, dest, block_migration) LOG.debug("Migrated VM to host %s", dest, instance=instance_ref) def poll_rebooting_instances(self, timeout, instances): ctxt = nova_context.get_admin_context() instances_info = dict(instance_count=len(instances), timeout=timeout) if instances_info["instance_count"] > 0: LOG.info(_("Found %(instance_count)d hung reboots " "older than %(timeout)d seconds") % instances_info) for instance in instances: LOG.info(_("Automatically hard rebooting"), instance=instance) self.compute_api.reboot(ctxt, instance, "HARD") def get_info(self, instance): vm_ref = vm_util.get_vm_ref(self._session, instance) lst_properties = ["summary.config.numCpu", "summary.config.memorySizeMB", "runtime.powerState"] vm_props = self._session._call_method(vim_util, "get_object_properties", None, vm_ref, "VirtualMachine", lst_properties) query = vm_util.get_values_from_object_properties( self._session, vm_props) max_mem = int(query['summary.config.memorySizeMB']) * 1024 return {'state': VMWARE_POWER_STATES[query['runtime.powerState']], 'max_mem': max_mem, 'mem': max_mem, 'num_cpu': int(query['summary.config.numCpu']), 'cpu_time': 0} def _get_diagnostics(self, instance): vm_ref = vm_util.get_vm_ref(self._session, instance) lst_properties = ["summary.config", "summary.quickStats", "summary.runtime"] vm_props = self._session._call_method(vim_util, "get_object_properties", None, vm_ref, "VirtualMachine", lst_properties) query = vm_util.get_values_from_object_properties(self._session, vm_props) data = {} # All of values received are objects. Convert them to dictionaries for value in query.values(): prop_dict = vim_util.object_to_dict(value, list_depth=1) data.update(prop_dict) return data def get_diagnostics(self, instance): data = self._get_diagnostics(instance) # Add a namespace to all of the diagnostsics return dict([('vmware:' + k, v) for k, v in data.items()]) def get_instance_diagnostics(self, instance): data = self._get_diagnostics(instance) state = data.get('powerState') if state: state = power_state.STATE_MAP[VMWARE_POWER_STATES[state]] uptime = data.get('uptimeSeconds', 0) config_drive = configdrive.required_by(instance) diags = diagnostics.Diagnostics(state=state, driver='vmwareapi', config_drive=config_drive, hypervisor_os='esxi', uptime=uptime) diags.memory_details.maximum = data.get('memorySizeMB', 0) diags.memory_details.used = data.get('guestMemoryUsage', 0) # TODO(garyk): add in cpu, nic and disk stats return diags def _get_vnc_console_connection(self, instance): vm_ref = vm_util.get_vm_ref(self._session, instance) opt_value = self._session._call_method(vim_util, 'get_dynamic_property', vm_ref, 'VirtualMachine', vm_util.VNC_CONFIG_KEY) if opt_value: port = int(opt_value.value) else: raise exception.ConsoleTypeUnavailable(console_type='vnc') return {'port': port, 'internal_access_path': None} @staticmethod def _get_machine_id_str(network_info): machine_id_str = '' for vif in network_info: # TODO(vish): add support for dns2 # TODO(sateesh): add support for injection of ipv6 configuration network = vif['network'] ip_v4 = netmask_v4 = gateway_v4 = broadcast_v4 = dns = None subnets_v4 = [s for s in network['subnets'] if s['version'] == 4] if len(subnets_v4) > 0: if len(subnets_v4[0]['ips']) > 0: ip_v4 = subnets_v4[0]['ips'][0] if len(subnets_v4[0]['dns']) > 0: dns = subnets_v4[0]['dns'][0]['address'] netmask_v4 = str(subnets_v4[0].as_netaddr().netmask) gateway_v4 = subnets_v4[0]['gateway']['address'] broadcast_v4 = str(subnets_v4[0].as_netaddr().broadcast) interface_str = ";".join([vif['address'], ip_v4 and ip_v4['address'] or '', netmask_v4 or '', gateway_v4 or '', broadcast_v4 or '', dns or '']) machine_id_str = machine_id_str + interface_str + ' return machine_id_str def _set_machine_id(self, client_factory, instance, network_info): vm_ref = vm_util.get_vm_ref(self._session, instance) machine_id_change_spec = vm_util.get_machine_id_change_spec( client_factory, self._get_machine_id_str(network_info)) LOG.debug("Reconfiguring VM instance to set the machine id", instance=instance) vm_util.reconfigure_vm(self._session, vm_ref, machine_id_change_spec) LOG.debug("Reconfigured VM instance to set the machine id", instance=instance) @utils.synchronized('vmware.get_and_set_vnc_port') def _get_and_set_vnc_config(self, client_factory, instance): port = vm_util.get_vnc_port(self._session) vm_ref = vm_util.get_vm_ref(self._session, instance) vnc_config_spec = vm_util.get_vnc_config_spec( client_factory, port) LOG.debug("Reconfiguring VM instance to enable vnc on " "port - %(port)s", {'port': port}, instance=instance) vm_util.reconfigure_vm(self._session, vm_ref, vnc_config_spec) LOG.debug("Reconfigured VM instance to enable vnc on " "port - %(port)s", {'port': port}, instance=instance) def _get_ds_browser(self, ds_ref): ds_browser = self._datastore_browser_mapping.get(ds_ref.value) if not ds_browser: ds_browser = self._session._call_method( vim_util, "get_dynamic_property", ds_ref, "Datastore", "browser") self._datastore_browser_mapping[ds_ref.value] = ds_browser return ds_browser def _get_host_ref_from_name(self, host_name): host_objs = self._session._call_method(vim_util, "get_objects", "HostSystem", ["name"]) vm_util._cancel_retrieve_if_necessary(self._session, host_objs) for host in host_objs: if hasattr(host, 'propSet'): if host.propSet[0].val == host_name: return host.obj return None def _get_vmfolder_ref(self): dc_objs = self._session._call_method(vim_util, "get_objects", "Datacenter", ["vmFolder"]) vm_util._cancel_retrieve_if_necessary(self._session, dc_objs) # There is only one default datacenter in a standalone ESX host vm_folder_ref = dc_objs.objects[0].propSet[0].val return vm_folder_ref def _create_folder_if_missing(self, ds_name, ds_ref, folder): path = ds_util.DatastorePath(ds_name, folder) dc_info = self.get_datacenter_ref_and_name(ds_ref) try: ds_util.mkdir(self._session, path, dc_info.ref) LOG.debug("Folder %s created.", path) except vexc.FileAlreadyExistsException: # NOTE(hartsocks): if the folder already exists, that # just means the folder was prepped by another process. pass def check_cache_folder(self, ds_name, ds_ref): self._create_folder_if_missing(ds_name, ds_ref, self._base_folder) def check_temp_folder(self, ds_name, ds_ref): self._create_folder_if_missing(ds_name, ds_ref, self._tmp_folder) def _check_if_folder_file_exists(self, ds_browser, ds_ref, ds_name, folder_name, file_name): # Ensure that the cache folder exists self.check_cache_folder(ds_name, ds_ref) # Check if the file exists or not. folder_ds_path = ds_util.DatastorePath(ds_name, folder_name) return ds_util.file_exists( self._session, ds_browser, folder_ds_path, file_name) def inject_network_info(self, instance, network_info): # Set the machine.id parameter of the instance to inject # the NIC configuration inside the VM client_factory = self._session._get_vim().client.factory self._set_machine_id(client_factory, instance, network_info) def manage_image_cache(self, context, instances): if not CONF.remove_unused_base_images: LOG.debug("Image aging disabled. Aging will not be done.") return datastores = ds_util.get_available_datastores(self._session, self._cluster, self._datastore_regex) datastores_info = [] for ds in datastores: dc_info = self.get_datacenter_ref_and_name(ds.ref) datastores_info.append((ds, dc_info)) self._imagecache.update(context, instances, datastores_info) def _get_valid_vms_from_retrieve_result(self, retrieve_result): lst_vm_names = [] while retrieve_result: token = vm_util._get_token(retrieve_result) for vm in retrieve_result.objects: vm_name = None conn_state = None for prop in vm.propSet: if prop.name == "name": vm_name = prop.val elif prop.name == "runtime.connectionState": conn_state = prop.val # Ignoring the orphaned or inaccessible VMs if conn_state not in ["orphaned", "inaccessible"]: lst_vm_names.append(vm_name) if token: retrieve_result = self._session._call_method(vim_util, "continue_to_get_objects", token) else: break return lst_vm_names def instance_exists(self, instance): try: vm_util.get_vm_ref(self._session, instance) return True except exception.InstanceNotFound: return False def attach_interface(self, instance, image_meta, vif): vif_model = image_meta.get("hw_vif_model", constants.DEFAULT_VIF_MODEL) vif_model = vm_util.convert_vif_model(vif_model) vif_info = vmwarevif.get_vif_dict(self._session, self._cluster, vif_model, utils.is_neutron(), vif) vm_ref = vm_util.get_vm_ref(self._session, instance) # Ensure that there is not a race with the port index management with lockutils.lock(instance.uuid, lock_file_prefix='nova-vmware-hot-plug'): port_index = vm_util.get_attach_port_index(self._session, vm_ref) client_factory = self._session._get_vim().client.factory attach_config_spec = vm_util.get_network_attach_config_spec( client_factory, vif_info, port_index) LOG.debug("Reconfiguring VM to attach interface", instance=instance) try: vm_util.reconfigure_vm(self._session, vm_ref, attach_config_spec) except Exception as e: LOG.error(_LE('Attaching network adapter failed. Exception: ' ' %s'), e, instance=instance) raise exception.InterfaceAttachFailed( instance_uuid=instance['uuid']) LOG.debug("Reconfigured VM to attach interface", instance=instance) def detach_interface(self, instance, vif): vm_ref = vm_util.get_vm_ref(self._session, instance) # Ensure that there is not a race with the port index management with lockutils.lock(instance.uuid, lock_file_prefix='nova-vmware-hot-plug'): port_index = vm_util.get_vm_detach_port_index(self._session, vm_ref, vif['id']) if port_index is None: msg = _("No device with interface-id %s exists on " "VM") % vif['id'] raise exception.NotFound(msg) hardware_devices = self._session._call_method(vim_util, "get_dynamic_property", vm_ref, "VirtualMachine", "config.hardware.device") device = vmwarevif.get_network_device(hardware_devices, vif['address']) if device is None: msg = _("No device with MAC address %s exists on the " "VM") % vif['address'] raise exception.NotFound(msg) client_factory = self._session._get_vim().client.factory detach_config_spec = vm_util.get_network_detach_config_spec( client_factory, device, port_index) LOG.debug("Reconfiguring VM to detach interface", instance=instance) try: vm_util.reconfigure_vm(self._session, vm_ref, detach_config_spec) except Exception as e: LOG.error(_LE('Detaching network adapter failed. Exception: ' '%s'), e, instance=instance) raise exception.InterfaceDetachFailed( instance_uuid=instance['uuid']) LOG.debug("Reconfigured VM to detach interface", instance=instance) def _use_disk_image_as_full_clone(self, vm_ref, vi): instance_folder = vi.instance_name root_disk_name = "%s.vmdk" % vi.instance_name root_disk_ds_loc = vi.datastore.build_path(instance_folder, root_disk_name) vm_util.copy_virtual_disk( self._session, vi.dc_info.ref, str(vi.cache_image_path), str(root_disk_ds_loc)) self._extend_if_required( vi.dc_info, vi.ii, vi.instance, str(root_disk_ds_loc)) self._volumeops.attach_disk_to_vm( vm_ref, vi.instance, vi.ii.adapter_type, vi.ii.disk_type, str(root_disk_ds_loc), vi.root_gb * units.Mi, False) def _sized_image_exists(self, sized_disk_ds_loc, ds_ref): ds_browser = self._get_ds_browser(ds_ref) return ds_util.file_exists( self._session, ds_browser, sized_disk_ds_loc.parent, sized_disk_ds_loc.basename) def _use_disk_image_as_linked_clone(self, vm_ref, vi): sized_image_disk_name = "%s.vmdk" % vi.ii.image_id if vi.root_gb > 0: sized_image_disk_name = "%s.%s.vmdk" % (vi.ii.image_id, vi.root_gb) sized_disk_ds_loc = vi.cache_image_folder.join(sized_image_disk_name) # Ensure only a single thread extends the image at once. # We do this by taking a lock on the name of the extended # image. This allows multiple threads to create resized # copies simultaneously, as long as they are different # sizes. Threads attempting to create the same resized copy # will be serialized, with only the first actually creating # the copy. # # Note that the object is in a per-nova cache directory, # so inter-nova locking is not a concern. Consequently we # can safely use simple thread locks. with lockutils.lock(str(sized_disk_ds_loc), lock_file_prefix='nova-vmware-image'): if not self._sized_image_exists(sized_disk_ds_loc, vi.datastore.ref): LOG.debug("Copying root disk of size %sGb", vi.root_gb) try: vm_util.copy_virtual_disk( self._session, vi.dc_info.ref, str(vi.cache_image_path), str(sized_disk_ds_loc)) except Exception as e: LOG.warning(_("Root disk file creation " "failed - %s"), e) with excutils.save_and_reraise_exception(): LOG.error(_LE('Failed to copy cached ' 'image %(source)s to ' '%(dest)s for resize: ' '%(error)s'), {'source': vi.cache_image_path, 'dest': sized_disk_ds_loc, 'error': e.message}) try: ds_util.file_delete(self._session, sized_disk_ds_loc, vi.dc_info.ref) except vexc.FileNotFoundException: # File was never created: cleanup not # required pass # Resize the copy to the appropriate size. No need # for cleanup up here, as _extend_virtual_disk # already does it self._extend_if_required( vi.dc_info, vi.ii, vi.instance, str(sized_disk_ds_loc)) # Associate the sized image disk to the VM by attaching to the VM a # COW child of said disk. self._volumeops.attach_disk_to_vm( vm_ref, vi.instance, vi.ii.adapter_type, vi.ii.disk_type, str(sized_disk_ds_loc), vi.root_gb * units.Mi, vi.ii.linked_clone) def _use_iso_image(self, vm_ref, vi): self._attach_cdrom_to_vm( vm_ref, vi.instance, vi.datastore.ref, str(vi.cache_image_path)) # Optionally create and attach blank disk if vi.root_gb > 0: instance_folder = vi.instance_name root_disk_name = "%s.vmdk" % vi.instance_name root_disk_ds_loc = vi.datastore.build_path(instance_folder, root_disk_name) # It is pointless to COW a blank disk linked_clone = False vm_util.create_virtual_disk( self._session, vi.dc_info.ref, vi.ii.adapter_type, vi.ii.disk_type, str(root_disk_ds_loc), vi.root_gb * units.Mi) self._volumeops.attach_disk_to_vm( vm_ref, vi.instance, vi.ii.adapter_type, vi.ii.disk_type, str(root_disk_ds_loc), vi.root_gb * units.Mi, linked_clone) def _update_datacenter_cache_from_objects(self, dcs): while dcs: token = vm_util._get_token(dcs) for dco in dcs.objects: dc_ref = dco.obj ds_refs = [] prop_dict = vm_util.propset_dict(dco.propSet) name = prop_dict.get('name') vmFolder = prop_dict.get('vmFolder') datastore_refs = prop_dict.get('datastore') if datastore_refs: datastore_refs = datastore_refs.ManagedObjectReference for ds in datastore_refs: ds_refs.append(ds.value) else: LOG.debug("Datacenter %s doesn't have any datastore " "associated with it, ignoring it", name) for ds_ref in ds_refs: self._datastore_dc_mapping[ds_ref] = DcInfo(ref=dc_ref, name=name, vmFolder=vmFolder) if token: dcs = self._session._call_method(vim_util, "continue_to_get_objects", token) else: break def get_datacenter_ref_and_name(self, ds_ref): dc_info = self._datastore_dc_mapping.get(ds_ref.value) if not dc_info: dcs = self._session._call_method(vim_util, "get_objects", "Datacenter", ["name", "datastore", "vmFolder"]) self._update_datacenter_cache_from_objects(dcs) dc_info = self._datastore_dc_mapping.get(ds_ref.value) return dc_info def list_instances(self): properties = ['name', 'runtime.connectionState'] LOG.debug("Getting list of instances from cluster %s", self._cluster) vms = [] root_res_pool = self._session._call_method( vim_util, "get_dynamic_property", self._cluster, 'ClusterComputeResource', 'resourcePool') if root_res_pool: vms = self._session._call_method( vim_util, 'get_inner_objects', root_res_pool, 'vm', 'VirtualMachine', properties) lst_vm_names = self._get_valid_vms_from_retrieve_result(vms) LOG.debug("Got total of %s instances", str(len(lst_vm_names))) return lst_vm_names def get_vnc_console(self, instance): vnc_console = self._get_vnc_console_connection(instance) host_name = vm_util.get_host_name_for_vm( self._session, instance) vnc_console['host'] = host_name LOG.debug("VM %(uuid)s is currently on host %(host_name)s", {'uuid': instance.name, 'host_name': host_name}, instance=instance) return ctype.ConsoleVNC(**vnc_console)
true
true
f705ff18b45378b112b53ebc92088be2ecafca92
318
py
Python
output.py
ton86/worklog
1743972b1d7aceb5960a323516a671c3c444d1bc
[ "MIT" ]
null
null
null
output.py
ton86/worklog
1743972b1d7aceb5960a323516a671c3c444d1bc
[ "MIT" ]
null
null
null
output.py
ton86/worklog
1743972b1d7aceb5960a323516a671c3c444d1bc
[ "MIT" ]
null
null
null
from pathlib import Path import json # Directory dir = Path().resolve() # Configuration with open(dir/'config.json') as config_file: CONFIG = json.load(config_file) def output_dir(config = CONFIG): """ Return string for default output directory path. """ return(config['output_dir']['path'])
17.666667
52
0.685535
from pathlib import Path import json dir = Path().resolve() with open(dir/'config.json') as config_file: CONFIG = json.load(config_file) def output_dir(config = CONFIG): return(config['output_dir']['path'])
true
true
f705ff6551509dfbb0053affdd45604b366b75b9
4,516
py
Python
abel/tools/polar.py
Derollez/PyAbel
c8be4ed7e8e08ee026634b9e856fb473e58d7330
[ "MIT" ]
null
null
null
abel/tools/polar.py
Derollez/PyAbel
c8be4ed7e8e08ee026634b9e856fb473e58d7330
[ "MIT" ]
null
null
null
abel/tools/polar.py
Derollez/PyAbel
c8be4ed7e8e08ee026634b9e856fb473e58d7330
[ "MIT" ]
null
null
null
# -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals import numpy as np from scipy.ndimage import map_coordinates from scipy.ndimage.interpolation import shift from scipy.optimize import curve_fit, minimize def reproject_image_into_polar(data, origin=None, Jacobian=False, dr=1, dt=None): """ Reprojects a 2D numpy array (``data``) into a polar coordinate system. "origin" is a tuple of (x0, y0) relative to the bottom-left image corner, and defaults to the center of the image. Parameters ---------- data : 2D np.array origin : tuple The coordinate of the image center, relative to bottom-left Jacobian : boolean Include ``r`` intensity scaling in the coordinate transform. This should be included to account for the changing pixel size that occurs during the transform. dr : float Radial coordinate spacing for the grid interpolation tests show that there is not much point in going below 0.5 dt : float Angular coordinate spacing (in radians) if ``dt=None``, dt will be set such that the number of theta values is equal to the maximum value between the height or the width of the image. Returns ------- output : 2D np.array The polar image (r, theta) r_grid : 2D np.array meshgrid of radial coordinates theta_grid : 2D np.array meshgrid of theta coordinates Notes ----- Adapted from: http://stackoverflow.com/questions/3798333/image-information-along-a-polar-coordinate-system """ # bottom-left coordinate system requires numpy image to be np.flipud data = np.flipud(data) ny, nx = data.shape[:2] if origin is None: origin = (nx//2, ny//2) # Determine that the min and max r and theta coords will be... x, y = index_coords(data, origin=origin) # (x,y) coordinates of each pixel r, theta = cart2polar(x, y) # convert (x,y) -> (r,θ), note θ=0 is vertical nr = np.int(np.ceil((r.max()-r.min())/dr)) if dt is None: nt = max(nx, ny) else: # dt in radians nt = np.int(np.ceil((theta.max()-theta.min())/dt)) # Make a regular (in polar space) grid based on the min and max r & theta r_i = np.linspace(r.min(), r.max(), nr, endpoint=False) theta_i = np.linspace(theta.min(), theta.max(), nt, endpoint=False) theta_grid, r_grid = np.meshgrid(theta_i, r_i) # Project the r and theta grid back into pixel coordinates X, Y = polar2cart(r_grid, theta_grid) X += origin[0] # We need to shift the origin Y += origin[1] # back to the bottom-left corner... xi, yi = X.flatten(), Y.flatten() coords = np.vstack((yi, xi)) # (map_coordinates requires a 2xn array) zi = map_coordinates(data, coords) output = zi.reshape((nr, nt)) if Jacobian: output = output*r_i[:, np.newaxis] return output, r_grid, theta_grid def index_coords(data, origin=None): """ Creates x & y coords for the indicies in a numpy array Parameters ---------- data : numpy array 2D data origin : (x,y) tuple defaults to the center of the image. Specify origin=(0,0) to set the origin to the *bottom-left* corner of the image. Returns ------- x, y : arrays """ ny, nx = data.shape[:2] if origin is None: origin_x, origin_y = nx//2, ny//2 else: origin_x, origin_y = origin x, y = np.meshgrid(np.arange(float(nx)), np.arange(float(ny))) x -= origin_x y -= origin_y return x, y def cart2polar(x, y): """ Transform Cartesian coordinates to polar Parameters ---------- x, y : floats or arrays Cartesian coordinates Returns ------- r, theta : floats or arrays Polar coordinates """ r = np.sqrt(x**2 + y**2) theta = np.arctan2(x, y) # θ referenced to vertical return r, theta def polar2cart(r, theta): """ Transform polar coordinates to Cartesian Parameters ------- r, theta : floats or arrays Polar coordinates Returns ---------- x, y : floats or arrays Cartesian coordinates """ y = r * np.cos(theta) # θ referenced to vertical x = r * np.sin(theta) return x, y
28.049689
96
0.613153
from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals import numpy as np from scipy.ndimage import map_coordinates from scipy.ndimage.interpolation import shift from scipy.optimize import curve_fit, minimize def reproject_image_into_polar(data, origin=None, Jacobian=False, dr=1, dt=None): data = np.flipud(data) ny, nx = data.shape[:2] if origin is None: origin = (nx//2, ny//2) x, y = index_coords(data, origin=origin) r, theta = cart2polar(x, y) nr = np.int(np.ceil((r.max()-r.min())/dr)) if dt is None: nt = max(nx, ny) else: nt = np.int(np.ceil((theta.max()-theta.min())/dt)) r_i = np.linspace(r.min(), r.max(), nr, endpoint=False) theta_i = np.linspace(theta.min(), theta.max(), nt, endpoint=False) theta_grid, r_grid = np.meshgrid(theta_i, r_i) X, Y = polar2cart(r_grid, theta_grid) X += origin[0] Y += origin[1] xi, yi = X.flatten(), Y.flatten() coords = np.vstack((yi, xi)) zi = map_coordinates(data, coords) output = zi.reshape((nr, nt)) if Jacobian: output = output*r_i[:, np.newaxis] return output, r_grid, theta_grid def index_coords(data, origin=None): ny, nx = data.shape[:2] if origin is None: origin_x, origin_y = nx//2, ny//2 else: origin_x, origin_y = origin x, y = np.meshgrid(np.arange(float(nx)), np.arange(float(ny))) x -= origin_x y -= origin_y return x, y def cart2polar(x, y): r = np.sqrt(x**2 + y**2) theta = np.arctan2(x, y) return r, theta def polar2cart(r, theta): y = r * np.cos(theta) x = r * np.sin(theta) return x, y
true
true
f705ffdd342fc61517b1e81622606cf4d4b511a4
41,696
py
Python
python/ccxt/async_support/okcoinusd.py
rkingy/ccxt
ba532b36df8aac8c39f638514fb613d077a23b01
[ "MIT" ]
5
2019-04-22T15:03:56.000Z
2019-10-27T13:49:32.000Z
python/ccxt/async_support/okcoinusd.py
rkingy/ccxt
ba532b36df8aac8c39f638514fb613d077a23b01
[ "MIT" ]
5
2019-12-28T05:46:26.000Z
2021-05-08T12:47:41.000Z
python/ccxt/async_support/okcoinusd.py
rkingy/ccxt
ba532b36df8aac8c39f638514fb613d077a23b01
[ "MIT" ]
3
2019-06-15T07:21:58.000Z
2020-06-07T18:24:20.000Z
# -*- coding: utf-8 -*- # PLEASE DO NOT EDIT THIS FILE, IT IS GENERATED AND WILL BE OVERWRITTEN: # https://github.com/ccxt/ccxt/blob/master/CONTRIBUTING.md#how-to-contribute-code from ccxt.async_support.base.exchange import Exchange import math from ccxt.base.errors import ExchangeError from ccxt.base.errors import AuthenticationError from ccxt.base.errors import ArgumentsRequired from ccxt.base.errors import InsufficientFunds from ccxt.base.errors import InvalidOrder from ccxt.base.errors import OrderNotFound from ccxt.base.errors import DDoSProtection class okcoinusd (Exchange): def describe(self): return self.deep_extend(super(okcoinusd, self).describe(), { 'id': 'okcoinusd', 'name': 'OKCoin USD', 'countries': ['CN', 'US'], 'version': 'v1', 'rateLimit': 1000, # up to 3000 requests per 5 minutes ≈ 600 requests per minute ≈ 10 requests per second ≈ 100 ms 'has': { 'CORS': False, 'fetchOHLCV': True, 'fetchOrder': True, 'fetchOrders': False, 'fetchOpenOrders': True, 'fetchClosedOrders': True, 'withdraw': True, 'futures': False, }, 'extension': '.do', # appended to endpoint URL 'timeframes': { '1m': '1min', '3m': '3min', '5m': '5min', '15m': '15min', '30m': '30min', '1h': '1hour', '2h': '2hour', '4h': '4hour', '6h': '6hour', '12h': '12hour', '1d': '1day', '3d': '3day', '1w': '1week', }, 'api': { 'web': { 'get': [ 'futures/pc/market/marketOverview', # todo: merge in fetchMarkets 'spot/markets/index-tickers', # todo: add fetchTickers 'spot/markets/currencies', 'spot/markets/products', 'spot/markets/tickers', 'spot/user-level', ], }, 'public': { 'get': [ 'depth', 'exchange_rate', 'future_depth', 'future_estimated_price', 'future_hold_amount', 'future_index', 'future_kline', 'future_price_limit', 'future_ticker', 'future_trades', 'kline', 'otcs', 'ticker', 'tickers', # todo: add fetchTickers 'trades', ], }, 'private': { 'post': [ 'account_records', 'batch_trade', 'borrow_money', 'borrow_order_info', 'borrows_info', 'cancel_borrow', 'cancel_order', 'cancel_otc_order', 'cancel_withdraw', 'funds_transfer', 'future_batch_trade', 'future_cancel', 'future_devolve', 'future_explosive', 'future_order_info', 'future_orders_info', 'future_position', 'future_position_4fix', 'future_trade', 'future_trades_history', 'future_userinfo', 'future_userinfo_4fix', 'lend_depth', 'order_fee', 'order_history', 'order_info', 'orders_info', 'otc_order_history', 'otc_order_info', 'repayment', 'submit_otc_order', 'trade', 'trade_history', 'trade_otc_order', 'wallet_info', 'withdraw', 'withdraw_info', 'unrepayments_info', 'userinfo', ], }, }, 'urls': { 'logo': 'https://user-images.githubusercontent.com/1294454/27766791-89ffb502-5ee5-11e7-8a5b-c5950b68ac65.jpg', 'api': { 'web': 'https://www.okcoin.com/v2', 'public': 'https://www.okcoin.com/api', 'private': 'https://www.okcoin.com', }, 'www': 'https://www.okcoin.com', 'doc': [ 'https://www.okcoin.com/docs/en/', 'https://www.npmjs.com/package/okcoin.com', ], }, 'fees': { 'trading': { 'taker': 0.002, 'maker': 0.002, }, }, 'exceptions': { # see https://github.com/okcoin-okex/API-docs-OKEx.com/blob/master/API-For-Spot-EN/Error%20Code%20For%20Spot.md '10000': ExchangeError, # "Required field, can not be null" '10001': DDoSProtection, # "Request frequency too high to exceed the limit allowed" '10005': AuthenticationError, # "'SecretKey' does not exist" '10006': AuthenticationError, # "'Api_key' does not exist" '10007': AuthenticationError, # "Signature does not match" '1002': InsufficientFunds, # "The transaction amount exceed the balance" '1003': InvalidOrder, # "The transaction amount is less than the minimum requirement" '1004': InvalidOrder, # "The transaction amount is less than 0" '1013': InvalidOrder, # no contract type(PR-1101) '1027': InvalidOrder, # createLimitBuyOrder(symbol, 0, 0): Incorrect parameter may exceeded limits '1050': InvalidOrder, # returned when trying to cancel an order that was filled or canceled previously '1217': InvalidOrder, # "Order was sent at ±5% of the current market price. Please resend" '10014': InvalidOrder, # "Order price must be between 0 and 1,000,000" '1009': OrderNotFound, # for spot markets, cancelling closed order '1019': OrderNotFound, # order closed?("Undo order failed") '1051': OrderNotFound, # for spot markets, cancelling "just closed" order '10009': OrderNotFound, # for spot markets, "Order does not exist" '20015': OrderNotFound, # for future markets '10008': ExchangeError, # Illegal URL parameter # todo: sort out below # 10000 Required parameter is empty # 10001 Request frequency too high to exceed the limit allowed # 10002 Authentication failure # 10002 System error # 10003 This connection has requested other user data # 10004 Request failed # 10005 api_key or sign is invalid, 'SecretKey' does not exist # 10006 'Api_key' does not exist # 10007 Signature does not match # 10008 Illegal parameter, Parameter erorr # 10009 Order does not exist # 10010 Insufficient funds # 10011 Amount too low # 10012 Only btc_usd ltc_usd supported # 10013 Only support https request # 10014 Order price must be between 0 and 1,000,000 # 10015 Order price differs from current market price too much / Channel subscription temporally not available # 10016 Insufficient coins balance # 10017 API authorization error / WebSocket authorization error # 10018 borrow amount less than lower limit [usd:100,btc:0.1,ltc:1] # 10019 loan agreement not checked # 1002 The transaction amount exceed the balance # 10020 rate cannot exceed 1% # 10021 rate cannot less than 0.01% # 10023 fail to get latest ticker # 10024 balance not sufficient # 10025 quota is full, cannot borrow temporarily # 10026 Loan(including reserved loan) and margin cannot be withdrawn # 10027 Cannot withdraw within 24 hrs of authentication information modification # 10028 Withdrawal amount exceeds daily limit # 10029 Account has unpaid loan, please cancel/pay off the loan before withdraw # 1003 The transaction amount is less than the minimum requirement # 10031 Deposits can only be withdrawn after 6 confirmations # 10032 Please enabled phone/google authenticator # 10033 Fee higher than maximum network transaction fee # 10034 Fee lower than minimum network transaction fee # 10035 Insufficient BTC/LTC # 10036 Withdrawal amount too low # 10037 Trade password not set # 1004 The transaction amount is less than 0 # 10040 Withdrawal cancellation fails # 10041 Withdrawal address not exsit or approved # 10042 Admin password error # 10043 Account equity error, withdrawal failure # 10044 fail to cancel borrowing order # 10047 self function is disabled for sub-account # 10048 withdrawal information does not exist # 10049 User can not have more than 50 unfilled small orders(amount<0.15BTC) # 10050 can't cancel more than once # 10051 order completed transaction # 10052 not allowed to withdraw # 10064 after a USD deposit, that portion of assets will not be withdrawable for the next 48 hours # 1007 No trading market information # 1008 No latest market information # 1009 No order # 1010 Different user of the cancelled order and the original order # 10100 User account frozen # 10101 order type is wrong # 10102 incorrect ID # 10103 the private otc order's key incorrect # 10106 API key domain not matched # 1011 No documented user # 1013 No order type # 1014 No login # 1015 No market depth information # 1017 Date error # 1018 Order failed # 1019 Undo order failed # 10216 Non-available API / non-public API # 1024 Currency does not exist # 1025 No chart type # 1026 No base currency quantity # 1027 Incorrect parameter may exceeded limits # 1028 Reserved decimal failed # 1029 Preparing # 1030 Account has margin and futures, transactions can not be processed # 1031 Insufficient Transferring Balance # 1032 Transferring Not Allowed # 1035 Password incorrect # 1036 Google Verification code Invalid # 1037 Google Verification code incorrect # 1038 Google Verification replicated # 1039 Message Verification Input exceed the limit # 1040 Message Verification invalid # 1041 Message Verification incorrect # 1042 Wrong Google Verification Input exceed the limit # 1043 Login password cannot be same as the trading password # 1044 Old password incorrect # 1045 2nd Verification Needed # 1046 Please input old password # 1048 Account Blocked # 1050 Orders have been withdrawn or withdrawn # 1051 Order completed # 1201 Account Deleted at 00: 00 # 1202 Account Not Exist # 1203 Insufficient Balance # 1204 Invalid currency # 1205 Invalid Account # 1206 Cash Withdrawal Blocked # 1207 Transfer Not Support # 1208 No designated account # 1209 Invalid api # 1216 Market order temporarily suspended. Please send limit order # 1217 Order was sent at ±5% of the current market price. Please resend # 1218 Place order failed. Please try again later # 20001 User does not exist # 20002 Account frozen # 20003 Account frozen due to forced liquidation # 20004 Contract account frozen # 20005 User contract account does not exist # 20006 Required field missing # 20007 Illegal parameter # 20008 Contract account balance is too low # 20009 Contract status error # 20010 Risk rate ratio does not exist # 20011 Risk rate lower than 90%/80% before opening BTC position with 10x/20x leverage. or risk rate lower than 80%/60% before opening LTC position with 10x/20x leverage # 20012 Risk rate lower than 90%/80% after opening BTC position with 10x/20x leverage. or risk rate lower than 80%/60% after opening LTC position with 10x/20x leverage # 20013 Temporally no counter party price # 20014 System error # 20015 Order does not exist # 20016 Close amount bigger than your open positions, liquidation quantity bigger than holding # 20017 Not authorized/illegal operation/illegal order ID # 20018 Order price cannot be more than 103-105% or less than 95-97% of the previous minute price # 20019 IP restricted from accessing the resource # 20020 Secret key does not exist # 20021 Index information does not exist # 20022 Wrong API interface(Cross margin mode shall call cross margin API, fixed margin mode shall call fixed margin API) # 20023 Account in fixed-margin mode # 20024 Signature does not match # 20025 Leverage rate error # 20026 API Permission Error # 20027 no transaction record # 20028 no such contract # 20029 Amount is large than available funds # 20030 Account still has debts # 20038 Due to regulation, self function is not availavle in the country/region your currently reside in. # 20049 Request frequency too high # 20100 request time out # 20101 the format of data is error # 20102 invalid login # 20103 event type error # 20104 subscription type error # 20107 JSON format error # 20115 The quote is not match # 20116 Param not match # 21020 Contracts are being delivered, orders cannot be placed # 21021 Contracts are being settled, contracts cannot be placed }, 'options': { 'marketBuyPrice': False, 'defaultContractType': 'this_week', # next_week, quarter 'warnOnFetchOHLCVLimitArgument': True, 'fiats': ['USD', 'CNY'], 'futures': { 'BCH': True, 'BTC': True, 'BTG': True, 'EOS': True, 'ETC': True, 'ETH': True, 'LTC': True, 'NEO': True, 'QTUM': True, 'USDT': True, 'XRP': True, }, }, }) async def fetch_markets(self, params={}): response = await self.webGetSpotMarketsProducts() markets = response['data'] result = [] for i in range(0, len(markets)): id = markets[i]['symbol'] baseId, quoteId = id.split('_') baseIdUppercase = baseId.upper() quoteIdUppercase = quoteId.upper() base = self.common_currency_code(baseIdUppercase) quote = self.common_currency_code(quoteIdUppercase) symbol = base + '/' + quote precision = { 'amount': markets[i]['maxSizeDigit'], 'price': markets[i]['maxPriceDigit'], } minAmount = markets[i]['minTradeSize'] minPrice = math.pow(10, -precision['price']) active = (markets[i]['online'] != 0) baseNumericId = markets[i]['baseCurrency'] quoteNumericId = markets[i]['quoteCurrency'] market = self.extend(self.fees['trading'], { 'id': id, 'symbol': symbol, 'base': base, 'quote': quote, 'baseId': baseId, 'quoteId': quoteId, 'baseNumericId': baseNumericId, 'quoteNumericId': quoteNumericId, 'info': markets[i], 'type': 'spot', 'spot': True, 'future': False, 'active': active, 'precision': precision, 'limits': { 'amount': { 'min': minAmount, 'max': None, }, 'price': { 'min': minPrice, 'max': None, }, 'cost': { 'min': minAmount * minPrice, 'max': None, }, }, }) result.append(market) if (self.has['futures']) and(market['base'] in list(self.options['futures'].keys())): fiats = self.options['fiats'] for j in range(0, len(fiats)): fiat = fiats[j] lowercaseFiat = fiat.lower() result.append(self.extend(market, { 'quote': fiat, 'symbol': market['base'] + '/' + fiat, 'id': market['base'].lower() + '_' + lowercaseFiat, 'quoteId': lowercaseFiat, 'type': 'future', 'spot': False, 'future': True, })) return result async def fetch_order_book(self, symbol, limit=None, params={}): await self.load_markets() market = self.market(symbol) method = 'publicGet' request = { 'symbol': market['id'], } if limit is not None: request['size'] = limit if market['future']: method += 'Future' request['contract_type'] = self.options['defaultContractType'] # self_week, next_week, quarter method += 'Depth' orderbook = await getattr(self, method)(self.extend(request, params)) return self.parse_order_book(orderbook) def parse_ticker(self, ticker, market=None): # # { buy: "48.777300", # change: "-1.244500", # changePercentage: "-2.47%", # close: "49.064000", # createdDate: 1531704852254, # currencyId: 527, # dayHigh: "51.012500", # dayLow: "48.124200", # high: "51.012500", # inflows: "0", # last: "49.064000", # low: "48.124200", # marketFrom: 627, # name: {}, # open: "50.308500", # outflows: "0", # productId: 527, # sell: "49.064000", # symbol: "zec_okb", # volume: "1049.092535" } # timestamp = self.safe_integer_2(ticker, 'timestamp', 'createdDate') symbol = None if market is None: if 'symbol' in ticker: marketId = ticker['symbol'] if marketId in self.markets_by_id: market = self.markets_by_id[marketId] else: baseId, quoteId = ticker['symbol'].split('_') base = baseId.upper() quote = quoteId.upper() base = self.common_currency_code(base) quote = self.common_currency_code(quote) symbol = base + '/' + quote if market is not None: symbol = market['symbol'] last = self.safe_float(ticker, 'last') open = self.safe_float(ticker, 'open') change = self.safe_float(ticker, 'change') percentage = self.safe_float(ticker, 'changePercentage') return { 'symbol': symbol, 'timestamp': timestamp, 'datetime': self.iso8601(timestamp), 'high': self.safe_float(ticker, 'high'), 'low': self.safe_float(ticker, 'low'), 'bid': self.safe_float(ticker, 'buy'), 'bidVolume': None, 'ask': self.safe_float(ticker, 'sell'), 'askVolume': None, 'vwap': None, 'open': open, 'close': last, 'last': last, 'previousClose': None, 'change': change, 'percentage': percentage, 'average': None, 'baseVolume': self.safe_float_2(ticker, 'vol', 'volume'), 'quoteVolume': None, 'info': ticker, } async def fetch_ticker(self, symbol, params={}): await self.load_markets() market = self.market(symbol) method = 'publicGet' request = { 'symbol': market['id'], } if market['future']: method += 'Future' request['contract_type'] = self.options['defaultContractType'] # self_week, next_week, quarter method += 'Ticker' response = await getattr(self, method)(self.extend(request, params)) ticker = self.safe_value(response, 'ticker') if ticker is None: raise ExchangeError(self.id + ' fetchTicker returned an empty response: ' + self.json(response)) timestamp = self.safe_integer(response, 'date') if timestamp is not None: timestamp *= 1000 ticker = self.extend(ticker, {'timestamp': timestamp}) return self.parse_ticker(ticker, market) def parse_trade(self, trade, market=None): symbol = None if market: symbol = market['symbol'] return { 'info': trade, 'timestamp': trade['date_ms'], 'datetime': self.iso8601(trade['date_ms']), 'symbol': symbol, 'id': str(trade['tid']), 'order': None, 'type': None, 'side': trade['type'], 'price': self.safe_float(trade, 'price'), 'amount': self.safe_float(trade, 'amount'), } async def fetch_trades(self, symbol, since=None, limit=None, params={}): await self.load_markets() market = self.market(symbol) method = 'publicGet' request = { 'symbol': market['id'], } if market['future']: method += 'Future' request['contract_type'] = self.options['defaultContractType'] # self_week, next_week, quarter method += 'Trades' response = await getattr(self, method)(self.extend(request, params)) return self.parse_trades(response, market, since, limit) def parse_ohlcv(self, ohlcv, market=None, timeframe='1m', since=None, limit=None): numElements = len(ohlcv) volumeIndex = 6 if (numElements > 6) else 5 return [ ohlcv[0], # timestamp float(ohlcv[1]), # Open float(ohlcv[2]), # High float(ohlcv[3]), # Low float(ohlcv[4]), # Close # float(ohlcv[5]), # quote volume # float(ohlcv[6]), # base volume float(ohlcv[volumeIndex]), # okex will return base volume in the 7th element for future markets ] async def fetch_ohlcv(self, symbol, timeframe='1m', since=None, limit=None, params={}): await self.load_markets() market = self.market(symbol) method = 'publicGet' request = { 'symbol': market['id'], 'type': self.timeframes[timeframe], } if market['future']: method += 'Future' request['contract_type'] = self.options['defaultContractType'] # self_week, next_week, quarter method += 'Kline' if limit is not None: if self.options['warnOnFetchOHLCVLimitArgument']: raise ExchangeError(self.id + ' fetchOHLCV counts "limit" candles from current time backwards, therefore the "limit" argument for ' + self.id + ' is disabled. Set ' + self.id + '.options["warnOnFetchOHLCVLimitArgument"] = False to suppress self warning message.') request['size'] = int(limit) # max is 1440 candles if since is not None: request['since'] = since else: request['since'] = self.milliseconds() - 86400000 # last 24 hours response = await getattr(self, method)(self.extend(request, params)) return self.parse_ohlcvs(response, market, timeframe, since, limit) async def fetch_balance(self, params={}): await self.load_markets() response = await self.privatePostUserinfo(params) balances = response['info']['funds'] result = {'info': response} ids = list(balances['free'].keys()) usedField = 'freezed' # wtf, okex? # https://github.com/okcoin-okex/API-docs-OKEx.com/commit/01cf9dd57b1f984a8737ef76a037d4d3795d2ac7 if not(usedField in list(balances.keys())): usedField = 'holds' usedKeys = list(balances[usedField].keys()) ids = self.array_concat(ids, usedKeys) for i in range(0, len(ids)): id = ids[i] code = id.upper() if id in self.currencies_by_id: code = self.currencies_by_id[id]['code'] else: code = self.common_currency_code(code) account = self.account() account['free'] = self.safe_float(balances['free'], id, 0.0) account['used'] = self.safe_float(balances[usedField], id, 0.0) account['total'] = self.sum(account['free'], account['used']) result[code] = account return self.parse_balance(result) async def create_order(self, symbol, type, side, amount, price=None, params={}): await self.load_markets() market = self.market(symbol) method = 'privatePost' order = { 'symbol': market['id'], 'type': side, } if market['future']: method += 'Future' order = self.extend(order, { 'contract_type': self.options['defaultContractType'], # self_week, next_week, quarter 'match_price': 0, # match best counter party price? 0 or 1, ignores price if 1 'lever_rate': 10, # leverage rate value: 10 or 20(10 by default) 'price': price, 'amount': amount, }) else: if type == 'limit': order['price'] = price order['amount'] = amount else: order['type'] += '_market' if side == 'buy': if self.options['marketBuyPrice']: if price is None: # eslint-disable-next-line quotes raise ExchangeError(self.id + " market buy orders require a price argument(the amount you want to spend or the cost of the order) when self.options['marketBuyPrice'] is True.") order['price'] = price else: order['price'] = self.safe_float(params, 'cost') if not order['price']: # eslint-disable-next-line quotes raise ExchangeError(self.id + " market buy orders require an additional cost parameter, cost = price * amount. If you want to pass the cost of the market order(the amount you want to spend) in the price argument(the default " + self.id + " behaviour), set self.options['marketBuyPrice'] = True. It will effectively suppress self warning exception as well.") else: order['amount'] = amount params = self.omit(params, 'cost') method += 'Trade' response = await getattr(self, method)(self.extend(order, params)) timestamp = self.milliseconds() return { 'info': response, 'id': str(response['order_id']), 'timestamp': timestamp, 'datetime': self.iso8601(timestamp), 'lastTradeTimestamp': None, 'status': None, 'symbol': symbol, 'type': type, 'side': side, 'price': price, 'amount': amount, 'filled': None, 'remaining': None, 'cost': None, 'trades': None, 'fee': None, } async def cancel_order(self, id, symbol=None, params={}): if symbol is None: raise ArgumentsRequired(self.id + ' cancelOrder() requires a symbol argument') await self.load_markets() market = self.market(symbol) request = { 'symbol': market['id'], 'order_id': id, } method = 'privatePost' if market['future']: method += 'FutureCancel' request['contract_type'] = self.options['defaultContractType'] # self_week, next_week, quarter else: method += 'CancelOrder' response = await getattr(self, method)(self.extend(request, params)) return response def parse_order_status(self, status): statuses = { '-1': 'canceled', '0': 'open', '1': 'open', '2': 'closed', '3': 'open', '4': 'canceled', } return self.safe_value(statuses, status, status) def parse_order_side(self, side): if side == 1: return 'buy' # open long position if side == 2: return 'sell' # open short position if side == 3: return 'sell' # liquidate long position if side == 4: return 'buy' # liquidate short position return side def parse_order(self, order, market=None): side = None type = None if 'type' in order: if (order['type'] == 'buy') or (order['type'] == 'sell'): side = order['type'] type = 'limit' elif order['type'] == 'buy_market': side = 'buy' type = 'market' elif order['type'] == 'sell_market': side = 'sell' type = 'market' else: side = self.parse_order_side(order['type']) if ('contract_name' in list(order.keys())) or ('lever_rate' in list(order.keys())): type = 'margin' status = self.parse_order_status(self.safe_string(order, 'status')) symbol = None if market is None: marketId = self.safe_string(order, 'symbol') if marketId in self.markets_by_id: market = self.markets_by_id[marketId] if market: symbol = market['symbol'] timestamp = None createDateField = self.get_create_date_field() if createDateField in order: timestamp = order[createDateField] amount = self.safe_float(order, 'amount') filled = self.safe_float(order, 'deal_amount') amount = max(amount, filled) remaining = max(0, amount - filled) if type == 'market': remaining = 0 average = self.safe_float(order, 'avg_price') # https://github.com/ccxt/ccxt/issues/2452 average = self.safe_float(order, 'price_avg', average) cost = average * filled result = { 'info': order, 'id': str(order['order_id']), 'timestamp': timestamp, 'datetime': self.iso8601(timestamp), 'lastTradeTimestamp': None, 'symbol': symbol, 'type': type, 'side': side, 'price': order['price'], 'average': average, 'cost': cost, 'amount': amount, 'filled': filled, 'remaining': remaining, 'status': status, 'fee': None, } return result def get_create_date_field(self): # needed for derived exchanges # allcoin typo create_data instead of create_date return 'create_date' def get_orders_field(self): # needed for derived exchanges # allcoin typo order instead of orders(expected based on their API docs) return 'orders' async def fetch_order(self, id, symbol=None, params={}): if symbol is None: raise ExchangeError(self.id + ' fetchOrder requires a symbol parameter') await self.load_markets() market = self.market(symbol) method = 'privatePost' request = { 'order_id': id, 'symbol': market['id'], # 'status': 0, # 0 for unfilled orders, 1 for filled orders # 'current_page': 1, # current page number # 'page_length': 200, # number of orders returned per page, maximum 200 } if market['future']: method += 'Future' request['contract_type'] = self.options['defaultContractType'] # self_week, next_week, quarter method += 'OrderInfo' response = await getattr(self, method)(self.extend(request, params)) ordersField = self.get_orders_field() numOrders = len(response[ordersField]) if numOrders > 0: return self.parse_order(response[ordersField][0]) raise OrderNotFound(self.id + ' order ' + id + ' not found') async def fetch_orders(self, symbol=None, since=None, limit=None, params={}): if symbol is None: raise ExchangeError(self.id + ' fetchOrders requires a symbol parameter') await self.load_markets() market = self.market(symbol) method = 'privatePost' request = { 'symbol': market['id'], } order_id_in_params = ('order_id' in list(params.keys())) if market['future']: method += 'FutureOrdersInfo' request['contract_type'] = self.options['defaultContractType'] # self_week, next_week, quarter if not order_id_in_params: raise ExchangeError(self.id + ' fetchOrders() requires order_id param for futures market ' + symbol + '(a string of one or more order ids, comma-separated)') else: status = None if 'type' in params: status = params['type'] elif 'status' in params: status = params['status'] else: name = 'type' if order_id_in_params else 'status' raise ExchangeError(self.id + ' fetchOrders() requires ' + name + ' param for spot market ' + symbol + '(0 - for unfilled orders, 1 - for filled/canceled orders)') if order_id_in_params: method += 'OrdersInfo' request = self.extend(request, { 'type': status, 'order_id': params['order_id'], }) else: method += 'OrderHistory' request = self.extend(request, { 'status': status, 'current_page': 1, # current page number 'page_length': 200, # number of orders returned per page, maximum 200 }) params = self.omit(params, ['type', 'status']) response = await getattr(self, method)(self.extend(request, params)) ordersField = self.get_orders_field() return self.parse_orders(response[ordersField], market, since, limit) async def fetch_open_orders(self, symbol=None, since=None, limit=None, params={}): open = 0 # 0 for unfilled orders, 1 for filled orders return await self.fetch_orders(symbol, since, limit, self.extend({ 'status': open, }, params)) async def fetch_closed_orders(self, symbol=None, since=None, limit=None, params={}): closed = 1 # 0 for unfilled orders, 1 for filled orders orders = await self.fetch_orders(symbol, since, limit, self.extend({ 'status': closed, }, params)) return orders async def withdraw(self, code, amount, address, tag=None, params={}): self.check_address(address) await self.load_markets() currency = self.currency(code) # if amount < 0.01: # raise ExchangeError(self.id + ' withdraw() requires amount > 0.01') # for some reason they require to supply a pair of currencies for withdrawing one currency currencyId = currency['id'] + '_usd' if tag: address = address + ':' + tag request = { 'symbol': currencyId, 'withdraw_address': address, 'withdraw_amount': amount, 'target': 'address', # or 'okcn', 'okcom', 'okex' } query = params if 'chargefee' in query: request['chargefee'] = query['chargefee'] query = self.omit(query, 'chargefee') else: raise ExchangeError(self.id + ' withdraw() requires a `chargefee` parameter') if self.password: request['trade_pwd'] = self.password elif 'password' in query: request['trade_pwd'] = query['password'] query = self.omit(query, 'password') elif 'trade_pwd' in query: request['trade_pwd'] = query['trade_pwd'] query = self.omit(query, 'trade_pwd') passwordInRequest = ('trade_pwd' in list(request.keys())) if not passwordInRequest: raise ExchangeError(self.id + ' withdraw() requires self.password set on the exchange instance or a password / trade_pwd parameter') response = await self.privatePostWithdraw(self.extend(request, query)) return { 'info': response, 'id': self.safe_string(response, 'withdraw_id'), } def sign(self, path, api='public', method='GET', params={}, headers=None, body=None): url = '/' if api != 'web': url += self.version + '/' url += path if api != 'web': url += self.extension if api == 'private': self.check_required_credentials() query = self.keysort(self.extend({ 'api_key': self.apiKey, }, params)) # secret key must be at the end of query queryString = self.rawencode(query) + '&secret_key=' + self.secret query['sign'] = self.hash(self.encode(queryString)).upper() body = self.urlencode(query) headers = {'Content-Type': 'application/x-www-form-urlencoded'} else: if params: url += '?' + self.urlencode(params) url = self.urls['api'][api] + url return {'url': url, 'method': method, 'body': body, 'headers': headers} def handle_errors(self, code, reason, url, method, headers, body, response): if len(body) < 2: return # fallback to default error handler if body[0] == '{': if 'error_code' in response: error = self.safe_string(response, 'error_code') message = self.id + ' ' + self.json(response) if error in self.exceptions: ExceptionClass = self.exceptions[error] raise ExceptionClass(message) else: raise ExchangeError(message) if 'result' in response: if not response['result']: raise ExchangeError(self.id + ' ' + self.json(response))
45.028078
385
0.514246
from ccxt.async_support.base.exchange import Exchange import math from ccxt.base.errors import ExchangeError from ccxt.base.errors import AuthenticationError from ccxt.base.errors import ArgumentsRequired from ccxt.base.errors import InsufficientFunds from ccxt.base.errors import InvalidOrder from ccxt.base.errors import OrderNotFound from ccxt.base.errors import DDoSProtection class okcoinusd (Exchange): def describe(self): return self.deep_extend(super(okcoinusd, self).describe(), { 'id': 'okcoinusd', 'name': 'OKCoin USD', 'countries': ['CN', 'US'], 'version': 'v1', 'rateLimit': 1000, 'has': { 'CORS': False, 'fetchOHLCV': True, 'fetchOrder': True, 'fetchOrders': False, 'fetchOpenOrders': True, 'fetchClosedOrders': True, 'withdraw': True, 'futures': False, }, 'extension': '.do', 'timeframes': { '1m': '1min', '3m': '3min', '5m': '5min', '15m': '15min', '30m': '30min', '1h': '1hour', '2h': '2hour', '4h': '4hour', '6h': '6hour', '12h': '12hour', '1d': '1day', '3d': '3day', '1w': '1week', }, 'api': { 'web': { 'get': [ 'futures/pc/market/marketOverview', 'spot/markets/index-tickers', 'spot/markets/currencies', 'spot/markets/products', 'spot/markets/tickers', 'spot/user-level', ], }, 'public': { 'get': [ 'depth', 'exchange_rate', 'future_depth', 'future_estimated_price', 'future_hold_amount', 'future_index', 'future_kline', 'future_price_limit', 'future_ticker', 'future_trades', 'kline', 'otcs', 'ticker', 'tickers', 'trades', ], }, 'private': { 'post': [ 'account_records', 'batch_trade', 'borrow_money', 'borrow_order_info', 'borrows_info', 'cancel_borrow', 'cancel_order', 'cancel_otc_order', 'cancel_withdraw', 'funds_transfer', 'future_batch_trade', 'future_cancel', 'future_devolve', 'future_explosive', 'future_order_info', 'future_orders_info', 'future_position', 'future_position_4fix', 'future_trade', 'future_trades_history', 'future_userinfo', 'future_userinfo_4fix', 'lend_depth', 'order_fee', 'order_history', 'order_info', 'orders_info', 'otc_order_history', 'otc_order_info', 'repayment', 'submit_otc_order', 'trade', 'trade_history', 'trade_otc_order', 'wallet_info', 'withdraw', 'withdraw_info', 'unrepayments_info', 'userinfo', ], }, }, 'urls': { 'logo': 'https://user-images.githubusercontent.com/1294454/27766791-89ffb502-5ee5-11e7-8a5b-c5950b68ac65.jpg', 'api': { 'web': 'https://www.okcoin.com/v2', 'public': 'https://www.okcoin.com/api', 'private': 'https://www.okcoin.com', }, 'www': 'https://www.okcoin.com', 'doc': [ 'https://www.okcoin.com/docs/en/', 'https://www.npmjs.com/package/okcoin.com', ], }, 'fees': { 'trading': { 'taker': 0.002, 'maker': 0.002, }, }, 'exceptions': { '10000': ExchangeError, '10001': DDoSProtection, '10005': AuthenticationError, '10006': AuthenticationError, '10007': AuthenticationError, '1002': InsufficientFunds, '1003': InvalidOrder, '1004': InvalidOrder, '1013': InvalidOrder, '1027': InvalidOrder, '1050': InvalidOrder, '1217': InvalidOrder, '10014': InvalidOrder, '1009': OrderNotFound, '1019': OrderNotFound, '1051': OrderNotFound, '10009': OrderNotFound, '20015': OrderNotFound, '10008': ExchangeError, # 10051 order completed transaction # 10052 not allowed to withdraw # 10064 after a USD deposit, that portion of assets will not be withdrawable for the next 48 hours # 1007 No trading market information # 1008 No latest market information # 1009 No order # 1010 Different user of the cancelled order and the original order # 10100 User account frozen # 10101 order type is wrong # 10102 incorrect ID # 10103 the private otc order's key incorrect }, 'options': { 'marketBuyPrice': False, 'defaultContractType': 'this_week', 'warnOnFetchOHLCVLimitArgument': True, 'fiats': ['USD', 'CNY'], 'futures': { 'BCH': True, 'BTC': True, 'BTG': True, 'EOS': True, 'ETC': True, 'ETH': True, 'LTC': True, 'NEO': True, 'QTUM': True, 'USDT': True, 'XRP': True, }, }, }) async def fetch_markets(self, params={}): response = await self.webGetSpotMarketsProducts() markets = response['data'] result = [] for i in range(0, len(markets)): id = markets[i]['symbol'] baseId, quoteId = id.split('_') baseIdUppercase = baseId.upper() quoteIdUppercase = quoteId.upper() base = self.common_currency_code(baseIdUppercase) quote = self.common_currency_code(quoteIdUppercase) symbol = base + '/' + quote precision = { 'amount': markets[i]['maxSizeDigit'], 'price': markets[i]['maxPriceDigit'], } minAmount = markets[i]['minTradeSize'] minPrice = math.pow(10, -precision['price']) active = (markets[i]['online'] != 0) baseNumericId = markets[i]['baseCurrency'] quoteNumericId = markets[i]['quoteCurrency'] market = self.extend(self.fees['trading'], { 'id': id, 'symbol': symbol, 'base': base, 'quote': quote, 'baseId': baseId, 'quoteId': quoteId, 'baseNumericId': baseNumericId, 'quoteNumericId': quoteNumericId, 'info': markets[i], 'type': 'spot', 'spot': True, 'future': False, 'active': active, 'precision': precision, 'limits': { 'amount': { 'min': minAmount, 'max': None, }, 'price': { 'min': minPrice, 'max': None, }, 'cost': { 'min': minAmount * minPrice, 'max': None, }, }, }) result.append(market) if (self.has['futures']) and(market['base'] in list(self.options['futures'].keys())): fiats = self.options['fiats'] for j in range(0, len(fiats)): fiat = fiats[j] lowercaseFiat = fiat.lower() result.append(self.extend(market, { 'quote': fiat, 'symbol': market['base'] + '/' + fiat, 'id': market['base'].lower() + '_' + lowercaseFiat, 'quoteId': lowercaseFiat, 'type': 'future', 'spot': False, 'future': True, })) return result async def fetch_order_book(self, symbol, limit=None, params={}): await self.load_markets() market = self.market(symbol) method = 'publicGet' request = { 'symbol': market['id'], } if limit is not None: request['size'] = limit if market['future']: method += 'Future' request['contract_type'] = self.options['defaultContractType'] method += 'Depth' orderbook = await getattr(self, method)(self.extend(request, params)) return self.parse_order_book(orderbook) def parse_ticker(self, ticker, market=None): timestamp = self.safe_integer_2(ticker, 'timestamp', 'createdDate') symbol = None if market is None: if 'symbol' in ticker: marketId = ticker['symbol'] if marketId in self.markets_by_id: market = self.markets_by_id[marketId] else: baseId, quoteId = ticker['symbol'].split('_') base = baseId.upper() quote = quoteId.upper() base = self.common_currency_code(base) quote = self.common_currency_code(quote) symbol = base + '/' + quote if market is not None: symbol = market['symbol'] last = self.safe_float(ticker, 'last') open = self.safe_float(ticker, 'open') change = self.safe_float(ticker, 'change') percentage = self.safe_float(ticker, 'changePercentage') return { 'symbol': symbol, 'timestamp': timestamp, 'datetime': self.iso8601(timestamp), 'high': self.safe_float(ticker, 'high'), 'low': self.safe_float(ticker, 'low'), 'bid': self.safe_float(ticker, 'buy'), 'bidVolume': None, 'ask': self.safe_float(ticker, 'sell'), 'askVolume': None, 'vwap': None, 'open': open, 'close': last, 'last': last, 'previousClose': None, 'change': change, 'percentage': percentage, 'average': None, 'baseVolume': self.safe_float_2(ticker, 'vol', 'volume'), 'quoteVolume': None, 'info': ticker, } async def fetch_ticker(self, symbol, params={}): await self.load_markets() market = self.market(symbol) method = 'publicGet' request = { 'symbol': market['id'], } if market['future']: method += 'Future' request['contract_type'] = self.options['defaultContractType'] method += 'Ticker' response = await getattr(self, method)(self.extend(request, params)) ticker = self.safe_value(response, 'ticker') if ticker is None: raise ExchangeError(self.id + ' fetchTicker returned an empty response: ' + self.json(response)) timestamp = self.safe_integer(response, 'date') if timestamp is not None: timestamp *= 1000 ticker = self.extend(ticker, {'timestamp': timestamp}) return self.parse_ticker(ticker, market) def parse_trade(self, trade, market=None): symbol = None if market: symbol = market['symbol'] return { 'info': trade, 'timestamp': trade['date_ms'], 'datetime': self.iso8601(trade['date_ms']), 'symbol': symbol, 'id': str(trade['tid']), 'order': None, 'type': None, 'side': trade['type'], 'price': self.safe_float(trade, 'price'), 'amount': self.safe_float(trade, 'amount'), } async def fetch_trades(self, symbol, since=None, limit=None, params={}): await self.load_markets() market = self.market(symbol) method = 'publicGet' request = { 'symbol': market['id'], } if market['future']: method += 'Future' request['contract_type'] = self.options['defaultContractType'] method += 'Trades' response = await getattr(self, method)(self.extend(request, params)) return self.parse_trades(response, market, since, limit) def parse_ohlcv(self, ohlcv, market=None, timeframe='1m', since=None, limit=None): numElements = len(ohlcv) volumeIndex = 6 if (numElements > 6) else 5 return [ ohlcv[0], float(ohlcv[1]), float(ohlcv[2]), float(ohlcv[3]), float(ohlcv[4]), float(ohlcv[volumeIndex]), ] async def fetch_ohlcv(self, symbol, timeframe='1m', since=None, limit=None, params={}): await self.load_markets() market = self.market(symbol) method = 'publicGet' request = { 'symbol': market['id'], 'type': self.timeframes[timeframe], } if market['future']: method += 'Future' request['contract_type'] = self.options['defaultContractType'] method += 'Kline' if limit is not None: if self.options['warnOnFetchOHLCVLimitArgument']: raise ExchangeError(self.id + ' fetchOHLCV counts "limit" candles from current time backwards, therefore the "limit" argument for ' + self.id + ' is disabled. Set ' + self.id + '.options["warnOnFetchOHLCVLimitArgument"] = False to suppress self warning message.') request['size'] = int(limit) if since is not None: request['since'] = since else: request['since'] = self.milliseconds() - 86400000 response = await getattr(self, method)(self.extend(request, params)) return self.parse_ohlcvs(response, market, timeframe, since, limit) async def fetch_balance(self, params={}): await self.load_markets() response = await self.privatePostUserinfo(params) balances = response['info']['funds'] result = {'info': response} ids = list(balances['free'].keys()) usedField = 'freezed' if not(usedField in list(balances.keys())): usedField = 'holds' usedKeys = list(balances[usedField].keys()) ids = self.array_concat(ids, usedKeys) for i in range(0, len(ids)): id = ids[i] code = id.upper() if id in self.currencies_by_id: code = self.currencies_by_id[id]['code'] else: code = self.common_currency_code(code) account = self.account() account['free'] = self.safe_float(balances['free'], id, 0.0) account['used'] = self.safe_float(balances[usedField], id, 0.0) account['total'] = self.sum(account['free'], account['used']) result[code] = account return self.parse_balance(result) async def create_order(self, symbol, type, side, amount, price=None, params={}): await self.load_markets() market = self.market(symbol) method = 'privatePost' order = { 'symbol': market['id'], 'type': side, } if market['future']: method += 'Future' order = self.extend(order, { 'contract_type': self.options['defaultContractType'], 'match_price': 0, 'lever_rate': 10, 'price': price, 'amount': amount, }) else: if type == 'limit': order['price'] = price order['amount'] = amount else: order['type'] += '_market' if side == 'buy': if self.options['marketBuyPrice']: if price is None: raise ExchangeError(self.id + " market buy orders require a price argument(the amount you want to spend or the cost of the order) when self.options['marketBuyPrice'] is True.") order['price'] = price else: order['price'] = self.safe_float(params, 'cost') if not order['price']: raise ExchangeError(self.id + " market buy orders require an additional cost parameter, cost = price * amount. If you want to pass the cost of the market order(the amount you want to spend) in the price argument(the default " + self.id + " behaviour), set self.options['marketBuyPrice'] = True. It will effectively suppress self warning exception as well.") else: order['amount'] = amount params = self.omit(params, 'cost') method += 'Trade' response = await getattr(self, method)(self.extend(order, params)) timestamp = self.milliseconds() return { 'info': response, 'id': str(response['order_id']), 'timestamp': timestamp, 'datetime': self.iso8601(timestamp), 'lastTradeTimestamp': None, 'status': None, 'symbol': symbol, 'type': type, 'side': side, 'price': price, 'amount': amount, 'filled': None, 'remaining': None, 'cost': None, 'trades': None, 'fee': None, } async def cancel_order(self, id, symbol=None, params={}): if symbol is None: raise ArgumentsRequired(self.id + ' cancelOrder() requires a symbol argument') await self.load_markets() market = self.market(symbol) request = { 'symbol': market['id'], 'order_id': id, } method = 'privatePost' if market['future']: method += 'FutureCancel' request['contract_type'] = self.options['defaultContractType'] else: method += 'CancelOrder' response = await getattr(self, method)(self.extend(request, params)) return response def parse_order_status(self, status): statuses = { '-1': 'canceled', '0': 'open', '1': 'open', '2': 'closed', '3': 'open', '4': 'canceled', } return self.safe_value(statuses, status, status) def parse_order_side(self, side): if side == 1: return 'buy' if side == 2: return 'sell' if side == 3: return 'sell' if side == 4: return 'buy' return side def parse_order(self, order, market=None): side = None type = None if 'type' in order: if (order['type'] == 'buy') or (order['type'] == 'sell'): side = order['type'] type = 'limit' elif order['type'] == 'buy_market': side = 'buy' type = 'market' elif order['type'] == 'sell_market': side = 'sell' type = 'market' else: side = self.parse_order_side(order['type']) if ('contract_name' in list(order.keys())) or ('lever_rate' in list(order.keys())): type = 'margin' status = self.parse_order_status(self.safe_string(order, 'status')) symbol = None if market is None: marketId = self.safe_string(order, 'symbol') if marketId in self.markets_by_id: market = self.markets_by_id[marketId] if market: symbol = market['symbol'] timestamp = None createDateField = self.get_create_date_field() if createDateField in order: timestamp = order[createDateField] amount = self.safe_float(order, 'amount') filled = self.safe_float(order, 'deal_amount') amount = max(amount, filled) remaining = max(0, amount - filled) if type == 'market': remaining = 0 average = self.safe_float(order, 'avg_price') average = self.safe_float(order, 'price_avg', average) cost = average * filled result = { 'info': order, 'id': str(order['order_id']), 'timestamp': timestamp, 'datetime': self.iso8601(timestamp), 'lastTradeTimestamp': None, 'symbol': symbol, 'type': type, 'side': side, 'price': order['price'], 'average': average, 'cost': cost, 'amount': amount, 'filled': filled, 'remaining': remaining, 'status': status, 'fee': None, } return result def get_create_date_field(self): return 'create_date' def get_orders_field(self): return 'orders' async def fetch_order(self, id, symbol=None, params={}): if symbol is None: raise ExchangeError(self.id + ' fetchOrder requires a symbol parameter') await self.load_markets() market = self.market(symbol) method = 'privatePost' request = { 'order_id': id, 'symbol': market['id'], } if market['future']: method += 'Future' request['contract_type'] = self.options['defaultContractType'] method += 'OrderInfo' response = await getattr(self, method)(self.extend(request, params)) ordersField = self.get_orders_field() numOrders = len(response[ordersField]) if numOrders > 0: return self.parse_order(response[ordersField][0]) raise OrderNotFound(self.id + ' order ' + id + ' not found') async def fetch_orders(self, symbol=None, since=None, limit=None, params={}): if symbol is None: raise ExchangeError(self.id + ' fetchOrders requires a symbol parameter') await self.load_markets() market = self.market(symbol) method = 'privatePost' request = { 'symbol': market['id'], } order_id_in_params = ('order_id' in list(params.keys())) if market['future']: method += 'FutureOrdersInfo' request['contract_type'] = self.options['defaultContractType'] if not order_id_in_params: raise ExchangeError(self.id + ' fetchOrders() requires order_id param for futures market ' + symbol + '(a string of one or more order ids, comma-separated)') else: status = None if 'type' in params: status = params['type'] elif 'status' in params: status = params['status'] else: name = 'type' if order_id_in_params else 'status' raise ExchangeError(self.id + ' fetchOrders() requires ' + name + ' param for spot market ' + symbol + '(0 - for unfilled orders, 1 - for filled/canceled orders)') if order_id_in_params: method += 'OrdersInfo' request = self.extend(request, { 'type': status, 'order_id': params['order_id'], }) else: method += 'OrderHistory' request = self.extend(request, { 'status': status, 'current_page': 1, 'page_length': 200, }) params = self.omit(params, ['type', 'status']) response = await getattr(self, method)(self.extend(request, params)) ordersField = self.get_orders_field() return self.parse_orders(response[ordersField], market, since, limit) async def fetch_open_orders(self, symbol=None, since=None, limit=None, params={}): open = 0 return await self.fetch_orders(symbol, since, limit, self.extend({ 'status': open, }, params)) async def fetch_closed_orders(self, symbol=None, since=None, limit=None, params={}): closed = 1 orders = await self.fetch_orders(symbol, since, limit, self.extend({ 'status': closed, }, params)) return orders async def withdraw(self, code, amount, address, tag=None, params={}): self.check_address(address) await self.load_markets() currency = self.currency(code) currencyId = currency['id'] + '_usd' if tag: address = address + ':' + tag request = { 'symbol': currencyId, 'withdraw_address': address, 'withdraw_amount': amount, 'target': 'address', } query = params if 'chargefee' in query: request['chargefee'] = query['chargefee'] query = self.omit(query, 'chargefee') else: raise ExchangeError(self.id + ' withdraw() requires a `chargefee` parameter') if self.password: request['trade_pwd'] = self.password elif 'password' in query: request['trade_pwd'] = query['password'] query = self.omit(query, 'password') elif 'trade_pwd' in query: request['trade_pwd'] = query['trade_pwd'] query = self.omit(query, 'trade_pwd') passwordInRequest = ('trade_pwd' in list(request.keys())) if not passwordInRequest: raise ExchangeError(self.id + ' withdraw() requires self.password set on the exchange instance or a password / trade_pwd parameter') response = await self.privatePostWithdraw(self.extend(request, query)) return { 'info': response, 'id': self.safe_string(response, 'withdraw_id'), } def sign(self, path, api='public', method='GET', params={}, headers=None, body=None): url = '/' if api != 'web': url += self.version + '/' url += path if api != 'web': url += self.extension if api == 'private': self.check_required_credentials() query = self.keysort(self.extend({ 'api_key': self.apiKey, }, params)) queryString = self.rawencode(query) + '&secret_key=' + self.secret query['sign'] = self.hash(self.encode(queryString)).upper() body = self.urlencode(query) headers = {'Content-Type': 'application/x-www-form-urlencoded'} else: if params: url += '?' + self.urlencode(params) url = self.urls['api'][api] + url return {'url': url, 'method': method, 'body': body, 'headers': headers} def handle_errors(self, code, reason, url, method, headers, body, response): if len(body) < 2: return if body[0] == '{': if 'error_code' in response: error = self.safe_string(response, 'error_code') message = self.id + ' ' + self.json(response) if error in self.exceptions: ExceptionClass = self.exceptions[error] raise ExceptionClass(message) else: raise ExchangeError(message) if 'result' in response: if not response['result']: raise ExchangeError(self.id + ' ' + self.json(response))
true
true
f70602a4d6e1c149fd39b8c0015b53b971bd17ca
1,960
py
Python
Top_Interview_Question_Easy/Design/Shuffle an Array/by_random_shuffle.py
coderMaruf/leetcode-1
20ffe26e43999e44c8acf9800acb371a49bb5853
[ "MIT" ]
32
2020-01-05T13:37:16.000Z
2022-03-26T07:27:09.000Z
Top_Interview_Question_Easy/Design/Shuffle an Array/by_random_shuffle.py
coderMaruf/leetcode-1
20ffe26e43999e44c8acf9800acb371a49bb5853
[ "MIT" ]
null
null
null
Top_Interview_Question_Easy/Design/Shuffle an Array/by_random_shuffle.py
coderMaruf/leetcode-1
20ffe26e43999e44c8acf9800acb371a49bb5853
[ "MIT" ]
8
2020-06-18T16:17:27.000Z
2022-03-15T23:58:18.000Z
''' Description: Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] nums = {1,2,3}; Solution solution = new Solution(nums); // Shuffle the array [1,2,3] and return its result. Any permutation of [1,2,3] must equally likely to be returned. solution.shuffle(); // Resets the array back to its original configuration [1,2,3]. solution.reset(); // Returns the random shuffling of array [1,2,3]. solution.shuffle(); Hint #1 The solution expects that we always use the original array to shuffle() else some of the test cases fail. (Credits; @snehasingh31) ''' from typing import List from random import shuffle class Solution: def __init__(self, nums: List[int]): # copy to class member: self.array self.array = [ *nums ] self.origin = nums def reset(self) -> List[int]: """ Resets the array to its original configuration and return it. """ self.array = [ *(self.origin) ] return self.array def shuffle(self) -> List[int]: """ Returns a random shuffling of the array. """ shuffle( self.array ) return self.array # n : the length of input array ## Time Complexity: O( n ) # # The overhead in time is the cost of shuffle and reset, which are of O( n ). ## Space Complexity: O( n ) # # The overhead in space is the storage for class member, self.array, which is of O( n ). from collections import namedtuple TestEntry = namedtuple('TestEntry', 'sequence') def test_bench(): t = TestEntry( sequence = [1,2,3] ) # reference output # this is a challenge about randomness, so any valid permutation of shuffling result is accpeted. ''' [1, 3, 2] [1, 2, 3] [3, 2, 1] ''' obj = Solution( t.sequence ) print( obj.shuffle() ) print( obj.reset() ) print( obj.shuffle() ) if __name__ == '__main__': test_bench()
20.851064
130
0.627551
from typing import List from random import shuffle class Solution: def __init__(self, nums: List[int]): self.array = [ *nums ] self.origin = nums def reset(self) -> List[int]: self.array = [ *(self.origin) ] return self.array def shuffle(self) -> List[int]: shuffle( self.array ) return self.array from collections import namedtuple TestEntry = namedtuple('TestEntry', 'sequence') def test_bench(): t = TestEntry( sequence = [1,2,3] ) obj = Solution( t.sequence ) print( obj.shuffle() ) print( obj.reset() ) print( obj.shuffle() ) if __name__ == '__main__': test_bench()
true
true
f706046282c238390879809cfc550ca00aa9d7c5
19,105
py
Python
python/cuml/test/test_umap.py
siddheshmhatre/cuml
ed0e58c6b3ebfc17b944cdad7c04cd4af8860736
[ "Apache-2.0" ]
2,743
2018-10-11T17:28:58.000Z
2022-03-31T19:20:50.000Z
python/cuml/test/test_umap.py
siddheshmhatre/cuml
ed0e58c6b3ebfc17b944cdad7c04cd4af8860736
[ "Apache-2.0" ]
4,280
2018-10-11T22:29:57.000Z
2022-03-31T22:02:44.000Z
python/cuml/test/test_umap.py
siddheshmhatre/cuml
ed0e58c6b3ebfc17b944cdad7c04cd4af8860736
[ "Apache-2.0" ]
454
2018-10-11T17:40:56.000Z
2022-03-25T17:07:09.000Z
# Copyright (c) 2019-2021, NVIDIA CORPORATION. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # # Please install UMAP before running the code # use 'conda install -c conda-forge umap-learn' command to install it import numpy as np import pytest import umap import copy import cupyx import scipy.sparse from cuml.manifold.umap import UMAP as cuUMAP from cuml.test.utils import array_equal, unit_param, \ quality_param, stress_param from sklearn.neighbors import NearestNeighbors import joblib from cuml.common import logger from sklearn import datasets from sklearn.cluster import KMeans from sklearn.datasets import make_blobs from sklearn.manifold import trustworthiness from sklearn.metrics import adjusted_rand_score dataset_names = ['iris', 'digits', 'wine', 'blobs'] @pytest.mark.parametrize('nrows', [unit_param(500), quality_param(5000), stress_param(500000)]) @pytest.mark.parametrize('n_feats', [unit_param(20), quality_param(100), stress_param(1000)]) def test_blobs_cluster(nrows, n_feats): data, labels = datasets.make_blobs( n_samples=nrows, n_features=n_feats, centers=5, random_state=0) embedding = cuUMAP().fit_transform(data, convert_dtype=True) if nrows < 500000: score = adjusted_rand_score(labels, KMeans(5).fit_predict(embedding)) assert score == 1.0 @pytest.mark.parametrize('nrows', [unit_param(500), quality_param(5000), stress_param(500000)]) @pytest.mark.parametrize('n_feats', [unit_param(10), quality_param(100), stress_param(1000)]) def test_umap_fit_transform_score(nrows, n_feats): n_samples = nrows n_features = n_feats data, labels = make_blobs(n_samples=n_samples, n_features=n_features, centers=10, random_state=42) model = umap.UMAP(n_neighbors=10, min_dist=0.1) cuml_model = cuUMAP(n_neighbors=10, min_dist=0.01) embedding = model.fit_transform(data) cuml_embedding = cuml_model.fit_transform(data, convert_dtype=True) assert not np.isnan(embedding).any() assert not np.isnan(cuml_embedding).any() if nrows < 500000: cuml_score = adjusted_rand_score(labels, KMeans(10).fit_predict( cuml_embedding)) score = adjusted_rand_score(labels, KMeans(10).fit_predict(embedding)) assert array_equal(score, cuml_score, 1e-2, with_sign=True) def test_supervised_umap_trustworthiness_on_iris(): iris = datasets.load_iris() data = iris.data embedding = cuUMAP(n_neighbors=10, random_state=0, min_dist=0.01).fit_transform( data, iris.target, convert_dtype=True) trust = trustworthiness(iris.data, embedding, n_neighbors=10) assert trust >= 0.97 def test_semisupervised_umap_trustworthiness_on_iris(): iris = datasets.load_iris() data = iris.data target = iris.target.copy() target[25:75] = -1 embedding = cuUMAP(n_neighbors=10, random_state=0, min_dist=0.01).fit_transform( data, target, convert_dtype=True) trust = trustworthiness(iris.data, embedding, n_neighbors=10) assert trust >= 0.97 def test_umap_trustworthiness_on_iris(): iris = datasets.load_iris() data = iris.data embedding = cuUMAP(n_neighbors=10, min_dist=0.01, random_state=0).fit_transform( data, convert_dtype=True) trust = trustworthiness(iris.data, embedding, n_neighbors=10) assert trust >= 0.97 @pytest.mark.parametrize('target_metric', ["categorical", "euclidean"]) def test_umap_transform_on_iris(target_metric): iris = datasets.load_iris() iris_selection = np.random.RandomState(42).choice( [True, False], 150, replace=True, p=[0.75, 0.25]) data = iris.data[iris_selection] fitter = cuUMAP(n_neighbors=10, init="random", n_epochs=800, min_dist=0.01, random_state=42, target_metric=target_metric) fitter.fit(data, convert_dtype=True) new_data = iris.data[~iris_selection] embedding = fitter.transform(new_data, convert_dtype=True) assert not np.isnan(embedding).any() trust = trustworthiness(new_data, embedding, n_neighbors=10) assert trust >= 0.85 @pytest.mark.parametrize('input_type', ['cupy', 'scipy']) @pytest.mark.parametrize('xform_method', ['fit', 'fit_transform']) @pytest.mark.parametrize('target_metric', ["categorical", "euclidean"]) def test_umap_transform_on_digits_sparse(target_metric, input_type, xform_method): digits = datasets.load_digits() digits_selection = np.random.RandomState(42).choice( [True, False], 1797, replace=True, p=[0.75, 0.25]) if input_type == 'cupy': sp_prefix = cupyx.scipy.sparse else: sp_prefix = scipy.sparse data = sp_prefix.csr_matrix( scipy.sparse.csr_matrix(digits.data[digits_selection])) fitter = cuUMAP(n_neighbors=15, verbose=logger.level_info, init="random", n_epochs=0, min_dist=0.01, random_state=42, target_metric=target_metric) new_data = sp_prefix.csr_matrix( scipy.sparse.csr_matrix(digits.data[~digits_selection])) if xform_method == 'fit': fitter.fit(data, convert_dtype=True) embedding = fitter.transform(new_data, convert_dtype=True) else: embedding = fitter.fit_transform(new_data, convert_dtype=True) if input_type == 'cupy': embedding = embedding.get() trust = trustworthiness(digits.data[~digits_selection], embedding, n_neighbors=15) assert trust >= 0.96 @pytest.mark.parametrize('target_metric', ["categorical", "euclidean"]) def test_umap_transform_on_digits(target_metric): digits = datasets.load_digits() digits_selection = np.random.RandomState(42).choice( [True, False], 1797, replace=True, p=[0.75, 0.25]) data = digits.data[digits_selection] fitter = cuUMAP(n_neighbors=15, verbose=logger.level_debug, init="random", n_epochs=0, min_dist=0.01, random_state=42, target_metric=target_metric) fitter.fit(data, convert_dtype=True) new_data = digits.data[~digits_selection] embedding = fitter.transform(new_data, convert_dtype=True) trust = trustworthiness(digits.data[~digits_selection], embedding, n_neighbors=15) assert trust >= 0.96 @pytest.mark.parametrize('target_metric', ["categorical", "euclidean"]) @pytest.mark.parametrize('name', dataset_names) def test_umap_fit_transform_trust(name, target_metric): if name == 'iris': iris = datasets.load_iris() data = iris.data labels = iris.target elif name == 'digits': digits = datasets.load_digits(n_class=5) data = digits.data labels = digits.target elif name == 'wine': wine = datasets.load_wine() data = wine.data labels = wine.target else: data, labels = make_blobs(n_samples=500, n_features=10, centers=10, random_state=42) model = umap.UMAP(n_neighbors=10, min_dist=0.01, target_metric=target_metric) cuml_model = cuUMAP(n_neighbors=10, min_dist=0.01, target_metric=target_metric) embedding = model.fit_transform(data) cuml_embedding = cuml_model.fit_transform(data, convert_dtype=True) trust = trustworthiness(data, embedding, n_neighbors=10) cuml_trust = trustworthiness(data, cuml_embedding, n_neighbors=10) assert array_equal(trust, cuml_trust, 1e-1, with_sign=True) @pytest.mark.parametrize('target_metric', ["categorical", "euclidean"]) @pytest.mark.parametrize('name', [unit_param('digits')]) @pytest.mark.parametrize('nrows', [quality_param(5000), stress_param(500000)]) @pytest.mark.parametrize('n_feats', [quality_param(100), stress_param(1000)]) @pytest.mark.parametrize('should_downcast', [True]) @pytest.mark.parametrize('input_type', ['dataframe', 'ndarray']) def test_umap_data_formats(input_type, should_downcast, nrows, n_feats, name, target_metric): dtype = np.float32 if not should_downcast else np.float64 n_samples = nrows n_feats = n_feats if name == 'digits': # use the digits dataset for unit test digits = datasets.load_digits(n_class=9) X = digits["data"].astype(dtype) else: X, y = datasets.make_blobs(n_samples=n_samples, n_features=n_feats, random_state=0) umap = cuUMAP(n_neighbors=3, n_components=2, target_metric=target_metric) embeds = umap.fit_transform(X) assert type(embeds) == np.ndarray @pytest.mark.parametrize('target_metric', ["categorical", "euclidean"]) @pytest.mark.filterwarnings("ignore:(.*)connected(.*):UserWarning:sklearn[.*]") def test_umap_fit_transform_score_default(target_metric): n_samples = 500 n_features = 20 data, labels = make_blobs(n_samples=n_samples, n_features=n_features, centers=10, random_state=42) model = umap.UMAP(target_metric=target_metric) cuml_model = cuUMAP(target_metric=target_metric) embedding = model.fit_transform(data) cuml_embedding = cuml_model.fit_transform(data, convert_dtype=True) cuml_score = adjusted_rand_score(labels, KMeans(10).fit_predict( cuml_embedding)) score = adjusted_rand_score(labels, KMeans(10).fit_predict(embedding)) assert array_equal(score, cuml_score, 1e-2, with_sign=True) def test_umap_fit_transform_against_fit_and_transform(): n_samples = 500 n_features = 20 data, labels = make_blobs(n_samples=n_samples, n_features=n_features, centers=10, random_state=42) """ First test the default option does not hash the input """ cuml_model = cuUMAP() ft_embedding = cuml_model.fit_transform(data, convert_dtype=True) fit_embedding_same_input = cuml_model.transform(data, convert_dtype=True) assert joblib.hash(ft_embedding) != joblib.hash(fit_embedding_same_input) """ Next, test explicitly enabling feature hashes the input """ cuml_model = cuUMAP(hash_input=True) ft_embedding = cuml_model.fit_transform(data, convert_dtype=True) fit_embedding_same_input = cuml_model.transform(data, convert_dtype=True) assert joblib.hash(ft_embedding) == joblib.hash(fit_embedding_same_input) fit_embedding_diff_input = cuml_model.transform(data[1:], convert_dtype=True) assert joblib.hash(ft_embedding) != joblib.hash(fit_embedding_diff_input) @pytest.mark.parametrize('n_components,random_state', [unit_param(2, None), unit_param(2, 8), unit_param(2, np.random.RandomState(42)), unit_param(21, None), unit_param(21, np.random.RandomState(42)), unit_param(25, 8), unit_param(50, None), stress_param(50, 8)]) def test_umap_fit_transform_reproducibility(n_components, random_state): n_samples = 8000 n_features = 200 if random_state is None: n_components *= 2 data, labels = make_blobs(n_samples=n_samples, n_features=n_features, centers=10, random_state=42) def get_embedding(n_components, random_state): reducer = cuUMAP(init="random", n_components=n_components, random_state=random_state) return reducer.fit_transform(data, convert_dtype=True) state = copy.copy(random_state) cuml_embedding1 = get_embedding(n_components, state) state = copy.copy(random_state) cuml_embedding2 = get_embedding(n_components, state) assert not np.isnan(cuml_embedding1).any() assert not np.isnan(cuml_embedding2).any() # Reproducibility threshold raised until intermittent failure is fixed # Ref: https://github.com/rapidsai/cuml/issues/1903 mean_diff = np.mean(np.abs(cuml_embedding1 - cuml_embedding2)) if random_state is not None: assert mean_diff == 0.0 else: assert mean_diff > 0.5 @pytest.mark.parametrize('n_components,random_state', [unit_param(2, None), unit_param(2, 8), unit_param(2, np.random.RandomState(42)), unit_param(21, None), unit_param(25, 8), unit_param(25, np.random.RandomState(42)), unit_param(50, None), stress_param(50, 8)]) def test_umap_transform_reproducibility(n_components, random_state): n_samples = 5000 n_features = 200 if random_state is None: n_components *= 2 data, labels = make_blobs(n_samples=n_samples, n_features=n_features, centers=10, random_state=42) selection = np.random.RandomState(42).choice( [True, False], n_samples, replace=True, p=[0.5, 0.5]) fit_data = data[selection] transform_data = data[~selection] def get_embedding(n_components, random_state): reducer = cuUMAP(init="random", n_components=n_components, random_state=random_state) reducer.fit(fit_data, convert_dtype=True) return reducer.transform(transform_data, convert_dtype=True) state = copy.copy(random_state) cuml_embedding1 = get_embedding(n_components, state) state = copy.copy(random_state) cuml_embedding2 = get_embedding(n_components, state) assert not np.isnan(cuml_embedding1).any() assert not np.isnan(cuml_embedding2).any() # Reproducibility threshold raised until intermittent failure is fixed # Ref: https://github.com/rapidsai/cuml/issues/1903 mean_diff = np.mean(np.abs(cuml_embedding1 - cuml_embedding2)) if random_state is not None: assert mean_diff == 0.0 else: assert mean_diff > 0.5 def test_umap_fit_transform_trustworthiness_with_consistency_enabled(): iris = datasets.load_iris() data = iris.data algo = cuUMAP(n_neighbors=10, min_dist=0.01, init="random", random_state=42) embedding = algo.fit_transform(data, convert_dtype=True) trust = trustworthiness(iris.data, embedding, n_neighbors=10) assert trust >= 0.97 def test_umap_transform_trustworthiness_with_consistency_enabled(): iris = datasets.load_iris() data = iris.data selection = np.random.RandomState(42).choice( [True, False], data.shape[0], replace=True, p=[0.5, 0.5]) fit_data = data[selection] transform_data = data[~selection] model = cuUMAP(n_neighbors=10, min_dist=0.01, init="random", random_state=42) model.fit(fit_data, convert_dtype=True) embedding = model.transform(transform_data, convert_dtype=True) trust = trustworthiness(transform_data, embedding, n_neighbors=10) assert trust >= 0.92 @pytest.mark.filterwarnings("ignore:(.*)zero(.*)::scipy[.*]|umap[.*]") def test_exp_decay_params(): def compare_exp_decay_params(a=None, b=None, min_dist=0.1, spread=1.0): cuml_model = cuUMAP(a=a, b=b, min_dist=min_dist, spread=spread) state = cuml_model.__getstate__() cuml_a, cuml_b = state['a'], state['b'] skl_model = umap.UMAP(a=a, b=b, min_dist=min_dist, spread=spread) skl_model.fit(np.zeros((1, 1))) sklearn_a, sklearn_b = skl_model._a, skl_model._b assert abs(cuml_a) - abs(sklearn_a) < 1e-6 assert abs(cuml_b) - abs(sklearn_b) < 1e-6 compare_exp_decay_params(min_dist=0.1, spread=1.0) compare_exp_decay_params(a=0.5, b=2.0) compare_exp_decay_params(a=0.5) compare_exp_decay_params(b=0.5) compare_exp_decay_params(min_dist=0.1, spread=10.0) @pytest.mark.parametrize('n_neighbors', [5, 15]) def test_umap_knn_parameters(n_neighbors): data, labels = datasets.make_blobs( n_samples=2000, n_features=10, centers=5, random_state=0) data = data.astype(np.float32) def fit_transform_embed(knn_graph=None): model = cuUMAP(random_state=42, init='random', n_neighbors=n_neighbors) return model.fit_transform(data, knn_graph=knn_graph, convert_dtype=True) def transform_embed(knn_graph=None): model = cuUMAP(random_state=42, init='random', n_neighbors=n_neighbors) model.fit(data, knn_graph=knn_graph, convert_dtype=True) return model.transform(data, knn_graph=knn_graph, convert_dtype=True) def test_trustworthiness(embedding): trust = trustworthiness(data, embedding, n_neighbors=n_neighbors) assert trust >= 0.92 def test_equality(e1, e2): mean_diff = np.mean(np.abs(e1 - e2)) print("mean diff: %s" % mean_diff) assert mean_diff < 1.0 neigh = NearestNeighbors(n_neighbors=n_neighbors) neigh.fit(data) knn_graph = neigh.kneighbors_graph(data, mode="distance") embedding1 = fit_transform_embed(None) embedding2 = fit_transform_embed(knn_graph.tocsr()) embedding3 = fit_transform_embed(knn_graph.tocoo()) embedding4 = fit_transform_embed(knn_graph.tocsc()) embedding5 = transform_embed(knn_graph.tocsr()) embedding6 = transform_embed(knn_graph.tocoo()) embedding7 = transform_embed(knn_graph.tocsc()) test_trustworthiness(embedding1) test_trustworthiness(embedding2) test_trustworthiness(embedding3) test_trustworthiness(embedding4) test_trustworthiness(embedding5) test_trustworthiness(embedding6) test_trustworthiness(embedding7) test_equality(embedding2, embedding3) test_equality(embedding3, embedding4) test_equality(embedding5, embedding6) test_equality(embedding6, embedding7)
35.844278
79
0.655745
import numpy as np import pytest import umap import copy import cupyx import scipy.sparse from cuml.manifold.umap import UMAP as cuUMAP from cuml.test.utils import array_equal, unit_param, \ quality_param, stress_param from sklearn.neighbors import NearestNeighbors import joblib from cuml.common import logger from sklearn import datasets from sklearn.cluster import KMeans from sklearn.datasets import make_blobs from sklearn.manifold import trustworthiness from sklearn.metrics import adjusted_rand_score dataset_names = ['iris', 'digits', 'wine', 'blobs'] @pytest.mark.parametrize('nrows', [unit_param(500), quality_param(5000), stress_param(500000)]) @pytest.mark.parametrize('n_feats', [unit_param(20), quality_param(100), stress_param(1000)]) def test_blobs_cluster(nrows, n_feats): data, labels = datasets.make_blobs( n_samples=nrows, n_features=n_feats, centers=5, random_state=0) embedding = cuUMAP().fit_transform(data, convert_dtype=True) if nrows < 500000: score = adjusted_rand_score(labels, KMeans(5).fit_predict(embedding)) assert score == 1.0 @pytest.mark.parametrize('nrows', [unit_param(500), quality_param(5000), stress_param(500000)]) @pytest.mark.parametrize('n_feats', [unit_param(10), quality_param(100), stress_param(1000)]) def test_umap_fit_transform_score(nrows, n_feats): n_samples = nrows n_features = n_feats data, labels = make_blobs(n_samples=n_samples, n_features=n_features, centers=10, random_state=42) model = umap.UMAP(n_neighbors=10, min_dist=0.1) cuml_model = cuUMAP(n_neighbors=10, min_dist=0.01) embedding = model.fit_transform(data) cuml_embedding = cuml_model.fit_transform(data, convert_dtype=True) assert not np.isnan(embedding).any() assert not np.isnan(cuml_embedding).any() if nrows < 500000: cuml_score = adjusted_rand_score(labels, KMeans(10).fit_predict( cuml_embedding)) score = adjusted_rand_score(labels, KMeans(10).fit_predict(embedding)) assert array_equal(score, cuml_score, 1e-2, with_sign=True) def test_supervised_umap_trustworthiness_on_iris(): iris = datasets.load_iris() data = iris.data embedding = cuUMAP(n_neighbors=10, random_state=0, min_dist=0.01).fit_transform( data, iris.target, convert_dtype=True) trust = trustworthiness(iris.data, embedding, n_neighbors=10) assert trust >= 0.97 def test_semisupervised_umap_trustworthiness_on_iris(): iris = datasets.load_iris() data = iris.data target = iris.target.copy() target[25:75] = -1 embedding = cuUMAP(n_neighbors=10, random_state=0, min_dist=0.01).fit_transform( data, target, convert_dtype=True) trust = trustworthiness(iris.data, embedding, n_neighbors=10) assert trust >= 0.97 def test_umap_trustworthiness_on_iris(): iris = datasets.load_iris() data = iris.data embedding = cuUMAP(n_neighbors=10, min_dist=0.01, random_state=0).fit_transform( data, convert_dtype=True) trust = trustworthiness(iris.data, embedding, n_neighbors=10) assert trust >= 0.97 @pytest.mark.parametrize('target_metric', ["categorical", "euclidean"]) def test_umap_transform_on_iris(target_metric): iris = datasets.load_iris() iris_selection = np.random.RandomState(42).choice( [True, False], 150, replace=True, p=[0.75, 0.25]) data = iris.data[iris_selection] fitter = cuUMAP(n_neighbors=10, init="random", n_epochs=800, min_dist=0.01, random_state=42, target_metric=target_metric) fitter.fit(data, convert_dtype=True) new_data = iris.data[~iris_selection] embedding = fitter.transform(new_data, convert_dtype=True) assert not np.isnan(embedding).any() trust = trustworthiness(new_data, embedding, n_neighbors=10) assert trust >= 0.85 @pytest.mark.parametrize('input_type', ['cupy', 'scipy']) @pytest.mark.parametrize('xform_method', ['fit', 'fit_transform']) @pytest.mark.parametrize('target_metric', ["categorical", "euclidean"]) def test_umap_transform_on_digits_sparse(target_metric, input_type, xform_method): digits = datasets.load_digits() digits_selection = np.random.RandomState(42).choice( [True, False], 1797, replace=True, p=[0.75, 0.25]) if input_type == 'cupy': sp_prefix = cupyx.scipy.sparse else: sp_prefix = scipy.sparse data = sp_prefix.csr_matrix( scipy.sparse.csr_matrix(digits.data[digits_selection])) fitter = cuUMAP(n_neighbors=15, verbose=logger.level_info, init="random", n_epochs=0, min_dist=0.01, random_state=42, target_metric=target_metric) new_data = sp_prefix.csr_matrix( scipy.sparse.csr_matrix(digits.data[~digits_selection])) if xform_method == 'fit': fitter.fit(data, convert_dtype=True) embedding = fitter.transform(new_data, convert_dtype=True) else: embedding = fitter.fit_transform(new_data, convert_dtype=True) if input_type == 'cupy': embedding = embedding.get() trust = trustworthiness(digits.data[~digits_selection], embedding, n_neighbors=15) assert trust >= 0.96 @pytest.mark.parametrize('target_metric', ["categorical", "euclidean"]) def test_umap_transform_on_digits(target_metric): digits = datasets.load_digits() digits_selection = np.random.RandomState(42).choice( [True, False], 1797, replace=True, p=[0.75, 0.25]) data = digits.data[digits_selection] fitter = cuUMAP(n_neighbors=15, verbose=logger.level_debug, init="random", n_epochs=0, min_dist=0.01, random_state=42, target_metric=target_metric) fitter.fit(data, convert_dtype=True) new_data = digits.data[~digits_selection] embedding = fitter.transform(new_data, convert_dtype=True) trust = trustworthiness(digits.data[~digits_selection], embedding, n_neighbors=15) assert trust >= 0.96 @pytest.mark.parametrize('target_metric', ["categorical", "euclidean"]) @pytest.mark.parametrize('name', dataset_names) def test_umap_fit_transform_trust(name, target_metric): if name == 'iris': iris = datasets.load_iris() data = iris.data labels = iris.target elif name == 'digits': digits = datasets.load_digits(n_class=5) data = digits.data labels = digits.target elif name == 'wine': wine = datasets.load_wine() data = wine.data labels = wine.target else: data, labels = make_blobs(n_samples=500, n_features=10, centers=10, random_state=42) model = umap.UMAP(n_neighbors=10, min_dist=0.01, target_metric=target_metric) cuml_model = cuUMAP(n_neighbors=10, min_dist=0.01, target_metric=target_metric) embedding = model.fit_transform(data) cuml_embedding = cuml_model.fit_transform(data, convert_dtype=True) trust = trustworthiness(data, embedding, n_neighbors=10) cuml_trust = trustworthiness(data, cuml_embedding, n_neighbors=10) assert array_equal(trust, cuml_trust, 1e-1, with_sign=True) @pytest.mark.parametrize('target_metric', ["categorical", "euclidean"]) @pytest.mark.parametrize('name', [unit_param('digits')]) @pytest.mark.parametrize('nrows', [quality_param(5000), stress_param(500000)]) @pytest.mark.parametrize('n_feats', [quality_param(100), stress_param(1000)]) @pytest.mark.parametrize('should_downcast', [True]) @pytest.mark.parametrize('input_type', ['dataframe', 'ndarray']) def test_umap_data_formats(input_type, should_downcast, nrows, n_feats, name, target_metric): dtype = np.float32 if not should_downcast else np.float64 n_samples = nrows n_feats = n_feats if name == 'digits': digits = datasets.load_digits(n_class=9) X = digits["data"].astype(dtype) else: X, y = datasets.make_blobs(n_samples=n_samples, n_features=n_feats, random_state=0) umap = cuUMAP(n_neighbors=3, n_components=2, target_metric=target_metric) embeds = umap.fit_transform(X) assert type(embeds) == np.ndarray @pytest.mark.parametrize('target_metric', ["categorical", "euclidean"]) @pytest.mark.filterwarnings("ignore:(.*)connected(.*):UserWarning:sklearn[.*]") def test_umap_fit_transform_score_default(target_metric): n_samples = 500 n_features = 20 data, labels = make_blobs(n_samples=n_samples, n_features=n_features, centers=10, random_state=42) model = umap.UMAP(target_metric=target_metric) cuml_model = cuUMAP(target_metric=target_metric) embedding = model.fit_transform(data) cuml_embedding = cuml_model.fit_transform(data, convert_dtype=True) cuml_score = adjusted_rand_score(labels, KMeans(10).fit_predict( cuml_embedding)) score = adjusted_rand_score(labels, KMeans(10).fit_predict(embedding)) assert array_equal(score, cuml_score, 1e-2, with_sign=True) def test_umap_fit_transform_against_fit_and_transform(): n_samples = 500 n_features = 20 data, labels = make_blobs(n_samples=n_samples, n_features=n_features, centers=10, random_state=42) cuml_model = cuUMAP() ft_embedding = cuml_model.fit_transform(data, convert_dtype=True) fit_embedding_same_input = cuml_model.transform(data, convert_dtype=True) assert joblib.hash(ft_embedding) != joblib.hash(fit_embedding_same_input) cuml_model = cuUMAP(hash_input=True) ft_embedding = cuml_model.fit_transform(data, convert_dtype=True) fit_embedding_same_input = cuml_model.transform(data, convert_dtype=True) assert joblib.hash(ft_embedding) == joblib.hash(fit_embedding_same_input) fit_embedding_diff_input = cuml_model.transform(data[1:], convert_dtype=True) assert joblib.hash(ft_embedding) != joblib.hash(fit_embedding_diff_input) @pytest.mark.parametrize('n_components,random_state', [unit_param(2, None), unit_param(2, 8), unit_param(2, np.random.RandomState(42)), unit_param(21, None), unit_param(21, np.random.RandomState(42)), unit_param(25, 8), unit_param(50, None), stress_param(50, 8)]) def test_umap_fit_transform_reproducibility(n_components, random_state): n_samples = 8000 n_features = 200 if random_state is None: n_components *= 2 data, labels = make_blobs(n_samples=n_samples, n_features=n_features, centers=10, random_state=42) def get_embedding(n_components, random_state): reducer = cuUMAP(init="random", n_components=n_components, random_state=random_state) return reducer.fit_transform(data, convert_dtype=True) state = copy.copy(random_state) cuml_embedding1 = get_embedding(n_components, state) state = copy.copy(random_state) cuml_embedding2 = get_embedding(n_components, state) assert not np.isnan(cuml_embedding1).any() assert not np.isnan(cuml_embedding2).any() mean_diff = np.mean(np.abs(cuml_embedding1 - cuml_embedding2)) if random_state is not None: assert mean_diff == 0.0 else: assert mean_diff > 0.5 @pytest.mark.parametrize('n_components,random_state', [unit_param(2, None), unit_param(2, 8), unit_param(2, np.random.RandomState(42)), unit_param(21, None), unit_param(25, 8), unit_param(25, np.random.RandomState(42)), unit_param(50, None), stress_param(50, 8)]) def test_umap_transform_reproducibility(n_components, random_state): n_samples = 5000 n_features = 200 if random_state is None: n_components *= 2 data, labels = make_blobs(n_samples=n_samples, n_features=n_features, centers=10, random_state=42) selection = np.random.RandomState(42).choice( [True, False], n_samples, replace=True, p=[0.5, 0.5]) fit_data = data[selection] transform_data = data[~selection] def get_embedding(n_components, random_state): reducer = cuUMAP(init="random", n_components=n_components, random_state=random_state) reducer.fit(fit_data, convert_dtype=True) return reducer.transform(transform_data, convert_dtype=True) state = copy.copy(random_state) cuml_embedding1 = get_embedding(n_components, state) state = copy.copy(random_state) cuml_embedding2 = get_embedding(n_components, state) assert not np.isnan(cuml_embedding1).any() assert not np.isnan(cuml_embedding2).any() mean_diff = np.mean(np.abs(cuml_embedding1 - cuml_embedding2)) if random_state is not None: assert mean_diff == 0.0 else: assert mean_diff > 0.5 def test_umap_fit_transform_trustworthiness_with_consistency_enabled(): iris = datasets.load_iris() data = iris.data algo = cuUMAP(n_neighbors=10, min_dist=0.01, init="random", random_state=42) embedding = algo.fit_transform(data, convert_dtype=True) trust = trustworthiness(iris.data, embedding, n_neighbors=10) assert trust >= 0.97 def test_umap_transform_trustworthiness_with_consistency_enabled(): iris = datasets.load_iris() data = iris.data selection = np.random.RandomState(42).choice( [True, False], data.shape[0], replace=True, p=[0.5, 0.5]) fit_data = data[selection] transform_data = data[~selection] model = cuUMAP(n_neighbors=10, min_dist=0.01, init="random", random_state=42) model.fit(fit_data, convert_dtype=True) embedding = model.transform(transform_data, convert_dtype=True) trust = trustworthiness(transform_data, embedding, n_neighbors=10) assert trust >= 0.92 @pytest.mark.filterwarnings("ignore:(.*)zero(.*)::scipy[.*]|umap[.*]") def test_exp_decay_params(): def compare_exp_decay_params(a=None, b=None, min_dist=0.1, spread=1.0): cuml_model = cuUMAP(a=a, b=b, min_dist=min_dist, spread=spread) state = cuml_model.__getstate__() cuml_a, cuml_b = state['a'], state['b'] skl_model = umap.UMAP(a=a, b=b, min_dist=min_dist, spread=spread) skl_model.fit(np.zeros((1, 1))) sklearn_a, sklearn_b = skl_model._a, skl_model._b assert abs(cuml_a) - abs(sklearn_a) < 1e-6 assert abs(cuml_b) - abs(sklearn_b) < 1e-6 compare_exp_decay_params(min_dist=0.1, spread=1.0) compare_exp_decay_params(a=0.5, b=2.0) compare_exp_decay_params(a=0.5) compare_exp_decay_params(b=0.5) compare_exp_decay_params(min_dist=0.1, spread=10.0) @pytest.mark.parametrize('n_neighbors', [5, 15]) def test_umap_knn_parameters(n_neighbors): data, labels = datasets.make_blobs( n_samples=2000, n_features=10, centers=5, random_state=0) data = data.astype(np.float32) def fit_transform_embed(knn_graph=None): model = cuUMAP(random_state=42, init='random', n_neighbors=n_neighbors) return model.fit_transform(data, knn_graph=knn_graph, convert_dtype=True) def transform_embed(knn_graph=None): model = cuUMAP(random_state=42, init='random', n_neighbors=n_neighbors) model.fit(data, knn_graph=knn_graph, convert_dtype=True) return model.transform(data, knn_graph=knn_graph, convert_dtype=True) def test_trustworthiness(embedding): trust = trustworthiness(data, embedding, n_neighbors=n_neighbors) assert trust >= 0.92 def test_equality(e1, e2): mean_diff = np.mean(np.abs(e1 - e2)) print("mean diff: %s" % mean_diff) assert mean_diff < 1.0 neigh = NearestNeighbors(n_neighbors=n_neighbors) neigh.fit(data) knn_graph = neigh.kneighbors_graph(data, mode="distance") embedding1 = fit_transform_embed(None) embedding2 = fit_transform_embed(knn_graph.tocsr()) embedding3 = fit_transform_embed(knn_graph.tocoo()) embedding4 = fit_transform_embed(knn_graph.tocsc()) embedding5 = transform_embed(knn_graph.tocsr()) embedding6 = transform_embed(knn_graph.tocoo()) embedding7 = transform_embed(knn_graph.tocsc()) test_trustworthiness(embedding1) test_trustworthiness(embedding2) test_trustworthiness(embedding3) test_trustworthiness(embedding4) test_trustworthiness(embedding5) test_trustworthiness(embedding6) test_trustworthiness(embedding7) test_equality(embedding2, embedding3) test_equality(embedding3, embedding4) test_equality(embedding5, embedding6) test_equality(embedding6, embedding7)
true
true
f70604e996bcced6a875cdc45706aed741786a1e
884
py
Python
python/Depth_of_BinaryTree.py
samael65535/LeetCode_Kata
9f95ef7906dfd0dfbd3d1a9d86713491ca614d73
[ "Beerware" ]
null
null
null
python/Depth_of_BinaryTree.py
samael65535/LeetCode_Kata
9f95ef7906dfd0dfbd3d1a9d86713491ca614d73
[ "Beerware" ]
3
2020-09-06T23:00:37.000Z
2021-05-10T08:40:16.000Z
python/Depth_of_BinaryTree.py
samael65535/LeetCode_Kata
9f95ef7906dfd0dfbd3d1a9d86713491ca614d73
[ "Beerware" ]
null
null
null
# Given a binary tree, find its maximum depth. # The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. # Definition for a binary tree node class TreeNode: def __init__(self, x): self.val = x self.left = None self.right = None class Solution: # @param root, a tree node # @return an integer def maxDepth(self, root): if root is None: return 0 L = 0; R = 0; if not (root.left is None): L = self.maxDepth(root.left) if not (root.right is None): R = self.maxDepth(root.right) return (L + 1) if L > R else (R + 1) # if __name__ == "__main__" : # root = TreeNode(1) # root.left = TreeNode(1) # root.right = TreeNode(1) # s = Solution() # print s.maxDepth(root) # print "hello"
27.625
116
0.574661
class TreeNode: def __init__(self, x): self.val = x self.left = None self.right = None class Solution: def maxDepth(self, root): if root is None: return 0 L = 0; R = 0; if not (root.left is None): L = self.maxDepth(root.left) if not (root.right is None): R = self.maxDepth(root.right) return (L + 1) if L > R else (R + 1)
true
true
f70605983681cf001e0bd742787b59712a82a096
441
py
Python
duckshell/Python (Alpha)/duckshell.py
notbeandev/duckshell
c9da7b7dbf7e13811f934fd08e009505e47e759c
[ "Apache-2.0" ]
null
null
null
duckshell/Python (Alpha)/duckshell.py
notbeandev/duckshell
c9da7b7dbf7e13811f934fd08e009505e47e759c
[ "Apache-2.0" ]
null
null
null
duckshell/Python (Alpha)/duckshell.py
notbeandev/duckshell
c9da7b7dbf7e13811f934fd08e009505e47e759c
[ "Apache-2.0" ]
null
null
null
import os def shell(): inputuser = input("> ") if (inputuser == "exit"): exit() os.system(inputuser) return shell() print("Welcome to duckshell (python alpha)") print("System info: ") os.system("echo distro: $(uname -o)"); os.system("echo hostname: $(uname -n)"); os.system("echo architecture: $(uname -m)"); os.system("echo linux kernel: $(uname -r)"); os.system("echo python version: $(python --version)") shell()
25.941176
53
0.630385
import os def shell(): inputuser = input("> ") if (inputuser == "exit"): exit() os.system(inputuser) return shell() print("Welcome to duckshell (python alpha)") print("System info: ") os.system("echo distro: $(uname -o)"); os.system("echo hostname: $(uname -n)"); os.system("echo architecture: $(uname -m)"); os.system("echo linux kernel: $(uname -r)"); os.system("echo python version: $(python --version)") shell()
true
true
f706078f25935d3f6dc896b8ed8817a6dac40291
658
py
Python
cli/commands/cmd_coverage.py
Pythonian/catwatch
25730faa9d8ec6564b075de78bbbf4ff125ada97
[ "MIT" ]
null
null
null
cli/commands/cmd_coverage.py
Pythonian/catwatch
25730faa9d8ec6564b075de78bbbf4ff125ada97
[ "MIT" ]
null
null
null
cli/commands/cmd_coverage.py
Pythonian/catwatch
25730faa9d8ec6564b075de78bbbf4ff125ada97
[ "MIT" ]
2
2018-08-04T16:46:55.000Z
2019-07-02T19:30:24.000Z
import logging import subprocess import click try: from instance import settings APP_ROOT = settings.APP_ROOT except ImportError: logging.error('Ensure __init__.py and settings.py both exist in instance/') exit(1) except AttributeError: from config import settings APP_ROOT = settings.APP_ROOT PACKAGE_PATH = '{0}/{1}'.format(APP_ROOT, '/catwatch') @click.command() @click.argument('path', default=PACKAGE_PATH) def cli(path): """ Run test coverage report. :return: Subprocess call result """ cmd = 'py.test --cov-report term-missing --cov {0} {0}'.format(path) return subprocess.call(cmd, shell=True)
21.225806
79
0.699088
import logging import subprocess import click try: from instance import settings APP_ROOT = settings.APP_ROOT except ImportError: logging.error('Ensure __init__.py and settings.py both exist in instance/') exit(1) except AttributeError: from config import settings APP_ROOT = settings.APP_ROOT PACKAGE_PATH = '{0}/{1}'.format(APP_ROOT, '/catwatch') @click.command() @click.argument('path', default=PACKAGE_PATH) def cli(path): cmd = 'py.test --cov-report term-missing --cov {0} {0}'.format(path) return subprocess.call(cmd, shell=True)
true
true
f70609b6a4e0c7889d505fe6072dfde0383cb4af
4,218
py
Python
src/specific_models/federated/single_machine_simulation_flower/single_machine_simulation.py
kaylani2/machineLearning
692623abf6fe02bde6c7da6c2f8c0ec526a3e8f8
[ "MIT" ]
7
2019-11-06T14:35:37.000Z
2022-03-06T03:55:06.000Z
src/specific_models/federated/single_machine_simulation_flower/single_machine_simulation.py
kaylani2/machineLearning
692623abf6fe02bde6c7da6c2f8c0ec526a3e8f8
[ "MIT" ]
10
2020-05-16T02:38:35.000Z
2021-04-11T23:55:35.000Z
src/specific_models/federated/single_machine_simulation_flower/single_machine_simulation.py
kaylani2/machineLearning
692623abf6fe02bde6c7da6c2f8c0ec526a3e8f8
[ "MIT" ]
2
2020-06-26T21:39:41.000Z
2020-09-15T03:38:32.000Z
import os import time from multiprocessing import Process from typing import Tuple import flwr as fl import numpy as np import tensorflow as tf from flwr.server.strategy import FedAvg import dataset # generate random integer values from random import seed from random import randint # Make TensorFlow log less verbose os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3" # K: Prevent TF from using GPU (not enough memory) os.environ["CUDA_VISIBLE_DEVICES"] = "-1" DATASET = Tuple[Tuple[np.ndarray, np.ndarray], Tuple[np.ndarray, np.ndarray]] def start_server(num_rounds: int, num_clients: int, fraction_fit: float): """Start the server with a slightly adjusted FedAvg strategy.""" strategy = FedAvg(min_available_clients=num_clients, fraction_fit=fraction_fit) # Exposes the server by default on port 8080 fl.server.start_server(strategy=strategy, config={"num_rounds": num_rounds}) def start_client(dataset: DATASET) -> None: """Start a single client with the provided dataset.""" # Load and compile a Keras model for CIFAR-10 #model = tf.keras.applications.MobileNetV2((32, 32, 3), classes=10, weights=None) model = tf.keras.Sequential( [ tf.keras.Input(shape=(32, 32, 3)), tf.keras.layers.Conv2D(32, kernel_size=(3, 3), activation="relu"), tf.keras.layers.MaxPooling2D(pool_size=(2, 2)), tf.keras.layers.Conv2D(64, kernel_size=(3, 3), activation="relu"), tf.keras.layers.MaxPooling2D(pool_size=(2, 2)), tf.keras.layers.Flatten(), tf.keras.layers.Dropout(0.5), tf.keras.layers.Dense(10, activation="softmax"), ] ) model.compile("adam", "sparse_categorical_crossentropy", metrics=[tf.keras.metrics.CategoricalAccuracy(), tf.keras.metrics.MeanSquaredError()]) ### @TODO: check if "accuracy" and tf.keras.metrics.CategoricalAccuracy() return the same results # Unpack the CIFAR-10 dataset partition (x_train, y_train), (x_test, y_test) = dataset # Define a Flower client class CifarClient(fl.client.NumPyClient): def get_parameters(self): """Return current weights.""" return model.get_weights() def fit(self, parameters, config): """Fit model and return new weights as well as number of training examples.""" model.set_weights(parameters) # Remove steps_per_epoch if you want to train over the full dataset # https://keras.io/api/models/model_training_apis/#fit-method #nap_time = randint (0, 5) #time.sleep (nap_time) #print ("Slept for", nap_time, "seconds.") model.fit(x_train, y_train, epochs=10, batch_size=256, steps_per_epoch=10) return model.get_weights(), len(x_train), {} def evaluate(self, parameters, config): """Evaluate using provided parameters.""" model.set_weights(parameters) loss, accuracy, mse = model.evaluate(x_test, y_test) print ('"Loss:', loss, ". Accuracy:", accuracy, ". MSE:", mse, ".") return loss, len(x_test), {"accuracy": accuracy} # Start Flower client fl.client.start_numpy_client("0.0.0.0:8080", client=CifarClient()) def run_simulation(num_rounds: int, num_clients: int, fraction_fit: float): """Start a FL simulation.""" # This will hold all the processes which we are going to create processes = [] # Start the server server_process = Process( target=start_server, args=(num_rounds, num_clients, fraction_fit) ) server_process.start() processes.append(server_process) # Optionally block the script here for a second or two so the server has time to start time.sleep(2) # Load the dataset partitions partitions = dataset.load(num_partitions=num_clients) # Start all the clients for partition in partitions: client_process = Process(target=start_client, args=(partition,)) client_process.start() processes.append(client_process) # Block until all processes are finished for p in processes: p.join() if __name__ == "__main__": run_simulation(num_rounds=100, num_clients=5, fraction_fit=0.5)
35.745763
147
0.676387
import os import time from multiprocessing import Process from typing import Tuple import flwr as fl import numpy as np import tensorflow as tf from flwr.server.strategy import FedAvg import dataset from random import seed from random import randint os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3" os.environ["CUDA_VISIBLE_DEVICES"] = "-1" DATASET = Tuple[Tuple[np.ndarray, np.ndarray], Tuple[np.ndarray, np.ndarray]] def start_server(num_rounds: int, num_clients: int, fraction_fit: float): strategy = FedAvg(min_available_clients=num_clients, fraction_fit=fraction_fit) fl.server.start_server(strategy=strategy, config={"num_rounds": num_rounds}) def start_client(dataset: DATASET) -> None: model = tf.keras.Sequential( [ tf.keras.Input(shape=(32, 32, 3)), tf.keras.layers.Conv2D(32, kernel_size=(3, 3), activation="relu"), tf.keras.layers.MaxPooling2D(pool_size=(2, 2)), tf.keras.layers.Conv2D(64, kernel_size=(3, 3), activation="relu"), tf.keras.layers.MaxPooling2D(pool_size=(2, 2)), tf.keras.layers.Flatten(), tf.keras.layers.Dropout(0.5), tf.keras.layers.Dense(10, activation="softmax"), ] ) model.compile("adam", "sparse_categorical_crossentropy", metrics=[tf.keras.metrics.CategoricalAccuracy(), tf.keras.metrics.MeanSquaredError()]) (x_train, y_train), (x_test, y_test) = dataset class CifarClient(fl.client.NumPyClient): def get_parameters(self): return model.get_weights() def fit(self, parameters, config): model.set_weights(parameters) model.fit(x_train, y_train, epochs=10, batch_size=256, steps_per_epoch=10) return model.get_weights(), len(x_train), {} def evaluate(self, parameters, config): model.set_weights(parameters) loss, accuracy, mse = model.evaluate(x_test, y_test) print ('"Loss:', loss, ". Accuracy:", accuracy, ". MSE:", mse, ".") return loss, len(x_test), {"accuracy": accuracy} # Start Flower client fl.client.start_numpy_client("0.0.0.0:8080", client=CifarClient()) def run_simulation(num_rounds: int, num_clients: int, fraction_fit: float): # This will hold all the processes which we are going to create processes = [] # Start the server server_process = Process( target=start_server, args=(num_rounds, num_clients, fraction_fit) ) server_process.start() processes.append(server_process) # Optionally block the script here for a second or two so the server has time to start time.sleep(2) # Load the dataset partitions partitions = dataset.load(num_partitions=num_clients) # Start all the clients for partition in partitions: client_process = Process(target=start_client, args=(partition,)) client_process.start() processes.append(client_process) # Block until all processes are finished for p in processes: p.join() if __name__ == "__main__": run_simulation(num_rounds=100, num_clients=5, fraction_fit=0.5)
true
true
f7060c08bc11ae2c68ff0c0e583ea8e09574f0ef
551
py
Python
lecture06/01_bitsets/bitsets.py
nd-cse-34872-su20/cse-34872-su20-examples
683039c19b2e2bd0c3c28713b73e46a9deaa227c
[ "MIT" ]
2
2020-06-01T16:46:39.000Z
2020-12-01T21:50:12.000Z
lecture06/01_bitsets/bitsets.py
nd-cse-34872-su20/cse-34872-su20-examples
683039c19b2e2bd0c3c28713b73e46a9deaa227c
[ "MIT" ]
null
null
null
lecture06/01_bitsets/bitsets.py
nd-cse-34872-su20/cse-34872-su20-examples
683039c19b2e2bd0c3c28713b73e46a9deaa227c
[ "MIT" ]
6
2020-06-03T23:56:26.000Z
2020-08-26T15:05:17.000Z
#!/usr/bin/env python3 elements = (1, 2, 4) bitset = 0 # 1. Initialize bitset # Add elements to bitset for i in elements: bitset = bitset | 1<<i # 2. Add element to bitset # Print contents of bitset print(bitset) # Test for elements in bitset for i in range(6): if bitset & 1<<i: # 3. Test if element is in bitset print(i) # Remove elements from bitset for i in elements: bitset = bitset & ~(1<<i) # 4. Remove element from bitset # Print contents of bitset print(bitset)
22.958333
69
0.602541
elements = (1, 2, 4) bitset = 0 for i in elements: bitset = bitset | 1<<i print(bitset) for i in range(6): if bitset & 1<<i: print(i) for i in elements: bitset = bitset & ~(1<<i) print(bitset)
true
true
f7060d6db54a0a075b206cc8b5ec9fc32267fc6b
6,891
py
Python
salt/cli/__init__.py
mitsuhiko/salt
3b211f87e307936f43c937edc62207dd1b887e19
[ "Apache-2.0" ]
4
2015-10-06T22:20:27.000Z
2017-09-04T08:03:44.000Z
salt/cli/__init__.py
mitsuhiko/salt
3b211f87e307936f43c937edc62207dd1b887e19
[ "Apache-2.0" ]
null
null
null
salt/cli/__init__.py
mitsuhiko/salt
3b211f87e307936f43c937edc62207dd1b887e19
[ "Apache-2.0" ]
null
null
null
''' The management of salt command line utilities are stored in here ''' # Import python libs import os import sys # Import salt components import salt.cli.caller import salt.cli.cp import salt.cli.key import salt.cli.batch import salt.client import salt.output import salt.runner import optparse from salt.utils import parsers from salt.utils.verify import verify_env from salt.version import __version__ as VERSION from salt.exceptions import SaltInvocationError, SaltClientError, SaltException class SaltCMD(parsers.SaltCMDOptionParser): ''' The execution of a salt command happens here ''' def run(self): ''' Execute the salt command line ''' self.parse_args() try: local = salt.client.LocalClient(self.get_config_file_path('master')) except SaltClientError as exc: self.exit(2, '{0}\n'.format(exc)) return if self.options.query: ret = local.find_cmd(self.config['cmd']) for jid in ret: if isinstance(ret, list) or isinstance(ret, dict): print('Return data for job {0}:'.format(jid)) salt.output.display_output(ret[jid], None, self.config) print('') elif self.options.batch: batch = salt.cli.batch.Batch(self.config) batch.run() else: if self.options.timeout <= 0: self.options.timeout = local.opts['timeout'] args = [ self.config['tgt'], self.config['fun'], self.config['arg'], self.options.timeout, ] if self.selected_target_option: args.append(self.selected_target_option) else: args.append('glob') if getattr(self.options, 'return'): args.append(getattr(self.options, 'return')) else: args.append('') try: # local will be None when there was an error if local: if self.options.static: if self.options.verbose: args.append(True) full_ret = local.cmd_full_return(*args) ret, out = self._format_ret(full_ret) self._output_ret(ret, out) elif self.config['fun'] == 'sys.doc': ret = {} out = '' for full_ret in local.cmd_cli(*args): ret_, out = self._format_ret(full_ret) ret.update(ret_) self._output_ret(ret, out) else: if self.options.verbose: args.append(True) for full_ret in local.cmd_cli(*args): ret, out = self._format_ret(full_ret) self._output_ret(ret, out) except SaltInvocationError as exc: ret = exc out = '' def _output_ret(self, ret, out): ''' Print the output from a single return to the terminal ''' # Handle special case commands if self.config['fun'] == 'sys.doc': self._print_docs(ret) else: # Determine the proper output method and run it salt.output.display_output(ret, out, self.config) def _format_ret(self, full_ret): ''' Take the full return data and format it to simple output ''' ret = {} out = '' for key, data in full_ret.items(): ret[key] = data['ret'] if 'out' in data: out = data['out'] return ret, out def _print_docs(self, ret): ''' Print out the docstrings for all of the functions on the minions ''' docs = {} if not ret: self.exit(2, 'No minions found to gather docs from\n') for host in ret: for fun in ret[host]: if fun not in docs: if ret[host][fun]: docs[fun] = ret[host][fun] for fun in sorted(docs): print(fun + ':') print(docs[fun]) print('') class SaltCP(parsers.SaltCPOptionParser): ''' Run the salt-cp command line client ''' def run(self): ''' Execute salt-cp ''' self.parse_args() cp_ = salt.cli.cp.SaltCP(self.config) cp_.run() class SaltKey(parsers.SaltKeyOptionParser): ''' Initialize the Salt key manager ''' def run(self): ''' Execute salt-key ''' self.parse_args() if self.config['verify_env']: verify_env([ os.path.join(self.config['pki_dir'], 'minions'), os.path.join(self.config['pki_dir'], 'minions_pre'), os.path.join(self.config['pki_dir'], 'minions_rejected'), os.path.dirname(self.config['key_logfile']), ], self.config['user'], permissive=self.config['permissive_pki_access'], pki_dir=self.config['pki_dir'], ) self.setup_logfile_logger() key = salt.cli.key.Key(self.config) key.run() class SaltCall(parsers.SaltCallOptionParser): ''' Used to locally execute a salt command ''' def run(self): ''' Execute the salt call! ''' self.parse_args() if self.config['verify_env']: verify_env([ self.config['pki_dir'], self.config['cachedir'], os.path.dirname(self.config['log_file']) ], self.config['user'], permissive=self.config['permissive_pki_access'], pki_dir=self.config['pki_dir'], ) caller = salt.cli.caller.Caller(self.config) if self.options.doc: caller.print_docs() self.exit(0) if self.options.grains_run: caller.print_grains() self.exit(0) caller.run() class SaltRun(parsers.SaltRunOptionParser): def run(self): ''' Execute salt-run ''' self.parse_args() runner = salt.runner.Runner(self.config) if self.options.doc: runner._print_docs() else: # Run this here so SystemExit isn't raised anywhere else when # someone tries to use the runners via the python api try: runner.run() except SaltClientError as exc: raise SystemExit(str(exc))
29.199153
80
0.506748
import os import sys import salt.cli.caller import salt.cli.cp import salt.cli.key import salt.cli.batch import salt.client import salt.output import salt.runner import optparse from salt.utils import parsers from salt.utils.verify import verify_env from salt.version import __version__ as VERSION from salt.exceptions import SaltInvocationError, SaltClientError, SaltException class SaltCMD(parsers.SaltCMDOptionParser): def run(self): self.parse_args() try: local = salt.client.LocalClient(self.get_config_file_path('master')) except SaltClientError as exc: self.exit(2, '{0}\n'.format(exc)) return if self.options.query: ret = local.find_cmd(self.config['cmd']) for jid in ret: if isinstance(ret, list) or isinstance(ret, dict): print('Return data for job {0}:'.format(jid)) salt.output.display_output(ret[jid], None, self.config) print('') elif self.options.batch: batch = salt.cli.batch.Batch(self.config) batch.run() else: if self.options.timeout <= 0: self.options.timeout = local.opts['timeout'] args = [ self.config['tgt'], self.config['fun'], self.config['arg'], self.options.timeout, ] if self.selected_target_option: args.append(self.selected_target_option) else: args.append('glob') if getattr(self.options, 'return'): args.append(getattr(self.options, 'return')) else: args.append('') try: if local: if self.options.static: if self.options.verbose: args.append(True) full_ret = local.cmd_full_return(*args) ret, out = self._format_ret(full_ret) self._output_ret(ret, out) elif self.config['fun'] == 'sys.doc': ret = {} out = '' for full_ret in local.cmd_cli(*args): ret_, out = self._format_ret(full_ret) ret.update(ret_) self._output_ret(ret, out) else: if self.options.verbose: args.append(True) for full_ret in local.cmd_cli(*args): ret, out = self._format_ret(full_ret) self._output_ret(ret, out) except SaltInvocationError as exc: ret = exc out = '' def _output_ret(self, ret, out): if self.config['fun'] == 'sys.doc': self._print_docs(ret) else: salt.output.display_output(ret, out, self.config) def _format_ret(self, full_ret): ret = {} out = '' for key, data in full_ret.items(): ret[key] = data['ret'] if 'out' in data: out = data['out'] return ret, out def _print_docs(self, ret): docs = {} if not ret: self.exit(2, 'No minions found to gather docs from\n') for host in ret: for fun in ret[host]: if fun not in docs: if ret[host][fun]: docs[fun] = ret[host][fun] for fun in sorted(docs): print(fun + ':') print(docs[fun]) print('') class SaltCP(parsers.SaltCPOptionParser): def run(self): self.parse_args() cp_ = salt.cli.cp.SaltCP(self.config) cp_.run() class SaltKey(parsers.SaltKeyOptionParser): def run(self): self.parse_args() if self.config['verify_env']: verify_env([ os.path.join(self.config['pki_dir'], 'minions'), os.path.join(self.config['pki_dir'], 'minions_pre'), os.path.join(self.config['pki_dir'], 'minions_rejected'), os.path.dirname(self.config['key_logfile']), ], self.config['user'], permissive=self.config['permissive_pki_access'], pki_dir=self.config['pki_dir'], ) self.setup_logfile_logger() key = salt.cli.key.Key(self.config) key.run() class SaltCall(parsers.SaltCallOptionParser): def run(self): self.parse_args() if self.config['verify_env']: verify_env([ self.config['pki_dir'], self.config['cachedir'], os.path.dirname(self.config['log_file']) ], self.config['user'], permissive=self.config['permissive_pki_access'], pki_dir=self.config['pki_dir'], ) caller = salt.cli.caller.Caller(self.config) if self.options.doc: caller.print_docs() self.exit(0) if self.options.grains_run: caller.print_grains() self.exit(0) caller.run() class SaltRun(parsers.SaltRunOptionParser): def run(self): self.parse_args() runner = salt.runner.Runner(self.config) if self.options.doc: runner._print_docs() else: # someone tries to use the runners via the python api try: runner.run() except SaltClientError as exc: raise SystemExit(str(exc))
true
true
f7060f746805a30aa2c550af85590696b9df583c
19,094
py
Python
mango/group.py
liqprotocol/mango-explorer
c2eecdd80f3e57261e209109af39bc05d34cabe9
[ "MIT" ]
null
null
null
mango/group.py
liqprotocol/mango-explorer
c2eecdd80f3e57261e209109af39bc05d34cabe9
[ "MIT" ]
null
null
null
mango/group.py
liqprotocol/mango-explorer
c2eecdd80f3e57261e209109af39bc05d34cabe9
[ "MIT" ]
null
null
null
# # ⚠ Warning # # 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. # # [🥭 Mango Markets](https://mango.markets/) support is available at: # [Docs](https://docs.mango.markets/) # [Discord](https://discord.gg/67jySBhxrg) # [Twitter](https://twitter.com/mangomarkets) # [Github](https://github.com/blockworks-foundation) # [Email](mailto:hello@blockworks.foundation) import typing from decimal import Decimal from solana.publickey import PublicKey from .accountinfo import AccountInfo from .addressableaccount import AddressableAccount from .cache import Cache, PerpMarketCache, MarketCache from .constants import SYSTEM_PROGRAM_ADDRESS from .context import Context from .instrumentlookup import InstrumentLookup from .instrumentvalue import InstrumentValue from .layouts import layouts from .lotsizeconverter import LotSizeConverter, RaisingLotSizeConverter from .marketlookup import MarketLookup from .metadata import Metadata from .token import Instrument, Token from .tokenbank import TokenBank from .version import Version # # 🥭 GroupSlotSpotMarket class # class GroupSlotSpotMarket: def __init__(self, address: PublicKey, maint_asset_weight: Decimal, init_asset_weight: Decimal, maint_liab_weight: Decimal, init_liab_weight: Decimal) -> None: self.address: PublicKey = address self.maint_asset_weight: Decimal = maint_asset_weight self.init_asset_weight: Decimal = init_asset_weight self.maint_liab_weight: Decimal = maint_liab_weight self.init_liab_weight: Decimal = init_liab_weight @staticmethod def from_layout(layout: typing.Any) -> "GroupSlotSpotMarket": spot_market: PublicKey = layout.spot_market maint_asset_weight: Decimal = round(layout.maint_asset_weight, 8) init_asset_weight: Decimal = round(layout.init_asset_weight, 8) maint_liab_weight: Decimal = round(layout.maint_liab_weight, 8) init_liab_weight: Decimal = round(layout.init_liab_weight, 8) return GroupSlotSpotMarket(spot_market, maint_asset_weight, init_asset_weight, maint_liab_weight, init_liab_weight) @staticmethod def from_layout_or_none(layout: typing.Any) -> typing.Optional["GroupSlotSpotMarket"]: if (layout.spot_market is None) or (layout.spot_market == SYSTEM_PROGRAM_ADDRESS): return None return GroupSlotSpotMarket.from_layout(layout) def __str__(self) -> str: return f"""« 𝙶𝚛𝚘𝚞𝚙𝚂𝚕𝚘𝚝𝚂𝚙𝚘𝚝𝙼𝚊𝚛𝚔𝚎𝚝 [{self.address}] Asset Weights: Initial: {self.init_asset_weight} Maintenance: {self.maint_asset_weight} Liability Weights: Initial: {self.init_liab_weight} Maintenance: {self.maint_liab_weight} »""" def __repr__(self) -> str: return f"{self}" # # 🥭 GroupSlotPerpMarket class # class GroupSlotPerpMarket: def __init__(self, address: PublicKey, maint_asset_weight: Decimal, init_asset_weight: Decimal, maint_liab_weight: Decimal, init_liab_weight: Decimal, liquidation_fee: Decimal, base_lot_size: Decimal, quote_lot_size: Decimal) -> None: self.address: PublicKey = address self.maint_asset_weight: Decimal = maint_asset_weight self.init_asset_weight: Decimal = init_asset_weight self.maint_liab_weight: Decimal = maint_liab_weight self.init_liab_weight: Decimal = init_liab_weight self.liquidation_fee: Decimal = liquidation_fee self.base_lot_size: Decimal = base_lot_size self.quote_lot_size: Decimal = quote_lot_size @staticmethod def from_layout(layout: typing.Any) -> "GroupSlotPerpMarket": perp_market: PublicKey = layout.perp_market maint_asset_weight: Decimal = round(layout.maint_asset_weight, 8) init_asset_weight: Decimal = round(layout.init_asset_weight, 8) maint_liab_weight: Decimal = round(layout.maint_liab_weight, 8) init_liab_weight: Decimal = round(layout.init_liab_weight, 8) liquidation_fee: Decimal = round(layout.liquidation_fee, 8) base_lot_size: Decimal = layout.base_lot_size quote_lot_size: Decimal = layout.quote_lot_size return GroupSlotPerpMarket(perp_market, maint_asset_weight, init_asset_weight, maint_liab_weight, init_liab_weight, liquidation_fee, base_lot_size, quote_lot_size) @staticmethod def from_layout_or_none(layout: typing.Any) -> typing.Optional["GroupSlotPerpMarket"]: if (layout.perp_market is None) or (layout.perp_market == SYSTEM_PROGRAM_ADDRESS): return None return GroupSlotPerpMarket.from_layout(layout) def __str__(self) -> str: return f"""« 𝙶𝚛𝚘𝚞𝚙𝚂𝚕𝚘𝚝𝙿𝚎𝚛𝚙𝙼𝚊𝚛𝚔𝚎𝚝 [{self.address}] Asset Weights: Initial: {self.init_asset_weight} Maintenance: {self.maint_asset_weight} Liability Weights: Initial: {self.init_liab_weight} Maintenance: {self.maint_liab_weight} Liquidation Fee: {self.liquidation_fee} Base Lot Size: {self.base_lot_size} Quote Lot Size: {self.quote_lot_size} »""" def __repr__(self) -> str: return f"{self}" # # 🥭 GroupSlot class # # `GroupSlot` gathers indexed slot items together instead of separate arrays. # class GroupSlot: def __init__(self, index: int, base_instrument: Instrument, base_token_bank: typing.Optional[TokenBank], quote_token_bank: TokenBank, spot_market_info: typing.Optional[GroupSlotSpotMarket], perp_market_info: typing.Optional[GroupSlotPerpMarket], perp_lot_size_converter: LotSizeConverter, oracle: PublicKey) -> None: self.index: int = index self.base_instrument: Instrument = base_instrument self.base_token_bank: typing.Optional[TokenBank] = base_token_bank self.quote_token_bank: TokenBank = quote_token_bank self.spot_market: typing.Optional[GroupSlotSpotMarket] = spot_market_info self.perp_market: typing.Optional[GroupSlotPerpMarket] = perp_market_info self.perp_lot_size_converter: LotSizeConverter = perp_lot_size_converter self.oracle: PublicKey = oracle def __str__(self) -> str: base_token_bank = f"{self.base_token_bank}".replace("\n", "\n ") quote_token_bank = f"{self.quote_token_bank}".replace("\n", "\n ") spot_market_info = f"{self.spot_market}".replace("\n", "\n ") perp_market_info = f"{self.perp_market}".replace("\n", "\n ") return f"""« 𝙶𝚛𝚘𝚞𝚙𝚂𝚕𝚘𝚝[{self.index}] {self.base_instrument} Base Token Info: {base_token_bank} Quote Token Info: {quote_token_bank} Oracle: {self.oracle} Spot Market: {spot_market_info} Perp Market: {perp_market_info} »""" def __repr__(self) -> str: return f"{self}" # # 🥭 Group class # # `Group` defines root functionality for Mango Markets. # class Group(AddressableAccount): def __init__(self, account_info: AccountInfo, version: Version, name: str, meta_data: Metadata, shared_quote: TokenBank, slot_indices: typing.Sequence[bool], slots: typing.Sequence[GroupSlot], signer_nonce: Decimal, signer_key: PublicKey, admin: PublicKey, serum_program_address: PublicKey, cache: PublicKey, valid_interval: Decimal, insurance_vault: PublicKey, srm_vault: PublicKey, msrm_vault: PublicKey, fees_vault: PublicKey) -> None: super().__init__(account_info) self.version: Version = version self.name: str = name self.meta_data: Metadata = meta_data self.shared_quote: TokenBank = shared_quote self.slot_indices: typing.Sequence[bool] = slot_indices self.slots: typing.Sequence[GroupSlot] = slots self.signer_nonce: Decimal = signer_nonce self.signer_key: PublicKey = signer_key self.admin: PublicKey = admin self.serum_program_address: PublicKey = serum_program_address self.cache: PublicKey = cache self.valid_interval: Decimal = valid_interval self.insurance_vault: PublicKey = insurance_vault self.srm_vault: PublicKey = srm_vault self.msrm_vault: PublicKey = msrm_vault self.fees_vault: PublicKey = fees_vault @property def shared_quote_token(self) -> Token: return Token.ensure(self.shared_quote.token) @property def liquidity_incentive_token_bank(self) -> TokenBank: for token_bank in self.tokens: if token_bank.token.symbol_matches("MNGO"): return token_bank raise Exception(f"Could not find token info for symbol 'MNGO' in group {self.address}") @property def liquidity_incentive_token(self) -> Token: return Token.ensure(self.liquidity_incentive_token_bank.token) @property def tokens(self) -> typing.Sequence[TokenBank]: return [*self.base_tokens, self.shared_quote] @property def tokens_by_index(self) -> typing.Sequence[typing.Optional[TokenBank]]: return [*self.base_tokens_by_index, self.shared_quote] @property def slots_by_index(self) -> typing.Sequence[typing.Optional[GroupSlot]]: mapped_items: typing.List[typing.Optional[GroupSlot]] = [] slot_counter = 0 for available in self.slot_indices: if available: mapped_items += [self.slots[slot_counter]] slot_counter += 1 else: mapped_items += [None] return mapped_items @property def base_tokens(self) -> typing.Sequence[TokenBank]: return [slot.base_token_bank for slot in self.slots if slot.base_token_bank is not None] @property def base_tokens_by_index(self) -> typing.Sequence[typing.Optional[TokenBank]]: return [slot.base_token_bank if slot is not None else None for slot in self.slots_by_index] @property def oracles(self) -> typing.Sequence[PublicKey]: return [slot.oracle for slot in self.slots if slot.oracle is not None] @property def oracles_by_index(self) -> typing.Sequence[typing.Optional[PublicKey]]: return [slot.oracle if slot is not None else None for slot in self.slots_by_index] @property def spot_markets(self) -> typing.Sequence[GroupSlotSpotMarket]: return [slot.spot_market for slot in self.slots if slot.spot_market is not None] @property def spot_markets_by_index(self) -> typing.Sequence[typing.Optional[GroupSlotSpotMarket]]: return [slot.spot_market if slot is not None else None for slot in self.slots_by_index] @property def perp_markets(self) -> typing.Sequence[GroupSlotPerpMarket]: return [slot.perp_market for slot in self.slots if slot.perp_market is not None] @property def perp_markets_by_index(self) -> typing.Sequence[typing.Optional[GroupSlotPerpMarket]]: return [slot.perp_market if slot is not None else None for slot in self.slots_by_index] @staticmethod def from_layout(layout: typing.Any, name: str, account_info: AccountInfo, version: Version, instrument_lookup: InstrumentLookup, market_lookup: MarketLookup) -> "Group": meta_data: Metadata = Metadata.from_layout(layout.meta_data) tokens: typing.List[typing.Optional[TokenBank]] = [ TokenBank.from_layout_or_none(t, instrument_lookup) for t in layout.tokens] # By convention, the shared quote token is always at the end. quote_token_bank: typing.Optional[TokenBank] = tokens[-1] if quote_token_bank is None: raise Exception("Could not find quote token info at end of group tokens.") slots: typing.List[GroupSlot] = [] in_slots: typing.List[bool] = [] for index in range(len(tokens) - 1): spot_market_info: typing.Optional[GroupSlotSpotMarket] = GroupSlotSpotMarket.from_layout_or_none( layout.spot_markets[index]) perp_market_info: typing.Optional[GroupSlotPerpMarket] = GroupSlotPerpMarket.from_layout_or_none( layout.perp_markets[index]) if (spot_market_info is None) and (perp_market_info is None): in_slots += [False] else: perp_lot_size_converter: LotSizeConverter = RaisingLotSizeConverter() base_token_bank: typing.Optional[TokenBank] = tokens[index] base_instrument: Instrument if base_token_bank is not None: base_instrument = base_token_bank.token else: # It's possible there's no underlying SPL token and we have a pure PERP market. if perp_market_info is None: raise Exception(f"Cannot find base token or perp market info for index {index}") perp_market = market_lookup.find_by_address(perp_market_info.address) if perp_market is None: raise Exception(f"Cannot find base token or perp market for index {index}") base_instrument = perp_market.base if perp_market_info is not None: perp_lot_size_converter = LotSizeConverter( base_instrument, perp_market_info.base_lot_size, quote_token_bank.token, perp_market_info.quote_lot_size) oracle: PublicKey = layout.oracles[index] slot: GroupSlot = GroupSlot(index, base_instrument, base_token_bank, quote_token_bank, spot_market_info, perp_market_info, perp_lot_size_converter, oracle) slots += [slot] in_slots += [True] signer_nonce: Decimal = layout.signer_nonce signer_key: PublicKey = layout.signer_key admin: PublicKey = layout.admin serum_program_address: PublicKey = layout.serum_program_address cache_address: PublicKey = layout.cache valid_interval: Decimal = layout.valid_interval insurance_vault: PublicKey = layout.insurance_vault srm_vault: PublicKey = layout.srm_vault msrm_vault: PublicKey = layout.msrm_vault fees_vault: PublicKey = layout.fees_vault return Group(account_info, version, name, meta_data, quote_token_bank, in_slots, slots, signer_nonce, signer_key, admin, serum_program_address, cache_address, valid_interval, insurance_vault, srm_vault, msrm_vault, fees_vault) @staticmethod def parse(account_info: AccountInfo, name: str, instrument_lookup: InstrumentLookup, market_lookup: MarketLookup) -> "Group": data = account_info.data if len(data) != layouts.GROUP.sizeof(): raise Exception( f"Group data length ({len(data)}) does not match expected size ({layouts.GROUP.sizeof()})") layout = layouts.GROUP.parse(data) return Group.from_layout(layout, name, account_info, Version.V3, instrument_lookup, market_lookup) @staticmethod def parse_with_context(context: Context, account_info: AccountInfo) -> "Group": name = context.lookup_group_name(account_info.address) return Group.parse(account_info, name, context.instrument_lookup, context.market_lookup) @staticmethod def load(context: Context, address: typing.Optional[PublicKey] = None) -> "Group": group_address: PublicKey = address or context.group_address account_info = AccountInfo.load(context, group_address) if account_info is None: raise Exception(f"Group account not found at address '{group_address}'") name = context.lookup_group_name(account_info.address) return Group.parse(account_info, name, context.instrument_lookup, context.market_lookup) def slot_by_spot_market_address(self, spot_market_address: PublicKey) -> GroupSlot: for slot in self.slots: if slot.spot_market is not None and slot.spot_market.address == spot_market_address: return slot raise Exception(f"Could not find spot market {spot_market_address} in group {self.address}") def slot_by_perp_market_address(self, perp_market_address: PublicKey) -> GroupSlot: for slot in self.slots: if slot.perp_market is not None and slot.perp_market.address == perp_market_address: return slot raise Exception(f"Could not find perp market {perp_market_address} in group {self.address}") def slot_by_instrument_or_none(self, instrument: Instrument) -> typing.Optional[GroupSlot]: for slot in self.slots: if slot.base_instrument == instrument: return slot return None def slot_by_instrument(self, instrument: Instrument) -> GroupSlot: slot: typing.Optional[GroupSlot] = self.slot_by_instrument_or_none(instrument) if slot is not None: return slot raise Exception(f"Could not find slot for {instrument} in group {self.address}") def token_bank_by_instrument(self, instrument: Instrument) -> TokenBank: for token_bank in self.tokens: if token_bank.token == instrument: return token_bank raise Exception(f"Could not find token {instrument} in group {self.address}") def token_price_from_cache(self, cache: Cache, token: Instrument) -> InstrumentValue: market_cache: MarketCache = self.market_cache_from_cache(cache, token) return market_cache.adjusted_price(token, self.shared_quote_token) def perp_market_cache_from_cache(self, cache: Cache, token: Instrument) -> typing.Optional[PerpMarketCache]: market_cache: MarketCache = self.market_cache_from_cache(cache, token) return market_cache.perp_market def market_cache_from_cache(self, cache: Cache, instrument: Instrument) -> MarketCache: slot: GroupSlot = self.slot_by_instrument(instrument) instrument_index: int = slot.index return cache.market_cache_for_index(instrument_index) def fetch_cache(self, context: Context) -> Cache: return Cache.load(context, self.cache) def __str__(self) -> str: slot_count = len(self.slots) slots = "\n ".join([f"{item}".replace("\n", "\n ") for item in self.slots]) return f"""« 𝙶𝚛𝚘𝚞𝚙 {self.version} [{self.address}] {self.meta_data} Name: {self.name} Signer [Nonce: {self.signer_nonce}]: {self.signer_key} Admin: {self.admin} DEX Program ID: {self.serum_program_address} Cache: {self.cache} Insurance Vault: {self.insurance_vault} SRM Vault: {self.srm_vault} MSRM Vault: {self.msrm_vault} Fees Vault: {self.fees_vault} Valid Interval: {self.valid_interval} Basket [{slot_count} markets]: {slots} »"""
46.120773
320
0.697601
import typing from decimal import Decimal from solana.publickey import PublicKey from .accountinfo import AccountInfo from .addressableaccount import AddressableAccount from .cache import Cache, PerpMarketCache, MarketCache from .constants import SYSTEM_PROGRAM_ADDRESS from .context import Context from .instrumentlookup import InstrumentLookup from .instrumentvalue import InstrumentValue from .layouts import layouts from .lotsizeconverter import LotSizeConverter, RaisingLotSizeConverter from .marketlookup import MarketLookup from .metadata import Metadata from .token import Instrument, Token from .tokenbank import TokenBank from .version import Version class GroupSlotSpotMarket: def __init__(self, address: PublicKey, maint_asset_weight: Decimal, init_asset_weight: Decimal, maint_liab_weight: Decimal, init_liab_weight: Decimal) -> None: self.address: PublicKey = address self.maint_asset_weight: Decimal = maint_asset_weight self.init_asset_weight: Decimal = init_asset_weight self.maint_liab_weight: Decimal = maint_liab_weight self.init_liab_weight: Decimal = init_liab_weight @staticmethod def from_layout(layout: typing.Any) -> "GroupSlotSpotMarket": spot_market: PublicKey = layout.spot_market maint_asset_weight: Decimal = round(layout.maint_asset_weight, 8) init_asset_weight: Decimal = round(layout.init_asset_weight, 8) maint_liab_weight: Decimal = round(layout.maint_liab_weight, 8) init_liab_weight: Decimal = round(layout.init_liab_weight, 8) return GroupSlotSpotMarket(spot_market, maint_asset_weight, init_asset_weight, maint_liab_weight, init_liab_weight) @staticmethod def from_layout_or_none(layout: typing.Any) -> typing.Optional["GroupSlotSpotMarket"]: if (layout.spot_market is None) or (layout.spot_market == SYSTEM_PROGRAM_ADDRESS): return None return GroupSlotSpotMarket.from_layout(layout) def __str__(self) -> str: return f"""« 𝙶𝚛𝚘𝚞𝚙𝚂𝚕𝚘𝚝𝚂𝚙𝚘𝚝𝙼𝚊𝚛𝚔𝚎𝚝 [{self.address}] Asset Weights: Initial: {self.init_asset_weight} Maintenance: {self.maint_asset_weight} Liability Weights: Initial: {self.init_liab_weight} Maintenance: {self.maint_liab_weight} »""" def __repr__(self) -> str: return f"{self}" class GroupSlotPerpMarket: def __init__(self, address: PublicKey, maint_asset_weight: Decimal, init_asset_weight: Decimal, maint_liab_weight: Decimal, init_liab_weight: Decimal, liquidation_fee: Decimal, base_lot_size: Decimal, quote_lot_size: Decimal) -> None: self.address: PublicKey = address self.maint_asset_weight: Decimal = maint_asset_weight self.init_asset_weight: Decimal = init_asset_weight self.maint_liab_weight: Decimal = maint_liab_weight self.init_liab_weight: Decimal = init_liab_weight self.liquidation_fee: Decimal = liquidation_fee self.base_lot_size: Decimal = base_lot_size self.quote_lot_size: Decimal = quote_lot_size @staticmethod def from_layout(layout: typing.Any) -> "GroupSlotPerpMarket": perp_market: PublicKey = layout.perp_market maint_asset_weight: Decimal = round(layout.maint_asset_weight, 8) init_asset_weight: Decimal = round(layout.init_asset_weight, 8) maint_liab_weight: Decimal = round(layout.maint_liab_weight, 8) init_liab_weight: Decimal = round(layout.init_liab_weight, 8) liquidation_fee: Decimal = round(layout.liquidation_fee, 8) base_lot_size: Decimal = layout.base_lot_size quote_lot_size: Decimal = layout.quote_lot_size return GroupSlotPerpMarket(perp_market, maint_asset_weight, init_asset_weight, maint_liab_weight, init_liab_weight, liquidation_fee, base_lot_size, quote_lot_size) @staticmethod def from_layout_or_none(layout: typing.Any) -> typing.Optional["GroupSlotPerpMarket"]: if (layout.perp_market is None) or (layout.perp_market == SYSTEM_PROGRAM_ADDRESS): return None return GroupSlotPerpMarket.from_layout(layout) def __str__(self) -> str: return f"""« 𝙶𝚛𝚘𝚞𝚙𝚂𝚕𝚘𝚝𝙿𝚎𝚛𝚙𝙼𝚊𝚛𝚔𝚎𝚝 [{self.address}] Asset Weights: Initial: {self.init_asset_weight} Maintenance: {self.maint_asset_weight} Liability Weights: Initial: {self.init_liab_weight} Maintenance: {self.maint_liab_weight} Liquidation Fee: {self.liquidation_fee} Base Lot Size: {self.base_lot_size} Quote Lot Size: {self.quote_lot_size} »""" def __repr__(self) -> str: return f"{self}" class GroupSlot: def __init__(self, index: int, base_instrument: Instrument, base_token_bank: typing.Optional[TokenBank], quote_token_bank: TokenBank, spot_market_info: typing.Optional[GroupSlotSpotMarket], perp_market_info: typing.Optional[GroupSlotPerpMarket], perp_lot_size_converter: LotSizeConverter, oracle: PublicKey) -> None: self.index: int = index self.base_instrument: Instrument = base_instrument self.base_token_bank: typing.Optional[TokenBank] = base_token_bank self.quote_token_bank: TokenBank = quote_token_bank self.spot_market: typing.Optional[GroupSlotSpotMarket] = spot_market_info self.perp_market: typing.Optional[GroupSlotPerpMarket] = perp_market_info self.perp_lot_size_converter: LotSizeConverter = perp_lot_size_converter self.oracle: PublicKey = oracle def __str__(self) -> str: base_token_bank = f"{self.base_token_bank}".replace("\n", "\n ") quote_token_bank = f"{self.quote_token_bank}".replace("\n", "\n ") spot_market_info = f"{self.spot_market}".replace("\n", "\n ") perp_market_info = f"{self.perp_market}".replace("\n", "\n ") return f"""« 𝙶𝚛𝚘𝚞𝚙𝚂𝚕𝚘𝚝[{self.index}] {self.base_instrument} Base Token Info: {base_token_bank} Quote Token Info: {quote_token_bank} Oracle: {self.oracle} Spot Market: {spot_market_info} Perp Market: {perp_market_info} »""" def __repr__(self) -> str: return f"{self}" class Group(AddressableAccount): def __init__(self, account_info: AccountInfo, version: Version, name: str, meta_data: Metadata, shared_quote: TokenBank, slot_indices: typing.Sequence[bool], slots: typing.Sequence[GroupSlot], signer_nonce: Decimal, signer_key: PublicKey, admin: PublicKey, serum_program_address: PublicKey, cache: PublicKey, valid_interval: Decimal, insurance_vault: PublicKey, srm_vault: PublicKey, msrm_vault: PublicKey, fees_vault: PublicKey) -> None: super().__init__(account_info) self.version: Version = version self.name: str = name self.meta_data: Metadata = meta_data self.shared_quote: TokenBank = shared_quote self.slot_indices: typing.Sequence[bool] = slot_indices self.slots: typing.Sequence[GroupSlot] = slots self.signer_nonce: Decimal = signer_nonce self.signer_key: PublicKey = signer_key self.admin: PublicKey = admin self.serum_program_address: PublicKey = serum_program_address self.cache: PublicKey = cache self.valid_interval: Decimal = valid_interval self.insurance_vault: PublicKey = insurance_vault self.srm_vault: PublicKey = srm_vault self.msrm_vault: PublicKey = msrm_vault self.fees_vault: PublicKey = fees_vault @property def shared_quote_token(self) -> Token: return Token.ensure(self.shared_quote.token) @property def liquidity_incentive_token_bank(self) -> TokenBank: for token_bank in self.tokens: if token_bank.token.symbol_matches("MNGO"): return token_bank raise Exception(f"Could not find token info for symbol 'MNGO' in group {self.address}") @property def liquidity_incentive_token(self) -> Token: return Token.ensure(self.liquidity_incentive_token_bank.token) @property def tokens(self) -> typing.Sequence[TokenBank]: return [*self.base_tokens, self.shared_quote] @property def tokens_by_index(self) -> typing.Sequence[typing.Optional[TokenBank]]: return [*self.base_tokens_by_index, self.shared_quote] @property def slots_by_index(self) -> typing.Sequence[typing.Optional[GroupSlot]]: mapped_items: typing.List[typing.Optional[GroupSlot]] = [] slot_counter = 0 for available in self.slot_indices: if available: mapped_items += [self.slots[slot_counter]] slot_counter += 1 else: mapped_items += [None] return mapped_items @property def base_tokens(self) -> typing.Sequence[TokenBank]: return [slot.base_token_bank for slot in self.slots if slot.base_token_bank is not None] @property def base_tokens_by_index(self) -> typing.Sequence[typing.Optional[TokenBank]]: return [slot.base_token_bank if slot is not None else None for slot in self.slots_by_index] @property def oracles(self) -> typing.Sequence[PublicKey]: return [slot.oracle for slot in self.slots if slot.oracle is not None] @property def oracles_by_index(self) -> typing.Sequence[typing.Optional[PublicKey]]: return [slot.oracle if slot is not None else None for slot in self.slots_by_index] @property def spot_markets(self) -> typing.Sequence[GroupSlotSpotMarket]: return [slot.spot_market for slot in self.slots if slot.spot_market is not None] @property def spot_markets_by_index(self) -> typing.Sequence[typing.Optional[GroupSlotSpotMarket]]: return [slot.spot_market if slot is not None else None for slot in self.slots_by_index] @property def perp_markets(self) -> typing.Sequence[GroupSlotPerpMarket]: return [slot.perp_market for slot in self.slots if slot.perp_market is not None] @property def perp_markets_by_index(self) -> typing.Sequence[typing.Optional[GroupSlotPerpMarket]]: return [slot.perp_market if slot is not None else None for slot in self.slots_by_index] @staticmethod def from_layout(layout: typing.Any, name: str, account_info: AccountInfo, version: Version, instrument_lookup: InstrumentLookup, market_lookup: MarketLookup) -> "Group": meta_data: Metadata = Metadata.from_layout(layout.meta_data) tokens: typing.List[typing.Optional[TokenBank]] = [ TokenBank.from_layout_or_none(t, instrument_lookup) for t in layout.tokens] quote_token_bank: typing.Optional[TokenBank] = tokens[-1] if quote_token_bank is None: raise Exception("Could not find quote token info at end of group tokens.") slots: typing.List[GroupSlot] = [] in_slots: typing.List[bool] = [] for index in range(len(tokens) - 1): spot_market_info: typing.Optional[GroupSlotSpotMarket] = GroupSlotSpotMarket.from_layout_or_none( layout.spot_markets[index]) perp_market_info: typing.Optional[GroupSlotPerpMarket] = GroupSlotPerpMarket.from_layout_or_none( layout.perp_markets[index]) if (spot_market_info is None) and (perp_market_info is None): in_slots += [False] else: perp_lot_size_converter: LotSizeConverter = RaisingLotSizeConverter() base_token_bank: typing.Optional[TokenBank] = tokens[index] base_instrument: Instrument if base_token_bank is not None: base_instrument = base_token_bank.token else: if perp_market_info is None: raise Exception(f"Cannot find base token or perp market info for index {index}") perp_market = market_lookup.find_by_address(perp_market_info.address) if perp_market is None: raise Exception(f"Cannot find base token or perp market for index {index}") base_instrument = perp_market.base if perp_market_info is not None: perp_lot_size_converter = LotSizeConverter( base_instrument, perp_market_info.base_lot_size, quote_token_bank.token, perp_market_info.quote_lot_size) oracle: PublicKey = layout.oracles[index] slot: GroupSlot = GroupSlot(index, base_instrument, base_token_bank, quote_token_bank, spot_market_info, perp_market_info, perp_lot_size_converter, oracle) slots += [slot] in_slots += [True] signer_nonce: Decimal = layout.signer_nonce signer_key: PublicKey = layout.signer_key admin: PublicKey = layout.admin serum_program_address: PublicKey = layout.serum_program_address cache_address: PublicKey = layout.cache valid_interval: Decimal = layout.valid_interval insurance_vault: PublicKey = layout.insurance_vault srm_vault: PublicKey = layout.srm_vault msrm_vault: PublicKey = layout.msrm_vault fees_vault: PublicKey = layout.fees_vault return Group(account_info, version, name, meta_data, quote_token_bank, in_slots, slots, signer_nonce, signer_key, admin, serum_program_address, cache_address, valid_interval, insurance_vault, srm_vault, msrm_vault, fees_vault) @staticmethod def parse(account_info: AccountInfo, name: str, instrument_lookup: InstrumentLookup, market_lookup: MarketLookup) -> "Group": data = account_info.data if len(data) != layouts.GROUP.sizeof(): raise Exception( f"Group data length ({len(data)}) does not match expected size ({layouts.GROUP.sizeof()})") layout = layouts.GROUP.parse(data) return Group.from_layout(layout, name, account_info, Version.V3, instrument_lookup, market_lookup) @staticmethod def parse_with_context(context: Context, account_info: AccountInfo) -> "Group": name = context.lookup_group_name(account_info.address) return Group.parse(account_info, name, context.instrument_lookup, context.market_lookup) @staticmethod def load(context: Context, address: typing.Optional[PublicKey] = None) -> "Group": group_address: PublicKey = address or context.group_address account_info = AccountInfo.load(context, group_address) if account_info is None: raise Exception(f"Group account not found at address '{group_address}'") name = context.lookup_group_name(account_info.address) return Group.parse(account_info, name, context.instrument_lookup, context.market_lookup) def slot_by_spot_market_address(self, spot_market_address: PublicKey) -> GroupSlot: for slot in self.slots: if slot.spot_market is not None and slot.spot_market.address == spot_market_address: return slot raise Exception(f"Could not find spot market {spot_market_address} in group {self.address}") def slot_by_perp_market_address(self, perp_market_address: PublicKey) -> GroupSlot: for slot in self.slots: if slot.perp_market is not None and slot.perp_market.address == perp_market_address: return slot raise Exception(f"Could not find perp market {perp_market_address} in group {self.address}") def slot_by_instrument_or_none(self, instrument: Instrument) -> typing.Optional[GroupSlot]: for slot in self.slots: if slot.base_instrument == instrument: return slot return None def slot_by_instrument(self, instrument: Instrument) -> GroupSlot: slot: typing.Optional[GroupSlot] = self.slot_by_instrument_or_none(instrument) if slot is not None: return slot raise Exception(f"Could not find slot for {instrument} in group {self.address}") def token_bank_by_instrument(self, instrument: Instrument) -> TokenBank: for token_bank in self.tokens: if token_bank.token == instrument: return token_bank raise Exception(f"Could not find token {instrument} in group {self.address}") def token_price_from_cache(self, cache: Cache, token: Instrument) -> InstrumentValue: market_cache: MarketCache = self.market_cache_from_cache(cache, token) return market_cache.adjusted_price(token, self.shared_quote_token) def perp_market_cache_from_cache(self, cache: Cache, token: Instrument) -> typing.Optional[PerpMarketCache]: market_cache: MarketCache = self.market_cache_from_cache(cache, token) return market_cache.perp_market def market_cache_from_cache(self, cache: Cache, instrument: Instrument) -> MarketCache: slot: GroupSlot = self.slot_by_instrument(instrument) instrument_index: int = slot.index return cache.market_cache_for_index(instrument_index) def fetch_cache(self, context: Context) -> Cache: return Cache.load(context, self.cache) def __str__(self) -> str: slot_count = len(self.slots) slots = "\n ".join([f"{item}".replace("\n", "\n ") for item in self.slots]) return f"""« 𝙶𝚛𝚘𝚞𝚙 {self.version} [{self.address}] {self.meta_data} Name: {self.name} Signer [Nonce: {self.signer_nonce}]: {self.signer_key} Admin: {self.admin} DEX Program ID: {self.serum_program_address} Cache: {self.cache} Insurance Vault: {self.insurance_vault} SRM Vault: {self.srm_vault} MSRM Vault: {self.msrm_vault} Fees Vault: {self.fees_vault} Valid Interval: {self.valid_interval} Basket [{slot_count} markets]: {slots} »"""
true
true
f7060f8c4698c96e5529ffec9c6be7b4e1582361
114
py
Python
Algorithm/sort/__init__.py
hyhplus/LeetCodeByPython
ebc0a59610e4c8b72660a9c96f408cc3319a7c28
[ "MIT" ]
2
2019-01-03T10:04:50.000Z
2019-10-29T07:46:39.000Z
Algorithm/sort/__init__.py
hyhplus/LeetCodeByPython
ebc0a59610e4c8b72660a9c96f408cc3319a7c28
[ "MIT" ]
null
null
null
Algorithm/sort/__init__.py
hyhplus/LeetCodeByPython
ebc0a59610e4c8b72660a9c96f408cc3319a7c28
[ "MIT" ]
null
null
null
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 排序算法: O(n^2) 冒泡排序 插入排序 选择排序 Q(n log n) 分而治之 快速排序 归并排序 """
6.333333
23
0.552632
true
true
f7061066591ffa49a0024bd409f76b17d31c1a6f
10,337
py
Python
lib/galaxy/webapps/galaxy/services/dataset_collections.py
beatrizserrano/galaxy
e149d9d32e1bca6c07c38b1a9cdabfee60323610
[ "CC-BY-3.0" ]
null
null
null
lib/galaxy/webapps/galaxy/services/dataset_collections.py
beatrizserrano/galaxy
e149d9d32e1bca6c07c38b1a9cdabfee60323610
[ "CC-BY-3.0" ]
2
2017-05-18T16:12:55.000Z
2022-03-08T12:08:43.000Z
lib/galaxy/webapps/galaxy/services/dataset_collections.py
beatrizserrano/galaxy
e149d9d32e1bca6c07c38b1a9cdabfee60323610
[ "CC-BY-3.0" ]
null
null
null
from logging import getLogger from typing import ( List, Optional, Set, ) from pydantic import ( BaseModel, Extra, Field, ) from galaxy import exceptions from galaxy.datatypes.registry import Registry from galaxy.managers.collections import DatasetCollectionManager from galaxy.managers.collections_util import ( api_payload_to_create_params, dictify_dataset_collection_instance, dictify_element_reference, ) from galaxy.managers.context import ProvidesHistoryContext from galaxy.managers.hdcas import HDCAManager from galaxy.managers.histories import HistoryManager from galaxy.schema.fields import ( EncodedDatabaseIdField, ModelClassField, ) from galaxy.schema.schema import ( AnyHDCA, CreateNewCollectionPayload, DatasetCollectionInstanceType, DCESummary, DCEType, HDCADetailed, TagCollection, ) from galaxy.security.idencoding import IdEncodingHelper from galaxy.webapps.base.controller import UsesLibraryMixinItems from galaxy.webapps.galaxy.services.base import ServiceBase log = getLogger(__name__) class UpdateCollectionAttributePayload(BaseModel): """Contains attributes that can be updated for all elements in a dataset collection.""" dbkey: str = Field(..., description="TODO") class Config: extra = Extra.forbid # will cause validation to fail if extra attributes are included, class DatasetCollectionAttributesResult(BaseModel): dbkey: str = Field(..., description="TODO") # Are the following fields really used/needed? extension: str = Field(..., description="The dataset file extension.", example="txt") model_class: str = ModelClassField("HistoryDatasetCollectionAssociation") dbkeys: Optional[Set[str]] extensions: Optional[Set[str]] tags: TagCollection class SuitableConverter(BaseModel): tool_id: str = Field(..., description="The ID of the tool that can perform the type conversion.") name: str = Field(..., description="The name of the converter.") target_type: str = Field(..., description="The type to convert to.") original_type: str = Field(..., description="The type to convert from.") class SuitableConverters(BaseModel): """Collection of converters that can be used on a particular dataset collection.""" __root__: List[SuitableConverter] class DatasetCollectionContentElements(BaseModel): """Represents a collection of elements contained in the dataset collection.""" __root__: List[DCESummary] class DatasetCollectionsService(ServiceBase, UsesLibraryMixinItems): def __init__( self, security: IdEncodingHelper, history_manager: HistoryManager, hdca_manager: HDCAManager, collection_manager: DatasetCollectionManager, datatypes_registry: Registry, ): super().__init__(security) self.history_manager = history_manager self.hdca_manager = hdca_manager self.collection_manager = collection_manager self.datatypes_registry = datatypes_registry def create(self, trans: ProvidesHistoryContext, payload: CreateNewCollectionPayload) -> HDCADetailed: """ Create a new dataset collection instance. :type payload: dict :param payload: (optional) dictionary structure containing: * collection_type: dataset collection type to create. * instance_type: Instance type - 'history' or 'library'. * name: the new dataset collections's name * datasets: object describing datasets for collection :rtype: dict :returns: element view of new dataset collection """ # TODO: Error handling... create_params = api_payload_to_create_params(payload.dict(exclude_unset=True)) if payload.instance_type == DatasetCollectionInstanceType.history: if payload.history_id is None: raise exceptions.RequestParameterInvalidException("Parameter history_id is required.") history_id = self.decode_id(payload.history_id) history = self.history_manager.get_owned(history_id, trans.user, current_history=trans.history) create_params["parent"] = history create_params["history"] = history elif payload.instance_type == DatasetCollectionInstanceType.library: library_folder = self.get_library_folder(trans, payload.folder_id, check_accessible=True) self.check_user_can_add_to_library_item(trans, library_folder, check_accessible=False) create_params["parent"] = library_folder else: raise exceptions.RequestParameterInvalidException() dataset_collection_instance = self.collection_manager.create(trans=trans, **create_params) rval = dictify_dataset_collection_instance( dataset_collection_instance, security=trans.security, url_builder=trans.url_builder, parent=create_params["parent"], ) return rval def copy( self, trans: ProvidesHistoryContext, id: EncodedDatabaseIdField, payload: UpdateCollectionAttributePayload ): """ Iterate over all datasets of a collection and copy datasets with new attributes to a new collection. e.g attributes = {'dbkey': 'dm3'} """ self.collection_manager.copy( trans, trans.history, "hdca", id, copy_elements=True, dataset_instance_attributes=payload.dict() ) def attributes( self, trans: ProvidesHistoryContext, id: EncodedDatabaseIdField, instance_type: DatasetCollectionInstanceType = DatasetCollectionInstanceType.history, ) -> DatasetCollectionAttributesResult: """ Returns dbkey/extension for collection elements """ dataset_collection_instance = self.collection_manager.get_dataset_collection_instance( trans, id=id, instance_type=instance_type, check_ownership=True ) rval = dataset_collection_instance.to_dict(view="dbkeysandextensions") return rval def suitable_converters( self, trans: ProvidesHistoryContext, id: EncodedDatabaseIdField, instance_type: DatasetCollectionInstanceType = DatasetCollectionInstanceType.history, ) -> SuitableConverters: """ Returns suitable converters for all datatypes in collection """ rval = self.collection_manager.get_converters_for_collection(trans, id, self.datatypes_registry, instance_type) return rval def show( self, trans: ProvidesHistoryContext, id: EncodedDatabaseIdField, instance_type: DatasetCollectionInstanceType = DatasetCollectionInstanceType.history, ) -> AnyHDCA: """ Returns information about a particular dataset collection. """ dataset_collection_instance = self.collection_manager.get_dataset_collection_instance( trans, id=id, instance_type=instance_type, ) if instance_type == DatasetCollectionInstanceType.history: parent = dataset_collection_instance.history elif instance_type == DatasetCollectionInstanceType.library: parent = dataset_collection_instance.folder else: raise exceptions.RequestParameterInvalidException() rval = dictify_dataset_collection_instance( dataset_collection_instance, security=trans.security, url_builder=trans.url_builder, parent=parent, view="element", ) return rval def contents( self, trans: ProvidesHistoryContext, hdca_id: EncodedDatabaseIdField, parent_id: EncodedDatabaseIdField, instance_type: DatasetCollectionInstanceType = DatasetCollectionInstanceType.history, limit: Optional[int] = None, offset: Optional[int] = None, ) -> DatasetCollectionContentElements: """ Shows direct child contents of indicated dataset collection parent id :type string: encoded string id :param id: HDCA.id :type string: encoded string id :param parent_id: parent dataset_collection.id for the dataset contents to be viewed :type integer: int :param limit: pagination limit for returned dataset collection elements :type integer: int :param offset: pagination offset for returned dataset collection elements :rtype: list :returns: list of dataset collection elements and contents """ # validate HDCA for current user, will throw error if not permitted # TODO: refactor get_dataset_collection_instance hdca = self.collection_manager.get_dataset_collection_instance( trans, id=hdca_id, check_ownership=True, instance_type=instance_type ) # check to make sure the dsc is part of the validated hdca decoded_parent_id = self.decode_id(parent_id) if parent_id != hdca_id and not hdca.contains_collection(decoded_parent_id): raise exceptions.ObjectNotFound( "Requested dataset collection is not contained within indicated history content" ) # retrieve contents contents = self.collection_manager.get_collection_contents(trans, decoded_parent_id, limit=limit, offset=offset) # dictify and tack on a collection_url for drilling down into nested collections def serialize_element(dsc_element) -> DCESummary: result = dictify_element_reference(dsc_element, recursive=False, security=trans.security) if result["element_type"] == DCEType.dataset_collection: assert trans.url_builder result["object"]["contents_url"] = trans.url_builder( "contents_dataset_collection", hdca_id=self.encode_id(hdca.id), parent_id=self.encode_id(result["object"]["id"]), ) trans.security.encode_all_ids(result, recursive=True) return result rval = [serialize_element(el) for el in contents] return DatasetCollectionContentElements.parse_obj(rval)
40.065891
120
0.689659
from logging import getLogger from typing import ( List, Optional, Set, ) from pydantic import ( BaseModel, Extra, Field, ) from galaxy import exceptions from galaxy.datatypes.registry import Registry from galaxy.managers.collections import DatasetCollectionManager from galaxy.managers.collections_util import ( api_payload_to_create_params, dictify_dataset_collection_instance, dictify_element_reference, ) from galaxy.managers.context import ProvidesHistoryContext from galaxy.managers.hdcas import HDCAManager from galaxy.managers.histories import HistoryManager from galaxy.schema.fields import ( EncodedDatabaseIdField, ModelClassField, ) from galaxy.schema.schema import ( AnyHDCA, CreateNewCollectionPayload, DatasetCollectionInstanceType, DCESummary, DCEType, HDCADetailed, TagCollection, ) from galaxy.security.idencoding import IdEncodingHelper from galaxy.webapps.base.controller import UsesLibraryMixinItems from galaxy.webapps.galaxy.services.base import ServiceBase log = getLogger(__name__) class UpdateCollectionAttributePayload(BaseModel): dbkey: str = Field(..., description="TODO") class Config: extra = Extra.forbid class DatasetCollectionAttributesResult(BaseModel): dbkey: str = Field(..., description="TODO") extension: str = Field(..., description="The dataset file extension.", example="txt") model_class: str = ModelClassField("HistoryDatasetCollectionAssociation") dbkeys: Optional[Set[str]] extensions: Optional[Set[str]] tags: TagCollection class SuitableConverter(BaseModel): tool_id: str = Field(..., description="The ID of the tool that can perform the type conversion.") name: str = Field(..., description="The name of the converter.") target_type: str = Field(..., description="The type to convert to.") original_type: str = Field(..., description="The type to convert from.") class SuitableConverters(BaseModel): __root__: List[SuitableConverter] class DatasetCollectionContentElements(BaseModel): __root__: List[DCESummary] class DatasetCollectionsService(ServiceBase, UsesLibraryMixinItems): def __init__( self, security: IdEncodingHelper, history_manager: HistoryManager, hdca_manager: HDCAManager, collection_manager: DatasetCollectionManager, datatypes_registry: Registry, ): super().__init__(security) self.history_manager = history_manager self.hdca_manager = hdca_manager self.collection_manager = collection_manager self.datatypes_registry = datatypes_registry def create(self, trans: ProvidesHistoryContext, payload: CreateNewCollectionPayload) -> HDCADetailed: create_params = api_payload_to_create_params(payload.dict(exclude_unset=True)) if payload.instance_type == DatasetCollectionInstanceType.history: if payload.history_id is None: raise exceptions.RequestParameterInvalidException("Parameter history_id is required.") history_id = self.decode_id(payload.history_id) history = self.history_manager.get_owned(history_id, trans.user, current_history=trans.history) create_params["parent"] = history create_params["history"] = history elif payload.instance_type == DatasetCollectionInstanceType.library: library_folder = self.get_library_folder(trans, payload.folder_id, check_accessible=True) self.check_user_can_add_to_library_item(trans, library_folder, check_accessible=False) create_params["parent"] = library_folder else: raise exceptions.RequestParameterInvalidException() dataset_collection_instance = self.collection_manager.create(trans=trans, **create_params) rval = dictify_dataset_collection_instance( dataset_collection_instance, security=trans.security, url_builder=trans.url_builder, parent=create_params["parent"], ) return rval def copy( self, trans: ProvidesHistoryContext, id: EncodedDatabaseIdField, payload: UpdateCollectionAttributePayload ): self.collection_manager.copy( trans, trans.history, "hdca", id, copy_elements=True, dataset_instance_attributes=payload.dict() ) def attributes( self, trans: ProvidesHistoryContext, id: EncodedDatabaseIdField, instance_type: DatasetCollectionInstanceType = DatasetCollectionInstanceType.history, ) -> DatasetCollectionAttributesResult: dataset_collection_instance = self.collection_manager.get_dataset_collection_instance( trans, id=id, instance_type=instance_type, check_ownership=True ) rval = dataset_collection_instance.to_dict(view="dbkeysandextensions") return rval def suitable_converters( self, trans: ProvidesHistoryContext, id: EncodedDatabaseIdField, instance_type: DatasetCollectionInstanceType = DatasetCollectionInstanceType.history, ) -> SuitableConverters: rval = self.collection_manager.get_converters_for_collection(trans, id, self.datatypes_registry, instance_type) return rval def show( self, trans: ProvidesHistoryContext, id: EncodedDatabaseIdField, instance_type: DatasetCollectionInstanceType = DatasetCollectionInstanceType.history, ) -> AnyHDCA: dataset_collection_instance = self.collection_manager.get_dataset_collection_instance( trans, id=id, instance_type=instance_type, ) if instance_type == DatasetCollectionInstanceType.history: parent = dataset_collection_instance.history elif instance_type == DatasetCollectionInstanceType.library: parent = dataset_collection_instance.folder else: raise exceptions.RequestParameterInvalidException() rval = dictify_dataset_collection_instance( dataset_collection_instance, security=trans.security, url_builder=trans.url_builder, parent=parent, view="element", ) return rval def contents( self, trans: ProvidesHistoryContext, hdca_id: EncodedDatabaseIdField, parent_id: EncodedDatabaseIdField, instance_type: DatasetCollectionInstanceType = DatasetCollectionInstanceType.history, limit: Optional[int] = None, offset: Optional[int] = None, ) -> DatasetCollectionContentElements: hdca = self.collection_manager.get_dataset_collection_instance( trans, id=hdca_id, check_ownership=True, instance_type=instance_type ) decoded_parent_id = self.decode_id(parent_id) if parent_id != hdca_id and not hdca.contains_collection(decoded_parent_id): raise exceptions.ObjectNotFound( "Requested dataset collection is not contained within indicated history content" ) contents = self.collection_manager.get_collection_contents(trans, decoded_parent_id, limit=limit, offset=offset) def serialize_element(dsc_element) -> DCESummary: result = dictify_element_reference(dsc_element, recursive=False, security=trans.security) if result["element_type"] == DCEType.dataset_collection: assert trans.url_builder result["object"]["contents_url"] = trans.url_builder( "contents_dataset_collection", hdca_id=self.encode_id(hdca.id), parent_id=self.encode_id(result["object"]["id"]), ) trans.security.encode_all_ids(result, recursive=True) return result rval = [serialize_element(el) for el in contents] return DatasetCollectionContentElements.parse_obj(rval)
true
true
f7061158552864417effbee3b029c9645befcd05
1,125
py
Python
netbox/dcim/migrations/0102_applicationtype.py
fireman0865/PingBox
0f00eaf88b88e9441fffd5173a1501e56c13db03
[ "Apache-2.0" ]
1
2021-09-23T00:06:51.000Z
2021-09-23T00:06:51.000Z
netbox/dcim/migrations/0102_applicationtype.py
fireman0865/PingBox
0f00eaf88b88e9441fffd5173a1501e56c13db03
[ "Apache-2.0" ]
2
2021-06-08T21:05:10.000Z
2021-09-08T01:46:58.000Z
netbox/dcim/migrations/0102_applicationtype.py
fireman0865/PingBox
0f00eaf88b88e9441fffd5173a1501e56c13db03
[ "Apache-2.0" ]
null
null
null
# Generated by Django 2.2.10 on 2020-03-04 23:25 from django.db import migrations, models class Migration(migrations.Migration): dependencies = [ ('dcim', '0101_auto_20200304_1403'), ] operations = [ migrations.CreateModel( name='ApplicationType', fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False)), ('created', models.DateField(auto_now_add=True, null=True)), ('last_updated', models.DateTimeField(auto_now=True, null=True)), ('name', models.CharField(max_length=100, unique=True)), ('slug', models.SlugField(max_length=100, unique=True)), ('language', models.CharField(blank=True, max_length=50, null=True)), ('actual_version', models.CharField(blank=True, max_length=50)), ('recommended_version', models.CharField(max_length=100)), ], options={ 'ordering': ['name', 'slug', 'language', 'actual_version', 'recommended_version'], }, ), ]
37.5
98
0.580444
from django.db import migrations, models class Migration(migrations.Migration): dependencies = [ ('dcim', '0101_auto_20200304_1403'), ] operations = [ migrations.CreateModel( name='ApplicationType', fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False)), ('created', models.DateField(auto_now_add=True, null=True)), ('last_updated', models.DateTimeField(auto_now=True, null=True)), ('name', models.CharField(max_length=100, unique=True)), ('slug', models.SlugField(max_length=100, unique=True)), ('language', models.CharField(blank=True, max_length=50, null=True)), ('actual_version', models.CharField(blank=True, max_length=50)), ('recommended_version', models.CharField(max_length=100)), ], options={ 'ordering': ['name', 'slug', 'language', 'actual_version', 'recommended_version'], }, ), ]
true
true
f7061171c0575a50ba7e9049d89c083ce46428d7
956
py
Python
built-in/TensorFlow/Official/cv/image_classification/ResnetVariant_for_TensorFlow/automl/test/core/test_run.py
Huawei-Ascend/modelzoo
df51ed9c1d6dbde1deef63f2a037a369f8554406
[ "Apache-2.0" ]
null
null
null
built-in/TensorFlow/Official/cv/image_classification/ResnetVariant_for_TensorFlow/automl/test/core/test_run.py
Huawei-Ascend/modelzoo
df51ed9c1d6dbde1deef63f2a037a369f8554406
[ "Apache-2.0" ]
3
2021-03-31T20:15:40.000Z
2022-02-09T23:50:46.000Z
built-in/TensorFlow/Research/cv/image_classification/Darts_for_TensorFlow/automl/test/core/test_run.py
Huawei-Ascend/modelzoo
df51ed9c1d6dbde1deef63f2a037a369f8554406
[ "Apache-2.0" ]
null
null
null
# -*- coding:utf-8 -*- """Test function for vega.run""" import unittest def lazy(func): """lazy function wrapper :param func: function name """ attr_name = "_lazy_" + func.__name__ def lazy_func(*args, **kwargs): """Wrapper of lazy func :param args: any object :param kwargs: any kwargs :return: """ if not hasattr(func, attr_name): setattr(func, attr_name, func(*args, **kwargs)) return getattr(func, attr_name) return lazy_func @lazy def env_args(args): """A lazy function will be execute when call :param args: any object :return: """ return args class TestPipeline(unittest.TestCase): """Test lazy function worked in pipeline""" def test_env_args(self): """Test function 'env_args' is a lazy function""" args = {'env': 'test'} env_args(args) self.assertEqual(env_args(), {'env': 'test'})
21.244444
59
0.593096
import unittest def lazy(func): attr_name = "_lazy_" + func.__name__ def lazy_func(*args, **kwargs): if not hasattr(func, attr_name): setattr(func, attr_name, func(*args, **kwargs)) return getattr(func, attr_name) return lazy_func @lazy def env_args(args): return args class TestPipeline(unittest.TestCase): def test_env_args(self): args = {'env': 'test'} env_args(args) self.assertEqual(env_args(), {'env': 'test'})
true
true
f7061176279a8b38453b28791f0ce65eda3f2bb7
3,308
py
Python
build/fbcode_builder/shell_quoting.py
dgrnbrg-meta/fbzmq
cf292f1042afe5c49dcf5231f16b86e49ddb186e
[ "MIT" ]
3
2022-02-10T10:48:36.000Z
2022-02-21T23:18:10.000Z
build/fbcode_builder/shell_quoting.py
dgrnbrg-meta/fbzmq
cf292f1042afe5c49dcf5231f16b86e49ddb186e
[ "MIT" ]
23
2021-11-11T12:38:17.000Z
2022-01-28T10:59:38.000Z
build/fbcode_builder/shell_quoting.py
dgrnbrg-meta/fbzmq
cf292f1042afe5c49dcf5231f16b86e49ddb186e
[ "MIT" ]
8
2021-11-11T12:26:21.000Z
2022-01-12T08:58:32.000Z
#!/usr/bin/env python # Copyright (c) Facebook, Inc. and its affiliates. """ Almost every FBCodeBuilder string is ultimately passed to a shell. Escaping too little or too much tends to be the most common error. The utilities in this file give a systematic way of avoiding such bugs: - When you write literal strings destined for the shell, use `ShellQuoted`. - When these literal strings are parameterized, use `ShellQuoted.format`. - Any parameters that are raw strings get `shell_quote`d automatically, while any ShellQuoted parameters will be left intact. - Use `path_join` to join path components. - Use `shell_join` to join already-quoted command arguments or shell lines. """ import os from collections import namedtuple # pyre-fixme[13] This is too magical for Pyre. class ShellQuoted(namedtuple("ShellQuoted", ("do_not_use_raw_str",))): """ Wrap a string with this to make it transparent to shell_quote(). It will almost always suffice to use ShellQuoted.format(), path_join(), or shell_join(). If you really must, use raw_shell() to access the raw string. """ def __new__(cls, s): "No need to nest ShellQuoted." return super(ShellQuoted, cls).__new__( cls, s.do_not_use_raw_str if isinstance(s, ShellQuoted) else s ) def __str__(self): raise RuntimeError( "One does not simply convert {0} to a string -- use path_join() " "or ShellQuoted.format() instead".format(repr(self)) ) def __repr__(self) -> str: return "{0}({1})".format(self.__class__.__name__, repr(self.do_not_use_raw_str)) def format(self, **kwargs) -> "ShellQuoted": """ Use instead of str.format() when the arguments are either `ShellQuoted()` or raw strings needing to be `shell_quote()`d. Positional args are deliberately not supported since they are more error-prone. """ return ShellQuoted( self.do_not_use_raw_str.format( **dict( (k, shell_quote(v).do_not_use_raw_str) for k, v in kwargs.items() ) ) ) def shell_quote(s) -> ShellQuoted: "Quotes a string if it is not already quoted" return ( s if isinstance(s, ShellQuoted) else ShellQuoted("'" + str(s).replace("'", "'\\''") + "'") ) def raw_shell(s: ShellQuoted): "Not a member of ShellQuoted so we get a useful error for raw strings" if isinstance(s, ShellQuoted): return s.do_not_use_raw_str raise RuntimeError("{0} should have been ShellQuoted".format(s)) def shell_join(delim, it) -> ShellQuoted: "Joins an iterable of ShellQuoted with a delimiter between each two" return ShellQuoted(delim.join(raw_shell(s) for s in it)) def path_join(*args) -> ShellQuoted: "Joins ShellQuoted and raw pieces of paths to make a shell-quoted path" return ShellQuoted(os.path.join(*[raw_shell(shell_quote(s)) for s in args])) def shell_comment(c: ShellQuoted) -> ShellQuoted: "Do not shell-escape raw strings in comments, but do handle line breaks." return ShellQuoted("# {c}").format( c=ShellQuoted( (raw_shell(c) if isinstance(c, ShellQuoted) else c).replace("\n", "\n# ") ) )
32.752475
88
0.658102
import os from collections import namedtuple class ShellQuoted(namedtuple("ShellQuoted", ("do_not_use_raw_str",))): def __new__(cls, s): return super(ShellQuoted, cls).__new__( cls, s.do_not_use_raw_str if isinstance(s, ShellQuoted) else s ) def __str__(self): raise RuntimeError( "One does not simply convert {0} to a string -- use path_join() " "or ShellQuoted.format() instead".format(repr(self)) ) def __repr__(self) -> str: return "{0}({1})".format(self.__class__.__name__, repr(self.do_not_use_raw_str)) def format(self, **kwargs) -> "ShellQuoted": return ShellQuoted( self.do_not_use_raw_str.format( **dict( (k, shell_quote(v).do_not_use_raw_str) for k, v in kwargs.items() ) ) ) def shell_quote(s) -> ShellQuoted: return ( s if isinstance(s, ShellQuoted) else ShellQuoted("'" + str(s).replace("'", "'\\''") + "'") ) def raw_shell(s: ShellQuoted): if isinstance(s, ShellQuoted): return s.do_not_use_raw_str raise RuntimeError("{0} should have been ShellQuoted".format(s)) def shell_join(delim, it) -> ShellQuoted: return ShellQuoted(delim.join(raw_shell(s) for s in it)) def path_join(*args) -> ShellQuoted: return ShellQuoted(os.path.join(*[raw_shell(shell_quote(s)) for s in args])) def shell_comment(c: ShellQuoted) -> ShellQuoted: return ShellQuoted("# {c}").format( c=ShellQuoted( (raw_shell(c) if isinstance(c, ShellQuoted) else c).replace("\n", "\n# ") ) )
true
true
f7061273d3c8137b689febcaac33374fabaa5045
2,250
py
Python
myproj/util/__init__.py
jbarguil/python-rest-server
d050d095555c9bad3e2e94b4e0c51075cc6ad3bc
[ "MIT" ]
null
null
null
myproj/util/__init__.py
jbarguil/python-rest-server
d050d095555c9bad3e2e94b4e0c51075cc6ad3bc
[ "MIT" ]
null
null
null
myproj/util/__init__.py
jbarguil/python-rest-server
d050d095555c9bad3e2e94b4e0c51075cc6ad3bc
[ "MIT" ]
null
null
null
"""Assortment of utilities. """ from datetime import datetime from hashlib import sha256, md5 import random import re import string import unicodedata epoch = datetime.utcfromtimestamp(0) def unix_time(dt=None): """Returns the UNIX time representation for a given date or current UTC.""" dt = dt or datetime.utcnow() return int((dt - epoch).total_seconds()) def unix_time_millis(dt=None): """Returns the UNIX time representation for a given date or current UTC.""" dt = dt or datetime.utcnow() return int((dt - epoch).total_seconds() * 1000) def from_iso_to_unix(iso_dt): """Converts from ISO format (e.g., "1984-06-02T19:05:00.000Z") to UNIX. """ if isinstance(iso_dt, int): return iso_dt # Already UNIX. try: dt = datetime.strptime(iso_dt, '%Y-%m-%dT%H:%M:%S.%fZ') except ValueError: dt = datetime.strptime(iso_dt, '%Y-%m-%dT%H:%M:%SZ') return unix_time(dt) def from_unix_to_iso(dt=None): """Returns the ISO representation for a given date or current UTC.""" dt = int(dt) if dt is not None else unix_time() return datetime.utcfromtimestamp(dt).strftime('%Y-%m-%dT%H:%M:%SZ') def datetime_from_unix_time(unix_dt): """Returns a datetime object.""" return datetime.utcfromtimestamp(unix_dt) def datetime_from_iso(iso_dt): """Returns a datetime object.""" return datetime.strptime(iso_dt, '%Y-%m-%dT%H:%M:%SZ') def password_hash(password, salt): if salt: return sha256(password + salt).hexdigest() else: return md5(password).hexdigest()[::-1] def random_string(size): return ''.join(random.SystemRandom().choice( string.ascii_letters + string.digits) for _ in range(size)) def remove_accents(input_str): """Replaces accented chars by their ascii equivalents.""" nfkd_form = unicodedata.normalize('NFKD', input_str) return u"".join([c for c in nfkd_form if not unicodedata.combining(c)]) def make_url_friendly(input_str): """Returns a URL-friedly version of input_str. Removes non-word chars, replaces accented ones and makes it lowercase. """ if input_str is None: return None return re.sub(r'[\W\\/_]+', '-', remove_accents(input_str)).lower()
27.439024
79
0.674667
from datetime import datetime from hashlib import sha256, md5 import random import re import string import unicodedata epoch = datetime.utcfromtimestamp(0) def unix_time(dt=None): dt = dt or datetime.utcnow() return int((dt - epoch).total_seconds()) def unix_time_millis(dt=None): dt = dt or datetime.utcnow() return int((dt - epoch).total_seconds() * 1000) def from_iso_to_unix(iso_dt): if isinstance(iso_dt, int): return iso_dt try: dt = datetime.strptime(iso_dt, '%Y-%m-%dT%H:%M:%S.%fZ') except ValueError: dt = datetime.strptime(iso_dt, '%Y-%m-%dT%H:%M:%SZ') return unix_time(dt) def from_unix_to_iso(dt=None): dt = int(dt) if dt is not None else unix_time() return datetime.utcfromtimestamp(dt).strftime('%Y-%m-%dT%H:%M:%SZ') def datetime_from_unix_time(unix_dt): return datetime.utcfromtimestamp(unix_dt) def datetime_from_iso(iso_dt): return datetime.strptime(iso_dt, '%Y-%m-%dT%H:%M:%SZ') def password_hash(password, salt): if salt: return sha256(password + salt).hexdigest() else: return md5(password).hexdigest()[::-1] def random_string(size): return ''.join(random.SystemRandom().choice( string.ascii_letters + string.digits) for _ in range(size)) def remove_accents(input_str): nfkd_form = unicodedata.normalize('NFKD', input_str) return u"".join([c for c in nfkd_form if not unicodedata.combining(c)]) def make_url_friendly(input_str): if input_str is None: return None return re.sub(r'[\W\\/_]+', '-', remove_accents(input_str)).lower()
true
true
f70612cc26f34cc6ad38194178f11e8869f22713
598
py
Python
src/deep_net/__init__.py
nadundesilva/deep-net
b6599740b8215a1f0b390d5256daa0b93201cc41
[ "Apache-2.0" ]
null
null
null
src/deep_net/__init__.py
nadundesilva/deep-net
b6599740b8215a1f0b390d5256daa0b93201cc41
[ "Apache-2.0" ]
null
null
null
src/deep_net/__init__.py
nadundesilva/deep-net
b6599740b8215a1f0b390d5256daa0b93201cc41
[ "Apache-2.0" ]
null
null
null
"""Copyright (c) 2021, Deep Net. 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. """ EPSILON = 1e-20
35.176471
72
0.777592
EPSILON = 1e-20
true
true
f706144295d2afcac1781e2d5a2e11deb21faf18
18,819
py
Python
log_complete/model_615.py
LoLab-VU/Bayesian_Inference_of_Network_Dynamics
54a5ef7e868be34289836bbbb024a2963c0c9c86
[ "MIT" ]
null
null
null
log_complete/model_615.py
LoLab-VU/Bayesian_Inference_of_Network_Dynamics
54a5ef7e868be34289836bbbb024a2963c0c9c86
[ "MIT" ]
null
null
null
log_complete/model_615.py
LoLab-VU/Bayesian_Inference_of_Network_Dynamics
54a5ef7e868be34289836bbbb024a2963c0c9c86
[ "MIT" ]
null
null
null
# exported from PySB model 'model' from pysb import Model, Monomer, Parameter, Expression, Compartment, Rule, Observable, Initial, MatchOnce, Annotation, ANY, WILD Model() Monomer('Ligand', ['Receptor']) Monomer('ParpU', ['C3A']) Monomer('C8A', ['BidU', 'C3pro']) Monomer('SmacM', ['BaxA']) Monomer('BaxM', ['BidM', 'BaxA']) Monomer('Apop', ['C3pro', 'Xiap']) Monomer('Fadd', ['Receptor', 'C8pro']) Monomer('SmacC', ['Xiap']) Monomer('ParpC') Monomer('Xiap', ['SmacC', 'Apop', 'C3A']) Monomer('C9') Monomer('C3ub') Monomer('C8pro', ['Fadd', 'C6A']) Monomer('C6A', ['C8pro']) Monomer('C3pro', ['Apop', 'C8A']) Monomer('CytoCM', ['BaxA']) Monomer('CytoCC') Monomer('BaxA', ['BaxM', 'BaxA_1', 'BaxA_2', 'SmacM', 'CytoCM']) Monomer('ApafI') Monomer('BidU', ['C8A']) Monomer('BidT') Monomer('C3A', ['Xiap', 'ParpU', 'C6pro']) Monomer('ApafA') Monomer('BidM', ['BaxM']) Monomer('Receptor', ['Ligand', 'Fadd']) Monomer('C6pro', ['C3A']) Parameter('bind_0_Ligand_binder_Receptor_binder_target_2kf', 1.0) Parameter('bind_0_Ligand_binder_Receptor_binder_target_1kr', 1.0) Parameter('bind_0_Receptor_binder_Fadd_binder_target_2kf', 1.0) Parameter('bind_0_Receptor_binder_Fadd_binder_target_1kr', 1.0) Parameter('substrate_binding_0_Fadd_catalyzer_C8pro_substrate_2kf', 1.0) Parameter('substrate_binding_0_Fadd_catalyzer_C8pro_substrate_1kr', 1.0) Parameter('catalytic_step_0_Fadd_catalyzer_C8pro_substrate_C8A_product_1kc', 1.0) Parameter('catalysis_0_C8A_catalyzer_BidU_substrate_BidT_product_2kf', 1.0) Parameter('catalysis_0_C8A_catalyzer_BidU_substrate_BidT_product_1kr', 1.0) Parameter('catalysis_1_C8A_catalyzer_BidU_substrate_BidT_product_1kc', 1.0) Parameter('conversion_0_CytoCC_subunit_d_ApafI_subunit_c_ApafA_complex_2kf', 1.0) Parameter('conversion_0_CytoCC_subunit_d_ApafI_subunit_c_ApafA_complex_1kr', 1.0) Parameter('inhibition_0_SmacC_inhibitor_Xiap_inh_target_2kf', 1.0) Parameter('inhibition_0_SmacC_inhibitor_Xiap_inh_target_1kr', 1.0) Parameter('conversion_0_C9_subunit_d_ApafA_subunit_c_Apop_complex_2kf', 1.0) Parameter('conversion_0_C9_subunit_d_ApafA_subunit_c_Apop_complex_1kr', 1.0) Parameter('catalysis_0_Apop_catalyzer_C3pro_substrate_C3A_product_2kf', 1.0) Parameter('catalysis_0_Apop_catalyzer_C3pro_substrate_C3A_product_1kr', 1.0) Parameter('catalysis_1_Apop_catalyzer_C3pro_substrate_C3A_product_1kc', 1.0) Parameter('inhibition_0_Xiap_inhibitor_Apop_inh_target_2kf', 1.0) Parameter('inhibition_0_Xiap_inhibitor_Apop_inh_target_1kr', 1.0) Parameter('catalysis_0_Xiap_catalyzer_C3A_substrate_C3ub_product_2kf', 1.0) Parameter('catalysis_0_Xiap_catalyzer_C3A_substrate_C3ub_product_1kr', 1.0) Parameter('catalysis_1_Xiap_catalyzer_C3A_substrate_C3ub_product_1kc', 1.0) Parameter('catalysis_0_C3A_catalyzer_ParpU_substrate_ParpC_product_2kf', 1.0) Parameter('catalysis_0_C3A_catalyzer_ParpU_substrate_ParpC_product_1kr', 1.0) Parameter('catalysis_1_C3A_catalyzer_ParpU_substrate_ParpC_product_1kc', 1.0) Parameter('equilibration_0_BidT_equil_a_BidM_equil_b_1kf', 1.0) Parameter('equilibration_0_BidT_equil_a_BidM_equil_b_1kr', 1.0) Parameter('catalysis_0_BidM_catalyzer_BaxM_substrate_BaxA_product_2kf', 1.0) Parameter('catalysis_0_BidM_catalyzer_BaxM_substrate_BaxA_product_1kr', 1.0) Parameter('catalysis_1_BidM_catalyzer_BaxM_substrate_BaxA_product_1kc', 1.0) Parameter('self_catalyze_0_BaxA_self_catalyzer_BaxM_self_substrate_2kf', 1.0) Parameter('self_catalyze_0_BaxA_self_catalyzer_BaxM_self_substrate_1kr', 1.0) Parameter('self_catalyze_1_BaxA_self_catalyzer_BaxM_self_substrate_1kc', 1.0) Parameter('pore_formation_0_BaxA_pore_2kf', 1.0) Parameter('pore_formation_0_BaxA_pore_1kr', 1.0) Parameter('pore_formation_1_BaxA_pore_2kf', 1.0) Parameter('pore_formation_1_BaxA_pore_1kr', 1.0) Parameter('pore_formation_2_BaxA_pore_2kf', 1.0) Parameter('pore_formation_2_BaxA_pore_1kr', 1.0) Parameter('transport_0_BaxA_pore_SmacM_cargo_M_SmacC_cargo_C_2kf', 1.0) Parameter('transport_0_BaxA_pore_SmacM_cargo_M_SmacC_cargo_C_1kr', 1.0) Parameter('transport_1_BaxA_pore_SmacM_cargo_M_SmacC_cargo_C_1kc', 1.0) Parameter('transport_0_BaxA_pore_CytoCM_cargo_M_CytoCC_cargo_C_2kf', 1.0) Parameter('transport_0_BaxA_pore_CytoCM_cargo_M_CytoCC_cargo_C_1kr', 1.0) Parameter('transport_1_BaxA_pore_CytoCM_cargo_M_CytoCC_cargo_C_1kc', 1.0) Parameter('catalysis_0_C8A_catalyzer_C3pro_substrate_C3A_product_2kf', 1.0) Parameter('catalysis_0_C8A_catalyzer_C3pro_substrate_C3A_product_1kr', 1.0) Parameter('catalysis_1_C8A_catalyzer_C3pro_substrate_C3A_product_1kc', 1.0) Parameter('catalysis_0_C3A_catalyzer_C6pro_substrate_C6A_product_2kf', 1.0) Parameter('catalysis_0_C3A_catalyzer_C6pro_substrate_C6A_product_1kr', 1.0) Parameter('catalysis_1_C3A_catalyzer_C6pro_substrate_C6A_product_1kc', 1.0) Parameter('catalysis_0_C6A_catalyzer_C8pro_substrate_C8A_product_2kf', 1.0) Parameter('catalysis_0_C6A_catalyzer_C8pro_substrate_C8A_product_1kr', 1.0) Parameter('catalysis_1_C6A_catalyzer_C8pro_substrate_C8A_product_1kc', 1.0) Parameter('Ligand_0', 1000.0) Parameter('ParpU_0', 1000000.0) Parameter('C8A_0', 0.0) Parameter('SmacM_0', 100000.0) Parameter('BaxM_0', 40000.0) Parameter('Apop_0', 0.0) Parameter('Fadd_0', 130000.0) Parameter('SmacC_0', 0.0) Parameter('ParpC_0', 0.0) Parameter('Xiap_0', 153750.0) Parameter('C9_0', 100000.0) Parameter('C3ub_0', 0.0) Parameter('C8pro_0', 130000.0) Parameter('C6A_0', 0.0) Parameter('C3pro_0', 21000.0) Parameter('CytoCM_0', 500000.0) Parameter('CytoCC_0', 0.0) Parameter('BaxA_0', 0.0) Parameter('ApafI_0', 100000.0) Parameter('BidU_0', 171000.0) Parameter('BidT_0', 0.0) Parameter('C3A_0', 0.0) Parameter('ApafA_0', 0.0) Parameter('BidM_0', 0.0) Parameter('Receptor_0', 100.0) Parameter('C6pro_0', 100.0) Observable('Ligand_obs', Ligand()) Observable('ParpU_obs', ParpU()) Observable('C8A_obs', C8A()) Observable('SmacM_obs', SmacM()) Observable('BaxM_obs', BaxM()) Observable('Apop_obs', Apop()) Observable('Fadd_obs', Fadd()) Observable('SmacC_obs', SmacC()) Observable('ParpC_obs', ParpC()) Observable('Xiap_obs', Xiap()) Observable('C9_obs', C9()) Observable('C3ub_obs', C3ub()) Observable('C8pro_obs', C8pro()) Observable('C6A_obs', C6A()) Observable('C3pro_obs', C3pro()) Observable('CytoCM_obs', CytoCM()) Observable('CytoCC_obs', CytoCC()) Observable('BaxA_obs', BaxA()) Observable('ApafI_obs', ApafI()) Observable('BidU_obs', BidU()) Observable('BidT_obs', BidT()) Observable('C3A_obs', C3A()) Observable('ApafA_obs', ApafA()) Observable('BidM_obs', BidM()) Observable('Receptor_obs', Receptor()) Observable('C6pro_obs', C6pro()) Rule('bind_0_Ligand_binder_Receptor_binder_target', Ligand(Receptor=None) + Receptor(Ligand=None, Fadd=None) | Ligand(Receptor=1) % Receptor(Ligand=1, Fadd=None), bind_0_Ligand_binder_Receptor_binder_target_2kf, bind_0_Ligand_binder_Receptor_binder_target_1kr) Rule('bind_0_Receptor_binder_Fadd_binder_target', Receptor(Ligand=ANY, Fadd=None) + Fadd(Receptor=None, C8pro=None) | Receptor(Ligand=ANY, Fadd=1) % Fadd(Receptor=1, C8pro=None), bind_0_Receptor_binder_Fadd_binder_target_2kf, bind_0_Receptor_binder_Fadd_binder_target_1kr) Rule('substrate_binding_0_Fadd_catalyzer_C8pro_substrate', Fadd(Receptor=ANY, C8pro=None) + C8pro(Fadd=None, C6A=None) | Fadd(Receptor=ANY, C8pro=1) % C8pro(Fadd=1, C6A=None), substrate_binding_0_Fadd_catalyzer_C8pro_substrate_2kf, substrate_binding_0_Fadd_catalyzer_C8pro_substrate_1kr) Rule('catalytic_step_0_Fadd_catalyzer_C8pro_substrate_C8A_product', Fadd(Receptor=ANY, C8pro=1) % C8pro(Fadd=1, C6A=None) >> Fadd(Receptor=ANY, C8pro=None) + C8A(BidU=None, C3pro=None), catalytic_step_0_Fadd_catalyzer_C8pro_substrate_C8A_product_1kc) Rule('catalysis_0_C8A_catalyzer_BidU_substrate_BidT_product', C8A(BidU=None, C3pro=None) + BidU(C8A=None) | C8A(BidU=1, C3pro=None) % BidU(C8A=1), catalysis_0_C8A_catalyzer_BidU_substrate_BidT_product_2kf, catalysis_0_C8A_catalyzer_BidU_substrate_BidT_product_1kr) Rule('catalysis_1_C8A_catalyzer_BidU_substrate_BidT_product', C8A(BidU=1, C3pro=None) % BidU(C8A=1) >> C8A(BidU=None, C3pro=None) + BidT(), catalysis_1_C8A_catalyzer_BidU_substrate_BidT_product_1kc) Rule('conversion_0_CytoCC_subunit_d_ApafI_subunit_c_ApafA_complex', ApafI() + CytoCC() | ApafA(), conversion_0_CytoCC_subunit_d_ApafI_subunit_c_ApafA_complex_2kf, conversion_0_CytoCC_subunit_d_ApafI_subunit_c_ApafA_complex_1kr) Rule('inhibition_0_SmacC_inhibitor_Xiap_inh_target', SmacC(Xiap=None) + Xiap(SmacC=None, Apop=None, C3A=None) | SmacC(Xiap=1) % Xiap(SmacC=1, Apop=None, C3A=None), inhibition_0_SmacC_inhibitor_Xiap_inh_target_2kf, inhibition_0_SmacC_inhibitor_Xiap_inh_target_1kr) Rule('conversion_0_C9_subunit_d_ApafA_subunit_c_Apop_complex', ApafA() + C9() | Apop(C3pro=None, Xiap=None), conversion_0_C9_subunit_d_ApafA_subunit_c_Apop_complex_2kf, conversion_0_C9_subunit_d_ApafA_subunit_c_Apop_complex_1kr) Rule('catalysis_0_Apop_catalyzer_C3pro_substrate_C3A_product', Apop(C3pro=None, Xiap=None) + C3pro(Apop=None, C8A=None) | Apop(C3pro=1, Xiap=None) % C3pro(Apop=1, C8A=None), catalysis_0_Apop_catalyzer_C3pro_substrate_C3A_product_2kf, catalysis_0_Apop_catalyzer_C3pro_substrate_C3A_product_1kr) Rule('catalysis_1_Apop_catalyzer_C3pro_substrate_C3A_product', Apop(C3pro=1, Xiap=None) % C3pro(Apop=1, C8A=None) >> Apop(C3pro=None, Xiap=None) + C3A(Xiap=None, ParpU=None, C6pro=None), catalysis_1_Apop_catalyzer_C3pro_substrate_C3A_product_1kc) Rule('inhibition_0_Xiap_inhibitor_Apop_inh_target', Xiap(SmacC=None, Apop=None, C3A=None) + Apop(C3pro=None, Xiap=None) | Xiap(SmacC=None, Apop=1, C3A=None) % Apop(C3pro=None, Xiap=1), inhibition_0_Xiap_inhibitor_Apop_inh_target_2kf, inhibition_0_Xiap_inhibitor_Apop_inh_target_1kr) Rule('catalysis_0_Xiap_catalyzer_C3A_substrate_C3ub_product', Xiap(SmacC=None, Apop=None, C3A=None) + C3A(Xiap=None, ParpU=None, C6pro=None) | Xiap(SmacC=None, Apop=None, C3A=1) % C3A(Xiap=1, ParpU=None, C6pro=None), catalysis_0_Xiap_catalyzer_C3A_substrate_C3ub_product_2kf, catalysis_0_Xiap_catalyzer_C3A_substrate_C3ub_product_1kr) Rule('catalysis_1_Xiap_catalyzer_C3A_substrate_C3ub_product', Xiap(SmacC=None, Apop=None, C3A=1) % C3A(Xiap=1, ParpU=None, C6pro=None) >> Xiap(SmacC=None, Apop=None, C3A=None) + C3ub(), catalysis_1_Xiap_catalyzer_C3A_substrate_C3ub_product_1kc) Rule('catalysis_0_C3A_catalyzer_ParpU_substrate_ParpC_product', C3A(Xiap=None, ParpU=None, C6pro=None) + ParpU(C3A=None) | C3A(Xiap=None, ParpU=1, C6pro=None) % ParpU(C3A=1), catalysis_0_C3A_catalyzer_ParpU_substrate_ParpC_product_2kf, catalysis_0_C3A_catalyzer_ParpU_substrate_ParpC_product_1kr) Rule('catalysis_1_C3A_catalyzer_ParpU_substrate_ParpC_product', C3A(Xiap=None, ParpU=1, C6pro=None) % ParpU(C3A=1) >> C3A(Xiap=None, ParpU=None, C6pro=None) + ParpC(), catalysis_1_C3A_catalyzer_ParpU_substrate_ParpC_product_1kc) Rule('equilibration_0_BidT_equil_a_BidM_equil_b', BidT() | BidM(BaxM=None), equilibration_0_BidT_equil_a_BidM_equil_b_1kf, equilibration_0_BidT_equil_a_BidM_equil_b_1kr) Rule('catalysis_0_BidM_catalyzer_BaxM_substrate_BaxA_product', BidM(BaxM=None) + BaxM(BidM=None, BaxA=None) | BidM(BaxM=1) % BaxM(BidM=1, BaxA=None), catalysis_0_BidM_catalyzer_BaxM_substrate_BaxA_product_2kf, catalysis_0_BidM_catalyzer_BaxM_substrate_BaxA_product_1kr) Rule('catalysis_1_BidM_catalyzer_BaxM_substrate_BaxA_product', BidM(BaxM=1) % BaxM(BidM=1, BaxA=None) >> BidM(BaxM=None) + BaxA(BaxM=None, BaxA_1=None, BaxA_2=None, SmacM=None, CytoCM=None), catalysis_1_BidM_catalyzer_BaxM_substrate_BaxA_product_1kc) Rule('self_catalyze_0_BaxA_self_catalyzer_BaxM_self_substrate', BaxA(BaxM=None, BaxA_1=None, BaxA_2=None, SmacM=None, CytoCM=None) + BaxM(BidM=None, BaxA=None) | BaxA(BaxM=1, BaxA_1=None, BaxA_2=None, SmacM=None, CytoCM=None) % BaxM(BidM=None, BaxA=1), self_catalyze_0_BaxA_self_catalyzer_BaxM_self_substrate_2kf, self_catalyze_0_BaxA_self_catalyzer_BaxM_self_substrate_1kr) Rule('self_catalyze_1_BaxA_self_catalyzer_BaxM_self_substrate', BaxA(BaxM=1, BaxA_1=None, BaxA_2=None, SmacM=None, CytoCM=None) % BaxM(BidM=None, BaxA=1) >> BaxA(BaxM=None, BaxA_1=None, BaxA_2=None, SmacM=None, CytoCM=None) + BaxA(BaxM=None, BaxA_1=None, BaxA_2=None, SmacM=None, CytoCM=None), self_catalyze_1_BaxA_self_catalyzer_BaxM_self_substrate_1kc) Rule('pore_formation_0_BaxA_pore', BaxA(BaxM=None, BaxA_1=None, BaxA_2=None, SmacM=None, CytoCM=None) + BaxA(BaxM=None, BaxA_1=None, BaxA_2=None, SmacM=None, CytoCM=None) | BaxA(BaxM=None, BaxA_1=None, BaxA_2=1, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=1, BaxA_2=None, SmacM=None, CytoCM=None), pore_formation_0_BaxA_pore_2kf, pore_formation_0_BaxA_pore_1kr) Rule('pore_formation_1_BaxA_pore', BaxA(BaxM=None, BaxA_1=None, BaxA_2=None, SmacM=None, CytoCM=None) + BaxA(BaxM=None, BaxA_1=None, BaxA_2=1, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=1, BaxA_2=None, SmacM=None, CytoCM=None) | BaxA(BaxM=None, BaxA_1=3, BaxA_2=1, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=1, BaxA_2=2, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=2, BaxA_2=3, SmacM=None, CytoCM=None), pore_formation_1_BaxA_pore_2kf, pore_formation_1_BaxA_pore_1kr) Rule('pore_formation_2_BaxA_pore', BaxA(BaxM=None, BaxA_1=None, BaxA_2=None, SmacM=None, CytoCM=None) + BaxA(BaxM=None, BaxA_1=3, BaxA_2=1, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=1, BaxA_2=2, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=2, BaxA_2=3, SmacM=None, CytoCM=None) | BaxA(BaxM=None, BaxA_1=4, BaxA_2=1, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=1, BaxA_2=2, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=2, BaxA_2=3, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=3, BaxA_2=4, SmacM=None, CytoCM=None), pore_formation_2_BaxA_pore_2kf, pore_formation_2_BaxA_pore_1kr) Rule('transport_0_BaxA_pore_SmacM_cargo_M_SmacC_cargo_C', BaxA(BaxM=None, BaxA_1=4, BaxA_2=1, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=1, BaxA_2=2, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=2, BaxA_2=3, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=3, BaxA_2=4, SmacM=None, CytoCM=None) + SmacM(BaxA=None) | BaxA(BaxM=None, BaxA_1=4, BaxA_2=1, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=1, BaxA_2=2, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=2, BaxA_2=3, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=3, BaxA_2=4, SmacM=5, CytoCM=None) % SmacM(BaxA=5), transport_0_BaxA_pore_SmacM_cargo_M_SmacC_cargo_C_2kf, transport_0_BaxA_pore_SmacM_cargo_M_SmacC_cargo_C_1kr) Rule('transport_1_BaxA_pore_SmacM_cargo_M_SmacC_cargo_C', BaxA(BaxM=None, BaxA_1=4, BaxA_2=1, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=1, BaxA_2=2, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=2, BaxA_2=3, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=3, BaxA_2=4, SmacM=5, CytoCM=None) % SmacM(BaxA=5) >> BaxA(BaxM=None, BaxA_1=4, BaxA_2=1, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=1, BaxA_2=2, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=2, BaxA_2=3, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=3, BaxA_2=4, SmacM=None, CytoCM=None) + SmacC(Xiap=None), transport_1_BaxA_pore_SmacM_cargo_M_SmacC_cargo_C_1kc) Rule('transport_0_BaxA_pore_CytoCM_cargo_M_CytoCC_cargo_C', BaxA(BaxM=None, BaxA_1=4, BaxA_2=1, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=1, BaxA_2=2, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=2, BaxA_2=3, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=3, BaxA_2=4, SmacM=None, CytoCM=None) + CytoCM(BaxA=None) | BaxA(BaxM=None, BaxA_1=4, BaxA_2=1, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=1, BaxA_2=2, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=2, BaxA_2=3, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=3, BaxA_2=4, SmacM=None, CytoCM=5) % CytoCM(BaxA=5), transport_0_BaxA_pore_CytoCM_cargo_M_CytoCC_cargo_C_2kf, transport_0_BaxA_pore_CytoCM_cargo_M_CytoCC_cargo_C_1kr) Rule('transport_1_BaxA_pore_CytoCM_cargo_M_CytoCC_cargo_C', BaxA(BaxM=None, BaxA_1=4, BaxA_2=1, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=1, BaxA_2=2, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=2, BaxA_2=3, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=3, BaxA_2=4, SmacM=None, CytoCM=5) % CytoCM(BaxA=5) >> BaxA(BaxM=None, BaxA_1=4, BaxA_2=1, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=1, BaxA_2=2, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=2, BaxA_2=3, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=3, BaxA_2=4, SmacM=None, CytoCM=None) + CytoCC(), transport_1_BaxA_pore_CytoCM_cargo_M_CytoCC_cargo_C_1kc) Rule('catalysis_0_C8A_catalyzer_C3pro_substrate_C3A_product', C8A(BidU=None, C3pro=None) + C3pro(Apop=None, C8A=None) | C8A(BidU=None, C3pro=1) % C3pro(Apop=None, C8A=1), catalysis_0_C8A_catalyzer_C3pro_substrate_C3A_product_2kf, catalysis_0_C8A_catalyzer_C3pro_substrate_C3A_product_1kr) Rule('catalysis_1_C8A_catalyzer_C3pro_substrate_C3A_product', C8A(BidU=None, C3pro=1) % C3pro(Apop=None, C8A=1) >> C8A(BidU=None, C3pro=None) + C3A(Xiap=None, ParpU=None, C6pro=None), catalysis_1_C8A_catalyzer_C3pro_substrate_C3A_product_1kc) Rule('catalysis_0_C3A_catalyzer_C6pro_substrate_C6A_product', C3A(Xiap=None, ParpU=None, C6pro=None) + C6pro(C3A=None) | C3A(Xiap=None, ParpU=None, C6pro=1) % C6pro(C3A=1), catalysis_0_C3A_catalyzer_C6pro_substrate_C6A_product_2kf, catalysis_0_C3A_catalyzer_C6pro_substrate_C6A_product_1kr) Rule('catalysis_1_C3A_catalyzer_C6pro_substrate_C6A_product', C3A(Xiap=None, ParpU=None, C6pro=1) % C6pro(C3A=1) >> C3A(Xiap=None, ParpU=None, C6pro=None) + C6A(C8pro=None), catalysis_1_C3A_catalyzer_C6pro_substrate_C6A_product_1kc) Rule('catalysis_0_C6A_catalyzer_C8pro_substrate_C8A_product', C6A(C8pro=None) + C8pro(Fadd=None, C6A=None) | C6A(C8pro=1) % C8pro(Fadd=None, C6A=1), catalysis_0_C6A_catalyzer_C8pro_substrate_C8A_product_2kf, catalysis_0_C6A_catalyzer_C8pro_substrate_C8A_product_1kr) Rule('catalysis_1_C6A_catalyzer_C8pro_substrate_C8A_product', C6A(C8pro=1) % C8pro(Fadd=None, C6A=1) >> C6A(C8pro=None) + C8A(BidU=None, C3pro=None), catalysis_1_C6A_catalyzer_C8pro_substrate_C8A_product_1kc) Initial(Ligand(Receptor=None), Ligand_0) Initial(ParpU(C3A=None), ParpU_0) Initial(C8A(BidU=None, C3pro=None), C8A_0) Initial(SmacM(BaxA=None), SmacM_0) Initial(BaxM(BidM=None, BaxA=None), BaxM_0) Initial(Apop(C3pro=None, Xiap=None), Apop_0) Initial(Fadd(Receptor=None, C8pro=None), Fadd_0) Initial(SmacC(Xiap=None), SmacC_0) Initial(ParpC(), ParpC_0) Initial(Xiap(SmacC=None, Apop=None, C3A=None), Xiap_0) Initial(C9(), C9_0) Initial(C3ub(), C3ub_0) Initial(C8pro(Fadd=None, C6A=None), C8pro_0) Initial(C6A(C8pro=None), C6A_0) Initial(C3pro(Apop=None, C8A=None), C3pro_0) Initial(CytoCM(BaxA=None), CytoCM_0) Initial(CytoCC(), CytoCC_0) Initial(BaxA(BaxM=None, BaxA_1=None, BaxA_2=None, SmacM=None, CytoCM=None), BaxA_0) Initial(ApafI(), ApafI_0) Initial(BidU(C8A=None), BidU_0) Initial(BidT(), BidT_0) Initial(C3A(Xiap=None, ParpU=None, C6pro=None), C3A_0) Initial(ApafA(), ApafA_0) Initial(BidM(BaxM=None), BidM_0) Initial(Receptor(Ligand=None, Fadd=None), Receptor_0) Initial(C6pro(C3A=None), C6pro_0)
91.354369
710
0.806525
from pysb import Model, Monomer, Parameter, Expression, Compartment, Rule, Observable, Initial, MatchOnce, Annotation, ANY, WILD Model() Monomer('Ligand', ['Receptor']) Monomer('ParpU', ['C3A']) Monomer('C8A', ['BidU', 'C3pro']) Monomer('SmacM', ['BaxA']) Monomer('BaxM', ['BidM', 'BaxA']) Monomer('Apop', ['C3pro', 'Xiap']) Monomer('Fadd', ['Receptor', 'C8pro']) Monomer('SmacC', ['Xiap']) Monomer('ParpC') Monomer('Xiap', ['SmacC', 'Apop', 'C3A']) Monomer('C9') Monomer('C3ub') Monomer('C8pro', ['Fadd', 'C6A']) Monomer('C6A', ['C8pro']) Monomer('C3pro', ['Apop', 'C8A']) Monomer('CytoCM', ['BaxA']) Monomer('CytoCC') Monomer('BaxA', ['BaxM', 'BaxA_1', 'BaxA_2', 'SmacM', 'CytoCM']) Monomer('ApafI') Monomer('BidU', ['C8A']) Monomer('BidT') Monomer('C3A', ['Xiap', 'ParpU', 'C6pro']) Monomer('ApafA') Monomer('BidM', ['BaxM']) Monomer('Receptor', ['Ligand', 'Fadd']) Monomer('C6pro', ['C3A']) Parameter('bind_0_Ligand_binder_Receptor_binder_target_2kf', 1.0) Parameter('bind_0_Ligand_binder_Receptor_binder_target_1kr', 1.0) Parameter('bind_0_Receptor_binder_Fadd_binder_target_2kf', 1.0) Parameter('bind_0_Receptor_binder_Fadd_binder_target_1kr', 1.0) Parameter('substrate_binding_0_Fadd_catalyzer_C8pro_substrate_2kf', 1.0) Parameter('substrate_binding_0_Fadd_catalyzer_C8pro_substrate_1kr', 1.0) Parameter('catalytic_step_0_Fadd_catalyzer_C8pro_substrate_C8A_product_1kc', 1.0) Parameter('catalysis_0_C8A_catalyzer_BidU_substrate_BidT_product_2kf', 1.0) Parameter('catalysis_0_C8A_catalyzer_BidU_substrate_BidT_product_1kr', 1.0) Parameter('catalysis_1_C8A_catalyzer_BidU_substrate_BidT_product_1kc', 1.0) Parameter('conversion_0_CytoCC_subunit_d_ApafI_subunit_c_ApafA_complex_2kf', 1.0) Parameter('conversion_0_CytoCC_subunit_d_ApafI_subunit_c_ApafA_complex_1kr', 1.0) Parameter('inhibition_0_SmacC_inhibitor_Xiap_inh_target_2kf', 1.0) Parameter('inhibition_0_SmacC_inhibitor_Xiap_inh_target_1kr', 1.0) Parameter('conversion_0_C9_subunit_d_ApafA_subunit_c_Apop_complex_2kf', 1.0) Parameter('conversion_0_C9_subunit_d_ApafA_subunit_c_Apop_complex_1kr', 1.0) Parameter('catalysis_0_Apop_catalyzer_C3pro_substrate_C3A_product_2kf', 1.0) Parameter('catalysis_0_Apop_catalyzer_C3pro_substrate_C3A_product_1kr', 1.0) Parameter('catalysis_1_Apop_catalyzer_C3pro_substrate_C3A_product_1kc', 1.0) Parameter('inhibition_0_Xiap_inhibitor_Apop_inh_target_2kf', 1.0) Parameter('inhibition_0_Xiap_inhibitor_Apop_inh_target_1kr', 1.0) Parameter('catalysis_0_Xiap_catalyzer_C3A_substrate_C3ub_product_2kf', 1.0) Parameter('catalysis_0_Xiap_catalyzer_C3A_substrate_C3ub_product_1kr', 1.0) Parameter('catalysis_1_Xiap_catalyzer_C3A_substrate_C3ub_product_1kc', 1.0) Parameter('catalysis_0_C3A_catalyzer_ParpU_substrate_ParpC_product_2kf', 1.0) Parameter('catalysis_0_C3A_catalyzer_ParpU_substrate_ParpC_product_1kr', 1.0) Parameter('catalysis_1_C3A_catalyzer_ParpU_substrate_ParpC_product_1kc', 1.0) Parameter('equilibration_0_BidT_equil_a_BidM_equil_b_1kf', 1.0) Parameter('equilibration_0_BidT_equil_a_BidM_equil_b_1kr', 1.0) Parameter('catalysis_0_BidM_catalyzer_BaxM_substrate_BaxA_product_2kf', 1.0) Parameter('catalysis_0_BidM_catalyzer_BaxM_substrate_BaxA_product_1kr', 1.0) Parameter('catalysis_1_BidM_catalyzer_BaxM_substrate_BaxA_product_1kc', 1.0) Parameter('self_catalyze_0_BaxA_self_catalyzer_BaxM_self_substrate_2kf', 1.0) Parameter('self_catalyze_0_BaxA_self_catalyzer_BaxM_self_substrate_1kr', 1.0) Parameter('self_catalyze_1_BaxA_self_catalyzer_BaxM_self_substrate_1kc', 1.0) Parameter('pore_formation_0_BaxA_pore_2kf', 1.0) Parameter('pore_formation_0_BaxA_pore_1kr', 1.0) Parameter('pore_formation_1_BaxA_pore_2kf', 1.0) Parameter('pore_formation_1_BaxA_pore_1kr', 1.0) Parameter('pore_formation_2_BaxA_pore_2kf', 1.0) Parameter('pore_formation_2_BaxA_pore_1kr', 1.0) Parameter('transport_0_BaxA_pore_SmacM_cargo_M_SmacC_cargo_C_2kf', 1.0) Parameter('transport_0_BaxA_pore_SmacM_cargo_M_SmacC_cargo_C_1kr', 1.0) Parameter('transport_1_BaxA_pore_SmacM_cargo_M_SmacC_cargo_C_1kc', 1.0) Parameter('transport_0_BaxA_pore_CytoCM_cargo_M_CytoCC_cargo_C_2kf', 1.0) Parameter('transport_0_BaxA_pore_CytoCM_cargo_M_CytoCC_cargo_C_1kr', 1.0) Parameter('transport_1_BaxA_pore_CytoCM_cargo_M_CytoCC_cargo_C_1kc', 1.0) Parameter('catalysis_0_C8A_catalyzer_C3pro_substrate_C3A_product_2kf', 1.0) Parameter('catalysis_0_C8A_catalyzer_C3pro_substrate_C3A_product_1kr', 1.0) Parameter('catalysis_1_C8A_catalyzer_C3pro_substrate_C3A_product_1kc', 1.0) Parameter('catalysis_0_C3A_catalyzer_C6pro_substrate_C6A_product_2kf', 1.0) Parameter('catalysis_0_C3A_catalyzer_C6pro_substrate_C6A_product_1kr', 1.0) Parameter('catalysis_1_C3A_catalyzer_C6pro_substrate_C6A_product_1kc', 1.0) Parameter('catalysis_0_C6A_catalyzer_C8pro_substrate_C8A_product_2kf', 1.0) Parameter('catalysis_0_C6A_catalyzer_C8pro_substrate_C8A_product_1kr', 1.0) Parameter('catalysis_1_C6A_catalyzer_C8pro_substrate_C8A_product_1kc', 1.0) Parameter('Ligand_0', 1000.0) Parameter('ParpU_0', 1000000.0) Parameter('C8A_0', 0.0) Parameter('SmacM_0', 100000.0) Parameter('BaxM_0', 40000.0) Parameter('Apop_0', 0.0) Parameter('Fadd_0', 130000.0) Parameter('SmacC_0', 0.0) Parameter('ParpC_0', 0.0) Parameter('Xiap_0', 153750.0) Parameter('C9_0', 100000.0) Parameter('C3ub_0', 0.0) Parameter('C8pro_0', 130000.0) Parameter('C6A_0', 0.0) Parameter('C3pro_0', 21000.0) Parameter('CytoCM_0', 500000.0) Parameter('CytoCC_0', 0.0) Parameter('BaxA_0', 0.0) Parameter('ApafI_0', 100000.0) Parameter('BidU_0', 171000.0) Parameter('BidT_0', 0.0) Parameter('C3A_0', 0.0) Parameter('ApafA_0', 0.0) Parameter('BidM_0', 0.0) Parameter('Receptor_0', 100.0) Parameter('C6pro_0', 100.0) Observable('Ligand_obs', Ligand()) Observable('ParpU_obs', ParpU()) Observable('C8A_obs', C8A()) Observable('SmacM_obs', SmacM()) Observable('BaxM_obs', BaxM()) Observable('Apop_obs', Apop()) Observable('Fadd_obs', Fadd()) Observable('SmacC_obs', SmacC()) Observable('ParpC_obs', ParpC()) Observable('Xiap_obs', Xiap()) Observable('C9_obs', C9()) Observable('C3ub_obs', C3ub()) Observable('C8pro_obs', C8pro()) Observable('C6A_obs', C6A()) Observable('C3pro_obs', C3pro()) Observable('CytoCM_obs', CytoCM()) Observable('CytoCC_obs', CytoCC()) Observable('BaxA_obs', BaxA()) Observable('ApafI_obs', ApafI()) Observable('BidU_obs', BidU()) Observable('BidT_obs', BidT()) Observable('C3A_obs', C3A()) Observable('ApafA_obs', ApafA()) Observable('BidM_obs', BidM()) Observable('Receptor_obs', Receptor()) Observable('C6pro_obs', C6pro()) Rule('bind_0_Ligand_binder_Receptor_binder_target', Ligand(Receptor=None) + Receptor(Ligand=None, Fadd=None) | Ligand(Receptor=1) % Receptor(Ligand=1, Fadd=None), bind_0_Ligand_binder_Receptor_binder_target_2kf, bind_0_Ligand_binder_Receptor_binder_target_1kr) Rule('bind_0_Receptor_binder_Fadd_binder_target', Receptor(Ligand=ANY, Fadd=None) + Fadd(Receptor=None, C8pro=None) | Receptor(Ligand=ANY, Fadd=1) % Fadd(Receptor=1, C8pro=None), bind_0_Receptor_binder_Fadd_binder_target_2kf, bind_0_Receptor_binder_Fadd_binder_target_1kr) Rule('substrate_binding_0_Fadd_catalyzer_C8pro_substrate', Fadd(Receptor=ANY, C8pro=None) + C8pro(Fadd=None, C6A=None) | Fadd(Receptor=ANY, C8pro=1) % C8pro(Fadd=1, C6A=None), substrate_binding_0_Fadd_catalyzer_C8pro_substrate_2kf, substrate_binding_0_Fadd_catalyzer_C8pro_substrate_1kr) Rule('catalytic_step_0_Fadd_catalyzer_C8pro_substrate_C8A_product', Fadd(Receptor=ANY, C8pro=1) % C8pro(Fadd=1, C6A=None) >> Fadd(Receptor=ANY, C8pro=None) + C8A(BidU=None, C3pro=None), catalytic_step_0_Fadd_catalyzer_C8pro_substrate_C8A_product_1kc) Rule('catalysis_0_C8A_catalyzer_BidU_substrate_BidT_product', C8A(BidU=None, C3pro=None) + BidU(C8A=None) | C8A(BidU=1, C3pro=None) % BidU(C8A=1), catalysis_0_C8A_catalyzer_BidU_substrate_BidT_product_2kf, catalysis_0_C8A_catalyzer_BidU_substrate_BidT_product_1kr) Rule('catalysis_1_C8A_catalyzer_BidU_substrate_BidT_product', C8A(BidU=1, C3pro=None) % BidU(C8A=1) >> C8A(BidU=None, C3pro=None) + BidT(), catalysis_1_C8A_catalyzer_BidU_substrate_BidT_product_1kc) Rule('conversion_0_CytoCC_subunit_d_ApafI_subunit_c_ApafA_complex', ApafI() + CytoCC() | ApafA(), conversion_0_CytoCC_subunit_d_ApafI_subunit_c_ApafA_complex_2kf, conversion_0_CytoCC_subunit_d_ApafI_subunit_c_ApafA_complex_1kr) Rule('inhibition_0_SmacC_inhibitor_Xiap_inh_target', SmacC(Xiap=None) + Xiap(SmacC=None, Apop=None, C3A=None) | SmacC(Xiap=1) % Xiap(SmacC=1, Apop=None, C3A=None), inhibition_0_SmacC_inhibitor_Xiap_inh_target_2kf, inhibition_0_SmacC_inhibitor_Xiap_inh_target_1kr) Rule('conversion_0_C9_subunit_d_ApafA_subunit_c_Apop_complex', ApafA() + C9() | Apop(C3pro=None, Xiap=None), conversion_0_C9_subunit_d_ApafA_subunit_c_Apop_complex_2kf, conversion_0_C9_subunit_d_ApafA_subunit_c_Apop_complex_1kr) Rule('catalysis_0_Apop_catalyzer_C3pro_substrate_C3A_product', Apop(C3pro=None, Xiap=None) + C3pro(Apop=None, C8A=None) | Apop(C3pro=1, Xiap=None) % C3pro(Apop=1, C8A=None), catalysis_0_Apop_catalyzer_C3pro_substrate_C3A_product_2kf, catalysis_0_Apop_catalyzer_C3pro_substrate_C3A_product_1kr) Rule('catalysis_1_Apop_catalyzer_C3pro_substrate_C3A_product', Apop(C3pro=1, Xiap=None) % C3pro(Apop=1, C8A=None) >> Apop(C3pro=None, Xiap=None) + C3A(Xiap=None, ParpU=None, C6pro=None), catalysis_1_Apop_catalyzer_C3pro_substrate_C3A_product_1kc) Rule('inhibition_0_Xiap_inhibitor_Apop_inh_target', Xiap(SmacC=None, Apop=None, C3A=None) + Apop(C3pro=None, Xiap=None) | Xiap(SmacC=None, Apop=1, C3A=None) % Apop(C3pro=None, Xiap=1), inhibition_0_Xiap_inhibitor_Apop_inh_target_2kf, inhibition_0_Xiap_inhibitor_Apop_inh_target_1kr) Rule('catalysis_0_Xiap_catalyzer_C3A_substrate_C3ub_product', Xiap(SmacC=None, Apop=None, C3A=None) + C3A(Xiap=None, ParpU=None, C6pro=None) | Xiap(SmacC=None, Apop=None, C3A=1) % C3A(Xiap=1, ParpU=None, C6pro=None), catalysis_0_Xiap_catalyzer_C3A_substrate_C3ub_product_2kf, catalysis_0_Xiap_catalyzer_C3A_substrate_C3ub_product_1kr) Rule('catalysis_1_Xiap_catalyzer_C3A_substrate_C3ub_product', Xiap(SmacC=None, Apop=None, C3A=1) % C3A(Xiap=1, ParpU=None, C6pro=None) >> Xiap(SmacC=None, Apop=None, C3A=None) + C3ub(), catalysis_1_Xiap_catalyzer_C3A_substrate_C3ub_product_1kc) Rule('catalysis_0_C3A_catalyzer_ParpU_substrate_ParpC_product', C3A(Xiap=None, ParpU=None, C6pro=None) + ParpU(C3A=None) | C3A(Xiap=None, ParpU=1, C6pro=None) % ParpU(C3A=1), catalysis_0_C3A_catalyzer_ParpU_substrate_ParpC_product_2kf, catalysis_0_C3A_catalyzer_ParpU_substrate_ParpC_product_1kr) Rule('catalysis_1_C3A_catalyzer_ParpU_substrate_ParpC_product', C3A(Xiap=None, ParpU=1, C6pro=None) % ParpU(C3A=1) >> C3A(Xiap=None, ParpU=None, C6pro=None) + ParpC(), catalysis_1_C3A_catalyzer_ParpU_substrate_ParpC_product_1kc) Rule('equilibration_0_BidT_equil_a_BidM_equil_b', BidT() | BidM(BaxM=None), equilibration_0_BidT_equil_a_BidM_equil_b_1kf, equilibration_0_BidT_equil_a_BidM_equil_b_1kr) Rule('catalysis_0_BidM_catalyzer_BaxM_substrate_BaxA_product', BidM(BaxM=None) + BaxM(BidM=None, BaxA=None) | BidM(BaxM=1) % BaxM(BidM=1, BaxA=None), catalysis_0_BidM_catalyzer_BaxM_substrate_BaxA_product_2kf, catalysis_0_BidM_catalyzer_BaxM_substrate_BaxA_product_1kr) Rule('catalysis_1_BidM_catalyzer_BaxM_substrate_BaxA_product', BidM(BaxM=1) % BaxM(BidM=1, BaxA=None) >> BidM(BaxM=None) + BaxA(BaxM=None, BaxA_1=None, BaxA_2=None, SmacM=None, CytoCM=None), catalysis_1_BidM_catalyzer_BaxM_substrate_BaxA_product_1kc) Rule('self_catalyze_0_BaxA_self_catalyzer_BaxM_self_substrate', BaxA(BaxM=None, BaxA_1=None, BaxA_2=None, SmacM=None, CytoCM=None) + BaxM(BidM=None, BaxA=None) | BaxA(BaxM=1, BaxA_1=None, BaxA_2=None, SmacM=None, CytoCM=None) % BaxM(BidM=None, BaxA=1), self_catalyze_0_BaxA_self_catalyzer_BaxM_self_substrate_2kf, self_catalyze_0_BaxA_self_catalyzer_BaxM_self_substrate_1kr) Rule('self_catalyze_1_BaxA_self_catalyzer_BaxM_self_substrate', BaxA(BaxM=1, BaxA_1=None, BaxA_2=None, SmacM=None, CytoCM=None) % BaxM(BidM=None, BaxA=1) >> BaxA(BaxM=None, BaxA_1=None, BaxA_2=None, SmacM=None, CytoCM=None) + BaxA(BaxM=None, BaxA_1=None, BaxA_2=None, SmacM=None, CytoCM=None), self_catalyze_1_BaxA_self_catalyzer_BaxM_self_substrate_1kc) Rule('pore_formation_0_BaxA_pore', BaxA(BaxM=None, BaxA_1=None, BaxA_2=None, SmacM=None, CytoCM=None) + BaxA(BaxM=None, BaxA_1=None, BaxA_2=None, SmacM=None, CytoCM=None) | BaxA(BaxM=None, BaxA_1=None, BaxA_2=1, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=1, BaxA_2=None, SmacM=None, CytoCM=None), pore_formation_0_BaxA_pore_2kf, pore_formation_0_BaxA_pore_1kr) Rule('pore_formation_1_BaxA_pore', BaxA(BaxM=None, BaxA_1=None, BaxA_2=None, SmacM=None, CytoCM=None) + BaxA(BaxM=None, BaxA_1=None, BaxA_2=1, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=1, BaxA_2=None, SmacM=None, CytoCM=None) | BaxA(BaxM=None, BaxA_1=3, BaxA_2=1, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=1, BaxA_2=2, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=2, BaxA_2=3, SmacM=None, CytoCM=None), pore_formation_1_BaxA_pore_2kf, pore_formation_1_BaxA_pore_1kr) Rule('pore_formation_2_BaxA_pore', BaxA(BaxM=None, BaxA_1=None, BaxA_2=None, SmacM=None, CytoCM=None) + BaxA(BaxM=None, BaxA_1=3, BaxA_2=1, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=1, BaxA_2=2, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=2, BaxA_2=3, SmacM=None, CytoCM=None) | BaxA(BaxM=None, BaxA_1=4, BaxA_2=1, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=1, BaxA_2=2, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=2, BaxA_2=3, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=3, BaxA_2=4, SmacM=None, CytoCM=None), pore_formation_2_BaxA_pore_2kf, pore_formation_2_BaxA_pore_1kr) Rule('transport_0_BaxA_pore_SmacM_cargo_M_SmacC_cargo_C', BaxA(BaxM=None, BaxA_1=4, BaxA_2=1, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=1, BaxA_2=2, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=2, BaxA_2=3, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=3, BaxA_2=4, SmacM=None, CytoCM=None) + SmacM(BaxA=None) | BaxA(BaxM=None, BaxA_1=4, BaxA_2=1, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=1, BaxA_2=2, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=2, BaxA_2=3, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=3, BaxA_2=4, SmacM=5, CytoCM=None) % SmacM(BaxA=5), transport_0_BaxA_pore_SmacM_cargo_M_SmacC_cargo_C_2kf, transport_0_BaxA_pore_SmacM_cargo_M_SmacC_cargo_C_1kr) Rule('transport_1_BaxA_pore_SmacM_cargo_M_SmacC_cargo_C', BaxA(BaxM=None, BaxA_1=4, BaxA_2=1, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=1, BaxA_2=2, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=2, BaxA_2=3, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=3, BaxA_2=4, SmacM=5, CytoCM=None) % SmacM(BaxA=5) >> BaxA(BaxM=None, BaxA_1=4, BaxA_2=1, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=1, BaxA_2=2, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=2, BaxA_2=3, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=3, BaxA_2=4, SmacM=None, CytoCM=None) + SmacC(Xiap=None), transport_1_BaxA_pore_SmacM_cargo_M_SmacC_cargo_C_1kc) Rule('transport_0_BaxA_pore_CytoCM_cargo_M_CytoCC_cargo_C', BaxA(BaxM=None, BaxA_1=4, BaxA_2=1, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=1, BaxA_2=2, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=2, BaxA_2=3, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=3, BaxA_2=4, SmacM=None, CytoCM=None) + CytoCM(BaxA=None) | BaxA(BaxM=None, BaxA_1=4, BaxA_2=1, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=1, BaxA_2=2, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=2, BaxA_2=3, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=3, BaxA_2=4, SmacM=None, CytoCM=5) % CytoCM(BaxA=5), transport_0_BaxA_pore_CytoCM_cargo_M_CytoCC_cargo_C_2kf, transport_0_BaxA_pore_CytoCM_cargo_M_CytoCC_cargo_C_1kr) Rule('transport_1_BaxA_pore_CytoCM_cargo_M_CytoCC_cargo_C', BaxA(BaxM=None, BaxA_1=4, BaxA_2=1, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=1, BaxA_2=2, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=2, BaxA_2=3, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=3, BaxA_2=4, SmacM=None, CytoCM=5) % CytoCM(BaxA=5) >> BaxA(BaxM=None, BaxA_1=4, BaxA_2=1, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=1, BaxA_2=2, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=2, BaxA_2=3, SmacM=None, CytoCM=None) % BaxA(BaxM=None, BaxA_1=3, BaxA_2=4, SmacM=None, CytoCM=None) + CytoCC(), transport_1_BaxA_pore_CytoCM_cargo_M_CytoCC_cargo_C_1kc) Rule('catalysis_0_C8A_catalyzer_C3pro_substrate_C3A_product', C8A(BidU=None, C3pro=None) + C3pro(Apop=None, C8A=None) | C8A(BidU=None, C3pro=1) % C3pro(Apop=None, C8A=1), catalysis_0_C8A_catalyzer_C3pro_substrate_C3A_product_2kf, catalysis_0_C8A_catalyzer_C3pro_substrate_C3A_product_1kr) Rule('catalysis_1_C8A_catalyzer_C3pro_substrate_C3A_product', C8A(BidU=None, C3pro=1) % C3pro(Apop=None, C8A=1) >> C8A(BidU=None, C3pro=None) + C3A(Xiap=None, ParpU=None, C6pro=None), catalysis_1_C8A_catalyzer_C3pro_substrate_C3A_product_1kc) Rule('catalysis_0_C3A_catalyzer_C6pro_substrate_C6A_product', C3A(Xiap=None, ParpU=None, C6pro=None) + C6pro(C3A=None) | C3A(Xiap=None, ParpU=None, C6pro=1) % C6pro(C3A=1), catalysis_0_C3A_catalyzer_C6pro_substrate_C6A_product_2kf, catalysis_0_C3A_catalyzer_C6pro_substrate_C6A_product_1kr) Rule('catalysis_1_C3A_catalyzer_C6pro_substrate_C6A_product', C3A(Xiap=None, ParpU=None, C6pro=1) % C6pro(C3A=1) >> C3A(Xiap=None, ParpU=None, C6pro=None) + C6A(C8pro=None), catalysis_1_C3A_catalyzer_C6pro_substrate_C6A_product_1kc) Rule('catalysis_0_C6A_catalyzer_C8pro_substrate_C8A_product', C6A(C8pro=None) + C8pro(Fadd=None, C6A=None) | C6A(C8pro=1) % C8pro(Fadd=None, C6A=1), catalysis_0_C6A_catalyzer_C8pro_substrate_C8A_product_2kf, catalysis_0_C6A_catalyzer_C8pro_substrate_C8A_product_1kr) Rule('catalysis_1_C6A_catalyzer_C8pro_substrate_C8A_product', C6A(C8pro=1) % C8pro(Fadd=None, C6A=1) >> C6A(C8pro=None) + C8A(BidU=None, C3pro=None), catalysis_1_C6A_catalyzer_C8pro_substrate_C8A_product_1kc) Initial(Ligand(Receptor=None), Ligand_0) Initial(ParpU(C3A=None), ParpU_0) Initial(C8A(BidU=None, C3pro=None), C8A_0) Initial(SmacM(BaxA=None), SmacM_0) Initial(BaxM(BidM=None, BaxA=None), BaxM_0) Initial(Apop(C3pro=None, Xiap=None), Apop_0) Initial(Fadd(Receptor=None, C8pro=None), Fadd_0) Initial(SmacC(Xiap=None), SmacC_0) Initial(ParpC(), ParpC_0) Initial(Xiap(SmacC=None, Apop=None, C3A=None), Xiap_0) Initial(C9(), C9_0) Initial(C3ub(), C3ub_0) Initial(C8pro(Fadd=None, C6A=None), C8pro_0) Initial(C6A(C8pro=None), C6A_0) Initial(C3pro(Apop=None, C8A=None), C3pro_0) Initial(CytoCM(BaxA=None), CytoCM_0) Initial(CytoCC(), CytoCC_0) Initial(BaxA(BaxM=None, BaxA_1=None, BaxA_2=None, SmacM=None, CytoCM=None), BaxA_0) Initial(ApafI(), ApafI_0) Initial(BidU(C8A=None), BidU_0) Initial(BidT(), BidT_0) Initial(C3A(Xiap=None, ParpU=None, C6pro=None), C3A_0) Initial(ApafA(), ApafA_0) Initial(BidM(BaxM=None), BidM_0) Initial(Receptor(Ligand=None, Fadd=None), Receptor_0) Initial(C6pro(C3A=None), C6pro_0)
true
true
f7061506fa2cce695f12947d1688e170169e08f4
8,490
py
Python
src/magql/filter.py
ruoyangW-dev/magql
a7b6a217495a785afcac12c350cb234ff60f265e
[ "BSD-3-Clause" ]
25
2020-02-13T21:25:08.000Z
2021-11-29T07:19:16.000Z
src/magql/filter.py
ruoyangW-dev/magql
a7b6a217495a785afcac12c350cb234ff60f265e
[ "BSD-3-Clause" ]
40
2020-02-14T00:54:06.000Z
2021-07-28T18:45:32.000Z
src/magql/filter.py
ruoyangW-dev/magql
a7b6a217495a785afcac12c350cb234ff60f265e
[ "BSD-3-Clause" ]
2
2021-06-07T21:50:46.000Z
2021-12-21T14:47:31.000Z
from __future__ import annotations import typing as t from functools import singledispatch from inflection import underscore from sqlalchemy import Date from sqlalchemy import DateTime from sqlalchemy import Text from sqlalchemy import Time from sqlalchemy import Unicode from sqlalchemy import UnicodeText from sqlalchemy.orm import RelationshipProperty from sqlalchemy.types import Boolean from sqlalchemy.types import Float from sqlalchemy.types import Integer from sqlalchemy.types import Numeric from sqlalchemy.types import String from sqlalchemy.types import VARCHAR from sqlalchemy_utils import EmailType from sqlalchemy_utils import get_mapper from sqlalchemy_utils import JSONType from sqlalchemy_utils import PhoneNumberType from sqlalchemy_utils import URLType from sqlalchemy_utils.types import ChoiceType from .definitions import MagqlEnumType from .definitions import MagqlInputField from .definitions import MagqlInputObjectType StringFilter = MagqlInputObjectType( "StringFilter", { "operator": MagqlInputField( MagqlEnumType( "StringOperator", { "INCLUDES": "INCLUDES", "EQUALS": "EQUALS", "EXISTS": "EXISTS", "DOESNOTEXIST": "DOESNOTEXIST", }, ) ), "value": MagqlInputField("String"), }, ) DateFilter = MagqlInputObjectType( "DateFilter", { "operator": MagqlInputField( MagqlEnumType( "DateOperator", {"BEFORE": "BEFORE", "ON": "ON", "AFTER": "AFTER"} ) ), "value": MagqlInputField("String"), }, ) IntFilter = MagqlInputObjectType( "IntFilter", { "operator": MagqlInputField( MagqlEnumType( "IntOperator", { "lt": "lt", "lte": "lte", "eq": "eq", "neq": "neq", "gt": "gt", "gte": "gte", }, ) ), "value": MagqlInputField("Int"), }, ) FloatFilter = MagqlInputObjectType( "FloatFilter", { "operator": MagqlInputField( MagqlEnumType( "FloatOperator", { "lt": "lt", "lte": "lte", "eq": "eq", "neq": "neq", "gt": "gt", "gte": "gte", }, ) ), "value": MagqlInputField("Float"), }, ) RelFilter = MagqlInputObjectType( "RelFilter", { "operator": MagqlInputField( MagqlEnumType("RelOperator", {"INCLUDES": "INCLUDES"}) ), "value": MagqlInputField("Int"), }, ) BooleanFilter = MagqlInputObjectType( "BooleanFilter", { "operator": MagqlInputField( MagqlEnumType( "BooleanOperator", {"EQUALS": "EQUALS", "NOTEQUALS": "NOTEQUALS"} ) ), "value": MagqlInputField("Boolean"), }, ) EnumOperator = MagqlEnumType("EnumOperator", {"INCLUDES": "INCLUDES"}) def EnumFilter(base_type: t.Any) -> MagqlInputObjectType: name = base_type.name + "Filter" input_ = { "operator": MagqlInputField(EnumOperator), "value": MagqlInputField(base_type), } return MagqlInputObjectType(name, input_) @singledispatch def get_filter_comparator(type: t.Any) -> t.Any: raise TypeError(f"No comparator registered for {type.__class__.__name__!r}.") @get_filter_comparator.register(RelationshipProperty) def _get_relationship_comparator(rel: RelationshipProperty) -> t.Optional[t.Callable]: direction = rel.direction.name if "TOONE" in direction: def condition(filter_value: t.Any, filter_operator: str, field: t.Any) -> t.Any: if filter_operator == "INCLUDES": return field == filter_value return None return condition elif "TOMANY" in direction: def condition(filter_value: t.Any, filter_operator: str, field: t.Any) -> t.Any: if filter_operator == "INCLUDES": return field.any(field.contains(filter_value)) return None return condition return None @get_filter_comparator.register(DateTime) @get_filter_comparator.register(Date) def _get_date_comparator(_: t.Union[DateTime, Date]) -> t.Callable: def condition( filter_value: t.Union[DateTime, Date], filter_operator: str, field: t.Union[DateTime, Date], ) -> t.Any: if filter_operator == "BEFORE": return field < filter_value elif filter_operator == "ON": return field == filter_value elif filter_operator == "After": return field > filter_value return None return condition @get_filter_comparator.register(JSONType) @get_filter_comparator.register(Text) @get_filter_comparator.register(UnicodeText) @get_filter_comparator.register(Unicode) @get_filter_comparator.register(URLType) @get_filter_comparator.register(PhoneNumberType) @get_filter_comparator.register(EmailType) @get_filter_comparator.register(Time) @get_filter_comparator.register(String) @get_filter_comparator.register(VARCHAR) def _get_string_comparator(_: t.Any) -> t.Callable: def condition(filter_value: t.Any, filter_operator: str, field: t.Any) -> t.Any: if filter_operator == "INCLUDES": return field.like(f"%{filter_value}%") elif filter_operator == "EQUALS": return field == filter_value elif filter_operator == "EXISTS": return field.like("%") elif filter_operator == "DOESNOTEXIST": return field.is_(None) return condition @get_filter_comparator.register(Float) @get_filter_comparator.register(Numeric) @get_filter_comparator.register(Integer) def _get_number_comparator(_: t.Any) -> t.Callable: def condition(filter_value: t.Any, filter_operator: str, field: t.Any) -> t.Any: if filter_operator == "lt": return field < filter_value elif filter_operator == "lte": return field <= filter_value elif filter_operator == "eq": return field == filter_value elif filter_operator == "neq": return field != filter_value elif filter_operator == "gt": return field > filter_value elif filter_operator == "gte": return field >= filter_value return condition @get_filter_comparator.register(Boolean) def _get_boolean_comparator(_: t.Any) -> t.Callable: def condition(filter_value: t.Any, filter_operator: str, field: t.Any) -> t.Any: if filter_operator == "EQUALS": return field == filter_value elif filter_operator == "NOTEQUALS": return field != filter_value return condition @get_filter_comparator.register(ChoiceType) def _get_choice_comparator(_: t.Any) -> t.Callable: def condition(filter_value: t.Any, filter_operator: str, field: t.Any) -> t.Any: if filter_operator == "INCLUDES": return field == filter_value return condition def generate_filters(table: t.Any, info: t.Any, *args: t.Any, **kwargs: t.Any) -> t.Any: sqla_filters = [] if "filter" in kwargs and kwargs["filter"] is not None: mapper = get_mapper(table) gql_filters = kwargs["filter"] for filter_name, gql_filter in gql_filters.items(): gql_filter_value = gql_filter["value"] filter_name = underscore(filter_name) if filter_name in table.c: filter_type = table.c[filter_name].type elif filter_name in mapper.relationships: rel = mapper.relationships[filter_name] rel_mapper = get_mapper(rel.target) gql_filter_value = ( info.context.query(rel_mapper.class_) .filter_by(id=gql_filter_value) .one() ) filter_type = rel else: raise KeyError(filter_name) sql_filter = get_filter_comparator(filter_type)( gql_filter_value, gql_filter["operator"], getattr(mapper.class_, filter_name), ) sqla_filters.append(sql_filter) return sqla_filters
30.430108
88
0.610718
from __future__ import annotations import typing as t from functools import singledispatch from inflection import underscore from sqlalchemy import Date from sqlalchemy import DateTime from sqlalchemy import Text from sqlalchemy import Time from sqlalchemy import Unicode from sqlalchemy import UnicodeText from sqlalchemy.orm import RelationshipProperty from sqlalchemy.types import Boolean from sqlalchemy.types import Float from sqlalchemy.types import Integer from sqlalchemy.types import Numeric from sqlalchemy.types import String from sqlalchemy.types import VARCHAR from sqlalchemy_utils import EmailType from sqlalchemy_utils import get_mapper from sqlalchemy_utils import JSONType from sqlalchemy_utils import PhoneNumberType from sqlalchemy_utils import URLType from sqlalchemy_utils.types import ChoiceType from .definitions import MagqlEnumType from .definitions import MagqlInputField from .definitions import MagqlInputObjectType StringFilter = MagqlInputObjectType( "StringFilter", { "operator": MagqlInputField( MagqlEnumType( "StringOperator", { "INCLUDES": "INCLUDES", "EQUALS": "EQUALS", "EXISTS": "EXISTS", "DOESNOTEXIST": "DOESNOTEXIST", }, ) ), "value": MagqlInputField("String"), }, ) DateFilter = MagqlInputObjectType( "DateFilter", { "operator": MagqlInputField( MagqlEnumType( "DateOperator", {"BEFORE": "BEFORE", "ON": "ON", "AFTER": "AFTER"} ) ), "value": MagqlInputField("String"), }, ) IntFilter = MagqlInputObjectType( "IntFilter", { "operator": MagqlInputField( MagqlEnumType( "IntOperator", { "lt": "lt", "lte": "lte", "eq": "eq", "neq": "neq", "gt": "gt", "gte": "gte", }, ) ), "value": MagqlInputField("Int"), }, ) FloatFilter = MagqlInputObjectType( "FloatFilter", { "operator": MagqlInputField( MagqlEnumType( "FloatOperator", { "lt": "lt", "lte": "lte", "eq": "eq", "neq": "neq", "gt": "gt", "gte": "gte", }, ) ), "value": MagqlInputField("Float"), }, ) RelFilter = MagqlInputObjectType( "RelFilter", { "operator": MagqlInputField( MagqlEnumType("RelOperator", {"INCLUDES": "INCLUDES"}) ), "value": MagqlInputField("Int"), }, ) BooleanFilter = MagqlInputObjectType( "BooleanFilter", { "operator": MagqlInputField( MagqlEnumType( "BooleanOperator", {"EQUALS": "EQUALS", "NOTEQUALS": "NOTEQUALS"} ) ), "value": MagqlInputField("Boolean"), }, ) EnumOperator = MagqlEnumType("EnumOperator", {"INCLUDES": "INCLUDES"}) def EnumFilter(base_type: t.Any) -> MagqlInputObjectType: name = base_type.name + "Filter" input_ = { "operator": MagqlInputField(EnumOperator), "value": MagqlInputField(base_type), } return MagqlInputObjectType(name, input_) @singledispatch def get_filter_comparator(type: t.Any) -> t.Any: raise TypeError(f"No comparator registered for {type.__class__.__name__!r}.") @get_filter_comparator.register(RelationshipProperty) def _get_relationship_comparator(rel: RelationshipProperty) -> t.Optional[t.Callable]: direction = rel.direction.name if "TOONE" in direction: def condition(filter_value: t.Any, filter_operator: str, field: t.Any) -> t.Any: if filter_operator == "INCLUDES": return field == filter_value return None return condition elif "TOMANY" in direction: def condition(filter_value: t.Any, filter_operator: str, field: t.Any) -> t.Any: if filter_operator == "INCLUDES": return field.any(field.contains(filter_value)) return None return condition return None @get_filter_comparator.register(DateTime) @get_filter_comparator.register(Date) def _get_date_comparator(_: t.Union[DateTime, Date]) -> t.Callable: def condition( filter_value: t.Union[DateTime, Date], filter_operator: str, field: t.Union[DateTime, Date], ) -> t.Any: if filter_operator == "BEFORE": return field < filter_value elif filter_operator == "ON": return field == filter_value elif filter_operator == "After": return field > filter_value return None return condition @get_filter_comparator.register(JSONType) @get_filter_comparator.register(Text) @get_filter_comparator.register(UnicodeText) @get_filter_comparator.register(Unicode) @get_filter_comparator.register(URLType) @get_filter_comparator.register(PhoneNumberType) @get_filter_comparator.register(EmailType) @get_filter_comparator.register(Time) @get_filter_comparator.register(String) @get_filter_comparator.register(VARCHAR) def _get_string_comparator(_: t.Any) -> t.Callable: def condition(filter_value: t.Any, filter_operator: str, field: t.Any) -> t.Any: if filter_operator == "INCLUDES": return field.like(f"%{filter_value}%") elif filter_operator == "EQUALS": return field == filter_value elif filter_operator == "EXISTS": return field.like("%") elif filter_operator == "DOESNOTEXIST": return field.is_(None) return condition @get_filter_comparator.register(Float) @get_filter_comparator.register(Numeric) @get_filter_comparator.register(Integer) def _get_number_comparator(_: t.Any) -> t.Callable: def condition(filter_value: t.Any, filter_operator: str, field: t.Any) -> t.Any: if filter_operator == "lt": return field < filter_value elif filter_operator == "lte": return field <= filter_value elif filter_operator == "eq": return field == filter_value elif filter_operator == "neq": return field != filter_value elif filter_operator == "gt": return field > filter_value elif filter_operator == "gte": return field >= filter_value return condition @get_filter_comparator.register(Boolean) def _get_boolean_comparator(_: t.Any) -> t.Callable: def condition(filter_value: t.Any, filter_operator: str, field: t.Any) -> t.Any: if filter_operator == "EQUALS": return field == filter_value elif filter_operator == "NOTEQUALS": return field != filter_value return condition @get_filter_comparator.register(ChoiceType) def _get_choice_comparator(_: t.Any) -> t.Callable: def condition(filter_value: t.Any, filter_operator: str, field: t.Any) -> t.Any: if filter_operator == "INCLUDES": return field == filter_value return condition def generate_filters(table: t.Any, info: t.Any, *args: t.Any, **kwargs: t.Any) -> t.Any: sqla_filters = [] if "filter" in kwargs and kwargs["filter"] is not None: mapper = get_mapper(table) gql_filters = kwargs["filter"] for filter_name, gql_filter in gql_filters.items(): gql_filter_value = gql_filter["value"] filter_name = underscore(filter_name) if filter_name in table.c: filter_type = table.c[filter_name].type elif filter_name in mapper.relationships: rel = mapper.relationships[filter_name] rel_mapper = get_mapper(rel.target) gql_filter_value = ( info.context.query(rel_mapper.class_) .filter_by(id=gql_filter_value) .one() ) filter_type = rel else: raise KeyError(filter_name) sql_filter = get_filter_comparator(filter_type)( gql_filter_value, gql_filter["operator"], getattr(mapper.class_, filter_name), ) sqla_filters.append(sql_filter) return sqla_filters
true
true
f70616730f443ffb7030c4b8c2a0120588c0536f
61
py
Python
app/__init__.py
michalvi/heroes3calc
7c55bd13d01b6e3e5ab9f3c236ac4e71ca58f82d
[ "MIT" ]
1
2020-09-04T18:57:52.000Z
2020-09-04T18:57:52.000Z
app/__init__.py
michalvi/heroes3calc
7c55bd13d01b6e3e5ab9f3c236ac4e71ca58f82d
[ "MIT" ]
3
2021-06-08T22:24:03.000Z
2022-03-12T00:47:53.000Z
app/__init__.py
michalvi/heroes3calc
7c55bd13d01b6e3e5ab9f3c236ac4e71ca58f82d
[ "MIT" ]
null
null
null
from .__main__ import create_app application = create_app()
15.25
32
0.803279
from .__main__ import create_app application = create_app()
true
true
f70616ce0a8a228cd073571ebccf6743a53717ef
11,351
py
Python
mindspore/communication/_comm_helper.py
GuoSuiming/mindspore
48afc4cfa53d970c0b20eedfb46e039db2a133d5
[ "Apache-2.0" ]
55
2020-12-17T10:26:06.000Z
2022-03-28T07:18:26.000Z
mindspore/communication/_comm_helper.py
forwhat461/mindspore
59a277756eb4faad9ac9afcc7fd526e8277d4994
[ "Apache-2.0" ]
1
2020-12-29T06:46:38.000Z
2020-12-29T06:46:38.000Z
mindspore/communication/_comm_helper.py
forwhat461/mindspore
59a277756eb4faad9ac9afcc7fd526e8277d4994
[ "Apache-2.0" ]
14
2021-01-29T02:39:47.000Z
2022-03-23T05:00:26.000Z
# Copyright 2020 Huawei Technologies Co., Ltd # # 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. # ============================================================================ """comm_helper""" from mindspore.parallel._ps_context import _is_role_pserver, _is_role_sched from ._hccl_management import load_lib as hccl_load_lib _HCCL_AVAILABLE = False _NCCL_AVAILABLE = False try: import mindspore._ms_mpi as mpi _NCCL_AVAILABLE = True except ImportError: _NCCL_AVAILABLE = False try: hccl_load_lib() _HCCL_AVAILABLE = True except RuntimeError: _HCCL_AVAILABLE = False if _HCCL_AVAILABLE: from . import _hccl_management as hccl else: try: import hccl_test.manage.api as hccl _HCCL_AVAILABLE = True except ImportError: _HCCL_AVAILABLE = False HCCL_WORLD_COMM_GROUP = "hccl_world_group" NCCL_WORLD_COMM_GROUP = "nccl_world_group" class Backend: """ Class for available backends. Note: The backends' value should be string, e.g., "hccl". If backend is set to Backend.UNDEFINED, it will be seen as invaliad. Args: name (str): The name of backend. Raises: TypeError: If name is not a string. ValueError: If backend is invalid. Examples: >>> Backend("abc") >>> hccl = Backend("hccl") """ UNDEFINED = "undefined" HCCL = "hccl" NCCL = "nccl" def __new__(cls, name): """Create instance object of Backend.""" if not isinstance(name, str): raise TypeError("Backend name must be a string, but got {}".format(type(name))) value = getattr(Backend, name.upper(), Backend.UNDEFINED) if value == Backend.UNDEFINED: raise ValueError("Invalid backend: '{}'".format(name)) return value def is_hccl_available(): """ Check hccl api is available. Returns: Boolean. Return whether hccl is available or not. """ return _HCCL_AVAILABLE def is_nccl_available(): """ Check nccl api is available. Returns: Boolean. Return whether nccl is available or not. """ return _NCCL_AVAILABLE def check_parameter_available(func): """ Check parameter is available. If not available, raise Error. Args: func (Function): The function to be run. Raises: RuntimeError. Returns: Wrapper. If not available, raise Error. """ def wrapper(*args, **kargs): if _is_role_pserver() or _is_role_sched(): return func(*args, **kargs) group = None if "group" in kargs.keys(): group = kargs.get("group") if group is not None and not isinstance(group, str): raise TypeError("Group should be str or None, " "but got group {}".format(type(group))) if "backend" in kargs.keys(): backend = kargs.get("backend") if backend is Backend.HCCL and not is_hccl_available(): raise RuntimeError("Distributed Communication doesn't have HCCL built in") if backend is Backend.NCCL and not is_nccl_available(): raise RuntimeError("Distributed Communication doesn't have NCCL built in") if group is None: if backend is Backend.HCCL: group = HCCL_WORLD_COMM_GROUP elif backend is Backend.NCCL: group = NCCL_WORLD_COMM_GROUP return func(*args, **kargs) return wrapper @check_parameter_available def _get_rank_helper(group, backend): """ The Helper to do get_rank_id. Args: group (str): The communication group. backend (str): The backend, like "hccl". Raises: ValueError: If backend is invalid. Returns: Integer. The local rank id of the calling process. """ rank_id = None if _is_role_pserver() or _is_role_sched(): rank_id = 0 return rank_id if backend == Backend.HCCL: if group == HCCL_WORLD_COMM_GROUP: rank_id = hccl.get_rank_id() else: rank_id = hccl.get_rank_id(group) elif backend == Backend.NCCL: rank_id = mpi.get_rank_id(group) else: raise ValueError("Invalid backend: '{}'".format(backend)) return rank_id @check_parameter_available def _get_local_rank_helper(group, backend): """ The Helper to do get_local_rank_id. Args: group (str): The communication group. backend (str): The backend, like "hccl". Raises: ValueError: If backend is invalid. Returns: Integer. The local rank id of the calling process. """ rank_id = None if backend == Backend.HCCL: if group == HCCL_WORLD_COMM_GROUP: rank_id = hccl.get_local_rank_id() else: rank_id = hccl.get_local_rank_id(group) elif backend == Backend.NCCL: raise RuntimeError("Nccl doesn't support get_local_rank_id now.") else: raise ValueError("Invalid backend: '{}'".format(backend)) return rank_id @check_parameter_available def _get_size_helper(group, backend): """ The Helper to do get_rank_size. Args: group (str): The communication group. backend (str): The backend, like "hccl". Raises: ValueError: If backend is invalid. Returns: Integer. The rank size of specified group. """ size = None if _is_role_pserver() or _is_role_sched(): size = 1 return size if backend == Backend.HCCL: if group == HCCL_WORLD_COMM_GROUP: size = hccl.get_rank_size() else: size = hccl.get_rank_size(group) elif backend == Backend.NCCL: size = mpi.get_rank_size(group) else: raise ValueError("Invalid backend: '{}'".format(backend)) return size @check_parameter_available def _get_local_size_helper(group, backend): """ The Helper to do get_local_rank_size. Args: group (str): The communication group. backend (str): The backend, like "hccl". Raises: ValueError: If backend is invalid. Returns: Integer. The local rank size where the calling process is being within specified group. """ size = None if backend == Backend.HCCL: if group == HCCL_WORLD_COMM_GROUP: size = hccl.get_local_rank_size() else: size = hccl.get_local_rank_size(group) elif backend == Backend.NCCL: raise RuntimeError("Nccl doesn't support get_local_rank_size now.") else: raise ValueError("Invalid backend: '{}'".format(backend)) return size @check_parameter_available def _get_world_rank_from_group_rank_helper(group, group_rank_id, backend): """ The Helper to do get_world_rank_from_group_rank. Args: group (str): The user communication group. group_rank_id (int): A rank id in user communication group. backend (str): The backend, like "hccl". Raises: TypeError: If group_rank_id is not int. ValueError: If group is "hccl_world_group" or backend is invalid. Returns: Integer. A rank id in world communication group. """ world_rank_id = None if not isinstance(group_rank_id, int): raise TypeError("group_rank_id should be int, but got type {}".format(type(group_rank_id))) if backend == Backend.HCCL: if group == HCCL_WORLD_COMM_GROUP: raise ValueError("Group cannot be 'hccl_world_group'. ") world_rank_id = hccl.get_world_rank_from_group_rank(group, group_rank_id) elif backend == Backend.NCCL: raise RuntimeError("Nccl doesn't support get_world_rank_from_group_rank now.") else: raise ValueError("Invalid backend: '{}'".format(backend)) return world_rank_id @check_parameter_available def _get_group_rank_from_world_rank_helper(world_rank_id, group, backend): """ The Helper to do get_group_rank_from_world_rank. Args: world_rank_id (int): A rank id in world communication group. group (str): The user communication group. backend (str): The backend, like "hccl". Raises: TypeError: If world_rank_id is not int. ValueError: If group is 'hccl_world_group' or backend is invalid. Returns: Integer. A rank id in user communication group. """ group_rank_id = None if not isinstance(world_rank_id, int): raise TypeError("world_rank_id should be int, but got type {}".format(type(world_rank_id))) if backend == Backend.HCCL: if group == HCCL_WORLD_COMM_GROUP: raise ValueError("Group cannot be 'hccl_world_group'. ") group_rank_id = hccl.get_group_rank_from_world_rank(world_rank_id, group) elif backend == Backend.NCCL: raise RuntimeError("Nccl doesn't support get_group_rank_from_world_rank now.") else: raise ValueError("Invalid backend: '{}'".format(backend)) return group_rank_id @check_parameter_available def _create_group_helper(group, rank_ids, backend): """ The Helper to do create_group. Args: group (str): The communication group. rank_ids (list): Rank ids in the group. backend (str): The backend, like "hccl". Raises: TypeError: If rank_ids is not a list. ValueError: If rank_ids size is not larger than 1 or rank_ids has duplicate data or backend is invalid. """ if backend == Backend.HCCL: if not isinstance(rank_ids, list): raise TypeError("Rank_ids {} should be list".format(rank_ids)) rank_size = len(rank_ids) if rank_size < 1: raise ValueError("Rank_ids size {} should be large than 0".format(rank_size)) if len(rank_ids) - len(list(set(rank_ids))) > 0: raise ValueError("List rank_ids in Group {} has duplicate data!".format(group)) hccl.create_group(group, rank_size, rank_ids) elif backend == Backend.NCCL: raise RuntimeError("Nccl doesn't support create_group now.") else: raise ValueError("Invalid backend: '{}'".format(backend)) @check_parameter_available def _destroy_group_helper(group, backend): """ The Helper to do destroy_group. Args: group (str): The user communication group. backend (str): The backend, like "hccl". Raises: ValueError: If group is "hccl_world_group" or backend is invalid. """ if backend == Backend.HCCL: if group == HCCL_WORLD_COMM_GROUP: raise ValueError("The hccl_world_group does not support destruction.") hccl.destroy_group(group) elif backend == Backend.NCCL: raise RuntimeError("Nccl doesn't support destroy_group now.") else: raise ValueError("Invalid backend: '{}'".format(backend))
30.761518
111
0.646903
from mindspore.parallel._ps_context import _is_role_pserver, _is_role_sched from ._hccl_management import load_lib as hccl_load_lib _HCCL_AVAILABLE = False _NCCL_AVAILABLE = False try: import mindspore._ms_mpi as mpi _NCCL_AVAILABLE = True except ImportError: _NCCL_AVAILABLE = False try: hccl_load_lib() _HCCL_AVAILABLE = True except RuntimeError: _HCCL_AVAILABLE = False if _HCCL_AVAILABLE: from . import _hccl_management as hccl else: try: import hccl_test.manage.api as hccl _HCCL_AVAILABLE = True except ImportError: _HCCL_AVAILABLE = False HCCL_WORLD_COMM_GROUP = "hccl_world_group" NCCL_WORLD_COMM_GROUP = "nccl_world_group" class Backend: UNDEFINED = "undefined" HCCL = "hccl" NCCL = "nccl" def __new__(cls, name): if not isinstance(name, str): raise TypeError("Backend name must be a string, but got {}".format(type(name))) value = getattr(Backend, name.upper(), Backend.UNDEFINED) if value == Backend.UNDEFINED: raise ValueError("Invalid backend: '{}'".format(name)) return value def is_hccl_available(): return _HCCL_AVAILABLE def is_nccl_available(): return _NCCL_AVAILABLE def check_parameter_available(func): def wrapper(*args, **kargs): if _is_role_pserver() or _is_role_sched(): return func(*args, **kargs) group = None if "group" in kargs.keys(): group = kargs.get("group") if group is not None and not isinstance(group, str): raise TypeError("Group should be str or None, " "but got group {}".format(type(group))) if "backend" in kargs.keys(): backend = kargs.get("backend") if backend is Backend.HCCL and not is_hccl_available(): raise RuntimeError("Distributed Communication doesn't have HCCL built in") if backend is Backend.NCCL and not is_nccl_available(): raise RuntimeError("Distributed Communication doesn't have NCCL built in") if group is None: if backend is Backend.HCCL: group = HCCL_WORLD_COMM_GROUP elif backend is Backend.NCCL: group = NCCL_WORLD_COMM_GROUP return func(*args, **kargs) return wrapper @check_parameter_available def _get_rank_helper(group, backend): rank_id = None if _is_role_pserver() or _is_role_sched(): rank_id = 0 return rank_id if backend == Backend.HCCL: if group == HCCL_WORLD_COMM_GROUP: rank_id = hccl.get_rank_id() else: rank_id = hccl.get_rank_id(group) elif backend == Backend.NCCL: rank_id = mpi.get_rank_id(group) else: raise ValueError("Invalid backend: '{}'".format(backend)) return rank_id @check_parameter_available def _get_local_rank_helper(group, backend): rank_id = None if backend == Backend.HCCL: if group == HCCL_WORLD_COMM_GROUP: rank_id = hccl.get_local_rank_id() else: rank_id = hccl.get_local_rank_id(group) elif backend == Backend.NCCL: raise RuntimeError("Nccl doesn't support get_local_rank_id now.") else: raise ValueError("Invalid backend: '{}'".format(backend)) return rank_id @check_parameter_available def _get_size_helper(group, backend): size = None if _is_role_pserver() or _is_role_sched(): size = 1 return size if backend == Backend.HCCL: if group == HCCL_WORLD_COMM_GROUP: size = hccl.get_rank_size() else: size = hccl.get_rank_size(group) elif backend == Backend.NCCL: size = mpi.get_rank_size(group) else: raise ValueError("Invalid backend: '{}'".format(backend)) return size @check_parameter_available def _get_local_size_helper(group, backend): size = None if backend == Backend.HCCL: if group == HCCL_WORLD_COMM_GROUP: size = hccl.get_local_rank_size() else: size = hccl.get_local_rank_size(group) elif backend == Backend.NCCL: raise RuntimeError("Nccl doesn't support get_local_rank_size now.") else: raise ValueError("Invalid backend: '{}'".format(backend)) return size @check_parameter_available def _get_world_rank_from_group_rank_helper(group, group_rank_id, backend): world_rank_id = None if not isinstance(group_rank_id, int): raise TypeError("group_rank_id should be int, but got type {}".format(type(group_rank_id))) if backend == Backend.HCCL: if group == HCCL_WORLD_COMM_GROUP: raise ValueError("Group cannot be 'hccl_world_group'. ") world_rank_id = hccl.get_world_rank_from_group_rank(group, group_rank_id) elif backend == Backend.NCCL: raise RuntimeError("Nccl doesn't support get_world_rank_from_group_rank now.") else: raise ValueError("Invalid backend: '{}'".format(backend)) return world_rank_id @check_parameter_available def _get_group_rank_from_world_rank_helper(world_rank_id, group, backend): group_rank_id = None if not isinstance(world_rank_id, int): raise TypeError("world_rank_id should be int, but got type {}".format(type(world_rank_id))) if backend == Backend.HCCL: if group == HCCL_WORLD_COMM_GROUP: raise ValueError("Group cannot be 'hccl_world_group'. ") group_rank_id = hccl.get_group_rank_from_world_rank(world_rank_id, group) elif backend == Backend.NCCL: raise RuntimeError("Nccl doesn't support get_group_rank_from_world_rank now.") else: raise ValueError("Invalid backend: '{}'".format(backend)) return group_rank_id @check_parameter_available def _create_group_helper(group, rank_ids, backend): if backend == Backend.HCCL: if not isinstance(rank_ids, list): raise TypeError("Rank_ids {} should be list".format(rank_ids)) rank_size = len(rank_ids) if rank_size < 1: raise ValueError("Rank_ids size {} should be large than 0".format(rank_size)) if len(rank_ids) - len(list(set(rank_ids))) > 0: raise ValueError("List rank_ids in Group {} has duplicate data!".format(group)) hccl.create_group(group, rank_size, rank_ids) elif backend == Backend.NCCL: raise RuntimeError("Nccl doesn't support create_group now.") else: raise ValueError("Invalid backend: '{}'".format(backend)) @check_parameter_available def _destroy_group_helper(group, backend): if backend == Backend.HCCL: if group == HCCL_WORLD_COMM_GROUP: raise ValueError("The hccl_world_group does not support destruction.") hccl.destroy_group(group) elif backend == Backend.NCCL: raise RuntimeError("Nccl doesn't support destroy_group now.") else: raise ValueError("Invalid backend: '{}'".format(backend))
true
true
f706171537799e2583aed68b68e365bd286ea23b
235
py
Python
tests/test_blackrock_rstart.py
sgowda/brain-python-interface
708e2a5229d0496a8ce9de32bda66f0925d366d9
[ "Apache-2.0" ]
7
2015-08-25T00:28:49.000Z
2020-04-14T22:58:51.000Z
tests/test_blackrock_rstart.py
sgowda/brain-python-interface
708e2a5229d0496a8ce9de32bda66f0925d366d9
[ "Apache-2.0" ]
89
2020-08-03T16:54:08.000Z
2022-03-09T19:56:19.000Z
tests/test_blackrock_rstart.py
sgowda/brain-python-interface
708e2a5229d0496a8ce9de32bda66f0925d366d9
[ "Apache-2.0" ]
4
2016-10-05T17:54:26.000Z
2020-08-06T15:37:09.000Z
from features.arduino_features import BlackrockSerialDIORowByte, SerialDIORowByte from riglib import experiment class par(object): def init(self): pass class F(BlackrockSerialDIORowByte, par): pass f = F() f.init()
16.785714
81
0.744681
from features.arduino_features import BlackrockSerialDIORowByte, SerialDIORowByte from riglib import experiment class par(object): def init(self): pass class F(BlackrockSerialDIORowByte, par): pass f = F() f.init()
true
true
f7061794ddceca6e6d6672e4e6533207f5f3e020
3,985
py
Python
src/snex/core.py
jwbargsten/snex
d922a3f71243d0eb3da6290ead2d3fb1fec18b3d
[ "MIT" ]
null
null
null
src/snex/core.py
jwbargsten/snex
d922a3f71243d0eb3da6290ead2d3fb1fec18b3d
[ "MIT" ]
null
null
null
src/snex/core.py
jwbargsten/snex
d922a3f71243d0eb3da6290ead2d3fb1fec18b3d
[ "MIT" ]
null
null
null
import logging import chevron import re from itertools import dropwhile import snex.util as util logger = logging.getLogger(__name__) DEFAULT = { # :snippet global-default-config lang: python "output_template": "```{{lang}}\n{{{snippet}}}\n```\n", "valid_param_keys": ["name", "lang", "lnum", "fname", "path"], "output_path": "snippets", "line_prefix": "", "comment_prefix": "# ", "comment_suffix": "", "snippet_start": ":snippet", "snippet_end": ":endsnippet", "cloak_start": ":cloak", "cloak_end": ":endcloak", "output_suffix": ".md", # :endsnippet } class Snippet: @classmethod def from_raw_data(cls, params, data, origin=None, prefix=""): (head,) = data[:1] (tail,) = data[-1:] body = data[1:-1] idlvl = util.det_indent_lvl(body) body = (line[idlvl:] for line in body) body = dropwhile(util.is_empty, body) body = util.dropwhile_right(util.is_empty, list(body)) body = (prefix + line for line in body) return cls(params=params, head=head.lstrip(), body=list(body), tail=tail.lstrip(), origin=origin) @property def name(self): return self.params["name"] @property def line_number(self): return self.params.get("lnum", -1) def __init__(self, params=None, head=None, body=None, tail=None, origin=None): self.params = params self.head = head self.body = body self.tail = tail self.origin = origin def __repr__(self): return f"Snippet({self.params!r}, {self.head!r}, {self.body!r}, {self.tail!r}, {self.origin!r})" def render_snippet(template, params, body): return chevron.render(template, {**params, **{"snippet": "\n".join(body)}}) def get_configs(conf): configs = conf["config"] default = configs["default"] if "default" in configs else None for name in [n for n in configs if not n == "default"]: c = util.merge_with_default_conf(configs[name], default, global_default=DEFAULT) # prevent defining a global name for all snippets in the config c.pop("name", None) yield (name, c) def extract_from_path(f, conf, base_path): comment_prefix = re.escape(conf["comment_prefix"]) comment_suffix = re.escape(conf["comment_suffix"]) line_prefix = conf["line_prefix"] cloak_start = re.escape(conf["cloak_start"]) cloak_end = re.escape(conf["cloak_end"]) cloak_start_re = f"^\\s*{comment_prefix}{cloak_start}{comment_suffix}$" cloak_end_re = f"^\\s*{comment_prefix}{cloak_end}{comment_suffix}$" snippet_start = re.escape(conf["snippet_start"]) snippet_end = re.escape(conf["snippet_end"]) snippet_start_re = f"^\\s*{comment_prefix}{snippet_start}(.*){comment_suffix}$" snippet_end_re = f"^\\s*{comment_prefix}{snippet_end}{comment_suffix}$" snippets = [] in_snippet = False cloaked = False data = [] params = {} for idx, line in util.read_path(f): lnum = idx + 1 if re.search(cloak_end_re, line): cloaked = False continue if re.search(cloak_start_re, line): cloaked = True if cloaked: continue if match := re.search(snippet_start_re, line): try: params = util.construct_params(match.group(1), f, base_path, lnum) params = util.sanitize_params(params, conf["valid_param_keys"]) except Exception as ex: logger.error(f"could not parse snippet params: {line} in file {f}:{lnum}") raise ex in_snippet = True if not in_snippet: continue data.append(line.rstrip("\n")) if re.search(snippet_end_re, line): in_snippet = False s = Snippet.from_raw_data(params, data, origin=f, prefix=line_prefix) snippets.append(s) data = [] params = {} return snippets
31.377953
105
0.608783
import logging import chevron import re from itertools import dropwhile import snex.util as util logger = logging.getLogger(__name__) DEFAULT = { "output_template": "```{{lang}}\n{{{snippet}}}\n```\n", "valid_param_keys": ["name", "lang", "lnum", "fname", "path"], "output_path": "snippets", "line_prefix": "", "comment_prefix": "# ", "comment_suffix": "", "snippet_start": ":snippet", "snippet_end": ":endsnippet", "cloak_start": ":cloak", "cloak_end": ":endcloak", "output_suffix": ".md", } class Snippet: @classmethod def from_raw_data(cls, params, data, origin=None, prefix=""): (head,) = data[:1] (tail,) = data[-1:] body = data[1:-1] idlvl = util.det_indent_lvl(body) body = (line[idlvl:] for line in body) body = dropwhile(util.is_empty, body) body = util.dropwhile_right(util.is_empty, list(body)) body = (prefix + line for line in body) return cls(params=params, head=head.lstrip(), body=list(body), tail=tail.lstrip(), origin=origin) @property def name(self): return self.params["name"] @property def line_number(self): return self.params.get("lnum", -1) def __init__(self, params=None, head=None, body=None, tail=None, origin=None): self.params = params self.head = head self.body = body self.tail = tail self.origin = origin def __repr__(self): return f"Snippet({self.params!r}, {self.head!r}, {self.body!r}, {self.tail!r}, {self.origin!r})" def render_snippet(template, params, body): return chevron.render(template, {**params, **{"snippet": "\n".join(body)}}) def get_configs(conf): configs = conf["config"] default = configs["default"] if "default" in configs else None for name in [n for n in configs if not n == "default"]: c = util.merge_with_default_conf(configs[name], default, global_default=DEFAULT) c.pop("name", None) yield (name, c) def extract_from_path(f, conf, base_path): comment_prefix = re.escape(conf["comment_prefix"]) comment_suffix = re.escape(conf["comment_suffix"]) line_prefix = conf["line_prefix"] cloak_start = re.escape(conf["cloak_start"]) cloak_end = re.escape(conf["cloak_end"]) cloak_start_re = f"^\\s*{comment_prefix}{cloak_start}{comment_suffix}$" cloak_end_re = f"^\\s*{comment_prefix}{cloak_end}{comment_suffix}$" snippet_start = re.escape(conf["snippet_start"]) snippet_end = re.escape(conf["snippet_end"]) snippet_start_re = f"^\\s*{comment_prefix}{snippet_start}(.*){comment_suffix}$" snippet_end_re = f"^\\s*{comment_prefix}{snippet_end}{comment_suffix}$" snippets = [] in_snippet = False cloaked = False data = [] params = {} for idx, line in util.read_path(f): lnum = idx + 1 if re.search(cloak_end_re, line): cloaked = False continue if re.search(cloak_start_re, line): cloaked = True if cloaked: continue if match := re.search(snippet_start_re, line): try: params = util.construct_params(match.group(1), f, base_path, lnum) params = util.sanitize_params(params, conf["valid_param_keys"]) except Exception as ex: logger.error(f"could not parse snippet params: {line} in file {f}:{lnum}") raise ex in_snippet = True if not in_snippet: continue data.append(line.rstrip("\n")) if re.search(snippet_end_re, line): in_snippet = False s = Snippet.from_raw_data(params, data, origin=f, prefix=line_prefix) snippets.append(s) data = [] params = {} return snippets
true
true
f70618c39c020effbe9aad023c7832f65e695fdd
5,470
py
Python
scarletio/utils/type_modulizer.py
HuyaneMatsu/scarletio
49e434a118bbf31586e2bf8631ac8d7c7a2f6ffb
[ "0BSD" ]
3
2021-12-01T07:56:36.000Z
2021-12-05T17:37:41.000Z
scarletio/utils/type_modulizer.py
HuyaneMatsu/scarletio
49e434a118bbf31586e2bf8631ac8d7c7a2f6ffb
[ "0BSD" ]
7
2021-12-04T14:29:27.000Z
2022-03-07T18:32:28.000Z
scarletio/utils/type_modulizer.py
HuyaneMatsu/scarletio
49e434a118bbf31586e2bf8631ac8d7c7a2f6ffb
[ "0BSD" ]
null
null
null
__all__ = ('modulize',) import sys from types import FunctionType, GetSetDescriptorType, MappingProxyType, ModuleType from .docs import has_docs NoneType = type(None) try: from _weakref import ref as WeakrefType except ImportError: from weakref import ref as WeakrefType # This 2 type can be function WrapperDescriptorType = type(object.__ne__) MethodDescriptorType = type(object.__format__) DO_NOT_MODULIZE_TYPES = [MappingProxyType, GetSetDescriptorType] if WrapperDescriptorType is not FunctionType: DO_NOT_MODULIZE_TYPES.append(WrapperDescriptorType) if MethodDescriptorType is not FunctionType: DO_NOT_MODULIZE_TYPES.append(MethodDescriptorType) DO_NOT_MODULIZE_TYPES = tuple(DO_NOT_MODULIZE_TYPES) @has_docs def _modulize_function(old, globals_, source_module, module_name, module_path): """ Changes the given function's scopes and qualname if they were defined inside of a modulized class. Parameters ---------- old : `function` A function present inside of a modulized class. globals_ : `dict` of (`str`, `Any`) Global variables of the respective module. source_module : `module` The module, where the modulized class was defined. module_name : `str` The newly created module's name. module_path : `str` The newly created module's path. Returns ------- new : `function` Newly recreated function if applicable. """ if old.__module__ != source_module: return old new = FunctionType(old.__code__, globals_, old.__name__, old.__defaults__, old.__closure__) new.__module__ = module_path qualname = old.__qualname__ if (qualname is not None) and (len(qualname) > len(module_name)) and qualname[len(module_name)] =='.' and \ qualname.startswith(module_name): new.__qualname__ = qualname[len(module_name) + 1:] return new @has_docs def _modulize_type(klass, globals_, source_module, module_name, module_path): """ Changes the given class's scopes and qualname if they were defined inside of a modulized class. Parameters ---------- klass : `type` A class present inside of a modulized class. globals_ : `dict` of (`str`, `Any`) Global variables of the respective module. source_module : `module` The module, where the modulized class was defined. module_name : `str` The newly created module's name. module_path : `str` The newly created module's path. """ if klass.__module__ != source_module: return qualname = klass.__qualname__ if (qualname is None) or (len(qualname) <= len(module_name)) or qualname[len(module_name)] != '.' \ or not qualname.startswith(module_name): return klass.__qualname__ = qualname[len(module_name) + 1:] klass.__module__ = module_path for name in dir(klass): value = getattr(klass, name) value_type = value.__class__ if value_type is FunctionType: value = _modulize_function(value, globals_, source_module, module_name, module_path) setattr(klass, name, value) if issubclass(value_type, type): _modulize_type(value, globals_, source_module, module_name, module_path) @has_docs def modulize(klass): """ Transforms the given class to a module. Every functions and classes defined inside of given class, which are also present at transformation as well, will have their global scope modified. Parameters ---------- klass : `type` The class to transform to module. Returns ------- result_module : `module` The created module object. Raises ------ TypeError If `klass` is not given as `type`. """ if not isinstance(klass, type): raise TypeError( f'Only types can be modulized, got {klass.__class__.__name__}; {klass!r}.' ) source_module = klass.__module__ module_name = klass.__name__ module_path = f'{klass.__module__}.{module_name}' try: result_module = sys.modules['module_path'] except KeyError: result_module = ModuleType(module_path) sys.modules[module_path] = result_module globals_ = result_module.__dict__ globals_['__builtins__'] = __builtins__ else: globals_ = result_module.__dict__ collected_names = [] for name in globals_.keys(): if name.startswith('__') and name.endswith('__'): continue collected_names.append(name) for name in collected_names: del globals_[name] globals_['__doc__'] = None for name in type.__dir__(klass): if name.startswith('__') and name.endswith('__') and name != '__doc__': continue value = type.__getattribute__(klass, name) value_type = type(value) if value_type in DO_NOT_MODULIZE_TYPES: continue if value_type is FunctionType: value = _modulize_function(value, globals_, source_module, module_name, module_path) if issubclass(value_type, type): _modulize_type(value, globals_, source_module, module_name, module_path) ModuleType.__setattr__(result_module, name, value) return result_module
31.257143
117
0.652468
__all__ = ('modulize',) import sys from types import FunctionType, GetSetDescriptorType, MappingProxyType, ModuleType from .docs import has_docs NoneType = type(None) try: from _weakref import ref as WeakrefType except ImportError: from weakref import ref as WeakrefType WrapperDescriptorType = type(object.__ne__) MethodDescriptorType = type(object.__format__) DO_NOT_MODULIZE_TYPES = [MappingProxyType, GetSetDescriptorType] if WrapperDescriptorType is not FunctionType: DO_NOT_MODULIZE_TYPES.append(WrapperDescriptorType) if MethodDescriptorType is not FunctionType: DO_NOT_MODULIZE_TYPES.append(MethodDescriptorType) DO_NOT_MODULIZE_TYPES = tuple(DO_NOT_MODULIZE_TYPES) @has_docs def _modulize_function(old, globals_, source_module, module_name, module_path): if old.__module__ != source_module: return old new = FunctionType(old.__code__, globals_, old.__name__, old.__defaults__, old.__closure__) new.__module__ = module_path qualname = old.__qualname__ if (qualname is not None) and (len(qualname) > len(module_name)) and qualname[len(module_name)] =='.' and \ qualname.startswith(module_name): new.__qualname__ = qualname[len(module_name) + 1:] return new @has_docs def _modulize_type(klass, globals_, source_module, module_name, module_path): if klass.__module__ != source_module: return qualname = klass.__qualname__ if (qualname is None) or (len(qualname) <= len(module_name)) or qualname[len(module_name)] != '.' \ or not qualname.startswith(module_name): return klass.__qualname__ = qualname[len(module_name) + 1:] klass.__module__ = module_path for name in dir(klass): value = getattr(klass, name) value_type = value.__class__ if value_type is FunctionType: value = _modulize_function(value, globals_, source_module, module_name, module_path) setattr(klass, name, value) if issubclass(value_type, type): _modulize_type(value, globals_, source_module, module_name, module_path) @has_docs def modulize(klass): if not isinstance(klass, type): raise TypeError( f'Only types can be modulized, got {klass.__class__.__name__}; {klass!r}.' ) source_module = klass.__module__ module_name = klass.__name__ module_path = f'{klass.__module__}.{module_name}' try: result_module = sys.modules['module_path'] except KeyError: result_module = ModuleType(module_path) sys.modules[module_path] = result_module globals_ = result_module.__dict__ globals_['__builtins__'] = __builtins__ else: globals_ = result_module.__dict__ collected_names = [] for name in globals_.keys(): if name.startswith('__') and name.endswith('__'): continue collected_names.append(name) for name in collected_names: del globals_[name] globals_['__doc__'] = None for name in type.__dir__(klass): if name.startswith('__') and name.endswith('__') and name != '__doc__': continue value = type.__getattribute__(klass, name) value_type = type(value) if value_type in DO_NOT_MODULIZE_TYPES: continue if value_type is FunctionType: value = _modulize_function(value, globals_, source_module, module_name, module_path) if issubclass(value_type, type): _modulize_type(value, globals_, source_module, module_name, module_path) ModuleType.__setattr__(result_module, name, value) return result_module
true
true
f70618d1936aad1ec8ef3dbc308ea36fef818331
11,554
py
Python
yatube/posts/tests/test_views.py
deadbit-dev/yatube
10be5278cd2bca6f47a7d3295e4cb4142883f48e
[ "MIT" ]
null
null
null
yatube/posts/tests/test_views.py
deadbit-dev/yatube
10be5278cd2bca6f47a7d3295e4cb4142883f48e
[ "MIT" ]
null
null
null
yatube/posts/tests/test_views.py
deadbit-dev/yatube
10be5278cd2bca6f47a7d3295e4cb4142883f48e
[ "MIT" ]
null
null
null
import shutil import tempfile from django.conf import settings from django.core.files.uploadedfile import SimpleUploadedFile from django.core.cache import cache from django.contrib.auth import get_user_model from django.test import Client, TestCase, override_settings from django.urls import reverse from django import forms from ..models import Post, Group, Follow User = get_user_model() TEMP_MEDIA_ROOT = tempfile.mkdtemp(dir=settings.BASE_DIR) class PaginatorTest(TestCase): @classmethod def setUpClass(cls): super().setUpClass() cls.user = User.objects.create_user(username='NoName') cls.user_client = Client() cls.user_client.force_login(cls.user) cls.count_posts_on_page = 10 cls.count_posts = 15 cls.rest_posts = cls.count_posts % cls.count_posts_on_page cls.group = Group.objects.create( title='Тестовый заголовок', slug='test-slug', description='Описание...' ) posts = ( Post( text='Тестовый текст поста.', author=cls.user, group=cls.group ) for i in range(cls.count_posts) ) Post.objects.bulk_create(posts, cls.count_posts) def test_paginator_pages(self): pages_paginator = [ reverse( 'posts:index' ), reverse( 'posts:group_list', kwargs={'slug': PaginatorTest.group.slug} ), reverse( 'posts:profile', kwargs={'username': PaginatorTest.user} ) ] for page in pages_paginator: with self.subTest(page=page): response = PaginatorTest.user_client.get(page) self.assertEqual( len(response.context['page_obj']), ( PaginatorTest.count_posts_on_page ) ) response = PaginatorTest.user_client.get( page + '?page=2' ) self.assertEqual( len(response.context['page_obj']), PaginatorTest.rest_posts ) @override_settings(MEDIA_ROOT=TEMP_MEDIA_ROOT) class PostPagesTests(TestCase): @classmethod def setUpClass(cls): super().setUpClass() cls.user = User.objects.create_user(username='NoName') cls.user_client = Client() cls.user_client.force_login(cls.user) small_gif = ( b'\x47\x49\x46\x38\x39\x61\x02\x00' b'\x01\x00\x80\x00\x00\x00\x00\x00' b'\xFF\xFF\xFF\x21\xF9\x04\x00\x00' b'\x00\x00\x00\x2C\x00\x00\x00\x00' b'\x02\x00\x01\x00\x00\x02\x02\x0C' b'\x0A\x00\x3B' ) image = SimpleUploadedFile( name='small.gif', content=small_gif, content_type='image/gif' ) cls.group = Group.objects.create( title='Тестовый заголовок', slug='test-slug', description='Описание...' ) cls.post = Post.objects.create( text='Тестовый текст поста.', image=image, author=cls.user, group=cls.group ) @classmethod def tearDownClass(cls): super().tearDownClass() shutil.rmtree(TEMP_MEDIA_ROOT, ignore_errors=True) def test_pages_uses_correct_template(self): templates_pages_names = { reverse('posts:index'): 'posts/index.html', reverse( 'posts:group_list', kwargs={'slug': PostPagesTests.group.slug} ): 'posts/group_list.html', reverse( 'posts:profile', kwargs={'username': PostPagesTests.user} ): 'posts/profile.html', reverse( 'posts:post_detail', kwargs={'post_id': PostPagesTests.post.id} ): 'posts/post_detail.html', reverse('posts:post_create'): 'posts/create_post.html', reverse( 'posts:post_edit', kwargs={'post_id': PostPagesTests.post.id} ): 'posts/create_post.html' } for reverse_name, template in templates_pages_names.items(): with self.subTest(reverse_name=reverse_name): response = PostPagesTests.user_client.get(reverse_name) self.assertTemplateUsed(response, template) def check_post_context(self, post): self.assertEqual(post.id, PostPagesTests.post.id) self.assertEqual(post.author, PostPagesTests.post.author) self.assertEqual(post.text, PostPagesTests.post.text) self.assertEqual(post.image, PostPagesTests.post.image) self.assertEqual(post.group, PostPagesTests.post.group) def test_index_page_context(self): response = PostPagesTests.user_client.get(reverse('posts:index')) self.check_post_context(response.context['page_obj'][0]) def test_group_list_page_context(self): response = PostPagesTests.user_client.get( reverse( 'posts:group_list', kwargs={'slug': PostPagesTests.group.slug} ) ) self.check_post_context(response.context['page_obj'][0]) self.assertEqual( PostPagesTests.group, response.context['group'] ) def test_new_group_list_none(self): group = Group.objects.create( title='Тестовый заголовок', slug='test-slug-new', description='Описание...' ) response = PostPagesTests.user_client.get( reverse( 'posts:group_list', kwargs={'slug': group.slug} ) ) self.assertEqual(len(response.context['page_obj']), 0) def test_profile_page_context(self): response = PostPagesTests.user_client.get( reverse( 'posts:profile', kwargs={'username': PostPagesTests.user} ) ) self.check_post_context(response.context['page_obj'][0]) self.assertEqual( PostPagesTests.user, response.context['author'] ) self.assertIsNotNone(response.context['following']) def test_post_detail_page_context(self): response = PostPagesTests.user_client.get( reverse( 'posts:post_detail', kwargs={'post_id': PostPagesTests.post.id} ) ) self.check_post_context(response.context['post']) form_fields = {'text': forms.fields.CharField} for value, expected in form_fields.items(): with self.subTest(value=value): form_fields = response.context['form'].fields.get(value) self.assertIsInstance(form_fields, expected) self.assertIsNotNone(response.context['comments']) def test_edit_post_page_context(self): response = PostPagesTests.user_client.get( reverse( 'posts:post_edit', kwargs={'post_id': PostPagesTests.post.id} ) ) self.assertIsNotNone(response.context['form']) self.assertIsNotNone(response.context['is_edit']) self.assertTrue(response.context['is_edit']) form_fields = { 'text': forms.fields.CharField, 'group': forms.fields.ChoiceField, } for value, expected in form_fields.items(): with self.subTest(value=value): form_fields = response.context['form'].fields.get(value) self.assertIsInstance(form_fields, expected) def test_create_post_page_context(self): response = PostPagesTests.user_client.get( reverse('posts:post_create') ) form_fields = { 'text': forms.fields.CharField, 'group': forms.fields.ChoiceField, } for value, expected in form_fields.items(): with self.subTest(value=value): form_fields = response.context['form'].fields.get(value) self.assertIsInstance(form_fields, expected) def test_cache_index(self): post = Post.objects.create( text='Тестовый текст поста.', author=PostPagesTests.user, ) response = PostPagesTests.user_client.get( reverse('posts:index') ) page = response.content post.delete() response = PostPagesTests.user_client.get( reverse('posts:index') ) self.assertEqual(page, response.content) cache.clear() response = PostPagesTests.user_client.get( reverse('posts:index') ) self.assertNotEqual(page, response.content) class FollowTests(TestCase): @classmethod def setUpClass(cls): super().setUpClass() cls.user = User.objects.create_user(username='NoName') cls.user_client = Client() cls.user_client.force_login(cls.user) cls.author = User.objects.create_user(username='Author') cls.author_client = Client() cls.author_client.force_login(cls.author) def test_following_auth(self): #FIXME: connection through ORM FollowTests.user_client.get( reverse( 'posts:profile_follow', kwargs={'username': FollowTests.author} ) ) #FIXME: necessary to calculate that a BD increased by one object follow = Follow.objects.last() self.assertEqual(follow.user, FollowTests.user) self.assertEqual(follow.author, FollowTests.author) def test_unfollow_auth(self): #FIXME: connection through ORM FollowTests.user_client.get( reverse( 'posts:profile_follow', kwargs={'username': FollowTests.author} ) ) follows_count = Follow.objects.count() FollowTests.user_client.get( reverse( 'posts:profile_unfollow', kwargs={'username': FollowTests.author} ) ) self.assertEqual(Follow.objects.count(), follows_count - 1) def test_new_post_follow(self): FollowTests.user_client.get( reverse( 'posts:profile_follow', kwargs={'username': FollowTests.author} ) ) post = Post.objects.create( text='Тестовый текст поста.', author=FollowTests.author, ) response = FollowTests.user_client.get( reverse( 'posts:follow_index' ) ) self.assertEqual( post.id, response.context['page_obj'][0].id ) def test_new_post_unfollow(self): FollowTests.user_client.get( reverse( 'posts:profile_follow', kwargs={'username': FollowTests.author} ) ) Post.objects.create( text='Тестовый текст поста.', author=FollowTests.author, ) user = User.objects.create_user(username='NameNo') user_client = Client() user_client.force_login(user) response = user_client.get( reverse( 'posts:follow_index' ) ) self.assertEqual(len(response.context['page_obj']), 0)
33.982353
73
0.571404
import shutil import tempfile from django.conf import settings from django.core.files.uploadedfile import SimpleUploadedFile from django.core.cache import cache from django.contrib.auth import get_user_model from django.test import Client, TestCase, override_settings from django.urls import reverse from django import forms from ..models import Post, Group, Follow User = get_user_model() TEMP_MEDIA_ROOT = tempfile.mkdtemp(dir=settings.BASE_DIR) class PaginatorTest(TestCase): @classmethod def setUpClass(cls): super().setUpClass() cls.user = User.objects.create_user(username='NoName') cls.user_client = Client() cls.user_client.force_login(cls.user) cls.count_posts_on_page = 10 cls.count_posts = 15 cls.rest_posts = cls.count_posts % cls.count_posts_on_page cls.group = Group.objects.create( title='Тестовый заголовок', slug='test-slug', description='Описание...' ) posts = ( Post( text='Тестовый текст поста.', author=cls.user, group=cls.group ) for i in range(cls.count_posts) ) Post.objects.bulk_create(posts, cls.count_posts) def test_paginator_pages(self): pages_paginator = [ reverse( 'posts:index' ), reverse( 'posts:group_list', kwargs={'slug': PaginatorTest.group.slug} ), reverse( 'posts:profile', kwargs={'username': PaginatorTest.user} ) ] for page in pages_paginator: with self.subTest(page=page): response = PaginatorTest.user_client.get(page) self.assertEqual( len(response.context['page_obj']), ( PaginatorTest.count_posts_on_page ) ) response = PaginatorTest.user_client.get( page + '?page=2' ) self.assertEqual( len(response.context['page_obj']), PaginatorTest.rest_posts ) @override_settings(MEDIA_ROOT=TEMP_MEDIA_ROOT) class PostPagesTests(TestCase): @classmethod def setUpClass(cls): super().setUpClass() cls.user = User.objects.create_user(username='NoName') cls.user_client = Client() cls.user_client.force_login(cls.user) small_gif = ( b'\x47\x49\x46\x38\x39\x61\x02\x00' b'\x01\x00\x80\x00\x00\x00\x00\x00' b'\xFF\xFF\xFF\x21\xF9\x04\x00\x00' b'\x00\x00\x00\x2C\x00\x00\x00\x00' b'\x02\x00\x01\x00\x00\x02\x02\x0C' b'\x0A\x00\x3B' ) image = SimpleUploadedFile( name='small.gif', content=small_gif, content_type='image/gif' ) cls.group = Group.objects.create( title='Тестовый заголовок', slug='test-slug', description='Описание...' ) cls.post = Post.objects.create( text='Тестовый текст поста.', image=image, author=cls.user, group=cls.group ) @classmethod def tearDownClass(cls): super().tearDownClass() shutil.rmtree(TEMP_MEDIA_ROOT, ignore_errors=True) def test_pages_uses_correct_template(self): templates_pages_names = { reverse('posts:index'): 'posts/index.html', reverse( 'posts:group_list', kwargs={'slug': PostPagesTests.group.slug} ): 'posts/group_list.html', reverse( 'posts:profile', kwargs={'username': PostPagesTests.user} ): 'posts/profile.html', reverse( 'posts:post_detail', kwargs={'post_id': PostPagesTests.post.id} ): 'posts/post_detail.html', reverse('posts:post_create'): 'posts/create_post.html', reverse( 'posts:post_edit', kwargs={'post_id': PostPagesTests.post.id} ): 'posts/create_post.html' } for reverse_name, template in templates_pages_names.items(): with self.subTest(reverse_name=reverse_name): response = PostPagesTests.user_client.get(reverse_name) self.assertTemplateUsed(response, template) def check_post_context(self, post): self.assertEqual(post.id, PostPagesTests.post.id) self.assertEqual(post.author, PostPagesTests.post.author) self.assertEqual(post.text, PostPagesTests.post.text) self.assertEqual(post.image, PostPagesTests.post.image) self.assertEqual(post.group, PostPagesTests.post.group) def test_index_page_context(self): response = PostPagesTests.user_client.get(reverse('posts:index')) self.check_post_context(response.context['page_obj'][0]) def test_group_list_page_context(self): response = PostPagesTests.user_client.get( reverse( 'posts:group_list', kwargs={'slug': PostPagesTests.group.slug} ) ) self.check_post_context(response.context['page_obj'][0]) self.assertEqual( PostPagesTests.group, response.context['group'] ) def test_new_group_list_none(self): group = Group.objects.create( title='Тестовый заголовок', slug='test-slug-new', description='Описание...' ) response = PostPagesTests.user_client.get( reverse( 'posts:group_list', kwargs={'slug': group.slug} ) ) self.assertEqual(len(response.context['page_obj']), 0) def test_profile_page_context(self): response = PostPagesTests.user_client.get( reverse( 'posts:profile', kwargs={'username': PostPagesTests.user} ) ) self.check_post_context(response.context['page_obj'][0]) self.assertEqual( PostPagesTests.user, response.context['author'] ) self.assertIsNotNone(response.context['following']) def test_post_detail_page_context(self): response = PostPagesTests.user_client.get( reverse( 'posts:post_detail', kwargs={'post_id': PostPagesTests.post.id} ) ) self.check_post_context(response.context['post']) form_fields = {'text': forms.fields.CharField} for value, expected in form_fields.items(): with self.subTest(value=value): form_fields = response.context['form'].fields.get(value) self.assertIsInstance(form_fields, expected) self.assertIsNotNone(response.context['comments']) def test_edit_post_page_context(self): response = PostPagesTests.user_client.get( reverse( 'posts:post_edit', kwargs={'post_id': PostPagesTests.post.id} ) ) self.assertIsNotNone(response.context['form']) self.assertIsNotNone(response.context['is_edit']) self.assertTrue(response.context['is_edit']) form_fields = { 'text': forms.fields.CharField, 'group': forms.fields.ChoiceField, } for value, expected in form_fields.items(): with self.subTest(value=value): form_fields = response.context['form'].fields.get(value) self.assertIsInstance(form_fields, expected) def test_create_post_page_context(self): response = PostPagesTests.user_client.get( reverse('posts:post_create') ) form_fields = { 'text': forms.fields.CharField, 'group': forms.fields.ChoiceField, } for value, expected in form_fields.items(): with self.subTest(value=value): form_fields = response.context['form'].fields.get(value) self.assertIsInstance(form_fields, expected) def test_cache_index(self): post = Post.objects.create( text='Тестовый текст поста.', author=PostPagesTests.user, ) response = PostPagesTests.user_client.get( reverse('posts:index') ) page = response.content post.delete() response = PostPagesTests.user_client.get( reverse('posts:index') ) self.assertEqual(page, response.content) cache.clear() response = PostPagesTests.user_client.get( reverse('posts:index') ) self.assertNotEqual(page, response.content) class FollowTests(TestCase): @classmethod def setUpClass(cls): super().setUpClass() cls.user = User.objects.create_user(username='NoName') cls.user_client = Client() cls.user_client.force_login(cls.user) cls.author = User.objects.create_user(username='Author') cls.author_client = Client() cls.author_client.force_login(cls.author) def test_following_auth(self): FollowTests.user_client.get( reverse( 'posts:profile_follow', kwargs={'username': FollowTests.author} ) ) follow = Follow.objects.last() self.assertEqual(follow.user, FollowTests.user) self.assertEqual(follow.author, FollowTests.author) def test_unfollow_auth(self): FollowTests.user_client.get( reverse( 'posts:profile_follow', kwargs={'username': FollowTests.author} ) ) follows_count = Follow.objects.count() FollowTests.user_client.get( reverse( 'posts:profile_unfollow', kwargs={'username': FollowTests.author} ) ) self.assertEqual(Follow.objects.count(), follows_count - 1) def test_new_post_follow(self): FollowTests.user_client.get( reverse( 'posts:profile_follow', kwargs={'username': FollowTests.author} ) ) post = Post.objects.create( text='Тестовый текст поста.', author=FollowTests.author, ) response = FollowTests.user_client.get( reverse( 'posts:follow_index' ) ) self.assertEqual( post.id, response.context['page_obj'][0].id ) def test_new_post_unfollow(self): FollowTests.user_client.get( reverse( 'posts:profile_follow', kwargs={'username': FollowTests.author} ) ) Post.objects.create( text='Тестовый текст поста.', author=FollowTests.author, ) user = User.objects.create_user(username='NameNo') user_client = Client() user_client.force_login(user) response = user_client.get( reverse( 'posts:follow_index' ) ) self.assertEqual(len(response.context['page_obj']), 0)
true
true
f7061903a264680f9b39d3a390c04ca8597fdfc7
8,316
py
Python
tests/test_oracle.py
lkpdn/alembic
85cc0fdcf5bf8cf34fcc87362052bcc9fb9d1f2c
[ "MIT" ]
null
null
null
tests/test_oracle.py
lkpdn/alembic
85cc0fdcf5bf8cf34fcc87362052bcc9fb9d1f2c
[ "MIT" ]
null
null
null
tests/test_oracle.py
lkpdn/alembic
85cc0fdcf5bf8cf34fcc87362052bcc9fb9d1f2c
[ "MIT" ]
null
null
null
from sqlalchemy import Column from sqlalchemy import Integer from alembic import command from alembic import op from alembic.testing import config from alembic.testing.env import _no_sql_testing_config from alembic.testing.env import clear_staging_env from alembic.testing.env import staging_env from alembic.testing.env import three_rev_fixture from alembic.testing.fixtures import capture_context_buffer from alembic.testing.fixtures import op_fixture from alembic.testing.fixtures import TestBase from alembic.util import sqla_compat class FullEnvironmentTests(TestBase): @classmethod def setup_class(cls): staging_env() cls.cfg = cfg = _no_sql_testing_config("oracle") cls.a, cls.b, cls.c = three_rev_fixture(cfg) @classmethod def teardown_class(cls): clear_staging_env() def test_begin_comit(self): with capture_context_buffer(transactional_ddl=True) as buf: command.upgrade(self.cfg, self.a, sql=True) assert "SET TRANSACTION READ WRITE\n\n/" in buf.getvalue() assert "COMMIT\n\n/" in buf.getvalue() def test_batch_separator_default(self): with capture_context_buffer() as buf: command.upgrade(self.cfg, self.a, sql=True) assert "/" in buf.getvalue() assert ";" not in buf.getvalue() def test_batch_separator_custom(self): with capture_context_buffer(oracle_batch_separator="BYE") as buf: command.upgrade(self.cfg, self.a, sql=True) assert "BYE" in buf.getvalue() class OpTest(TestBase): def test_add_column(self): context = op_fixture("oracle") op.add_column("t1", Column("c1", Integer, nullable=False)) context.assert_("ALTER TABLE t1 ADD c1 INTEGER NOT NULL") def test_add_column_with_default(self): context = op_fixture("oracle") op.add_column( "t1", Column("c1", Integer, nullable=False, server_default="12") ) context.assert_("ALTER TABLE t1 ADD c1 INTEGER DEFAULT '12' NOT NULL") @config.requirements.comments def test_add_column_with_comment(self): context = op_fixture("oracle") op.add_column( "t1", Column("c1", Integer, nullable=False, comment="c1 comment") ) context.assert_( "ALTER TABLE t1 ADD c1 INTEGER NOT NULL", "COMMENT ON COLUMN t1.c1 IS 'c1 comment'", ) @config.requirements.computed_columns_api def test_add_column_computed(self): context = op_fixture("oracle") op.add_column( "t1", Column("some_column", Integer, sqla_compat.Computed("foo * 5")), ) context.assert_( "ALTER TABLE t1 ADD some_column " "INTEGER GENERATED ALWAYS AS (foo * 5)" ) def test_alter_table_rename_oracle(self): context = op_fixture("oracle") op.rename_table("s", "t") context.assert_("ALTER TABLE s RENAME TO t") def test_alter_table_rename_schema_oracle(self): context = op_fixture("oracle") op.rename_table("s", "t", schema="myowner") context.assert_("ALTER TABLE myowner.s RENAME TO t") def test_alter_column_rename_oracle(self): context = op_fixture("oracle") op.alter_column("t", "c", new_column_name="x") context.assert_("ALTER TABLE t RENAME COLUMN c TO x") def test_alter_column_new_type(self): context = op_fixture("oracle") op.alter_column("t", "c", type_=Integer) context.assert_("ALTER TABLE t MODIFY c INTEGER") def test_alter_column_add_comment(self): context = op_fixture("oracle") op.alter_column("t", "c", type_=Integer, comment="c comment") context.assert_( "ALTER TABLE t MODIFY c INTEGER", "COMMENT ON COLUMN t.c IS 'c comment'", ) def test_alter_column_add_comment_quotes(self): context = op_fixture("oracle") op.alter_column("t", "c", type_=Integer, comment="c 'comment'") context.assert_( "ALTER TABLE t MODIFY c INTEGER", "COMMENT ON COLUMN t.c IS 'c ''comment'''", ) def test_alter_column_drop_comment(self): context = op_fixture("oracle") op.alter_column("t", "c", type_=Integer, comment=None) context.assert_( "ALTER TABLE t MODIFY c INTEGER", "COMMENT ON COLUMN t.c IS ''" ) def test_create_table_comment(self): # this is handled by SQLAlchemy's compilers context = op_fixture("oracle") op.create_table_comment("t2", comment="t2 table", schema="foo") context.assert_("COMMENT ON TABLE foo.t2 IS 't2 table'") def test_drop_table_comment(self): # this is handled by SQLAlchemy's compilers context = op_fixture("oracle") op.drop_table_comment("t2", existing_comment="t2 table", schema="foo") context.assert_("COMMENT ON TABLE foo.t2 IS ''") def test_drop_index(self): context = op_fixture("oracle") op.drop_index("my_idx", "my_table") context.assert_contains("DROP INDEX my_idx") def test_drop_column_w_default(self): context = op_fixture("oracle") op.drop_column("t1", "c1") context.assert_("ALTER TABLE t1 DROP COLUMN c1") def test_drop_column_w_check(self): context = op_fixture("oracle") op.drop_column("t1", "c1") context.assert_("ALTER TABLE t1 DROP COLUMN c1") def test_alter_column_nullable_w_existing_type(self): context = op_fixture("oracle") op.alter_column("t", "c", nullable=True, existing_type=Integer) context.assert_("ALTER TABLE t MODIFY c NULL") def test_alter_column_not_nullable_w_existing_type(self): context = op_fixture("oracle") op.alter_column("t", "c", nullable=False, existing_type=Integer) context.assert_("ALTER TABLE t MODIFY c NOT NULL") def test_alter_column_nullable_w_new_type(self): context = op_fixture("oracle") op.alter_column("t", "c", nullable=True, type_=Integer) context.assert_( "ALTER TABLE t MODIFY c NULL", "ALTER TABLE t MODIFY c INTEGER" ) def test_alter_column_not_nullable_w_new_type(self): context = op_fixture("oracle") op.alter_column("t", "c", nullable=False, type_=Integer) context.assert_( "ALTER TABLE t MODIFY c NOT NULL", "ALTER TABLE t MODIFY c INTEGER" ) def test_alter_add_server_default(self): context = op_fixture("oracle") op.alter_column("t", "c", server_default="5") context.assert_("ALTER TABLE t MODIFY c DEFAULT '5'") def test_alter_replace_server_default(self): context = op_fixture("oracle") op.alter_column( "t", "c", server_default="5", existing_server_default="6" ) context.assert_("ALTER TABLE t MODIFY c DEFAULT '5'") def test_alter_remove_server_default(self): context = op_fixture("oracle") op.alter_column("t", "c", server_default=None) context.assert_("ALTER TABLE t MODIFY c DEFAULT NULL") def test_alter_do_everything(self): context = op_fixture("oracle") op.alter_column( "t", "c", new_column_name="c2", nullable=True, type_=Integer, server_default="5", ) context.assert_( "ALTER TABLE t MODIFY c NULL", "ALTER TABLE t MODIFY c DEFAULT '5'", "ALTER TABLE t MODIFY c INTEGER", "ALTER TABLE t RENAME COLUMN c TO c2", ) @config.requirements.comments def test_create_table_with_column_comments(self): context = op_fixture("oracle") op.create_table( "t2", Column("c1", Integer, primary_key=True), comment="t2 comment" ) context.assert_( "CREATE TABLE t2 (c1 INTEGER NOT NULL, PRIMARY KEY (c1))", "COMMENT ON TABLE t2 IS 't2 comment'", ) # TODO: when we add schema support # def test_alter_column_rename_oracle_schema(self): # context = op_fixture('oracle') # op.alter_column("t", "c", name="x", schema="y") # context.assert_( # 'ALTER TABLE y.t RENAME COLUMN c TO c2' # )
36.31441
79
0.637807
from sqlalchemy import Column from sqlalchemy import Integer from alembic import command from alembic import op from alembic.testing import config from alembic.testing.env import _no_sql_testing_config from alembic.testing.env import clear_staging_env from alembic.testing.env import staging_env from alembic.testing.env import three_rev_fixture from alembic.testing.fixtures import capture_context_buffer from alembic.testing.fixtures import op_fixture from alembic.testing.fixtures import TestBase from alembic.util import sqla_compat class FullEnvironmentTests(TestBase): @classmethod def setup_class(cls): staging_env() cls.cfg = cfg = _no_sql_testing_config("oracle") cls.a, cls.b, cls.c = three_rev_fixture(cfg) @classmethod def teardown_class(cls): clear_staging_env() def test_begin_comit(self): with capture_context_buffer(transactional_ddl=True) as buf: command.upgrade(self.cfg, self.a, sql=True) assert "SET TRANSACTION READ WRITE\n\n/" in buf.getvalue() assert "COMMIT\n\n/" in buf.getvalue() def test_batch_separator_default(self): with capture_context_buffer() as buf: command.upgrade(self.cfg, self.a, sql=True) assert "/" in buf.getvalue() assert ";" not in buf.getvalue() def test_batch_separator_custom(self): with capture_context_buffer(oracle_batch_separator="BYE") as buf: command.upgrade(self.cfg, self.a, sql=True) assert "BYE" in buf.getvalue() class OpTest(TestBase): def test_add_column(self): context = op_fixture("oracle") op.add_column("t1", Column("c1", Integer, nullable=False)) context.assert_("ALTER TABLE t1 ADD c1 INTEGER NOT NULL") def test_add_column_with_default(self): context = op_fixture("oracle") op.add_column( "t1", Column("c1", Integer, nullable=False, server_default="12") ) context.assert_("ALTER TABLE t1 ADD c1 INTEGER DEFAULT '12' NOT NULL") @config.requirements.comments def test_add_column_with_comment(self): context = op_fixture("oracle") op.add_column( "t1", Column("c1", Integer, nullable=False, comment="c1 comment") ) context.assert_( "ALTER TABLE t1 ADD c1 INTEGER NOT NULL", "COMMENT ON COLUMN t1.c1 IS 'c1 comment'", ) @config.requirements.computed_columns_api def test_add_column_computed(self): context = op_fixture("oracle") op.add_column( "t1", Column("some_column", Integer, sqla_compat.Computed("foo * 5")), ) context.assert_( "ALTER TABLE t1 ADD some_column " "INTEGER GENERATED ALWAYS AS (foo * 5)" ) def test_alter_table_rename_oracle(self): context = op_fixture("oracle") op.rename_table("s", "t") context.assert_("ALTER TABLE s RENAME TO t") def test_alter_table_rename_schema_oracle(self): context = op_fixture("oracle") op.rename_table("s", "t", schema="myowner") context.assert_("ALTER TABLE myowner.s RENAME TO t") def test_alter_column_rename_oracle(self): context = op_fixture("oracle") op.alter_column("t", "c", new_column_name="x") context.assert_("ALTER TABLE t RENAME COLUMN c TO x") def test_alter_column_new_type(self): context = op_fixture("oracle") op.alter_column("t", "c", type_=Integer) context.assert_("ALTER TABLE t MODIFY c INTEGER") def test_alter_column_add_comment(self): context = op_fixture("oracle") op.alter_column("t", "c", type_=Integer, comment="c comment") context.assert_( "ALTER TABLE t MODIFY c INTEGER", "COMMENT ON COLUMN t.c IS 'c comment'", ) def test_alter_column_add_comment_quotes(self): context = op_fixture("oracle") op.alter_column("t", "c", type_=Integer, comment="c 'comment'") context.assert_( "ALTER TABLE t MODIFY c INTEGER", "COMMENT ON COLUMN t.c IS 'c ''comment'''", ) def test_alter_column_drop_comment(self): context = op_fixture("oracle") op.alter_column("t", "c", type_=Integer, comment=None) context.assert_( "ALTER TABLE t MODIFY c INTEGER", "COMMENT ON COLUMN t.c IS ''" ) def test_create_table_comment(self): # this is handled by SQLAlchemy's compilers context = op_fixture("oracle") op.create_table_comment("t2", comment="t2 table", schema="foo") context.assert_("COMMENT ON TABLE foo.t2 IS 't2 table'") def test_drop_table_comment(self): # this is handled by SQLAlchemy's compilers context = op_fixture("oracle") op.drop_table_comment("t2", existing_comment="t2 table", schema="foo") context.assert_("COMMENT ON TABLE foo.t2 IS ''") def test_drop_index(self): context = op_fixture("oracle") op.drop_index("my_idx", "my_table") context.assert_contains("DROP INDEX my_idx") def test_drop_column_w_default(self): context = op_fixture("oracle") op.drop_column("t1", "c1") context.assert_("ALTER TABLE t1 DROP COLUMN c1") def test_drop_column_w_check(self): context = op_fixture("oracle") op.drop_column("t1", "c1") context.assert_("ALTER TABLE t1 DROP COLUMN c1") def test_alter_column_nullable_w_existing_type(self): context = op_fixture("oracle") op.alter_column("t", "c", nullable=True, existing_type=Integer) context.assert_("ALTER TABLE t MODIFY c NULL") def test_alter_column_not_nullable_w_existing_type(self): context = op_fixture("oracle") op.alter_column("t", "c", nullable=False, existing_type=Integer) context.assert_("ALTER TABLE t MODIFY c NOT NULL") def test_alter_column_nullable_w_new_type(self): context = op_fixture("oracle") op.alter_column("t", "c", nullable=True, type_=Integer) context.assert_( "ALTER TABLE t MODIFY c NULL", "ALTER TABLE t MODIFY c INTEGER" ) def test_alter_column_not_nullable_w_new_type(self): context = op_fixture("oracle") op.alter_column("t", "c", nullable=False, type_=Integer) context.assert_( "ALTER TABLE t MODIFY c NOT NULL", "ALTER TABLE t MODIFY c INTEGER" ) def test_alter_add_server_default(self): context = op_fixture("oracle") op.alter_column("t", "c", server_default="5") context.assert_("ALTER TABLE t MODIFY c DEFAULT '5'") def test_alter_replace_server_default(self): context = op_fixture("oracle") op.alter_column( "t", "c", server_default="5", existing_server_default="6" ) context.assert_("ALTER TABLE t MODIFY c DEFAULT '5'") def test_alter_remove_server_default(self): context = op_fixture("oracle") op.alter_column("t", "c", server_default=None) context.assert_("ALTER TABLE t MODIFY c DEFAULT NULL") def test_alter_do_everything(self): context = op_fixture("oracle") op.alter_column( "t", "c", new_column_name="c2", nullable=True, type_=Integer, server_default="5", ) context.assert_( "ALTER TABLE t MODIFY c NULL", "ALTER TABLE t MODIFY c DEFAULT '5'", "ALTER TABLE t MODIFY c INTEGER", "ALTER TABLE t RENAME COLUMN c TO c2", ) @config.requirements.comments def test_create_table_with_column_comments(self): context = op_fixture("oracle") op.create_table( "t2", Column("c1", Integer, primary_key=True), comment="t2 comment" ) context.assert_( "CREATE TABLE t2 (c1 INTEGER NOT NULL, PRIMARY KEY (c1))", "COMMENT ON TABLE t2 IS 't2 comment'", ) # TODO: when we add schema support # def test_alter_column_rename_oracle_schema(self): # context = op_fixture('oracle') # op.alter_column("t", "c", name="x", schema="y") # context.assert_( # 'ALTER TABLE y.t RENAME COLUMN c TO c2' # )
true
true
f7061912c1fad41615b2d215e801104e54d2ac68
3,934
bzl
Python
tests/shell_tests.bzl
laszlocsomor/bazel-skylib
f4a2bae427c4958af834c34624767b0144f7ab12
[ "Apache-2.0" ]
null
null
null
tests/shell_tests.bzl
laszlocsomor/bazel-skylib
f4a2bae427c4958af834c34624767b0144f7ab12
[ "Apache-2.0" ]
null
null
null
tests/shell_tests.bzl
laszlocsomor/bazel-skylib
f4a2bae427c4958af834c34624767b0144f7ab12
[ "Apache-2.0" ]
null
null
null
# Copyright 2017 The Bazel Authors. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Unit tests for shell.bzl.""" load("//lib:shell.bzl", "shell") load("//lib:unittest.bzl", "asserts", "unittest") def _shell_array_literal_test(ctx): """Unit tests for shell.array_literal.""" env = unittest.begin(ctx) asserts.equals(env, "()", shell.array_literal([])) asserts.equals(env, "('1')", shell.array_literal([1])) asserts.equals(env, "('1' '2' '3')", shell.array_literal([1, 2, 3])) asserts.equals(env, "('$foo')", shell.array_literal(["$foo"])) asserts.equals(env, "('qu\"o\"te')", shell.array_literal(['qu"o"te'])) unittest.end(env) shell_array_literal_test = unittest.make(_shell_array_literal_test) def _shell_quote_test(ctx): """Unit tests for shell.quote.""" env = unittest.begin(ctx) asserts.equals(env, "'foo'", shell.quote("foo")) asserts.equals(env, "'foo bar'", shell.quote("foo bar")) asserts.equals(env, "'three spaces'", shell.quote("three spaces")) asserts.equals(env, "' leading'", shell.quote(" leading")) asserts.equals(env, "'trailing '", shell.quote("trailing ")) asserts.equals(env, "'new\nline'", shell.quote("new\nline")) asserts.equals(env, "'tab\tcharacter'", shell.quote("tab\tcharacter")) asserts.equals(env, "'$foo'", shell.quote("$foo")) asserts.equals(env, "'qu\"o\"te'", shell.quote('qu"o"te')) asserts.equals(env, "'it'\\''s'", shell.quote("it's")) asserts.equals(env, "'foo\\bar'", shell.quote("foo\\bar")) asserts.equals(env, "'back`echo q`uote'", shell.quote("back`echo q`uote")) unittest.end(env) shell_quote_test = unittest.make(_shell_quote_test) def _shell_spawn_e2e_test_impl(ctx): """Test spawning a real shell.""" args = [ "foo", "foo bar", "three spaces", " leading", "trailing ", "new\nline", "tab\tcharacter", "$foo", 'qu"o"te', "it's", "foo\\bar", "back`echo q`uote", ] script_content = "\n".join([ "#!/bin/bash", "myarray=" + shell.array_literal(args), 'output=$(echo "${myarray[@]}")', # For logging: 'echo "DEBUG: output=[${output}]" >&2', # The following is a shell representation of what the echo of the quoted # array will look like. It looks a bit confusing considering it's shell # quoted into Python. Shell using single quotes to minimize shell # escaping, so only the single quote needs to be escaped as '\'', all # others are essentially kept literally. "expected='foo foo bar three spaces leading trailing new", "line tab\tcharacter $foo qu\"o\"te it'\\''s foo\\bar back`echo q`uote'", '[[ "${output}" == "${expected}" ]]', ]) script_file = ctx.actions.declare_file("%s.sh" % (ctx.label.name)) ctx.actions.write( output = script_file, content = script_content, is_executable = True, ) return [ DefaultInfo(executable = script_file), ] shell_spawn_e2e_test = rule( test = True, implementation = _shell_spawn_e2e_test_impl, ) def shell_test_suite(): """Creates the test targets and test suite for shell.bzl tests.""" unittest.suite( "shell_tests", shell_array_literal_test, shell_quote_test, shell_spawn_e2e_test, )
36.091743
81
0.628876
load("//lib:shell.bzl", "shell") load("//lib:unittest.bzl", "asserts", "unittest") def _shell_array_literal_test(ctx): env = unittest.begin(ctx) asserts.equals(env, "()", shell.array_literal([])) asserts.equals(env, "('1')", shell.array_literal([1])) asserts.equals(env, "('1' '2' '3')", shell.array_literal([1, 2, 3])) asserts.equals(env, "('$foo')", shell.array_literal(["$foo"])) asserts.equals(env, "('qu\"o\"te')", shell.array_literal(['qu"o"te'])) unittest.end(env) shell_array_literal_test = unittest.make(_shell_array_literal_test) def _shell_quote_test(ctx): env = unittest.begin(ctx) asserts.equals(env, "'foo'", shell.quote("foo")) asserts.equals(env, "'foo bar'", shell.quote("foo bar")) asserts.equals(env, "'three spaces'", shell.quote("three spaces")) asserts.equals(env, "' leading'", shell.quote(" leading")) asserts.equals(env, "'trailing '", shell.quote("trailing ")) asserts.equals(env, "'new\nline'", shell.quote("new\nline")) asserts.equals(env, "'tab\tcharacter'", shell.quote("tab\tcharacter")) asserts.equals(env, "'$foo'", shell.quote("$foo")) asserts.equals(env, "'qu\"o\"te'", shell.quote('qu"o"te')) asserts.equals(env, "'it'\\''s'", shell.quote("it's")) asserts.equals(env, "'foo\\bar'", shell.quote("foo\\bar")) asserts.equals(env, "'back`echo q`uote'", shell.quote("back`echo q`uote")) unittest.end(env) shell_quote_test = unittest.make(_shell_quote_test) def _shell_spawn_e2e_test_impl(ctx): args = [ "foo", "foo bar", "three spaces", " leading", "trailing ", "new\nline", "tab\tcharacter", "$foo", 'qu"o"te', "it's", "foo\\bar", "back`echo q`uote", ] script_content = "\n".join([ "#!/bin/bash", "myarray=" + shell.array_literal(args), 'output=$(echo "${myarray[@]}")', # For logging: 'echo "DEBUG: output=[${output}]" >&2', # The following is a shell representation of what the echo of the quoted # array will look like. It looks a bit confusing considering it's shell # others are essentially kept literally. "expected='foo foo bar three spaces leading trailing new", "line tab\tcharacter $foo qu\"o\"te it'\\''s foo\\bar back`echo q`uote'", '[[ "${output}" == "${expected}" ]]', ]) script_file = ctx.actions.declare_file("%s.sh" % (ctx.label.name)) ctx.actions.write( output = script_file, content = script_content, is_executable = True, ) return [ DefaultInfo(executable = script_file), ] shell_spawn_e2e_test = rule( test = True, implementation = _shell_spawn_e2e_test_impl, ) def shell_test_suite(): unittest.suite( "shell_tests", shell_array_literal_test, shell_quote_test, shell_spawn_e2e_test, )
true
true
f7061945918a4538a4c2f546ccac1c0c7da56a61
1,284
py
Python
tests/test_contexts.py
jhowl01/WFSim
c96603659111964ea3bf76bbb6681eaed4d62c64
[ "BSD-3-Clause" ]
null
null
null
tests/test_contexts.py
jhowl01/WFSim
c96603659111964ea3bf76bbb6681eaed4d62c64
[ "BSD-3-Clause" ]
19
2021-12-01T11:05:12.000Z
2022-03-01T11:05:28.000Z
tests/test_contexts.py
jhowl01/WFSim
c96603659111964ea3bf76bbb6681eaed4d62c64
[ "BSD-3-Clause" ]
null
null
null
import strax import straxen import wfsim def test_nt_context(register=None, context=None): """ Test a context if it is properly setup. To this end, we perform a simple scan of the field "time" since all plugins should have this field, if there is some option specified badly, we will quickly find out since for this scan, we activate all the plugins. :param register: Register a plugin (optional) :param context: Test with some other context than the nT simulation context """ if not straxen.utilix_is_configured(): return if context is None: context = straxen.contexts.xenonnt_simulation(cmt_run_id_sim='010000', cmt_version='global_ONLINE') assert isinstance(context, strax.Context), f'{context} is not a context' if register is not None: assert issubclass(register, strax.Plugin), f'{register} is not a plugin' # Search all plugins for the time field (each should have one) context.search_field('time') # def test_mc_chain(): # test_nt_context(wfsim.RawRecordsFromMcChain) # def test_fax_nveto(): # test_nt_context(wfsim.RawRecordsFromFaxnVeto) # def test_1t_context(): # test_nt_context(context=straxen.contexts.xenon1t_simulation())
32.1
108
0.702492
import strax import straxen import wfsim def test_nt_context(register=None, context=None): if not straxen.utilix_is_configured(): return if context is None: context = straxen.contexts.xenonnt_simulation(cmt_run_id_sim='010000', cmt_version='global_ONLINE') assert isinstance(context, strax.Context), f'{context} is not a context' if register is not None: assert issubclass(register, strax.Plugin), f'{register} is not a plugin' context.search_field('time')
true
true
f706196c0ccb92b556eae5fc3117c3060a88dde4
1,730
py
Python
classify.py
gajen105/video-servillance-analysis-sysytem
c79f9010aa7c2fe00297fb0370b0635603faf135
[ "MIT" ]
null
null
null
classify.py
gajen105/video-servillance-analysis-sysytem
c79f9010aa7c2fe00297fb0370b0635603faf135
[ "MIT" ]
null
null
null
classify.py
gajen105/video-servillance-analysis-sysytem
c79f9010aa7c2fe00297fb0370b0635603faf135
[ "MIT" ]
null
null
null
import glob import datetime import inference import numpy as np flist = [] def run_classifier(): flist = [] list1 = glob.glob("./images/*.jpg") list1.sort() print("Printing the time of Interesting Events.....\n\n") temp = str(inference.run_inference_on_image()) for i in range(len(list1) - 1): inference.imagePath = list1[i] temp2 = str(inference.run_inference_on_image2()) inference.imagePath = list1[i+1] temp = str(inference.run_inference_on_image2()) if temp2 != temp: print("Time : " + str(datetime.timedelta(seconds=(i)))) flist.extend([i]) else: print("." ,) d = np.array(flist) d.sort() diff = [y - x for x, y in zip(*[iter(d)] * 2)] avg = sum(diff) / len(diff) m = [[d[0]]] for x in d[1:]: if x - m[-1][0] < avg: m[-1].append(x) else: m.append([x]) # print(m) # print(type(m)) with open('list.txt' , 'w') as f: print("Writing to file\n") for i in range(0 , (len(m))): with open('list.txt' , 'a') as f: print("\n", file=f) print("start time : " + str(datetime.timedelta(seconds = int((m[i][0])) )) , file=f) print("end time : " + str(datetime.timedelta(seconds = int((m[i][len(m[i]) - 1])) )) , file=f ) print("\n\nFinished Analysis\n\n") print("The timestanps of all interesting events is stored in a File named list.txt ") if __name__ == '__main__': run_classifier()
33.921569
111
0.483815
import glob import datetime import inference import numpy as np flist = [] def run_classifier(): flist = [] list1 = glob.glob("./images/*.jpg") list1.sort() print("Printing the time of Interesting Events.....\n\n") temp = str(inference.run_inference_on_image()) for i in range(len(list1) - 1): inference.imagePath = list1[i] temp2 = str(inference.run_inference_on_image2()) inference.imagePath = list1[i+1] temp = str(inference.run_inference_on_image2()) if temp2 != temp: print("Time : " + str(datetime.timedelta(seconds=(i)))) flist.extend([i]) else: print("." ,) d = np.array(flist) d.sort() diff = [y - x for x, y in zip(*[iter(d)] * 2)] avg = sum(diff) / len(diff) m = [[d[0]]] for x in d[1:]: if x - m[-1][0] < avg: m[-1].append(x) else: m.append([x]) with open('list.txt' , 'w') as f: print("Writing to file\n") for i in range(0 , (len(m))): with open('list.txt' , 'a') as f: print("\n", file=f) print("start time : " + str(datetime.timedelta(seconds = int((m[i][0])) )) , file=f) print("end time : " + str(datetime.timedelta(seconds = int((m[i][len(m[i]) - 1])) )) , file=f ) print("\n\nFinished Analysis\n\n") print("The timestanps of all interesting events is stored in a File named list.txt ") if __name__ == '__main__': run_classifier()
true
true
f706196f8938e963ed2643eb371c64c87e808477
9,942
py
Python
bottleCBV.py
deltaflyer/bottleCBV
6628bc31ce472a5e6398fa9dd9b2d3edccb98b84
[ "BSD-2-Clause" ]
null
null
null
bottleCBV.py
deltaflyer/bottleCBV
6628bc31ce472a5e6398fa9dd9b2d3edccb98b84
[ "BSD-2-Clause" ]
null
null
null
bottleCBV.py
deltaflyer/bottleCBV
6628bc31ce472a5e6398fa9dd9b2d3edccb98b84
[ "BSD-2-Clause" ]
null
null
null
import re import sys import inspect _py2 = sys.version_info[0] == 2 _py3 = sys.version_info[0] == 3 # noinspection PyPep8Naming class route(object): def __init__(self, rule, **options): """ Class Initializer - This will only execute if using BottleCBV's original route() style. """ # Not sure if this is needed, need to test what happens when you specify a rule but not options in BottleCBV. if not options: options = dict(method='ANY') self.rule = rule self.options = options def __call__(self, func): f = func rule = self.rule options = self.options def decorator(*_, **__): if not hasattr(f, '_rule_cache') or f._rule_cache is None: f._rule_cache = {f.__name__: [(rule, options)]} elif f.__name__ not in f._rule_cache: f._rule_cache[f.__name__] = [(rule, options)] else: f._rule_cache[f.__name__].append((rule, options)) return f return decorator() @staticmethod def decorate(f, rule, **options): if not hasattr(f, '_rule_cache') or f._rule_cache is None: f._rule_cache = {f.__name__: [(rule, options)]} elif f.__name__ not in f._rule_cache: f._rule_cache[f.__name__] = [(rule, options)] else: f._rule_cache[f.__name__].append((rule, options)) return f @staticmethod def get(rule): """ GET Method CRUD Use Case: Read Example: Request a user profile """ options = dict(method='GET') def decorator(f): return route.decorate(f, rule, **options) return decorator @staticmethod def post(rule): """ POST Method CRUD Use Case: Create Example: Create a new user """ options = dict(method='POST') def decorator(f): return route.decorate(f, rule, **options) return decorator @staticmethod def put(rule): """ PUT Method CRUD Use Case: Update / Replace Example: Set item# 4022 to Red Seedless Grapes, instead of tomatoes """ options = dict(method='PUT') def decorator(f): return route.decorate(f, rule, **options) return decorator @staticmethod def patch(rule): """ PATCH Method CRUD Use Case: Update / Modify Example: Rename then user's name from Jon to John """ options = dict(method='PATCH') def decorator(f): return route.decorate(f, rule, **options) return decorator @staticmethod def delete(rule): """ DELETE Method CRUD Use Case: Delete Example: Delete user# 12403 (John) """ options = dict(method='DELETE') def decorator(f): return route.decorate(f, rule, **options) return decorator @staticmethod def head(rule): """ HEAD Method CRUD Use Case: Read (in-part) Note: This is the same as GET, but without the response body. This is useful for items such as checking if a user exists, such as this example: Request: GET /user/12403 Response: (status code) 404 - Not Found If you are closely following the REST standard, you can also verify if the requested PATCH (update) was successfully applied, in this example: Request: PUT /user/12404 { "name": "John"} Response: (status code) 304 - Not Modified """ options = dict(method='HEAD') def decorator(f): return route.decorate(f, rule, **options) return decorator @staticmethod def any(rule): """ From the Bottle Documentation: The non-standard ANY method works as a low priority fallback: Routes that listen to ANY will match requests regardless of their HTTP method but only if no other more specific route is defined. This is helpful for proxy-routes that redirect requests to more specific sub-applications. """ options = dict(method='ANY') def decorator(f): return route.decorate(f, rule, **options) return decorator class BottleView(object): """ Class based view implementation for bottle (following flask-classy architech) """ decorators = [] DEFAULT_ROUTES = ["get", "put", "post", "delete", "index", "options"] base_route = None route_prefix = None view_identifier = "view" @classmethod def register(cls, app, base_route=None, route_prefix=None): """ Register all the possible routes of the subclass :param app: bottle app instance :param base_route: prepend to the route rule (/base_route/<class_name OR route_prefix>) :param route_prefix: used when want to register custom rule, which is not class name """ if cls is BottleView: raise TypeError("cls must be a subclass of BottleView, not BottleView itself") cls._app = app cls.route_prefix = route_prefix or cls.route_prefix cls.base_route = base_route or cls.base_route # import ipdb; ipdb.set_trace() # get all the valid members of the class to register Endpoints routes = cls._get_interesting_members(BottleView) # initialize the class klass = cls() # Iterate through class members to register Endpoints for func_name, func in routes: # print "*"*50 method_args = inspect.getargspec(func)[0] # Get rule = cls._build_route_rule(func_name, *method_args) method = "GET" if func_name in cls.DEFAULT_ROUTES: if func_name == "index": method = "GET" else: method = func_name.upper() # create name for endpoint endpoint = "%s:%s" % (cls.__name__, func_name) callable_method = getattr(klass, func_name) for decorator in cls.decorators: callable_method = decorator(callable_method) try: # noinspection PyProtectedMember custom_rule = func._rule_cache except AttributeError: method_args = inspect.getargspec(func)[0] rule = cls._build_route_rule(func_name, *method_args) method = "GET" if func_name in cls.DEFAULT_ROUTES: if func_name == "index": method = "GET" else: method = func_name.upper() cls._app.route(callback=callable_method, method=method, path=rule, name=endpoint) else: custom_rule_list = list(custom_rule.values()) if _py3: custom_rule_list = list(custom_rule_list) for cached_rule in custom_rule_list[0]: rule, options = cached_rule try: method = options.pop("method") except KeyError: method = "GET" try: endpoint = options.pop("name") except KeyError: pass cls._app.route(callback=callable_method, path=rule, method=method, name=endpoint, **options) print(("%s : %s, Endpoint: %s" % (method, rule, endpoint))) @classmethod def _build_route_rule(cls, func_name, *method_args): klass_name = cls.__name__.lower() klass_name = (klass_name[:-len(cls.view_identifier)] if klass_name.endswith(cls.view_identifier) else klass_name) rule = klass_name if not (cls.base_route or cls.route_prefix): rule = klass_name elif not cls.base_route and cls.route_prefix: rule = cls.route_prefix elif cls.base_route and not cls.route_prefix: rule = "%s/%s" % (cls.base_route, klass_name) elif cls.base_route and cls.route_prefix: rule = "%s/%s" % (cls.base_route, cls.route_prefix) rule_parts = [rule] if func_name not in cls.DEFAULT_ROUTES: rule_parts.append(func_name.replace("_", "-").lower()) ignored_rule_args = ['self'] if hasattr(cls, 'base_args'): ignored_rule_args += cls.base_args for arg in method_args: if arg not in ignored_rule_args: rule_parts.append("<%s>" % arg) result = "/%s/" % join_paths(*rule_parts) result = re.sub(r'(/)\1+', r'\1', result) result = re.sub("/{2,}", "/", result) return result @classmethod def _get_interesting_members(cls, base_class): """Returns a list of methods that can be routed to""" base_members = dir(base_class) predicate = inspect.ismethod if _py2 else inspect.isfunction all_members = inspect.getmembers(cls, predicate=predicate) return [member for member in all_members if not member[0] in base_members and ((hasattr(member[1], "__self__") and not member[1].__self__ in cls.__class__.__mro__) if _py2 else True) and not member[0].startswith("_")] def join_paths(*path_pieces): """Join parts of a url path""" # Remove blank strings, and make sure everything is a string cleaned_parts = list(map(str, [_f for _f in path_pieces if _f])) if _py3: cleaned_parts = list(cleaned_parts) return "/".join(cleaned_parts)
32.070968
117
0.565077
import re import sys import inspect _py2 = sys.version_info[0] == 2 _py3 = sys.version_info[0] == 3 class route(object): def __init__(self, rule, **options): if not options: options = dict(method='ANY') self.rule = rule self.options = options def __call__(self, func): f = func rule = self.rule options = self.options def decorator(*_, **__): if not hasattr(f, '_rule_cache') or f._rule_cache is None: f._rule_cache = {f.__name__: [(rule, options)]} elif f.__name__ not in f._rule_cache: f._rule_cache[f.__name__] = [(rule, options)] else: f._rule_cache[f.__name__].append((rule, options)) return f return decorator() @staticmethod def decorate(f, rule, **options): if not hasattr(f, '_rule_cache') or f._rule_cache is None: f._rule_cache = {f.__name__: [(rule, options)]} elif f.__name__ not in f._rule_cache: f._rule_cache[f.__name__] = [(rule, options)] else: f._rule_cache[f.__name__].append((rule, options)) return f @staticmethod def get(rule): options = dict(method='GET') def decorator(f): return route.decorate(f, rule, **options) return decorator @staticmethod def post(rule): options = dict(method='POST') def decorator(f): return route.decorate(f, rule, **options) return decorator @staticmethod def put(rule): options = dict(method='PUT') def decorator(f): return route.decorate(f, rule, **options) return decorator @staticmethod def patch(rule): options = dict(method='PATCH') def decorator(f): return route.decorate(f, rule, **options) return decorator @staticmethod def delete(rule): options = dict(method='DELETE') def decorator(f): return route.decorate(f, rule, **options) return decorator @staticmethod def head(rule): options = dict(method='HEAD') def decorator(f): return route.decorate(f, rule, **options) return decorator @staticmethod def any(rule): options = dict(method='ANY') def decorator(f): return route.decorate(f, rule, **options) return decorator class BottleView(object): decorators = [] DEFAULT_ROUTES = ["get", "put", "post", "delete", "index", "options"] base_route = None route_prefix = None view_identifier = "view" @classmethod def register(cls, app, base_route=None, route_prefix=None): if cls is BottleView: raise TypeError("cls must be a subclass of BottleView, not BottleView itself") cls._app = app cls.route_prefix = route_prefix or cls.route_prefix cls.base_route = base_route or cls.base_route routes = cls._get_interesting_members(BottleView) klass = cls() for func_name, func in routes: method_args = inspect.getargspec(func)[0] rule = cls._build_route_rule(func_name, *method_args) method = "GET" if func_name in cls.DEFAULT_ROUTES: if func_name == "index": method = "GET" else: method = func_name.upper() endpoint = "%s:%s" % (cls.__name__, func_name) callable_method = getattr(klass, func_name) for decorator in cls.decorators: callable_method = decorator(callable_method) try: custom_rule = func._rule_cache except AttributeError: method_args = inspect.getargspec(func)[0] rule = cls._build_route_rule(func_name, *method_args) method = "GET" if func_name in cls.DEFAULT_ROUTES: if func_name == "index": method = "GET" else: method = func_name.upper() cls._app.route(callback=callable_method, method=method, path=rule, name=endpoint) else: custom_rule_list = list(custom_rule.values()) if _py3: custom_rule_list = list(custom_rule_list) for cached_rule in custom_rule_list[0]: rule, options = cached_rule try: method = options.pop("method") except KeyError: method = "GET" try: endpoint = options.pop("name") except KeyError: pass cls._app.route(callback=callable_method, path=rule, method=method, name=endpoint, **options) print(("%s : %s, Endpoint: %s" % (method, rule, endpoint))) @classmethod def _build_route_rule(cls, func_name, *method_args): klass_name = cls.__name__.lower() klass_name = (klass_name[:-len(cls.view_identifier)] if klass_name.endswith(cls.view_identifier) else klass_name) rule = klass_name if not (cls.base_route or cls.route_prefix): rule = klass_name elif not cls.base_route and cls.route_prefix: rule = cls.route_prefix elif cls.base_route and not cls.route_prefix: rule = "%s/%s" % (cls.base_route, klass_name) elif cls.base_route and cls.route_prefix: rule = "%s/%s" % (cls.base_route, cls.route_prefix) rule_parts = [rule] if func_name not in cls.DEFAULT_ROUTES: rule_parts.append(func_name.replace("_", "-").lower()) ignored_rule_args = ['self'] if hasattr(cls, 'base_args'): ignored_rule_args += cls.base_args for arg in method_args: if arg not in ignored_rule_args: rule_parts.append("<%s>" % arg) result = "/%s/" % join_paths(*rule_parts) result = re.sub(r'(/)\1+', r'\1', result) result = re.sub("/{2,}", "/", result) return result @classmethod def _get_interesting_members(cls, base_class): base_members = dir(base_class) predicate = inspect.ismethod if _py2 else inspect.isfunction all_members = inspect.getmembers(cls, predicate=predicate) return [member for member in all_members if not member[0] in base_members and ((hasattr(member[1], "__self__") and not member[1].__self__ in cls.__class__.__mro__) if _py2 else True) and not member[0].startswith("_")] def join_paths(*path_pieces): cleaned_parts = list(map(str, [_f for _f in path_pieces if _f])) if _py3: cleaned_parts = list(cleaned_parts) return "/".join(cleaned_parts)
true
true
f7061a8d7e970649e89a45494fde209c8fcc08c6
4,151
py
Python
diffutils/diff.py
restwzeasy/video_organizer_scripts
9f94794a8c625055f9e6f2f0ca030604ccf3f5e4
[ "Apache-2.0" ]
null
null
null
diffutils/diff.py
restwzeasy/video_organizer_scripts
9f94794a8c625055f9e6f2f0ca030604ccf3f5e4
[ "Apache-2.0" ]
null
null
null
diffutils/diff.py
restwzeasy/video_organizer_scripts
9f94794a8c625055f9e6f2f0ca030604ccf3f5e4
[ "Apache-2.0" ]
null
null
null
#!/usr/bin/python # # Logic for computing missing and duplicate files using Postgresql DB. # import os import fnmatch # from shutil import copy2 # from shutil import copyfile from dbutils import required as db MISSING_FROM_COMP_FOLDER = "missing_from_comp" MISSING_FROM_SRC_FOLDER = "missing_from_source" FILE_NAME_COLUMN = "file" FILE_EXTENSIONS_TO_SKIP = ['.ini', '.db', '.info', '.pdfcp '] def handleDuplicates(conn): # conn = db.getConnection(dbName) cur = conn.cursor try: # duplicates on the source location cur.execute("SELECT * from filehashes WHERE ") # duplicates on the comparison location cur.execute("SELECT * from filehashes ") finally: cur.close # conn.close db.returnConnection(conn) ####################### # Logic for identifying missing files in the source and destination/comparison locations. ####################### def handleMissing(dbName, sourceSchema, compSchema): conn = db.getConnection(dbName) cur = conn.cursor() try: # identify missing files from the comparison location command = "SELECT %s from %s.filehashes where hash in (select hash from %s.filehashes except select hash from %s.filehashes);" %(FILE_NAME_COLUMN, sourceSchema, sourceSchema, compSchema) print("Identifying all missing files in comparison location. Executing: " + command) cur.execute( command ) missingInComp = cur.fetchall() # print("Missing files in comparison location: %s" % missingInComp) # __collectMissingFiles__(list(missingInComp), MISSING_FROM_COMP_FOLDER) try: for missingFile in missingInComp: __collectMissingFiles__(missingFile[0], MISSING_FROM_COMP_FOLDER) except Exception as ce: print("There was a problem locating files on comparison location. The comparison location files may have changed/moved since the scan. ") print(ce) return # identify missing files from the source location command = "SELECT %s from %s.filehashes where hash in (select hash from %s.filehashes except select hash from %s.filehashes);" % (FILE_NAME_COLUMN, compSchema, compSchema, sourceSchema) print("Identifying all missing files in source location. Executing: " + command) cur.execute(command) missingInSource = cur.fetchall() # print("Missing files in source location: %s" % missingInSource) # __collectMissingFiles__(list(missingInSource), MISSING_FROM_SRC_FOLDER) try: for missingFile in missingInSource: __collectMissingFiles__(missingFile[0], MISSING_FROM_SRC_FOLDER) except Exception as se: print("There was a problem locating files on source location. The source location files may have changed/moved since the scan. ") print(se) except Exception as e: print("Unable to identify missing files! Cause: " + e) finally: cur.close() # conn.close db.returnConnection(conn) print("Done processing missing files. Please look at %s and %s folders for missing files." % (MISSING_FROM_SRC_FOLDER, MISSING_FROM_COMP_FOLDER)) ####################### # Utility for copying all of the missing files from the specified result-set into the specified folder. ####################### def __collectMissingFiles__( missingFile, folderName ): # for missingFile in missingFiles: # missingFile.endswith(tuple(set(['.ini', '.db']))) if not missingFile.endswith(tuple(FILE_EXTENSIONS_TO_SKIP)): dst = "./" + folderName + missingFile print("Attempting to copy missing file: " + missingFile + " to destination: " + dst) if not os.path.exists(os.path.dirname(dst)): os.makedirs(os.path.dirname(dst)) # TODO implement file type filtering to only get files we want and skip ones we don't care about like *.txt, *.ini, etc. # if not fnmatch.fnmatch(missingFile, '.*.ini'): # copyfile( missingFile, dst) # copy2(missingFile, dst) os.system('cp -v -p "' + missingFile + '" "' + dst + '"')
45.615385
194
0.667309
import os import fnmatch from dbutils import required as db MISSING_FROM_COMP_FOLDER = "missing_from_comp" MISSING_FROM_SRC_FOLDER = "missing_from_source" FILE_NAME_COLUMN = "file" FILE_EXTENSIONS_TO_SKIP = ['.ini', '.db', '.info', '.pdfcp '] def handleDuplicates(conn): cur = conn.cursor try: cur.execute("SELECT * from filehashes WHERE ") cur.execute("SELECT * from filehashes ") finally: cur.close db.returnConnection(conn) def handleMissing(dbName, sourceSchema, compSchema): conn = db.getConnection(dbName) cur = conn.cursor() try: command = "SELECT %s from %s.filehashes where hash in (select hash from %s.filehashes except select hash from %s.filehashes);" %(FILE_NAME_COLUMN, sourceSchema, sourceSchema, compSchema) print("Identifying all missing files in comparison location. Executing: " + command) cur.execute( command ) missingInComp = cur.fetchall() try: for missingFile in missingInComp: __collectMissingFiles__(missingFile[0], MISSING_FROM_COMP_FOLDER) except Exception as ce: print("There was a problem locating files on comparison location. The comparison location files may have changed/moved since the scan. ") print(ce) return command = "SELECT %s from %s.filehashes where hash in (select hash from %s.filehashes except select hash from %s.filehashes);" % (FILE_NAME_COLUMN, compSchema, compSchema, sourceSchema) print("Identifying all missing files in source location. Executing: " + command) cur.execute(command) missingInSource = cur.fetchall() try: for missingFile in missingInSource: __collectMissingFiles__(missingFile[0], MISSING_FROM_SRC_FOLDER) except Exception as se: print("There was a problem locating files on source location. The source location files may have changed/moved since the scan. ") print(se) except Exception as e: print("Unable to identify missing files! Cause: " + e) finally: cur.close() db.returnConnection(conn) print("Done processing missing files. Please look at %s and %s folders for missing files." % (MISSING_FROM_SRC_FOLDER, MISSING_FROM_COMP_FOLDER)) def __collectMissingFiles__( missingFile, folderName ): if not missingFile.endswith(tuple(FILE_EXTENSIONS_TO_SKIP)): dst = "./" + folderName + missingFile print("Attempting to copy missing file: " + missingFile + " to destination: " + dst) if not os.path.exists(os.path.dirname(dst)): os.makedirs(os.path.dirname(dst)) # if not fnmatch.fnmatch(missingFile, '.*.ini'): # copyfile( missingFile, dst) # copy2(missingFile, dst) os.system('cp -v -p "' + missingFile + '" "' + dst + '"')
true
true
f7061aa1fbcf5019bdf65a6a62e5ffbf57e52458
1,400
py
Python
vsts/vsts/graph/v4_1/models/paged_graph_users.py
kenkuo/azure-devops-python-api
9e920bd25e938fa89ff7f60153e5b9e113ca839d
[ "MIT" ]
null
null
null
vsts/vsts/graph/v4_1/models/paged_graph_users.py
kenkuo/azure-devops-python-api
9e920bd25e938fa89ff7f60153e5b9e113ca839d
[ "MIT" ]
null
null
null
vsts/vsts/graph/v4_1/models/paged_graph_users.py
kenkuo/azure-devops-python-api
9e920bd25e938fa89ff7f60153e5b9e113ca839d
[ "MIT" ]
null
null
null
# -------------------------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for license information. # -------------------------------------------------------------------------------------------- # Generated file, DO NOT EDIT # Changes may cause incorrect behavior and will be lost if the code is regenerated. # -------------------------------------------------------------------------------------------- from msrest.serialization import Model class PagedGraphUsers(Model): """PagedGraphUsers. :param continuation_token: This will be non-null if there is another page of data. There will never be more than one continuation token returned by a request. :type continuation_token: list of str :param graph_users: The enumerable set of users found within a page. :type graph_users: list of :class:`GraphUser <graph.v4_1.models.GraphUser>` """ _attribute_map = { 'continuation_token': {'key': 'continuationToken', 'type': '[str]'}, 'graph_users': {'key': 'graphUsers', 'type': '[GraphUser]'} } def __init__(self, continuation_token=None, graph_users=None): super(PagedGraphUsers, self).__init__() self.continuation_token = continuation_token self.graph_users = graph_users
46.666667
162
0.575
from msrest.serialization import Model class PagedGraphUsers(Model): _attribute_map = { 'continuation_token': {'key': 'continuationToken', 'type': '[str]'}, 'graph_users': {'key': 'graphUsers', 'type': '[GraphUser]'} } def __init__(self, continuation_token=None, graph_users=None): super(PagedGraphUsers, self).__init__() self.continuation_token = continuation_token self.graph_users = graph_users
true
true
f7061d34912d236341afe1491c4e202361a09c4d
2,409
py
Python
multitask-learning/cityscapestask/decoders.py
oscarkey/multitask-learning
c4503c044ca7a29bebd4e70e9e030524654e5d00
[ "MIT" ]
44
2019-11-25T01:09:49.000Z
2022-03-29T12:04:38.000Z
multitask-learning/cityscapestask/decoders.py
oscarkey/multitask-learning
c4503c044ca7a29bebd4e70e9e030524654e5d00
[ "MIT" ]
2
2020-02-16T17:26:47.000Z
2020-03-13T15:49:47.000Z
multitask-learning/cityscapestask/decoders.py
oscarkey/multitask-learning
c4503c044ca7a29bebd4e70e9e030524654e5d00
[ "MIT" ]
5
2020-03-09T21:11:49.000Z
2021-06-04T05:20:46.000Z
"""Decoder portion of the model.""" import torch import torch.nn.functional as F from torch import nn def _build_base_decoder(): """Builds the base decoder shared by all three decoder types.""" return nn.Sequential(nn.Conv2d(in_channels=1280, out_channels=256, kernel_size=(3, 3), stride=1, padding=1), nn.BatchNorm2d(num_features=256), nn.ReLU()) class Decoders(nn.Module): """Module which contains all three decoders.""" def __init__(self, num_classes: int, enabled_tasks: (bool, bool, bool), output_size=(128, 256)): super().__init__() self._output_size = output_size self._num_classes = num_classes self._enabled_tasks = enabled_tasks self._base_semseg = _build_base_decoder() self._base_insseg = _build_base_decoder() self._base_depth = _build_base_decoder() kernel_size = (1, 1) self._semsegcls = nn.Conv2d(256, self._num_classes, kernel_size) self._inssegcls = nn.Conv2d(256, 2, kernel_size) self._depthcls = nn.Conv2d(256, 1, kernel_size) def set_output_size(self, size): self._output_size = size def forward(self, x): """Returns (sem seg, instance seg, depth).""" # x: [batch x 1280 x H/8 x W/8] sem_seg_enabled, inst_seg_enabled, depth_enabled = self._enabled_tasks if sem_seg_enabled: x1 = self._base_semseg(x) x1 = self._semsegcls(x1) x1 = F.interpolate(x1, size=self._output_size, mode='bilinear', align_corners=True) else: x1 = None if inst_seg_enabled: x2 = self._base_insseg(x) x2 = self._inssegcls(x2) x2 = F.interpolate(x2, size=self._output_size, mode='bilinear', align_corners=True) else: x2 = None if depth_enabled: x3 = self._base_depth(x) x3 = self._depthcls(x3) x3 = F.interpolate(x3, size=self._output_size, mode='bilinear', align_corners=True) else: x3 = None return x1, x2, x3 if __name__ == '__main__': # ### Shape test output_size = (123, 432) model = Decoders(num_classes=20, output_size=output_size) test = torch.zeros(size=(2, 1280, 256, 256)) result = model.forward(test) assert result[0].shape == (2, 20, *output_size), "output shape is {}".format(result[0].shape)
33.458333
112
0.624325
import torch import torch.nn.functional as F from torch import nn def _build_base_decoder(): return nn.Sequential(nn.Conv2d(in_channels=1280, out_channels=256, kernel_size=(3, 3), stride=1, padding=1), nn.BatchNorm2d(num_features=256), nn.ReLU()) class Decoders(nn.Module): def __init__(self, num_classes: int, enabled_tasks: (bool, bool, bool), output_size=(128, 256)): super().__init__() self._output_size = output_size self._num_classes = num_classes self._enabled_tasks = enabled_tasks self._base_semseg = _build_base_decoder() self._base_insseg = _build_base_decoder() self._base_depth = _build_base_decoder() kernel_size = (1, 1) self._semsegcls = nn.Conv2d(256, self._num_classes, kernel_size) self._inssegcls = nn.Conv2d(256, 2, kernel_size) self._depthcls = nn.Conv2d(256, 1, kernel_size) def set_output_size(self, size): self._output_size = size def forward(self, x): sem_seg_enabled, inst_seg_enabled, depth_enabled = self._enabled_tasks if sem_seg_enabled: x1 = self._base_semseg(x) x1 = self._semsegcls(x1) x1 = F.interpolate(x1, size=self._output_size, mode='bilinear', align_corners=True) else: x1 = None if inst_seg_enabled: x2 = self._base_insseg(x) x2 = self._inssegcls(x2) x2 = F.interpolate(x2, size=self._output_size, mode='bilinear', align_corners=True) else: x2 = None if depth_enabled: x3 = self._base_depth(x) x3 = self._depthcls(x3) x3 = F.interpolate(x3, size=self._output_size, mode='bilinear', align_corners=True) else: x3 = None return x1, x2, x3 if __name__ == '__main__': output_size = (123, 432) model = Decoders(num_classes=20, output_size=output_size) test = torch.zeros(size=(2, 1280, 256, 256)) result = model.forward(test) assert result[0].shape == (2, 20, *output_size), "output shape is {}".format(result[0].shape)
true
true
f7061e8e448f19db4b79bc0bbd1919c658ef6b7c
3,072
py
Python
Queue.py
nemesisBR/Multilevel-Queue-Scheduling
c139e4df31f6ea5e41d0a215b1fa6eff90d7daaa
[ "Apache-2.0" ]
null
null
null
Queue.py
nemesisBR/Multilevel-Queue-Scheduling
c139e4df31f6ea5e41d0a215b1fa6eff90d7daaa
[ "Apache-2.0" ]
null
null
null
Queue.py
nemesisBR/Multilevel-Queue-Scheduling
c139e4df31f6ea5e41d0a215b1fa6eff90d7daaa
[ "Apache-2.0" ]
null
null
null
class Queue: """ A class used to represent an Queue for for storing processes ... Attributes ---------- processList : list The list which would store the process currently in line for execution priority : str The type of process (Foreground,Background etc) SchedulingAlgo : object The object for corresponding scheduling algorithm to be used in this queue (FCFS,SJF etc) Methods ------- queue_enqueue(pList,process) Uses the scheduling algorithm object to insert process into process list queue_dequeue(pList,quantum) Uses the scheduling algorithm object to remove process from process list size() Returns the size of process list isEmpty() Checks whether the process list is empty or not front() Returns the first element in process list if its not empty. """ def __init__(self,type): """ Parameters ---------- type : str The type of process (Foreground,Background etc) """ self.processList = [] self.priority = type self.SchedulingAlgo = None def size(self): """Returs the total number of processes left for execution. Returns ------- int : The length of process list """ return len(self.processList) def isEmpty(self): """Checks whether all processes have been executed or not. Returns ------- bool : True if processList has 0 length , False otherwise. """ return self.size() == 0 def front(self): """Returns the first process in list Returns ------- object : The first process if process list is not empty else None """ return self.processList[0] if not self.isEmpty else None def queue_enqueue(self,process): """Uses the queue's scheduling algorithm enqueue method to insert the new process into the process list. Parameters ---------- process : object The Process class object defining the new process to insert. """ self.SchedulingAlgo.enqueue(self.processList,process) def queue_dequeue(self,quantum): """Takes the quantum time elapsed and removes processes from the process list using scheduling algorithim;s dequeue method. Parameters ---------- quantum : int The time elapsed Returns ------- removed : list A list of rocesses removed during the current cycle quantum : int Quantum time updated after being used by each process (Usually 0) """ removed,quantum = self.SchedulingAlgo.dequeue(self.processList,quantum) return removed,quantum
28.981132
98
0.556966
class Queue: def __init__(self,type): self.processList = [] self.priority = type self.SchedulingAlgo = None def size(self): return len(self.processList) def isEmpty(self): return self.size() == 0 def front(self): return self.processList[0] if not self.isEmpty else None def queue_enqueue(self,process): self.SchedulingAlgo.enqueue(self.processList,process) def queue_dequeue(self,quantum): removed,quantum = self.SchedulingAlgo.dequeue(self.processList,quantum) return removed,quantum
true
true
f7061ea583df9c0b9b86e3fa71dddfd466e63ae3
1,691
py
Python
src/decoder/decoder.py
PatrickPrakash/lipreading
6380508ba3ffad64fc01ce10a5f43e4da0f652fd
[ "MIT" ]
6
2020-09-09T23:56:14.000Z
2021-05-17T07:39:38.000Z
src/decoder/decoder.py
PatrickPrakash/lipreading
6380508ba3ffad64fc01ce10a5f43e4da0f652fd
[ "MIT" ]
4
2021-03-03T11:44:46.000Z
2021-08-23T07:40:30.000Z
src/decoder/decoder.py
PatrickPrakash/lipreading
6380508ba3ffad64fc01ce10a5f43e4da0f652fd
[ "MIT" ]
2
2021-04-01T08:18:10.000Z
2021-04-18T11:08:35.000Z
import numpy as np import editdistance class Decoder(): def __init__(self, vocab): self.vocab_list = [char for char in vocab] def predict(self, batch_size, logits, y, lengths, y_lengths, n_show=5): decoded = self.decode(logits, lengths) cursor = 0 gt = [] n = min(n_show, logits.size(1)) samples = [] for b in range(batch_size): y_str = ''.join([self.vocab_list[ch] for ch in y[cursor: cursor + y_lengths[b]]]) gt.append(y_str) cursor += y_lengths[b] if b < n: samples.append([y_str, decoded[b]]) return decoded, gt, samples def decode(self, logits, seq_lens): raise NotImplementedError def wer(self, s1, s2): s1_words, s2_words = s1.split(), s2.split() distance = editdistance.eval(s1_words, s2_words) return distance / max(len(s1_words), len(s2_words)) def cer(self, s1, s2): s1, s2 = s1.replace(' ', ''), s2.replace(' ', '') distance = editdistance.eval(s1, s2) return distance / max(len(s1), len(s2)) def cer_batch(self, decoded, gt): return self.compare_batch(decoded, gt, self.cer) def wer_batch(self, decoded, gt): return self.compare_batch(decoded, gt, self.wer) def compare_batch(self, decoded, gt, func): assert len(decoded) == len(gt), f'batch size mismatch: {len(decoded)}!={len(gt)}' results = [] for i, batch in enumerate(decoded): for sentence in range(len(batch)): error = func(decoded[i][sentence], gt[i]) results.append(error) return np.mean(results)
31.314815
93
0.579539
import numpy as np import editdistance class Decoder(): def __init__(self, vocab): self.vocab_list = [char for char in vocab] def predict(self, batch_size, logits, y, lengths, y_lengths, n_show=5): decoded = self.decode(logits, lengths) cursor = 0 gt = [] n = min(n_show, logits.size(1)) samples = [] for b in range(batch_size): y_str = ''.join([self.vocab_list[ch] for ch in y[cursor: cursor + y_lengths[b]]]) gt.append(y_str) cursor += y_lengths[b] if b < n: samples.append([y_str, decoded[b]]) return decoded, gt, samples def decode(self, logits, seq_lens): raise NotImplementedError def wer(self, s1, s2): s1_words, s2_words = s1.split(), s2.split() distance = editdistance.eval(s1_words, s2_words) return distance / max(len(s1_words), len(s2_words)) def cer(self, s1, s2): s1, s2 = s1.replace(' ', ''), s2.replace(' ', '') distance = editdistance.eval(s1, s2) return distance / max(len(s1), len(s2)) def cer_batch(self, decoded, gt): return self.compare_batch(decoded, gt, self.cer) def wer_batch(self, decoded, gt): return self.compare_batch(decoded, gt, self.wer) def compare_batch(self, decoded, gt, func): assert len(decoded) == len(gt), f'batch size mismatch: {len(decoded)}!={len(gt)}' results = [] for i, batch in enumerate(decoded): for sentence in range(len(batch)): error = func(decoded[i][sentence], gt[i]) results.append(error) return np.mean(results)
true
true
f7061f55f4a28a8f0484a0db2f3457b1d288f640
5,120
py
Python
tests/spellbook.py
surfer19/packit
39f4564ca48e39a936d8c890b03ecb922a62a11a
[ "MIT" ]
null
null
null
tests/spellbook.py
surfer19/packit
39f4564ca48e39a936d8c890b03ecb922a62a11a
[ "MIT" ]
null
null
null
tests/spellbook.py
surfer19/packit
39f4564ca48e39a936d8c890b03ecb922a62a11a
[ "MIT" ]
null
null
null
# MIT License # # Copyright (c) 2018-2019 Red Hat, 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. """ A book with our finest spells """ import shutil import subprocess from pathlib import Path from click.testing import CliRunner from packit.cli.packit_base import packit_base from packit.config import Config from packit.utils import cwd, run_command TESTS_DIR = Path(__file__).parent DATA_DIR = TESTS_DIR / "data" UPSTREAM = DATA_DIR / "upstream_git" UPSTREAM_WITH_MUTLIPLE_SOURCES = DATA_DIR / "upstream_git_with_multiple_sources" EMPTY_CHANGELOG = DATA_DIR / "empty_changelog" DISTGIT = DATA_DIR / "dist_git" UP_COCKPIT_OSTREE = DATA_DIR / "cockpit-ostree" UP_OSBUILD = DATA_DIR / "osbuild" UP_SNAPD = DATA_DIR / "snapd" TARBALL_NAME = "beerware-0.1.0.tar.gz" SOURCEGIT_UPSTREAM = DATA_DIR / "sourcegit" / "upstream" SOURCEGIT_SOURCEGIT = DATA_DIR / "sourcegit" / "source_git" DG_OGR = DATA_DIR / "dg-ogr" SPECFILE = DATA_DIR / "upstream_git/beer.spec" UPSTREAM_SPEC_NOT_IN_ROOT = DATA_DIR / "spec_not_in_root/upstream" def git_set_user_email(directory): subprocess.check_call( ["git", "config", "user.email", "test@example.com"], cwd=directory ) subprocess.check_call( ["git", "config", "user.name", "Packit Test Suite"], cwd=directory ) def get_test_config(): conf = Config() conf._pagure_user_token = "test" conf._github_token = "test" return conf def git_add_and_commit(directory, message): subprocess.check_call(["git", "add", "."], cwd=directory) subprocess.check_call(["git", "commit", "-m", message], cwd=directory) def initiate_git_repo( directory, tag=None, upstream_remote="https://lol.wat", push=False, copy_from: str = None, remotes=None, ): """ Initiate a git repo for testing. :param directory: path to the git repo :param tag: if set, tag the latest commit with this tag :param upstream_remote: name of the origin - upstream remote (not used when remotes are set) :param remotes: provide list of tuples (name, remote_url) :param push: push to the remote? :param copy_from: source tree to copy to the newly created git repo """ if remotes is None: remotes = [("origin", upstream_remote)] if copy_from: shutil.copytree(copy_from, directory) subprocess.check_call(["git", "init", "."], cwd=directory) Path(directory).joinpath("README").write_text("Best upstream project ever!") git_set_user_email(directory) subprocess.check_call(["git", "add", "."], cwd=directory) subprocess.check_call(["git", "commit", "-m", "initial commit"], cwd=directory) if tag: subprocess.check_call( ["git", "tag", "-a", "-m", f"tag {tag}, tests", tag], cwd=directory ) for name, url in remotes: subprocess.check_call(["git", "remote", "add", name, url], cwd=directory) if push: subprocess.check_call(["git", "fetch", "origin"], cwd=directory) # tox strips some env vars so your user gitconfig is not picked up # hence we need to be very explicit with git commands here subprocess.check_call( ["git", "push", "--tags", "-u", "origin", "master:master"], cwd=directory ) def prepare_dist_git_repo(directory, push=True): subprocess.check_call(["git", "branch", "f30"], cwd=directory) if push: subprocess.check_call(["git", "push", "-u", "origin", "f30:f30"], cwd=directory) def call_packit(fnc=None, parameters=None, envs=None, working_dir=None): working_dir = working_dir or "." fnc = fnc or packit_base runner = CliRunner() envs = envs or {} parameters = parameters or [] # catch exceptions enables debugger with cwd(working_dir): return runner.invoke(fnc, args=parameters, env=envs, catch_exceptions=False) def build_srpm(path: Path): run_command(["rpmbuild", "--rebuild", str(path)]) def can_a_module_be_imported(module_name): """ can a module be imported? """ try: __import__(module_name) return True except ImportError: return False
34.594595
96
0.698242
import shutil import subprocess from pathlib import Path from click.testing import CliRunner from packit.cli.packit_base import packit_base from packit.config import Config from packit.utils import cwd, run_command TESTS_DIR = Path(__file__).parent DATA_DIR = TESTS_DIR / "data" UPSTREAM = DATA_DIR / "upstream_git" UPSTREAM_WITH_MUTLIPLE_SOURCES = DATA_DIR / "upstream_git_with_multiple_sources" EMPTY_CHANGELOG = DATA_DIR / "empty_changelog" DISTGIT = DATA_DIR / "dist_git" UP_COCKPIT_OSTREE = DATA_DIR / "cockpit-ostree" UP_OSBUILD = DATA_DIR / "osbuild" UP_SNAPD = DATA_DIR / "snapd" TARBALL_NAME = "beerware-0.1.0.tar.gz" SOURCEGIT_UPSTREAM = DATA_DIR / "sourcegit" / "upstream" SOURCEGIT_SOURCEGIT = DATA_DIR / "sourcegit" / "source_git" DG_OGR = DATA_DIR / "dg-ogr" SPECFILE = DATA_DIR / "upstream_git/beer.spec" UPSTREAM_SPEC_NOT_IN_ROOT = DATA_DIR / "spec_not_in_root/upstream" def git_set_user_email(directory): subprocess.check_call( ["git", "config", "user.email", "test@example.com"], cwd=directory ) subprocess.check_call( ["git", "config", "user.name", "Packit Test Suite"], cwd=directory ) def get_test_config(): conf = Config() conf._pagure_user_token = "test" conf._github_token = "test" return conf def git_add_and_commit(directory, message): subprocess.check_call(["git", "add", "."], cwd=directory) subprocess.check_call(["git", "commit", "-m", message], cwd=directory) def initiate_git_repo( directory, tag=None, upstream_remote="https://lol.wat", push=False, copy_from: str = None, remotes=None, ): if remotes is None: remotes = [("origin", upstream_remote)] if copy_from: shutil.copytree(copy_from, directory) subprocess.check_call(["git", "init", "."], cwd=directory) Path(directory).joinpath("README").write_text("Best upstream project ever!") git_set_user_email(directory) subprocess.check_call(["git", "add", "."], cwd=directory) subprocess.check_call(["git", "commit", "-m", "initial commit"], cwd=directory) if tag: subprocess.check_call( ["git", "tag", "-a", "-m", f"tag {tag}, tests", tag], cwd=directory ) for name, url in remotes: subprocess.check_call(["git", "remote", "add", name, url], cwd=directory) if push: subprocess.check_call(["git", "fetch", "origin"], cwd=directory) subprocess.check_call( ["git", "push", "--tags", "-u", "origin", "master:master"], cwd=directory ) def prepare_dist_git_repo(directory, push=True): subprocess.check_call(["git", "branch", "f30"], cwd=directory) if push: subprocess.check_call(["git", "push", "-u", "origin", "f30:f30"], cwd=directory) def call_packit(fnc=None, parameters=None, envs=None, working_dir=None): working_dir = working_dir or "." fnc = fnc or packit_base runner = CliRunner() envs = envs or {} parameters = parameters or [] with cwd(working_dir): return runner.invoke(fnc, args=parameters, env=envs, catch_exceptions=False) def build_srpm(path: Path): run_command(["rpmbuild", "--rebuild", str(path)]) def can_a_module_be_imported(module_name): try: __import__(module_name) return True except ImportError: return False
true
true
f70620bb1f5c1fbc24e7c0565115d4e788803b4e
2,709
py
Python
orders/models.py
garcia116/ecommerce-django
6c416b20446f0daba5774040715313ae28501cf9
[ "PostgreSQL" ]
null
null
null
orders/models.py
garcia116/ecommerce-django
6c416b20446f0daba5774040715313ae28501cf9
[ "PostgreSQL" ]
null
null
null
orders/models.py
garcia116/ecommerce-django
6c416b20446f0daba5774040715313ae28501cf9
[ "PostgreSQL" ]
null
null
null
from django.db import models from accounts.models import Account from store.models import Product, Variation # Create your models here. class Payment(models.Model): user = models.ForeignKey(Account, on_delete=models.CASCADE) payment_id = models.CharField(max_length=100) payment_method = models.CharField(max_length=100) amount_paid = models.CharField(max_length=100) status = models.CharField(max_length=100) created_at = models.DateTimeField(auto_now_add=True) def __str__(self): return self.payment_id class Order(models.Model): STATUS = ( ('New', 'New'), ('Accepted', 'Accepted'), ('Completed', 'Completed'), ('Cancelled', 'Cancelled'), ) user = models.ForeignKey(Account, on_delete=models.SET_NULL, null=True) payment = models.ForeignKey(Payment, on_delete=models.SET_NULL, blank=True, null=True) order_number = models.CharField(max_length=20) first_name = models.CharField(max_length=50) last_name= models.CharField(max_length=50) phone = models.CharField(max_length=15) email = models.EmailField(max_length=50) address_line_1 = models.CharField(max_length=50) address_line_2 = models.CharField(max_length=50, blank=True) country = models.CharField(max_length=50) state = models.CharField(max_length=50) city = models.CharField(max_length=50) order_note = models.CharField(max_length=100, blank=True) order_total = models.FloatField() tax = models.FloatField() status = models.CharField(max_length=10, choices=STATUS, default='New') ip = models.CharField(blank=True, max_length=20) is_ordered = models.BooleanField(default=False) created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) def full_name(self): return f'{self.first_name} {self.last_name}' def full_address(self): return f'{self.address_line_1} {self.address_line_2}' def __str__(self): return self.first_name class OrderProduct(models.Model): order = models.ForeignKey(Order, on_delete=models.CASCADE) payment = models.ForeignKey(Payment, on_delete=models.SET_NULL, blank=True, null=True) user = models.ForeignKey(Account, on_delete=models.CASCADE) product = models.ForeignKey(Product, on_delete=models.CASCADE) variations = models.ManyToManyField(Variation, blank=True) quantity = models.IntegerField() product_price = models.FloatField() ordered = models.BooleanField(default=False) created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) def __str__(self): return self.product.product_name
38.15493
90
0.727206
from django.db import models from accounts.models import Account from store.models import Product, Variation class Payment(models.Model): user = models.ForeignKey(Account, on_delete=models.CASCADE) payment_id = models.CharField(max_length=100) payment_method = models.CharField(max_length=100) amount_paid = models.CharField(max_length=100) status = models.CharField(max_length=100) created_at = models.DateTimeField(auto_now_add=True) def __str__(self): return self.payment_id class Order(models.Model): STATUS = ( ('New', 'New'), ('Accepted', 'Accepted'), ('Completed', 'Completed'), ('Cancelled', 'Cancelled'), ) user = models.ForeignKey(Account, on_delete=models.SET_NULL, null=True) payment = models.ForeignKey(Payment, on_delete=models.SET_NULL, blank=True, null=True) order_number = models.CharField(max_length=20) first_name = models.CharField(max_length=50) last_name= models.CharField(max_length=50) phone = models.CharField(max_length=15) email = models.EmailField(max_length=50) address_line_1 = models.CharField(max_length=50) address_line_2 = models.CharField(max_length=50, blank=True) country = models.CharField(max_length=50) state = models.CharField(max_length=50) city = models.CharField(max_length=50) order_note = models.CharField(max_length=100, blank=True) order_total = models.FloatField() tax = models.FloatField() status = models.CharField(max_length=10, choices=STATUS, default='New') ip = models.CharField(blank=True, max_length=20) is_ordered = models.BooleanField(default=False) created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) def full_name(self): return f'{self.first_name} {self.last_name}' def full_address(self): return f'{self.address_line_1} {self.address_line_2}' def __str__(self): return self.first_name class OrderProduct(models.Model): order = models.ForeignKey(Order, on_delete=models.CASCADE) payment = models.ForeignKey(Payment, on_delete=models.SET_NULL, blank=True, null=True) user = models.ForeignKey(Account, on_delete=models.CASCADE) product = models.ForeignKey(Product, on_delete=models.CASCADE) variations = models.ManyToManyField(Variation, blank=True) quantity = models.IntegerField() product_price = models.FloatField() ordered = models.BooleanField(default=False) created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) def __str__(self): return self.product.product_name
true
true
f706212a3c277558269064ef25bec2d7e49ac9ec
573
py
Python
backend/Clout9/celery.py
landdafku11/mobile-backend
3f3328afd81f85f90170a57689af72f8f705b8a3
[ "MIT" ]
null
null
null
backend/Clout9/celery.py
landdafku11/mobile-backend
3f3328afd81f85f90170a57689af72f8f705b8a3
[ "MIT" ]
null
null
null
backend/Clout9/celery.py
landdafku11/mobile-backend
3f3328afd81f85f90170a57689af72f8f705b8a3
[ "MIT" ]
null
null
null
from __future__ import absolute_import import os from celery import Celery from django.conf import settings # set the default Django settings module for the 'celery' program. os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Clout9.settings.production") app = Celery('Clout9') # Using a string here means the worker will not have to # pickle the object when using Windows. app.config_from_object('django.conf:settings') # app.autodiscover_tasks(lambda: settings.INSTALLED_APPS) @app.task(bind=True) def debug_task(self): print('Request: {0!r}'.format(self.request))
30.157895
77
0.78534
from __future__ import absolute_import import os from celery import Celery from django.conf import settings os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Clout9.settings.production") app = Celery('Clout9') app.config_from_object('django.conf:settings') @app.task(bind=True) def debug_task(self): print('Request: {0!r}'.format(self.request))
true
true
f70621467fab8e1d22e5d59550b5a4137d4f315b
876
py
Python
chapter-7/blog/asgi.py
PacktPublishing/Real-time-Django
07480a089fc0880d752d4ee5740ae6587de93aee
[ "MIT" ]
null
null
null
chapter-7/blog/asgi.py
PacktPublishing/Real-time-Django
07480a089fc0880d752d4ee5740ae6587de93aee
[ "MIT" ]
null
null
null
chapter-7/blog/asgi.py
PacktPublishing/Real-time-Django
07480a089fc0880d752d4ee5740ae6587de93aee
[ "MIT" ]
null
null
null
# blog/asgi.py import os import django os.environ.setdefault("DJANGO_SETTINGS_MODULE", "blog.settings") from django.conf import settings django.setup() from django.core.asgi import get_asgi_application from channels.security.websocket import OriginValidator from channels.auth import AuthMiddlewareStack from channels.routing import ProtocolTypeRouter, URLRouter from django.urls import re_path from app.website.consumers import ExampleConsumer application = ProtocolTypeRouter( { # Django's ASGI application to handle traditional HTTP requests "http": get_asgi_application(), # WebSocket handler "websocket": OriginValidator(AuthMiddlewareStack( URLRouter( [ re_path(r"^ws/example/$", ExampleConsumer.as_asgi()), ] ) ), settings.ALLOWED_HOSTS) } )
29.2
73
0.700913
import os import django os.environ.setdefault("DJANGO_SETTINGS_MODULE", "blog.settings") from django.conf import settings django.setup() from django.core.asgi import get_asgi_application from channels.security.websocket import OriginValidator from channels.auth import AuthMiddlewareStack from channels.routing import ProtocolTypeRouter, URLRouter from django.urls import re_path from app.website.consumers import ExampleConsumer application = ProtocolTypeRouter( { "http": get_asgi_application(), # WebSocket handler "websocket": OriginValidator(AuthMiddlewareStack( URLRouter( [ re_path(r"^ws/example/$", ExampleConsumer.as_asgi()), ] ) ), settings.ALLOWED_HOSTS) } )
true
true
f70621a35ffd5247cdc20090ec6e58e9b19c730c
284
py
Python
Ex 85.py
brunobendel/Exercicios-python-Pycharm
145ded6cb5533aeef1b89f0bce20f0a90e37216c
[ "MIT" ]
null
null
null
Ex 85.py
brunobendel/Exercicios-python-Pycharm
145ded6cb5533aeef1b89f0bce20f0a90e37216c
[ "MIT" ]
null
null
null
Ex 85.py
brunobendel/Exercicios-python-Pycharm
145ded6cb5533aeef1b89f0bce20f0a90e37216c
[ "MIT" ]
null
null
null
valor = 0 num = [[],[]] for c in range (0,7): valor = int(input(f'Digite o {c+1}° valor:')) if valor % 2 == 0: num[0].append(valor) else: num[1].append(valor) print('-='*30) print(f'Os numeros pares foram {num[0]}\nOs numeros ímpares foram {num[1]}.')
18.933333
77
0.545775
valor = 0 num = [[],[]] for c in range (0,7): valor = int(input(f'Digite o {c+1}° valor:')) if valor % 2 == 0: num[0].append(valor) else: num[1].append(valor) print('-='*30) print(f'Os numeros pares foram {num[0]}\nOs numeros ímpares foram {num[1]}.')
true
true
f70622f46e8557156d215966f8f5d50845b2482d
5,316
py
Python
node_modules/@ensdomains/dnsprovejs/docs/conf.py
anantaramdas/ddns-action
6cf4832f441e617908b4ae980ac8da05766e4245
[ "MIT" ]
null
null
null
node_modules/@ensdomains/dnsprovejs/docs/conf.py
anantaramdas/ddns-action
6cf4832f441e617908b4ae980ac8da05766e4245
[ "MIT" ]
null
null
null
node_modules/@ensdomains/dnsprovejs/docs/conf.py
anantaramdas/ddns-action
6cf4832f441e617908b4ae980ac8da05766e4245
[ "MIT" ]
null
null
null
# -*- coding: utf-8 -*- # # Configuration file for the Sphinx documentation builder. # # This file does only contain a selection of the most common options. For a # full list see the documentation: # http://www.sphinx-doc.org/en/master/config # -- Path setup -------------------------------------------------------------- # If extensions (or modules to document with autodoc) are in another directory, # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. # # import os # import sys # sys.path.insert(0, os.path.abspath('.')) # -- Project information ----------------------------------------------------- project = 'dnsprovejs' copyright = '2018, Makoto Inoue' author = 'Makoto Inoue' # The short X.Y version version = '' # The full version, including alpha/beta/rc tags release = '' # -- General configuration --------------------------------------------------- # If your documentation needs a minimal Sphinx version, state it here. # # needs_sphinx = '1.0' # Add any Sphinx extension module names here, as strings. They can be # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom # ones. extensions = ['sphinx_js', 'sphinx.ext.autodoc'] root_for_relative_js_paths = '../lib' js_source_path = ['../lib', '../lib/oracle', '../lib/dns'] primary_domain = 'js' # Add any paths that contain templates here, relative to this directory. templates_path = ['_templates'] # The suffix(es) of source filenames. # You can specify multiple suffix as a list of string: # source_suffix = ['.rst', '.md'] # The master toctree document. master_doc = 'index' # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. # # This is also used if you do content translation via gettext catalogs. # Usually you set "language" from the command line for these cases. language = None # List of patterns, relative to source directory, that match files and # directories to ignore when looking for source files. # This pattern also affects html_static_path and html_extra_path. exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store'] # The name of the Pygments (syntax highlighting) style to use. pygments_style = None # -- Options for HTML output ------------------------------------------------- # The theme to use for HTML and HTML Help pages. See the documentation for # a list of builtin themes. # # html_theme = 'alabaster' html_theme = 'sphinx_rtd_theme' # Theme options are theme-specific and customize the look and feel of a theme # further. For a list of options available for each theme, see the # documentation. # # html_theme_options = {} # Add any paths that contain custom static files (such as style sheets) here, # relative to this directory. They are copied after the builtin static files, # so a file named "default.css" will overwrite the builtin "default.css". html_static_path = ['_static'] # Custom sidebar templates, must be a dictionary that maps document names # to template names. # # The default sidebars (for documents that don't match any pattern) are # defined by theme itself. Builtin themes are using these templates by # default: ``['localtoc.html', 'relations.html', 'sourcelink.html', # 'searchbox.html']``. # # html_sidebars = {} # -- Options for HTMLHelp output --------------------------------------------- # Output file base name for HTML help builder. htmlhelp_basename = 'dnsprovejsdoc' # -- Options for LaTeX output ------------------------------------------------ latex_elements = { # The paper size ('letterpaper' or 'a4paper'). # # 'papersize': 'letterpaper', # The font size ('10pt', '11pt' or '12pt'). # # 'pointsize': '10pt', # Additional stuff for the LaTeX preamble. # # 'preamble': '', # Latex figure (float) alignment # # 'figure_align': 'htbp', } # Grouping the document tree into LaTeX files. List of tuples # (source start file, target name, title, # author, documentclass [howto, manual, or own class]). latex_documents = [ (master_doc, 'dnsprovejs.tex', 'dnsprovejs Documentation', 'Makoto Inoue', 'manual'), ] # -- Options for manual page output ------------------------------------------ # One entry per manual page. List of tuples # (source start file, name, description, authors, manual section). man_pages = [ (master_doc, 'dnsprovejs', 'dnsprovejs Documentation', [author], 1) ] # -- Options for Texinfo output ---------------------------------------------- # Grouping the document tree into Texinfo files. List of tuples # (source start file, target name, title, author, # dir menu entry, description, category) texinfo_documents = [ (master_doc, 'dnsprovejs', 'dnsprovejs Documentation', author, 'dnsprovejs', 'One line description of project.', 'Miscellaneous'), ] # -- Options for Epub output ------------------------------------------------- # Bibliographic Dublin Core info. epub_title = project # The unique identifier of the text. This can be a ISBN number # or the project homepage. # # epub_identifier = '' # A unique identification for the text. # # epub_uid = '' # A list of files that should not be packed into the epub file. epub_exclude_files = ['search.html']
30.204545
79
0.655192
project = 'dnsprovejs' copyright = '2018, Makoto Inoue' author = 'Makoto Inoue' version = '' release = '' extensions = ['sphinx_js', 'sphinx.ext.autodoc'] root_for_relative_js_paths = '../lib' js_source_path = ['../lib', '../lib/oracle', '../lib/dns'] primary_domain = 'js' templates_path = ['_templates'] source_suffix = ['.rst', '.md'] master_doc = 'index' language = None exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store'] pygments_style = None html_theme = 'sphinx_rtd_theme' html_static_path = ['_static'] # defined by theme itself. Builtin themes are using these templates by # default: ``['localtoc.html', 'relations.html', 'sourcelink.html', # 'searchbox.html']``. # # html_sidebars = {} # -- Options for HTMLHelp output --------------------------------------------- # Output file base name for HTML help builder. htmlhelp_basename = 'dnsprovejsdoc' # -- Options for LaTeX output ------------------------------------------------ latex_elements = { # The paper size ('letterpaper' or 'a4paper'). # # 'papersize': 'letterpaper', # The font size ('10pt', '11pt' or '12pt'). # # 'pointsize': '10pt', # Additional stuff for the LaTeX preamble. # # 'preamble': '', # Latex figure (float) alignment # # 'figure_align': 'htbp', } # Grouping the document tree into LaTeX files. List of tuples # (source start file, target name, title, # author, documentclass [howto, manual, or own class]). latex_documents = [ (master_doc, 'dnsprovejs.tex', 'dnsprovejs Documentation', 'Makoto Inoue', 'manual'), ] # -- Options for manual page output ------------------------------------------ # One entry per manual page. List of tuples # (source start file, name, description, authors, manual section). man_pages = [ (master_doc, 'dnsprovejs', 'dnsprovejs Documentation', [author], 1) ] # -- Options for Texinfo output ---------------------------------------------- # Grouping the document tree into Texinfo files. List of tuples # (source start file, target name, title, author, # dir menu entry, description, category) texinfo_documents = [ (master_doc, 'dnsprovejs', 'dnsprovejs Documentation', author, 'dnsprovejs', 'One line description of project.', 'Miscellaneous'), ] # -- Options for Epub output ------------------------------------------------- # Bibliographic Dublin Core info. epub_title = project # The unique identifier of the text. This can be a ISBN number # or the project homepage. # # epub_identifier = '' # A unique identification for the text. # # epub_uid = '' # A list of files that should not be packed into the epub file. epub_exclude_files = ['search.html']
true
true
f70623d04a953f9c608f06a3e97fc29b80626cba
5,650
py
Python
benchmarks/misc/membound.py
kxxt/taichi
15f39b79c258080f1e34fcbdc29646d9ced0a4fe
[ "MIT" ]
15
2020-01-29T19:07:19.000Z
2021-05-12T02:53:22.000Z
benchmarks/misc/membound.py
kxxt/taichi
15f39b79c258080f1e34fcbdc29646d9ced0a4fe
[ "MIT" ]
1
2020-02-08T02:11:58.000Z
2020-02-08T02:11:58.000Z
benchmarks/misc/membound.py
kxxt/taichi
15f39b79c258080f1e34fcbdc29646d9ced0a4fe
[ "MIT" ]
2
2020-01-31T20:10:35.000Z
2021-03-16T07:51:59.000Z
import os import time from membound_cases import memory_bound_cases_list from utils import (arch_name, datatime_with_format, dtype2str, dump2json, geometric_mean, md_table_header, scaled_repeat_times, size2str) import taichi as ti class MemoryBound: suite_name = 'memorybound' supported_archs = [ti.x64, ti.cuda] test_cases = memory_bound_cases_list test_dtype_list = [ti.i32, ti.i64, ti.f32, ti.f64] test_dsize_list = [ (4**i) * 1024 # kibibytes(KiB) = 1024 for i in range(1, 10) # [4KB,16KB...256MB] ] basic_repeat_times = 10 evaluator = [geometric_mean] def __init__(self, arch): self._arch = arch self._cases_impl = [] for case in self.test_cases: for dtype in self.test_dtype_list: impl = CaseImpl(case, arch, dtype, self.test_dsize_list, self.evaluator) self._cases_impl.append(impl) def run(self): for case in self._cases_impl: case.run() def save_as_json(self, arch_dir='./'): #folder of suite suite_path = os.path.join(arch_dir, self.suite_name) os.makedirs(suite_path) #json files self._save_suite_info_as_json(suite_path) self._save_cases_info_as_json(suite_path) def save_as_markdown(self, arch_dir='./'): current_time = datatime_with_format() commit_hash = ti.core.get_commit_hash() #[:8] file_name = f'{self.suite_name}.md' file_path = os.path.join(arch_dir, file_name) with open(file_path, 'w') as f: lines = [ f'commit_hash: {commit_hash}\n', f'datatime: {current_time}\n' ] lines += self._get_markdown_lines() for line in lines: print(line, file=f) def _save_suite_info_as_json(self, suite_path='./'): info_dict = { 'cases': [func.__name__ for func in self.test_cases], 'dtype': [dtype2str(dtype) for dtype in self.test_dtype_list], 'dsize': [size for size in self.test_dsize_list], 'repeat': [ scaled_repeat_times(self._arch, size, self.basic_repeat_times) for size in self.test_dsize_list ], 'evaluator': [func.__name__ for func in self.evaluator] } info_path = os.path.join(suite_path, '_info.json') with open(info_path, 'w') as f: print(dump2json(info_dict), file=f) def _save_cases_info_as_json(self, suite_path='./'): for case in self.test_cases: #for case [fill,saxpy,reduction] results_dict = {} for impl in self._cases_impl: #find [ti.i32, ti.i64, ti.f32, ti.f64] if impl._name != case.__name__: continue result_name = dtype2str(impl._test_dtype) results_dict[result_name] = impl.get_results_dict() case_path = os.path.join(suite_path, (case.__name__ + '.json')) with open(case_path, 'w') as f: case_str = dump2json(results_dict) print(case_str, file=f) def _get_markdown_lines(self): lines = [] lines += md_table_header(self.suite_name, self._arch, self.test_dsize_list, self.basic_repeat_times, self.evaluator) result_header = '|kernel elapsed time(ms)' + ''.join( '|' for i in range( len(self.test_dsize_list) + len(MemoryBound.evaluator))) lines += [result_header] for case in self._cases_impl: lines += case.get_markdown_lines() lines.append('') return lines class CaseImpl: def __init__(self, func, arch, test_dtype, test_dsize_list, evaluator): self._func = func self._name = func.__name__ self._arch = arch self._test_dtype = test_dtype self._test_dsize_list = test_dsize_list self._min_time_in_us = [] #test results self._evaluator = evaluator def run(self): ti.init(kernel_profiler=True, arch=self._arch) print("TestCase[%s.%s.%s]" % (self._func.__name__, arch_name( self._arch), dtype2str(self._test_dtype))) for test_dsize in self._test_dsize_list: print("test_dsize = %s" % (size2str(test_dsize))) self._min_time_in_us.append( self._func(self._arch, self._test_dtype, test_dsize, MemoryBound.basic_repeat_times)) time.sleep(0.2) ti.reset() def get_markdown_lines(self): string = '|' + self._name + '.' + dtype2str(self._test_dtype) + '|' string += ''.join( str(round(time, 4)) + '|' for time in self._min_time_in_us) string += ''.join( str(round(item(self._min_time_in_us), 4)) + '|' for item in self._evaluator) return [string] def get_results_dict(self): results_dict = {} for i in range(len(self._test_dsize_list)): dsize = self._test_dsize_list[i] repeat = scaled_repeat_times(self._arch, dsize, MemoryBound.basic_repeat_times) elapsed_time = self._min_time_in_us[i] item_name = size2str(dsize).replace('.0', '') item_dict = { 'dsize_byte': dsize, 'repeat': repeat, 'elapsed_time_ms': elapsed_time } results_dict[item_name] = item_dict return results_dict
38.175676
81
0.578938
import os import time from membound_cases import memory_bound_cases_list from utils import (arch_name, datatime_with_format, dtype2str, dump2json, geometric_mean, md_table_header, scaled_repeat_times, size2str) import taichi as ti class MemoryBound: suite_name = 'memorybound' supported_archs = [ti.x64, ti.cuda] test_cases = memory_bound_cases_list test_dtype_list = [ti.i32, ti.i64, ti.f32, ti.f64] test_dsize_list = [ (4**i) * 1024 for i in range(1, 10) ] basic_repeat_times = 10 evaluator = [geometric_mean] def __init__(self, arch): self._arch = arch self._cases_impl = [] for case in self.test_cases: for dtype in self.test_dtype_list: impl = CaseImpl(case, arch, dtype, self.test_dsize_list, self.evaluator) self._cases_impl.append(impl) def run(self): for case in self._cases_impl: case.run() def save_as_json(self, arch_dir='./'): suite_path = os.path.join(arch_dir, self.suite_name) os.makedirs(suite_path) self._save_suite_info_as_json(suite_path) self._save_cases_info_as_json(suite_path) def save_as_markdown(self, arch_dir='./'): current_time = datatime_with_format() commit_hash = ti.core.get_commit_hash() file_name = f'{self.suite_name}.md' file_path = os.path.join(arch_dir, file_name) with open(file_path, 'w') as f: lines = [ f'commit_hash: {commit_hash}\n', f'datatime: {current_time}\n' ] lines += self._get_markdown_lines() for line in lines: print(line, file=f) def _save_suite_info_as_json(self, suite_path='./'): info_dict = { 'cases': [func.__name__ for func in self.test_cases], 'dtype': [dtype2str(dtype) for dtype in self.test_dtype_list], 'dsize': [size for size in self.test_dsize_list], 'repeat': [ scaled_repeat_times(self._arch, size, self.basic_repeat_times) for size in self.test_dsize_list ], 'evaluator': [func.__name__ for func in self.evaluator] } info_path = os.path.join(suite_path, '_info.json') with open(info_path, 'w') as f: print(dump2json(info_dict), file=f) def _save_cases_info_as_json(self, suite_path='./'): for case in self.test_cases: results_dict = {} for impl in self._cases_impl: if impl._name != case.__name__: continue result_name = dtype2str(impl._test_dtype) results_dict[result_name] = impl.get_results_dict() case_path = os.path.join(suite_path, (case.__name__ + '.json')) with open(case_path, 'w') as f: case_str = dump2json(results_dict) print(case_str, file=f) def _get_markdown_lines(self): lines = [] lines += md_table_header(self.suite_name, self._arch, self.test_dsize_list, self.basic_repeat_times, self.evaluator) result_header = '|kernel elapsed time(ms)' + ''.join( '|' for i in range( len(self.test_dsize_list) + len(MemoryBound.evaluator))) lines += [result_header] for case in self._cases_impl: lines += case.get_markdown_lines() lines.append('') return lines class CaseImpl: def __init__(self, func, arch, test_dtype, test_dsize_list, evaluator): self._func = func self._name = func.__name__ self._arch = arch self._test_dtype = test_dtype self._test_dsize_list = test_dsize_list self._min_time_in_us = [] self._evaluator = evaluator def run(self): ti.init(kernel_profiler=True, arch=self._arch) print("TestCase[%s.%s.%s]" % (self._func.__name__, arch_name( self._arch), dtype2str(self._test_dtype))) for test_dsize in self._test_dsize_list: print("test_dsize = %s" % (size2str(test_dsize))) self._min_time_in_us.append( self._func(self._arch, self._test_dtype, test_dsize, MemoryBound.basic_repeat_times)) time.sleep(0.2) ti.reset() def get_markdown_lines(self): string = '|' + self._name + '.' + dtype2str(self._test_dtype) + '|' string += ''.join( str(round(time, 4)) + '|' for time in self._min_time_in_us) string += ''.join( str(round(item(self._min_time_in_us), 4)) + '|' for item in self._evaluator) return [string] def get_results_dict(self): results_dict = {} for i in range(len(self._test_dsize_list)): dsize = self._test_dsize_list[i] repeat = scaled_repeat_times(self._arch, dsize, MemoryBound.basic_repeat_times) elapsed_time = self._min_time_in_us[i] item_name = size2str(dsize).replace('.0', '') item_dict = { 'dsize_byte': dsize, 'repeat': repeat, 'elapsed_time_ms': elapsed_time } results_dict[item_name] = item_dict return results_dict
true
true
f706245900cc076c316732f0b5d3e8eaea28b692
1,909
py
Python
freenit/api/user.py
Veskor/backend-1
e7063da73fe39ad03dbd63edb512c1a7b5cc3809
[ "BSD-2-Clause" ]
1
2019-12-24T19:41:37.000Z
2019-12-24T19:41:37.000Z
freenit/api/user.py
cbarrettninja/freenit-backend
12d765214c0eb79abacb2cf2a5e5687c1e4365e8
[ "BSD-2-Clause" ]
null
null
null
freenit/api/user.py
cbarrettninja/freenit-backend
12d765214c0eb79abacb2cf2a5e5687c1e4365e8
[ "BSD-2-Clause" ]
null
null
null
from flask_security.utils import hash_password from flask_smorest import Blueprint, abort from ..models.user import User from ..schemas.paging import PageInSchema, paginate from ..schemas.user import UserPageOutSchema, UserSchema from .methodviews import ProtectedMethodView blueprint = Blueprint('users', 'user') @blueprint.route('', endpoint='list') class UserListAPI(ProtectedMethodView): @blueprint.arguments(PageInSchema(), location='headers') @blueprint.response(UserPageOutSchema) def get(self, pagination): """List users""" return paginate(User.select(), pagination) @blueprint.arguments(UserSchema) @blueprint.response(UserSchema) def post(self, args): """Create user""" user = User(**args) user.password = hash_password(user.password) user.save() return user @blueprint.route('/<user_id>', endpoint='detail') class UserAPI(ProtectedMethodView): @blueprint.response(UserSchema) def get(self, user_id): """Get user details""" try: user = User.get(id=user_id) except User.DoesNotExist: abort(404, message='User not found') return user @blueprint.arguments(UserSchema(partial=True)) @blueprint.response(UserSchema) def patch(self, args, user_id): try: user = User.get(id=user_id) except User.DoesNotExist: abort(404, message='User not found') for field in args: setattr(user, field, args[field]) if 'password' in args: user.password = hash_password(user.password) user.save() return user @blueprint.response(UserSchema) def delete(self, user_id): try: user = User.get(id=user_id) except User.DoesNotExist: abort(404, message='User not found') user.delete_instance() return user
30.301587
60
0.647459
from flask_security.utils import hash_password from flask_smorest import Blueprint, abort from ..models.user import User from ..schemas.paging import PageInSchema, paginate from ..schemas.user import UserPageOutSchema, UserSchema from .methodviews import ProtectedMethodView blueprint = Blueprint('users', 'user') @blueprint.route('', endpoint='list') class UserListAPI(ProtectedMethodView): @blueprint.arguments(PageInSchema(), location='headers') @blueprint.response(UserPageOutSchema) def get(self, pagination): return paginate(User.select(), pagination) @blueprint.arguments(UserSchema) @blueprint.response(UserSchema) def post(self, args): user = User(**args) user.password = hash_password(user.password) user.save() return user @blueprint.route('/<user_id>', endpoint='detail') class UserAPI(ProtectedMethodView): @blueprint.response(UserSchema) def get(self, user_id): try: user = User.get(id=user_id) except User.DoesNotExist: abort(404, message='User not found') return user @blueprint.arguments(UserSchema(partial=True)) @blueprint.response(UserSchema) def patch(self, args, user_id): try: user = User.get(id=user_id) except User.DoesNotExist: abort(404, message='User not found') for field in args: setattr(user, field, args[field]) if 'password' in args: user.password = hash_password(user.password) user.save() return user @blueprint.response(UserSchema) def delete(self, user_id): try: user = User.get(id=user_id) except User.DoesNotExist: abort(404, message='User not found') user.delete_instance() return user
true
true
f70624a43131fb5c8d69861a9f04112e2b1e1ec5
1,982
py
Python
Selection_sort.py
anantvikram/General_Code
8c5d1e97111be84edc029a77d257f87bdb615ee5
[ "MIT" ]
null
null
null
Selection_sort.py
anantvikram/General_Code
8c5d1e97111be84edc029a77d257f87bdb615ee5
[ "MIT" ]
null
null
null
Selection_sort.py
anantvikram/General_Code
8c5d1e97111be84edc029a77d257f87bdb615ee5
[ "MIT" ]
null
null
null
# -*- coding: utf-8 -*- """ Created on Fri Dec 22 18:44:02 2017 @author: Tirthajyoti Sarkar Simple selection sort with counter for total number of operations (finding minimum and swapping) Accepts user input on minimum and maximum bound of the array and the size of the array. """ import random def find_min(array): n=len(array) r = array[0] count=0 for i in range(1,n): count+=1 if r>array[i]: r=array[i] return(r,count) def selection_sort(array): n=len(array) num_op=0 # Iterate over the length of the array, pushing smaller values to the left for i in range(n): # Scan the array from i-th element (where the iterator is currently) to the end for minimum m,c_min=find_min(array[i:n]) # IMPORTANT: Get the index of the minimum element w.r.t. to the main array m_index=array[i:n].index(m)+i # If the first element of the unsorted portion i.e. i-th element> minimum, then SWAP if (array[i]>m): # Print statement for examining minimum and its index, Troubleshooting #print("Minimum found {} at position {}. Swapping positions {} and {}".format(m,m_index,i,m_index)) temp=array[i] array[i]=m array[m_index]=temp num_op+=(c_min+1) print(array) else: pass return (array,num_op) # User inputs for generating the random arrays mini = int(input("Enter the minimum bound:")) maxi = int(input("Enter the maximum bound:")) num = int(input("Enter the size:")) # Create random array based on user-specified minimum/maximum bounds and number of elements a= [] for i in range(num): a.append(random.randint(mini,maxi)) print("\nInitial array:",a) # Get the sorted array back along with the count of # of operations it took to sort sorted_array,n_op=selection_sort(a) print("Sorted array: {}\nTook {} operations".format(sorted_array,n_op))
34.77193
111
0.644803
import random def find_min(array): n=len(array) r = array[0] count=0 for i in range(1,n): count+=1 if r>array[i]: r=array[i] return(r,count) def selection_sort(array): n=len(array) num_op=0 for i in range(n): m,c_min=find_min(array[i:n]) m_index=array[i:n].index(m)+i if (array[i]>m): temp=array[i] array[i]=m array[m_index]=temp num_op+=(c_min+1) print(array) else: pass return (array,num_op) mini = int(input("Enter the minimum bound:")) maxi = int(input("Enter the maximum bound:")) num = int(input("Enter the size:")) a= [] for i in range(num): a.append(random.randint(mini,maxi)) print("\nInitial array:",a) sorted_array,n_op=selection_sort(a) print("Sorted array: {}\nTook {} operations".format(sorted_array,n_op))
true
true
f706251629adbf9ea80984700c7870d348561269
1,003
py
Python
xlsxwriter/test/comparison/test_hyperlink23.py
hugovk/XlsxWriter
e97cc66637d9895480ee32cfb5e561d652d3787b
[ "BSD-2-Clause" ]
null
null
null
xlsxwriter/test/comparison/test_hyperlink23.py
hugovk/XlsxWriter
e97cc66637d9895480ee32cfb5e561d652d3787b
[ "BSD-2-Clause" ]
null
null
null
xlsxwriter/test/comparison/test_hyperlink23.py
hugovk/XlsxWriter
e97cc66637d9895480ee32cfb5e561d652d3787b
[ "BSD-2-Clause" ]
null
null
null
############################################################################### # # Tests for XlsxWriter. # # SPDX-License-Identifier: BSD-2-Clause # Copyright (c), 2013-2022, John McNamara, jmcnamara@cpan.org # from ..excel_comparison_test import ExcelComparisonTest from ...workbook import Workbook class TestCompareXLSXFiles(ExcelComparisonTest): """ Test file created by XlsxWriter against a file created by Excel. """ def setUp(self): self.set_filename('hyperlink23.xlsx') def test_create_file(self): """Test the creation of a simple XlsxWriter file with hyperlinks.""" workbook = Workbook(self.got_filename) # Turn off default URL format for testing. workbook.default_url_format = None worksheet = workbook.add_worksheet() worksheet.write_url('A1', 'https://en.wikipedia.org/wiki/Microsoft_Excel#Data_storage_and_communication', None, 'Display text') workbook.close() self.assertExcelEqual()
26.394737
135
0.645065
from ..excel_comparison_test import ExcelComparisonTest from ...workbook import Workbook class TestCompareXLSXFiles(ExcelComparisonTest): def setUp(self): self.set_filename('hyperlink23.xlsx') def test_create_file(self): workbook = Workbook(self.got_filename) workbook.default_url_format = None worksheet = workbook.add_worksheet() worksheet.write_url('A1', 'https://en.wikipedia.org/wiki/Microsoft_Excel#Data_storage_and_communication', None, 'Display text') workbook.close() self.assertExcelEqual()
true
true
f70625880e8c93bd1889d0674cd79c69a847e672
2,340
py
Python
src/util/match_tags.py
billhuang56/job-miner
472945be442b3ab5080a28bbcdc8e57653cd2af0
[ "MIT" ]
null
null
null
src/util/match_tags.py
billhuang56/job-miner
472945be442b3ab5080a28bbcdc8e57653cd2af0
[ "MIT" ]
null
null
null
src/util/match_tags.py
billhuang56/job-miner
472945be442b3ab5080a28bbcdc8e57653cd2af0
[ "MIT" ]
null
null
null
import boto3 import pickle import logging from datetime import date from pyspark.sql.functions import udf from pyspark.sql.types import ArrayType, StringType from generate_common_words import dump_pickle import config as conf ''' Assign Top 500 Stackoverflow tags to job postings ''' # ===== Logger Configs ===== TS = date.today().strftime('%y%m%d') logger = logging.getLogger('jd_logger') logger.setLevel(logging.INFO) fh = logging.FileHandler(conf.LOG_DIR + TS + '_batch_process.log') fh.setLevel(logging.INFO) logger.addHandler(fh) def read_pickle(bucket, key): s3 = boto3.resource('s3') pickled = pickle.loads(s3.Bucket(bucket)\ .Object(key)\ .get()['Body']\ .read()) return pickled def filter_common_words(tokens, word_list): """ Filter out words that appear in many of the documents Args: tokens: a list of word lemmas word_list: a list of common words Returns: a set of word lemmas with the common words removed """ return list(set([word for word in tokens if word not in word_list])) def select_tag_words(tokens, tag_list): """ Match Stackoverflow tags to word lemmas Args: tokens: a list of word lemmas tag_list: a list of tags Returns: a list of tags for each job posting """ return [tag for tag in tag_list if tag in tokens] def assign_tags(jd): """ Assign Stackoverflow tags and construct a set of keywords Args: jd: cleaned job posting dataframe Returns: a dataframe with columns containing keywords and tags """ logger.info('[STARTING]: Assigning Tags') common_words = read_pickle(conf.PARQUET_BUCKET, conf.COMMON_WORDS_PATH) stack_tags = read_pickle(conf.PARQUET_BUCKET, conf.TAG_PATH) logger.info('[PROCESSING]: Removing Common Words') cw_remover = udf(lambda body: filter_common_words(body, common_words), ArrayType(StringType())) jd_keywords = jd.withColumn('keywords', cw_remover('stemmed')) logger.info('[PROCESSING]: Getting Tags') tagger = udf(lambda body: select_tag_words(body, stack_tags), ArrayType(StringType())) jd_tags = jd_keywords.withColumn('tags', tagger('keywords')) logger.info('[Finished]: Assigning Tags') return jd_tags
32.5
99
0.67906
import boto3 import pickle import logging from datetime import date from pyspark.sql.functions import udf from pyspark.sql.types import ArrayType, StringType from generate_common_words import dump_pickle import config as conf TS = date.today().strftime('%y%m%d') logger = logging.getLogger('jd_logger') logger.setLevel(logging.INFO) fh = logging.FileHandler(conf.LOG_DIR + TS + '_batch_process.log') fh.setLevel(logging.INFO) logger.addHandler(fh) def read_pickle(bucket, key): s3 = boto3.resource('s3') pickled = pickle.loads(s3.Bucket(bucket)\ .Object(key)\ .get()['Body']\ .read()) return pickled def filter_common_words(tokens, word_list): return list(set([word for word in tokens if word not in word_list])) def select_tag_words(tokens, tag_list): return [tag for tag in tag_list if tag in tokens] def assign_tags(jd): logger.info('[STARTING]: Assigning Tags') common_words = read_pickle(conf.PARQUET_BUCKET, conf.COMMON_WORDS_PATH) stack_tags = read_pickle(conf.PARQUET_BUCKET, conf.TAG_PATH) logger.info('[PROCESSING]: Removing Common Words') cw_remover = udf(lambda body: filter_common_words(body, common_words), ArrayType(StringType())) jd_keywords = jd.withColumn('keywords', cw_remover('stemmed')) logger.info('[PROCESSING]: Getting Tags') tagger = udf(lambda body: select_tag_words(body, stack_tags), ArrayType(StringType())) jd_tags = jd_keywords.withColumn('tags', tagger('keywords')) logger.info('[Finished]: Assigning Tags') return jd_tags
true
true
f7062614191aea318baf15492ba8710c185f1d76
398
py
Python
simplerest/wsgi.py
dunnock/simplerest
aecf9ff97a4d5c075f5ce95a2da8900793d73f37
[ "MIT" ]
null
null
null
simplerest/wsgi.py
dunnock/simplerest
aecf9ff97a4d5c075f5ce95a2da8900793d73f37
[ "MIT" ]
null
null
null
simplerest/wsgi.py
dunnock/simplerest
aecf9ff97a4d5c075f5ce95a2da8900793d73f37
[ "MIT" ]
null
null
null
""" WSGI config for simplerest project. It exposes the WSGI callable as a module-level variable named ``application``. For more information on this file, see https://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/ """ import os from django.core.wsgi import get_wsgi_application os.environ.setdefault("DJANGO_SETTINGS_MODULE", "simplerest.settings") application = get_wsgi_application()
23.411765
78
0.788945
import os from django.core.wsgi import get_wsgi_application os.environ.setdefault("DJANGO_SETTINGS_MODULE", "simplerest.settings") application = get_wsgi_application()
true
true
f706268a2eb5993c2788bc2e86f7c473ce41d33f
1,852
py
Python
pom_tracker/views/export_poms.py
YorBoyBlue/PomTracker
8de73a21fc910e8796ee2666d2f47a17f8f93def
[ "Apache-2.0" ]
null
null
null
pom_tracker/views/export_poms.py
YorBoyBlue/PomTracker
8de73a21fc910e8796ee2666d2f47a17f8f93def
[ "Apache-2.0" ]
null
null
null
pom_tracker/views/export_poms.py
YorBoyBlue/PomTracker
8de73a21fc910e8796ee2666d2f47a17f8f93def
[ "Apache-2.0" ]
null
null
null
import json import os from mako.template import Template from models.pomodoro_model import PomodoroModel from datetime import datetime class ExportPomsResource: def on_get(self, req, resp): """Handles GET requests""" resp.content_type = 'text/html' dir_path = os.path.dirname(os.path.realpath(__file__)) user_login_template = Template( filename=dir_path + '/export_poms_view.mako') resp.body = user_login_template.render() def on_post(self, req, resp): """Handles POST requests""" start_date = req.get_param('start_date') end_date = req.get_param('end_date') # Query poms within start and end dates poms = req.context['session'].query( PomodoroModel).filter(PomodoroModel.created <= end_date). \ filter(PomodoroModel.created >= start_date).filter_by( user_id=req.context['user'].id).order_by( PomodoroModel.created, PomodoroModel.start_time).all() data = {'poms': []} for row in poms: pom = { 'created': datetime.strftime(row.created, '%Y-%m-%d'), 'title': row.task, 'start_time': row.start_time.strftime('%I:%M%p'), 'end_time': row.end_time.strftime('%I:%M%p'), 'distractions': row.distractions, 'pom_success': row.pom_success, 'review': row.review, 'flags': [] } for flag in row.flags: pom['flags'].append(flag.flag_type) data['poms'].append(pom) filename = str(start_date) + '-' + str( end_date) + '_Arin_Pom_Sheets.json' resp.body = json.dumps(data, indent=2) resp.downloadable_as = filename resp.content_type = 'application/octet-stream'
34.943396
71
0.583153
import json import os from mako.template import Template from models.pomodoro_model import PomodoroModel from datetime import datetime class ExportPomsResource: def on_get(self, req, resp): resp.content_type = 'text/html' dir_path = os.path.dirname(os.path.realpath(__file__)) user_login_template = Template( filename=dir_path + '/export_poms_view.mako') resp.body = user_login_template.render() def on_post(self, req, resp): start_date = req.get_param('start_date') end_date = req.get_param('end_date') poms = req.context['session'].query( PomodoroModel).filter(PomodoroModel.created <= end_date). \ filter(PomodoroModel.created >= start_date).filter_by( user_id=req.context['user'].id).order_by( PomodoroModel.created, PomodoroModel.start_time).all() data = {'poms': []} for row in poms: pom = { 'created': datetime.strftime(row.created, '%Y-%m-%d'), 'title': row.task, 'start_time': row.start_time.strftime('%I:%M%p'), 'end_time': row.end_time.strftime('%I:%M%p'), 'distractions': row.distractions, 'pom_success': row.pom_success, 'review': row.review, 'flags': [] } for flag in row.flags: pom['flags'].append(flag.flag_type) data['poms'].append(pom) filename = str(start_date) + '-' + str( end_date) + '_Arin_Pom_Sheets.json' resp.body = json.dumps(data, indent=2) resp.downloadable_as = filename resp.content_type = 'application/octet-stream'
true
true
f706278d4ad701dd1b2914e8ac7dcf2e2fdf786e
3,634
py
Python
code/convert_game.py
bws0013/retrosheet-to-sql
a6219281f3e5dab9bf9bc9e742ccf133002e758d
[ "Apache-2.0" ]
null
null
null
code/convert_game.py
bws0013/retrosheet-to-sql
a6219281f3e5dab9bf9bc9e742ccf133002e758d
[ "Apache-2.0" ]
null
null
null
code/convert_game.py
bws0013/retrosheet-to-sql
a6219281f3e5dab9bf9bc9e742ccf133002e758d
[ "Apache-2.0" ]
null
null
null
import csv import json def read_data_file(filename): all_records = [] with open(filename, mode='r') as csv_file: csv_reader = csv.reader(csv_file) row_count = 0 for row in csv_reader: all_records.append(row) return all_records def parse_play_fields(play_fields): plays = [] for p in play_fields: temp = {} temp["event_count"] = int(p[0]) temp["inning_number"] = int(p[1]) temp["home_field_indicator"] = int(p[2]) temp["player_code"] = p[3] temp["count_at_action"] = p[4] temp["all_pitches"] = p[5] temp["play_events"] = p[6] plays.append(temp) return plays def parse_sub_fields(sub_fields): subs = [] for s in sub_fields: temp = {} temp["event_count"] = int(s[0]) temp["player_code"] = s[1] temp["name"] = s[2] temp["home_field_indicator"] = int(s[3]) temp["batting_order"] = int(s[4]) temp["field_position"] = int(s[5]) subs.append(temp) return subs def parse_info_fields(info_fields): infos = [] for i in info_fields: temp = {} temp[i[0]] = i[1] infos.append(temp) return infos def parse_start_fields(start_fields): starts = [] for s in start_fields: temp = {} temp["player_code"] = s[0] temp["name"] = s[1] temp["home_field_indicator"] = int(s[2]) temp["batting_order"] = int(s[3]) temp["field_position"] = int(s[4]) starts.append(temp) return starts def parse_data_fields(data_fields): datas = [] for d in data_fields: temp = {} temp["type"] = d[0] temp["player_code"] = d[1] temp["earned_runs"] = int(d[2]) datas.append(temp) return datas def get_fields(records): id = "" version = "" play = [] info = [] start = [] data = [] sub = [] event_count = 0 for r in records: if r[0] == "play": r[0] = event_count event_count += 1 play.append(r) elif r[0] == "info": info.append(r[1:]) elif r[0] == "start": start.append(r[1:]) elif r[0] == "data": data.append(r[1:]) elif r[0] == "sub": r[0] = event_count event_count += 1 sub.append(r) elif r[0] == "com": continue # This one we should ignore elif r[0].endswith("adj"): continue # This one we should ignore elif r[0] == "id": id = r[1] elif r[0] == "version": version = r[1] else: print("ERROR") print(r) return id, version, play, info, start, data, sub def get_game(game_records): id, version, plays, infos, starts, datas, subs = get_fields(game_records) play_list = parse_play_fields(plays) info_list = parse_info_fields(infos) start_list = parse_start_fields(starts) data_list = parse_data_fields(datas) sub_list = parse_sub_fields(subs) game = {} game["id"] = id game["version"] = version game["plays"] = play_list game["info"] = info_list game["start"] = start_list game["data"] = data_list game["subs"] = sub_list return game def get_all_games(all_game_records): all_games = [] for g in all_game_records: all_games.append(get_game(g)) return json.dumps(all_games) # path = "./../storage/ANA201808100.in" # game_records = read_data_file(path) # game = get_game(game_records) # # out = json.loads(game) # print(out["id"])
25.957143
77
0.548431
import csv import json def read_data_file(filename): all_records = [] with open(filename, mode='r') as csv_file: csv_reader = csv.reader(csv_file) row_count = 0 for row in csv_reader: all_records.append(row) return all_records def parse_play_fields(play_fields): plays = [] for p in play_fields: temp = {} temp["event_count"] = int(p[0]) temp["inning_number"] = int(p[1]) temp["home_field_indicator"] = int(p[2]) temp["player_code"] = p[3] temp["count_at_action"] = p[4] temp["all_pitches"] = p[5] temp["play_events"] = p[6] plays.append(temp) return plays def parse_sub_fields(sub_fields): subs = [] for s in sub_fields: temp = {} temp["event_count"] = int(s[0]) temp["player_code"] = s[1] temp["name"] = s[2] temp["home_field_indicator"] = int(s[3]) temp["batting_order"] = int(s[4]) temp["field_position"] = int(s[5]) subs.append(temp) return subs def parse_info_fields(info_fields): infos = [] for i in info_fields: temp = {} temp[i[0]] = i[1] infos.append(temp) return infos def parse_start_fields(start_fields): starts = [] for s in start_fields: temp = {} temp["player_code"] = s[0] temp["name"] = s[1] temp["home_field_indicator"] = int(s[2]) temp["batting_order"] = int(s[3]) temp["field_position"] = int(s[4]) starts.append(temp) return starts def parse_data_fields(data_fields): datas = [] for d in data_fields: temp = {} temp["type"] = d[0] temp["player_code"] = d[1] temp["earned_runs"] = int(d[2]) datas.append(temp) return datas def get_fields(records): id = "" version = "" play = [] info = [] start = [] data = [] sub = [] event_count = 0 for r in records: if r[0] == "play": r[0] = event_count event_count += 1 play.append(r) elif r[0] == "info": info.append(r[1:]) elif r[0] == "start": start.append(r[1:]) elif r[0] == "data": data.append(r[1:]) elif r[0] == "sub": r[0] = event_count event_count += 1 sub.append(r) elif r[0] == "com": continue elif r[0].endswith("adj"): continue elif r[0] == "id": id = r[1] elif r[0] == "version": version = r[1] else: print("ERROR") print(r) return id, version, play, info, start, data, sub def get_game(game_records): id, version, plays, infos, starts, datas, subs = get_fields(game_records) play_list = parse_play_fields(plays) info_list = parse_info_fields(infos) start_list = parse_start_fields(starts) data_list = parse_data_fields(datas) sub_list = parse_sub_fields(subs) game = {} game["id"] = id game["version"] = version game["plays"] = play_list game["info"] = info_list game["start"] = start_list game["data"] = data_list game["subs"] = sub_list return game def get_all_games(all_game_records): all_games = [] for g in all_game_records: all_games.append(get_game(g)) return json.dumps(all_games)
true
true
f70627ba6ce4ce8b453aaf72d8c2afb1dbfc110d
8,589
py
Python
src/android/toga_android/widgets/detailedlist.py
luizoti/toga
3c49e685f325f1aba2ce048b253402d7e4519f97
[ "BSD-3-Clause" ]
3
2017-10-05T10:25:51.000Z
2021-07-09T09:34:00.000Z
src/android/toga_android/widgets/detailedlist.py
luizoti/toga
3c49e685f325f1aba2ce048b253402d7e4519f97
[ "BSD-3-Clause" ]
1
2017-11-18T02:53:35.000Z
2017-11-18T03:38:31.000Z
src/android/toga_android/widgets/detailedlist.py
luizoti/toga
3c49e685f325f1aba2ce048b253402d7e4519f97
[ "BSD-3-Clause" ]
null
null
null
from rubicon.java.android_events import Handler, PythonRunnable from rubicon.java.jni import java from travertino.size import at_least from ..libs.android import R__color from ..libs.android.graphics import BitmapFactory, Rect from ..libs.android.view import Gravity, OnClickListener, View__MeasureSpec from ..libs.android.widget import ( ImageView, ImageView__ScaleType, LinearLayout, LinearLayout__LayoutParams, RelativeLayout, RelativeLayout__LayoutParams, ScrollView, TextView ) from ..libs.androidx.swiperefreshlayout import ( SwipeRefreshLayout, SwipeRefreshLayout__OnRefreshListener ) from .base import Widget class DetailedListOnClickListener(OnClickListener): def __init__(self, impl, row_number): super().__init__() self._impl = impl self._row_number = row_number def onClick(self, _view): row = self._impl.interface.data[self._row_number] self._impl._selection = row if self._impl.interface.on_select: self._impl.interface.on_select(self._impl.interface, row=self._impl.interface.data[self._row_number]) class OnRefreshListener(SwipeRefreshLayout__OnRefreshListener): def __init__(self, interface): super().__init__() self._interface = interface def onRefresh(self): if self._interface.on_refresh: self._interface.on_refresh(self._interface) class DetailedList(Widget): ROW_HEIGHT = 250 _swipe_refresh_layout = None _scroll_view = None _dismissable_container = None _selection = None def create(self): # DetailedList is not a specific widget on Android, so we build it out # of a few pieces. if self.native is None: self.native = LinearLayout(self._native_activity) self.native.setOrientation(LinearLayout.VERTICAL) else: # If create() is called a second time, clear the widget and regenerate it. self.native.removeAllViews() scroll_view = ScrollView(self._native_activity) self._scroll_view = ScrollView( __jni__=java.NewGlobalRef(scroll_view)) scroll_view_layout_params = LinearLayout__LayoutParams( LinearLayout__LayoutParams.MATCH_PARENT, LinearLayout__LayoutParams.MATCH_PARENT ) scroll_view_layout_params.gravity = Gravity.TOP swipe_refresh_wrapper = SwipeRefreshLayout(self._native_activity) swipe_refresh_wrapper.setOnRefreshListener(OnRefreshListener(self.interface)) self._swipe_refresh_layout = SwipeRefreshLayout( __jni__=java.NewGlobalRef(swipe_refresh_wrapper)) swipe_refresh_wrapper.addView(scroll_view) self.native.addView(swipe_refresh_wrapper, scroll_view_layout_params) dismissable_container = LinearLayout(self._native_activity) self._dismissable_container = LinearLayout( __jni__=java.NewGlobalRef(dismissable_container) ) dismissable_container.setOrientation(LinearLayout.VERTICAL) dismissable_container_params = LinearLayout__LayoutParams( LinearLayout__LayoutParams.MATCH_PARENT, LinearLayout__LayoutParams.MATCH_PARENT ) scroll_view.addView( dismissable_container, dismissable_container_params ) for i in range(len((self.interface.data or []))): self._make_row(dismissable_container, i) def _make_row(self, container, i): # Create the foreground. row_foreground = RelativeLayout(self._native_activity) container.addView(row_foreground) # Add user-provided icon to layout. icon_image_view = ImageView(self._native_activity) icon = self.interface.data[i].icon if icon is not None: icon.bind(self.interface.factory) bitmap = BitmapFactory.decodeFile(str(icon._impl.path)) icon_image_view.setImageBitmap(bitmap) icon_layout_params = RelativeLayout__LayoutParams( RelativeLayout__LayoutParams.WRAP_CONTENT, RelativeLayout__LayoutParams.WRAP_CONTENT) icon_layout_params.width = 150 icon_layout_params.setMargins(25, 0, 25, 0) icon_layout_params.height = self.ROW_HEIGHT icon_image_view.setScaleType(ImageView__ScaleType.FIT_CENTER) row_foreground.addView(icon_image_view, icon_layout_params) # Create layout to show top_text and bottom_text. text_container = LinearLayout(self._native_activity) text_container_params = RelativeLayout__LayoutParams( RelativeLayout__LayoutParams.WRAP_CONTENT, RelativeLayout__LayoutParams.WRAP_CONTENT) text_container_params.height = self.ROW_HEIGHT text_container_params.setMargins(25 + 25 + 150, 0, 0, 0) row_foreground.addView(text_container, text_container_params) text_container.setOrientation(LinearLayout.VERTICAL) text_container.setWeightSum(2.0) # Create top & bottom text; add them to layout. top_text = TextView(self._native_activity) top_text.setText(str(getattr(self.interface.data[i], 'title', ''))) top_text.setTextSize(20.0) top_text.setTextColor(self._native_activity.getResources().getColor(R__color.black)) bottom_text = TextView(self._native_activity) bottom_text.setTextColor(self._native_activity.getResources().getColor(R__color.black)) bottom_text.setText(str(getattr(self.interface.data[i], 'subtitle', ''))) bottom_text.setTextSize(16.0) top_text_params = LinearLayout__LayoutParams( RelativeLayout__LayoutParams.WRAP_CONTENT, RelativeLayout__LayoutParams.MATCH_PARENT) top_text_params.weight = 1.0 top_text.setGravity(Gravity.BOTTOM) text_container.addView(top_text, top_text_params) bottom_text_params = LinearLayout__LayoutParams( RelativeLayout__LayoutParams.WRAP_CONTENT, RelativeLayout__LayoutParams.MATCH_PARENT) bottom_text_params.weight = 1.0 bottom_text.setGravity(Gravity.TOP) bottom_text_params.gravity = Gravity.TOP text_container.addView(bottom_text, bottom_text_params) # Apply an onclick listener so that clicking anywhere on the row triggers Toga's on_select(row). row_foreground.setOnClickListener(DetailedListOnClickListener(self, i)) def change_source(self, source): # If the source changes, re-build the widget. self.create() def set_on_refresh(self, handler): # No special handling needed. pass def after_on_refresh(self): if self._swipe_refresh_layout: self._swipe_refresh_layout.setRefreshing(False) def insert(self, index, item): # If the data changes, re-build the widget. Brutally effective. self.create() def change(self, item): # If the data changes, re-build the widget. Brutally effective. self.create() def remove(self, index, item): # If the data changes, re-build the widget. Brutally effective. self.create() def clear(self): # If the data changes, re-build the widget. Brutally effective. self.create() def get_selection(self): return self._selection def set_on_select(self, handler): # No special handling required. pass def set_on_delete(self, handler): # This widget currently does not implement event handlers for data change. self.interface.factory.not_implemented("DetailedList.set_on_delete()") def scroll_to_row(self, row): def scroll(): row_obj = self._dismissable_container.getChildAt(row) hit_rect = Rect() row_obj.getHitRect(hit_rect) self._scroll_view.requestChildRectangleOnScreen( self._dismissable_container, hit_rect, False, ) Handler().post(PythonRunnable(scroll)) def rehint(self): # Android can crash when rendering some widgets until they have their layout params set. Guard for that case. if self.native.getLayoutParams() is None: return self.native.measure( View__MeasureSpec.UNSPECIFIED, View__MeasureSpec.UNSPECIFIED, ) self.interface.intrinsic.width = at_least(self.native.getMeasuredWidth()) self.interface.intrinsic.height = self.native.getMeasuredHeight()
40.323944
117
0.692048
from rubicon.java.android_events import Handler, PythonRunnable from rubicon.java.jni import java from travertino.size import at_least from ..libs.android import R__color from ..libs.android.graphics import BitmapFactory, Rect from ..libs.android.view import Gravity, OnClickListener, View__MeasureSpec from ..libs.android.widget import ( ImageView, ImageView__ScaleType, LinearLayout, LinearLayout__LayoutParams, RelativeLayout, RelativeLayout__LayoutParams, ScrollView, TextView ) from ..libs.androidx.swiperefreshlayout import ( SwipeRefreshLayout, SwipeRefreshLayout__OnRefreshListener ) from .base import Widget class DetailedListOnClickListener(OnClickListener): def __init__(self, impl, row_number): super().__init__() self._impl = impl self._row_number = row_number def onClick(self, _view): row = self._impl.interface.data[self._row_number] self._impl._selection = row if self._impl.interface.on_select: self._impl.interface.on_select(self._impl.interface, row=self._impl.interface.data[self._row_number]) class OnRefreshListener(SwipeRefreshLayout__OnRefreshListener): def __init__(self, interface): super().__init__() self._interface = interface def onRefresh(self): if self._interface.on_refresh: self._interface.on_refresh(self._interface) class DetailedList(Widget): ROW_HEIGHT = 250 _swipe_refresh_layout = None _scroll_view = None _dismissable_container = None _selection = None def create(self): if self.native is None: self.native = LinearLayout(self._native_activity) self.native.setOrientation(LinearLayout.VERTICAL) else: self.native.removeAllViews() scroll_view = ScrollView(self._native_activity) self._scroll_view = ScrollView( __jni__=java.NewGlobalRef(scroll_view)) scroll_view_layout_params = LinearLayout__LayoutParams( LinearLayout__LayoutParams.MATCH_PARENT, LinearLayout__LayoutParams.MATCH_PARENT ) scroll_view_layout_params.gravity = Gravity.TOP swipe_refresh_wrapper = SwipeRefreshLayout(self._native_activity) swipe_refresh_wrapper.setOnRefreshListener(OnRefreshListener(self.interface)) self._swipe_refresh_layout = SwipeRefreshLayout( __jni__=java.NewGlobalRef(swipe_refresh_wrapper)) swipe_refresh_wrapper.addView(scroll_view) self.native.addView(swipe_refresh_wrapper, scroll_view_layout_params) dismissable_container = LinearLayout(self._native_activity) self._dismissable_container = LinearLayout( __jni__=java.NewGlobalRef(dismissable_container) ) dismissable_container.setOrientation(LinearLayout.VERTICAL) dismissable_container_params = LinearLayout__LayoutParams( LinearLayout__LayoutParams.MATCH_PARENT, LinearLayout__LayoutParams.MATCH_PARENT ) scroll_view.addView( dismissable_container, dismissable_container_params ) for i in range(len((self.interface.data or []))): self._make_row(dismissable_container, i) def _make_row(self, container, i): row_foreground = RelativeLayout(self._native_activity) container.addView(row_foreground) icon_image_view = ImageView(self._native_activity) icon = self.interface.data[i].icon if icon is not None: icon.bind(self.interface.factory) bitmap = BitmapFactory.decodeFile(str(icon._impl.path)) icon_image_view.setImageBitmap(bitmap) icon_layout_params = RelativeLayout__LayoutParams( RelativeLayout__LayoutParams.WRAP_CONTENT, RelativeLayout__LayoutParams.WRAP_CONTENT) icon_layout_params.width = 150 icon_layout_params.setMargins(25, 0, 25, 0) icon_layout_params.height = self.ROW_HEIGHT icon_image_view.setScaleType(ImageView__ScaleType.FIT_CENTER) row_foreground.addView(icon_image_view, icon_layout_params) text_container = LinearLayout(self._native_activity) text_container_params = RelativeLayout__LayoutParams( RelativeLayout__LayoutParams.WRAP_CONTENT, RelativeLayout__LayoutParams.WRAP_CONTENT) text_container_params.height = self.ROW_HEIGHT text_container_params.setMargins(25 + 25 + 150, 0, 0, 0) row_foreground.addView(text_container, text_container_params) text_container.setOrientation(LinearLayout.VERTICAL) text_container.setWeightSum(2.0) top_text = TextView(self._native_activity) top_text.setText(str(getattr(self.interface.data[i], 'title', ''))) top_text.setTextSize(20.0) top_text.setTextColor(self._native_activity.getResources().getColor(R__color.black)) bottom_text = TextView(self._native_activity) bottom_text.setTextColor(self._native_activity.getResources().getColor(R__color.black)) bottom_text.setText(str(getattr(self.interface.data[i], 'subtitle', ''))) bottom_text.setTextSize(16.0) top_text_params = LinearLayout__LayoutParams( RelativeLayout__LayoutParams.WRAP_CONTENT, RelativeLayout__LayoutParams.MATCH_PARENT) top_text_params.weight = 1.0 top_text.setGravity(Gravity.BOTTOM) text_container.addView(top_text, top_text_params) bottom_text_params = LinearLayout__LayoutParams( RelativeLayout__LayoutParams.WRAP_CONTENT, RelativeLayout__LayoutParams.MATCH_PARENT) bottom_text_params.weight = 1.0 bottom_text.setGravity(Gravity.TOP) bottom_text_params.gravity = Gravity.TOP text_container.addView(bottom_text, bottom_text_params) row_foreground.setOnClickListener(DetailedListOnClickListener(self, i)) def change_source(self, source): # If the source changes, re-build the widget. self.create() def set_on_refresh(self, handler): # No special handling needed. pass def after_on_refresh(self): if self._swipe_refresh_layout: self._swipe_refresh_layout.setRefreshing(False) def insert(self, index, item): # If the data changes, re-build the widget. Brutally effective. self.create() def change(self, item): # If the data changes, re-build the widget. Brutally effective. self.create() def remove(self, index, item): # If the data changes, re-build the widget. Brutally effective. self.create() def clear(self): # If the data changes, re-build the widget. Brutally effective. self.create() def get_selection(self): return self._selection def set_on_select(self, handler): # No special handling required. pass def set_on_delete(self, handler): # This widget currently does not implement event handlers for data change. self.interface.factory.not_implemented("DetailedList.set_on_delete()") def scroll_to_row(self, row): def scroll(): row_obj = self._dismissable_container.getChildAt(row) hit_rect = Rect() row_obj.getHitRect(hit_rect) self._scroll_view.requestChildRectangleOnScreen( self._dismissable_container, hit_rect, False, ) Handler().post(PythonRunnable(scroll)) def rehint(self): # Android can crash when rendering some widgets until they have their layout params set. Guard for that case. if self.native.getLayoutParams() is None: return self.native.measure( View__MeasureSpec.UNSPECIFIED, View__MeasureSpec.UNSPECIFIED, ) self.interface.intrinsic.width = at_least(self.native.getMeasuredWidth()) self.interface.intrinsic.height = self.native.getMeasuredHeight()
true
true
f706288a231083cbe786ab30ed1d68ce39db9b47
1,830
py
Python
invenio_records_marc21/services/permissions.py
philippgualdi/invenio-records-marc21
6a4d17692649d8256ec8c9671d023e1065ce6063
[ "MIT" ]
1
2022-03-15T13:32:12.000Z
2022-03-15T13:32:12.000Z
invenio_records_marc21/services/permissions.py
philippgualdi/invenio-records-marc21
6a4d17692649d8256ec8c9671d023e1065ce6063
[ "MIT" ]
null
null
null
invenio_records_marc21/services/permissions.py
philippgualdi/invenio-records-marc21
6a4d17692649d8256ec8c9671d023e1065ce6063
[ "MIT" ]
null
null
null
# -*- coding: utf-8 -*- # # This file is part of Invenio. # # Copyright (C) 2021 Graz University of Technology. # # Invenio-Records-Marc21 is free software; you can redistribute it and/or # modify it under the terms of the MIT License; see LICENSE file for more # details. """Permissions for Invenio Marc21 Records.""" from invenio_records_permissions.generators import AnyUser, SystemProcess from invenio_records_permissions.policies.records import RecordPermissionPolicy class Marc21RecordPermissionPolicy(RecordPermissionPolicy): """Access control configuration for records. Note that even if the array is empty, the invenio_access Permission class always adds the ``superuser-access``, so admins will always be allowed. - Create action given to everyone for now. - Read access given to everyone if public record and given to owners always. (inherited) - Update access given to record owners. (inherited) - Delete access given to admins only. (inherited) """ # TODO: Change all below when permissions settled can_create = [AnyUser()] can_update_files = [AnyUser()] can_publish = [AnyUser()] can_read = [AnyUser()] can_update = [AnyUser()] can_new_version = [AnyUser()] can_edit = [AnyUser()] can_lift_embargo = [AnyUser()] # Draft permissions can_read_draft = [AnyUser()] can_delete_draft = [AnyUser()] can_update_draft = [AnyUser()] can_search_drafts = [AnyUser()] can_draft_read_files = [AnyUser()] can_draft_create_files = [AnyUser(), SystemProcess()] can_draft_update_files = [AnyUser()] can_draft_delete_files = [AnyUser()] # Files permissions can_read_files = [AnyUser()] can_create_files = [AnyUser(), SystemProcess()] can_update_files = [AnyUser()] can_delete_files = [AnyUser()]
33.272727
79
0.711475
from invenio_records_permissions.generators import AnyUser, SystemProcess from invenio_records_permissions.policies.records import RecordPermissionPolicy class Marc21RecordPermissionPolicy(RecordPermissionPolicy): can_create = [AnyUser()] can_update_files = [AnyUser()] can_publish = [AnyUser()] can_read = [AnyUser()] can_update = [AnyUser()] can_new_version = [AnyUser()] can_edit = [AnyUser()] can_lift_embargo = [AnyUser()] can_read_draft = [AnyUser()] can_delete_draft = [AnyUser()] can_update_draft = [AnyUser()] can_search_drafts = [AnyUser()] can_draft_read_files = [AnyUser()] can_draft_create_files = [AnyUser(), SystemProcess()] can_draft_update_files = [AnyUser()] can_draft_delete_files = [AnyUser()] can_read_files = [AnyUser()] can_create_files = [AnyUser(), SystemProcess()] can_update_files = [AnyUser()] can_delete_files = [AnyUser()]
true
true
f70628a2775d6cde3016b032cd7b3fb1c400265a
810
py
Python
manage.py
raq929/animal-rescue
02709701f911ae2d8863e7f066b8fb437b70bde9
[ "MIT" ]
null
null
null
manage.py
raq929/animal-rescue
02709701f911ae2d8863e7f066b8fb437b70bde9
[ "MIT" ]
null
null
null
manage.py
raq929/animal-rescue
02709701f911ae2d8863e7f066b8fb437b70bde9
[ "MIT" ]
null
null
null
#!/usr/bin/env python import os import sys if __name__ == "__main__": os.environ.setdefault("DJANGO_SETTINGS_MODULE", "animalRescue.settings") try: from django.core.management import execute_from_command_line except ImportError: # The above import may fail for some other reason. Ensure that the # issue is really that Django is missing to avoid masking other # exceptions on Python 2. try: import django except ImportError: raise ImportError( "Couldn't import Django. Are you sure it's installed and " "available on your PYTHONPATH environment variable? Did you " "forget to activate a virtual environment?" ) raise execute_from_command_line(sys.argv)
35.217391
77
0.644444
import os import sys if __name__ == "__main__": os.environ.setdefault("DJANGO_SETTINGS_MODULE", "animalRescue.settings") try: from django.core.management import execute_from_command_line except ImportError: try: import django except ImportError: raise ImportError( "Couldn't import Django. Are you sure it's installed and " "available on your PYTHONPATH environment variable? Did you " "forget to activate a virtual environment?" ) raise execute_from_command_line(sys.argv)
true
true
f7062aded80a6635855c5cc68eb6f354e08795da
1,361
py
Python
Python/simple-web-server.py
babebeebaboo/Practicum_SmartBedRoom
ee7fc152851b0df7ec491351fcbf4b31e822523e
[ "MIT" ]
null
null
null
Python/simple-web-server.py
babebeebaboo/Practicum_SmartBedRoom
ee7fc152851b0df7ec491351fcbf4b31e822523e
[ "MIT" ]
null
null
null
Python/simple-web-server.py
babebeebaboo/Practicum_SmartBedRoom
ee7fc152851b0df7ec491351fcbf4b31e822523e
[ "MIT" ]
null
null
null
import http.server import threading import testPackage class MyHTTPRequestHandler(http.server.BaseHTTPRequestHandler): def _set_headers(self): self.send_response(200) self.send_header('Content-type', 'text/html') self.end_headers() def do_GET(self): self._set_headers() self.wfile.write(b"<html><head><title>My Title</title></head>") self.wfile.write(bytes("<body><p>You accessed path: %s</p>" % self.path,"utf-8")) self.wfile.write(bytes("Light Value: %d" % testPackage.LIGHT_VALUE,"utf-8")) self.wfile.write(b"</body></html>") print("Sent light value") def do_POST(self): testPackage.increase_light() self._set_headers() self.wfile.write(b"Success") print("Increasing Light value") class serverThread (threading.Thread) : def __init__(self, threadID): threading.Thread.__init__(self) self.threadID = threadID def run(self): print ("Starting serverThread: %d" % self.threadID) httpd = http.server.HTTPServer(server_address, request_handler) httpd.serve_forever() print ("Exiting thread") port = 30000 server_address = ('',port) request_handler = MyHTTPRequestHandler thread_server = serverThread(1) thread_server.start() testPackage.polling_forever() print('After serve_forever()')
31.651163
89
0.671565
import http.server import threading import testPackage class MyHTTPRequestHandler(http.server.BaseHTTPRequestHandler): def _set_headers(self): self.send_response(200) self.send_header('Content-type', 'text/html') self.end_headers() def do_GET(self): self._set_headers() self.wfile.write(b"<html><head><title>My Title</title></head>") self.wfile.write(bytes("<body><p>You accessed path: %s</p>" % self.path,"utf-8")) self.wfile.write(bytes("Light Value: %d" % testPackage.LIGHT_VALUE,"utf-8")) self.wfile.write(b"</body></html>") print("Sent light value") def do_POST(self): testPackage.increase_light() self._set_headers() self.wfile.write(b"Success") print("Increasing Light value") class serverThread (threading.Thread) : def __init__(self, threadID): threading.Thread.__init__(self) self.threadID = threadID def run(self): print ("Starting serverThread: %d" % self.threadID) httpd = http.server.HTTPServer(server_address, request_handler) httpd.serve_forever() print ("Exiting thread") port = 30000 server_address = ('',port) request_handler = MyHTTPRequestHandler thread_server = serverThread(1) thread_server.start() testPackage.polling_forever() print('After serve_forever()')
true
true
f7062bce3b55defc7f8114903eb38c8b40ae863c
380
py
Python
app/__main__.py
vitaliy-ukiru/math-bot
72c116b4f5a4aa6a5f8eaae67ecbbf3df821f9e9
[ "MIT" ]
1
2021-12-11T07:41:38.000Z
2021-12-11T07:41:38.000Z
app/__main__.py
vitaliy-ukiru/math-bot
72c116b4f5a4aa6a5f8eaae67ecbbf3df821f9e9
[ "MIT" ]
8
2021-05-08T21:48:34.000Z
2022-01-20T15:42:00.000Z
app/__main__.py
vitaliy-ukiru/math-bot
72c116b4f5a4aa6a5f8eaae67ecbbf3df821f9e9
[ "MIT" ]
null
null
null
import sys from datetime import datetime from app import filters from app.misc.execute import run def main(args: list[str]): from app.handlers import dp # Сохраняем нынешнее время для подсчёта времени безотказной работы dp.bot['start_time'] = datetime.now() dp['args'] = args run(dp) if __name__ == '__main__': filters.setup() main(sys.argv[1:])
19
70
0.692105
import sys from datetime import datetime from app import filters from app.misc.execute import run def main(args: list[str]): from app.handlers import dp dp.bot['start_time'] = datetime.now() dp['args'] = args run(dp) if __name__ == '__main__': filters.setup() main(sys.argv[1:])
true
true
f7062bec006652094e432ba541af287898016ea8
2,071
py
Python
camera.py
EliteArrow/Web-Cam-Recorder
ccbf66e8c20c7de042d8e3560b6f70ea038ef5d2
[ "MIT" ]
1
2021-01-07T18:26:43.000Z
2021-01-07T18:26:43.000Z
camera.py
VincentK16/web-camera-recorder
6cc738220b6034774c9e9cca7ab82e3fc8fd0142
[ "MIT" ]
null
null
null
camera.py
VincentK16/web-camera-recorder
6cc738220b6034774c9e9cca7ab82e3fc8fd0142
[ "MIT" ]
null
null
null
import cv2 import threading class RecordingThread (threading.Thread): def __init__(self, name, camera): threading.Thread.__init__(self) self.name = name self.isRunning = True self.cap = camera fourcc = cv2.VideoWriter_fourcc(*'MJPG') self.out = cv2.VideoWriter('./static/video.avi',fourcc, 20.0, (640,480)) def run(self): while self.isRunning: ret, frame = self.cap.read() if ret: self.out.write(frame) self.out.release() def stop(self): self.isRunning = False def __del__(self): self.out.release() class VideoCamera(object): def __init__(self): # Open a camera self.cap = cv2.VideoCapture(2) # Initialize video recording environment self.is_record = False self.out = None # Thread for recording self.recordingThread = None def __del__(self): self.cap.release() def get_frame(self): ret, frame = self.cap.read() if ret: ret, jpeg = cv2.imencode('.jpg', frame) # Record video # if self.is_record: # if self.out == None: # fourcc = cv2.VideoWriter_fourcc(*'MJPG') # self.out = cv2.VideoWriter('./static/video.avi',fourcc, 20.0, (640,480)) # ret, frame = self.cap.read() # if ret: # self.out.write(frame) # else: # if self.out != None: # self.out.release() # self.out = None return jpeg.tobytes() else: return None def start_record(self): self.is_record = True self.recordingThread = RecordingThread("Video Recording Thread", self.cap) self.recordingThread.start() def stop_record(self): self.is_record = False if self.recordingThread != None: self.recordingThread.stop()
26.21519
94
0.522936
import cv2 import threading class RecordingThread (threading.Thread): def __init__(self, name, camera): threading.Thread.__init__(self) self.name = name self.isRunning = True self.cap = camera fourcc = cv2.VideoWriter_fourcc(*'MJPG') self.out = cv2.VideoWriter('./static/video.avi',fourcc, 20.0, (640,480)) def run(self): while self.isRunning: ret, frame = self.cap.read() if ret: self.out.write(frame) self.out.release() def stop(self): self.isRunning = False def __del__(self): self.out.release() class VideoCamera(object): def __init__(self): self.cap = cv2.VideoCapture(2) self.is_record = False self.out = None self.recordingThread = None def __del__(self): self.cap.release() def get_frame(self): ret, frame = self.cap.read() if ret: ret, jpeg = cv2.imencode('.jpg', frame) return jpeg.tobytes() else: return None def start_record(self): self.is_record = True self.recordingThread = RecordingThread("Video Recording Thread", self.cap) self.recordingThread.start() def stop_record(self): self.is_record = False if self.recordingThread != None: self.recordingThread.stop()
true
true
f7062bf844f50249bacf44df24849dd3919c8562
10,289
py
Python
lib/requests/packages/urllib3/connection.py
All4Gis/instagram2qgis
cf74a6308db3679e92ebc30c48b434bc276fadb3
[ "FSFAP" ]
19
2016-03-23T21:13:53.000Z
2021-05-28T10:11:54.000Z
lib/requests/packages/urllib3/connection.py
All4Gis/instagram2qgis
cf74a6308db3679e92ebc30c48b434bc276fadb3
[ "FSFAP" ]
2
2016-04-09T19:46:47.000Z
2021-04-19T14:36:23.000Z
lib/requests/packages/urllib3/connection.py
All4Gis/instagram2qgis
cf74a6308db3679e92ebc30c48b434bc276fadb3
[ "FSFAP" ]
2
2018-03-03T12:55:24.000Z
2019-09-15T10:59:41.000Z
from __future__ import absolute_import import datetime import os from socket import error as SocketError, timeout as SocketTimeout import socket import sys import warnings from .exceptions import ( NewConnectionError, ConnectTimeoutError, SubjectAltNameWarning, SystemTimeWarning, ) from .packages import six from .packages.ssl_match_hostname import match_hostname from .util import connection from .util.ssl_ import ( resolve_cert_reqs, resolve_ssl_version, ssl_wrap_socket, assert_fingerprint, ) try: # Python 3 from http.client import HTTPConnection as _HTTPConnection from http.client import HTTPException # noqa: unused in this module except ImportError: from httplib import HTTPConnection as _HTTPConnection from httplib import HTTPException # noqa: unused in this module try: # Compiled with SSL? import ssl BaseSSLError = ssl.SSLError except (ImportError, AttributeError): # Platform-specific: No SSL. ssl = None class BaseSSLError(BaseException): pass try: # Python 3: # Not a no-op, we're adding this to the namespace so it can be imported. ConnectionError = ConnectionError except NameError: # Python 2: class ConnectionError(Exception): pass port_by_scheme = { 'http': 80, 'https': 443, } RECENT_DATE = datetime.date(2014, 1, 1) class DummyConnection(object): """Used to detect a failed ConnectionCls import.""" pass class HTTPConnection(_HTTPConnection, object): """ Based on httplib.HTTPConnection but provides an extra constructor backwards-compatibility layer between older and newer Pythons. Additional keyword parameters are used to configure attributes of the connection. Accepted parameters include: - ``strict``: See the documentation on :class:`urllib3.connectionpool.HTTPConnectionPool` - ``source_address``: Set the source address for the current connection. .. note:: This is ignored for Python 2.6. It is only applied for 2.7 and 3.x - ``socket_options``: Set specific options on the underlying socket. If not specified, then defaults are loaded from ``HTTPConnection.default_socket_options`` which includes disabling Nagle's algorithm (sets TCP_NODELAY to 1) unless the connection is behind a proxy. For example, if you wish to enable TCP Keep Alive in addition to the defaults, you might pass:: HTTPConnection.default_socket_options + [ (socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1), ] Or you may want to disable the defaults by passing an empty list (e.g., ``[]``). """ default_port = port_by_scheme['http'] #: Disable Nagle's algorithm by default. #: ``[(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)]`` default_socket_options = [(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)] #: Whether this connection verifies the host's certificate. is_verified = False def __init__(self, *args, **kw): if six.PY3: # Python 3 kw.pop('strict', None) # Pre-set source_address in case we have an older Python like 2.6. self.source_address = kw.get('source_address') if sys.version_info < (2, 7): # Python 2.6 # _HTTPConnection on Python 2.6 will balk at this keyword arg, but # not newer versions. We can still use it when creating a # connection though, so we pop it *after* we have saved it as # self.source_address. kw.pop('source_address', None) #: The socket options provided by the user. If no options are #: provided, we use the default options. self.socket_options = kw.pop('socket_options', self.default_socket_options) # Superclass also sets self.source_address in Python 2.7+. _HTTPConnection.__init__(self, *args, **kw) def _new_conn(self): """ Establish a socket connection and set nodelay settings on it. :return: New socket connection. """ extra_kw = {} if self.source_address: extra_kw['source_address'] = self.source_address if self.socket_options: extra_kw['socket_options'] = self.socket_options try: conn = connection.create_connection( (self.host, self.port), self.timeout, **extra_kw) except SocketTimeout as e: raise ConnectTimeoutError( self, "Connection to %s timed out. (connect timeout=%s)" % (self.host, self.timeout)) except SocketError as e: raise NewConnectionError( self, "Failed to establish a new connection: %s" % e) return conn def _prepare_conn(self, conn): self.sock = conn # the _tunnel_host attribute was added in python 2.6.3 (via # http://hg.python.org/cpython/rev/0f57b30a152f) so pythons 2.6(0-2) do # not have them. if getattr(self, '_tunnel_host', None): # TODO: Fix tunnel so it doesn't depend on self.sock state. self._tunnel() # Mark this connection as not reusable self.auto_open = 0 def connect(self): conn = self._new_conn() self._prepare_conn(conn) class HTTPSConnection(HTTPConnection): default_port = port_by_scheme['https'] def __init__(self, host, port=None, key_file=None, cert_file=None, strict=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT, **kw): HTTPConnection.__init__(self, host, port, strict=strict, timeout=timeout, **kw) self.key_file = key_file self.cert_file = cert_file # Required property for Google AppEngine 1.9.0 which otherwise causes # HTTPS requests to go out as HTTP. (See Issue #356) self._protocol = 'https' def connect(self): conn = self._new_conn() self._prepare_conn(conn) self.sock = ssl.wrap_socket(conn, self.key_file, self.cert_file) class VerifiedHTTPSConnection(HTTPSConnection): """ Based on httplib.HTTPSConnection but wraps the socket with SSL certification. """ cert_reqs = None ca_certs = None ca_cert_dir = None ssl_version = None assert_fingerprint = None def set_cert(self, key_file=None, cert_file=None, cert_reqs=None, ca_certs=None, assert_hostname=None, assert_fingerprint=None, ca_cert_dir=None): if (ca_certs or ca_cert_dir) and cert_reqs is None: cert_reqs = 'CERT_REQUIRED' self.key_file = key_file self.cert_file = cert_file self.cert_reqs = cert_reqs self.assert_hostname = assert_hostname self.assert_fingerprint = assert_fingerprint self.ca_certs = ca_certs and os.path.expanduser(ca_certs) self.ca_cert_dir = ca_cert_dir and os.path.expanduser(ca_cert_dir) def connect(self): # Add certificate verification conn = self._new_conn() resolved_cert_reqs = resolve_cert_reqs(self.cert_reqs) resolved_ssl_version = resolve_ssl_version(self.ssl_version) hostname = self.host if getattr(self, '_tunnel_host', None): # _tunnel_host was added in Python 2.6.3 # (See: http://hg.python.org/cpython/rev/0f57b30a152f) self.sock = conn # Calls self._set_hostport(), so self.host is # self._tunnel_host below. self._tunnel() # Mark this connection as not reusable self.auto_open = 0 # Override the host with the one we're requesting data from. hostname = self._tunnel_host is_time_off = datetime.date.today() < RECENT_DATE if is_time_off: warnings.warn(( 'System time is way off (before {0}). This will probably ' 'lead to SSL verification errors').format(RECENT_DATE), SystemTimeWarning ) # Wrap socket using verification with the root certs in # trusted_root_certs self.sock = ssl_wrap_socket(conn, self.key_file, self.cert_file, cert_reqs=resolved_cert_reqs, ca_certs=self.ca_certs, ca_cert_dir=self.ca_cert_dir, server_hostname=hostname, ssl_version=resolved_ssl_version) if self.assert_fingerprint: assert_fingerprint(self.sock.getpeercert(binary_form=True), self.assert_fingerprint) elif resolved_cert_reqs != ssl.CERT_NONE \ and self.assert_hostname is not False: cert = self.sock.getpeercert() if not cert.get('subjectAltName', ()): warnings.warn(( 'Certificate for {0} has no `subjectAltName`, falling back to check for a ' '`commonName` for now. This feature is being removed by major browsers and ' 'deprecated by RFC 2818. (See https://github.com/shazow/urllib3/issues/497 ' 'for details.)'.format(hostname)), SubjectAltNameWarning ) # In case the hostname is an IPv6 address, strip the square # brackets from it before using it to validate. This is because # a certificate with an IPv6 address in it won't have square # brackets around that address. Sadly, match_hostname won't do this # for us: it expects the plain host part without any extra work # that might have been done to make it palatable to httplib. asserted_hostname = self.assert_hostname or hostname asserted_hostname = asserted_hostname.strip('[]') match_hostname(cert, asserted_hostname) self.is_verified = (resolved_cert_reqs == ssl.CERT_REQUIRED or self.assert_fingerprint is not None) if ssl: # Make a copy for testing. UnverifiedHTTPSConnection = HTTPSConnection HTTPSConnection = VerifiedHTTPSConnection else: HTTPSConnection = DummyConnection
35.236301
99
0.637088
from __future__ import absolute_import import datetime import os from socket import error as SocketError, timeout as SocketTimeout import socket import sys import warnings from .exceptions import ( NewConnectionError, ConnectTimeoutError, SubjectAltNameWarning, SystemTimeWarning, ) from .packages import six from .packages.ssl_match_hostname import match_hostname from .util import connection from .util.ssl_ import ( resolve_cert_reqs, resolve_ssl_version, ssl_wrap_socket, assert_fingerprint, ) try: from http.client import HTTPConnection as _HTTPConnection from http.client import HTTPException except ImportError: from httplib import HTTPConnection as _HTTPConnection from httplib import HTTPException try: import ssl BaseSSLError = ssl.SSLError except (ImportError, AttributeError): ssl = None class BaseSSLError(BaseException): pass try: ConnectionError = ConnectionError except NameError: # Python 2: class ConnectionError(Exception): pass port_by_scheme = { 'http': 80, 'https': 443, } RECENT_DATE = datetime.date(2014, 1, 1) class DummyConnection(object): pass class HTTPConnection(_HTTPConnection, object): default_port = port_by_scheme['http'] #: Disable Nagle's algorithm by default. default_socket_options = [(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)] is_verified = False def __init__(self, *args, **kw): if six.PY3: # Python 3 kw.pop('strict', None) # Pre-set source_address in case we have an older Python like 2.6. self.source_address = kw.get('source_address') if sys.version_info < (2, 7): # Python 2.6 # _HTTPConnection on Python 2.6 will balk at this keyword arg, but # not newer versions. We can still use it when creating a # connection though, so we pop it *after* we have saved it as # self.source_address. kw.pop('source_address', None) #: The socket options provided by the user. If no options are #: provided, we use the default options. self.socket_options = kw.pop('socket_options', self.default_socket_options) # Superclass also sets self.source_address in Python 2.7+. _HTTPConnection.__init__(self, *args, **kw) def _new_conn(self): extra_kw = {} if self.source_address: extra_kw['source_address'] = self.source_address if self.socket_options: extra_kw['socket_options'] = self.socket_options try: conn = connection.create_connection( (self.host, self.port), self.timeout, **extra_kw) except SocketTimeout as e: raise ConnectTimeoutError( self, "Connection to %s timed out. (connect timeout=%s)" % (self.host, self.timeout)) except SocketError as e: raise NewConnectionError( self, "Failed to establish a new connection: %s" % e) return conn def _prepare_conn(self, conn): self.sock = conn # the _tunnel_host attribute was added in python 2.6.3 (via # http://hg.python.org/cpython/rev/0f57b30a152f) so pythons 2.6(0-2) do # not have them. if getattr(self, '_tunnel_host', None): # TODO: Fix tunnel so it doesn't depend on self.sock state. self._tunnel() self.auto_open = 0 def connect(self): conn = self._new_conn() self._prepare_conn(conn) class HTTPSConnection(HTTPConnection): default_port = port_by_scheme['https'] def __init__(self, host, port=None, key_file=None, cert_file=None, strict=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT, **kw): HTTPConnection.__init__(self, host, port, strict=strict, timeout=timeout, **kw) self.key_file = key_file self.cert_file = cert_file self._protocol = 'https' def connect(self): conn = self._new_conn() self._prepare_conn(conn) self.sock = ssl.wrap_socket(conn, self.key_file, self.cert_file) class VerifiedHTTPSConnection(HTTPSConnection): cert_reqs = None ca_certs = None ca_cert_dir = None ssl_version = None assert_fingerprint = None def set_cert(self, key_file=None, cert_file=None, cert_reqs=None, ca_certs=None, assert_hostname=None, assert_fingerprint=None, ca_cert_dir=None): if (ca_certs or ca_cert_dir) and cert_reqs is None: cert_reqs = 'CERT_REQUIRED' self.key_file = key_file self.cert_file = cert_file self.cert_reqs = cert_reqs self.assert_hostname = assert_hostname self.assert_fingerprint = assert_fingerprint self.ca_certs = ca_certs and os.path.expanduser(ca_certs) self.ca_cert_dir = ca_cert_dir and os.path.expanduser(ca_cert_dir) def connect(self): conn = self._new_conn() resolved_cert_reqs = resolve_cert_reqs(self.cert_reqs) resolved_ssl_version = resolve_ssl_version(self.ssl_version) hostname = self.host if getattr(self, '_tunnel_host', None): self.sock = conn self._tunnel() self.auto_open = 0 hostname = self._tunnel_host is_time_off = datetime.date.today() < RECENT_DATE if is_time_off: warnings.warn(( 'System time is way off (before {0}). This will probably ' 'lead to SSL verification errors').format(RECENT_DATE), SystemTimeWarning ) # Wrap socket using verification with the root certs in # trusted_root_certs self.sock = ssl_wrap_socket(conn, self.key_file, self.cert_file, cert_reqs=resolved_cert_reqs, ca_certs=self.ca_certs, ca_cert_dir=self.ca_cert_dir, server_hostname=hostname, ssl_version=resolved_ssl_version) if self.assert_fingerprint: assert_fingerprint(self.sock.getpeercert(binary_form=True), self.assert_fingerprint) elif resolved_cert_reqs != ssl.CERT_NONE \ and self.assert_hostname is not False: cert = self.sock.getpeercert() if not cert.get('subjectAltName', ()): warnings.warn(( 'Certificate for {0} has no `subjectAltName`, falling back to check for a ' '`commonName` for now. This feature is being removed by major browsers and ' 'deprecated by RFC 2818. (See https://github.com/shazow/urllib3/issues/497 ' 'for details.)'.format(hostname)), SubjectAltNameWarning ) # In case the hostname is an IPv6 address, strip the square # brackets from it before using it to validate. This is because # a certificate with an IPv6 address in it won't have square # for us: it expects the plain host part without any extra work # that might have been done to make it palatable to httplib. asserted_hostname = self.assert_hostname or hostname asserted_hostname = asserted_hostname.strip('[]') match_hostname(cert, asserted_hostname) self.is_verified = (resolved_cert_reqs == ssl.CERT_REQUIRED or self.assert_fingerprint is not None) if ssl: # Make a copy for testing. UnverifiedHTTPSConnection = HTTPSConnection HTTPSConnection = VerifiedHTTPSConnection else: HTTPSConnection = DummyConnection
true
true
f7062c5ad5a28d268213498e9a5e9b653325178f
3,020
py
Python
wigs-validation-linux/pre_wigs_validation/repo_access.py
therin/aws-managed-services
8950d6ed902c1007427a7e5adc0b7b5c89bb2531
[ "Apache-2.0" ]
null
null
null
wigs-validation-linux/pre_wigs_validation/repo_access.py
therin/aws-managed-services
8950d6ed902c1007427a7e5adc0b7b5c89bb2531
[ "Apache-2.0" ]
null
null
null
wigs-validation-linux/pre_wigs_validation/repo_access.py
therin/aws-managed-services
8950d6ed902c1007427a7e5adc0b7b5c89bb2531
[ "Apache-2.0" ]
null
null
null
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 import shutil import subprocess from pre_wigs_validation.enums import ValidationEnforcement, ValidationResult from pre_wigs_validation.instance import ValidationInstance from pre_wigs_validation.dataclasses import ValidationOutput from pre_wigs_validation.utils import check_validation_config, get_ld_library_orig class RepoAccess: """Validate that an instance has access to Yum Repositories.""" validation = "Repo Access" enforcement = ValidationEnforcement.REQUIRED @classmethod def validate( cls, *, enabled: bool = True, instance: ValidationInstance ) -> ValidationOutput: """ Parameters: enabled (bool): whether or not to run this validation function instance (ValidationInstance): the instance object being validated Returns: ValidationOutput: output of validation """ if not enabled: return ValidationOutput( validation=cls.validation, result=ValidationResult.NOT_RUN, enforcement=cls.enforcement, ) if instance.distribution == "sles": repo_check_command = ["zypper", "refresh"] verbose_error_message = "Command 'zypper' not in $PATH" package_manager_found = not shutil.which("zypper") == None else: repo_check_command = ["yum", "check-update"] verbose_error_message = "Command 'yum' not in $PATH" package_manager_found = not shutil.which("yum") == None error_message = "Unable to validate due to unsupported environment" fail_message = "Unable to access repositories" config = check_validation_config( default_params=cls.validate.__kwdefaults__, local_params=locals() ) if not package_manager_found: return ValidationOutput( validation=cls.validation, result=ValidationResult.FAIL, enforcement=cls.enforcement, config=config, message=error_message, verbose_message=verbose_error_message, ) proc = subprocess.run( repo_check_command, env=get_ld_library_orig(), stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, ) if proc.returncode == 1: verbose_message = proc.stderr.decode("utf-8") return ValidationOutput( validation=cls.validation, result=ValidationResult.FAIL, enforcement=cls.enforcement, config=config, message=fail_message, verbose_message=verbose_message, ) return ValidationOutput( validation=cls.validation, result=ValidationResult.PASS, enforcement=cls.enforcement, config=config, )
34.318182
82
0.62649
import shutil import subprocess from pre_wigs_validation.enums import ValidationEnforcement, ValidationResult from pre_wigs_validation.instance import ValidationInstance from pre_wigs_validation.dataclasses import ValidationOutput from pre_wigs_validation.utils import check_validation_config, get_ld_library_orig class RepoAccess: validation = "Repo Access" enforcement = ValidationEnforcement.REQUIRED @classmethod def validate( cls, *, enabled: bool = True, instance: ValidationInstance ) -> ValidationOutput: if not enabled: return ValidationOutput( validation=cls.validation, result=ValidationResult.NOT_RUN, enforcement=cls.enforcement, ) if instance.distribution == "sles": repo_check_command = ["zypper", "refresh"] verbose_error_message = "Command 'zypper' not in $PATH" package_manager_found = not shutil.which("zypper") == None else: repo_check_command = ["yum", "check-update"] verbose_error_message = "Command 'yum' not in $PATH" package_manager_found = not shutil.which("yum") == None error_message = "Unable to validate due to unsupported environment" fail_message = "Unable to access repositories" config = check_validation_config( default_params=cls.validate.__kwdefaults__, local_params=locals() ) if not package_manager_found: return ValidationOutput( validation=cls.validation, result=ValidationResult.FAIL, enforcement=cls.enforcement, config=config, message=error_message, verbose_message=verbose_error_message, ) proc = subprocess.run( repo_check_command, env=get_ld_library_orig(), stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, ) if proc.returncode == 1: verbose_message = proc.stderr.decode("utf-8") return ValidationOutput( validation=cls.validation, result=ValidationResult.FAIL, enforcement=cls.enforcement, config=config, message=fail_message, verbose_message=verbose_message, ) return ValidationOutput( validation=cls.validation, result=ValidationResult.PASS, enforcement=cls.enforcement, config=config, )
true
true
f7062cf857e35ec488d5c372f50de79c582064b6
4,037
py
Python
pydeeprecsys/rl/agents/rainbow.py
luksfarris/pydeeprecsys
0409ca220a235bb65ccf72d9077aaecf108722bb
[ "MIT" ]
2
2021-06-21T06:38:38.000Z
2021-12-16T10:26:43.000Z
pydeeprecsys/rl/agents/rainbow.py
luksfarris/pydeeprecsys
0409ca220a235bb65ccf72d9077aaecf108722bb
[ "MIT" ]
6
2021-05-10T21:23:12.000Z
2021-06-03T19:46:39.000Z
pydeeprecsys/rl/agents/rainbow.py
luksfarris/pydeeprecsys
0409ca220a235bb65ccf72d9077aaecf108722bb
[ "MIT" ]
1
2021-12-20T16:54:28.000Z
2021-12-20T16:54:28.000Z
from numpy.random import RandomState from typing import Any, Optional, List from numpy import arange from copy import deepcopy from pydeeprecsys.rl.neural_networks.dueling import DuelingDDQN from pydeeprecsys.rl.experience_replay.priority_replay_buffer import ( PrioritizedExperienceReplayBuffer, ) from pydeeprecsys.rl.experience_replay.buffer_parameters import ( PERBufferParameters, ExperienceReplayBufferParameters, ) from pydeeprecsys.rl.agents.agent import ReinforcementLearning from pydeeprecsys.rl.learning_statistics import LearningStatistics class RainbowDQNAgent(ReinforcementLearning): """Instead of sampling randomly from the buffer we prioritize experiences with PER Instead of epsilon-greedy we use gaussian noisy layers for exploration Instead of the Q value we calculate Value and Advantage (Dueling DQN). This implementation does not include the Categorical DQN part (yet).""" def __init__( self, input_size: int, output_size: int, network_update_frequency: int = 5, network_sync_frequency: int = 200, priority_importance: float = 0.6, priority_weigth_growth: float = 0.001, buffer_size: int = 10000, buffer_burn_in: int = 1000, batch_size: int = 32, noise_sigma: float = 0.017, discount_factor: float = 0.99, learning_rate: float = 0.0001, hidden_layers: List[int] = None, random_state: RandomState = RandomState(), statistics: Optional[LearningStatistics] = None, ): self.network = DuelingDDQN( n_input=input_size, n_output=output_size, learning_rate=learning_rate, noise_sigma=noise_sigma, discount_factor=discount_factor, statistics=statistics, hidden_layers=hidden_layers, ) self.target_network = deepcopy(self.network) self.buffer = PrioritizedExperienceReplayBuffer( ExperienceReplayBufferParameters( max_experiences=buffer_size, minimum_experiences_to_start_predicting=buffer_burn_in, batch_size=batch_size, random_state=random_state, ), PERBufferParameters( alpha=priority_importance, beta_growth=priority_weigth_growth, ), ) self.step_count = 0 self.network_update_frequency = network_update_frequency self.network_sync_frequency = network_sync_frequency self.actions = arange(output_size) self.random_state = random_state def _check_update_network(self): # we only start training the network once the buffer is ready # (the burn in is filled) if self.buffer.ready_to_predict(): self.step_count += 1 if self.step_count % self.network_update_frequency == 0: # we train at every K steps self.network.learn_with(self.buffer, self.target_network) if self.step_count % self.network_sync_frequency == 0: # at every N steps replaces the target network with the main network self.target_network.load_state_dict(self.network.state_dict()) def top_k_actions_for_state(self, state: Any, k: int = 1) -> Any: state_flat = state.flatten() if self.buffer.ready_to_predict(): actions = self.target_network.top_k_actions_for_state(state_flat, k=k) else: actions = self.random_state.choice(self.actions, size=k) self._check_update_network() return actions def action_for_state(self, state: Any) -> Any: return self.top_k_actions_for_state(state, k=1)[0] def store_experience( self, state: Any, action: Any, reward: float, done: bool, new_state: Any ): state_flat = state.flatten() new_state_flat = new_state.flatten() self.buffer.store_experience(state_flat, action, reward, done, new_state_flat)
39.578431
86
0.672281
from numpy.random import RandomState from typing import Any, Optional, List from numpy import arange from copy import deepcopy from pydeeprecsys.rl.neural_networks.dueling import DuelingDDQN from pydeeprecsys.rl.experience_replay.priority_replay_buffer import ( PrioritizedExperienceReplayBuffer, ) from pydeeprecsys.rl.experience_replay.buffer_parameters import ( PERBufferParameters, ExperienceReplayBufferParameters, ) from pydeeprecsys.rl.agents.agent import ReinforcementLearning from pydeeprecsys.rl.learning_statistics import LearningStatistics class RainbowDQNAgent(ReinforcementLearning): def __init__( self, input_size: int, output_size: int, network_update_frequency: int = 5, network_sync_frequency: int = 200, priority_importance: float = 0.6, priority_weigth_growth: float = 0.001, buffer_size: int = 10000, buffer_burn_in: int = 1000, batch_size: int = 32, noise_sigma: float = 0.017, discount_factor: float = 0.99, learning_rate: float = 0.0001, hidden_layers: List[int] = None, random_state: RandomState = RandomState(), statistics: Optional[LearningStatistics] = None, ): self.network = DuelingDDQN( n_input=input_size, n_output=output_size, learning_rate=learning_rate, noise_sigma=noise_sigma, discount_factor=discount_factor, statistics=statistics, hidden_layers=hidden_layers, ) self.target_network = deepcopy(self.network) self.buffer = PrioritizedExperienceReplayBuffer( ExperienceReplayBufferParameters( max_experiences=buffer_size, minimum_experiences_to_start_predicting=buffer_burn_in, batch_size=batch_size, random_state=random_state, ), PERBufferParameters( alpha=priority_importance, beta_growth=priority_weigth_growth, ), ) self.step_count = 0 self.network_update_frequency = network_update_frequency self.network_sync_frequency = network_sync_frequency self.actions = arange(output_size) self.random_state = random_state def _check_update_network(self): if self.buffer.ready_to_predict(): self.step_count += 1 if self.step_count % self.network_update_frequency == 0: self.network.learn_with(self.buffer, self.target_network) if self.step_count % self.network_sync_frequency == 0: self.target_network.load_state_dict(self.network.state_dict()) def top_k_actions_for_state(self, state: Any, k: int = 1) -> Any: state_flat = state.flatten() if self.buffer.ready_to_predict(): actions = self.target_network.top_k_actions_for_state(state_flat, k=k) else: actions = self.random_state.choice(self.actions, size=k) self._check_update_network() return actions def action_for_state(self, state: Any) -> Any: return self.top_k_actions_for_state(state, k=1)[0] def store_experience( self, state: Any, action: Any, reward: float, done: bool, new_state: Any ): state_flat = state.flatten() new_state_flat = new_state.flatten() self.buffer.store_experience(state_flat, action, reward, done, new_state_flat)
true
true
f7062e562abc33c0c952b28947ef3cdfb48e0393
5,940
py
Python
grad-cam_5category.py
MuAuan/cheating_DL
e8c543d83c304ca072b479cf34fe0a07b58ec6e3
[ "MIT" ]
2
2019-07-20T10:50:47.000Z
2020-03-09T08:07:12.000Z
grad-cam_5category.py
MuAuan/cheating_DL
e8c543d83c304ca072b479cf34fe0a07b58ec6e3
[ "MIT" ]
null
null
null
grad-cam_5category.py
MuAuan/cheating_DL
e8c543d83c304ca072b479cf34fe0a07b58ec6e3
[ "MIT" ]
2
2019-07-20T10:59:43.000Z
2020-12-28T05:10:33.000Z
#grad_cam #[keras-grad-cam/grad-cam.py](https://github.com/jacobgil/keras-grad-cam/blob/master/grad-cam.py) from keras.applications.vgg16 import (VGG16, preprocess_input, decode_predictions) from keras.models import Model from keras.preprocessing import image from keras.layers.core import Lambda from keras.models import Sequential from tensorflow.python.framework import ops import keras.backend as K import tensorflow as tf import numpy as np import keras import sys import cv2 #from keras.applications.resnet50 import ResNet50, preprocess_input, decode_predictions #from keras.applications.vgg19 import VGG19, preprocess_input, decode_predictions #from keras.applications.inception_v3 import InceptionV3, preprocess_input, decode_predictions def target_category_loss(x, category_index, nb_classes): return tf.multiply(x, K.one_hot([category_index], nb_classes)) def target_category_loss_output_shape(input_shape): return input_shape def normalize(x): # utility function to normalize a tensor by its L2 norm return x / (K.sqrt(K.mean(K.square(x))) + 1e-5) def load_image(path): img_path = sys.argv[1] img = image.load_img(img_path, target_size=(224,224)) #299,299)) #224, 224)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) return x def register_gradient(): if "GuidedBackProp" not in ops._gradient_registry._registry: @ops.RegisterGradient("GuidedBackProp") def _GuidedBackProp(op, grad): dtype = op.inputs[0].dtype return grad * tf.cast(grad > 0., dtype) * \ tf.cast(op.inputs[0] > 0., dtype) def compile_saliency_function(model, activation_layer='block5_conv3'): #mixed10 'activation_49' add_16 add_32 activation_98 input_img = model.input layer_dict = dict([(layer.name, layer) for layer in model.layers[1:]]) #print(layer_dict) layer_output = layer_dict[activation_layer].output max_output = K.max(layer_output, axis=3) saliency = K.gradients(K.sum(max_output), input_img)[0] return K.function([input_img, K.learning_phase()], [saliency]) def modify_backprop(model, name): g = tf.get_default_graph() with g.gradient_override_map({'Relu': name}): # get layers that have an activation layer_dict = [layer for layer in model.layers[1:] if hasattr(layer, 'activation')] # replace relu activation for layer in layer_dict: if layer.activation == keras.activations.relu: layer.activation = tf.nn.relu # re-instanciate a new model new_model = VGG16(weights='imagenet') #new_model = ResNet50(weights='imagenet') new_model.summary() return new_model def deprocess_image(x): ''' Same normalization as in: https://github.com/fchollet/keras/blob/master/examples/conv_filter_visualization.py ''' if np.ndim(x) > 3: x = np.squeeze(x) # normalize tensor: center on 0., ensure std is 0.1 x -= x.mean() x /= (x.std() + 1e-5) x *= 0.1 # clip to [0, 1] x += 0.5 x = np.clip(x, 0, 1) # convert to RGB array x *= 255 if K.image_dim_ordering() == 'th': x = x.transpose((1, 2, 0)) x = np.clip(x, 0, 255).astype('uint8') return x def _compute_gradients(tensor, var_list): grads = tf.gradients(tensor, var_list) return [grad if grad is not None else tf.zeros_like(var) for var, grad in zip(var_list, grads)] def grad_cam(input_model, image, category_index, layer_name): nb_classes = 1000 target_layer = lambda x: target_category_loss(x, category_index, nb_classes) x = Lambda(target_layer, output_shape = target_category_loss_output_shape)(input_model.output) model = Model(inputs=input_model.input, outputs=x) #model.summary() loss = K.sum(model.output) conv_output = [l for l in model.layers if l.name == layer_name][0].output #is grads = normalize(_compute_gradients(loss, [conv_output])[0]) gradient_function = K.function([model.input], [conv_output, grads]) output, grads_val = gradient_function([image]) output, grads_val = output[0, :], grads_val[0, :, :, :] weights = np.mean(grads_val, axis = (0, 1)) cam = np.ones(output.shape[0 : 2], dtype = np.float32) for i, w in enumerate(weights): cam += w * output[:, :, i] cam = cv2.resize(cam, (224,224)) #299,299)) #224, 224)) cam = np.maximum(cam, 0) heatmap = cam / np.max(cam) #Return to BGR [0..255] from the preprocessed image image = image[0, :] image -= np.min(image) image = np.minimum(image, 255) cam = cv2.applyColorMap(np.uint8(255*heatmap), cv2.COLORMAP_JET) cam = np.float32(cam) + np.float32(image) cam = 255 * cam / np.max(cam) return np.uint8(cam), heatmap preprocessed_input = load_image(sys.argv[1]) model = VGG16(weights='imagenet') #model = VGG19(weights='imagenet') #model = InceptionV3(weights='imagenet') #model = ResNet50(weights = 'imagenet') #model.summary() target_layer = 'block5_conv3' #'activation_49' add_16 "block5_conv3" predictions = model.predict(preprocessed_input) register_gradient() guided_model = modify_backprop(model, 'GuidedBackProp') guided_model.summary() for i in range(5): top_1 = decode_predictions(predictions)[0][i] print(predictions.argsort()[0][::-1][i]) print('Predicted class:') print('%s (%s) with probability %.2f' % (top_1[1], top_1[0], top_1[2])) predicted_class = predictions.argsort()[0][::-1][i] #np.argmax(predictions) cam, heatmap = grad_cam(model, preprocessed_input, predicted_class, target_layer) cv2.imwrite("gradcam"+str(top_1[1])+".jpg", cam) saliency_fn = compile_saliency_function(guided_model) saliency = saliency_fn([preprocessed_input, 0]) gradcam = saliency[0] * heatmap[..., np.newaxis] cv2.imwrite("guided_gradcam"+str(top_1[1])+".jpg", deprocess_image(gradcam))
37.358491
123
0.685522
from keras.applications.vgg16 import (VGG16, preprocess_input, decode_predictions) from keras.models import Model from keras.preprocessing import image from keras.layers.core import Lambda from keras.models import Sequential from tensorflow.python.framework import ops import keras.backend as K import tensorflow as tf import numpy as np import keras import sys import cv2 def target_category_loss(x, category_index, nb_classes): return tf.multiply(x, K.one_hot([category_index], nb_classes)) def target_category_loss_output_shape(input_shape): return input_shape def normalize(x): return x / (K.sqrt(K.mean(K.square(x))) + 1e-5) def load_image(path): img_path = sys.argv[1] img = image.load_img(img_path, target_size=(224,224)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) return x def register_gradient(): if "GuidedBackProp" not in ops._gradient_registry._registry: @ops.RegisterGradient("GuidedBackProp") def _GuidedBackProp(op, grad): dtype = op.inputs[0].dtype return grad * tf.cast(grad > 0., dtype) * \ tf.cast(op.inputs[0] > 0., dtype) def compile_saliency_function(model, activation_layer='block5_conv3'): input_img = model.input layer_dict = dict([(layer.name, layer) for layer in model.layers[1:]]) layer_output = layer_dict[activation_layer].output max_output = K.max(layer_output, axis=3) saliency = K.gradients(K.sum(max_output), input_img)[0] return K.function([input_img, K.learning_phase()], [saliency]) def modify_backprop(model, name): g = tf.get_default_graph() with g.gradient_override_map({'Relu': name}): layer_dict = [layer for layer in model.layers[1:] if hasattr(layer, 'activation')] for layer in layer_dict: if layer.activation == keras.activations.relu: layer.activation = tf.nn.relu new_model = VGG16(weights='imagenet') new_model.summary() return new_model def deprocess_image(x): if np.ndim(x) > 3: x = np.squeeze(x) x -= x.mean() x /= (x.std() + 1e-5) x *= 0.1 x += 0.5 x = np.clip(x, 0, 1) x *= 255 if K.image_dim_ordering() == 'th': x = x.transpose((1, 2, 0)) x = np.clip(x, 0, 255).astype('uint8') return x def _compute_gradients(tensor, var_list): grads = tf.gradients(tensor, var_list) return [grad if grad is not None else tf.zeros_like(var) for var, grad in zip(var_list, grads)] def grad_cam(input_model, image, category_index, layer_name): nb_classes = 1000 target_layer = lambda x: target_category_loss(x, category_index, nb_classes) x = Lambda(target_layer, output_shape = target_category_loss_output_shape)(input_model.output) model = Model(inputs=input_model.input, outputs=x) loss = K.sum(model.output) conv_output = [l for l in model.layers if l.name == layer_name][0].output grads = normalize(_compute_gradients(loss, [conv_output])[0]) gradient_function = K.function([model.input], [conv_output, grads]) output, grads_val = gradient_function([image]) output, grads_val = output[0, :], grads_val[0, :, :, :] weights = np.mean(grads_val, axis = (0, 1)) cam = np.ones(output.shape[0 : 2], dtype = np.float32) for i, w in enumerate(weights): cam += w * output[:, :, i] cam = cv2.resize(cam, (224,224)) cam = np.maximum(cam, 0) heatmap = cam / np.max(cam) image = image[0, :] image -= np.min(image) image = np.minimum(image, 255) cam = cv2.applyColorMap(np.uint8(255*heatmap), cv2.COLORMAP_JET) cam = np.float32(cam) + np.float32(image) cam = 255 * cam / np.max(cam) return np.uint8(cam), heatmap preprocessed_input = load_image(sys.argv[1]) model = VGG16(weights='imagenet') target_layer = 'block5_conv3' predictions = model.predict(preprocessed_input) register_gradient() guided_model = modify_backprop(model, 'GuidedBackProp') guided_model.summary() for i in range(5): top_1 = decode_predictions(predictions)[0][i] print(predictions.argsort()[0][::-1][i]) print('Predicted class:') print('%s (%s) with probability %.2f' % (top_1[1], top_1[0], top_1[2])) predicted_class = predictions.argsort()[0][::-1][i] cam, heatmap = grad_cam(model, preprocessed_input, predicted_class, target_layer) cv2.imwrite("gradcam"+str(top_1[1])+".jpg", cam) saliency_fn = compile_saliency_function(guided_model) saliency = saliency_fn([preprocessed_input, 0]) gradcam = saliency[0] * heatmap[..., np.newaxis] cv2.imwrite("guided_gradcam"+str(top_1[1])+".jpg", deprocess_image(gradcam))
true
true
f7062e97122adf278b2ee489fee616f89c2c3ed3
3,130
py
Python
utils/tex_file_writing.py
xfbs/manim
83d0aa57f994793f6286aec7c8f22adaacc6770f
[ "MIT" ]
null
null
null
utils/tex_file_writing.py
xfbs/manim
83d0aa57f994793f6286aec7c8f22adaacc6770f
[ "MIT" ]
null
null
null
utils/tex_file_writing.py
xfbs/manim
83d0aa57f994793f6286aec7c8f22adaacc6770f
[ "MIT" ]
null
null
null
import os import hashlib from constants import TEX_DIR from constants import TEX_TEXT_TO_REPLACE from constants import TEX_USE_CTEX from constants import TEX_FIX_SVG def tex_hash(expression, template_tex_file_body): id_str = str(expression + template_tex_file_body) hasher = hashlib.sha256() hasher.update(id_str.encode()) # Truncating at 16 bytes for cleanliness return hasher.hexdigest()[:16] def tex_to_svg_file(expression, template_tex_file_body): tex_file = generate_tex_file(expression, template_tex_file_body) dvi_file = tex_to_dvi(tex_file) return dvi_to_svg(dvi_file) def generate_tex_file(expression, template_tex_file_body): result = os.path.join( TEX_DIR, tex_hash(expression, template_tex_file_body) ) + ".tex" if not os.path.exists(result): print("Writing \"%s\" to %s" % ( "".join(expression), result )) new_body = template_tex_file_body.replace( TEX_TEXT_TO_REPLACE, expression ) with open(result, "w") as outfile: outfile.write(new_body) return result def get_null(): if os.name == "nt": return "NUL" return "/dev/null" def tex_to_dvi(tex_file): result = tex_file.replace(".tex", ".dvi" if not TEX_USE_CTEX else ".xdv") if not os.path.exists(result): commands = [ "latex", "-interaction=batchmode", "-halt-on-error", "-output-directory=" + TEX_DIR, tex_file, ">", get_null() ] if not TEX_USE_CTEX else [ "xelatex", "-no-pdf", "-interaction=batchmode", "-halt-on-error", "-output-directory=" + TEX_DIR, tex_file, ">", get_null() ] exit_code = os.system(" ".join(commands)) if exit_code != 0: log_file = tex_file.replace(".tex", ".log") raise Exception( ("Latex error converting to dvi. " if not TEX_USE_CTEX else "Xelatex error converting to xdv. ") + "See log output above or the log file: %s" % log_file) return result def dvi_to_svg(dvi_file, regen_if_exists=False): """ Converts a dvi, which potentially has multiple slides, into a directory full of enumerated pngs corresponding with these slides. Returns a list of PIL Image objects for these images sorted as they where in the dvi """ result = dvi_file.replace(".dvi" if not TEX_USE_CTEX else ".xdv", ".svg") if not os.path.exists(result): commands = [ "dvisvgm", dvi_file, "-n", "-v", "0", "-o", result, ">", get_null() ] os.system(" ".join(commands)) if TEX_FIX_SVG: commands = [ "cairosvg", result, "-f", "svg", "-o", result ] os.system(" ".join(commands)) return result
27.946429
77
0.556869
import os import hashlib from constants import TEX_DIR from constants import TEX_TEXT_TO_REPLACE from constants import TEX_USE_CTEX from constants import TEX_FIX_SVG def tex_hash(expression, template_tex_file_body): id_str = str(expression + template_tex_file_body) hasher = hashlib.sha256() hasher.update(id_str.encode()) return hasher.hexdigest()[:16] def tex_to_svg_file(expression, template_tex_file_body): tex_file = generate_tex_file(expression, template_tex_file_body) dvi_file = tex_to_dvi(tex_file) return dvi_to_svg(dvi_file) def generate_tex_file(expression, template_tex_file_body): result = os.path.join( TEX_DIR, tex_hash(expression, template_tex_file_body) ) + ".tex" if not os.path.exists(result): print("Writing \"%s\" to %s" % ( "".join(expression), result )) new_body = template_tex_file_body.replace( TEX_TEXT_TO_REPLACE, expression ) with open(result, "w") as outfile: outfile.write(new_body) return result def get_null(): if os.name == "nt": return "NUL" return "/dev/null" def tex_to_dvi(tex_file): result = tex_file.replace(".tex", ".dvi" if not TEX_USE_CTEX else ".xdv") if not os.path.exists(result): commands = [ "latex", "-interaction=batchmode", "-halt-on-error", "-output-directory=" + TEX_DIR, tex_file, ">", get_null() ] if not TEX_USE_CTEX else [ "xelatex", "-no-pdf", "-interaction=batchmode", "-halt-on-error", "-output-directory=" + TEX_DIR, tex_file, ">", get_null() ] exit_code = os.system(" ".join(commands)) if exit_code != 0: log_file = tex_file.replace(".tex", ".log") raise Exception( ("Latex error converting to dvi. " if not TEX_USE_CTEX else "Xelatex error converting to xdv. ") + "See log output above or the log file: %s" % log_file) return result def dvi_to_svg(dvi_file, regen_if_exists=False): result = dvi_file.replace(".dvi" if not TEX_USE_CTEX else ".xdv", ".svg") if not os.path.exists(result): commands = [ "dvisvgm", dvi_file, "-n", "-v", "0", "-o", result, ">", get_null() ] os.system(" ".join(commands)) if TEX_FIX_SVG: commands = [ "cairosvg", result, "-f", "svg", "-o", result ] os.system(" ".join(commands)) return result
true
true
f7062fe4bf76b1aa71c448dfb0c4e42c9c04222c
3,384
py
Python
hypha/apply/users/tests/test_forms.py
OpenTechFund/opentech.fund
701b17f36ea27f891cd7e8a6743007302a52f34d
[ "BSD-3-Clause" ]
14
2018-10-05T14:34:21.000Z
2020-01-17T17:45:44.000Z
hypha/apply/users/tests/test_forms.py
OpenTechFund/WebApp
d6e2bb21a39d1fa7566cb60fe19f372dabfa5f0f
[ "BSD-3-Clause" ]
1,098
2017-12-15T11:23:03.000Z
2020-01-24T07:58:07.000Z
hypha/apply/users/tests/test_forms.py
OpenTechFund/WebApp
d6e2bb21a39d1fa7566cb60fe19f372dabfa5f0f
[ "BSD-3-Clause" ]
7
2018-01-09T10:13:06.000Z
2019-08-12T16:38:59.000Z
from django.forms.models import model_to_dict from django.test import TestCase from ..forms import EmailChangePasswordForm, ProfileForm from .factories import StaffFactory, UserFactory class BaseTestProfileForm(TestCase): def form_data(self, user, **values): fields = ProfileForm.Meta.fields data = model_to_dict(user, fields) data.update(**values) return data def submit_form(self, instance, **extra_data): form = ProfileForm(instance=instance, data=self.form_data(instance, **extra_data)) if form.is_valid(): form.save() return form class TestProfileForm(BaseTestProfileForm): def setUp(self): self.user = UserFactory() def test_email_unique(self): other_user = UserFactory() form = self.submit_form(self.user, email=other_user.email) self.assertFalse(form.is_valid()) self.user.refresh_from_db() self.assertNotEqual(self.user.email, other_user.email) def test_can_change_email(self): new_email = 'me@another.com' self.submit_form(self.user, email=new_email) self.user.refresh_from_db() self.assertEqual(self.user.email, new_email) def test_cant_set_slack_name(self): slack_name = '@foobar' self.submit_form(self.user, slack=slack_name) self.user.refresh_from_db() self.assertNotEqual(self.user.slack, slack_name) class TestStaffProfileForm(BaseTestProfileForm): def setUp(self): self.staff = StaffFactory() def test_cant_change_email(self): new_email = 'me@this.com' self.submit_form(self.staff, email=new_email) self.staff.refresh_from_db() self.assertNotEqual(new_email, self.staff.email) def test_can_set_slack_name(self): slack_name = '@foobar' self.submit_form(self.staff, slack=slack_name) self.staff.refresh_from_db() self.assertEqual(self.staff.slack, slack_name) def test_can_set_slack_name_with_trailing_space(self): slack_name = '@foobar' self.submit_form(self.staff, slack=slack_name) self.staff.refresh_from_db() self.assertEqual(self.staff.slack, slack_name) def test_cant_set_slack_name_with_space(self): slack_name = '@ foobar' form = self.submit_form(self.staff, slack=slack_name) self.assertFalse(form.is_valid()) self.staff.refresh_from_db() self.assertNotEqual(self.staff.slack, slack_name) def test_auto_prepend_at(self): slack_name = 'foobar' self.submit_form(self.staff, slack=slack_name) self.staff.refresh_from_db() self.assertEqual(self.staff.slack, '@' + slack_name) def test_can_clear_slack_name(self): slack_name = '' self.submit_form(self.staff, slack=slack_name) self.staff.refresh_from_db() self.assertEqual(self.staff.slack, slack_name) class TestEmailChangePasswordForm(TestCase): def setUp(self): self.user = UserFactory() def test_doesnt_error_on_null_slack_field(self): form = EmailChangePasswordForm(self.user) form.save('', '', None) def test_can_update_slack(self): slack_name = 'foobar' form = EmailChangePasswordForm(self.user) form.save('', '', slack_name) self.assertEqual(self.user.slack, slack_name)
31.626168
90
0.681738
from django.forms.models import model_to_dict from django.test import TestCase from ..forms import EmailChangePasswordForm, ProfileForm from .factories import StaffFactory, UserFactory class BaseTestProfileForm(TestCase): def form_data(self, user, **values): fields = ProfileForm.Meta.fields data = model_to_dict(user, fields) data.update(**values) return data def submit_form(self, instance, **extra_data): form = ProfileForm(instance=instance, data=self.form_data(instance, **extra_data)) if form.is_valid(): form.save() return form class TestProfileForm(BaseTestProfileForm): def setUp(self): self.user = UserFactory() def test_email_unique(self): other_user = UserFactory() form = self.submit_form(self.user, email=other_user.email) self.assertFalse(form.is_valid()) self.user.refresh_from_db() self.assertNotEqual(self.user.email, other_user.email) def test_can_change_email(self): new_email = 'me@another.com' self.submit_form(self.user, email=new_email) self.user.refresh_from_db() self.assertEqual(self.user.email, new_email) def test_cant_set_slack_name(self): slack_name = '@foobar' self.submit_form(self.user, slack=slack_name) self.user.refresh_from_db() self.assertNotEqual(self.user.slack, slack_name) class TestStaffProfileForm(BaseTestProfileForm): def setUp(self): self.staff = StaffFactory() def test_cant_change_email(self): new_email = 'me@this.com' self.submit_form(self.staff, email=new_email) self.staff.refresh_from_db() self.assertNotEqual(new_email, self.staff.email) def test_can_set_slack_name(self): slack_name = '@foobar' self.submit_form(self.staff, slack=slack_name) self.staff.refresh_from_db() self.assertEqual(self.staff.slack, slack_name) def test_can_set_slack_name_with_trailing_space(self): slack_name = '@foobar' self.submit_form(self.staff, slack=slack_name) self.staff.refresh_from_db() self.assertEqual(self.staff.slack, slack_name) def test_cant_set_slack_name_with_space(self): slack_name = '@ foobar' form = self.submit_form(self.staff, slack=slack_name) self.assertFalse(form.is_valid()) self.staff.refresh_from_db() self.assertNotEqual(self.staff.slack, slack_name) def test_auto_prepend_at(self): slack_name = 'foobar' self.submit_form(self.staff, slack=slack_name) self.staff.refresh_from_db() self.assertEqual(self.staff.slack, '@' + slack_name) def test_can_clear_slack_name(self): slack_name = '' self.submit_form(self.staff, slack=slack_name) self.staff.refresh_from_db() self.assertEqual(self.staff.slack, slack_name) class TestEmailChangePasswordForm(TestCase): def setUp(self): self.user = UserFactory() def test_doesnt_error_on_null_slack_field(self): form = EmailChangePasswordForm(self.user) form.save('', '', None) def test_can_update_slack(self): slack_name = 'foobar' form = EmailChangePasswordForm(self.user) form.save('', '', slack_name) self.assertEqual(self.user.slack, slack_name)
true
true
f70630c896da4674f42d33645d2791bd38f3c82f
308
py
Python
getRssTickerValues.py
shakedown1979/xbrlparser
9a0cf70b98dbd63f4d5aa81070dc701f9987ad8d
[ "Apache-2.0" ]
1
2020-08-02T13:36:38.000Z
2020-08-02T13:36:38.000Z
getRssTickerValues.py
shakedown1979/xbrlparser
9a0cf70b98dbd63f4d5aa81070dc701f9987ad8d
[ "Apache-2.0" ]
null
null
null
getRssTickerValues.py
shakedown1979/xbrlparser
9a0cf70b98dbd63f4d5aa81070dc701f9987ad8d
[ "Apache-2.0" ]
null
null
null
from daoRefactor2 import DAO from rssTickerInfo import rssTickerInfo import json import boto3 table = 'CompanyRSSFeed' dao = DAO(table) def main(): tickerValues = dao.getRssTickerValues('UNP') print(tickerValues) if __name__ == "__main__": # calling main function main()
18.117647
46
0.691558
from daoRefactor2 import DAO from rssTickerInfo import rssTickerInfo import json import boto3 table = 'CompanyRSSFeed' dao = DAO(table) def main(): tickerValues = dao.getRssTickerValues('UNP') print(tickerValues) if __name__ == "__main__": main()
true
true
f70630cda102862e51f6cc74baafa0df63c24f48
2,556
py
Python
utils/test/testapi/opnfv_testapi/resources/project_models.py
hwoarang/releng
fc8cb9f1bc3a37039d19ae7a5a17699d8a36bfb8
[ "Apache-2.0" ]
null
null
null
utils/test/testapi/opnfv_testapi/resources/project_models.py
hwoarang/releng
fc8cb9f1bc3a37039d19ae7a5a17699d8a36bfb8
[ "Apache-2.0" ]
null
null
null
utils/test/testapi/opnfv_testapi/resources/project_models.py
hwoarang/releng
fc8cb9f1bc3a37039d19ae7a5a17699d8a36bfb8
[ "Apache-2.0" ]
null
null
null
############################################################################## # Copyright (c) 2015 Orange # guyrodrigue.koffi@orange.com / koffirodrigue@gmail.com # All rights reserved. This program and the accompanying materials # are made available under the terms of the Apache License, Version 2.0 # which accompanies this distribution, and is available at # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## from opnfv_testapi.tornado_swagger import swagger @swagger.model() class ProjectCreateRequest(object): def __init__(self, name, description=''): self.name = name self.description = description def format(self): return { "name": self.name, "description": self.description, } @swagger.model() class ProjectUpdateRequest(object): def __init__(self, name='', description=''): self.name = name self.description = description def format(self): return { "name": self.name, "description": self.description, } @swagger.model() class Project(object): def __init__(self, name=None, _id=None, description=None, create_date=None): self._id = _id self.name = name self.description = description self.creation_date = create_date @staticmethod def from_dict(res_dict): if res_dict is None: return None t = Project() t._id = res_dict.get('_id') t.creation_date = res_dict.get('creation_date') t.name = res_dict.get('name') t.description = res_dict.get('description') return t def format(self): return { "name": self.name, "description": self.description, "creation_date": str(self.creation_date) } def format_http(self): return { "_id": str(self._id), "name": self.name, "description": self.description, "creation_date": str(self.creation_date), } @swagger.model() class Projects(object): """ @property projects: @ptype projects: C{list} of L{Project} """ def __init__(self): self.projects = list() @staticmethod def from_dict(res_dict): if res_dict is None: return None res = Projects() for project in res_dict.get('projects'): res.projects.append(Project.from_dict(project)) return res
26.905263
78
0.563772
from opnfv_testapi.tornado_swagger import swagger @swagger.model() class ProjectCreateRequest(object): def __init__(self, name, description=''): self.name = name self.description = description def format(self): return { "name": self.name, "description": self.description, } @swagger.model() class ProjectUpdateRequest(object): def __init__(self, name='', description=''): self.name = name self.description = description def format(self): return { "name": self.name, "description": self.description, } @swagger.model() class Project(object): def __init__(self, name=None, _id=None, description=None, create_date=None): self._id = _id self.name = name self.description = description self.creation_date = create_date @staticmethod def from_dict(res_dict): if res_dict is None: return None t = Project() t._id = res_dict.get('_id') t.creation_date = res_dict.get('creation_date') t.name = res_dict.get('name') t.description = res_dict.get('description') return t def format(self): return { "name": self.name, "description": self.description, "creation_date": str(self.creation_date) } def format_http(self): return { "_id": str(self._id), "name": self.name, "description": self.description, "creation_date": str(self.creation_date), } @swagger.model() class Projects(object): def __init__(self): self.projects = list() @staticmethod def from_dict(res_dict): if res_dict is None: return None res = Projects() for project in res_dict.get('projects'): res.projects.append(Project.from_dict(project)) return res
true
true