content
stringlengths
27
928k
path
stringlengths
4
230
size
int64
27
928k
nl_text
stringlengths
21
396k
nl_size
int64
21
396k
nl_language
stringlengths
2
3
nl_language_score
float64
0.04
1
import pandas as pd import gc import transformers from transformers import BertForSequenceClassification, BertTokenizerFast, Trainer, TrainingArguments from nlp import load_dataset, Dataset import torch import numpy as np from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer from sklearn.linear_model import SGDClassifier from sklearn.metrics import accuracy_score, precision_recall_fscore_support, classification_report, hamming_loss from sklearn.model_selection import train_test_split, GridSearchCV from sklearn.naive_bayes import MultinomialNB from sklearn.pipeline import Pipeline from random import sample, choices from joblib import dump, load def string_labels_to_int(Y): keys={} new_Y=[] for item in Y: if item in keys: new_Y.append(keys[item]) else: keys.update({item:len(keys)+1}) new_Y.append(keys[item]) return new_Y, keys def int_labels_to_list(Y,keys): new_Y=[] for item in Y: sublist=[0] * len(keys) sublist[item-1]=1 sublist=torch.tensor(sublist) new_Y.append(sublist) return new_Y class LTP: def __init__ (self, Xdata=None, Ydata=None, csv=None,xlsx=None,x_col='X',y_col='Y',models='all',test_frac=0.1,train_frac=0.9): if models=='all': self.model_list = [ 'bert-base-uncased', 'albert-base-v2', 'roberta-base', 'linear_SVM', 'multinomial_naive_bayesian',] elif models=='count-vectorizer': self.model_list = [ 'linear_SVM', 'multinomial_naive_bayesian',] elif models=='transformers': self.model_list = [ 'bert-base-uncased', 'albert-base-v2', 'roberta-base',] else: print('Models not recognized, the available options are currently "all", "count-vectorizer", and "transformers"') return if csv!=None and xlsx!= None and Xdata!=None: print("You have provided too much data, give just x and y data, or a csv or xlsx file!") return if csv!=None: csv_data=pd.read_csv(csv) Xdata=csv_data[x_col] Ydata=csv_data[y_col] if xlsx!=None: xlsx_data=pd.read_excel(xlsx) Xdata=xlsx_data[x_col] Ydata=xlsx_data[y_col] if isinstance(Xdata, pd.Series): print('converting pandas series to list') Xdata=list(Xdata) if isinstance(Ydata, pd.Series): print('converting pandas series to list') Ydata=list(Ydata) if Xdata==Ydata==None or (Xdata==None and Ydata!=None) or (Xdata!=None and Ydata==None): print('Either you have not put in your own data, or you have only put in X or Y data, loading default dataset...') self.train_dataset_raw, self.test_dataset_raw = load_dataset('imdb', split=['train', 'test']) X=self.train_dataset_raw['text']+self.test_dataset_raw['text'] Xdata = X Y=self.train_dataset_raw['label']+self.test_dataset_raw['label'] Ydata = Y keys=set(Y) else: X=Xdata Y=Ydata if all(isinstance(n, int) for n in Y): keys=set(Y) else: Y,keys=string_labels_to_int(Y) #add method to make min label 0 if min(Y)>=1: Y=[y-min(Y) for y in Y] if len(Xdata)<20: print('dataset is really small, using default test/train split (0.25)') test_frac=None train_frac=None if len(Xdata)<8: print('dataset is really too small, using default test/train split (0.5)') test_frac=0.5 train_frac=0.5 if len(Xdata)!=len(Ydata): print('ERROR: X data and Y data lengths are not the same size, they need to be!') return X_train, X_test, Y_train, Y_test = train_test_split(X, Y, stratify=Y, test_size=test_frac, train_size=train_frac) self.num_labels=len(keys) #self.train_dataset_raw_CNN = TensorDataset(X_train, int_labels_to_list(Y_train,keys)) #self.test_dataset_raw_CNN = TensorDataset(X_test, int_labels_to_list(Y_test,keys)) print('X_train length: ' + str(len(X_train))) print('X_test length: ' + str(len(X_test))) print('Y_train length: ' + str(len(Y_train))) print('Y_test length: ' + str(len(Y_test))) self.train_dataset_raw = Dataset.from_pandas(pd.DataFrame({'text':X_train, 'labels': Y_train})) self.test_dataset_raw = Dataset.from_pandas(pd.DataFrame({'text':X_test, 'labels': Y_test})) self.all_metrics = {} def compute_metrics(self, pred): labels = pred.label_ids preds = pred.predictions.argmax(-1) precision, recall, f1, _ = precision_recall_fscore_support(labels, preds, average='weighted') full_report = classification_report(labels, preds, output_dict=True) acc = accuracy_score(labels, preds) return { 'accuracy': acc, 'f1': f1, 'precision': precision, 'recall': recall, 'full_report': full_report } def get_metrics(self): return self.all_metrics def get_metrics_df(self): dic = self.get_metrics() df = pd.DataFrame.from_dict(dic) df = df.rename_axis("model_name", axis="columns").T df.reset_index(inplace=True) df.rename_axis() return df def print_metrics_table(self): dic = self.get_metrics() print("{:>25} {:>15} {:>15} {:>15} {:>15} {:>15}".format('Model', 'loss', 'accuracy', 'f1', 'precision', 'recall')) for k, v in dic.items(): print("{:>25} {:15.5} {:15.5} {:15.5} {:15.5} {:15.5}".format(k, v['eval_loss'], v['eval_accuracy'], v['eval_f1'], v['eval_precision'], v['eval_recall'])) def run(self, focused=False, focused_model=None, training_epochs=5): if focused==True: self.model_list=[focused_model] else: pass for model_name in self.model_list: training_args = TrainingArguments( output_dir='./results/'+model_name, num_train_epochs=training_epochs, per_device_train_batch_size=16, per_device_eval_batch_size=64, warmup_steps=500, weight_decay=0.01, #evaluate_during_training=True, logging_dir='./logs/'+model_name, ) model = None tokenizer = None print('Training on a dataset with ' +str(self.num_labels)+ ' labels') if model_name == "bert-base-uncased": model = BertForSequenceClassification.from_pretrained(model_name, num_labels=self.num_labels) tokenizer = BertTokenizerFast.from_pretrained(model_name) elif model_name == "albert-base-v2": tokenizer = transformers.AlbertTokenizer.from_pretrained('albert-base-v2') model = transformers.AlbertForSequenceClassification.from_pretrained('albert-base-v2', return_dict=True, num_labels=self.num_labels) elif model_name == "roberta-base": tokenizer = transformers.RobertaTokenizer.from_pretrained('roberta-base') model = transformers.RobertaForSequenceClassification.from_pretrained('roberta-base', return_dict=True, num_labels=self.num_labels) elif model_name == "linear_SVM": tokenizer = None model = 'linear_SVM' parameters={ 'vect__ngram_range': [(1, 1), (1, 2)], 'tfidf__use_idf': (True, False), 'clf__alpha': (5e-2, 1e-2,5e-3, 1e-3,5e-3), 'clf__penalty': ('l2', 'l1', 'elasticnet') } classifier=SGDClassifier(loss='hinge',random_state=42,max_iter=5,tol=None) elif model_name == "multinomial_naive_bayesian": tokenizer = None model = 'multinomial_naive_bayesian' parameters= { 'vect__ngram_range': [(1, 1), (1, 2)], 'tfidf__use_idf': (True, False), 'clf__alpha': (1,1e-1,1e-2, 1e-3,1e-4), 'clf__fit_prior': (True, False), } classifier=MultinomialNB() if not model or not tokenizer: #use 'assert' here instead? print("ERROR") def tokenize(batch): return tokenizer(batch['text'], padding='max_length', truncation=True) if tokenizer is not None: train_dataset = self.train_dataset_raw.map(tokenize, batched=True, batch_size=len(self.train_dataset_raw)) test_dataset = self.test_dataset_raw.map(tokenize, batched=True, batch_size=len(self.train_dataset_raw)) train_dataset.set_format('torch', columns=['input_ids', 'attention_mask', 'labels']) test_dataset.set_format('torch', columns=['input_ids', 'attention_mask', 'labels']) else: train_dataset = self.train_dataset_raw test_dataset = self.test_dataset_raw if model_name== "linear_SVM" or model_name== "multinomial_naive_bayesian": trainer=None pipeline = Pipeline([('vect', CountVectorizer()), ('tfidf', TfidfTransformer()), ('clf', classifier), ]) gs_clf = GridSearchCV(pipeline, parameters, cv=5, n_jobs=-1) if len(train_dataset['labels'])<25: print('not enough data to use a count vectorizer, sorry!') else: gs_ind=int(len(train_dataset['labels'])/10) #use a tenth of the training dataset to do gridsearch gs_clf = gs_clf.fit(train_dataset['text'][:gs_ind], train_dataset['labels'][:gs_ind]) best_params=gs_clf.best_params_ pipeline.set_params(**best_params) pipeline.fit(train_dataset['text'], train_dataset['labels']) prediction=pipeline.predict(test_dataset['text']) precision, recall, f1, _ = precision_recall_fscore_support(test_dataset['labels'], prediction, average=None) full_report=classification_report(test_dataset['labels'], prediction) acc = accuracy_score(test_dataset['labels'], prediction) loss=hamming_loss(test_dataset['labels'], prediction) curr_metrics={ 'eval_loss': loss, 'eval_accuracy': np.mean(acc), 'eval_f1': np.mean(f1), 'eval_precision': np.mean(precision), 'eval_recall': np.mean(recall), 'eval_full_report': full_report } dump(pipeline, model_name + "_model.joblib") print('best parameters are:') print(best_params) else: trainer = Trainer(model=model, args=training_args, compute_metrics=self.compute_metrics, train_dataset=train_dataset, eval_dataset=test_dataset ) trainer.train() curr_metrics = trainer.evaluate() trainer.save_model(model_name+"_model") self.all_metrics[model_name] = curr_metrics print(curr_metrics) # adding this fully solves the out of memory (OOM) error; https://github.com/huggingface/transformers/issues/1742 del model, tokenizer, trainer # these 2 lines may not be needed gc.collect() torch.cuda.empty_cache() def predict(self,model_name=None,focused=False,text=None): if text == None: print('you did not enter any text to classify, sorry') return if focused==True: if model_name == "linear_SVM" or model_name == "multinomial_naive_bayesian": clf = load('/content/'+model_name+'_model.joblib') y=clf.predict([text]) else: if model_name == "bert-base-uncased": model=BertForSequenceClassification.from_pretrained('/content/bert-base-uncased_model') tokenizer=BertTokenizerFast.from_pretrained('bert-base-uncased') text_classification= transformers.pipeline('sentiment-analysis', model=model, tokenizer=tokenizer) y=text_classification(text)[0] elif model_name == "albert-base-v2": model=transformers.AlbertForSequenceClassification.from_pretrained('/content/albert-base-v2_model') tokenizer=transformers.AlbertTokenizer.from_pretrained('albert-base-v2') text_classification= transformers.pipeline('sentiment-analysis', model=model, tokenizer=tokenizer) y=text_classification(text)[0] elif model_name == "roberta-base": model=transformers.RobertaForSequenceClassification.from_pretrained('/content/roberta-base_model') tokenizer=transformers.RobertaTokenizer.from_pretrained('roberta-base') text_classification= transformers.pipeline('sentiment-analysis', model=model, tokenizer=tokenizer) y=text_classification(text)[0] print(model_name) print(y) else: for model_name in self.model_list: if model_name == "linear_SVM" or model_name == "multinomial_naive_bayesian": clf = load('/content/'+model_name+'_model.joblib') y=clf.predict([text]) else: if model_name == "bert-base-uncased": model=BertForSequenceClassification.from_pretrained('/content/bert-base-uncased_model') tokenizer=BertTokenizerFast.from_pretrained('bert-base-uncased') text_classification= transformers.pipeline('sentiment-analysis', model=model, tokenizer=tokenizer) y=text_classification(text)[0] elif model_name == "albert-base-v2": model=transformers.AlbertForSequenceClassification.from_pretrained('/content/albert-base-v2_model') tokenizer=transformers.AlbertTokenizer.from_pretrained('albert-base-v2') text_classification= transformers.pipeline('sentiment-analysis', model=model, tokenizer=tokenizer) y=text_classification(text)[0] elif model_name == "roberta-base": model=transformers.RobertaForSequenceClassification.from_pretrained('/content/roberta-base_model') tokenizer=transformers.RobertaTokenizer.from_pretrained('roberta-base') text_classification= transformers.pipeline('sentiment-analysis', model=model, tokenizer=tokenizer) y=text_classification(text)[0] print(model_name) print(y)
lazytextpredict/basic_classification.py
13,153
add method to make min label 0self.train_dataset_raw_CNN = TensorDataset(X_train, int_labels_to_list(Y_train,keys))self.test_dataset_raw_CNN = TensorDataset(X_test, int_labels_to_list(Y_test,keys))evaluate_during_training=True,use 'assert' here instead?use a tenth of the training dataset to do gridsearch adding this fully solves the out of memory (OOM) error; https://github.com/huggingface/transformers/issues/1742 these 2 lines may not be needed
449
en
0.62937
# blender imports import bpy # utility imports import numpy as np import csv import random import importlib from src.TSSBase import TSSBase class TSSMeshHandle(TSSBase): """docstring for TSSMeshHandle""" def __init__(self): super(TSSMeshHandle, self).__init__() # class vars ################################################################################################### self._mesh_list = [] # list of mesh [list] self._mesh_obj_list = [] # list of mesh nodes [list] ############################################################################################ end of class vars # def reset_module(self): """ reset all local vars Args: None Returns: None """ # reset all mesh ############################################################################################ for mesh in self._mesh_obj_list: # reset mesh mesh.reset_module() # maybe obsolete in future versions del mesh ##################################################################################### end of reset all mesh # self.reset_base() self._mesh_list = [] self._mesh_obj_list = [] def activate_pass(self,pass_name, pass_cfg, keyframe=-1): """ enables specific pass Args: pass_name: name of pass to activate [string] pass_cfg: specific parameters for the pass [dict] keyframe: current frame number; if value > -1, this should enable also the setting of a keyframe [int] Returns: None """ for mesh in self._mesh_obj_list: mesh.activate_pass(pass_name=pass_name,pass_cfg=pass_cfg,keyframe=keyframe) def create(self,stage_dict): """ create function Args: stage_dict: dict of stages [dict] Returns: None """ self._create_meshes(cfg=self._cfg["MESHES"], general_cfg=self._cfg["GENERAL"], stage_dict=stage_dict) def _create_meshes(self,cfg,general_cfg,stage_dict): """ create function Args: cfg: list of mesh cfgs [list] general_cfg: general cfg [dict] stage_dict: dict of stages [dict] Returns: success code [boolean] """ _current_instance_label_count = 0 for ii, mesh in enumerate(cfg): try: # import module and create class ####################################################################### _module_name = "src.assets.meshes." + mesh["type"] _module = importlib.import_module(_module_name) _class = getattr(_module, mesh["type"]) _mesh = _class() ################################################################ end of import module and create class # # set pass params and create pass ###################################################################### # set general cfg _mesh.set_general_cfg(cfg=general_cfg) _mesh.set_stage_dict(stage_dict=stage_dict) # save name of material mesh['meshParams']['name'] = mesh["name"] # update mesh cfg _mesh.update_cfg(cfg=mesh["meshParams"]) # create material _instance_count, _instance_label_count = _mesh.create(instance_id_offset=_current_instance_label_count) _current_instance_label_count += _instance_label_count ############################################################### end of set pass params and create pass # # add pass to list self._mesh_obj_list.append(_mesh) self._mesh_list.append(_mesh.get_meshes()) except ImportError: # manage import error raise Exception("Cannot add mesh") return -1 return 0 def get_meshes(self): """ get all meshes Args: None Returns: list of meshes [list] """ return self._mesh_list def get_mesh_objs(self): """ get all mesh objects Args: None Returns: list of mesh objects [list] """ return self._mesh_obj_list
src/assets/handle/TSSMeshHandle.py
4,633
docstring for TSSMeshHandle create function Args: cfg: list of mesh cfgs [list] general_cfg: general cfg [dict] stage_dict: dict of stages [dict] Returns: success code [boolean] enables specific pass Args: pass_name: name of pass to activate [string] pass_cfg: specific parameters for the pass [dict] keyframe: current frame number; if value > -1, this should enable also the setting of a keyframe [int] Returns: None create function Args: stage_dict: dict of stages [dict] Returns: None get all mesh objects Args: None Returns: list of mesh objects [list] get all meshes Args: None Returns: list of meshes [list] reset all local vars Args: None Returns: None blender imports utility imports class vars list of mesh [list] list of mesh nodes [list] end of class vars reset all mesh reset mesh maybe obsolete in future versions end of reset all mesh import module and create class end of import module and create class set pass params and create pass set general cfg save name of material update mesh cfg create material end of set pass params and create pass add pass to list manage import error
1,208
en
0.494004
# # Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information # regarding copyright ownership. The ASF licenses this file # to you under the Apache License, Version 2.0 (the # "License"); you may not use this file except in compliance # with the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, # software distributed under the License is distributed on an # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. import logging import unittest from unittest import mock from flask_appbuilder import SQLA, Model, expose, has_access from flask_appbuilder.security.sqla import models as sqla_models from flask_appbuilder.views import BaseView, ModelView from sqlalchemy import Column, Date, Float, Integer, String from airflow import settings from airflow.exceptions import AirflowException from airflow.models import DagModel from airflow.security import permissions from airflow.www import app as application from airflow.www.utils import CustomSQLAInterface from tests.test_utils import fab_utils from tests.test_utils.db import clear_db_dags, clear_db_runs from tests.test_utils.mock_security_manager import MockSecurityManager READ_WRITE = {permissions.ACTION_CAN_READ, permissions.ACTION_CAN_EDIT} READ_ONLY = {permissions.ACTION_CAN_READ} logging.basicConfig(format='%(asctime)s:%(levelname)s:%(name)s:%(message)s') logging.getLogger().setLevel(logging.DEBUG) log = logging.getLogger(__name__) class SomeModel(Model): id = Column(Integer, primary_key=True) field_string = Column(String(50), unique=True, nullable=False) field_integer = Column(Integer()) field_float = Column(Float()) field_date = Column(Date()) def __repr__(self): return str(self.field_string) class SomeModelView(ModelView): datamodel = CustomSQLAInterface(SomeModel) base_permissions = [ 'can_list', 'can_show', 'can_add', permissions.ACTION_CAN_EDIT, permissions.ACTION_CAN_DELETE, ] list_columns = ['field_string', 'field_integer', 'field_float', 'field_date'] class SomeBaseView(BaseView): route_base = '' @expose('/some_action') @has_access def some_action(self): return "action!" class TestSecurity(unittest.TestCase): @classmethod def setUpClass(cls): settings.configure_orm() cls.session = settings.Session cls.app = application.create_app(testing=True) cls.appbuilder = cls.app.appbuilder # pylint: disable=no-member cls.app.config['WTF_CSRF_ENABLED'] = False cls.security_manager = cls.appbuilder.sm cls.delete_roles() def setUp(self): clear_db_runs() clear_db_dags() self.db = SQLA(self.app) self.appbuilder.add_view(SomeBaseView, "SomeBaseView", category="BaseViews") self.appbuilder.add_view(SomeModelView, "SomeModelView", category="ModelViews") log.debug("Complete setup!") @classmethod def delete_roles(cls): for role_name in ['team-a', 'MyRole1', 'MyRole5', 'Test_Role', 'MyRole3', 'MyRole2']: fab_utils.delete_role(cls.app, role_name) def expect_user_is_in_role(self, user, rolename): self.security_manager.init_role(rolename, []) role = self.security_manager.find_role(rolename) if not role: self.security_manager.add_role(rolename) role = self.security_manager.find_role(rolename) user.roles = [role] self.security_manager.update_user(user) def assert_user_has_dag_perms(self, perms, dag_id, user=None): for perm in perms: self.assertTrue( self._has_dag_perm(perm, dag_id, user), f"User should have '{perm}' on DAG '{dag_id}'", ) def assert_user_does_not_have_dag_perms(self, dag_id, perms, user=None): for perm in perms: self.assertFalse( self._has_dag_perm(perm, dag_id, user), f"User should not have '{perm}' on DAG '{dag_id}'", ) def _has_dag_perm(self, perm, dag_id, user): # if not user: # user = self.user return self.security_manager.has_access(perm, self.security_manager.prefixed_dag_id(dag_id), user) def tearDown(self): clear_db_runs() clear_db_dags() self.appbuilder = None self.app = None self.db = None log.debug("Complete teardown!") def test_init_role_baseview(self): role_name = 'MyRole3' role_perms = [('can_some_action', 'SomeBaseView')] self.security_manager.init_role(role_name, perms=role_perms) role = self.appbuilder.sm.find_role(role_name) self.assertIsNotNone(role) self.assertEqual(len(role_perms), len(role.permissions)) def test_init_role_modelview(self): role_name = 'MyRole2' role_perms = [ ('can_list', 'SomeModelView'), ('can_show', 'SomeModelView'), ('can_add', 'SomeModelView'), (permissions.ACTION_CAN_EDIT, 'SomeModelView'), (permissions.ACTION_CAN_DELETE, 'SomeModelView'), ] self.security_manager.init_role(role_name, role_perms) role = self.appbuilder.sm.find_role(role_name) self.assertIsNotNone(role) self.assertEqual(len(role_perms), len(role.permissions)) def test_update_and_verify_permission_role(self): role_name = 'Test_Role' self.security_manager.init_role(role_name, []) role = self.security_manager.find_role(role_name) perm = self.security_manager.find_permission_view_menu(permissions.ACTION_CAN_EDIT, 'RoleModelView') self.security_manager.add_permission_role(role, perm) role_perms_len = len(role.permissions) self.security_manager.init_role(role_name, []) new_role_perms_len = len(role.permissions) self.assertEqual(role_perms_len, new_role_perms_len) def test_get_user_roles(self): user = mock.MagicMock() user.is_anonymous = False roles = self.appbuilder.sm.find_role('Admin') user.roles = roles self.assertEqual(self.security_manager.get_user_roles(user), roles) def test_get_user_roles_for_anonymous_user(self): viewer_role_perms = { (permissions.ACTION_CAN_READ, permissions.RESOURCE_CONFIG), (permissions.ACTION_CAN_READ, permissions.RESOURCE_DAG), (permissions.ACTION_CAN_READ, permissions.RESOURCE_DAG_CODE), (permissions.ACTION_CAN_READ, permissions.RESOURCE_DAG_RUN), (permissions.ACTION_CAN_READ, permissions.RESOURCE_IMPORT_ERROR), (permissions.ACTION_CAN_READ, permissions.RESOURCE_AUDIT_LOG), (permissions.ACTION_CAN_READ, permissions.RESOURCE_JOB), (permissions.ACTION_CAN_READ, permissions.RESOURCE_PLUGIN), (permissions.ACTION_CAN_READ, permissions.RESOURCE_SLA_MISS), (permissions.ACTION_CAN_READ, permissions.RESOURCE_TASK_INSTANCE), (permissions.ACTION_CAN_READ, permissions.RESOURCE_TASK_LOG), (permissions.ACTION_CAN_READ, permissions.RESOURCE_XCOM), (permissions.ACTION_CAN_READ, permissions.RESOURCE_WEBSITE), (permissions.ACTION_CAN_ACCESS_MENU, permissions.RESOURCE_BROWSE_MENU), (permissions.ACTION_CAN_ACCESS_MENU, permissions.RESOURCE_DAG_RUN), (permissions.ACTION_CAN_ACCESS_MENU, permissions.RESOURCE_DOCS_LINK), (permissions.ACTION_CAN_ACCESS_MENU, permissions.RESOURCE_DOCS_MENU), (permissions.ACTION_CAN_ACCESS_MENU, permissions.RESOURCE_JOB), (permissions.ACTION_CAN_ACCESS_MENU, permissions.RESOURCE_AUDIT_LOG), (permissions.ACTION_CAN_ACCESS_MENU, permissions.RESOURCE_PLUGIN), (permissions.ACTION_CAN_ACCESS_MENU, permissions.RESOURCE_SLA_MISS), (permissions.ACTION_CAN_ACCESS_MENU, permissions.RESOURCE_TASK_INSTANCE), (permissions.ACTION_CAN_THIS_FORM_GET, permissions.RESOURCE_RESET_MY_PASSWORD_VIEW), (permissions.ACTION_CAN_THIS_FORM_POST, permissions.RESOURCE_RESET_MY_PASSWORD_VIEW), (permissions.ACTION_RESETMYPASSWORD, permissions.RESOURCE_USER_DB_MODELVIEW), (permissions.ACTION_CAN_THIS_FORM_GET, permissions.RESOURCE_USERINFO_EDIT_VIEW), (permissions.ACTION_CAN_THIS_FORM_POST, permissions.RESOURCE_USERINFO_EDIT_VIEW), (permissions.ACTION_USERINFOEDIT, permissions.RESOURCE_USER_DB_MODELVIEW), (permissions.ACTION_CAN_USERINFO, permissions.RESOURCE_USER_DB_MODELVIEW), (permissions.ACTION_CAN_USERINFO, permissions.RESOURCE_USER_OID_MODELVIEW), (permissions.ACTION_CAN_USERINFO, permissions.RESOURCE_USER_LDAP_MODELVIEW), (permissions.ACTION_CAN_USERINFO, permissions.RESOURCE_USER_OAUTH_MODELVIEW), (permissions.ACTION_CAN_USERINFO, permissions.RESOURCE_USER_REMOTEUSER_MODELVIEW), } self.app.config['AUTH_ROLE_PUBLIC'] = 'Viewer' with self.app.app_context(): user = mock.MagicMock() user.is_anonymous = True perms_views = set() for role in self.security_manager.get_user_roles(user): perms_views.update( {(perm_view.permission.name, perm_view.view_menu.name) for perm_view in role.permissions} ) self.assertEqual(perms_views, viewer_role_perms) @mock.patch('airflow.www.security.AirflowSecurityManager.get_user_roles') def test_get_all_permissions_views(self, mock_get_user_roles): role_name = 'MyRole5' role_perm = 'can_some_action' role_vm = 'SomeBaseView' username = 'get_all_permissions_views' with self.app.app_context(): user = fab_utils.create_user( self.app, username, role_name, permissions=[ (role_perm, role_vm), ], ) role = user.roles[0] mock_get_user_roles.return_value = [role] self.assertEqual(self.security_manager.get_all_permissions_views(), {(role_perm, role_vm)}) mock_get_user_roles.return_value = [] self.assertEqual(len(self.security_manager.get_all_permissions_views()), 0) def test_get_accessible_dag_ids(self): role_name = 'MyRole1' permission_action = [permissions.ACTION_CAN_READ] dag_id = 'dag_id' username = "ElUser" user = fab_utils.create_user( self.app, username, role_name, permissions=[ (permissions.ACTION_CAN_READ, permissions.RESOURCE_DAG), (permissions.ACTION_CAN_READ, permissions.RESOURCE_DAG), ], ) dag_model = DagModel(dag_id=dag_id, fileloc="/tmp/dag_.py", schedule_interval="2 2 * * *") self.session.add(dag_model) self.session.commit() self.security_manager.sync_perm_for_dag( # type: ignore # pylint: disable=no-member dag_id, access_control={role_name: permission_action} ) self.assertEqual(self.security_manager.get_accessible_dag_ids(user), {'dag_id'}) def test_dont_get_inaccessible_dag_ids_for_dag_resource_permission(self): # In this test case, # get_readable_dag_ids() don't return DAGs to which the user has CAN_EDIT permission username = "Monsieur User" role_name = "MyRole1" permission_action = [permissions.ACTION_CAN_EDIT] dag_id = "dag_id" user = fab_utils.create_user( self.app, username, role_name, permissions=[ (permissions.ACTION_CAN_EDIT, permissions.RESOURCE_DAG), ], ) dag_model = DagModel(dag_id=dag_id, fileloc="/tmp/dag_.py", schedule_interval="2 2 * * *") self.session.add(dag_model) self.session.commit() self.security_manager.sync_perm_for_dag( # type: ignore # pylint: disable=no-member dag_id, access_control={role_name: permission_action} ) self.assertEqual(self.security_manager.get_readable_dag_ids(user), set()) @mock.patch('airflow.www.security.AirflowSecurityManager._has_view_access') def test_has_access(self, mock_has_view_access): user = mock.MagicMock() user.is_anonymous = False mock_has_view_access.return_value = True self.assertTrue(self.security_manager.has_access('perm', 'view', user)) def test_sync_perm_for_dag_creates_permissions_on_view_menus(self): test_dag_id = 'TEST_DAG' prefixed_test_dag_id = f'DAG:{test_dag_id}' self.security_manager.sync_perm_for_dag(test_dag_id, access_control=None) self.assertIsNotNone( self.security_manager.find_permission_view_menu(permissions.ACTION_CAN_READ, prefixed_test_dag_id) ) self.assertIsNotNone( self.security_manager.find_permission_view_menu(permissions.ACTION_CAN_EDIT, prefixed_test_dag_id) ) @mock.patch('airflow.www.security.AirflowSecurityManager._has_perm') @mock.patch('airflow.www.security.AirflowSecurityManager._has_role') def test_has_all_dag_access(self, mock_has_role, mock_has_perm): mock_has_role.return_value = True self.assertTrue(self.security_manager.has_all_dags_access()) mock_has_role.return_value = False mock_has_perm.return_value = False self.assertFalse(self.security_manager.has_all_dags_access()) mock_has_perm.return_value = True self.assertTrue(self.security_manager.has_all_dags_access()) def test_access_control_with_non_existent_role(self): with self.assertRaises(AirflowException) as context: self.security_manager.sync_perm_for_dag( dag_id='access-control-test', access_control={ 'this-role-does-not-exist': [permissions.ACTION_CAN_EDIT, permissions.ACTION_CAN_READ] }, ) self.assertIn("role does not exist", str(context.exception)) def test_all_dag_access_doesnt_give_non_dag_access(self): username = 'dag_access_user' role_name = 'dag_access_role' with self.app.app_context(): user = fab_utils.create_user( self.app, username, role_name, permissions=[ (permissions.ACTION_CAN_READ, permissions.RESOURCE_DAG), (permissions.ACTION_CAN_READ, permissions.RESOURCE_DAG), ], ) self.assertTrue( self.security_manager.has_access(permissions.ACTION_CAN_READ, permissions.RESOURCE_DAG, user) ) self.assertFalse( self.security_manager.has_access( permissions.ACTION_CAN_READ, permissions.RESOURCE_TASK_INSTANCE, user ) ) def test_access_control_with_invalid_permission(self): invalid_permissions = [ 'can_varimport', # a real permission, but not a member of DAG_PERMS 'can_eat_pudding', # clearly not a real permission ] username = "LaUser" user = fab_utils.create_user( self.app, username=username, role_name='team-a', ) for permission in invalid_permissions: self.expect_user_is_in_role(user, rolename='team-a') with self.assertRaises(AirflowException) as context: self.security_manager.sync_perm_for_dag( 'access_control_test', access_control={'team-a': {permission}} ) self.assertIn("invalid permissions", str(context.exception)) def test_access_control_is_set_on_init(self): username = 'access_control_is_set_on_init' role_name = 'team-a' with self.app.app_context(): user = fab_utils.create_user( self.app, username, role_name, permissions=[], ) self.expect_user_is_in_role(user, rolename='team-a') self.security_manager.sync_perm_for_dag( 'access_control_test', access_control={'team-a': [permissions.ACTION_CAN_EDIT, permissions.ACTION_CAN_READ]}, ) self.assert_user_has_dag_perms( perms=[permissions.ACTION_CAN_EDIT, permissions.ACTION_CAN_READ], dag_id='access_control_test', user=user, ) self.expect_user_is_in_role(user, rolename='NOT-team-a') self.assert_user_does_not_have_dag_perms( perms=[permissions.ACTION_CAN_EDIT, permissions.ACTION_CAN_READ], dag_id='access_control_test', user=user, ) def test_access_control_stale_perms_are_revoked(self): username = 'access_control_stale_perms_are_revoked' role_name = 'team-a' with self.app.app_context(): user = fab_utils.create_user( self.app, username, role_name, permissions=[], ) self.expect_user_is_in_role(user, rolename='team-a') self.security_manager.sync_perm_for_dag( 'access_control_test', access_control={'team-a': READ_WRITE} ) self.assert_user_has_dag_perms(perms=READ_WRITE, dag_id='access_control_test', user=user) self.security_manager.sync_perm_for_dag( 'access_control_test', access_control={'team-a': READ_ONLY} ) self.assert_user_has_dag_perms( perms=[permissions.ACTION_CAN_READ], dag_id='access_control_test', user=user ) self.assert_user_does_not_have_dag_perms( perms=[permissions.ACTION_CAN_EDIT], dag_id='access_control_test', user=user ) def test_no_additional_dag_permission_views_created(self): ab_perm_view_role = sqla_models.assoc_permissionview_role self.security_manager.sync_roles() num_pv_before = self.db.session().query(ab_perm_view_role).count() self.security_manager.sync_roles() num_pv_after = self.db.session().query(ab_perm_view_role).count() self.assertEqual(num_pv_before, num_pv_after) def test_override_role_vm(self): test_security_manager = MockSecurityManager(appbuilder=self.appbuilder) self.assertEqual(len(test_security_manager.VIEWER_VMS), 1) self.assertEqual(test_security_manager.VIEWER_VMS, {'Airflow'})
tests/www/test_security.py
19,165
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. pylint: disable=no-member if not user: user = self.user type: ignore pylint: disable=no-member In this test case, get_readable_dag_ids() don't return DAGs to which the user has CAN_EDIT permission type: ignore pylint: disable=no-member a real permission, but not a member of DAG_PERMS clearly not a real permission
1,075
en
0.865774
from typing import Any, Dict, Set import orjson from zerver.lib.test_classes import ZulipTestCase from zerver.lib.test_helpers import EventInfo, capture_event from zerver.lib.user_status import get_user_info_dict, update_user_status from zerver.models import UserProfile, UserStatus, get_client def get_away_user_ids(realm_id: int) -> Set[int]: user_dict = get_user_info_dict(realm_id) return { int(user_id) for user_id in user_dict if user_dict[user_id].get('away') } def user_info(user: UserProfile) -> Dict[str, Any]: user_dict = get_user_info_dict(user.realm_id) return user_dict.get(str(user.id), dict()) class UserStatusTest(ZulipTestCase): def test_basics(self) -> None: cordelia = self.example_user('cordelia') hamlet = self.example_user('hamlet') king_lear = self.lear_user('king') realm_id = hamlet.realm_id away_user_ids = get_away_user_ids(realm_id=realm_id) self.assertEqual(away_user_ids, set()) client1 = get_client('web') client2 = get_client('ZT') update_user_status( user_profile_id=hamlet.id, status=UserStatus.AWAY, status_text=None, client_id=client1.id, ) away_user_ids = get_away_user_ids(realm_id=realm_id) self.assertEqual(away_user_ids, {hamlet.id}) # Test that second client just updates # the record. We only store one record # per user. The user's status transcends # clients; we only store the client for # reference and to maybe reconcile timeout # situations. update_user_status( user_profile_id=hamlet.id, status=UserStatus.AWAY, status_text='out to lunch', client_id=client2.id, ) self.assertEqual( user_info(hamlet), dict(away=True, status_text='out to lunch'), ) away_user_ids = get_away_user_ids(realm_id=realm_id) self.assertEqual(away_user_ids, {hamlet.id}) rec_count = UserStatus.objects.filter(user_profile_id=hamlet.id).count() self.assertEqual(rec_count, 1) # Setting status_text to None causes it be ignored. update_user_status( user_profile_id=hamlet.id, status=UserStatus.NORMAL, status_text=None, client_id=client2.id, ) self.assertEqual( user_info(hamlet), dict(status_text='out to lunch'), ) # Clear the status_text now. update_user_status( user_profile_id=hamlet.id, status=None, status_text='', client_id=client2.id, ) self.assertEqual( user_info(hamlet), dict(), ) away_user_ids = get_away_user_ids(realm_id=realm_id) self.assertEqual(away_user_ids, set()) # Now set away status for three different users across # two realms. update_user_status( user_profile_id=hamlet.id, status=UserStatus.AWAY, status_text=None, client_id=client1.id, ) update_user_status( user_profile_id=cordelia.id, status=UserStatus.AWAY, status_text=None, client_id=client2.id, ) update_user_status( user_profile_id=king_lear.id, status=UserStatus.AWAY, status_text=None, client_id=client2.id, ) away_user_ids = get_away_user_ids(realm_id=realm_id) self.assertEqual(away_user_ids, {cordelia.id, hamlet.id}) away_user_ids = get_away_user_ids(realm_id=king_lear.realm.id) self.assertEqual(away_user_ids, {king_lear.id}) # Set Hamlet to NORMAL but in a meeting. update_user_status( user_profile_id=hamlet.id, status=UserStatus.NORMAL, status_text='in a meeting', client_id=client2.id, ) self.assertEqual( user_info(hamlet), dict(status_text='in a meeting'), ) away_user_ids = get_away_user_ids(realm_id=realm_id) self.assertEqual(away_user_ids, {cordelia.id}) def test_endpoints(self) -> None: hamlet = self.example_user('hamlet') realm_id = hamlet.realm_id self.login_user(hamlet) # Try to omit parameter--this should be an error. payload: Dict[str, Any] = dict() result = self.client_post('/json/users/me/status', payload) self.assert_json_error(result, "Client did not pass any new values.") # Try a long message. long_text = 'x' * 61 payload = dict(status_text=long_text) result = self.client_post('/json/users/me/status', payload) self.assert_json_error(result, "status_text is too long (limit: 60 characters)") payload = dict( away=orjson.dumps(True).decode(), status_text='on vacation', ) event_info = EventInfo() with capture_event(event_info): result = self.client_post('/json/users/me/status', payload) self.assert_json_success(result) self.assertEqual( event_info.payload, dict(type='user_status', user_id=hamlet.id, away=True, status_text='on vacation'), ) self.assertEqual( user_info(hamlet), dict(away=True, status_text='on vacation'), ) # Now revoke "away" status. payload = dict(away=orjson.dumps(False).decode()) event_info = EventInfo() with capture_event(event_info): result = self.client_post('/json/users/me/status', payload) self.assert_json_success(result) self.assertEqual( event_info.payload, dict(type='user_status', user_id=hamlet.id, away=False), ) away_user_ids = get_away_user_ids(realm_id=realm_id) self.assertEqual(away_user_ids, set()) # And now just update your info. # The server will trim the whitespace here. payload = dict(status_text=' in office ') event_info = EventInfo() with capture_event(event_info): result = self.client_post('/json/users/me/status', payload) self.assert_json_success(result) self.assertEqual( event_info.payload, dict(type='user_status', user_id=hamlet.id, status_text='in office'), ) self.assertEqual( user_info(hamlet), dict(status_text='in office'), ) # And finally clear your info. payload = dict(status_text='') event_info = EventInfo() with capture_event(event_info): result = self.client_post('/json/users/me/status', payload) self.assert_json_success(result) self.assertEqual( event_info.payload, dict(type='user_status', user_id=hamlet.id, status_text=''), ) self.assertEqual( get_user_info_dict(realm_id=realm_id), {}, )
zerver/tests/test_user_status.py
7,189
Test that second client just updates the record. We only store one record per user. The user's status transcends clients; we only store the client for reference and to maybe reconcile timeout situations. Setting status_text to None causes it be ignored. Clear the status_text now. Now set away status for three different users across two realms. Set Hamlet to NORMAL but in a meeting. Try to omit parameter--this should be an error. Try a long message. Now revoke "away" status. And now just update your info. The server will trim the whitespace here. And finally clear your info.
582
en
0.835098
''' DCGAN - MNIST Grayscale Handwritten Digits. Ref: https://machinelearningmastery.com/generative_adversarial_networks/ ''' # import tensorflow.python.ops.numpy_ops.np_config from data import loadRealSamples from generator import createGenerator from discriminator import createDiscriminator from gan import createGan, train if __name__ == '__main__': latentDim = 100 dataset = loadRealSamples() discriminator = createDiscriminator() generator = createGenerator(latentDim) gan = createGan(discriminator, generator) train(discriminator, generator, gan, dataset, latentDim)
main.py
600
DCGAN - MNIST Grayscale Handwritten Digits. Ref: https://machinelearningmastery.com/generative_adversarial_networks/ import tensorflow.python.ops.numpy_ops.np_config
168
en
0.435238
# -*- coding: utf-8 -*- import io import json import re import os import urllib.error, urllib.parse from collections import defaultdict from contextlib import closing from .. import biblio from .. import config from .. import constants from ..h import * from ..DefaultOrderedDict import DefaultOrderedDict from ..messages import * from ..Spec import Spec TEST_DIR = os.path.abspath(os.path.join(config.scriptPath(), "..", "tests")) def findTestFiles(): for root, dirnames, filenames in os.walk(TEST_DIR): for filename in filenames: if filename.endswith(".bs") and "/github/" in root: yield os.path.join(root, filename) def testNameForPath(path): if path.startswith(TEST_DIR): return path[len(TEST_DIR)+1:] return path def update(path, dryRun=False): return # early exit while working on this... say("Downloading backref data...") constants.quiet = float("inf") if not dryRun: specs = defaultdict(dict) backrefs = defaultdict(lambda: defaultdict(list)) for i,testPath in enumerate(findTestFiles()): if i > 1: break print(i,testNameForPath(testPath)) doc = Spec(inputFilename=testPath) doc.preprocess() if doc.md.ED: url = doc.md.ED elif doc.md.TR: url = doc.md.TR else: continue referencingShortname = doc.md.vshortname for ref in processRefs(doc.externalRefsUsed): _,_,referencedID = ref.url.partition("#") referencedID = urllib.parse.unquote(referencedID) referencedShortname = ref.spec referencingLinks = findAll("[href='{0}']".format(ref.url), doc) referencingIDs = [link.get("id") for link in referencingLinks if link.get("id")] referencingURLs = ["{0}#{1}".format(url, id) for id in referencingIDs] backrefs[referencedShortname][referencedID].append({ "shortname": referencingShortname, "urls": referencingURLs }) print(config.printjson(backrefs)) def processRefs(refs): seenRefs = set() # shape is {spec: {reftext: {forKey: ref}}}, just collect all the refs for keysByText in refs.values(): for refsByKey in keysByText.values(): for ref in refsByKey.values(): key = (ref.url, ref.spec) if key not in seenRefs: yield ref seenRefs.add(key) ignoredSpecs = { "css-foo-1", "css-typed-om-2", "css-2015-0", "d0???-1", "svg-color-1", "{{repo}}-1" }
bikeshed/update/updateBackRefs.py
2,729
-*- coding: utf-8 -*- early exit while working on this... shape is {spec: {reftext: {forKey: ref}}}, just collect all the refs
126
en
0.838236
#!/usr/bin/env/python3 # Modified by contributors from Intel Labs """ Create json with config tag """ import os import math def create_json_file(config): config_file = os.path.join(os.environ['TVM_HOME'],'3rdparty/vta-hw/config/vta_config.json') vta_params = config.split('_') gemm_params = vta_params[0].split('x') batch = int(math.log(int(gemm_params[0]),2)) blockIn = int(math.log(int(gemm_params[1]),2)) blockOut = int(math.log(int(gemm_params[2]),2)) try: fin = open(config_file, 'rt') data = fin.read() fin.close() data = data.replace('"LOG_BATCH" : 0', f'"LOG_BATCH" : {batch}') data = data.replace('"LOG_BLOCK_IN" : 4', f'"LOG_BLOCK_IN" : {blockIn}') data = data.replace('"LOG_BLOCK_OUT" : 4', f'"LOG_BLOCK_OUT" : {blockOut}') data = data.replace('"LOG_UOP_BUFF_SIZE" : 15', f'"LOG_UOP_BUFF_SIZE" : {vta_params[1]}') data = data.replace('"LOG_INP_BUFF_SIZE" : 15', f'"LOG_INP_BUFF_SIZE" : {vta_params[2]}') data = data.replace('"LOG_WGT_BUFF_SIZE" : 18', f'"LOG_WGT_BUFF_SIZE" : {vta_params[3]}') data = data.replace('"LOG_ACC_BUFF_SIZE" : 17', f'"LOG_ACC_BUFF_SIZE" : {vta_params[4]}') except IOError: print(f'Cannot open config file {config_file} for reading default config') new_config_file = os.path.join(os.environ['TVM_HOME'], '3rdparty/vta-hw/config', f'{config}.json') try: json_file = open(new_config_file, 'wt') json_file.write(data) json_file.close() print(f'New config written to {new_config_file}') except IOError: print(f'Cannot open config file {new_config_file} for writing new config') if __name__ == '__main__': import sys from argparse import ArgumentParser if len(sys.argv) < 2: sys.exit("At least 1 argument is required") else: if sys.argv[0].endswith('create_json.py'): ap = ArgumentParser(description='Create VTA json files with config and target') ap.add_argument('-c', '--config', type=str, default='1x16x16_15_15_18_17', help='VTA config (default: %(default)s)') args=ap.parse_args() create_json_file(args.config)
apps/params/create_json.py
2,184
Create json with config tag !/usr/bin/env/python3 Modified by contributors from Intel Labs
91
en
0.618127
# Generated by Django 2.2.20 on 2021-04-27 18:12 from django.db import migrations class Migration(migrations.Migration): """ Clean records in the `CERTIFICATES_GENERATEDCERTIFICATE` table that are in the downloadable state but also have old errors still part of their certificate record. As part of this migration we are also altering the Managers for the GeneratedCertificate model. We need the ability to access the `objects` attribute for the cleanup. """ def cleanup_certificate_records(apps, schema_editor): GeneratedCertificate = apps.get_model('certificates', 'GeneratedCertificate') GeneratedCertificate.objects.filter(status='downloadable').exclude(error_reason='').update(error_reason='') dependencies = [ ('certificates', '0024_delete_allowlistgenerationconfiguration'), ] operations = [ migrations.AlterModelManagers( name='generatedcertificate', managers=[ ], ), migrations.RunPython(cleanup_certificate_records, reverse_code=migrations.RunPython.noop) ]
Part-03-Understanding-Software-Crafting-Your-Own-Tools/models/edx-platform/lms/djangoapps/certificates/migrations/0025_cleanup_certificate_errors.py
1,103
Clean records in the `CERTIFICATES_GENERATEDCERTIFICATE` table that are in the downloadable state but also have old errors still part of their certificate record. As part of this migration we are also altering the Managers for the GeneratedCertificate model. We need the ability to access the `objects` attribute for the cleanup. Generated by Django 2.2.20 on 2021-04-27 18:12
379
en
0.939255
# Copyright 2010 United States Government as represented by the # Administrator of the National Aeronautics and Space Administration. # 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. """ Volume manager manages creating, attaching, detaching, and persistent storage. Persistent storage volumes keep their state independent of instances. You can attach to an instance, terminate the instance, spawn a new instance (even one from a different image) and re-attach the volume with the same data intact. **Related Flags** :volume_topic: What :mod:`rpc` topic to listen to (default: `cinder-volume`). :volume_manager: The module name of a class derived from :class:`manager.Manager` (default: :class:`cinder.volume.manager.Manager`). :volume_driver: Used by :class:`Manager`. Defaults to :class:`cinder.volume.drivers.lvm.LVMVolumeDriver`. :volume_group: Name of the group that will contain exported volumes (default: `cinder-volumes`) :num_shell_tries: Number of times to attempt to run commands (default: 3) """ import requests import time from oslo_config import cfg from oslo_log import log as logging import oslo_messaging as messaging from oslo_serialization import jsonutils from oslo_service import periodic_task from oslo_utils import excutils from oslo_utils import importutils from oslo_utils import timeutils from oslo_utils import units from oslo_utils import uuidutils profiler = importutils.try_import('osprofiler.profiler') import six from taskflow import exceptions as tfe from cinder import compute from cinder import context from cinder import exception from cinder import flow_utils from cinder.i18n import _, _LE, _LI, _LW from cinder.image import cache as image_cache from cinder.image import glance from cinder import manager from cinder.message import api as message_api from cinder.message import defined_messages from cinder.message import resource_types from cinder import objects from cinder.objects import fields from cinder import quota from cinder import utils from cinder import volume as cinder_volume from cinder.volume import configuration as config from cinder.volume.flows.manager import create_volume from cinder.volume.flows.manager import manage_existing from cinder.volume.flows.manager import manage_existing_snapshot from cinder.volume import rpcapi as volume_rpcapi from cinder.volume import utils as vol_utils from cinder.volume import volume_types LOG = logging.getLogger(__name__) QUOTAS = quota.QUOTAS CGQUOTAS = quota.CGQUOTAS VALID_REMOVE_VOL_FROM_CG_STATUS = ( 'available', 'in-use', 'error', 'error_deleting') VALID_ADD_VOL_TO_CG_STATUS = ( 'available', 'in-use') VALID_CREATE_CG_SRC_SNAP_STATUS = (fields.SnapshotStatus.AVAILABLE,) VALID_CREATE_CG_SRC_CG_STATUS = ('available',) volume_manager_opts = [ cfg.StrOpt('volume_driver', default='cinder.volume.drivers.lvm.LVMVolumeDriver', help='Driver to use for volume creation'), cfg.IntOpt('migration_create_volume_timeout_secs', default=300, help='Timeout for creating the volume to migrate to ' 'when performing volume migration (seconds)'), cfg.BoolOpt('volume_service_inithost_offload', default=False, help='Offload pending volume delete during ' 'volume service startup'), cfg.StrOpt('zoning_mode', help='FC Zoning mode configured'), cfg.StrOpt('extra_capabilities', default='{}', help='User defined capabilities, a JSON formatted string ' 'specifying key/value pairs. The key/value pairs can ' 'be used by the CapabilitiesFilter to select between ' 'backends when requests specify volume types. For ' 'example, specifying a service level or the geographical ' 'location of a backend, then creating a volume type to ' 'allow the user to select by these different ' 'properties.'), cfg.BoolOpt('suppress_requests_ssl_warnings', default=False, help='Suppress requests library SSL certificate warnings.'), ] CONF = cfg.CONF CONF.register_opts(volume_manager_opts) MAPPING = { 'cinder.volume.drivers.huawei.huawei_18000.Huawei18000ISCSIDriver': 'cinder.volume.drivers.huawei.huawei_driver.HuaweiISCSIDriver', 'cinder.volume.drivers.huawei.huawei_driver.Huawei18000ISCSIDriver': 'cinder.volume.drivers.huawei.huawei_driver.HuaweiISCSIDriver', 'cinder.volume.drivers.huawei.huawei_18000.Huawei18000FCDriver': 'cinder.volume.drivers.huawei.huawei_driver.HuaweiFCDriver', 'cinder.volume.drivers.huawei.huawei_driver.Huawei18000FCDriver': 'cinder.volume.drivers.huawei.huawei_driver.HuaweiFCDriver', 'cinder.volume.drivers.fujitsu_eternus_dx_fc.FJDXFCDriver': 'cinder.volume.drivers.fujitsu.eternus_dx_fc.FJDXFCDriver', 'cinder.volume.drivers.fujitsu_eternus_dx_iscsi.FJDXISCSIDriver': 'cinder.volume.drivers.fujitsu.eternus_dx_iscsi.FJDXISCSIDriver', 'cinder.volume.drivers.hds.nfs.HDSNFSDriver': 'cinder.volume.drivers.hitachi.hnas_nfs.HDSNFSDriver', 'cinder.volume.drivers.hds.iscsi.HDSISCSIDriver': 'cinder.volume.drivers.hitachi.hnas_iscsi.HDSISCSIDriver', 'cinder.volume.drivers.san.hp.hp_3par_fc.HP3PARFCDriver': 'cinder.volume.drivers.hpe.hpe_3par_fc.HPE3PARFCDriver', 'cinder.volume.drivers.san.hp.hp_3par_iscsi.HP3PARISCSIDriver': 'cinder.volume.drivers.hpe.hpe_3par_iscsi.HPE3PARISCSIDriver', 'cinder.volume.drivers.san.hp.hp_lefthand_iscsi.HPLeftHandISCSIDriver': 'cinder.volume.drivers.hpe.hpe_lefthand_iscsi.HPELeftHandISCSIDriver', 'cinder.volume.drivers.san.hp.hp_xp_fc.HPXPFCDriver': 'cinder.volume.drivers.hpe.hpe_xp_fc.HPEXPFCDriver', } def locked_volume_operation(f): """Lock decorator for volume operations. Takes a named lock prior to executing the operation. The lock is named with the operation executed and the id of the volume. This lock can then be used by other operations to avoid operation conflicts on shared volumes. Example use: If a volume operation uses this decorator, it will block until the named lock is free. This is used to protect concurrent operations on the same volume e.g. delete VolA while create volume VolB from VolA is in progress. """ def lvo_inner1(inst, context, volume_id, **kwargs): @utils.synchronized("%s-%s" % (volume_id, f.__name__), external=True) def lvo_inner2(*_args, **_kwargs): return f(*_args, **_kwargs) return lvo_inner2(inst, context, volume_id, **kwargs) return lvo_inner1 def locked_detach_operation(f): """Lock decorator for volume detach operations. Takes a named lock prior to executing the detach call. The lock is named with the operation executed and the id of the volume. This lock can then be used by other operations to avoid operation conflicts on shared volumes. This locking mechanism is only for detach calls. We can't use the locked_volume_operation, because detach requires an additional attachment_id in the parameter list. """ def ldo_inner1(inst, context, volume_id, attachment_id=None, **kwargs): @utils.synchronized("%s-%s" % (volume_id, f.__name__), external=True) def ldo_inner2(*_args, **_kwargs): return f(*_args, **_kwargs) return ldo_inner2(inst, context, volume_id, attachment_id, **kwargs) return ldo_inner1 def locked_snapshot_operation(f): """Lock decorator for snapshot operations. Takes a named lock prior to executing the operation. The lock is named with the operation executed and the id of the snapshot. This lock can then be used by other operations to avoid operation conflicts on shared snapshots. Example use: If a snapshot operation uses this decorator, it will block until the named lock is free. This is used to protect concurrent operations on the same snapshot e.g. delete SnapA while create volume VolA from SnapA is in progress. """ def lso_inner1(inst, context, snapshot, **kwargs): @utils.synchronized("%s-%s" % (snapshot.id, f.__name__), external=True) def lso_inner2(*_args, **_kwargs): return f(*_args, **_kwargs) return lso_inner2(inst, context, snapshot, **kwargs) return lso_inner1 class VolumeManager(manager.SchedulerDependentManager): """Manages attachable block storage devices.""" RPC_API_VERSION = '2.0' target = messaging.Target(version=RPC_API_VERSION) # On cloning a volume, we shouldn't copy volume_type, consistencygroup # and volume_attachment, because the db sets that according to [field]_id, # which we do copy. We also skip some other values that are set during # creation of Volume object. _VOLUME_CLONE_SKIP_PROPERTIES = { 'id', '_name_id', 'name_id', 'name', 'status', 'attach_status', 'migration_status', 'volume_type', 'consistencygroup', 'volume_attachment'} def __init__(self, volume_driver=None, service_name=None, *args, **kwargs): """Load the driver from the one specified in args, or from flags.""" # update_service_capabilities needs service_name to be volume super(VolumeManager, self).__init__(service_name='volume', *args, **kwargs) self.configuration = config.Configuration(volume_manager_opts, config_group=service_name) self.stats = {} if not volume_driver: # Get from configuration, which will get the default # if its not using the multi backend volume_driver = self.configuration.volume_driver if volume_driver in MAPPING: LOG.warning(_LW("Driver path %s is deprecated, update your " "configuration to the new path."), volume_driver) volume_driver = MAPPING[volume_driver] vol_db_empty = self._set_voldb_empty_at_startup_indicator( context.get_admin_context()) LOG.debug("Cinder Volume DB check: vol_db_empty=%s", vol_db_empty) # We pass the current setting for service.active_backend_id to # the driver on init, incase there was a restart or something curr_active_backend_id = None svc_host = vol_utils.extract_host(self.host, 'backend') try: service = objects.Service.get_by_args( context.get_admin_context(), svc_host, 'cinder-volume') except exception.ServiceNotFound: # NOTE(jdg): This is to solve problems with unit tests LOG.info(_LI("Service not found for updating " "active_backend_id, assuming default " "for driver init.")) else: curr_active_backend_id = service.active_backend_id if self.configuration.suppress_requests_ssl_warnings: LOG.warning(_LW("Suppressing requests library SSL Warnings")) requests.packages.urllib3.disable_warnings( requests.packages.urllib3.exceptions.InsecureRequestWarning) requests.packages.urllib3.disable_warnings( requests.packages.urllib3.exceptions.InsecurePlatformWarning) self.driver = importutils.import_object( volume_driver, configuration=self.configuration, db=self.db, host=self.host, is_vol_db_empty=vol_db_empty, active_backend_id=curr_active_backend_id) self.message_api = message_api.API() if CONF.profiler.enabled and profiler is not None: self.driver = profiler.trace_cls("driver")(self.driver) try: self.extra_capabilities = jsonutils.loads( self.driver.configuration.extra_capabilities) except AttributeError: self.extra_capabilities = {} except Exception: with excutils.save_and_reraise_exception(): LOG.error(_LE("Invalid JSON: %s"), self.driver.configuration.extra_capabilities) if self.driver.configuration.safe_get( 'image_volume_cache_enabled'): max_cache_size = self.driver.configuration.safe_get( 'image_volume_cache_max_size_gb') max_cache_entries = self.driver.configuration.safe_get( 'image_volume_cache_max_count') self.image_volume_cache = image_cache.ImageVolumeCache( self.db, cinder_volume.API(), max_cache_size, max_cache_entries ) LOG.info(_LI('Image-volume cache enabled for host %(host)s.'), {'host': self.host}) else: LOG.info(_LI('Image-volume cache disabled for host %(host)s.'), {'host': self.host}) self.image_volume_cache = None def _count_allocated_capacity(self, ctxt, volume): pool = vol_utils.extract_host(volume['host'], 'pool') if pool is None: # No pool name encoded in host, so this is a legacy # volume created before pool is introduced, ask # driver to provide pool info if it has such # knowledge and update the DB. try: pool = self.driver.get_pool(volume) except Exception: LOG.exception(_LE('Fetch volume pool name failed.'), resource=volume) return if pool: new_host = vol_utils.append_host(volume['host'], pool) self.db.volume_update(ctxt, volume['id'], {'host': new_host}) else: # Otherwise, put them into a special fixed pool with # volume_backend_name being the pool name, if # volume_backend_name is None, use default pool name. # This is only for counting purpose, doesn't update DB. pool = (self.driver.configuration.safe_get( 'volume_backend_name') or vol_utils.extract_host( volume['host'], 'pool', True)) try: pool_stat = self.stats['pools'][pool] except KeyError: # First volume in the pool self.stats['pools'][pool] = dict( allocated_capacity_gb=0) pool_stat = self.stats['pools'][pool] pool_sum = pool_stat['allocated_capacity_gb'] pool_sum += volume['size'] self.stats['pools'][pool]['allocated_capacity_gb'] = pool_sum self.stats['allocated_capacity_gb'] += volume['size'] def _set_voldb_empty_at_startup_indicator(self, ctxt): """Determine if the Cinder volume DB is empty. A check of the volume DB is done to determine whether it is empty or not at this point. :param ctxt: our working context """ vol_entries = self.db.volume_get_all(ctxt, None, 1, filters=None) if len(vol_entries) == 0: LOG.info(_LI("Determined volume DB was empty at startup.")) return True else: LOG.info(_LI("Determined volume DB was not empty at startup.")) return False def _sync_provider_info(self, ctxt, volumes, snapshots): # NOTE(jdg): For now this just updates provider_id, we can add more # add more items to the update if theyr'e releveant but we need # to be safe in what we allow and add a list of allowed keys # things that make sense are provider_*, replication_status etc updates, snapshot_updates = self.driver.update_provider_info( volumes, snapshots) if updates: for volume in volumes: # NOTE(JDG): Make sure returned item is in this hosts volumes update = ( [updt for updt in updates if updt['id'] == volume['id']][0]) if update: self.db.volume_update( ctxt, update['id'], {'provider_id': update['provider_id']}) # NOTE(jdg): snapshots are slighty harder, because # we do not have a host column and of course no get # all by host, so we use a get_all and bounce our # response off of it if snapshot_updates: cinder_snaps = self.db.snapshot_get_all(ctxt) for snap in cinder_snaps: # NOTE(jdg): For now we only update those that have no entry if not snap.get('provider_id', None): update = ( [updt for updt in snapshot_updates if updt['id'] == snap['id']][0]) if update: self.db.snapshot_update( ctxt, update['id'], {'provider_id': update['provider_id']}) def init_host(self): """Perform any required initialization.""" ctxt = context.get_admin_context() LOG.info(_LI("Starting volume driver %(driver_name)s (%(version)s)"), {'driver_name': self.driver.__class__.__name__, 'version': self.driver.get_version()}) try: self.driver.do_setup(ctxt) self.driver.check_for_setup_error() except Exception: LOG.exception(_LE("Failed to initialize driver."), resource={'type': 'driver', 'id': self.__class__.__name__}) # we don't want to continue since we failed # to initialize the driver correctly. return # Initialize backend capabilities list self.driver.init_capabilities() volumes = objects.VolumeList.get_all_by_host(ctxt, self.host) snapshots = self.db.snapshot_get_by_host(ctxt, self.host) self._sync_provider_info(ctxt, volumes, snapshots) # FIXME volume count for exporting is wrong try: self.stats['pools'] = {} self.stats.update({'allocated_capacity_gb': 0}) for volume in volumes: # available volume should also be counted into allocated if volume['status'] in ['in-use', 'available']: # calculate allocated capacity for driver self._count_allocated_capacity(ctxt, volume) try: if volume['status'] in ['in-use']: self.driver.ensure_export(ctxt, volume) except Exception: LOG.exception(_LE("Failed to re-export volume, " "setting to ERROR."), resource=volume) volume.status = 'error' volume.save() elif volume['status'] in ('downloading', 'creating'): LOG.warning(_LW("Detected volume stuck " "in %(curr_status)s " "status, setting to ERROR."), {'curr_status': volume['status']}, resource=volume) if volume['status'] == 'downloading': self.driver.clear_download(ctxt, volume) volume.status = 'error' volume.save() elif volume.status == 'uploading': # Set volume status to available or in-use. self.db.volume_update_status_based_on_attachment( ctxt, volume.id) else: pass snapshots = objects.SnapshotList.get_by_host( ctxt, self.host, {'status': fields.SnapshotStatus.CREATING}) for snapshot in snapshots: LOG.warning(_LW("Detected snapshot stuck in creating " "status, setting to ERROR."), resource=snapshot) snapshot.status = fields.SnapshotStatus.ERROR snapshot.save() except Exception: LOG.exception(_LE("Error during re-export on driver init."), resource=volume) return self.driver.set_throttle() # at this point the driver is considered initialized. # NOTE(jdg): Careful though because that doesn't mean # that an entry exists in the service table self.driver.set_initialized() for volume in volumes: if volume['status'] == 'deleting': if CONF.volume_service_inithost_offload: # Offload all the pending volume delete operations to the # threadpool to prevent the main volume service thread # from being blocked. self._add_to_threadpool(self.delete_volume, ctxt, volume['id'], volume=volume) else: # By default, delete volumes sequentially self.delete_volume(ctxt, volume['id'], volume=volume) LOG.info(_LI("Resume volume delete completed successfully."), resource=volume) # collect and publish service capabilities self.publish_service_capabilities(ctxt) LOG.info(_LI("Driver initialization completed successfully."), resource={'type': 'driver', 'id': self.driver.__class__.__name__}) def init_host_with_rpc(self): LOG.info(_LI("Initializing RPC dependent components of volume " "driver %(driver_name)s (%(version)s)"), {'driver_name': self.driver.__class__.__name__, 'version': self.driver.get_version()}) stats = self.driver.get_volume_stats(refresh=True) svc_host = vol_utils.extract_host(self.host, 'backend') try: service = objects.Service.get_by_args( context.get_admin_context(), svc_host, 'cinder-volume') except exception.ServiceNotFound: with excutils.save_and_reraise_exception(): LOG.error(_LE("Service not found for updating " "replication_status.")) if service.replication_status != ( fields.ReplicationStatus.FAILED_OVER): if stats and stats.get('replication_enabled', False): service.replication_status = fields.ReplicationStatus.ENABLED else: service.replication_status = fields.ReplicationStatus.DISABLED service.save() LOG.info(_LI("Driver post RPC initialization completed successfully."), resource={'type': 'driver', 'id': self.driver.__class__.__name__}) def is_working(self): """Return if Manager is ready to accept requests. This is to inform Service class that in case of volume driver initialization failure the manager is actually down and not ready to accept any requests. """ return self.driver.initialized def create_volume(self, context, volume_id, request_spec=None, filter_properties=None, allow_reschedule=True, volume=None): """Creates the volume.""" # FIXME(dulek): Remove this in v3.0 of RPC API. if volume is None: # For older clients, mimic the old behavior and look up the volume # by its volume_id. volume = objects.Volume.get_by_id(context, volume_id) context_elevated = context.elevated() if filter_properties is None: filter_properties = {} if request_spec is None: request_spec = {} try: # NOTE(flaper87): Driver initialization is # verified by the task itself. flow_engine = create_volume.get_flow( context_elevated, self, self.db, self.driver, self.scheduler_rpcapi, self.host, volume.id, allow_reschedule, context, request_spec, filter_properties, image_volume_cache=self.image_volume_cache, ) except Exception: msg = _("Create manager volume flow failed.") LOG.exception(msg, resource={'type': 'volume', 'id': volume.id}) raise exception.CinderException(msg) snapshot_id = request_spec.get('snapshot_id') source_volid = request_spec.get('source_volid') source_replicaid = request_spec.get('source_replicaid') if snapshot_id is not None: # Make sure the snapshot is not deleted until we are done with it. locked_action = "%s-%s" % (snapshot_id, 'delete_snapshot') elif source_volid is not None: # Make sure the volume is not deleted until we are done with it. locked_action = "%s-%s" % (source_volid, 'delete_volume') elif source_replicaid is not None: # Make sure the volume is not deleted until we are done with it. locked_action = "%s-%s" % (source_replicaid, 'delete_volume') else: locked_action = None def _run_flow(): # This code executes create volume flow. If something goes wrong, # flow reverts all job that was done and reraises an exception. # Otherwise, all data that was generated by flow becomes available # in flow engine's storage. with flow_utils.DynamicLogListener(flow_engine, logger=LOG): flow_engine.run() @utils.synchronized(locked_action, external=True) def _run_flow_locked(): _run_flow() # NOTE(dulek): Flag to indicate if volume was rescheduled. Used to # decide if allocated_capacity should be incremented. rescheduled = False vol_ref = None try: if locked_action is None: _run_flow() else: _run_flow_locked() finally: try: vol_ref = flow_engine.storage.fetch('volume_ref') except tfe.NotFound: # If there's no vol_ref, then flow is reverted. Lets check out # if rescheduling occurred. try: rescheduled = flow_engine.storage.get_revert_result( create_volume.OnFailureRescheduleTask.make_name( [create_volume.ACTION])) except tfe.NotFound: pass if not rescheduled: if not vol_ref: # Flow was reverted and not rescheduled, fetching # volume_ref from the DB, because it will be needed. vol_ref = objects.Volume.get_by_id(context, volume.id) # NOTE(dulek): Volume wasn't rescheduled so we need to update # volume stats as these are decremented on delete. self._update_allocated_capacity(vol_ref) LOG.info(_LI("Created volume successfully."), resource=vol_ref) return vol_ref.id @locked_volume_operation def delete_volume(self, context, volume_id, unmanage_only=False, volume=None, cascade=False): """Deletes and unexports volume. 1. Delete a volume(normal case) Delete a volume and update quotas. 2. Delete a migration volume If deleting the volume in a migration, we want to skip quotas but we need database updates for the volume. """ context = context.elevated() try: # FIXME(dulek): Remove this in v3.0 of RPC API. if volume is None: volume = objects.Volume.get_by_id(context, volume_id) else: volume.refresh() except exception.VolumeNotFound: # NOTE(thingee): It could be possible for a volume to # be deleted when resuming deletes from init_host(). LOG.debug("Attempted delete of non-existent volume: %s", volume_id) return if context.project_id != volume.project_id: project_id = volume.project_id else: project_id = context.project_id if volume['attach_status'] == "attached": # Volume is still attached, need to detach first raise exception.VolumeAttached(volume_id=volume_id) if vol_utils.extract_host(volume.host) != self.host: raise exception.InvalidVolume( reason=_("volume is not local to this node")) if unmanage_only and cascade: # This could be done, but is ruled out for now just # for simplicity. raise exception.Invalid( reason=_("Unmanage and cascade delete options " "are mutually exclusive.")) # The status 'deleting' is not included, because it only applies to # the source volume to be deleted after a migration. No quota # needs to be handled for it. is_migrating = volume.migration_status not in (None, 'error', 'success') is_migrating_dest = (is_migrating and volume.migration_status.startswith( 'target:')) self._notify_about_volume_usage(context, volume, "delete.start") try: # NOTE(flaper87): Verify the driver is enabled # before going forward. The exception will be caught # and the volume status updated. utils.require_driver_initialized(self.driver) self.driver.remove_export(context, volume) if unmanage_only: self.driver.unmanage(volume) elif cascade: LOG.debug('Performing cascade delete.') snapshots = objects.SnapshotList.get_all_for_volume(context, volume.id) for s in snapshots: if s.status != 'deleting': self._clear_db(context, is_migrating_dest, volume, 'error_deleting') msg = (_("Snapshot %(id)s was found in state " "%(state)s rather than 'deleting' during " "cascade delete.") % {'id': s.id, 'state': s.status}) raise exception.InvalidSnapshot(reason=msg) self.delete_snapshot(context, s) LOG.debug('Snapshots deleted, issuing volume delete') self.driver.delete_volume(volume) else: self.driver.delete_volume(volume) except exception.VolumeIsBusy: LOG.error(_LE("Unable to delete busy volume."), resource=volume) # If this is a destination volume, we have to clear the database # record to avoid user confusion. self._clear_db(context, is_migrating_dest, volume, 'available') return except Exception: with excutils.save_and_reraise_exception(): # If this is a destination volume, we have to clear the # database record to avoid user confusion. self._clear_db(context, is_migrating_dest, volume, 'error_deleting') # If deleting source/destination volume in a migration, we should # skip quotas. if not is_migrating: # Get reservations try: reserve_opts = {'volumes': -1, 'gigabytes': -volume.size} QUOTAS.add_volume_type_opts(context, reserve_opts, volume.volume_type_id) reservations = QUOTAS.reserve(context, project_id=project_id, **reserve_opts) except Exception: reservations = None LOG.exception(_LE("Failed to update usages deleting volume."), resource=volume) # Delete glance metadata if it exists self.db.volume_glance_metadata_delete_by_volume(context, volume_id) volume.destroy() # If deleting source/destination volume in a migration, we should # skip quotas. if not is_migrating: self._notify_about_volume_usage(context, volume, "delete.end") # Commit the reservations if reservations: QUOTAS.commit(context, reservations, project_id=project_id) pool = vol_utils.extract_host(volume.host, 'pool') if pool is None: # Legacy volume, put them into default pool pool = self.driver.configuration.safe_get( 'volume_backend_name') or vol_utils.extract_host( volume.host, 'pool', True) size = volume.size try: self.stats['pools'][pool]['allocated_capacity_gb'] -= size except KeyError: self.stats['pools'][pool] = dict( allocated_capacity_gb=-size) self.publish_service_capabilities(context) LOG.info(_LI("Deleted volume successfully."), resource=volume) def _clear_db(self, context, is_migrating_dest, volume_ref, status): # This method is called when driver.unmanage() or # driver.delete_volume() fails in delete_volume(), so it is already # in the exception handling part. if is_migrating_dest: volume_ref.destroy() LOG.error(_LE("Unable to delete the destination volume " "during volume migration, (NOTE: database " "record needs to be deleted)."), resource=volume_ref) else: volume_ref.status = status volume_ref.save() def create_snapshot(self, context, volume_id, snapshot): """Creates and exports the snapshot.""" context = context.elevated() self._notify_about_snapshot_usage( context, snapshot, "create.start") try: # NOTE(flaper87): Verify the driver is enabled # before going forward. The exception will be caught # and the snapshot status updated. utils.require_driver_initialized(self.driver) # Pass context so that drivers that want to use it, can, # but it is not a requirement for all drivers. snapshot.context = context model_update = self.driver.create_snapshot(snapshot) if model_update: snapshot.update(model_update) snapshot.save() except Exception: with excutils.save_and_reraise_exception(): snapshot.status = fields.SnapshotStatus.ERROR snapshot.save() vol_ref = self.db.volume_get(context, volume_id) if vol_ref.bootable: try: self.db.volume_glance_metadata_copy_to_snapshot( context, snapshot.id, volume_id) except exception.GlanceMetadataNotFound: # If volume is not created from image, No glance metadata # would be available for that volume in # volume glance metadata table pass except exception.CinderException as ex: LOG.exception(_LE("Failed updating snapshot" " metadata using the provided volumes" " %(volume_id)s metadata"), {'volume_id': volume_id}, resource=snapshot) snapshot.status = fields.SnapshotStatus.ERROR snapshot.save() raise exception.MetadataCopyFailure(reason=six.text_type(ex)) snapshot.status = fields.SnapshotStatus.AVAILABLE snapshot.progress = '100%' snapshot.save() self._notify_about_snapshot_usage(context, snapshot, "create.end") LOG.info(_LI("Create snapshot completed successfully"), resource=snapshot) return snapshot.id @locked_snapshot_operation def delete_snapshot(self, context, snapshot, unmanage_only=False): """Deletes and unexports snapshot.""" context = context.elevated() snapshot._context = context project_id = snapshot.project_id self._notify_about_snapshot_usage( context, snapshot, "delete.start") try: # NOTE(flaper87): Verify the driver is enabled # before going forward. The exception will be caught # and the snapshot status updated. utils.require_driver_initialized(self.driver) # Pass context so that drivers that want to use it, can, # but it is not a requirement for all drivers. snapshot.context = context snapshot.save() if unmanage_only: self.driver.unmanage_snapshot(snapshot) else: self.driver.delete_snapshot(snapshot) except exception.SnapshotIsBusy: LOG.error(_LE("Delete snapshot failed, due to snapshot busy."), resource=snapshot) snapshot.status = fields.SnapshotStatus.AVAILABLE snapshot.save() return except Exception: with excutils.save_and_reraise_exception(): snapshot.status = fields.SnapshotStatus.ERROR_DELETING snapshot.save() # Get reservations try: if CONF.no_snapshot_gb_quota: reserve_opts = {'snapshots': -1} else: reserve_opts = { 'snapshots': -1, 'gigabytes': -snapshot.volume_size, } volume_ref = self.db.volume_get(context, snapshot.volume_id) QUOTAS.add_volume_type_opts(context, reserve_opts, volume_ref.get('volume_type_id')) reservations = QUOTAS.reserve(context, project_id=project_id, **reserve_opts) except Exception: reservations = None LOG.exception(_LE("Update snapshot usages failed."), resource=snapshot) self.db.volume_glance_metadata_delete_by_snapshot(context, snapshot.id) snapshot.destroy() self._notify_about_snapshot_usage(context, snapshot, "delete.end") # Commit the reservations if reservations: QUOTAS.commit(context, reservations, project_id=project_id) LOG.info(_LI("Delete snapshot completed successfully"), resource=snapshot) def attach_volume(self, context, volume_id, instance_uuid, host_name, mountpoint, mode): """Updates db to show volume is attached.""" @utils.synchronized(volume_id, external=True) def do_attach(): # check the volume status before attaching volume = self.db.volume_get(context, volume_id) volume_metadata = self.db.volume_admin_metadata_get( context.elevated(), volume_id) if volume['status'] == 'attaching': if (volume_metadata.get('attached_mode') and volume_metadata.get('attached_mode') != mode): raise exception.InvalidVolume( reason=_("being attached by different mode")) if (volume['status'] == 'in-use' and not volume['multiattach'] and not volume['migration_status']): raise exception.InvalidVolume( reason=_("volume is already attached")) host_name_sanitized = utils.sanitize_hostname( host_name) if host_name else None if instance_uuid: attachments = \ self.db.volume_attachment_get_all_by_instance_uuid( context, volume_id, instance_uuid) else: attachments = ( self.db.volume_attachment_get_all_by_host( context, volume_id, host_name_sanitized)) if attachments: self.db.volume_update(context, volume_id, {'status': 'in-use'}) return self._notify_about_volume_usage(context, volume, "attach.start") values = {'volume_id': volume_id, 'attach_status': 'attaching', } attachment = self.db.volume_attach(context.elevated(), values) volume_metadata = self.db.volume_admin_metadata_update( context.elevated(), volume_id, {"attached_mode": mode}, False) attachment_id = attachment['id'] if instance_uuid and not uuidutils.is_uuid_like(instance_uuid): self.db.volume_attachment_update(context, attachment_id, {'attach_status': 'error_attaching'}) raise exception.InvalidUUID(uuid=instance_uuid) volume = self.db.volume_get(context, volume_id) if volume_metadata.get('readonly') == 'True' and mode != 'ro': self.db.volume_update(context, volume_id, {'status': 'error_attaching'}) self.message_api.create( context, defined_messages.ATTACH_READONLY_VOLUME, context.project_id, resource_type=resource_types.VOLUME, resource_uuid=volume_id) raise exception.InvalidVolumeAttachMode(mode=mode, volume_id=volume_id) try: # NOTE(flaper87): Verify the driver is enabled # before going forward. The exception will be caught # and the volume status updated. utils.require_driver_initialized(self.driver) LOG.debug('Attaching volume %(volume_id)s to instance ' '%(instance)s at mountpoint %(mount)s on host ' '%(host)s.', {'volume_id': volume_id, 'instance': instance_uuid, 'mount': mountpoint, 'host': host_name_sanitized}, resource=volume) self.driver.attach_volume(context, volume, instance_uuid, host_name_sanitized, mountpoint) except Exception: with excutils.save_and_reraise_exception(): self.db.volume_attachment_update( context, attachment_id, {'attach_status': 'error_attaching'}) volume = self.db.volume_attached(context.elevated(), attachment_id, instance_uuid, host_name_sanitized, mountpoint, mode) self._notify_about_volume_usage(context, volume, "attach.end") LOG.info(_LI("Attach volume completed successfully."), resource=volume) return self.db.volume_attachment_get(context, attachment_id) return do_attach() @locked_detach_operation def detach_volume(self, context, volume_id, attachment_id=None): """Updates db to show volume is detached.""" # TODO(vish): refactor this into a more general "unreserve" volume = self.db.volume_get(context, volume_id) attachment = None if attachment_id: try: attachment = self.db.volume_attachment_get(context, attachment_id) except exception.VolumeAttachmentNotFound: LOG.info(_LI("Volume detach called, but volume not attached."), resource=volume) # We need to make sure the volume status is set to the correct # status. It could be in detaching status now, and we don't # want to leave it there. self.db.volume_detached(context, volume_id, attachment_id) return else: # We can try and degrade gracefully here by trying to detach # a volume without the attachment_id here if the volume only has # one attachment. This is for backwards compatibility. attachments = self.db.volume_attachment_get_all_by_volume_id( context, volume_id) if len(attachments) > 1: # There are more than 1 attachments for this volume # we have to have an attachment id. msg = _("Detach volume failed: More than one attachment, " "but no attachment_id provided.") LOG.error(msg, resource=volume) raise exception.InvalidVolume(reason=msg) elif len(attachments) == 1: attachment = attachments[0] else: # there aren't any attachments for this volume. # so set the status to available and move on. LOG.info(_LI("Volume detach called, but volume not attached."), resource=volume) self.db.volume_update(context, volume_id, {'status': 'available', 'attach_status': 'detached'}) return self._notify_about_volume_usage(context, volume, "detach.start") try: # NOTE(flaper87): Verify the driver is enabled # before going forward. The exception will be caught # and the volume status updated. utils.require_driver_initialized(self.driver) LOG.debug('Detaching volume %(volume_id)s from instance ' '%(instance)s.', {'volume_id': volume_id, 'instance': attachment.get('instance_uuid')}, resource=volume) self.driver.detach_volume(context, volume, attachment) except Exception: with excutils.save_and_reraise_exception(): self.db.volume_attachment_update( context, attachment.get('id'), {'attach_status': 'error_detaching'}) self.db.volume_detached(context.elevated(), volume_id, attachment.get('id')) self.db.volume_admin_metadata_delete(context.elevated(), volume_id, 'attached_mode') # NOTE(jdg): We used to do an ensure export here to # catch upgrades while volumes were attached (E->F) # this was necessary to convert in-use volumes from # int ID's to UUID's. Don't need this any longer # We're going to remove the export here # (delete the iscsi target) volume = self.db.volume_get(context, volume_id) try: utils.require_driver_initialized(self.driver) self.driver.remove_export(context.elevated(), volume) except exception.DriverNotInitialized: with excutils.save_and_reraise_exception(): LOG.exception(_LE("Detach volume failed, due to " "uninitialized driver."), resource=volume) except Exception as ex: LOG.exception(_LE("Detach volume failed, due to " "remove-export failure."), resource=volume) raise exception.RemoveExportException(volume=volume_id, reason=six.text_type(ex)) self._notify_about_volume_usage(context, volume, "detach.end") LOG.info(_LI("Detach volume completed successfully."), resource=volume) def _create_image_cache_volume_entry(self, ctx, volume_ref, image_id, image_meta): """Create a new image-volume and cache entry for it. This assumes that the image has already been downloaded and stored in the volume described by the volume_ref. """ image_volume = None try: if not self.image_volume_cache.ensure_space( ctx, volume_ref['size'], volume_ref['host']): LOG.warning(_LW('Unable to ensure space for image-volume in' ' cache. Will skip creating entry for image' ' %(image)s on host %(host)s.'), {'image': image_id, 'host': volume_ref['host']}) return image_volume = self._clone_image_volume(ctx, volume_ref, image_meta) if not image_volume: LOG.warning(_LW('Unable to clone image_volume for image ' '%(image_id)s will not create cache entry.'), {'image_id': image_id}) return self.image_volume_cache.create_cache_entry( ctx, image_volume, image_id, image_meta ) except exception.CinderException as e: LOG.warning(_LW('Failed to create new image-volume cache entry.' ' Error: %(exception)s'), {'exception': e}) if image_volume: self.delete_volume(ctx, image_volume.id) def _clone_image_volume(self, ctx, volume, image_meta): volume_type_id = volume.get('volume_type_id') reserve_opts = {'volumes': 1, 'gigabytes': volume.size} QUOTAS.add_volume_type_opts(ctx, reserve_opts, volume_type_id) reservations = QUOTAS.reserve(ctx, **reserve_opts) try: new_vol_values = {k: volume[k] for k in set(volume.keys()) - self._VOLUME_CLONE_SKIP_PROPERTIES} new_vol_values['volume_type_id'] = volume_type_id new_vol_values['attach_status'] = 'detached' new_vol_values['status'] = 'creating' new_vol_values['project_id'] = ctx.project_id new_vol_values['display_name'] = 'image-%s' % image_meta['id'] new_vol_values['source_volid'] = volume.id LOG.debug('Creating image volume entry: %s.', new_vol_values) image_volume = objects.Volume(context=ctx, **new_vol_values) image_volume.create() except Exception as ex: LOG.exception(_LE('Create clone_image_volume: %(volume_id)s' 'for image %(image_id)s, ' 'failed (Exception: %(except)s)'), {'volume_id': volume.id, 'image_id': image_meta['id'], 'except': ex}) QUOTAS.rollback(ctx, reservations) return QUOTAS.commit(ctx, reservations, project_id=new_vol_values['project_id']) try: self.create_volume(ctx, image_volume.id, allow_reschedule=False, volume=image_volume) image_volume = self.db.volume_get(ctx, image_volume.id) if image_volume.status != 'available': raise exception.InvalidVolume(_('Volume is not available.')) self.db.volume_admin_metadata_update(ctx.elevated(), image_volume.id, {'readonly': 'True'}, False) return image_volume except exception.CinderException: LOG.exception(_LE('Failed to clone volume %(volume_id)s for ' 'image %(image_id)s.'), {'volume_id': volume.id, 'image_id': image_meta['id']}) try: self.delete_volume(ctx, image_volume.id) except exception.CinderException: LOG.exception(_LE('Could not delete the image volume %(id)s.'), {'id': volume.id}) return def _clone_image_volume_and_add_location(self, ctx, volume, image_service, image_meta): """Create a cloned volume and register its location to the image.""" if (image_meta['disk_format'] != 'raw' or image_meta['container_format'] != 'bare'): return False image_volume_context = ctx if self.driver.configuration.image_upload_use_internal_tenant: internal_ctx = context.get_internal_tenant_context() if internal_ctx: image_volume_context = internal_ctx image_volume = self._clone_image_volume(image_volume_context, volume, image_meta) if not image_volume: return False uri = 'cinder://%s' % image_volume.id image_registered = None try: image_registered = image_service.add_location( ctx, image_meta['id'], uri, {}) except (exception.NotAuthorized, exception.Invalid, exception.NotFound): LOG.exception(_LE('Failed to register image volume location ' '%(uri)s.'), {'uri': uri}) if not image_registered: LOG.warning(_LW('Registration of image volume URI %(uri)s ' 'to image %(image_id)s failed.'), {'uri': uri, 'image_id': image_meta['id']}) try: self.delete_volume(image_volume_context, image_volume) except exception.CinderException: LOG.exception(_LE('Could not delete failed image volume ' '%(id)s.'), {'id': image_volume.id}) return False image_volume_meta = {'glance_image_id': image_meta['id'], 'image_owner': ctx.project_id} self.db.volume_metadata_update(image_volume_context, image_volume.id, image_volume_meta, False) return True def copy_volume_to_image(self, context, volume_id, image_meta): """Uploads the specified volume to Glance. image_meta is a dictionary containing the following keys: 'id', 'container_format', 'disk_format' """ payload = {'volume_id': volume_id, 'image_id': image_meta['id']} image_service = None try: volume = self.db.volume_get(context, volume_id) # NOTE(flaper87): Verify the driver is enabled # before going forward. The exception will be caught # and the volume status updated. utils.require_driver_initialized(self.driver) image_service, image_id = \ glance.get_remote_image_service(context, image_meta['id']) if (self.driver.configuration.image_upload_use_cinder_backend and self._clone_image_volume_and_add_location( context, volume, image_service, image_meta)): LOG.debug("Registered image volume location to glance " "image-id: %(image_id)s.", {'image_id': image_meta['id']}, resource=volume) else: self.driver.copy_volume_to_image(context, volume, image_service, image_meta) LOG.debug("Uploaded volume to glance image-id: %(image_id)s.", {'image_id': image_meta['id']}, resource=volume) except Exception as error: LOG.error(_LE("Upload volume to image encountered an error " "(image-id: %(image_id)s)."), {'image_id': image_meta['id']}, resource=volume) if image_service is not None: # Deletes the image if it is in queued or saving state self._delete_image(context, image_meta['id'], image_service) with excutils.save_and_reraise_exception(): payload['message'] = six.text_type(error) if isinstance(error, exception.ImageLimitExceeded): self.message_api.create( context, defined_messages.IMAGE_FROM_VOLUME_OVER_QUOTA, context.project_id, resource_type=resource_types.VOLUME, resource_uuid=volume_id) finally: self.db.volume_update_status_based_on_attachment(context, volume_id) LOG.info(_LI("Copy volume to image completed successfully."), resource=volume) def _delete_image(self, context, image_id, image_service): """Deletes an image stuck in queued or saving state.""" try: image_meta = image_service.show(context, image_id) image_status = image_meta.get('status') if image_status == 'queued' or image_status == 'saving': LOG.warning(_LW("Deleting image in unexpected status: " "%(image_status)s."), {'image_status': image_status}, resource={'type': 'image', 'id': image_id}) image_service.delete(context, image_id) except Exception: LOG.warning(_LW("Image delete encountered an error."), exc_info=True, resource={'type': 'image', 'id': image_id}) def initialize_connection(self, context, volume_id, connector): """Prepare volume for connection from host represented by connector. This method calls the driver initialize_connection and returns it to the caller. The connector parameter is a dictionary with information about the host that will connect to the volume in the following format:: { 'ip': ip, 'initiator': initiator, } ip: the ip address of the connecting machine initiator: the iscsi initiator name of the connecting machine. This can be None if the connecting machine does not support iscsi connections. driver is responsible for doing any necessary security setup and returning a connection_info dictionary in the following format:: { 'driver_volume_type': driver_volume_type, 'data': data, } driver_volume_type: a string to identify the type of volume. This can be used by the calling code to determine the strategy for connecting to the volume. This could be 'iscsi', 'rbd', 'sheepdog', etc. data: this is the data that the calling code will use to connect to the volume. Keep in mind that this will be serialized to json in various places, so it should not contain any non-json data types. """ # NOTE(flaper87): Verify the driver is enabled # before going forward. The exception will be caught # and the volume status updated. utils.require_driver_initialized(self.driver) volume = self.db.volume_get(context, volume_id) model_update = None try: self.driver.validate_connector(connector) except exception.InvalidConnectorException as err: raise exception.InvalidInput(reason=six.text_type(err)) except Exception as err: err_msg = (_("Validate volume connection failed " "(error: %(err)s).") % {'err': six.text_type(err)}) LOG.error(err_msg, resource=volume) raise exception.VolumeBackendAPIException(data=err_msg) try: model_update = self.driver.create_export(context.elevated(), volume, connector) except exception.CinderException: err_msg = (_("Create export for volume failed.")) LOG.exception(err_msg, resource=volume) raise exception.VolumeBackendAPIException(data=err_msg) try: if model_update: volume = self.db.volume_update(context, volume_id, model_update) except exception.CinderException as ex: LOG.exception(_LE("Model update failed."), resource=volume) raise exception.ExportFailure(reason=six.text_type(ex)) try: conn_info = self.driver.initialize_connection(volume, connector) except Exception as err: err_msg = (_("Driver initialize connection failed " "(error: %(err)s).") % {'err': six.text_type(err)}) LOG.error(err_msg, resource=volume) self.driver.remove_export(context.elevated(), volume) raise exception.VolumeBackendAPIException(data=err_msg) # Add qos_specs to connection info typeid = volume['volume_type_id'] specs = None if typeid: res = volume_types.get_volume_type_qos_specs(typeid) qos = res['qos_specs'] # only pass qos_specs that is designated to be consumed by # front-end, or both front-end and back-end. if qos and qos.get('consumer') in ['front-end', 'both']: specs = qos.get('specs') qos_spec = dict(qos_specs=specs) conn_info['data'].update(qos_spec) # Add access_mode to connection info volume_metadata = self.db.volume_admin_metadata_get(context.elevated(), volume_id) access_mode = volume_metadata.get('attached_mode') if access_mode is None: # NOTE(zhiyan): client didn't call 'os-attach' before access_mode = ('ro' if volume_metadata.get('readonly') == 'True' else 'rw') conn_info['data']['access_mode'] = access_mode # Add encrypted flag to connection_info if not set in the driver. if conn_info['data'].get('encrypted') is None: encrypted = bool(volume.get('encryption_key_id')) conn_info['data']['encrypted'] = encrypted # Add discard flag to connection_info if not set in the driver and # configured to be reported. if conn_info['data'].get('discard') is None: discard_supported = (self.driver.configuration .safe_get('report_discard_supported')) if discard_supported: conn_info['data']['discard'] = True LOG.info(_LI("Initialize volume connection completed successfully."), resource=volume) return conn_info def terminate_connection(self, context, volume_id, connector, force=False): """Cleanup connection from host represented by connector. The format of connector is the same as for initialize_connection. """ # NOTE(flaper87): Verify the driver is enabled # before going forward. The exception will be caught # and the volume status updated. utils.require_driver_initialized(self.driver) volume_ref = self.db.volume_get(context, volume_id) try: self.driver.terminate_connection(volume_ref, connector, force=force) except Exception as err: err_msg = (_('Terminate volume connection failed: %(err)s') % {'err': six.text_type(err)}) LOG.error(err_msg, resource=volume_ref) raise exception.VolumeBackendAPIException(data=err_msg) LOG.info(_LI("Terminate volume connection completed successfully."), resource=volume_ref) def remove_export(self, context, volume_id): """Removes an export for a volume.""" utils.require_driver_initialized(self.driver) volume_ref = self.db.volume_get(context, volume_id) try: self.driver.remove_export(context, volume_ref) except Exception: msg = _("Remove volume export failed.") LOG.exception(msg, resource=volume_ref) raise exception.VolumeBackendAPIException(data=msg) LOG.info(_LI("Remove volume export completed successfully."), resource=volume_ref) def accept_transfer(self, context, volume_id, new_user, new_project): # NOTE(flaper87): Verify the driver is enabled # before going forward. The exception will be caught # and the volume status updated. utils.require_driver_initialized(self.driver) # NOTE(jdg): need elevated context as we haven't "given" the vol # yet volume_ref = self.db.volume_get(context.elevated(), volume_id) # NOTE(jdg): Some drivers tie provider info (CHAP) to tenant # for those that do allow them to return updated model info model_update = self.driver.accept_transfer(context, volume_ref, new_user, new_project) if model_update: try: self.db.volume_update(context.elevated(), volume_id, model_update) except exception.CinderException: with excutils.save_and_reraise_exception(): LOG.exception(_LE("Update volume model for " "transfer operation failed."), resource=volume_ref) self.db.volume_update(context.elevated(), volume_id, {'status': 'error'}) LOG.info(_LI("Transfer volume completed successfully."), resource=volume_ref) return model_update def _connect_device(self, conn): use_multipath = self.configuration.use_multipath_for_image_xfer device_scan_attempts = self.configuration.num_volume_device_scan_tries protocol = conn['driver_volume_type'] connector = utils.brick_get_connector( protocol, use_multipath=use_multipath, device_scan_attempts=device_scan_attempts, conn=conn) vol_handle = connector.connect_volume(conn['data']) root_access = True if not connector.check_valid_device(vol_handle['path'], root_access): if isinstance(vol_handle['path'], six.string_types): raise exception.DeviceUnavailable( path=vol_handle['path'], reason=(_("Unable to access the backend storage via the " "path %(path)s.") % {'path': vol_handle['path']})) else: raise exception.DeviceUnavailable( path=None, reason=(_("Unable to access the backend storage via file " "handle."))) return {'conn': conn, 'device': vol_handle, 'connector': connector} def _attach_volume(self, ctxt, volume, properties, remote=False): status = volume['status'] if remote: rpcapi = volume_rpcapi.VolumeAPI() try: conn = rpcapi.initialize_connection(ctxt, volume, properties) except Exception: with excutils.save_and_reraise_exception(): LOG.error(_LE("Failed to attach volume %(vol)s."), {'vol': volume['id']}) self.db.volume_update(ctxt, volume['id'], {'status': status}) else: conn = self.initialize_connection(ctxt, volume['id'], properties) return self._connect_device(conn) def _detach_volume(self, ctxt, attach_info, volume, properties, force=False, remote=False): connector = attach_info['connector'] connector.disconnect_volume(attach_info['conn']['data'], attach_info['device']) if remote: rpcapi = volume_rpcapi.VolumeAPI() rpcapi.terminate_connection(ctxt, volume, properties, force=force) rpcapi.remove_export(ctxt, volume) else: try: self.terminate_connection(ctxt, volume['id'], properties, force=force) self.remove_export(ctxt, volume['id']) except Exception as err: with excutils.save_and_reraise_exception(): LOG.error(_LE('Unable to terminate volume connection: ' '%(err)s.') % {'err': err}) def _copy_volume_data(self, ctxt, src_vol, dest_vol, remote=None): """Copy data from src_vol to dest_vol.""" LOG.debug('copy_data_between_volumes %(src)s -> %(dest)s.', {'src': src_vol['name'], 'dest': dest_vol['name']}) properties = utils.brick_get_connector_properties() dest_remote = remote in ['dest', 'both'] dest_attach_info = self._attach_volume(ctxt, dest_vol, properties, remote=dest_remote) try: src_remote = remote in ['src', 'both'] src_attach_info = self._attach_volume(ctxt, src_vol, properties, remote=src_remote) except Exception: with excutils.save_and_reraise_exception(): LOG.error(_LE("Failed to attach source volume for copy.")) self._detach_volume(ctxt, dest_attach_info, dest_vol, properties, remote=dest_remote) # Check the backend capabilities of migration destination host. rpcapi = volume_rpcapi.VolumeAPI() capabilities = rpcapi.get_capabilities(ctxt, dest_vol['host'], False) sparse_copy_volume = bool(capabilities and capabilities.get('sparse_copy_volume', False)) copy_error = True try: size_in_mb = int(src_vol['size']) * units.Ki # vol size is in GB vol_utils.copy_volume(src_attach_info['device']['path'], dest_attach_info['device']['path'], size_in_mb, self.configuration.volume_dd_blocksize, sparse=sparse_copy_volume) copy_error = False except Exception: with excutils.save_and_reraise_exception(): LOG.error(_LE("Failed to copy volume %(src)s to %(dest)s."), {'src': src_vol['id'], 'dest': dest_vol['id']}) finally: try: self._detach_volume(ctxt, dest_attach_info, dest_vol, properties, force=copy_error, remote=dest_remote) finally: self._detach_volume(ctxt, src_attach_info, src_vol, properties, force=copy_error, remote=src_remote) def _migrate_volume_generic(self, ctxt, volume, host, new_type_id): rpcapi = volume_rpcapi.VolumeAPI() # Create new volume on remote host skip = self._VOLUME_CLONE_SKIP_PROPERTIES | {'host'} new_vol_values = {k: volume[k] for k in set(volume.keys()) - skip} if new_type_id: new_vol_values['volume_type_id'] = new_type_id new_volume = objects.Volume( context=ctxt, host=host['host'], status='creating', attach_status='detached', migration_status='target:%s' % volume['id'], **new_vol_values ) new_volume.create() rpcapi.create_volume(ctxt, new_volume, host['host'], None, None, allow_reschedule=False) # Wait for new_volume to become ready starttime = time.time() deadline = starttime + CONF.migration_create_volume_timeout_secs # TODO(thangp): Replace get_by_id with refresh when it is available new_volume = objects.Volume.get_by_id(ctxt, new_volume.id) tries = 0 while new_volume.status != 'available': tries += 1 now = time.time() if new_volume.status == 'error': msg = _("failed to create new_volume on destination host") self._clean_temporary_volume(ctxt, volume, new_volume, clean_db_only=True) raise exception.VolumeMigrationFailed(reason=msg) elif now > deadline: msg = _("timeout creating new_volume on destination host") self._clean_temporary_volume(ctxt, volume, new_volume, clean_db_only=True) raise exception.VolumeMigrationFailed(reason=msg) else: time.sleep(tries ** 2) # TODO(thangp): Replace get_by_id with refresh when it is # available new_volume = objects.Volume.get_by_id(ctxt, new_volume.id) # Copy the source volume to the destination volume try: attachments = volume.volume_attachment if not attachments: # Pre- and post-copy driver-specific actions self.driver.before_volume_copy(ctxt, volume, new_volume, remote='dest') self._copy_volume_data(ctxt, volume, new_volume, remote='dest') self.driver.after_volume_copy(ctxt, volume, new_volume, remote='dest') # The above call is synchronous so we complete the migration self.migrate_volume_completion(ctxt, volume.id, new_volume.id, error=False, volume=volume, new_volume=new_volume) else: nova_api = compute.API() # This is an async call to Nova, which will call the completion # when it's done for attachment in attachments: instance_uuid = attachment['instance_uuid'] nova_api.update_server_volume(ctxt, instance_uuid, volume.id, new_volume.id) except Exception: with excutils.save_and_reraise_exception(): LOG.exception(_LE( "Failed to copy volume %(vol1)s to %(vol2)s"), { 'vol1': volume.id, 'vol2': new_volume.id}) self._clean_temporary_volume(ctxt, volume, new_volume) def _clean_temporary_volume(self, ctxt, volume, new_volume, clean_db_only=False): # If we're in the migrating phase, we need to cleanup # destination volume because source volume is remaining if volume.migration_status == 'migrating': try: if clean_db_only: # The temporary volume is not created, only DB data # is created new_volume.destroy() else: # The temporary volume is already created rpcapi = volume_rpcapi.VolumeAPI() rpcapi.delete_volume(ctxt, new_volume) except exception.VolumeNotFound: LOG.info(_LI("Couldn't find the temporary volume " "%(vol)s in the database. There is no need " "to clean up this volume."), {'vol': new_volume.id}) else: # If we're in the completing phase don't delete the # destination because we may have already deleted the # source! But the migration_status in database should # be cleared to handle volume after migration failure try: new_volume.migration_status = None new_volume.save() except exception.VolumeNotFound: LOG.info(_LI("Couldn't find destination volume " "%(vol)s in the database. The entry might be " "successfully deleted during migration " "completion phase."), {'vol': new_volume.id}) LOG.warning(_LW("Failed to migrate volume. The destination " "volume %(vol)s is not deleted since the " "source volume may have been deleted."), {'vol': new_volume.id}) def migrate_volume_completion(self, ctxt, volume_id, new_volume_id, error=False, volume=None, new_volume=None): # FIXME(dulek): Remove this in v3.0 of RPC API. if volume is None or new_volume is None: # For older clients, mimic the old behavior and look up the volume # by its volume_id. volume = objects.Volume.get_by_id(ctxt, volume_id) new_volume = objects.Volume.get_by_id(ctxt, new_volume_id) try: # NOTE(flaper87): Verify the driver is enabled # before going forward. The exception will be caught # and the migration status updated. utils.require_driver_initialized(self.driver) except exception.DriverNotInitialized: with excutils.save_and_reraise_exception(): volume.migration_status = 'error' volume.save() LOG.debug("migrate_volume_completion: completing migration for " "volume %(vol1)s (temporary volume %(vol2)s", {'vol1': volume.id, 'vol2': new_volume.id}) rpcapi = volume_rpcapi.VolumeAPI() orig_volume_status = volume.previous_status if error: LOG.info(_LI("migrate_volume_completion is cleaning up an error " "for volume %(vol1)s (temporary volume %(vol2)s"), {'vol1': volume['id'], 'vol2': new_volume.id}) rpcapi.delete_volume(ctxt, new_volume) updates = {'migration_status': 'error', 'status': orig_volume_status} volume.update(updates) volume.save() return volume.id volume.migration_status = 'completing' volume.save() # Detach the source volume (if it fails, don't fail the migration) try: if orig_volume_status == 'in-use': attachments = volume.volume_attachment for attachment in attachments: self.detach_volume(ctxt, volume.id, attachment['id']) except Exception as ex: LOG.error(_LE("Detach migration source volume failed: %(err)s"), {'err': ex}, resource=volume) # Give driver (new_volume) a chance to update things as needed # after a successful migration. # Note this needs to go through rpc to the host of the new volume # the current host and driver object is for the "existing" volume. rpcapi.update_migrated_volume(ctxt, volume, new_volume, orig_volume_status) volume.refresh() new_volume.refresh() # Swap src and dest DB records so we can continue using the src id and # asynchronously delete the destination id updated_new = volume.finish_volume_migration(new_volume) updates = {'status': orig_volume_status, 'previous_status': volume.status, 'migration_status': 'success'} if orig_volume_status == 'in-use': attachments = volume.volume_attachment for attachment in attachments: rpcapi.attach_volume(ctxt, volume, attachment['instance_uuid'], attachment['attached_host'], attachment['mountpoint'], 'rw') volume.update(updates) volume.save() # Asynchronous deletion of the source volume in the back-end (now # pointed by the target volume id) try: rpcapi.delete_volume(ctxt, updated_new) except Exception as ex: LOG.error(_LE('Failed to request async delete of migration source ' 'vol %(vol)s: %(err)s'), {'vol': volume.id, 'err': ex}) LOG.info(_LI("Complete-Migrate volume completed successfully."), resource=volume) return volume.id def migrate_volume(self, ctxt, volume_id, host, force_host_copy=False, new_type_id=None, volume=None): """Migrate the volume to the specified host (called on source host).""" # FIXME(dulek): Remove this in v3.0 of RPC API. if volume is None: # For older clients, mimic the old behavior and look up the volume # by its volume_id. volume = objects.Volume.get_by_id(ctxt, volume_id) try: # NOTE(flaper87): Verify the driver is enabled # before going forward. The exception will be caught # and the migration status updated. utils.require_driver_initialized(self.driver) except exception.DriverNotInitialized: with excutils.save_and_reraise_exception(): volume.migration_status = 'error' volume.save() model_update = None moved = False status_update = None if volume.status in ('retyping', 'maintenance'): status_update = {'status': volume.previous_status} volume.migration_status = 'migrating' volume.save() if not force_host_copy and new_type_id is None: try: LOG.debug("Issue driver.migrate_volume.", resource=volume) moved, model_update = self.driver.migrate_volume(ctxt, volume, host) if moved: updates = {'host': host['host'], 'migration_status': 'success', 'previous_status': volume.status} if status_update: updates.update(status_update) if model_update: updates.update(model_update) volume.update(updates) volume.save() except Exception: with excutils.save_and_reraise_exception(): updates = {'migration_status': 'error'} if status_update: updates.update(status_update) volume.update(updates) volume.save() if not moved: try: self._migrate_volume_generic(ctxt, volume, host, new_type_id) except Exception: with excutils.save_and_reraise_exception(): updates = {'migration_status': 'error'} if status_update: updates.update(status_update) volume.update(updates) volume.save() LOG.info(_LI("Migrate volume completed successfully."), resource=volume) @periodic_task.periodic_task def _report_driver_status(self, context): if not self.driver.initialized: if self.driver.configuration.config_group is None: config_group = '' else: config_group = ('(config name %s)' % self.driver.configuration.config_group) LOG.warning(_LW("Update driver status failed: %(config_group)s " "is uninitialized."), {'config_group': config_group}, resource={'type': 'driver', 'id': self.driver.__class__.__name__}) else: volume_stats = self.driver.get_volume_stats(refresh=True) if self.extra_capabilities: volume_stats.update(self.extra_capabilities) if volume_stats: # Append volume stats with 'allocated_capacity_gb' self._append_volume_stats(volume_stats) # Append filter and goodness function if needed volume_stats = ( self._append_filter_goodness_functions(volume_stats)) # queue it to be sent to the Schedulers. self.update_service_capabilities(volume_stats) def _append_volume_stats(self, vol_stats): pools = vol_stats.get('pools', None) if pools and isinstance(pools, list): for pool in pools: pool_name = pool['pool_name'] try: pool_stats = self.stats['pools'][pool_name] except KeyError: # Pool not found in volume manager pool_stats = dict(allocated_capacity_gb=0) pool.update(pool_stats) def _append_filter_goodness_functions(self, volume_stats): """Returns volume_stats updated as needed.""" # Append filter_function if needed if 'filter_function' not in volume_stats: volume_stats['filter_function'] = ( self.driver.get_filter_function()) # Append goodness_function if needed if 'goodness_function' not in volume_stats: volume_stats['goodness_function'] = ( self.driver.get_goodness_function()) return volume_stats def publish_service_capabilities(self, context): """Collect driver status and then publish.""" self._report_driver_status(context) self._publish_service_capabilities(context) def _notify_about_volume_usage(self, context, volume, event_suffix, extra_usage_info=None): vol_utils.notify_about_volume_usage( context, volume, event_suffix, extra_usage_info=extra_usage_info, host=self.host) def _notify_about_snapshot_usage(self, context, snapshot, event_suffix, extra_usage_info=None): vol_utils.notify_about_snapshot_usage( context, snapshot, event_suffix, extra_usage_info=extra_usage_info, host=self.host) def _notify_about_consistencygroup_usage(self, context, group, event_suffix, volumes=None, extra_usage_info=None): vol_utils.notify_about_consistencygroup_usage( context, group, event_suffix, extra_usage_info=extra_usage_info, host=self.host) if not volumes: volumes = self.db.volume_get_all_by_group(context, group.id) if volumes: for volume in volumes: vol_utils.notify_about_volume_usage( context, volume, event_suffix, extra_usage_info=extra_usage_info, host=self.host) def _notify_about_cgsnapshot_usage(self, context, cgsnapshot, event_suffix, snapshots=None, extra_usage_info=None): vol_utils.notify_about_cgsnapshot_usage( context, cgsnapshot, event_suffix, extra_usage_info=extra_usage_info, host=self.host) if not snapshots: snapshots = objects.SnapshotList.get_all_for_cgsnapshot( context, cgsnapshot.id) if snapshots: for snapshot in snapshots: vol_utils.notify_about_snapshot_usage( context, snapshot, event_suffix, extra_usage_info=extra_usage_info, host=self.host) def extend_volume(self, context, volume_id, new_size, reservations, volume=None): # FIXME(dulek): Remove this in v3.0 of RPC API. if volume is None: # For older clients, mimic the old behavior and look up the volume # by its volume_id. volume = objects.Volume.get_by_id(context, volume_id) try: # NOTE(flaper87): Verify the driver is enabled # before going forward. The exception will be caught # and the volume status updated. utils.require_driver_initialized(self.driver) except exception.DriverNotInitialized: with excutils.save_and_reraise_exception(): volume.status = 'error_extending' volume.save() project_id = volume.project_id size_increase = (int(new_size)) - volume.size self._notify_about_volume_usage(context, volume, "resize.start") try: self.driver.extend_volume(volume, new_size) except Exception: LOG.exception(_LE("Extend volume failed."), resource=volume) try: self.db.volume_update(context, volume.id, {'status': 'error_extending'}) raise exception.CinderException(_("Volume %s: Error trying " "to extend volume") % volume.id) finally: QUOTAS.rollback(context, reservations, project_id=project_id) return QUOTAS.commit(context, reservations, project_id=project_id) volume.update({'size': int(new_size), 'status': 'available'}) volume.save() pool = vol_utils.extract_host(volume.host, 'pool') if pool is None: # Legacy volume, put them into default pool pool = self.driver.configuration.safe_get( 'volume_backend_name') or vol_utils.extract_host( volume.host, 'pool', True) try: self.stats['pools'][pool]['allocated_capacity_gb'] += size_increase except KeyError: self.stats['pools'][pool] = dict( allocated_capacity_gb=size_increase) self._notify_about_volume_usage( context, volume, "resize.end", extra_usage_info={'size': int(new_size)}) LOG.info(_LI("Extend volume completed successfully."), resource=volume) def retype(self, ctxt, volume_id, new_type_id, host, migration_policy='never', reservations=None, volume=None, old_reservations=None): def _retype_error(context, volume, old_reservations, new_reservations, status_update): try: volume.update(status_update) volume.save() finally: QUOTAS.rollback(context, old_reservations) QUOTAS.rollback(context, new_reservations) context = ctxt.elevated() # FIXME(dulek): Remove this in v3.0 of RPC API. if volume is None: # For older clients, mimic the old behavior and look up the volume # by its volume_id. volume = objects.Volume.get_by_id(context, volume_id) status_update = {'status': volume.previous_status} if context.project_id != volume.project_id: project_id = volume.project_id else: project_id = context.project_id try: # NOTE(flaper87): Verify the driver is enabled # before going forward. The exception will be caught # and the volume status updated. utils.require_driver_initialized(self.driver) except exception.DriverNotInitialized: with excutils.save_and_reraise_exception(): # NOTE(flaper87): Other exceptions in this method don't # set the volume status to error. Should that be done # here? Setting the volume back to it's original status # for now. volume.update(status_update) volume.save() # If old_reservations has been passed in from the API, we should # skip quotas. # TODO(ntpttr): These reservation checks are left in to be backwards # compatible with Liberty and can be removed in N. if not old_reservations: # Get old reservations try: reserve_opts = {'volumes': -1, 'gigabytes': -volume.size} QUOTAS.add_volume_type_opts(context, reserve_opts, volume.volume_type_id) # NOTE(wanghao): We don't need to reserve volumes and gigabytes # quota for retyping operation since they didn't changed, just # reserve volume_type and type gigabytes is fine. reserve_opts.pop('volumes') reserve_opts.pop('gigabytes') old_reservations = QUOTAS.reserve(context, project_id=project_id, **reserve_opts) except Exception: volume.update(status_update) volume.save() msg = _("Failed to update quota usage while retyping volume.") LOG.exception(msg, resource=volume) raise exception.CinderException(msg) # We already got the new reservations new_reservations = reservations # If volume types have the same contents, no need to do anything retyped = False diff, all_equal = volume_types.volume_types_diff( context, volume.volume_type_id, new_type_id) if all_equal: retyped = True # Call driver to try and change the type retype_model_update = None # NOTE(jdg): Check to see if the destination host is the same # as the current. If it's not don't call the driver.retype # method, otherwise drivers that implement retype may report # success, but it's invalid in the case of a migrate. # We assume that those that support pools do this internally # so we strip off the pools designation if (not retyped and vol_utils.hosts_are_equivalent(self.driver.host, host['host'])): try: new_type = volume_types.get_volume_type(context, new_type_id) ret = self.driver.retype(context, volume, new_type, diff, host) # Check if the driver retype provided a model update or # just a retype indication if type(ret) == tuple: retyped, retype_model_update = ret else: retyped = ret if retyped: LOG.info(_LI("Volume %s: retyped successfully"), volume.id) except Exception: retyped = False LOG.exception(_LE("Volume %s: driver error when trying to " "retype, falling back to generic " "mechanism."), volume.id) # We could not change the type, so we need to migrate the volume, where # the destination volume will be of the new type if not retyped: if migration_policy == 'never': _retype_error(context, volume, old_reservations, new_reservations, status_update) msg = _("Retype requires migration but is not allowed.") raise exception.VolumeMigrationFailed(reason=msg) snaps = objects.SnapshotList.get_all_for_volume(context, volume.id) if snaps: _retype_error(context, volume, old_reservations, new_reservations, status_update) msg = _("Volume must not have snapshots.") LOG.error(msg) raise exception.InvalidVolume(reason=msg) # Don't allow volume with replicas to be migrated rep_status = volume.replication_status if rep_status is not None and rep_status != 'disabled': _retype_error(context, volume, old_reservations, new_reservations, status_update) msg = _("Volume must not be replicated.") LOG.error(msg) raise exception.InvalidVolume(reason=msg) volume.migration_status = 'starting' volume.save() try: self.migrate_volume(context, volume.id, host, new_type_id=new_type_id) except Exception: with excutils.save_and_reraise_exception(): _retype_error(context, volume, old_reservations, new_reservations, status_update) else: model_update = {'volume_type_id': new_type_id, 'host': host['host'], 'status': status_update['status']} if retype_model_update: model_update.update(retype_model_update) volume.update(model_update) volume.save() if old_reservations: QUOTAS.commit(context, old_reservations, project_id=project_id) if new_reservations: QUOTAS.commit(context, new_reservations, project_id=project_id) self._notify_about_volume_usage( context, volume, "retype", extra_usage_info={'volume_type': new_type_id}) self.publish_service_capabilities(context) LOG.info(_LI("Retype volume completed successfully."), resource=volume) def manage_existing(self, ctxt, volume_id, ref=None): try: flow_engine = manage_existing.get_flow( ctxt, self.db, self.driver, self.host, volume_id, ref) except Exception: msg = _("Failed to create manage_existing flow.") LOG.exception(msg, resource={'type': 'volume', 'id': volume_id}) raise exception.CinderException(msg) with flow_utils.DynamicLogListener(flow_engine, logger=LOG): flow_engine.run() # Fetch created volume from storage vol_ref = flow_engine.storage.fetch('volume') # Update volume stats pool = vol_utils.extract_host(vol_ref['host'], 'pool') if pool is None: # Legacy volume, put them into default pool pool = self.driver.configuration.safe_get( 'volume_backend_name') or vol_utils.extract_host( vol_ref['host'], 'pool', True) try: self.stats['pools'][pool]['allocated_capacity_gb'] \ += vol_ref['size'] except KeyError: self.stats['pools'][pool] = dict( allocated_capacity_gb=vol_ref['size']) LOG.info(_LI("Manage existing volume completed successfully."), resource=vol_ref) return vol_ref['id'] def promote_replica(self, ctxt, volume_id): """Promote volume replica secondary to be the primary volume.""" volume = self.db.volume_get(ctxt, volume_id) model_update = None try: utils.require_driver_initialized(self.driver) except exception.DriverNotInitialized: with excutils.save_and_reraise_exception(): LOG.exception(_LE("Promote volume replica failed."), resource=volume) try: model_update = self.driver.promote_replica(ctxt, volume) except exception.CinderException: err_msg = (_('Error promoting secondary volume to primary')) raise exception.ReplicationError(reason=err_msg, volume_id=volume_id) try: if model_update: volume = self.db.volume_update(ctxt, volume_id, model_update) except exception.CinderException: err_msg = (_("Failed updating model" " with driver provided model %(model)s") % {'model': model_update}) raise exception.ReplicationError(reason=err_msg, volume_id=volume_id) LOG.info(_LI("Promote volume replica completed successfully."), resource=volume) def reenable_replication(self, ctxt, volume_id): """Re-enable replication of secondary volume with primary volumes.""" volume = self.db.volume_get(ctxt, volume_id) model_update = None try: utils.require_driver_initialized(self.driver) except exception.DriverNotInitialized: with excutils.save_and_reraise_exception(): LOG.exception(_LE("Sync volume replica failed."), resource=volume) try: model_update = self.driver.reenable_replication(ctxt, volume) except exception.CinderException: err_msg = (_("Synchronizing secondary volume to primary failed.")) raise exception.ReplicationError(reason=err_msg, volume_id=volume_id) try: if model_update: volume = self.db.volume_update(ctxt, volume_id, model_update) except exception.CinderException: err_msg = (_("Failed updating model" " with driver provided model %(model)s") % {'model': model_update}) raise exception.ReplicationError(reason=err_msg, volume_id=volume_id) def _update_replication_relationship_status(self, ctxt): # Only want volumes that do not have a 'disabled' replication status filters = {'replication_status': ['active', 'copying', 'error', 'active-stopped', 'inactive']} volumes = self.db.volume_get_all_by_host(ctxt, self.host, filters=filters) for vol in volumes: model_update = None try: model_update = self.driver.get_replication_status( ctxt, vol) if model_update: self.db.volume_update(ctxt, vol['id'], model_update) except Exception: LOG.exception(_LE("Get replication status for volume failed."), resource=vol) def create_consistencygroup(self, context, group): """Creates the consistency group.""" context = context.elevated() status = fields.ConsistencyGroupStatus.AVAILABLE model_update = None self._notify_about_consistencygroup_usage( context, group, "create.start") try: utils.require_driver_initialized(self.driver) LOG.info(_LI("Consistency group %s: creating"), group.name) model_update = self.driver.create_consistencygroup(context, group) if model_update: if (model_update['status'] == fields.ConsistencyGroupStatus.ERROR): msg = (_('Create consistency group failed.')) LOG.error(msg, resource={'type': 'consistency_group', 'id': group.id}) raise exception.VolumeDriverException(message=msg) else: group.update(model_update) group.save() except Exception: with excutils.save_and_reraise_exception(): group.status = fields.ConsistencyGroupStatus.ERROR group.save() LOG.error(_LE("Consistency group %s: create failed"), group.name) group.status = status group.created_at = timeutils.utcnow() group.save() LOG.info(_LI("Consistency group %s: created successfully"), group.name) self._notify_about_consistencygroup_usage( context, group, "create.end") LOG.info(_LI("Create consistency group completed successfully."), resource={'type': 'consistency_group', 'id': group.id}) return group def create_consistencygroup_from_src(self, context, group, cgsnapshot=None, source_cg=None): """Creates the consistency group from source. The source can be a CG snapshot or a source CG. """ source_name = None snapshots = None source_vols = None try: volumes = self.db.volume_get_all_by_group(context, group.id) if cgsnapshot: try: # Check if cgsnapshot still exists cgsnapshot = objects.CGSnapshot.get_by_id( context, cgsnapshot.id) except exception.CgSnapshotNotFound: LOG.error(_LE("Create consistency group " "from snapshot-%(snap)s failed: " "SnapshotNotFound."), {'snap': cgsnapshot.id}, resource={'type': 'consistency_group', 'id': group.id}) raise source_name = _("snapshot-%s") % cgsnapshot.id snapshots = objects.SnapshotList.get_all_for_cgsnapshot( context, cgsnapshot.id) for snap in snapshots: if (snap.status not in VALID_CREATE_CG_SRC_SNAP_STATUS): msg = (_("Cannot create consistency group " "%(group)s because snapshot %(snap)s is " "not in a valid state. Valid states are: " "%(valid)s.") % {'group': group.id, 'snap': snap['id'], 'valid': VALID_CREATE_CG_SRC_SNAP_STATUS}) raise exception.InvalidConsistencyGroup(reason=msg) if source_cg: try: source_cg = objects.ConsistencyGroup.get_by_id( context, source_cg.id) except exception.ConsistencyGroupNotFound: LOG.error(_LE("Create consistency group " "from source cg-%(cg)s failed: " "ConsistencyGroupNotFound."), {'cg': source_cg.id}, resource={'type': 'consistency_group', 'id': group.id}) raise source_name = _("cg-%s") % source_cg.id source_vols = self.db.volume_get_all_by_group( context, source_cg.id) for source_vol in source_vols: if (source_vol['status'] not in VALID_CREATE_CG_SRC_CG_STATUS): msg = (_("Cannot create consistency group " "%(group)s because source volume " "%(source_vol)s is not in a valid " "state. Valid states are: " "%(valid)s.") % {'group': group.id, 'source_vol': source_vol['id'], 'valid': VALID_CREATE_CG_SRC_CG_STATUS}) raise exception.InvalidConsistencyGroup(reason=msg) # Sort source snapshots so that they are in the same order as their # corresponding target volumes. sorted_snapshots = None if cgsnapshot and snapshots: sorted_snapshots = self._sort_snapshots(volumes, snapshots) # Sort source volumes so that they are in the same order as their # corresponding target volumes. sorted_source_vols = None if source_cg and source_vols: sorted_source_vols = self._sort_source_vols(volumes, source_vols) self._notify_about_consistencygroup_usage( context, group, "create.start") utils.require_driver_initialized(self.driver) model_update, volumes_model_update = ( self.driver.create_consistencygroup_from_src( context, group, volumes, cgsnapshot, sorted_snapshots, source_cg, sorted_source_vols)) if volumes_model_update: for update in volumes_model_update: self.db.volume_update(context, update['id'], update) if model_update: group.update(model_update) group.save() except Exception: with excutils.save_and_reraise_exception(): group.status = 'error' group.save() LOG.error(_LE("Create consistency group " "from source %(source)s failed."), {'source': source_name}, resource={'type': 'consistency_group', 'id': group.id}) # Update volume status to 'error' as well. for vol in volumes: self.db.volume_update( context, vol['id'], {'status': 'error'}) now = timeutils.utcnow() status = 'available' for vol in volumes: update = {'status': status, 'created_at': now} self._update_volume_from_src(context, vol, update, group=group) self._update_allocated_capacity(vol) group.status = status group.created_at = now group.save() self._notify_about_consistencygroup_usage( context, group, "create.end") LOG.info(_LI("Create consistency group " "from source-%(source)s completed successfully."), {'source': source_name}, resource={'type': 'consistency_group', 'id': group.id}) return group def _sort_snapshots(self, volumes, snapshots): # Sort source snapshots so that they are in the same order as their # corresponding target volumes. Each source snapshot in the snapshots # list should have a corresponding target volume in the volumes list. if not volumes or not snapshots or len(volumes) != len(snapshots): msg = _("Input volumes or snapshots are invalid.") LOG.error(msg) raise exception.InvalidInput(reason=msg) sorted_snapshots = [] for vol in volumes: found_snaps = [snap for snap in snapshots if snap['id'] == vol['snapshot_id']] if not found_snaps: LOG.error(_LE("Source snapshot cannot be found for target " "volume %(volume_id)s."), {'volume_id': vol['id']}) raise exception.SnapshotNotFound( snapshot_id=vol['snapshot_id']) sorted_snapshots.extend(found_snaps) return sorted_snapshots def _sort_source_vols(self, volumes, source_vols): # Sort source volumes so that they are in the same order as their # corresponding target volumes. Each source volume in the source_vols # list should have a corresponding target volume in the volumes list. if not volumes or not source_vols or len(volumes) != len(source_vols): msg = _("Input volumes or source volumes are invalid.") LOG.error(msg) raise exception.InvalidInput(reason=msg) sorted_source_vols = [] for vol in volumes: found_source_vols = [source_vol for source_vol in source_vols if source_vol['id'] == vol['source_volid']] if not found_source_vols: LOG.error(_LE("Source volumes cannot be found for target " "volume %(volume_id)s."), {'volume_id': vol['id']}) raise exception.VolumeNotFound( volume_id=vol['source_volid']) sorted_source_vols.extend(found_source_vols) return sorted_source_vols def _update_volume_from_src(self, context, vol, update, group=None): try: snapshot_id = vol.get('snapshot_id') if snapshot_id: snapshot = objects.Snapshot.get_by_id(context, snapshot_id) orig_vref = self.db.volume_get(context, snapshot.volume_id) if orig_vref.bootable: update['bootable'] = True self.db.volume_glance_metadata_copy_to_volume( context, vol['id'], snapshot_id) except exception.SnapshotNotFound: LOG.error(_LE("Source snapshot %(snapshot_id)s cannot be found."), {'snapshot_id': vol['snapshot_id']}) self.db.volume_update(context, vol['id'], {'status': 'error'}) if group: group.status = 'error' group.save() raise except exception.VolumeNotFound: LOG.error(_LE("The source volume %(volume_id)s " "cannot be found."), {'volume_id': snapshot.volume_id}) self.db.volume_update(context, vol['id'], {'status': 'error'}) if group: group.status = 'error' group.save() raise except exception.CinderException as ex: LOG.error(_LE("Failed to update %(volume_id)s" " metadata using the provided snapshot" " %(snapshot_id)s metadata."), {'volume_id': vol['id'], 'snapshot_id': vol['snapshot_id']}) self.db.volume_update(context, vol['id'], {'status': 'error'}) if group: group.status = 'error' group.save() raise exception.MetadataCopyFailure(reason=six.text_type(ex)) self.db.volume_update(context, vol['id'], update) def _update_allocated_capacity(self, vol): # Update allocated capacity in volume stats pool = vol_utils.extract_host(vol['host'], 'pool') if pool is None: # Legacy volume, put them into default pool pool = self.driver.configuration.safe_get( 'volume_backend_name') or vol_utils.extract_host( vol['host'], 'pool', True) try: self.stats['pools'][pool]['allocated_capacity_gb'] += ( vol['size']) except KeyError: self.stats['pools'][pool] = dict( allocated_capacity_gb=vol['size']) def delete_consistencygroup(self, context, group): """Deletes consistency group and the volumes in the group.""" context = context.elevated() project_id = group.project_id if context.project_id != group.project_id: project_id = group.project_id else: project_id = context.project_id volumes = self.db.volume_get_all_by_group(context, group.id) for volume_ref in volumes: if volume_ref['attach_status'] == "attached": # Volume is still attached, need to detach first raise exception.VolumeAttached(volume_id=volume_ref['id']) # self.host is 'host@backend' # volume_ref['host'] is 'host@backend#pool' # Extract host before doing comparison if volume_ref['host']: new_host = vol_utils.extract_host(volume_ref['host']) if new_host != self.host: raise exception.InvalidVolume( reason=_("Volume is not local to this node")) self._notify_about_consistencygroup_usage( context, group, "delete.start") volumes_model_update = None model_update = None try: utils.require_driver_initialized(self.driver) model_update, volumes_model_update = ( self.driver.delete_consistencygroup(context, group, volumes)) if volumes_model_update: for volume in volumes_model_update: update = {'status': volume['status']} self.db.volume_update(context, volume['id'], update) # If we failed to delete a volume, make sure the status # for the cg is set to error as well if (volume['status'] in ['error_deleting', 'error'] and model_update['status'] not in ['error_deleting', 'error']): model_update['status'] = volume['status'] if model_update: if model_update['status'] in ['error_deleting', 'error']: msg = (_('Delete consistency group failed.')) LOG.error(msg, resource={'type': 'consistency_group', 'id': group.id}) raise exception.VolumeDriverException(message=msg) else: group.update(model_update) group.save() except Exception: with excutils.save_and_reraise_exception(): group.status = 'error' group.save() # Update volume status to 'error' if driver returns # None for volumes_model_update. if not volumes_model_update: for vol in volumes: self.db.volume_update( context, vol['id'], {'status': 'error'}) # Get reservations for group try: reserve_opts = {'consistencygroups': -1} cgreservations = CGQUOTAS.reserve(context, project_id=project_id, **reserve_opts) except Exception: cgreservations = None LOG.exception(_LE("Delete consistency group " "failed to update usages."), resource={'type': 'consistency_group', 'id': group.id}) for volume_ref in volumes: # Get reservations for volume try: volume_id = volume_ref['id'] reserve_opts = {'volumes': -1, 'gigabytes': -volume_ref['size']} QUOTAS.add_volume_type_opts(context, reserve_opts, volume_ref.get('volume_type_id')) reservations = QUOTAS.reserve(context, project_id=project_id, **reserve_opts) except Exception: reservations = None LOG.exception(_LE("Delete consistency group " "failed to update usages."), resource={'type': 'consistency_group', 'id': group.id}) # Delete glance metadata if it exists self.db.volume_glance_metadata_delete_by_volume(context, volume_id) self.db.volume_destroy(context, volume_id) # Commit the reservations if reservations: QUOTAS.commit(context, reservations, project_id=project_id) self.stats['allocated_capacity_gb'] -= volume_ref['size'] if cgreservations: CGQUOTAS.commit(context, cgreservations, project_id=project_id) group.destroy() self._notify_about_consistencygroup_usage( context, group, "delete.end", volumes) self.publish_service_capabilities(context) LOG.info(_LI("Delete consistency group " "completed successfully."), resource={'type': 'consistency_group', 'id': group.id}) def update_consistencygroup(self, context, group, add_volumes=None, remove_volumes=None): """Updates consistency group. Update consistency group by adding volumes to the group, or removing volumes from the group. """ add_volumes_ref = [] remove_volumes_ref = [] add_volumes_list = [] remove_volumes_list = [] if add_volumes: add_volumes_list = add_volumes.split(',') if remove_volumes: remove_volumes_list = remove_volumes.split(',') for add_vol in add_volumes_list: try: add_vol_ref = self.db.volume_get(context, add_vol) except exception.VolumeNotFound: LOG.error(_LE("Update consistency group " "failed to add volume-%(volume_id)s: " "VolumeNotFound."), {'volume_id': add_vol_ref['id']}, resource={'type': 'consistency_group', 'id': group.id}) raise if add_vol_ref['status'] not in VALID_ADD_VOL_TO_CG_STATUS: msg = (_("Cannot add volume %(volume_id)s to consistency " "group %(group_id)s because volume is in an invalid " "state: %(status)s. Valid states are: %(valid)s.") % {'volume_id': add_vol_ref['id'], 'group_id': group.id, 'status': add_vol_ref['status'], 'valid': VALID_ADD_VOL_TO_CG_STATUS}) raise exception.InvalidVolume(reason=msg) # self.host is 'host@backend' # volume_ref['host'] is 'host@backend#pool' # Extract host before doing comparison new_host = vol_utils.extract_host(add_vol_ref['host']) if new_host != self.host: raise exception.InvalidVolume( reason=_("Volume is not local to this node.")) add_volumes_ref.append(add_vol_ref) for remove_vol in remove_volumes_list: try: remove_vol_ref = self.db.volume_get(context, remove_vol) except exception.VolumeNotFound: LOG.error(_LE("Update consistency group " "failed to remove volume-%(volume_id)s: " "VolumeNotFound."), {'volume_id': remove_vol_ref['id']}, resource={'type': 'consistency_group', 'id': group.id}) raise if remove_vol_ref['status'] not in VALID_REMOVE_VOL_FROM_CG_STATUS: msg = (_("Cannot remove volume %(volume_id)s from consistency " "group %(group_id)s because volume is in an invalid " "state: %(status)s. Valid states are: %(valid)s.") % {'volume_id': remove_vol_ref['id'], 'group_id': group.id, 'status': remove_vol_ref['status'], 'valid': VALID_REMOVE_VOL_FROM_CG_STATUS}) raise exception.InvalidVolume(reason=msg) remove_volumes_ref.append(remove_vol_ref) self._notify_about_consistencygroup_usage( context, group, "update.start") try: utils.require_driver_initialized(self.driver) model_update, add_volumes_update, remove_volumes_update = ( self.driver.update_consistencygroup( context, group, add_volumes=add_volumes_ref, remove_volumes=remove_volumes_ref)) if add_volumes_update: for update in add_volumes_update: self.db.volume_update(context, update['id'], update) if remove_volumes_update: for update in remove_volumes_update: self.db.volume_update(context, update['id'], update) if model_update: if model_update['status'] in ( [fields.ConsistencyGroupStatus.ERROR]): msg = (_('Error occurred when updating consistency group ' '%s.') % group.id) LOG.error(msg) raise exception.VolumeDriverException(message=msg) group.update(model_update) group.save() except exception.VolumeDriverException: with excutils.save_and_reraise_exception(): LOG.error(_LE("Error occurred in the volume driver when " "updating consistency group %(group_id)s."), {'group_id': group.id}) group.status = 'error' group.save() for add_vol in add_volumes_ref: self.db.volume_update(context, add_vol['id'], {'status': 'error'}) for rem_vol in remove_volumes_ref: self.db.volume_update(context, rem_vol['id'], {'status': 'error'}) except Exception: with excutils.save_and_reraise_exception(): LOG.error(_LE("Error occurred when updating consistency " "group %(group_id)s."), {'group_id': group.id}) group.status = 'error' group.save() for add_vol in add_volumes_ref: self.db.volume_update(context, add_vol['id'], {'status': 'error'}) for rem_vol in remove_volumes_ref: self.db.volume_update(context, rem_vol['id'], {'status': 'error'}) now = timeutils.utcnow() group.status = 'available' group.update_at = now group.save() for add_vol in add_volumes_ref: self.db.volume_update(context, add_vol['id'], {'consistencygroup_id': group.id, 'updated_at': now}) for rem_vol in remove_volumes_ref: self.db.volume_update(context, rem_vol['id'], {'consistencygroup_id': None, 'updated_at': now}) self._notify_about_consistencygroup_usage( context, group, "update.end") LOG.info(_LI("Update consistency group " "completed successfully."), resource={'type': 'consistency_group', 'id': group.id}) def create_cgsnapshot(self, context, cgsnapshot): """Creates the cgsnapshot.""" caller_context = context context = context.elevated() LOG.info(_LI("Cgsnapshot %s: creating."), cgsnapshot.id) snapshots = objects.SnapshotList.get_all_for_cgsnapshot( context, cgsnapshot.id) self._notify_about_cgsnapshot_usage( context, cgsnapshot, "create.start") snapshots_model_update = None model_update = None try: utils.require_driver_initialized(self.driver) LOG.debug("Cgsnapshot %(cgsnap_id)s: creating.", {'cgsnap_id': cgsnapshot.id}) # Pass context so that drivers that want to use it, can, # but it is not a requirement for all drivers. cgsnapshot.context = caller_context for snapshot in snapshots: snapshot.context = caller_context model_update, snapshots_model_update = ( self.driver.create_cgsnapshot(context, cgsnapshot, snapshots)) if snapshots_model_update: for snap_model in snapshots_model_update: # Update db for snapshot. # NOTE(xyang): snapshots is a list of snapshot objects. # snapshots_model_update should be a list of dicts. self.db.snapshot_update(context, snap_model['id'], snap_model) if (snap_model['status'] in [ fields.SnapshotStatus.ERROR_DELETING, fields.SnapshotStatus.ERROR] and model_update['status'] not in ['error_deleting', 'error']): model_update['status'] = snap_model['status'] if model_update: if model_update['status'] == 'error': msg = (_('Error occurred when creating cgsnapshot ' '%s.') % cgsnapshot.id) LOG.error(msg) raise exception.VolumeDriverException(message=msg) cgsnapshot.update(model_update) cgsnapshot.save() except exception.CinderException: with excutils.save_and_reraise_exception(): cgsnapshot.status = 'error' cgsnapshot.save() # Update snapshot status to 'error' if driver returns # None for snapshots_model_update. if not snapshots_model_update: for snapshot in snapshots: snapshot.status = fields.SnapshotStatus.ERROR snapshot.save() for snapshot in snapshots: volume_id = snapshot['volume_id'] snapshot_id = snapshot['id'] vol_ref = self.db.volume_get(context, volume_id) if vol_ref.bootable: try: self.db.volume_glance_metadata_copy_to_snapshot( context, snapshot_id, volume_id) except exception.CinderException as ex: LOG.error(_LE("Failed updating %(snapshot_id)s" " metadata using the provided volumes" " %(volume_id)s metadata"), {'volume_id': volume_id, 'snapshot_id': snapshot_id}) # TODO(thangp): Switch over to use snapshot.update() # after cgsnapshot-objects bugs are fixed self.db.snapshot_update( context, snapshot_id, { 'status': fields.SnapshotStatus.ERROR}) raise exception.MetadataCopyFailure( reason=six.text_type(ex)) self.db.snapshot_update(context, snapshot['id'], {'status': fields.SnapshotStatus.AVAILABLE, 'progress': '100%'}) cgsnapshot.status = 'available' cgsnapshot.save() LOG.info(_LI("cgsnapshot %s: created successfully"), cgsnapshot.id) self._notify_about_cgsnapshot_usage( context, cgsnapshot, "create.end") return cgsnapshot def delete_cgsnapshot(self, context, cgsnapshot): """Deletes cgsnapshot.""" caller_context = context context = context.elevated() project_id = cgsnapshot.project_id LOG.info(_LI("cgsnapshot %s: deleting"), cgsnapshot.id) snapshots = objects.SnapshotList.get_all_for_cgsnapshot( context, cgsnapshot.id) self._notify_about_cgsnapshot_usage( context, cgsnapshot, "delete.start") snapshots_model_update = None model_update = None try: utils.require_driver_initialized(self.driver) LOG.debug("cgsnapshot %(cgsnap_id)s: deleting", {'cgsnap_id': cgsnapshot.id}) # Pass context so that drivers that want to use it, can, # but it is not a requirement for all drivers. cgsnapshot.context = caller_context for snapshot in snapshots: snapshot.context = caller_context model_update, snapshots_model_update = ( self.driver.delete_cgsnapshot(context, cgsnapshot, snapshots)) if snapshots_model_update: for snap_model in snapshots_model_update: # NOTE(xyang): snapshots is a list of snapshot objects. # snapshots_model_update should be a list of dicts. snap = next((item for item in snapshots if item.id == snap_model['id']), None) if snap: snap.status = snap_model['status'] snap.save() if (snap_model['status'] in [fields.SnapshotStatus.ERROR_DELETING, fields.SnapshotStatus.ERROR] and model_update['status'] not in ['error_deleting', 'error']): model_update['status'] = snap_model['status'] if model_update: if model_update['status'] in ['error_deleting', 'error']: msg = (_('Error occurred when deleting cgsnapshot ' '%s.') % cgsnapshot.id) LOG.error(msg) raise exception.VolumeDriverException(message=msg) else: cgsnapshot.update(model_update) cgsnapshot.save() except exception.CinderException: with excutils.save_and_reraise_exception(): cgsnapshot.status = 'error' cgsnapshot.save() # Update snapshot status to 'error' if driver returns # None for snapshots_model_update. if not snapshots_model_update: for snapshot in snapshots: snapshot.status = fields.SnapshotStatus.ERROR snapshot.save() for snapshot in snapshots: # Get reservations try: if CONF.no_snapshot_gb_quota: reserve_opts = {'snapshots': -1} else: reserve_opts = { 'snapshots': -1, 'gigabytes': -snapshot['volume_size'], } volume_ref = self.db.volume_get(context, snapshot['volume_id']) QUOTAS.add_volume_type_opts(context, reserve_opts, volume_ref.get('volume_type_id')) reservations = QUOTAS.reserve(context, project_id=project_id, **reserve_opts) except Exception: reservations = None LOG.exception(_LE("Failed to update usages deleting snapshot")) self.db.volume_glance_metadata_delete_by_snapshot(context, snapshot['id']) # TODO(thangp): Switch over to use snapshot.destroy() # after cgsnapshot-objects bugs are fixed self.db.snapshot_destroy(context, snapshot['id']) # Commit the reservations if reservations: QUOTAS.commit(context, reservations, project_id=project_id) cgsnapshot.destroy() LOG.info(_LI("cgsnapshot %s: deleted successfully"), cgsnapshot.id) self._notify_about_cgsnapshot_usage(context, cgsnapshot, "delete.end", snapshots) def update_migrated_volume(self, ctxt, volume, new_volume, volume_status): """Finalize migration process on backend device.""" model_update = None model_update_default = {'_name_id': new_volume.name_id, 'provider_location': new_volume.provider_location} try: model_update = self.driver.update_migrated_volume(ctxt, volume, new_volume, volume_status) except NotImplementedError: # If update_migrated_volume is not implemented for the driver, # _name_id and provider_location will be set with the values # from new_volume. model_update = model_update_default if model_update: model_update_default.update(model_update) # Swap keys that were changed in the source so we keep their values # in the temporary volume's DB record. # Need to convert 'metadata' and 'admin_metadata' since # they are not keys of volume, their corresponding keys are # 'volume_metadata' and 'volume_admin_metadata'. model_update_new = dict() for key in model_update: if key == 'metadata': if volume.get('volume_metadata'): model_update_new[key] = { metadata['key']: metadata['value'] for metadata in volume.volume_metadata} elif key == 'admin_metadata': model_update_new[key] = { metadata['key']: metadata['value'] for metadata in volume.volume_admin_metadata} else: model_update_new[key] = volume[key] with new_volume.obj_as_admin(): new_volume.update(model_update_new) new_volume.save() with volume.obj_as_admin(): volume.update(model_update_default) volume.save() # Replication V2.1 methods def failover_host(self, context, secondary_backend_id=None): """Failover a backend to a secondary replication target. Instructs a replication capable/configured backend to failover to one of it's secondary replication targets. host=None is an acceptable input, and leaves it to the driver to failover to the only configured target, or to choose a target on it's own. All of the hosts volumes will be passed on to the driver in order for it to determine the replicated volumes on the host, if needed. :param context: security context :param secondary_backend_id: Specifies backend_id to fail over to """ svc_host = vol_utils.extract_host(self.host, 'backend') service = objects.Service.get_by_args( context, svc_host, 'cinder-volume') volumes = objects.VolumeList.get_all_by_host(context, self.host) exception_encountered = False try: # expected form of volume_update_list: # [{volume_id: <cinder-volid>, updates: {'provider_id': xxxx....}}, # {volume_id: <cinder-volid>, updates: {'provider_id': xxxx....}}] (active_backend_id, volume_update_list) = ( self.driver.failover_host( context, volumes, secondary_id=secondary_backend_id)) except exception.UnableToFailOver: LOG.exception(_LE("Failed to perform replication failover")) service.replication_status = ( fields.ReplicationStatus.FAILOVER_ERROR) service.save() exception_encountered = True except exception.InvalidReplicationTarget: LOG.exception(_LE("Invalid replication target specified " "for failover")) # Preserve the replication_status if secondary_backend_id == "default": service.replication_status = ( fields.ReplicationStatus.FAILED_OVER) else: service.replication_status = fields.ReplicationStatus.ENABLED service.save() exception_encountered = True except exception.VolumeDriverException: # NOTE(jdg): Drivers need to be aware if they fail during # a failover sequence, we're expecting them to cleanup # and make sure the driver state is such that the original # backend is still set as primary as per driver memory LOG.error(_LE("Driver reported error during " "replication failover.")) service.status = 'error' service.save() exception_encountered = True if exception_encountered: LOG.error( _LE("Error encountered during failover on host: " "%(host)s invalid target ID %(backend_id)s"), {'host': self.host, 'backend_id': secondary_backend_id}) return if secondary_backend_id == "default": service.replication_status = fields.ReplicationStatus.ENABLED service.active_backend_id = "" if service.frozen: service.disabled = True service.disabled_reason = "frozen" else: service.disabled = False service.disabled_reason = "" service.save() else: service.replication_status = fields.ReplicationStatus.FAILED_OVER service.active_backend_id = active_backend_id service.disabled = True service.disabled_reason = "failed-over" service.save() for update in volume_update_list: # Response must include an id key: {volume_id: <cinder-uuid>} if not update.get('volume_id'): raise exception.UnableToFailOver( reason=_("Update list, doesn't include volume_id")) # Key things to consider (attaching failed-over volumes): # provider_location # provider_auth # provider_id # replication_status vobj = objects.Volume.get_by_id(context, update['volume_id']) vobj.update(update.get('updates', {})) vobj.save() LOG.info(_LI("Failed over to replication target successfully.")) def freeze_host(self, context): """Freeze management plane on this backend. Basically puts the control/management plane into a Read Only state. We should handle this in the scheduler, however this is provided to let the driver know in case it needs/wants to do something specific on the backend. :param context: security context """ # TODO(jdg): Return from driver? or catch? # Update status column in service entry try: self.driver.freeze_backend(context) except exception.VolumeDriverException: # NOTE(jdg): In the case of freeze, we don't really # need the backend's consent or anything, we'll just # disable the service, so we can just log this and # go about our business LOG.warning(_LW('Error encountered on Cinder backend during ' 'freeze operation, service is frozen, however ' 'notification to driver has failed.')) svc_host = vol_utils.extract_host(self.host, 'backend') service = objects.Service.get_by_args( context, svc_host, 'cinder-volume') service.disabled = True service.disabled_reason = "frozen" service.save() LOG.info(_LI("Set backend status to frozen successfully.")) return True def thaw_host(self, context): """UnFreeze management plane on this backend. Basically puts the control/management plane back into a normal state. We should handle this in the scheduler, however this is provided to let the driver know in case it needs/wants to do something specific on the backend. :param context: security context """ # TODO(jdg): Return from driver? or catch? # Update status column in service entry try: self.driver.thaw_backend(context) except exception.VolumeDriverException: # NOTE(jdg): Thaw actually matters, if this call # to the backend fails, we're stuck and can't re-enable LOG.error(_LE('Error encountered on Cinder backend during ' 'thaw operation, service will remain frozen.')) return False svc_host = vol_utils.extract_host(self.host, 'backend') service = objects.Service.get_by_args( context, svc_host, 'cinder-volume') service.disabled = False service.disabled_reason = "" service.save() LOG.info(_LI("Thawed backend successfully.")) return True def manage_existing_snapshot(self, ctxt, snapshot, ref=None): LOG.debug('manage_existing_snapshot: managing %s.', ref) try: flow_engine = manage_existing_snapshot.get_flow( ctxt, self.db, self.driver, self.host, snapshot.id, ref) except Exception: msg = _LE("Failed to create manage_existing flow: " "%(object_type)s %(object_id)s.") LOG.exception(msg, {'object_type': 'snapshot', 'object_id': snapshot.id}) raise exception.CinderException( _("Failed to create manage existing flow.")) with flow_utils.DynamicLogListener(flow_engine, logger=LOG): flow_engine.run() return snapshot.id def get_capabilities(self, context, discover): """Get capabilities of backend storage.""" if discover: self.driver.init_capabilities() capabilities = self.driver.capabilities LOG.debug("Obtained capabilities list: %s.", capabilities) return capabilities def get_backup_device(self, ctxt, backup): (backup_device, is_snapshot) = ( self.driver.get_backup_device(ctxt, backup)) secure_enabled = self.driver.secure_file_operations_enabled() backup_device_dict = {'backup_device': backup_device, 'secure_enabled': secure_enabled, 'is_snapshot': is_snapshot, } return backup_device_dict def secure_file_operations_enabled(self, ctxt, volume): secure_enabled = self.driver.secure_file_operations_enabled() return secure_enabled
cinder/volume/manager.py
153,649
Manages attachable block storage devices. Load the driver from the one specified in args, or from flags. Returns volume_stats updated as needed. Create a cloned volume and register its location to the image. Copy data from src_vol to dest_vol. Create a new image-volume and cache entry for it. This assumes that the image has already been downloaded and stored in the volume described by the volume_ref. Deletes an image stuck in queued or saving state. Determine if the Cinder volume DB is empty. A check of the volume DB is done to determine whether it is empty or not at this point. :param ctxt: our working context Updates db to show volume is attached. Uploads the specified volume to Glance. image_meta is a dictionary containing the following keys: 'id', 'container_format', 'disk_format' Creates the cgsnapshot. Creates the consistency group. Creates the consistency group from source. The source can be a CG snapshot or a source CG. Creates and exports the snapshot. Creates the volume. Deletes cgsnapshot. Deletes consistency group and the volumes in the group. Deletes and unexports snapshot. Deletes and unexports volume. 1. Delete a volume(normal case) Delete a volume and update quotas. 2. Delete a migration volume If deleting the volume in a migration, we want to skip quotas but we need database updates for the volume. Updates db to show volume is detached. Failover a backend to a secondary replication target. Instructs a replication capable/configured backend to failover to one of it's secondary replication targets. host=None is an acceptable input, and leaves it to the driver to failover to the only configured target, or to choose a target on it's own. All of the hosts volumes will be passed on to the driver in order for it to determine the replicated volumes on the host, if needed. :param context: security context :param secondary_backend_id: Specifies backend_id to fail over to Freeze management plane on this backend. Basically puts the control/management plane into a Read Only state. We should handle this in the scheduler, however this is provided to let the driver know in case it needs/wants to do something specific on the backend. :param context: security context Get capabilities of backend storage. Perform any required initialization. Prepare volume for connection from host represented by connector. This method calls the driver initialize_connection and returns it to the caller. The connector parameter is a dictionary with information about the host that will connect to the volume in the following format:: { 'ip': ip, 'initiator': initiator, } ip: the ip address of the connecting machine initiator: the iscsi initiator name of the connecting machine. This can be None if the connecting machine does not support iscsi connections. driver is responsible for doing any necessary security setup and returning a connection_info dictionary in the following format:: { 'driver_volume_type': driver_volume_type, 'data': data, } driver_volume_type: a string to identify the type of volume. This can be used by the calling code to determine the strategy for connecting to the volume. This could be 'iscsi', 'rbd', 'sheepdog', etc. data: this is the data that the calling code will use to connect to the volume. Keep in mind that this will be serialized to json in various places, so it should not contain any non-json data types. Return if Manager is ready to accept requests. This is to inform Service class that in case of volume driver initialization failure the manager is actually down and not ready to accept any requests. Lock decorator for volume detach operations. Takes a named lock prior to executing the detach call. The lock is named with the operation executed and the id of the volume. This lock can then be used by other operations to avoid operation conflicts on shared volumes. This locking mechanism is only for detach calls. We can't use the locked_volume_operation, because detach requires an additional attachment_id in the parameter list. Lock decorator for snapshot operations. Takes a named lock prior to executing the operation. The lock is named with the operation executed and the id of the snapshot. This lock can then be used by other operations to avoid operation conflicts on shared snapshots. Example use: If a snapshot operation uses this decorator, it will block until the named lock is free. This is used to protect concurrent operations on the same snapshot e.g. delete SnapA while create volume VolA from SnapA is in progress. Lock decorator for volume operations. Takes a named lock prior to executing the operation. The lock is named with the operation executed and the id of the volume. This lock can then be used by other operations to avoid operation conflicts on shared volumes. Example use: If a volume operation uses this decorator, it will block until the named lock is free. This is used to protect concurrent operations on the same volume e.g. delete VolA while create volume VolB from VolA is in progress. Migrate the volume to the specified host (called on source host). Promote volume replica secondary to be the primary volume. Collect driver status and then publish. Re-enable replication of secondary volume with primary volumes. Removes an export for a volume. Cleanup connection from host represented by connector. The format of connector is the same as for initialize_connection. UnFreeze management plane on this backend. Basically puts the control/management plane back into a normal state. We should handle this in the scheduler, however this is provided to let the driver know in case it needs/wants to do something specific on the backend. :param context: security context Updates consistency group. Update consistency group by adding volumes to the group, or removing volumes from the group. Finalize migration process on backend device. Volume manager manages creating, attaching, detaching, and persistent storage. Persistent storage volumes keep their state independent of instances. You can attach to an instance, terminate the instance, spawn a new instance (even one from a different image) and re-attach the volume with the same data intact. **Related Flags** :volume_topic: What :mod:`rpc` topic to listen to (default: `cinder-volume`). :volume_manager: The module name of a class derived from :class:`manager.Manager` (default: :class:`cinder.volume.manager.Manager`). :volume_driver: Used by :class:`Manager`. Defaults to :class:`cinder.volume.drivers.lvm.LVMVolumeDriver`. :volume_group: Name of the group that will contain exported volumes (default: `cinder-volumes`) :num_shell_tries: Number of times to attempt to run commands (default: 3) Copyright 2010 United States Government as represented by the Administrator of the National Aeronautics and Space Administration. 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. On cloning a volume, we shouldn't copy volume_type, consistencygroup and volume_attachment, because the db sets that according to [field]_id, which we do copy. We also skip some other values that are set during creation of Volume object. update_service_capabilities needs service_name to be volume Get from configuration, which will get the default if its not using the multi backend We pass the current setting for service.active_backend_id to the driver on init, incase there was a restart or something NOTE(jdg): This is to solve problems with unit tests No pool name encoded in host, so this is a legacy volume created before pool is introduced, ask driver to provide pool info if it has such knowledge and update the DB. Otherwise, put them into a special fixed pool with volume_backend_name being the pool name, if volume_backend_name is None, use default pool name. This is only for counting purpose, doesn't update DB. First volume in the pool NOTE(jdg): For now this just updates provider_id, we can add more add more items to the update if theyr'e releveant but we need to be safe in what we allow and add a list of allowed keys things that make sense are provider_*, replication_status etc NOTE(JDG): Make sure returned item is in this hosts volumes NOTE(jdg): snapshots are slighty harder, because we do not have a host column and of course no get all by host, so we use a get_all and bounce our response off of it NOTE(jdg): For now we only update those that have no entry we don't want to continue since we failed to initialize the driver correctly. Initialize backend capabilities list FIXME volume count for exporting is wrong available volume should also be counted into allocated calculate allocated capacity for driver Set volume status to available or in-use. at this point the driver is considered initialized. NOTE(jdg): Careful though because that doesn't mean that an entry exists in the service table Offload all the pending volume delete operations to the threadpool to prevent the main volume service thread from being blocked. By default, delete volumes sequentially collect and publish service capabilities FIXME(dulek): Remove this in v3.0 of RPC API. For older clients, mimic the old behavior and look up the volume by its volume_id. NOTE(flaper87): Driver initialization is verified by the task itself. Make sure the snapshot is not deleted until we are done with it. Make sure the volume is not deleted until we are done with it. Make sure the volume is not deleted until we are done with it. This code executes create volume flow. If something goes wrong, flow reverts all job that was done and reraises an exception. Otherwise, all data that was generated by flow becomes available in flow engine's storage. NOTE(dulek): Flag to indicate if volume was rescheduled. Used to decide if allocated_capacity should be incremented. If there's no vol_ref, then flow is reverted. Lets check out if rescheduling occurred. Flow was reverted and not rescheduled, fetching volume_ref from the DB, because it will be needed. NOTE(dulek): Volume wasn't rescheduled so we need to update volume stats as these are decremented on delete. FIXME(dulek): Remove this in v3.0 of RPC API. NOTE(thingee): It could be possible for a volume to be deleted when resuming deletes from init_host(). Volume is still attached, need to detach first This could be done, but is ruled out for now just for simplicity. The status 'deleting' is not included, because it only applies to the source volume to be deleted after a migration. No quota needs to be handled for it. NOTE(flaper87): Verify the driver is enabled before going forward. The exception will be caught and the volume status updated. If this is a destination volume, we have to clear the database record to avoid user confusion. If this is a destination volume, we have to clear the database record to avoid user confusion. If deleting source/destination volume in a migration, we should skip quotas. Get reservations Delete glance metadata if it exists If deleting source/destination volume in a migration, we should skip quotas. Commit the reservations Legacy volume, put them into default pool This method is called when driver.unmanage() or driver.delete_volume() fails in delete_volume(), so it is already in the exception handling part. NOTE(flaper87): Verify the driver is enabled before going forward. The exception will be caught and the snapshot status updated. Pass context so that drivers that want to use it, can, but it is not a requirement for all drivers. If volume is not created from image, No glance metadata would be available for that volume in volume glance metadata table NOTE(flaper87): Verify the driver is enabled before going forward. The exception will be caught and the snapshot status updated. Pass context so that drivers that want to use it, can, but it is not a requirement for all drivers. Get reservations Commit the reservations check the volume status before attaching NOTE(flaper87): Verify the driver is enabled before going forward. The exception will be caught and the volume status updated. TODO(vish): refactor this into a more general "unreserve" We need to make sure the volume status is set to the correct status. It could be in detaching status now, and we don't want to leave it there. We can try and degrade gracefully here by trying to detach a volume without the attachment_id here if the volume only has one attachment. This is for backwards compatibility. There are more than 1 attachments for this volume we have to have an attachment id. there aren't any attachments for this volume. so set the status to available and move on. NOTE(flaper87): Verify the driver is enabled before going forward. The exception will be caught and the volume status updated. NOTE(jdg): We used to do an ensure export here to catch upgrades while volumes were attached (E->F) this was necessary to convert in-use volumes from int ID's to UUID's. Don't need this any longer We're going to remove the export here (delete the iscsi target) NOTE(flaper87): Verify the driver is enabled before going forward. The exception will be caught and the volume status updated. Deletes the image if it is in queued or saving state NOTE(flaper87): Verify the driver is enabled before going forward. The exception will be caught and the volume status updated. Add qos_specs to connection info only pass qos_specs that is designated to be consumed by front-end, or both front-end and back-end. Add access_mode to connection info NOTE(zhiyan): client didn't call 'os-attach' before Add encrypted flag to connection_info if not set in the driver. Add discard flag to connection_info if not set in the driver and configured to be reported. NOTE(flaper87): Verify the driver is enabled before going forward. The exception will be caught and the volume status updated. NOTE(flaper87): Verify the driver is enabled before going forward. The exception will be caught and the volume status updated. NOTE(jdg): need elevated context as we haven't "given" the vol yet NOTE(jdg): Some drivers tie provider info (CHAP) to tenant for those that do allow them to return updated model info Check the backend capabilities of migration destination host. vol size is in GB Create new volume on remote host Wait for new_volume to become ready TODO(thangp): Replace get_by_id with refresh when it is available TODO(thangp): Replace get_by_id with refresh when it is available Copy the source volume to the destination volume Pre- and post-copy driver-specific actions The above call is synchronous so we complete the migration This is an async call to Nova, which will call the completion when it's done If we're in the migrating phase, we need to cleanup destination volume because source volume is remaining The temporary volume is not created, only DB data is created The temporary volume is already created If we're in the completing phase don't delete the destination because we may have already deleted the source! But the migration_status in database should be cleared to handle volume after migration failure FIXME(dulek): Remove this in v3.0 of RPC API. For older clients, mimic the old behavior and look up the volume by its volume_id. NOTE(flaper87): Verify the driver is enabled before going forward. The exception will be caught and the migration status updated. Detach the source volume (if it fails, don't fail the migration) Give driver (new_volume) a chance to update things as needed after a successful migration. Note this needs to go through rpc to the host of the new volume the current host and driver object is for the "existing" volume. Swap src and dest DB records so we can continue using the src id and asynchronously delete the destination id Asynchronous deletion of the source volume in the back-end (now pointed by the target volume id) FIXME(dulek): Remove this in v3.0 of RPC API. For older clients, mimic the old behavior and look up the volume by its volume_id. NOTE(flaper87): Verify the driver is enabled before going forward. The exception will be caught and the migration status updated. Append volume stats with 'allocated_capacity_gb' Append filter and goodness function if needed queue it to be sent to the Schedulers. Pool not found in volume manager Append filter_function if needed Append goodness_function if needed FIXME(dulek): Remove this in v3.0 of RPC API. For older clients, mimic the old behavior and look up the volume by its volume_id. NOTE(flaper87): Verify the driver is enabled before going forward. The exception will be caught and the volume status updated. Legacy volume, put them into default pool FIXME(dulek): Remove this in v3.0 of RPC API. For older clients, mimic the old behavior and look up the volume by its volume_id. NOTE(flaper87): Verify the driver is enabled before going forward. The exception will be caught and the volume status updated. NOTE(flaper87): Other exceptions in this method don't set the volume status to error. Should that be done here? Setting the volume back to it's original status for now. If old_reservations has been passed in from the API, we should skip quotas. TODO(ntpttr): These reservation checks are left in to be backwards compatible with Liberty and can be removed in N. Get old reservations NOTE(wanghao): We don't need to reserve volumes and gigabytes quota for retyping operation since they didn't changed, just reserve volume_type and type gigabytes is fine. We already got the new reservations If volume types have the same contents, no need to do anything Call driver to try and change the type NOTE(jdg): Check to see if the destination host is the same as the current. If it's not don't call the driver.retype method, otherwise drivers that implement retype may report success, but it's invalid in the case of a migrate. We assume that those that support pools do this internally so we strip off the pools designation Check if the driver retype provided a model update or just a retype indication We could not change the type, so we need to migrate the volume, where the destination volume will be of the new type Don't allow volume with replicas to be migrated Fetch created volume from storage Update volume stats Legacy volume, put them into default pool Only want volumes that do not have a 'disabled' replication status Check if cgsnapshot still exists Sort source snapshots so that they are in the same order as their corresponding target volumes. Sort source volumes so that they are in the same order as their corresponding target volumes. Update volume status to 'error' as well. Sort source snapshots so that they are in the same order as their corresponding target volumes. Each source snapshot in the snapshots list should have a corresponding target volume in the volumes list. Sort source volumes so that they are in the same order as their corresponding target volumes. Each source volume in the source_vols list should have a corresponding target volume in the volumes list. Update allocated capacity in volume stats Legacy volume, put them into default pool Volume is still attached, need to detach first self.host is 'host@backend' volume_ref['host'] is 'host@backendpool' Extract host before doing comparison If we failed to delete a volume, make sure the status for the cg is set to error as well Update volume status to 'error' if driver returns None for volumes_model_update. Get reservations for group Get reservations for volume Delete glance metadata if it exists Commit the reservations self.host is 'host@backend' volume_ref['host'] is 'host@backendpool' Extract host before doing comparison Pass context so that drivers that want to use it, can, but it is not a requirement for all drivers. Update db for snapshot. NOTE(xyang): snapshots is a list of snapshot objects. snapshots_model_update should be a list of dicts. Update snapshot status to 'error' if driver returns None for snapshots_model_update. TODO(thangp): Switch over to use snapshot.update() after cgsnapshot-objects bugs are fixed Pass context so that drivers that want to use it, can, but it is not a requirement for all drivers. NOTE(xyang): snapshots is a list of snapshot objects. snapshots_model_update should be a list of dicts. Update snapshot status to 'error' if driver returns None for snapshots_model_update. Get reservations TODO(thangp): Switch over to use snapshot.destroy() after cgsnapshot-objects bugs are fixed Commit the reservations If update_migrated_volume is not implemented for the driver, _name_id and provider_location will be set with the values from new_volume. Swap keys that were changed in the source so we keep their values in the temporary volume's DB record. Need to convert 'metadata' and 'admin_metadata' since they are not keys of volume, their corresponding keys are 'volume_metadata' and 'volume_admin_metadata'. Replication V2.1 methods expected form of volume_update_list: [{volume_id: <cinder-volid>, updates: {'provider_id': xxxx....}}, {volume_id: <cinder-volid>, updates: {'provider_id': xxxx....}}] Preserve the replication_status NOTE(jdg): Drivers need to be aware if they fail during a failover sequence, we're expecting them to cleanup and make sure the driver state is such that the original backend is still set as primary as per driver memory Response must include an id key: {volume_id: <cinder-uuid>} Key things to consider (attaching failed-over volumes): provider_location provider_auth provider_id replication_status TODO(jdg): Return from driver? or catch? Update status column in service entry NOTE(jdg): In the case of freeze, we don't really need the backend's consent or anything, we'll just disable the service, so we can just log this and go about our business TODO(jdg): Return from driver? or catch? Update status column in service entry NOTE(jdg): Thaw actually matters, if this call to the backend fails, we're stuck and can't re-enable
22,507
en
0.89724
from __future__ import absolute_import, division, print_function from six.moves import range from psana import * import sys import numpy as np from xfel.amo.pnccd_ana import pnccd_tbx from xfel.amo.pnccd_ana import pnccd_hit from xfel.amo.pnccd_ana import fxs import matplotlib.pyplot as plt from six.moves import zip plt.ion() ######################################## # Due to the mask sometimes having zero values # we're bound to get divisions with zeros at #times. Here ignoring those errors. np.seterr(divide='ignore', invalid='ignore') from mpi4py import MPI comm = MPI.COMM_WORLD rank = comm.Get_rank() size = comm.Get_size() def h5gen(run,timestamps = None, first = None, last = None): # Singel CPU if size == 1: nom = rank denom = size # MPI else: nom = rank - 1 denom = size - 1 times = timestamps nevents = len(times) mytimes,myevents = zip(*[(times[i],i) for i in range(nevents) if (i+nom)%denom == 0]) for j in range(len(mytimes)): yield myevents[j],mytimes[j] def idxgen(run,timestamps = None, first = None, last = None): #print "idx mode" # Use timestamps from index file if timestamps is None: timestamps = run.times() if first is None : first = 0 if last is None : last = len(timestamps) else: last = min(last,len(timestamps)) # Check that last time-stamp exists # Singel CPU if size == 1: nom = rank denom = size # MPI else: nom = rank - 1 denom = size - 1 times = timestamps[first:last] nevents = len(times) mytimes,myevents = zip(*[(times[i],i) for i in range(nevents) if (i+nom)%denom == 0]) for j in range(len(mytimes)): yield myevents[j],run.event(mytimes[j]) def smdgen(run,timestamps = None, first = None, last = None): #print "smd mode" if first is None : first = 0 if last is None : last = 1e20 # We typically don't know what the last events is. So for now use a large number # Singel CPU if size == 1: nom = rank denom = size # MPI else: nom = rank - 1 denom = size - 1 if timestamps is None : for nevent,evt in enumerate(run.events()): if nevent < first : continue elif nevent == last : return elif nevent%denom == nom: yield nevent-first,evt else : # Only applicable for xtc format ct = 0 for nevent,evt in enumerate(run.events()): t = pnccd_tbx.get_psana_time(evt) # Check if event exists in timestamps if np.equal(t, timestamps).all(axis=1).any() : if ct < first : continue elif ct == last : return elif ct%denom == nom: yield ct,evt ct += 1 def compute_mask(argv=None) : """Function to compute a mask of non-resposive pixels from FXS images extracted from xtc (smd,idx,xtc format) or h5 files. Works for Single CPU, Multi-Processor interactive jobs and MPI batch jobs For a definition of input arguments argv and batch processing instructions see *** mpi_fxs_launch.py *** compute_mask produces the following output files: * Index file : Information about the events processed including time-stamps, beam center, total and peak intensities, streak locations, particle size etc * Average : Average image in cartesian coordinates * Variance : Variance map of intensities in cartesian coordinates * Mask : Mask image in cartesian coordinates """ if argv == None: argv = sys.argv[1:] try: from mpi4py import MPI except ImportError: raise Sorry("MPI not found") comm = MPI.COMM_WORLD rank = comm.Get_rank() size = comm.Get_size() if argv.hit is None : hit = -1.0e20 # Process everything else: hit = argv.hit # Process everything > hit ftype = argv.ftype if argv.param_path is not None : if ftype == 'h5' : param_file = np.genfromtxt(argv.param_path,skiprows=1,dtype=None) timestamps,filestamps = pnccd_tbx.get_h5_event(param_file) elif ftype == 'xtc' : param_file = np.genfromtxt(argv.param_path,skiprows=1,dtype=None) timestamps = pnccd_tbx.get_time(param_file) else : param_file = np.genfromtxt(argv.param_path,skiprows=1) timestamps = pnccd_tbx.get_psana_event(param_file) else: timestamps = None # The first and last events to processed first = argv.first last = argv.last # Check data format if ftype == 'h5' : import h5py run = int(argv.run) # Get time-stamps from all h5-files if argv.param_path is None : timestamps = [] filestamps = [] # Loop over all h5-files and store the time-stamps for i in os.listdir(argv.xtc_dir): if i.endswith(".h5"): f = h5py.File(i,'r') filestamps.append(i[-7:-4]) timestamps.append(list(f.keys())) continue else: continue dataset_name = "%s-r%s"%(argv.experiment, str(argv.run).zfill(4)) # Ascert 4 digit run number exprun = os.path.join(argv.xtc_dir,dataset_name) if argv.first is None : first = 0 if argv.last is None : last = len(timestamps) else: last = min(last,len(timestamps)) # Check that last time-stamp exists timestamps = timestamps[first:last] filestamps = filestamps[first:last] evtgen = h5gen else : exprun = "exp=%s:run=%d"%(argv.experiment, argv.run) if (ftype == 'xtc') : dataset_name = exprun+':xtc' elif (ftype == 'idx') : dataset_name = exprun+':idx' elif(ftype == 'idx_ffb') : dataset_name = exprun+':idx' # as ffb is only at SLAC, ok to hardcode /reg/d here dataset_name += ":dir=/reg/d/ffb/%s/%s/xtc"%(argv.experiment[0:3],argv.experiment) elif(ftype == 'smd') : dataset_name = exprun+':smd' elif(ftype == 'smd_ffb') : dataset_name = exprun+':smd' # as ffb is only at SLAC, ok to hardcode /reg/d here ADD live! dataset_name += ":dir=/reg/d/ffb/%s/%s/xtc:live"%(argv.experiment[0:3],argv.experiment) exprun = dataset_name ds = DataSource(dataset_name) run = next(ds.runs()) # Select event generator if (ftype=='smd') or (ftype == 'smd_ffb') or (ftype == 'xtc'): evtgen = smdgen elif (ftype=='idx') or (ftype == 'idx_ffb'): evtgen = idxgen if size == 1: plot = argv.plot else: plot = 0 FXS = fxs.fluctuation_scattering(dataset_name = exprun, detector_address = argv.address, data_type = argv.ftype, mask_path = argv.mask_path, mask_angles = None, #np.array([88, 270]), # static masking at 88 and 270 deg mask_widths = None, #np.array([6, 10]), # +/- degree backimg_path = argv.bg_img_path, backmsk_path = argv.bg_msk_path, geom_path = argv.geom_path, det_dist = argv.det_distance, det_pix = argv.det_pixel, beam_l = argv.lambda_b, mask_thr = argv.thr, nQ = argv.nQ, nPhi = argv.nPhi, dQ = argv.dQ, dPhi = argv.dP, cent0 = [argv.x,argv.y], r_max = argv.r_max, dr = argv.dr, dx = argv.dx, dy = argv.dy, r_0 = argv.r0, q_bound = argv.q_bound, peak = [0.037, 0.064], dpeak = [0.002, 0.002]) # Initialize iterator FXS.cnt = np.array([0.]) # Initialize Index variables if argv.param_path is None : maxevents = 400000 # We don't always know the total nr of events. Therefore set to large value else: maxevents = min(len(timestamps),len(timestamps[first:last])) FXS.get_index(maxevents, flag = 1) # chop the list into pieces, depending on rank. This assigns each process # events such that the get every Nth event where N is the number of processes if size > 1 : if rank > 0 : hd=pnccd_hit.hit() # MPI process. Here we set rank 0 to work as a listening server only. for j,evt in evtgen(run,timestamps = timestamps, first = first, last = last): #print '***',rank,j,evt.get(EventId).fiducials() if j%10==0: print('Rank',rank,'processing event',j) if ftype == 'h5' : FXS.get_h5(filestamps[j],evt) else : FXS.get_image(evt) # Geometry applied image (FXS.img) FXS.image = np.copy(FXS.img) # Process hits if (FXS.image is not None) and (float(FXS.image.sum()) > hit) : FXS.get_beam(plot = plot) # Beam center refinement FXS.get_polar(plot = plot) # Polar transform FXS.get_streak_mask(plot = plot) # Get info on streak FXS.store_image(j) # Store raw images if ftype == 'h5' : FXS.store_index_h5(evt, j, flag = 0) else: ###################################### # Ugly way to get the time-stamps. Fix!! time = evt.get(EventId).time() fid = evt.get(EventId).fiducials() sec = time[0] nsec = time[1] et = EventTime(int((sec<<32)|nsec),fid) ####################################### FXS.store_index(et, j, flag = 0) # Store index if int(FXS.cnt)%10==0: print('Rank',rank,'processed events: ', int(FXS.cnt)) # Send partial results to master (rank 0) if int(FXS.cnt) % 50 == 0: # Send every 50 events # C2 and Saxs data tmp_n = int(FXS.cnt) # Total intensity, Size and Score tmp_ind = np.column_stack((FXS.tot_int,FXS.tot_size,FXS.tot_score)) hd.send(tmp_n,ind=tmp_ind) hd.endrun() print('Rank',rank,'total events: ', int(FXS.cnt),' * ') else: if ftype == 'h5' : FXS.run_nr = run else: FXS.run_nr = int(run.run()) hd = pnccd_hit.hit() idim = (maxevents,3) hd.total_ind = [np.zeros(idim)]*(size-1) hd.total_ev_i = [0.0]*(size-1) nClients = size - 1 while nClients > 0: # Remove client if the run ended if hd.recv(): nClients -= 1 else: ns = sum(hd.total_ev_s) ni = sum(hd.total_ev_i) if (ns % 100 == 0) : # Publish every 100 events IND = np.zeros(idim) for i in range(size-1) : IND = IND + hd.total_ind[i] FXS.publish(ind=IND, n_i=ni) else : # Single CPU for j,evt in evtgen(run,timestamps = timestamps, first = first, last = last): #print '***',rank,j,evt.get(EventId).fiducials() if j%10==0: print('Rank',rank,'processing event',j) if ftype == 'h5' : FXS.get_h5(filestamps[j],evt) else : FXS.get_image(evt) # Geometry applied image (FXS.img) FXS.image = np.copy(FXS.img) # Process hits if (FXS.image is not None)and (float(FXS.image.sum()) > hit) : FXS.get_beam(plot=plot) # Beam center refinement FXS.get_polar() # Polar transform FXS.get_streak_mask(plot=0) # Get info on streak FXS.store_image(j) # Store raw images if ftype == 'h5' : FXS.store_index_h5(evt, j, flag = 0) else: ###################################### # Ugly way to get the time-stamps. Fix!! time = evt.get(EventId).time() fid = evt.get(EventId).fiducials() sec = time[0] nsec = time[1] et = EventTime(int((sec<<32)|nsec),fid) ####################################### FXS.store_index(et, j, flag = 0) # Store index print('Rank',rank,'total events: ', int(FXS.cnt),' * ') #sum the images across mpi cores if size > 1: print("Synchronizing rank", rank) Tot = np.zeros(FXS.cnt.shape) comm.Reduce(FXS.cnt,Tot) if rank == 0 and Tot[0] == 0: raise Sorry("No events found in the run") # Collect Variables Images = np.zeros(FXS.images.shape) comm.Reduce(FXS.images,Images) # Collect Indexing variables Tot_t = np.zeros(FXS.tot_t.shape) comm.Reduce(FXS.tot_t,Tot_t) Tot_s = np.zeros(FXS.tot_s.shape) comm.Reduce(FXS.tot_s,Tot_s) Tot_ns = np.zeros(FXS.tot_ns.shape) comm.Reduce(FXS.tot_ns,Tot_ns) Tot_fd = np.zeros(FXS.tot_fd.shape) comm.Reduce(FXS.tot_fd,Tot_fd) Tot_int = np.zeros(FXS.tot_int.shape) comm.Reduce(FXS.tot_int,Tot_int) Tot_peak1 = np.zeros(FXS.tot_peak1_int.shape) comm.Reduce(FXS.tot_peak1_int,Tot_peak1) Tot_peak2 = np.zeros(FXS.tot_peak2_int.shape) comm.Reduce(FXS.tot_peak2_int,Tot_peak2) Tot_s_m = np.zeros(FXS.tot_streak_m.shape) comm.Reduce(FXS.tot_streak_m,Tot_s_m) Tot_s_s = np.zeros(FXS.tot_streak_s.shape) comm.Reduce(FXS.tot_streak_s,Tot_s_s) Tot_cx = np.zeros(FXS.tot_cx.shape) comm.Reduce(FXS.tot_cx,Tot_cx) Tot_cy = np.zeros(FXS.tot_cy.shape) comm.Reduce(FXS.tot_cy,Tot_cy) Tot_size = np.zeros(FXS.tot_size.shape) comm.Reduce(FXS.tot_size,Tot_size) Tot_score = np.zeros(FXS.tot_score.shape) comm.Reduce(FXS.tot_score,Tot_score) # Reduce results if rank==0: if size > 1: print("Synchronized") # Identify dead lines and pixels, get binary pixel mask Ave,Var,Mask = pnccd_tbx.pixel_mask(Images, thr = 0.12) # Write out data if argv.outputdir is None: opath = os.getcwd() else: opath = argv.outputdir f_index = os.path.join(opath,'Index_run' + str(argv.run) + '.dat') stamps = ['Time','Seconds','Nanoseconds','Fiducial','Total Intensity','Peak1, q='+str(FXS.peak[0])+'+/-'+str(FXS.dpeak[0]),'Peak2, q='+str(FXS.peak[1])+'+/-'+str(FXS.dpeak[1]),'Mean streak angle','Std streak angle','Beam X','Beam Y','Radius [Ang]','Score'] head =" ".join(stamps) f_ave = os.path.join(opath,'Average_map_' + str(argv.run) + '.dat') f_var = os.path.join(opath,'Variance_map_' + str(argv.run) + '.dat') f_mask = os.path.join(opath,'Mask_map_' + str(argv.run) + '.dat') # Get rid of zero lines at the end # Last non-zero intensity nz = np.nonzero(Tot_t) fend = nz[0][-1]+1 f = open(f_index,'w') np.savetxt(f,np.c_[Tot_t[:fend],Tot_s[:fend],Tot_ns[:fend],Tot_fd[:fend],Tot_int[:fend],Tot_peak1[:fend],Tot_peak2[:fend],Tot_s_m[:fend],Tot_s_s[:fend],Tot_cx[:fend],Tot_cy[:fend],Tot_size[:fend],Tot_score[:fend]],header = head, comments='' ) f.close() f = open(f_ave,'w') np.savetxt(f,Ave) f.close() f = open(f_var,'w') np.savetxt(f,Var) f.close() f = open(f_mask,'w') np.savetxt(f,Mask) f.close()
modules/cctbx_project/xfel/amo/pnccd_ana/mpi_fxs_mask.py
17,593
Function to compute a mask of non-resposive pixels from FXS images extracted from xtc (smd,idx,xtc format) or h5 files. Works for Single CPU, Multi-Processor interactive jobs and MPI batch jobs For a definition of input arguments argv and batch processing instructions see *** mpi_fxs_launch.py *** compute_mask produces the following output files: * Index file : Information about the events processed including time-stamps, beam center, total and peak intensities, streak locations, particle size etc * Average : Average image in cartesian coordinates * Variance : Variance map of intensities in cartesian coordinates * Mask : Mask image in cartesian coordinates Due to the mask sometimes having zero values we're bound to get divisions with zeros attimes. Here ignoring those errors. Singel CPU MPIprint "idx mode" Use timestamps from index file Check that last time-stamp exists Singel CPU MPIprint "smd mode" We typically don't know what the last events is. So for now use a large number Singel CPU MPI Only applicable for xtc format Check if event exists in timestamps Process everything Process everything > hit The first and last events to processed Check data format Get time-stamps from all h5-files Loop over all h5-files and store the time-stamps Ascert 4 digit run number Check that last time-stamp exists as ffb is only at SLAC, ok to hardcode /reg/d here as ffb is only at SLAC, ok to hardcode /reg/d here ADD live! Select event generatornp.array([88, 270]), static masking at 88 and 270 degnp.array([6, 10]), +/- degree Initialize iterator Initialize Index variables We don't always know the total nr of events. Therefore set to large value chop the list into pieces, depending on rank. This assigns each process events such that the get every Nth event where N is the number of processes MPI process. Here we set rank 0 to work as a listening server only.print '***',rank,j,evt.get(EventId).fiducials() Geometry applied image (FXS.img) Process hits Beam center refinement Polar transform Get info on streak Store raw images Ugly way to get the time-stamps. Fix!! Store index Send partial results to master (rank 0) Send every 50 events C2 and Saxs data Total intensity, Size and Score Remove client if the run ended Publish every 100 events Single CPUprint '***',rank,j,evt.get(EventId).fiducials() Geometry applied image (FXS.img) Process hits Beam center refinement Polar transform Get info on streak Store raw images Ugly way to get the time-stamps. Fix!! Store indexsum the images across mpi cores Collect Variables Collect Indexing variables Reduce results Identify dead lines and pixels, get binary pixel mask Write out data Get rid of zero lines at the end Last non-zero intensity
2,735
en
0.770759
""" Provides `to_ltx` to convert numpy arrays to LaTeX. """ import numpy as np from numpyarray_to_latex.utils import ( math_form, ) def to_ltx(a, fmt='{:6.4f}', latexarraytype='array', imstring='i', is_row_vector=True, mathform=True, brackets='()', mark_elements=[], mark_color='pink', separate_columns=[], separate_rows=[], ): r""" Return a LaTeX array given a numpy array. Parameters ---------- a : numpy.ndarray fmt : str, default = '{:6.2f}' python 3 formatter, optional- https://mkaz.tech/python-string-format.html latexarraytype : str, default = 'array' Any of .. code:: python "array" "pmatrix" "bmatrix" "vmatrix" "Vmatrix" "Bmatrix" if "array", you can specifiy the brackets with the keyword ``brackets``. imstring : str, default = 'i' Character to use to represent the imaginary unit. Usually ``'i'`` or ``'j'`` is_row_vector : bool, default = True If the array is 1D, should the output be a row (True) or column (False) vector? mathform : bool, default = True wether to convert strings like ``1e+05`` to ``1\times10^{5}``. brackets : iterable, default = '()' which brackets to use to wrap the matrix (must be two elements long). Use ``brackets = None`` if you don't want any brackets around the array. mark_elements : list, default = [] list of tuples containing element indices that should be marked with a colorbox. mark_color : str, default = 'pink' The color with which to mark matrix elements. separate_columns : list, default = [] list of column indices before which a vertical line should be drawn separate_rows : list, default = [] list of row indices before which a horizontal line should be drawn Returns ------- out: str Formatted LaTeX string Examples -------- >>> from numpyarray_to_latex import to_ltx >>> tex = to_ltx([[2.,2.],[2.,2.]]) >>> print(tex) \left( \begin{array}{} 2.00 & 2.00\\ 2.00 & 2.00 \end{array} \right) """ a = np.array(a) if len(a.shape) > 2: raise NotImplementedError('Arrays having more than two dimensions cannot be converted.') if mark_elements is None: mark_elements = [] if a.ndim == 2 and len(mark_elements)>0 and not all([hasattr(mark,'__len__') for mark in mark_elements]): raise ValueError("If the array is 2D, ``mark_elements`` should be 2D as well, but isn't") if len(a.shape) == 1: if len(mark_elements)>0 and hasattr(mark_elements[0],'__len__'): raise ValueError("If the array is 1D, ``mark_elements`` should be 1D as well, but isn't.") a = np.array([a]) if is_row_vector is False: a = a.T mark_elements = [ (mark,0) for mark in mark_elements] else: mark_elements = [ (0,mark) for mark in mark_elements] if isinstance(mark_elements, np.ndarray): mark_elements = mark_elements.tolist() mark_elements = [ tuple(row) for row in mark_elements ] nrow, ncol = a.shape out = '' if brackets is not None and latexarraytype not in [ "bmatrix", "pmatrix", "vmatrix", "Bmatrix", "Vmatrix", ]: out = r'\left' + brackets[0] + '\n' if len(separate_columns) > 0: if latexarraytype != 'array': raise ValueError('column separators can only be used for `latexarraytype = "array"`') colstr = '{' for i in range(ncol): if i in separate_columns and i > 0: colstr += '|' colstr += 'c' colstr += '}' else: colstr = '{}' out += r'\begin{' + latexarraytype + '}' +colstr+'\n' for i in np.arange(nrow): if i in separate_rows and i > 0: out += ' \\hline\n' out = out + ' ' for j in np.arange(ncol): this_element = '' if np.real(a[i, j]) < 0: leadstr = '' else: leadstr = ' ' if '.' not in fmt.format(a[i, j]): dot_space = ' ' else: dot_space = '' if np.iscomplexobj(a[i, j]): real = math_form(fmt.format(np.real(a[i, j])), mathform=mathform) real = real.lstrip(' ') imag = math_form(fmt.format(np.imag(a[i, j])), is_imaginary=True, mathform=mathform) imag = imag.lstrip(' ') if not (imag.startswith('-') or imag.startswith('+')): number = real + '+' + imag else: number = real + imag this_element = ( this_element + leadstr + number + imstring + dot_space ) else: this_element = ( this_element + leadstr + math_form(fmt.format(np.real(a[i, j])), mathform=mathform) + dot_space ) if (i,j) in mark_elements: this_element = r'\colorbox{'+ mark_color +'}{$'+ this_element+'$} ' if j < ncol-1: this_element += r' & ' out += this_element if i < nrow-1: out = out + '\\\\\n' out = out + '\n' + r'\end{' + latexarraytype + '}' if brackets is not None and latexarraytype not in [ "bmatrix", "pmatrix", "vmatrix", "Bmatrix", "Vmatrix", ]: out += '\n\\right' + brackets[1] return out
numpyarray_to_latex/main.py
6,289
Return a LaTeX array given a numpy array. Parameters ---------- a : numpy.ndarray fmt : str, default = '{:6.2f}' python 3 formatter, optional- https://mkaz.tech/python-string-format.html latexarraytype : str, default = 'array' Any of .. code:: python "array" "pmatrix" "bmatrix" "vmatrix" "Vmatrix" "Bmatrix" if "array", you can specifiy the brackets with the keyword ``brackets``. imstring : str, default = 'i' Character to use to represent the imaginary unit. Usually ``'i'`` or ``'j'`` is_row_vector : bool, default = True If the array is 1D, should the output be a row (True) or column (False) vector? mathform : bool, default = True wether to convert strings like ``1e+05`` to ``1\times10^{5}``. brackets : iterable, default = '()' which brackets to use to wrap the matrix (must be two elements long). Use ``brackets = None`` if you don't want any brackets around the array. mark_elements : list, default = [] list of tuples containing element indices that should be marked with a colorbox. mark_color : str, default = 'pink' The color with which to mark matrix elements. separate_columns : list, default = [] list of column indices before which a vertical line should be drawn separate_rows : list, default = [] list of row indices before which a horizontal line should be drawn Returns ------- out: str Formatted LaTeX string Examples -------- >>> from numpyarray_to_latex import to_ltx >>> tex = to_ltx([[2.,2.],[2.,2.]]) >>> print(tex) \left( \begin{array}{} 2.00 & 2.00\\ 2.00 & 2.00 \end{array} \right) Provides `to_ltx` to convert numpy arrays to LaTeX.
1,729
en
0.488365
# -*- test-case-name: xmantissa.test.test_offering -*- # Copyright 2008 Divmod, Inc. See LICENSE file for details """ Axiomatic commands for manipulating Mantissa offerings. """ from twisted.python import usage from axiom.scripts import axiomatic from xmantissa import offering, publicweb class Install(axiomatic.AxiomaticSubCommand): synopsis = "<offering>" def parseArgs(self, offering): self["offering"] = self.decodeCommandLine(offering) def postOptions(self): for o in offering.getOfferings(): if o.name == self["offering"]: offering.installOffering(self.store, o, None) break else: raise usage.UsageError("No such offering") class List(axiomatic.AxiomaticSubCommand): def postOptions(self): for o in offering.getOfferings(): print "%s: %s" % (o.name, o.description) class SetFrontPage(axiomatic.AxiomaticSubCommand): """ Command for selecting the site front page. """ def parseArgs(self, offering): """ Collect an installed offering's name. """ self["name"] = self.decodeCommandLine(offering) def postOptions(self): """ Find an installed offering and set the site front page to its application's front page. """ o = self.store.findFirst( offering.InstalledOffering, (offering.InstalledOffering.offeringName == self["name"])) if o is None: raise usage.UsageError("No offering of that name" " is installed.") fp = self.store.findUnique(publicweb.FrontPage) fp.defaultApplication = o.application class OfferingCommand(axiomatic.AxiomaticCommand): name = "offering" description = "View and accept the offerings of puny mortals." subCommands = [ ("install", None, Install, "Install an offering."), ("list", None, List, "List available offerings."), ("frontpage", None, SetFrontPage, "Select an application for the front page."), ] def getStore(self): return self.parent.getStore()
axiom/plugins/offeringcmd.py
2,181
-*- test-case-name: xmantissa.test.test_offering -*- Copyright 2008 Divmod, Inc. See LICENSE file for details
109
en
0.56703
# -*- coding: utf-8 -*- # # RequestsThrottler documentation build configuration file, created by # sphinx-quickstart on Tue Dec 31 13:40:59 2013. # # This file is execfile()d with the current directory set to its # containing dir. # # Note that not all possible configuration values are present in this # autogenerated file. # # All configuration values have a default; values that are commented out # serve to show the default. import sys import os import requests_throttler # 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. #sys.path.insert(0, os.path.abspath('.')) sys.path.insert(0, os.path.abspath('..')) # -- 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.ext.autodoc', ] # Add any paths that contain templates here, relative to this directory. templates_path = ['_templates'] # The suffix of source filenames. source_suffix = '.rst' # The encoding of source files. #source_encoding = 'utf-8-sig' # The master toctree document. master_doc = 'index' # General information about the project. project = u'RequestsThrottler' copyright = u'2013, Lou Marvin Caraig' # The version info for the project you're documenting, acts as replacement for # |version| and |release|, also used in various other places throughout the # built documents. # # The short X.Y version. version = requests_throttler.__version__ # The full version, including alpha/beta/rc tags. release = version # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. #language = None # There are two options for replacing |today|: either, you set today to some # non-false value, then it is used: #today = '' # Else, today_fmt is used as the format for a strftime call. #today_fmt = '%B %d, %Y' # List of patterns, relative to source directory, that match files and # directories to ignore when looking for source files. exclude_patterns = ['_build'] # The reST default role (used for this markup: `text`) to use for all # documents. #default_role = None # If true, '()' will be appended to :func: etc. cross-reference text. #add_function_parentheses = True # If true, the current module name will be prepended to all description # unit titles (such as .. function::). #add_module_names = True # If true, sectionauthor and moduleauthor directives will be shown in the # output. They are ignored by default. #show_authors = False # The name of the Pygments (syntax highlighting) style to use. pygments_style = 'sphinx' # A list of ignored prefixes for module index sorting. #modindex_common_prefix = [] # If true, keep warnings as "system message" paragraphs in the built documents. #keep_warnings = False # -- 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 = 'default' # 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 themes here, relative to this directory. #html_theme_path = [] # The name for this set of Sphinx documents. If None, it defaults to # "<project> v<release> documentation". #html_title = None # A shorter title for the navigation bar. Default is the same as html_title. #html_short_title = None # The name of an image file (relative to this directory) to place at the top # of the sidebar. #html_logo = None # The name of an image file (within the static path) to use as favicon of the # docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 # pixels large. #html_favicon = None # 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'] # Add any extra paths that contain custom files (such as robots.txt or # .htaccess) here, relative to this directory. These files are copied # directly to the root of the documentation. #html_extra_path = [] # If not '', a 'Last updated on:' timestamp is inserted at every page bottom, # using the given strftime format. #html_last_updated_fmt = '%b %d, %Y' # If true, SmartyPants will be used to convert quotes and dashes to # typographically correct entities. #html_use_smartypants = True # Custom sidebar templates, maps document names to template names. #html_sidebars = {} # Additional templates that should be rendered to pages, maps page names to # template names. #html_additional_pages = {} # If false, no module index is generated. #html_domain_indices = True # If false, no index is generated. #html_use_index = True # If true, the index is split into individual pages for each letter. #html_split_index = False # If true, links to the reST sources are added to the pages. #html_show_sourcelink = True # If true, "Created using Sphinx" is shown in the HTML footer. Default is True. #html_show_sphinx = True # If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. #html_show_copyright = True # If true, an OpenSearch description file will be output, and all pages will # contain a <link> tag referring to it. The value of this option must be the # base URL from which the finished HTML is served. #html_use_opensearch = '' # This is the file name suffix for HTML files (e.g. ".xhtml"). #html_file_suffix = None # Output file base name for HTML help builder. htmlhelp_basename = 'RequestsThrottlerdoc' # -- 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': '', } # 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 = [ ('index', 'RequestsThrottler.tex', u'RequestsThrottler Documentation', u'Lou Marvin Caraig', 'manual'), ] # The name of an image file (relative to this directory) to place at the top of # the title page. #latex_logo = None # For "manual" documents, if this is true, then toplevel headings are parts, # not chapters. #latex_use_parts = False # If true, show page references after internal links. #latex_show_pagerefs = False # If true, show URL addresses after external links. #latex_show_urls = False # Documents to append as an appendix to all manuals. #latex_appendices = [] # If false, no module index is generated. #latex_domain_indices = True # -- Options for manual page output --------------------------------------- # One entry per manual page. List of tuples # (source start file, name, description, authors, manual section). man_pages = [ ('index', 'requeststhrottler', u'RequestsThrottler Documentation', [u'Lou Marvin Caraig'], 1) ] # If true, show URL addresses after external links. #man_show_urls = False # -- 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 = [ ('index', 'RequestsThrottler', u'RequestsThrottler Documentation', u'Lou Marvin Caraig', 'RequestsThrottler', 'One line description of project.', 'Miscellaneous'), ] # Documents to append as an appendix to all manuals. #texinfo_appendices = [] # If false, no module index is generated. #texinfo_domain_indices = True # How to display URL addresses: 'footnote', 'no', or 'inline'. #texinfo_show_urls = 'footnote' # If true, do not generate a @detailmenu in the "Top" node's menu. #texinfo_no_detailmenu = False autodoc_member_order = 'bysource'
docs/conf.py
8,416
-*- coding: utf-8 -*- RequestsThrottler documentation build configuration file, created by sphinx-quickstart on Tue Dec 31 13:40:59 2013. This file is execfile()d with the current directory set to its containing dir. Note that not all possible configuration values are present in this autogenerated file. All configuration values have a default; values that are commented out serve to show the default. 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.sys.path.insert(0, os.path.abspath('.')) -- 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. Add any paths that contain templates here, relative to this directory. The suffix of source filenames. The encoding of source files.source_encoding = 'utf-8-sig' The master toctree document. General information about the project. The version info for the project you're documenting, acts as replacement for |version| and |release|, also used in various other places throughout the built documents. The short X.Y version. The full version, including alpha/beta/rc tags. The language for content autogenerated by Sphinx. Refer to documentation for a list of supported languages.language = None There are two options for replacing |today|: either, you set today to some non-false value, then it is used:today = '' Else, today_fmt is used as the format for a strftime call.today_fmt = '%B %d, %Y' List of patterns, relative to source directory, that match files and directories to ignore when looking for source files. The reST default role (used for this markup: `text`) to use for all documents.default_role = None If true, '()' will be appended to :func: etc. cross-reference text.add_function_parentheses = True If true, the current module name will be prepended to all description unit titles (such as .. function::).add_module_names = True If true, sectionauthor and moduleauthor directives will be shown in the output. They are ignored by default.show_authors = False The name of the Pygments (syntax highlighting) style to use. A list of ignored prefixes for module index sorting.modindex_common_prefix = [] If true, keep warnings as "system message" paragraphs in the built documents.keep_warnings = False -- Options for HTML output ---------------------------------------------- The theme to use for HTML and HTML Help pages. See the documentation for a list of builtin themes. 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 themes here, relative to this directory.html_theme_path = [] The name for this set of Sphinx documents. If None, it defaults to "<project> v<release> documentation".html_title = None A shorter title for the navigation bar. Default is the same as html_title.html_short_title = None The name of an image file (relative to this directory) to place at the top of the sidebar.html_logo = None The name of an image file (within the static path) to use as favicon of the docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 pixels large.html_favicon = None 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". Add any extra paths that contain custom files (such as robots.txt or .htaccess) here, relative to this directory. These files are copied directly to the root of the documentation.html_extra_path = [] If not '', a 'Last updated on:' timestamp is inserted at every page bottom, using the given strftime format.html_last_updated_fmt = '%b %d, %Y' If true, SmartyPants will be used to convert quotes and dashes to typographically correct entities.html_use_smartypants = True Custom sidebar templates, maps document names to template names.html_sidebars = {} Additional templates that should be rendered to pages, maps page names to template names.html_additional_pages = {} If false, no module index is generated.html_domain_indices = True If false, no index is generated.html_use_index = True If true, the index is split into individual pages for each letter.html_split_index = False If true, links to the reST sources are added to the pages.html_show_sourcelink = True If true, "Created using Sphinx" is shown in the HTML footer. Default is True.html_show_sphinx = True If true, "(C) Copyright ..." is shown in the HTML footer. Default is True.html_show_copyright = True If true, an OpenSearch description file will be output, and all pages will contain a <link> tag referring to it. The value of this option must be the base URL from which the finished HTML is served.html_use_opensearch = '' This is the file name suffix for HTML files (e.g. ".xhtml").html_file_suffix = None Output file base name for HTML help builder. -- Options for LaTeX output --------------------------------------------- The paper size ('letterpaper' or 'a4paper').'papersize': 'letterpaper', The font size ('10pt', '11pt' or '12pt').'pointsize': '10pt', Additional stuff for the LaTeX preamble.'preamble': '', Grouping the document tree into LaTeX files. List of tuples (source start file, target name, title, author, documentclass [howto, manual, or own class]). The name of an image file (relative to this directory) to place at the top of the title page.latex_logo = None For "manual" documents, if this is true, then toplevel headings are parts, not chapters.latex_use_parts = False If true, show page references after internal links.latex_show_pagerefs = False If true, show URL addresses after external links.latex_show_urls = False Documents to append as an appendix to all manuals.latex_appendices = [] If false, no module index is generated.latex_domain_indices = True -- Options for manual page output --------------------------------------- One entry per manual page. List of tuples (source start file, name, description, authors, manual section). If true, show URL addresses after external links.man_show_urls = False -- 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) Documents to append as an appendix to all manuals.texinfo_appendices = [] If false, no module index is generated.texinfo_domain_indices = True How to display URL addresses: 'footnote', 'no', or 'inline'.texinfo_show_urls = 'footnote' If true, do not generate a @detailmenu in the "Top" node's menu.texinfo_no_detailmenu = False
7,035
en
0.659033
# -*- coding: utf-8 -*- from datetime import date import json from operator import itemgetter import os import warnings from django.core.urlresolvers import NoReverseMatch from django.core.exceptions import ValidationError, ObjectDoesNotExist from django.db import models from django.db.models import signals, Model from django.db.models.base import model_unpickle, ModelBase from django.db.models.query_utils import DeferredAttribute from django.utils import six, timezone from django.utils.encoding import force_text, python_2_unicode_compatible from django.utils.safestring import mark_safe from django.utils.six.moves import filter from django.utils.translation import ugettext_lazy as _ from cms.exceptions import DontUsePageAttributeWarning from cms.models.placeholdermodel import Placeholder from cms.plugin_rendering import PluginContext, render_plugin from cms.utils import get_cms_setting from cms.utils.helpers import reversion_register from cms.utils.urlutils import admin_reverse from treebeard.mp_tree import MP_Node class BoundRenderMeta(object): def __init__(self, meta): self.index = 0 self.total = 1 self.text_enabled = getattr(meta, 'text_enabled', False) class PluginModelBase(ModelBase): """ Metaclass for all CMSPlugin subclasses. This class should not be used for any other type of models. """ def __new__(cls, name, bases, attrs): # remove RenderMeta from the plugin class attr_meta = attrs.pop('RenderMeta', None) # create a new class (using the super-metaclass) new_class = super(PluginModelBase, cls).__new__(cls, name, bases, attrs) # if there is a RenderMeta in attrs, use this one # else try to use the one from the superclass (if present) meta = attr_meta or getattr(new_class, '_render_meta', None) treebeard_view_fields = (f for f in new_class._meta.fields if f.name in ('depth', 'numchild', 'path')) for field in treebeard_view_fields: field.editable = False # set a new BoundRenderMeta to prevent leaking of state new_class._render_meta = BoundRenderMeta(meta) return new_class @python_2_unicode_compatible class CMSPlugin(six.with_metaclass(PluginModelBase, MP_Node)): ''' The base class for a CMS plugin model. When defining a new custom plugin, you should store plugin-instance specific information on a subclass of this class. An example for this would be to store the number of pictures to display in a galery. Two restrictions apply when subclassing this to use in your own models: 1. Subclasses of CMSPlugin *cannot be further subclassed* 2. Subclasses of CMSPlugin cannot define a "text" field. ''' placeholder = models.ForeignKey(Placeholder, editable=False, null=True) parent = models.ForeignKey('self', blank=True, null=True, editable=False) position = models.PositiveSmallIntegerField(_("position"), blank=True, null=True, editable=False) language = models.CharField(_("language"), max_length=15, blank=False, db_index=True, editable=False) plugin_type = models.CharField(_("plugin_name"), max_length=50, db_index=True, editable=False) creation_date = models.DateTimeField(_("creation date"), editable=False, default=timezone.now) changed_date = models.DateTimeField(auto_now=True) child_plugin_instances = None translatable_content_excluded_fields = [] class Meta: app_label = 'cms' class RenderMeta: index = 0 total = 1 text_enabled = False def __reduce__(self): """ Provide pickling support. Normally, this just dispatches to Python's standard handling. However, for models with deferred field loading, we need to do things manually, as they're dynamically created classes and only module-level classes can be pickled by the default path. """ data = self.__dict__ # The obvious thing to do here is to invoke super().__reduce__() # for the non-deferred case. Don't do that. # On Python 2.4, there is something wierd with __reduce__, # and as a result, the super call will cause an infinite recursion. # See #10547 and #12121. deferred_fields = [f for f in self._meta.fields if isinstance(self.__class__.__dict__.get(f.attname), DeferredAttribute)] model = self._meta.proxy_for_model return (model_unpickle, (model, deferred_fields), data) def __str__(self): return force_text(self.pk) def get_plugin_name(self): from cms.plugin_pool import plugin_pool return plugin_pool.get_plugin(self.plugin_type).name def get_short_description(self): instance = self.get_plugin_instance()[0] if instance is not None: return force_text(instance) return _("<Empty>") def get_plugin_class(self): from cms.plugin_pool import plugin_pool return plugin_pool.get_plugin(self.plugin_type) def get_plugin_class_instance(self, admin=None): plugin_class = self.get_plugin_class() # needed so we have the same signature as the original ModelAdmin return plugin_class(plugin_class.model, admin) def get_plugin_instance(self, admin=None): ''' Given a plugin instance (usually as a CMSPluginBase), this method returns a tuple containing: instance - The instance AS THE APPROPRIATE SUBCLASS OF CMSPluginBase and not necessarily just 'self', which is often just a CMSPluginBase, plugin - the associated plugin class instance (subclass of CMSPlugin) ''' plugin = self.get_plugin_class_instance(admin) if hasattr(self, "_inst"): return self._inst, plugin if plugin.model != self.__class__: # and self.__class__ == CMSPlugin: # (if self is actually a subclass, getattr below would break) try: instance = plugin.model.objects.get(cmsplugin_ptr=self) instance._render_meta = self._render_meta except (AttributeError, ObjectDoesNotExist): instance = None else: instance = self self._inst = instance return self._inst, plugin def render_plugin(self, context=None, placeholder=None, admin=False, processors=None): instance, plugin = self.get_plugin_instance() if instance and not (admin and not plugin.admin_preview): if not placeholder or not isinstance(placeholder, Placeholder): placeholder = instance.placeholder placeholder_slot = placeholder.slot current_app = context.current_app if context else None context = PluginContext(context, instance, placeholder, current_app=current_app) context = plugin.render(context, instance, placeholder_slot) request = context.get('request', None) page = None if request: page = request.current_page context['allowed_child_classes'] = plugin.get_child_classes(placeholder_slot, page) if plugin.render_plugin: template = plugin._get_render_template(context, instance, placeholder) if not template: raise ValidationError("plugin has no render_template: %s" % plugin.__class__) else: template = None return render_plugin(context, instance, placeholder, template, processors, context.current_app) else: from cms.middleware.toolbar import toolbar_plugin_processor if processors and toolbar_plugin_processor in processors: if not placeholder: placeholder = self.placeholder current_app = context.current_app if context else None context = PluginContext(context, self, placeholder, current_app=current_app) template = None return render_plugin(context, self, placeholder, template, processors, context.current_app) return "" def get_media_path(self, filename): pages = self.placeholder.page_set.all() if pages.count(): return pages[0].get_media_path(filename) else: # django 1.0.2 compatibility today = date.today() return os.path.join(get_cms_setting('PAGE_MEDIA_PATH'), str(today.year), str(today.month), str(today.day), filename) @property def page(self): warnings.warn( "Don't use the page attribute on CMSPlugins! CMSPlugins are not " "guaranteed to have a page associated with them!", DontUsePageAttributeWarning) return self.placeholder.page if self.placeholder_id else None def get_instance_icon_src(self): """ Get src URL for instance's icon """ instance, plugin = self.get_plugin_instance() return plugin.icon_src(instance) if instance else u'' def get_instance_icon_alt(self): """ Get alt text for instance's icon """ instance, plugin = self.get_plugin_instance() return force_text(plugin.icon_alt(instance)) if instance else u'' def save(self, no_signals=False, *args, **kwargs): if not self.depth: if self.parent_id or self.parent: self.parent.add_child(instance=self) else: if not self.position and not self.position == 0: self.position == CMSPlugin.objects.filter(parent__isnull=True, placeholder_id=self.placeholder_id).count() self.add_root(instance=self) return super(CMSPlugin, self).save() def reload(self): return CMSPlugin.objects.get(pk=self.pk) def move(self, target, pos=None): super(CMSPlugin, self).move(target, pos) return self.reload() def set_base_attr(self, plugin): for attr in ['parent_id', 'placeholder', 'language', 'plugin_type', 'creation_date', 'depth', 'path', 'numchild', 'pk', 'position']: setattr(plugin, attr, getattr(self, attr)) def copy_plugin(self, target_placeholder, target_language, parent_cache, no_signals=False): """ Copy this plugin and return the new plugin. The logic of this method is the following: # get a new generic plugin instance # assign the position in the plugin tree # save it to let mptt/treebeard calculate the tree attributes # then get a copy of the current plugin instance # assign to it the id of the generic plugin instance above; this will effectively change the generic plugin created above into a concrete one # copy the tree related attributes from the generic plugin to the concrete one # save the concrete plugin # trigger the copy relations # return the generic plugin instance This copy logic is required because we don't know what the fields of the real plugin are. By getting another instance of it at step 4 and then overwriting its ID at step 5, the ORM will copy the custom fields for us. """ try: plugin_instance, cls = self.get_plugin_instance() except KeyError: # plugin type not found anymore return # set up some basic attributes on the new_plugin new_plugin = CMSPlugin() new_plugin.placeholder = target_placeholder # we assign a parent to our new plugin parent_cache[self.pk] = new_plugin if self.parent: parent = parent_cache[self.parent_id] parent = CMSPlugin.objects.get(pk=parent.pk) new_plugin.parent_id = parent.pk new_plugin.parent = parent new_plugin.language = target_language new_plugin.plugin_type = self.plugin_type if no_signals: from cms.signals import pre_save_plugins signals.pre_save.disconnect(pre_save_plugins, sender=CMSPlugin, dispatch_uid='cms_pre_save_plugin') signals.pre_save.disconnect(pre_save_plugins, sender=CMSPlugin) new_plugin._no_reorder = True new_plugin.save() if plugin_instance: # get a new instance so references do not get mixed up plugin_instance = plugin_instance.__class__.objects.get(pk=plugin_instance.pk) plugin_instance.pk = new_plugin.pk plugin_instance.id = new_plugin.pk plugin_instance.placeholder = target_placeholder plugin_instance.cmsplugin_ptr = new_plugin plugin_instance.language = target_language plugin_instance.parent = new_plugin.parent plugin_instance.depth = new_plugin.depth plugin_instance.path = new_plugin.path plugin_instance.numchild = new_plugin.numchild plugin_instance._no_reorder = True plugin_instance.save() old_instance = plugin_instance.__class__.objects.get(pk=self.pk) plugin_instance.copy_relations(old_instance) if no_signals: signals.pre_save.connect(pre_save_plugins, sender=CMSPlugin, dispatch_uid='cms_pre_save_plugin') return new_plugin def post_copy(self, old_instance, new_old_ziplist): """ Handle more advanced cases (eg Text Plugins) after the original is copied """ pass def copy_relations(self, old_instance): """ Handle copying of any relations attached to this plugin. Custom plugins have to do this themselves! """ pass def has_change_permission(self, request): page = self.placeholder.page if self.placeholder else None if page: return page.has_change_permission(request) elif self.placeholder: return self.placeholder.has_change_permission(request) return False def get_position_in_placeholder(self): """ 1 based position! """ return self.position + 1 def get_breadcrumb(self): from cms.models import Page model = self.placeholder._get_attached_model() or Page breadcrumb = [] if not self.parent_id: try: url = force_text( admin_reverse("%s_%s_edit_plugin" % (model._meta.app_label, model._meta.model_name), args=[self.pk])) except NoReverseMatch: url = force_text( admin_reverse("%s_%s_edit_plugin" % (Page._meta.app_label, Page._meta.model_name), args=[self.pk])) breadcrumb.append({'title': force_text(self.get_plugin_name()), 'url': url}) return breadcrumb for parent in self.get_ancestors().reverse(): try: url = force_text( admin_reverse("%s_%s_edit_plugin" % (model._meta.app_label, model._meta.model_name), args=[parent.pk])) except NoReverseMatch: url = force_text( admin_reverse("%s_%s_edit_plugin" % (Page._meta.app_label, Page._meta.model_name), args=[parent.pk])) breadcrumb.append({'title': force_text(parent.get_plugin_name()), 'url': url}) return breadcrumb def get_breadcrumb_json(self): result = json.dumps(self.get_breadcrumb()) result = mark_safe(result) return result def num_children(self): return self.numchild def notify_on_autoadd(self, request, conf): """ Method called when we auto add this plugin via default_plugins in CMS_PLACEHOLDER_CONF. Some specific plugins may have some special stuff to do when they are auto added. """ pass def notify_on_autoadd_children(self, request, conf, children): """ Method called when we auto add children to this plugin via default_plugins/<plugin>/children in CMS_PLACEHOLDER_CONF. Some specific plugins may have some special stuff to do when we add children to them. ie : TextPlugin must update its content to add HTML tags to be able to see his children in WYSIWYG. """ pass def get_translatable_content(self): """ Returns {field_name: field_contents} for translatable fields, where field_contents > '' """ fields = (f for f in self._meta.fields if isinstance(f, (models.CharField, models.TextField)) and f.editable and not f.choices and f.name not in self.translatable_content_excluded_fields) return dict(filter(itemgetter(1), ((f.name, getattr(self, f.name)) for f in fields))) def set_translatable_content(self, fields): for field, value in fields.items(): setattr(self, field, value) self.save() return all(getattr(self, field) == value for field, value in fields.items()) def delete(self, no_mp=False, *args, **kwargs): if no_mp: Model.delete(self, *args, **kwargs) else: super(CMSPlugin, self).delete(*args, **kwargs) @property def add_url(self): """ Returns a custom url to add plugin instances """ return None @property def edit_url(self): """ Returns a custom url to edit plugin instances """ return None @property def move_url(self): """ Returns a custom url to move plugin instances """ return None @property def delete_url(self): """ Returns a custom url to delete plugin instances """ return None @property def copy_url(self): """ Returns a custom url to copy plugin instances """ return None reversion_register(CMSPlugin) def get_plugin_media_path(instance, filename): """ Django 1.7 requires that unbound function used in fields' definitions are defined outside the parent class (see https://docs.djangoproject.com/en/dev/topics/migrations/#serializing-values) This function is used withing field definition: file = models.FileField(_("file"), upload_to=get_plugin_media_path) and it invokes the bounded method on the given instance at runtime """ return instance.get_media_path(filename)
cms/models/pluginmodel.py
18,948
The base class for a CMS plugin model. When defining a new custom plugin, you should store plugin-instance specific information on a subclass of this class. An example for this would be to store the number of pictures to display in a galery. Two restrictions apply when subclassing this to use in your own models: 1. Subclasses of CMSPlugin *cannot be further subclassed* 2. Subclasses of CMSPlugin cannot define a "text" field. Metaclass for all CMSPlugin subclasses. This class should not be used for any other type of models. Provide pickling support. Normally, this just dispatches to Python's standard handling. However, for models with deferred field loading, we need to do things manually, as they're dynamically created classes and only module-level classes can be pickled by the default path. Returns a custom url to add plugin instances Copy this plugin and return the new plugin. The logic of this method is the following: # get a new generic plugin instance # assign the position in the plugin tree # save it to let mptt/treebeard calculate the tree attributes # then get a copy of the current plugin instance # assign to it the id of the generic plugin instance above; this will effectively change the generic plugin created above into a concrete one # copy the tree related attributes from the generic plugin to the concrete one # save the concrete plugin # trigger the copy relations # return the generic plugin instance This copy logic is required because we don't know what the fields of the real plugin are. By getting another instance of it at step 4 and then overwriting its ID at step 5, the ORM will copy the custom fields for us. Handle copying of any relations attached to this plugin. Custom plugins have to do this themselves! Returns a custom url to copy plugin instances Returns a custom url to delete plugin instances Returns a custom url to edit plugin instances Get alt text for instance's icon Get src URL for instance's icon Given a plugin instance (usually as a CMSPluginBase), this method returns a tuple containing: instance - The instance AS THE APPROPRIATE SUBCLASS OF CMSPluginBase and not necessarily just 'self', which is often just a CMSPluginBase, plugin - the associated plugin class instance (subclass of CMSPlugin) Django 1.7 requires that unbound function used in fields' definitions are defined outside the parent class (see https://docs.djangoproject.com/en/dev/topics/migrations/#serializing-values) This function is used withing field definition: file = models.FileField(_("file"), upload_to=get_plugin_media_path) and it invokes the bounded method on the given instance at runtime 1 based position! Returns {field_name: field_contents} for translatable fields, where field_contents > '' Returns a custom url to move plugin instances Method called when we auto add this plugin via default_plugins in CMS_PLACEHOLDER_CONF. Some specific plugins may have some special stuff to do when they are auto added. Method called when we auto add children to this plugin via default_plugins/<plugin>/children in CMS_PLACEHOLDER_CONF. Some specific plugins may have some special stuff to do when we add children to them. ie : TextPlugin must update its content to add HTML tags to be able to see his children in WYSIWYG. Handle more advanced cases (eg Text Plugins) after the original is copied -*- coding: utf-8 -*- remove RenderMeta from the plugin class create a new class (using the super-metaclass) if there is a RenderMeta in attrs, use this one else try to use the one from the superclass (if present) set a new BoundRenderMeta to prevent leaking of state The obvious thing to do here is to invoke super().__reduce__() for the non-deferred case. Don't do that. On Python 2.4, there is something wierd with __reduce__, and as a result, the super call will cause an infinite recursion. See 10547 and 12121. needed so we have the same signature as the original ModelAdmin and self.__class__ == CMSPlugin: (if self is actually a subclass, getattr below would break) django 1.0.2 compatibility plugin type not found anymore set up some basic attributes on the new_plugin we assign a parent to our new plugin get a new instance so references do not get mixed up
4,289
en
0.835019
from numpy import * from scipy import ndimage class RansacModel(object): """ Class for testing homography fit with ransac.py from http://www.scipy.org/Cookbook/RANSAC""" def __init__(self,debug=False): self.debug = debug def fit(self, data): """ Fit homography to four selected correspondences. """ # transpose to fit H_from_points() data = data.T # from points fp = data[:3,:4] # target points tp = data[3:,:4] # fit homography and return return H_from_points(fp,tp) def get_error( self, data, H): """ Apply homography to all correspondences, return error for each transformed point. """ data = data.T # from points fp = data[:3] # target points tp = data[3:] # transform fp fp_transformed = dot(H,fp) # normalize hom. coordinates fp_transformed = normalize(fp_transformed) # return error per point return sqrt( sum((tp-fp_transformed)**2,axis=0) ) def H_from_ransac(fp,tp,model,maxiter=1000,match_theshold=10): """ Robust estimation of homography H from point correspondences using RANSAC (ransac.py from http://www.scipy.org/Cookbook/RANSAC). input: fp,tp (3*n arrays) points in hom. coordinates. """ from PCV.tools import ransac # group corresponding points data = vstack((fp,tp)) # compute H and return H,ransac_data = ransac.ransac(data.T,model,4,maxiter,match_theshold,10,return_all=True) return H,ransac_data['inliers'] def H_from_points(fp,tp): """ Find homography H, such that fp is mapped to tp using the linear DLT method. Points are conditioned automatically. """ if fp.shape != tp.shape: raise RuntimeError('number of points do not match') # condition points (important for numerical reasons) # --from points-- m = mean(fp[:2], axis=1) maxstd = max(std(fp[:2], axis=1)) + 1e-9 C1 = diag([1/maxstd, 1/maxstd, 1]) C1[0][2] = -m[0]/maxstd C1[1][2] = -m[1]/maxstd fp = dot(C1,fp) # --to points-- m = mean(tp[:2], axis=1) maxstd = max(std(tp[:2], axis=1)) + 1e-9 C2 = diag([1/maxstd, 1/maxstd, 1]) C2[0][2] = -m[0]/maxstd C2[1][2] = -m[1]/maxstd tp = dot(C2,tp) # create matrix for linear method, 2 rows for each correspondence pair nbr_correspondences = fp.shape[1] A = zeros((2*nbr_correspondences,9)) for i in range(nbr_correspondences): A[2*i] = [-fp[0][i],-fp[1][i],-1,0,0,0, tp[0][i]*fp[0][i],tp[0][i]*fp[1][i],tp[0][i]] A[2*i+1] = [0,0,0,-fp[0][i],-fp[1][i],-1, tp[1][i]*fp[0][i],tp[1][i]*fp[1][i],tp[1][i]] U,S,V = linalg.svd(A) H = V[8].reshape((3,3)) # decondition H = dot(linalg.inv(C2),dot(H,C1)) # normalize and return return H / H[2,2] def Haffine_from_points(fp,tp): """ Find H, affine transformation, such that tp is affine transf of fp. """ if fp.shape != tp.shape: raise RuntimeError('number of points do not match') # condition points # --from points-- m = mean(fp[:2], axis=1) maxstd = max(std(fp[:2], axis=1)) + 1e-9 C1 = diag([1/maxstd, 1/maxstd, 1]) C1[0][2] = -m[0]/maxstd C1[1][2] = -m[1]/maxstd fp_cond = dot(C1,fp) # --to points-- m = mean(tp[:2], axis=1) C2 = C1.copy() #must use same scaling for both point sets C2[0][2] = -m[0]/maxstd C2[1][2] = -m[1]/maxstd tp_cond = dot(C2,tp) # conditioned points have mean zero, so translation is zero A = concatenate((fp_cond[:2],tp_cond[:2]), axis=0) U,S,V = linalg.svd(A.T) # create B and C matrices as Hartley-Zisserman (2:nd ed) p 130. tmp = V[:2].T B = tmp[:2] C = tmp[2:4] tmp2 = concatenate((dot(C,linalg.pinv(B)),zeros((2,1))), axis=1) H = vstack((tmp2,[0,0,1])) # decondition H = dot(linalg.inv(C2),dot(H,C1)) return H / H[2,2] def normalize(points): """ Normalize a collection of points in homogeneous coordinates so that last row = 1. """ for row in points: row /= points[-1] return points def make_homog(points): """ Convert a set of points (dim*n array) to homogeneous coordinates. """ return vstack((points,ones((1,points.shape[1]))))
PCV/geometry/homography.py
4,621
Class for testing homography fit with ransac.py from http://www.scipy.org/Cookbook/RANSAC Find homography H, such that fp is mapped to tp using the linear DLT method. Points are conditioned automatically. Robust estimation of homography H from point correspondences using RANSAC (ransac.py from http://www.scipy.org/Cookbook/RANSAC). input: fp,tp (3*n arrays) points in hom. coordinates. Find H, affine transformation, such that tp is affine transf of fp. Fit homography to four selected correspondences. Apply homography to all correspondences, return error for each transformed point. Convert a set of points (dim*n array) to homogeneous coordinates. Normalize a collection of points in homogeneous coordinates so that last row = 1. transpose to fit H_from_points() from points target points fit homography and return from points target points transform fp normalize hom. coordinates return error per point group corresponding points compute H and return condition points (important for numerical reasons) --from points-- --to points-- create matrix for linear method, 2 rows for each correspondence pair decondition normalize and return condition points --from points-- --to points--must use same scaling for both point sets conditioned points have mean zero, so translation is zero create B and C matrices as Hartley-Zisserman (2:nd ed) p 130. decondition
1,374
en
0.707961
from django.core.paginator import InvalidPage, EmptyPage, Paginator, PageNotAnInteger from django.http import HttpResponse from django.shortcuts import render from youtube.models import Youtube # Create your views here. def youtube(request): is_login = None try: is_login = request.session['is_login'] except: pass if not is_login: return render(request, "youtube.html", {}) elif request.session['user_level'] < 0: return render(request, "youtube.html", {}) # user_list = models.UserDetails.objects.all().values('user_name','user_password') youtubes = Youtube.objects.using('youtube_db').all().values('title', 'description', 'cover', 'uploadtime', 'channeltitle', 'video' ).order_by('-uploadtime') paginator = Paginator(youtubes, 10) res = [] if request.method == "GET": # 获取 url 后面的 page 参数的值, 首页不显示 page 参数, 默认值是 1 page = request.GET.get('page') if page is None: res_page = 1 else: res_page = int(page) try: res = paginator.page(page).object_list pages = paginator.page(page) # todo: 注意捕获异常 except PageNotAnInteger: # 如果请求的页数不是整数, 返回第一页。 res = paginator.page(1).object_list pages = paginator.page(1) res_page = 1 except InvalidPage: # 如果请求的页数不存在, 重定向页面 return HttpResponse('找不到页面的内容') except EmptyPage: # 如果请求的页数不在合法的页数范围内,返回结果的最后一页。 res = paginator.page(paginator.num_pages).object_list pages = paginator.page(paginator.num_pages) return render(request, "youtube.html", {"data": res, "pages": pages, "current_page": res_page})
PythonWeb/apps/youtube/views.py
2,103
Create your views here. user_list = models.UserDetails.objects.all().values('user_name','user_password') 获取 url 后面的 page 参数的值, 首页不显示 page 参数, 默认值是 1 todo: 注意捕获异常 如果请求的页数不是整数, 返回第一页。 如果请求的页数不存在, 重定向页面 如果请求的页数不在合法的页数范围内,返回结果的最后一页。
228
zh
0.90331
# ---------------------------------------------------------------------------- # Copyright 2021 MonaLabs.io # # 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. # ---------------------------------------------------------------------------- """ This module holds all authentication information and related functions. For a given api_key, it can provide a new access token, refresh an expired access token or give authentication status information. """ import os import time import datetime from functools import wraps from threading import Lock import requests from requests.models import Response from .logger import get_logger from .client_util import get_boolean_value_for_env_var from .client_exceptions import MonaAuthenticationException # A new token expires after 22 hours, REFRESH_TOKEN_SAFETY_MARGIN is the safety gap of # time to refresh the token before it expires (i.e. - in case # REFRESH_TOKEN_SAFETY_MARGIN = 2, and the token is about to expire in 2 hours or less, # the client will automatically refresh the token to a new one). REFRESH_TOKEN_SAFETY_MARGIN = datetime.timedelta( hours=int(os.environ.get("REFRESH_TOKEN_SAFETY_MARGIN", 12)) ) AUTH_API_TOKEN_URL = os.environ.get( "AUTH_API_TOKEN_URL", "https://monalabs.frontegg.com/identity/resources/auth/v1/api-token", ) REFRESH_TOKEN_URL = os.environ.get( "REFRESH_TOKEN_URL", "https://monalabs.frontegg.com/identity/resources/auth/v1/api-token/" "token/refresh", ) BASIC_HEADER = {"Content-Type": "application/json"} TOKEN_EXPIRED_DATE_FORMAT = "%a, %d %b %Y %H:%M:%S GMT" # Number of retries to authenticate in case the authentication server failed to # respond. NUM_OF_RETRIES_FOR_AUTHENTICATION = int( os.environ.get("NUM_OF_RETRIES_FOR_AUTHENTICATION", 3) ) # Time to wait (in seconds) between retries in case the authentication server failed to # respond. WAIT_TIME_FOR_AUTHENTICATION_RETRIES_SEC = int( os.environ.get("WAIT_TIME_FOR_AUTHENTICATION_RETRIES_SEC", 2) ) # Note: if RAISE_AUTHENTICATION_EXCEPTIONS = False and the client could not # authenticate, every function call will return false. # Use client.is_active() in order to check authentication status. RAISE_AUTHENTICATION_EXCEPTIONS = get_boolean_value_for_env_var( "RAISE_AUTHENTICATION_EXCEPTIONS", False ) # This dict maps between every api_key (each api_key is saved only once in this dict) # and its access token info (if the given api_key is authenticated it will contain the # token itself, its expiration date and the key to refresh it, otherwise it will contain # the errors that occurred while trying to authenticate). API_KEYS_TO_TOKEN_DATA = {} # Token data args names: ERRORS = "errors" EXPIRES = "expires" ACCESS_TOKEN = "accessToken" REFRESH_TOKEN = "refreshToken" TIME_TO_REFRESH = "timeToRefresh" IS_AUTHENTICATED = "isAuthenticated" # TODO(anat): consider initializing a different lock for each api_key. authentication_lock = Lock() def first_authentication(api_key, secret): # TODO(anat): Support non-authenticated init. if not is_authenticated(api_key): # Make sure only one instance of the client (with the given api_key) can get a # new token. That token will be shared between all instances that share an # api_key. with authentication_lock: # The inner check is needed to avoid multiple redundant authentications. if not is_authenticated(api_key): response = _request_access_token_with_retries(api_key, secret) API_KEYS_TO_TOKEN_DATA[api_key] = response.json() # response.ok will be True if authentication was successful and # false if not. _set_api_key_authentication_status(api_key, response.ok) _calculate_and_set_time_to_refresh(api_key) # If the authentication failed, handle error and return false. if not is_authenticated(api_key): return _handle_authentications_error( f"Mona's client could not authenticate. " f"errors: {_get_error_string_from_token_info(api_key)}" ) else: get_logger().info(f"New client token info: {API_KEYS_TO_TOKEN_DATA[api_key]}") return True def _get_error_string_from_token_info(api_key): error_list = _get_token_info_by_api_key(api_key, ERRORS) return ", ".join(_get_token_info_by_api_key(api_key, ERRORS)) if error_list else "" def _request_access_token_with_retries(api_key, secret): return _get_auth_response_with_retries( lambda: _request_access_token_once(api_key, secret) ) def _request_refresh_token_with_retries(refresh_token_key): return _get_auth_response_with_retries( lambda: _request_refresh_token_once(refresh_token_key) ) def _get_auth_response_with_retries( response_generator, num_of_retries=NUM_OF_RETRIES_FOR_AUTHENTICATION, auth_wait_time_sec=WAIT_TIME_FOR_AUTHENTICATION_RETRIES_SEC, ): """ Sends an authentication request (first time/refresh) with a retry mechanism. :param response_generator (lambda) A function call that sends the wanted REST request. :return: The response received from the authentication server. """ for i in range(num_of_retries + 1): try: response = response_generator() # Check that response is json-serializable. response.json() # Got a response, log and break the retry loop. get_logger().info(f"Got an authentication response after {i} retries.") break except Exception: if i == num_of_retries: # Retried to authenticate num_of_retries times and failed due to # authentications server problems, return a response with the relevant # info. response = _create_a_bad_response( '{"errors": ["Could not connect to authentication server",' ' "Number of retries: ' + str(i) + '"]}' ) else: # TODO(anat): support exponential growth in wait times between retries. # Has more retries, sleep before trying again. time.sleep(auth_wait_time_sec) return response def _request_access_token_once(api_key, secret): """ Sends an access token REST request and returns the response. """ return requests.request( "POST", AUTH_API_TOKEN_URL, headers=BASIC_HEADER, json={"clientId": api_key, "secret": secret}, ) def _request_refresh_token_once(refresh_token_key): """ Sends a refresh token REST request and returns the response. """ return requests.request( "POST", REFRESH_TOKEN_URL, headers=BASIC_HEADER, json={"refreshToken": refresh_token_key}, ) def _create_a_bad_response(content): """ :param: content (str) The content of the response. :return: A functioning bad REST response instance with the given content. """ response = Response() response.status_code = 400 if type(content) is str: # _content expect bytes. response._content = bytes(content, "utf8") return response def get_current_token_by_api_key(api_key): """ :return: The given api_key's current access token. """ return _get_token_info_by_api_key(api_key, ACCESS_TOKEN) def _get_token_info_by_api_key(api_key, token_data_arg): """ Returns the value of the wanted data for the given api_key. Returns None if the api_key or the arg does not exist. """ return API_KEYS_TO_TOKEN_DATA.get(api_key, {}).get(token_data_arg) def is_authenticated(api_key): """ :return: True if Mona's client holds a valid token and can communicate with Mona's servers (or can refresh the token in order to), False otherwise. """ return _get_token_info_by_api_key(api_key, IS_AUTHENTICATED) def _set_api_key_authentication_status(api_key, bool_value): """ Sets the IS_AUTHENTICATED arg in the token data dict of the given api_key, this setter is only needed to spare redundant calls for authentication. """ API_KEYS_TO_TOKEN_DATA[api_key][IS_AUTHENTICATED] = bool_value def _calculate_and_set_time_to_refresh(api_key): """ Calculates the time the access token needs to be refreshed and updates the relevant api_key token data. """ if is_authenticated(api_key): token_expires = datetime.datetime.strptime( _get_token_info_by_api_key(api_key, EXPIRES), TOKEN_EXPIRED_DATE_FORMAT ) # Set the found value in the clients token info. API_KEYS_TO_TOKEN_DATA[api_key][TIME_TO_REFRESH] = ( token_expires - REFRESH_TOKEN_SAFETY_MARGIN ) def _handle_authentications_error(error_message): """ Logs an error and raises MonaAuthenticationException if RAISE_AUTHENTICATION_EXCEPTIONS is true, else returns false. """ get_logger().error(error_message) if RAISE_AUTHENTICATION_EXCEPTIONS: raise MonaAuthenticationException(error_message) return False def _should_refresh_token(api_key): """ :return: True if the token has expired, or is about to expire in REFRESH_TOKEN_SAFETY_MARGIN hours or less, False otherwise. """ return ( _get_token_info_by_api_key(api_key, TIME_TO_REFRESH) < datetime.datetime.now() ) def _refresh_token(api_key): """ Gets a new token and sets the needed fields. """ refresh_token_key = _get_token_info_by_api_key(api_key, REFRESH_TOKEN) response = _request_refresh_token_with_retries(refresh_token_key) authentications_response_info = response.json() # Log or raise an error in case one occurred. # The current client token info will not change so that on the next function call # the client will try to refresh the token again. if not response.ok: return _handle_authentications_error( f"Could not refresh token: {response.text}" ) # Update the client's new token info. API_KEYS_TO_TOKEN_DATA[api_key] = authentications_response_info _set_api_key_authentication_status(api_key, True) _calculate_and_set_time_to_refresh(api_key) get_logger().info( f"Refreshed access token, the new token info:" f" {API_KEYS_TO_TOKEN_DATA[api_key]}" ) return True def get_basic_auth_header(api_key): return { "Content-Type": "application/json", "Authorization": f"Bearer " f"{get_current_token_by_api_key(api_key)}", } class Decorators(object): @classmethod def refresh_token_if_needed(cls, decorated): """ This decorator checks if the current client's access token is about to be expired/already expired, and if so, updates to a new one. """ @wraps(decorated) def inner(*args, **kwargs): # args[0] is the current client instance. api_key = args[0]._api_key if not is_authenticated(api_key): get_logger().warn("Mona's client is not authenticated") return False if _should_refresh_token(api_key): with authentication_lock: # The inner check is needed to avoid double token refresh. if _should_refresh_token(api_key): did_refresh_token = _refresh_token(api_key) if not did_refresh_token: # TODO(anat): Check if the current token is still valid to # call the function anyway. return False return decorated(*args, **kwargs) return inner
mona_sdk/authentication.py
12,288
Calculates the time the access token needs to be refreshed and updates the relevant api_key token data. :param: content (str) The content of the response. :return: A functioning bad REST response instance with the given content. Sends an authentication request (first time/refresh) with a retry mechanism. :param response_generator (lambda) A function call that sends the wanted REST request. :return: The response received from the authentication server. Returns the value of the wanted data for the given api_key. Returns None if the api_key or the arg does not exist. Logs an error and raises MonaAuthenticationException if RAISE_AUTHENTICATION_EXCEPTIONS is true, else returns false. Gets a new token and sets the needed fields. Sends an access token REST request and returns the response. Sends a refresh token REST request and returns the response. Sets the IS_AUTHENTICATED arg in the token data dict of the given api_key, this setter is only needed to spare redundant calls for authentication. :return: True if the token has expired, or is about to expire in REFRESH_TOKEN_SAFETY_MARGIN hours or less, False otherwise. :return: The given api_key's current access token. :return: True if Mona's client holds a valid token and can communicate with Mona's servers (or can refresh the token in order to), False otherwise. This decorator checks if the current client's access token is about to be expired/already expired, and if so, updates to a new one. This module holds all authentication information and related functions. For a given api_key, it can provide a new access token, refresh an expired access token or give authentication status information. ---------------------------------------------------------------------------- Copyright 2021 MonaLabs.io 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. ---------------------------------------------------------------------------- A new token expires after 22 hours, REFRESH_TOKEN_SAFETY_MARGIN is the safety gap of time to refresh the token before it expires (i.e. - in case REFRESH_TOKEN_SAFETY_MARGIN = 2, and the token is about to expire in 2 hours or less, the client will automatically refresh the token to a new one). Number of retries to authenticate in case the authentication server failed to respond. Time to wait (in seconds) between retries in case the authentication server failed to respond. Note: if RAISE_AUTHENTICATION_EXCEPTIONS = False and the client could not authenticate, every function call will return false. Use client.is_active() in order to check authentication status. This dict maps between every api_key (each api_key is saved only once in this dict) and its access token info (if the given api_key is authenticated it will contain the token itself, its expiration date and the key to refresh it, otherwise it will contain the errors that occurred while trying to authenticate). Token data args names: TODO(anat): consider initializing a different lock for each api_key. TODO(anat): Support non-authenticated init. Make sure only one instance of the client (with the given api_key) can get a new token. That token will be shared between all instances that share an api_key. The inner check is needed to avoid multiple redundant authentications. response.ok will be True if authentication was successful and false if not. If the authentication failed, handle error and return false. Check that response is json-serializable. Got a response, log and break the retry loop. Retried to authenticate num_of_retries times and failed due to authentications server problems, return a response with the relevant info. TODO(anat): support exponential growth in wait times between retries. Has more retries, sleep before trying again. _content expect bytes. Set the found value in the clients token info. Log or raise an error in case one occurred. The current client token info will not change so that on the next function call the client will try to refresh the token again. Update the client's new token info. args[0] is the current client instance. The inner check is needed to avoid double token refresh. TODO(anat): Check if the current token is still valid to call the function anyway.
4,693
en
0.774117
# Generated by Django 3.0.4 on 2020-04-08 18:23 from django.conf import settings from django.db import migrations, models import django.db.models.deletion class Migration(migrations.Migration): dependencies = [ ('core', '0001_initial'), ] operations = [ migrations.CreateModel( name='Tag', fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('name', models.CharField(max_length=255)), ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), ], ), ]
app/core/migrations/0002_tag.py
682
Generated by Django 3.0.4 on 2020-04-08 18:23
45
en
0.733229
""" Exceptions interface. Exceptions allow for ignoring detected issues. This is commonly done to suppress false positives or to ignore issues that a group has no intention of addressing. The two types of exceptions are a list of filenames or regular expressions. If using filename matching for the exception it is required that the reported issue contain the absolute path to the file containing the issue to be ignored. The path for the issue is set in the tool plugin that generates the issues. """ from __future__ import print_function import fnmatch import os import re import yaml class Exceptions(object): """Interface for applying exceptions.""" def __init__(self, filename): """Initialize exceptions interface.""" with open(filename) as fname: self.exceptions = yaml.safe_load(fname) def get_ignore_packages(self): """Get list of packages to skip when scanning a workspace.""" ignore = [] if "ignore_packages" in self.exceptions and self.exceptions["ignore_packages"] is not None: ignore = self.exceptions["ignore_packages"] return ignore def get_exceptions(self, package): """Get specific exceptions for given package.""" exceptions = {"file": [], "message_regex": []} if "global" in self.exceptions and "exceptions" in self.exceptions["global"]: global_exceptions = self.exceptions["global"]["exceptions"] if "file" in global_exceptions: exceptions["file"] += global_exceptions["file"] if "message_regex" in global_exceptions: exceptions["message_regex"] += global_exceptions["message_regex"] # pylint: disable=too-many-boolean-expressions if self.exceptions and "packages" in self.exceptions \ and self.exceptions["packages"] \ and package.name in self.exceptions["packages"] \ and self.exceptions["packages"][package.name] \ and "exceptions" in self.exceptions["packages"][package.name]: package_exceptions = self.exceptions["packages"][package.name]["exceptions"] if "file" in package_exceptions: exceptions["file"] += package_exceptions["file"] if "message_regex" in package_exceptions: exceptions["message_regex"] += package_exceptions["message_regex"] # pylint: enable=too-many-boolean-expressions return exceptions def filter_file_exceptions_early(self, package, file_list): """ Filter files based on file pattern exceptions list. Only filters files which have tools=all, intended for use after the discovery plugins have been run (so that Statick doesn't run the tool plugins against files which will be ignored anyway). """ exceptions = self.get_exceptions(package) to_remove = [] for filename in file_list: removed = False for exception in exceptions["file"]: if exception["tools"] == 'all': for pattern in exception["globs"]: # Hack to avoid exceptions for everything on Travis CI. fname = filename prefix = '/home/travis/build/' if pattern == '*/build/*' and fname.startswith(prefix): fname = fname[len(prefix):] if fnmatch.fnmatch(fname, pattern): to_remove.append(filename) removed = True break if removed: break file_list = [filename for filename in file_list if filename not in to_remove] return file_list def filter_file_exceptions(self, package, exceptions, issues): """Filter issues based on file pattern exceptions list.""" for tool, tool_issues in list(issues.items()): # pylint: disable=too-many-nested-blocks warning_printed = False to_remove = [] for issue in tool_issues: if not os.path.isabs(issue.filename): if not warning_printed: self.print_exception_warning(tool) warning_printed = True continue rel_path = os.path.relpath(issue.filename, package.path) for exception in exceptions: if exception["tools"] == 'all' or tool in exception["tools"]: for pattern in exception["globs"]: # Hack to avoid exceptions for everything on Travis CI. fname = issue.filename prefix = '/home/travis/build/' if pattern == '*/build/*' and fname.startswith(prefix): fname = fname[len(prefix):] if fnmatch.fnmatch(fname, pattern) or \ fnmatch.fnmatch(rel_path, pattern): to_remove.append(issue) issues[tool] = [issue for issue in tool_issues if issue not in to_remove] return issues @classmethod def filter_regex_exceptions(cls, exceptions, issues): """Filter issues based on message regex exceptions list.""" for exception in exceptions: exception_re = exception["regex"] exception_tools = exception["tools"] compiled_re = re.compile(exception_re) for tool, tool_issues in list(issues.items()): to_remove = [] if exception_tools == "all" or tool in exception_tools: for issue in tool_issues: match = compiled_re.match(issue.message) if match: to_remove.append(issue) issues[tool] = [issue for issue in tool_issues if issue not in to_remove] return issues def filter_nolint(self, issues): """ Filter out lines that have an explicit NOLINT on them. Sometimes the tools themselves don't properly filter these out if there is a complex macro or something. """ for tool, tool_issues in list(issues.items()): warning_printed = False to_remove = [] for issue in tool_issues: if not os.path.isabs(issue.filename): if not warning_printed: self.print_exception_warning(tool) warning_printed = True continue lines = open(issue.filename, "r+", encoding="utf-8").readlines() line_number = int(issue.line_number) - 1 if line_number < len(lines) and "NOLINT" in lines[line_number]: to_remove.append(issue) issues[tool] = [issue for issue in tool_issues if issue not in to_remove] return issues def filter_issues(self, package, issues): """Filter issues based on exceptions list.""" exceptions = self.get_exceptions(package) if exceptions["file"]: issues = self.filter_file_exceptions(package, exceptions["file"], issues) if exceptions["message_regex"]: issues = self.filter_regex_exceptions(exceptions["message_regex"], issues) issues = self.filter_nolint(issues) return issues @classmethod def print_exception_warning(cls, tool): """ Print warning about exception not being applied for an issue. Warning will only be printed once per tool. """ print("[WARNING] File exceptions not available for {} tool " "plugin due to lack of absolute paths for issues.".format(tool))
statick_tool/exceptions.py
8,122
Interface for applying exceptions. Initialize exceptions interface. Filter issues based on file pattern exceptions list. Filter files based on file pattern exceptions list. Only filters files which have tools=all, intended for use after the discovery plugins have been run (so that Statick doesn't run the tool plugins against files which will be ignored anyway). Filter issues based on exceptions list. Filter out lines that have an explicit NOLINT on them. Sometimes the tools themselves don't properly filter these out if there is a complex macro or something. Filter issues based on message regex exceptions list. Get specific exceptions for given package. Get list of packages to skip when scanning a workspace. Print warning about exception not being applied for an issue. Warning will only be printed once per tool. Exceptions interface. Exceptions allow for ignoring detected issues. This is commonly done to suppress false positives or to ignore issues that a group has no intention of addressing. The two types of exceptions are a list of filenames or regular expressions. If using filename matching for the exception it is required that the reported issue contain the absolute path to the file containing the issue to be ignored. The path for the issue is set in the tool plugin that generates the issues. pylint: disable=too-many-boolean-expressions pylint: enable=too-many-boolean-expressions Hack to avoid exceptions for everything on Travis CI. pylint: disable=too-many-nested-blocks Hack to avoid exceptions for everything on Travis CI.
1,559
en
0.886935
#!/usr/bin/env python # Decodes a given Caesar Cipher encoded string, provided on stdin # Credit: http://stackoverflow.com/a/10792382 import sys import string # Frequency of each character (English) frequency = dict(zip(string.ascii_uppercase, [.0817,.0149,.0278,.0425,.1270,.0223,.0202, .0609,.0697,.0015,.0077,.0402,.0241,.0675, .0751,.0193,.0009,.0599,.0633,.0906,.0276, .0098,.0236,.0015,.0197,.0007])) # Create 26 translation tables, one for each rotation 0 through 25 # eg: ABCDEFGHIJKLMNOPQRSTUVWXYZ, BCDEFGHIJKLMNOPQRSTUVWXYZA... trans_tables = [ string.maketrans(string.ascii_uppercase, string.ascii_uppercase[i:]+string.ascii_uppercase[:i]) for i in range(26)] def fitness(msg): # Sum all the frequencies of each character in a string return sum(frequency[char] for char in msg) def all_shifts(msg): # Try every rotation using the translation tables generated earlier # Returns a generator with three values msg = msg.upper() for index, table in enumerate(trans_tables): output = msg.translate(table) yield fitness(output), index, output # Main code - accept input from stdin, find rotation with highest fitness value ciphertext = raw_input().replace(" ", "") (score, index, output) = max(all_shifts(ciphertext)) print "Rotation by {:d} (key {:d}) yields decoded text:\n{}".format(index, 26-index, output)
caesar.py
1,514
!/usr/bin/env python Decodes a given Caesar Cipher encoded string, provided on stdin Credit: http://stackoverflow.com/a/10792382 Frequency of each character (English) Create 26 translation tables, one for each rotation 0 through 25 eg: ABCDEFGHIJKLMNOPQRSTUVWXYZ, BCDEFGHIJKLMNOPQRSTUVWXYZA... Sum all the frequencies of each character in a string Try every rotation using the translation tables generated earlier Returns a generator with three values Main code - accept input from stdin, find rotation with highest fitness value
529
en
0.651788
#El módulo 'os' nos permitirá consultar si un archivo existe. import os def mostrar_bienvenida(): print("Bienvenido a ... ") print(""" _ __ ____ ___ (_) ________ ____/ / / __ `__ \/ / / ___/ _ \/ __ / / / / / / / / / / / __/ /_/ / /_/ /_/ /_/_/ /_/ \___/\__,_/ """) def obtener_nombre(): nombre = input("Para empezar, dime como te llamas. ") return nombre def obtener_edad(): agno = int(input("Para preparar tu perfil, dime en qué año naciste. ")) return 2017-agno-1 def obtener_estatura(): estatura = float(input("Cuéntame más de ti, para agregarlo a tu perfil. ¿Cuánto mides? Dímelo en metros. ")) metros = int(estatura) centimetros = int( (estatura - metros)*100 ) return (metros, centimetros) def obtener_sexo(): sexo = input("Por favor, ingresa tu sexo (M=Masculino, F=Femenino): ") while sexo != 'M' and sexo != 'F': sexo = input("Por favor, ingresa tu sexo (M=Masculino, F=Femenino): ") return sexo def obtener_pais(): pais = input("Indica tu país de nacimiento: ") return pais def obtener_lista_amigos(): linea = input("Muy bien. Finalmente, escribe una lista con los nombres de tus amigos, separados por una ',': ") amigos = linea.split(",") return amigos def mostrar_perfil(nombre, edad, estatura_m, estatura_cm, sexo, pais, amigos): print("--------------------------------------------------") print("Nombre: ", nombre) print("Edad: ", edad, "años") print("Estatura: ", estatura_m, "m y ", estatura_cm, "centímetros") print("Sexo: ", sexo) print("País: ", pais) print("Amigos: ", len(amigos)) print("--------------------------------------------------") def opcion_menu(): print("Acciones disponibles:") print(" 1. Escribir un mensaje") print(" 2. Mostrar mi muro") print(" 3. Mostrar los datos de perfil") print(" 4. Actualizar el perfil de usuario") print(" 0. Salir") opcion = int(input("Ingresa una opción: ")) while opcion < 0 or opcion > 5: print("No conozco la opción que has ingresado. Inténtalo otra vez.") opcion = int(input("Ingresa una opción: ")) return opcion def obtener_mensaje(): mensaje = input("Ahora vamos a publicar un mensaje. ¿Qué piensas hoy? ") return mensaje def mostrar_mensaje(origen, mensaje): print("--------------------------------------------------") print(origen+":", mensaje) print("--------------------------------------------------") #Muestra los mensajes recibidos def mostrar_muro(muro): print("------ MURO ("+str(len(muro))+" mensajes) ---------") for mensaje in muro: print(mensaje) print("--------------------------------------------------") #Publica un mensaje en el timeline personal y en el de los amigos def publicar_mensaje(origen, amigos, mensaje, muro): print("--------------------------------------------------") print(origen, "dice:", mensaje) print("--------------------------------------------------") #Agrega el mensaje al final del timeline local muro.append(mensaje) #Agrega, al final del archivo de cada amigo, el mensaje publicado for amigo in amigos: if existe_archivo(amigo+".user"): archivo = open(amigo+".user","a") archivo.write(origen+":"+mensaje+"\n") archivo.close() def existe_archivo(ruta): return os.path.isfile(ruta) def leer_usuario(nombre): archivo_usuario = open(nombre+".user","r") nombre = archivo_usuario.readline().rstrip() edad = int(archivo_usuario.readline()) estatura = float(archivo_usuario.readline()) estatura_m = int(estatura) estatura_cm = int( (estatura - estatura_m)*100 ) sexo = archivo_usuario.readline().rstrip() pais = archivo_usuario.readline().rstrip() amigos = archivo_usuario.readline().rstrip().split(",") estado = archivo_usuario.readline().rstrip() #Lee el 'muro'. Esto es, todos los mensajes que han sido publicados en el timeline del usuario. muro = [] mensaje = archivo_usuario.readline().rstrip() while mensaje != "": muro.append(mensaje) mensaje = archivo_usuario.readline().rstrip() #Una vez que hemos leido los datos del usuario no debemos olvidar cerrar el archivo archivo_usuario.close() return(nombre, edad, estatura_m, estatura_cm, sexo, pais, amigos, estado, muro) def escribir_usuario(nombre, edad, estatura_m, estatura_cm, sexo, pais, amigos, estado, muro): archivo_usuario = open(nombre+".user","w") archivo_usuario.write(nombre+"\n") archivo_usuario.write(str(edad)+"\n") archivo_usuario.write(str(estatura_m + estatura_cm/100)+"\n") archivo_usuario.write(sexo+"\n") archivo_usuario.write(pais+"\n") archivo_usuario.write(",".join(amigos)+"\n") archivo_usuario.write(estado+"\n") #Escribe el 'timeline' en el archivo, a continuación del último estado for mensaje in muro: archivo_usuario.write(mensaje+"\n") #Una vez que hemos escrito todos los datos del usuario en el archivo, no debemos olvidar cerrarlo archivo_usuario.close()
s6red.py
5,252
El módulo 'os' nos permitirá consultar si un archivo existe.Muestra los mensajes recibidosPublica un mensaje en el timeline personal y en el de los amigosAgrega el mensaje al final del timeline localAgrega, al final del archivo de cada amigo, el mensaje publicadoLee el 'muro'. Esto es, todos los mensajes que han sido publicados en el timeline del usuario.Una vez que hemos leido los datos del usuario no debemos olvidar cerrar el archivoEscribe el 'timeline' en el archivo, a continuación del último estadoUna vez que hemos escrito todos los datos del usuario en el archivo, no debemos olvidar cerrarlo
608
es
0.99323
# coding: utf-8 """ Kubernetes No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator) # noqa: E501 The version of the OpenAPI document: v1.20.7 Generated by: https://openapi-generator.tech """ import pprint import re # noqa: F401 import six from kubernetes.client.configuration import Configuration class IoXK8sClusterInfrastructureV1alpha4AWSClusterTemplateList(object): """NOTE: This class is auto generated by OpenAPI Generator. Ref: https://openapi-generator.tech Do not edit the class manually. """ """ Attributes: openapi_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. """ openapi_types = { 'api_version': 'str', 'items': 'list[IoXK8sClusterInfrastructureV1alpha4AWSClusterTemplate]', 'kind': 'str', 'metadata': 'V1ListMeta' } attribute_map = { 'api_version': 'apiVersion', 'items': 'items', 'kind': 'kind', 'metadata': 'metadata' } def __init__(self, api_version=None, items=None, kind=None, metadata=None, local_vars_configuration=None): # noqa: E501 """IoXK8sClusterInfrastructureV1alpha4AWSClusterTemplateList - a model defined in OpenAPI""" # noqa: E501 if local_vars_configuration is None: local_vars_configuration = Configuration() self.local_vars_configuration = local_vars_configuration self._api_version = None self._items = None self._kind = None self._metadata = None self.discriminator = None if api_version is not None: self.api_version = api_version self.items = items if kind is not None: self.kind = kind if metadata is not None: self.metadata = metadata @property def api_version(self): """Gets the api_version of this IoXK8sClusterInfrastructureV1alpha4AWSClusterTemplateList. # noqa: E501 APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources # noqa: E501 :return: The api_version of this IoXK8sClusterInfrastructureV1alpha4AWSClusterTemplateList. # noqa: E501 :rtype: str """ return self._api_version @api_version.setter def api_version(self, api_version): """Sets the api_version of this IoXK8sClusterInfrastructureV1alpha4AWSClusterTemplateList. APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources # noqa: E501 :param api_version: The api_version of this IoXK8sClusterInfrastructureV1alpha4AWSClusterTemplateList. # noqa: E501 :type: str """ self._api_version = api_version @property def items(self): """Gets the items of this IoXK8sClusterInfrastructureV1alpha4AWSClusterTemplateList. # noqa: E501 List of awsclustertemplates. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md # noqa: E501 :return: The items of this IoXK8sClusterInfrastructureV1alpha4AWSClusterTemplateList. # noqa: E501 :rtype: list[IoXK8sClusterInfrastructureV1alpha4AWSClusterTemplate] """ return self._items @items.setter def items(self, items): """Sets the items of this IoXK8sClusterInfrastructureV1alpha4AWSClusterTemplateList. List of awsclustertemplates. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md # noqa: E501 :param items: The items of this IoXK8sClusterInfrastructureV1alpha4AWSClusterTemplateList. # noqa: E501 :type: list[IoXK8sClusterInfrastructureV1alpha4AWSClusterTemplate] """ if self.local_vars_configuration.client_side_validation and items is None: # noqa: E501 raise ValueError("Invalid value for `items`, must not be `None`") # noqa: E501 self._items = items @property def kind(self): """Gets the kind of this IoXK8sClusterInfrastructureV1alpha4AWSClusterTemplateList. # noqa: E501 Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds # noqa: E501 :return: The kind of this IoXK8sClusterInfrastructureV1alpha4AWSClusterTemplateList. # noqa: E501 :rtype: str """ return self._kind @kind.setter def kind(self, kind): """Sets the kind of this IoXK8sClusterInfrastructureV1alpha4AWSClusterTemplateList. Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds # noqa: E501 :param kind: The kind of this IoXK8sClusterInfrastructureV1alpha4AWSClusterTemplateList. # noqa: E501 :type: str """ self._kind = kind @property def metadata(self): """Gets the metadata of this IoXK8sClusterInfrastructureV1alpha4AWSClusterTemplateList. # noqa: E501 :return: The metadata of this IoXK8sClusterInfrastructureV1alpha4AWSClusterTemplateList. # noqa: E501 :rtype: V1ListMeta """ return self._metadata @metadata.setter def metadata(self, metadata): """Sets the metadata of this IoXK8sClusterInfrastructureV1alpha4AWSClusterTemplateList. :param metadata: The metadata of this IoXK8sClusterInfrastructureV1alpha4AWSClusterTemplateList. # noqa: E501 :type: V1ListMeta """ self._metadata = metadata def to_dict(self): """Returns the model properties as a dict""" result = {} for attr, _ in six.iteritems(self.openapi_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 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, IoXK8sClusterInfrastructureV1alpha4AWSClusterTemplateList): return False return self.to_dict() == other.to_dict() def __ne__(self, other): """Returns true if both objects are not equal""" if not isinstance(other, IoXK8sClusterInfrastructureV1alpha4AWSClusterTemplateList): return True return self.to_dict() != other.to_dict()
kubernetes/client/models/io_xk8s_cluster_infrastructure_v1alpha4_aws_cluster_template_list.py
8,019
NOTE: This class is auto generated by OpenAPI Generator. Ref: https://openapi-generator.tech Do not edit the class manually. Returns true if both objects are equal IoXK8sClusterInfrastructureV1alpha4AWSClusterTemplateList - a model defined in OpenAPI Returns true if both objects are not equal For `print` and `pprint` Gets the api_version of this IoXK8sClusterInfrastructureV1alpha4AWSClusterTemplateList. # noqa: E501 APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources # noqa: E501 :return: The api_version of this IoXK8sClusterInfrastructureV1alpha4AWSClusterTemplateList. # noqa: E501 :rtype: str Sets the api_version of this IoXK8sClusterInfrastructureV1alpha4AWSClusterTemplateList. APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources # noqa: E501 :param api_version: The api_version of this IoXK8sClusterInfrastructureV1alpha4AWSClusterTemplateList. # noqa: E501 :type: str Gets the items of this IoXK8sClusterInfrastructureV1alpha4AWSClusterTemplateList. # noqa: E501 List of awsclustertemplates. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md # noqa: E501 :return: The items of this IoXK8sClusterInfrastructureV1alpha4AWSClusterTemplateList. # noqa: E501 :rtype: list[IoXK8sClusterInfrastructureV1alpha4AWSClusterTemplate] Sets the items of this IoXK8sClusterInfrastructureV1alpha4AWSClusterTemplateList. List of awsclustertemplates. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md # noqa: E501 :param items: The items of this IoXK8sClusterInfrastructureV1alpha4AWSClusterTemplateList. # noqa: E501 :type: list[IoXK8sClusterInfrastructureV1alpha4AWSClusterTemplate] Gets the kind of this IoXK8sClusterInfrastructureV1alpha4AWSClusterTemplateList. # noqa: E501 Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds # noqa: E501 :return: The kind of this IoXK8sClusterInfrastructureV1alpha4AWSClusterTemplateList. # noqa: E501 :rtype: str Sets the kind of this IoXK8sClusterInfrastructureV1alpha4AWSClusterTemplateList. Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds # noqa: E501 :param kind: The kind of this IoXK8sClusterInfrastructureV1alpha4AWSClusterTemplateList. # noqa: E501 :type: str Gets the metadata of this IoXK8sClusterInfrastructureV1alpha4AWSClusterTemplateList. # noqa: E501 :return: The metadata of this IoXK8sClusterInfrastructureV1alpha4AWSClusterTemplateList. # noqa: E501 :rtype: V1ListMeta Sets the metadata of this IoXK8sClusterInfrastructureV1alpha4AWSClusterTemplateList. :param metadata: The metadata of this IoXK8sClusterInfrastructureV1alpha4AWSClusterTemplateList. # noqa: E501 :type: V1ListMeta Returns the model properties as a dict Returns the string representation of the model Kubernetes No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator) # noqa: E501 The version of the OpenAPI document: v1.20.7 Generated by: https://openapi-generator.tech coding: utf-8 noqa: F401 noqa: E501 noqa: E501 noqa: E501 noqa: E501
4,000
en
0.528699
"""Test the submodule “batchelper.py”.""" import unittest import audiorename import helper class TestBatch(unittest.TestCase): def setUp(self): self.singles = helper.gen_file_list( ['album', 'compilation'], helper.get_testfile('files'), ) self.album_broken = helper.gen_file_list( ['01', '03', '05', '07', '09', '11'], helper.get_testfile('files', 'album_broken'), ) self.album_broken_all = helper.gen_file_list( ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11'], helper.get_testfile('files', 'album_broken'), ) self.album_complete = helper.gen_file_list( ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11'], helper.get_testfile('files', 'album_complete'), ) self.album_incomplete = helper.gen_file_list( ['01', '02', '04', '05', '06', '07', '09', '10', '11'], helper.get_testfile('files', 'album_incomplete'), ) self.album_small = helper.gen_file_list( ['01', '02', '03', '04', '05'], helper.get_testfile('files', 'album_small'), ) self.all = self.singles + \ self.album_broken_all + \ self.album_complete + \ self.album_incomplete + \ self.album_small def test_single(self): single = helper.get_testfile('files', 'album.mp3') with helper.Capturing() as output: audiorename.execute('--dry-run', '--verbose', single) self.assertEqual([single], helper.filter_source(output)) def test_folder_complete(self): with helper.Capturing() as output: audiorename.execute('--dry-run', '--verbose', helper.get_testfile('files')) self.assertEqual(self.all, helper.filter_source(output)) def test_folder_sub(self): with helper.Capturing() as output: audiorename.execute( '--dry-run', '--verbose', helper.get_testfile('files', 'album_complete') ) self.assertEqual(self.album_complete, helper.filter_source(output)) def test_album_min(self): with helper.Capturing() as output: audiorename.execute( '--dry-run', '--verbose', '--album-min', '7', helper.get_testfile('files') ) self.assertEqual(self.album_complete + self.album_incomplete, helper.filter_source(output)) def test_album_min_no_match(self): with helper.Capturing() as output: audiorename.execute( '--dry-run', '--verbose', '--album-min', '23', helper.get_testfile('files') ) self.assertEqual([], helper.filter_source(output)) def test_album_complete(self): with helper.Capturing() as output: audiorename.execute( '--dry-run', '--verbose', '--album-complete', helper.get_testfile('files') ) self.assertEqual( self.singles + self.album_complete + self.album_small, helper.filter_source(output) ) def test_filter_all(self): with helper.Capturing() as output: audiorename.execute( '--dry-run', '--verbose', '--album-min', '7', '--album-complete', helper.get_testfile('files') ) self.assertEqual(self.album_complete, helper.filter_source(output)) class TestExtension(unittest.TestCase): def setUp(self): self.test_files = helper.get_testfile('mixed_formats') def test_default(self): with helper.Capturing() as output: audiorename.execute( '--dry-run', '--verbose', self.test_files, ) self.assertEqual( helper.filter_source(output), helper.gen_file_list( ['01.flac', '02.m4a', '03.mp3'], self.test_files, extension=False ) ) def test_one(self): with helper.Capturing() as output: audiorename.execute( '--dry-run', '--verbose', '--extension', 'mp3,flac', self.test_files ) self.assertEqual( helper.filter_source(output), helper.gen_file_list( ['01.flac', '03.mp3'], self.test_files, extension=False ) ) def test_two(self): with helper.Capturing() as output: audiorename.execute( '--dry-run', '--verbose', '--extension', 'mp3', self.test_files ) self.assertEqual( helper.filter_source(output), helper.gen_file_list(['03.mp3'], self.test_files, extension=False) ) class TestSkip(unittest.TestCase): def setUp(self): self.file = helper.get_testfile('broken', 'binary.mp3') with helper.Capturing() as output: audiorename.execute('-d', '--verbose', self.file) self.output = helper.join(output) def test_message(self): self.assertTrue('Broken file' in self.output) def test_file_in_message(self): self.assertTrue('Broken file' in self.output) self.assertTrue(self.file in self.output) def test_continuation(self): path = helper.get_testfile('broken') with helper.Capturing() as output: audiorename.execute( '--dry-run', '--verbose', path ) output = helper.filter_source(output) self.assertTrue(output[1]) if __name__ == '__main__': unittest.main()
test/test_batch.py
6,038
Test the submodule “batchelper.py”.
35
en
0.140849
#!/usr/bin/env python from __future__ import print_function import unittest import sys import hashlib import os import numpy as np import cv2 import cv2.cv as cv # Python 3 moved urlopen to urllib.requests try: from urllib.request import urlopen except ImportError: from urllib import urlopen class OpenCVTests(unittest.TestCase): # path to local repository folder containing 'samples' folder repoPath = None # github repository url repoUrl = 'https://raw.github.com/opencv/opencv/2.4' # path to local folder containing 'camera_calibration.tar.gz' dataPath = None # data url dataUrl = 'http://docs.opencv.org/data' depths = [ cv.IPL_DEPTH_8U, cv.IPL_DEPTH_8S, cv.IPL_DEPTH_16U, cv.IPL_DEPTH_16S, cv.IPL_DEPTH_32S, cv.IPL_DEPTH_32F, cv.IPL_DEPTH_64F ] mat_types = [ cv.CV_8UC1, cv.CV_8UC2, cv.CV_8UC3, cv.CV_8UC4, cv.CV_8SC1, cv.CV_8SC2, cv.CV_8SC3, cv.CV_8SC4, cv.CV_16UC1, cv.CV_16UC2, cv.CV_16UC3, cv.CV_16UC4, cv.CV_16SC1, cv.CV_16SC2, cv.CV_16SC3, cv.CV_16SC4, cv.CV_32SC1, cv.CV_32SC2, cv.CV_32SC3, cv.CV_32SC4, cv.CV_32FC1, cv.CV_32FC2, cv.CV_32FC3, cv.CV_32FC4, cv.CV_64FC1, cv.CV_64FC2, cv.CV_64FC3, cv.CV_64FC4, ] mat_types_single = [ cv.CV_8UC1, cv.CV_8SC1, cv.CV_16UC1, cv.CV_16SC1, cv.CV_32SC1, cv.CV_32FC1, cv.CV_64FC1, ] def depthsize(self, d): return { cv.IPL_DEPTH_8U : 1, cv.IPL_DEPTH_8S : 1, cv.IPL_DEPTH_16U : 2, cv.IPL_DEPTH_16S : 2, cv.IPL_DEPTH_32S : 4, cv.IPL_DEPTH_32F : 4, cv.IPL_DEPTH_64F : 8 }[d] def get_sample(self, filename, iscolor = cv.CV_LOAD_IMAGE_COLOR): if not filename in self.image_cache: filedata = None if OpenCVTests.repoPath is not None: candidate = OpenCVTests.repoPath + '/' + filename if os.path.isfile(candidate): with open(candidate, 'rb') as f: filedata = f.read() if filedata is None: filedata = urllib.urlopen(OpenCVTests.repoUrl + '/' + filename).read() imagefiledata = cv.CreateMatHeader(1, len(filedata), cv.CV_8UC1) cv.SetData(imagefiledata, filedata, len(filedata)) self.image_cache[filename] = cv.DecodeImageM(imagefiledata, iscolor) return self.image_cache[filename] def get_data(self, filename, urlbase): if (not os.path.isfile(filename)): if OpenCVTests.dataPath is not None: candidate = OpenCVTests.dataPath + '/' + filename if os.path.isfile(candidate): return candidate urllib.urlretrieve(urlbase + '/' + filename, filename) return filename def setUp(self): self.image_cache = {} def snap(self, img): self.snapL([img]) def snapL(self, L): for i,img in enumerate(L): cv.NamedWindow("snap-%d" % i, 1) cv.ShowImage("snap-%d" % i, img) cv.WaitKey() cv.DestroyAllWindows() def hashimg(self, im): """ Compute a hash for an image, useful for image comparisons """ return hashlib.md5(im.tostring()).digest() class NewOpenCVTests(unittest.TestCase): # path to local repository folder containing 'samples' folder repoPath = None extraTestDataPath = None # github repository url repoUrl = 'https://raw.github.com/opencv/opencv/master' def get_sample(self, filename, iscolor = cv2.IMREAD_COLOR): if not filename in self.image_cache: filedata = None if NewOpenCVTests.repoPath is not None: candidate = NewOpenCVTests.repoPath + '/' + filename if os.path.isfile(candidate): with open(candidate, 'rb') as f: filedata = f.read() if NewOpenCVTests.extraTestDataPath is not None: candidate = NewOpenCVTests.extraTestDataPath + '/' + filename if os.path.isfile(candidate): with open(candidate, 'rb') as f: filedata = f.read() if filedata is None: return None#filedata = urlopen(NewOpenCVTests.repoUrl + '/' + filename).read() self.image_cache[filename] = cv2.imdecode(np.fromstring(filedata, dtype=np.uint8), iscolor) return self.image_cache[filename] def setUp(self): cv2.setRNGSeed(10) self.image_cache = {} def hashimg(self, im): """ Compute a hash for an image, useful for image comparisons """ return hashlib.md5(im.tostring()).hexdigest() if sys.version_info[:2] == (2, 6): def assertLess(self, a, b, msg=None): if not a < b: self.fail('%s not less than %s' % (repr(a), repr(b))) def assertLessEqual(self, a, b, msg=None): if not a <= b: self.fail('%s not less than or equal to %s' % (repr(a), repr(b))) def assertGreater(self, a, b, msg=None): if not a > b: self.fail('%s not greater than %s' % (repr(a), repr(b))) def intersectionRate(s1, s2): x1, y1, x2, y2 = s1 s1 = np.array([[x1, y1], [x2,y1], [x2, y2], [x1, y2]]) x1, y1, x2, y2 = s2 s2 = np.array([[x1, y1], [x2,y1], [x2, y2], [x1, y2]]) area, intersection = cv2.intersectConvexConvex(s1, s2) return 2 * area / (cv2.contourArea(s1) + cv2.contourArea(s2)) def isPointInRect(p, rect): if rect[0] <= p[0] and rect[1] <=p[1] and p[0] <= rect[2] and p[1] <= rect[3]: return True else: return False
modules/python/test/tests_common.py
5,926
Compute a hash for an image, useful for image comparisons Compute a hash for an image, useful for image comparisons !/usr/bin/env python Python 3 moved urlopen to urllib.requests path to local repository folder containing 'samples' folder github repository url path to local folder containing 'camera_calibration.tar.gz' data url path to local repository folder containing 'samples' folder github repository urlfiledata = urlopen(NewOpenCVTests.repoUrl + '/' + filename).read()
480
en
0.58978
# Copyright 2013-2020 Lawrence Livermore National Security, LLC and other # Spack Project Developers. See the top-level COPYRIGHT file for details. # # SPDX-License-Identifier: (Apache-2.0 OR MIT) from spack import * class PyPycmd(PythonPackage): """pycmd is a collection of command line tools for helping with Python development.""" homepage = "https://pypi.org/project/pycmd/" url = "https://pypi.io/packages/source/p/pycmd/pycmd-1.2.tar.gz" version('1.2', sha256='adc1976c0106919e9338db20102b91009256dcfec924a66928d7297026f72477') depends_on('py-py@1.4.9:', type=('build', 'run')) depends_on('py-setuptools', type='build')
var/spack/repos/builtin/packages/py-pycmd/package.py
665
pycmd is a collection of command line tools for helping with Python development. Copyright 2013-2020 Lawrence Livermore National Security, LLC and other Spack Project Developers. See the top-level COPYRIGHT file for details. SPDX-License-Identifier: (Apache-2.0 OR MIT)
271
en
0.776454
#!/usr/bin/env python3 # Copyright (c) Facebook, Inc. and its affiliates. # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. """ Generates a dictionary file from the training data. ## Examples ```bash # learn the vocabulary from one task, then train on another task. parlai build_dict -t convai2 --dict-file premade.dict parlai train_model -t squad --dict-file premade.dict -m seq2seq ``` """ from parlai.core.dict import DictionaryAgent from parlai.core.params import ParlaiParser, str2class from parlai.core.worlds import create_task from parlai.utils.misc import TimeLogger from parlai.utils.distributed import is_distributed from parlai.core.script import ParlaiScript, register_script from parlai.utils.io import PathManager import parlai.utils.logging as logging import copy import tqdm def setup_args(parser=None, hidden=True): if parser is None: parser = ParlaiParser(True, True, 'Build a dictionary.') dict_loop = parser.add_argument_group('Dictionary Loop Arguments') dict_loop.add_argument( '--dict-maxexs', default=-1, type=int, help='max number of examples to build dict on', hidden=hidden, ) dict_loop.add_argument( '--dict-include-valid', default=False, type='bool', help='Include validation set in dictionary building ' 'for task.', hidden=hidden, ) dict_loop.add_argument( '--dict-include-test', default=False, type='bool', help='Include test set in dictionary building for task.', hidden=hidden, ) dict_loop.add_argument( '-ltim', '--log-every-n-secs', type=float, default=10, hidden=hidden ) DictionaryAgent.add_cmdline_args(parser) return parser def build_dict(opt, skip_if_built=False): if isinstance(opt, ParlaiParser): logging.error('Should be passed opt not Parser') opt = opt.parse_args() if not opt.get('dict_file'): logging.error( 'Tried to build dictionary but `--dict-file` is not set. Set ' 'this param so the dictionary can be saved.' ) return if skip_if_built and PathManager.exists(opt['dict_file']): # Dictionary already built, skip all loading or setup logging.debug("dictionary already built.") return None if opt.get('dict_class'): # Custom dictionary class dictionary = str2class(opt['dict_class'])(opt) else: # Default dictionary class dictionary = DictionaryAgent(opt) if PathManager.exists(opt['dict_file']) or ( hasattr(dictionary, 'is_prebuilt') and dictionary.is_prebuilt() ): # Dictionary already built, return loaded dictionary agent logging.debug("dictionary already built.") return dictionary if is_distributed(): raise ValueError('Dictionaries should be pre-built before distributed train.') ordered_opt = copy.deepcopy(opt) cnt = 0 # we use train set to build dictionary ordered_opt['batchsize'] = 1 # Set this to none so that image features are not calculated when Teacher is # instantiated while building the dict ordered_opt['image_mode'] = 'no_image_model' ordered_opt.log() datatypes = ['train:ordered:stream'] if opt.get('dict_include_valid'): datatypes.append('valid:stream') if opt.get('dict_include_test'): datatypes.append('test:stream') cnt = 0 for dt in datatypes: ordered_opt['datatype'] = dt world_dict = create_task(ordered_opt, dictionary) # pass examples to dictionary log_time = TimeLogger() total = world_dict.num_examples() if opt['dict_maxexs'] >= 0: total = min(total, opt['dict_maxexs']) log_every_n_secs = opt.get('log_every_n_secs', None) if log_every_n_secs: pbar = tqdm.tqdm( total=total, desc='Building dictionary', unit='ex', unit_scale=True ) else: pbar = None while not world_dict.epoch_done(): cnt += 1 if cnt > opt['dict_maxexs'] and opt['dict_maxexs'] >= 0: logging.info('Processed {} exs, moving on.'.format(opt['dict_maxexs'])) # don't wait too long... break world_dict.parley() if pbar: pbar.update(1) if pbar: pbar.close() dictionary.save(opt['dict_file'], sort=True) logging.info( f'dictionary built with {len(dictionary)} tokens ' f'in {log_time.total_time():.1f}s' ) return dictionary @register_script('build_dict', hidden=True) class BuildDict(ParlaiScript): @classmethod def setup_args(cls): return setup_args(hidden=False) def run(self): return build_dict(self.opt) if __name__ == '__main__': BuildDict.main()
parlai/scripts/build_dict.py
4,983
Generates a dictionary file from the training data. ## Examples ```bash # learn the vocabulary from one task, then train on another task. parlai build_dict -t convai2 --dict-file premade.dict parlai train_model -t squad --dict-file premade.dict -m seq2seq ``` !/usr/bin/env python3 Copyright (c) Facebook, Inc. and its affiliates. This source code is licensed under the MIT license found in the LICENSE file in the root directory of this source tree. Dictionary already built, skip all loading or setup Custom dictionary class Default dictionary class Dictionary already built, return loaded dictionary agent we use train set to build dictionary Set this to none so that image features are not calculated when Teacher is instantiated while building the dict pass examples to dictionary don't wait too long...
811
en
0.790841
import pytest from lcs import Perception from lcs.agents.acs2 import Configuration, ClassifiersList, \ Classifier class TestClassifierList: @pytest.fixture def cfg(self): return Configuration(8, 8) def test_should_deny_insertion_illegal_types(self, cfg): population = ClassifiersList() with pytest.raises(TypeError): # Try to insert an integer instead of classifier object population.append(4) def test_should_insert_classifier(self, cfg): # given population = ClassifiersList() cl = Classifier(cfg=cfg) # when population.append(cl) # then assert len(population) == 1 def test_should_form_match_set(self, cfg): # given cl_1 = Classifier(cfg=cfg) cl_2 = Classifier(condition='1###0###', cfg=cfg) cl_3 = Classifier(condition='0###1###', cfg=cfg) population = ClassifiersList(*[cl_1, cl_2, cl_3]) p0 = Perception('11110000') # when match_set = ClassifiersList.form_match_set(population, p0) # then assert len(match_set) == 2 assert cl_1 in match_set assert cl_2 in match_set def test_should_form_action_set(self, cfg): # given cl_1 = Classifier(action=0, cfg=cfg) cl_2 = Classifier(action=0, cfg=cfg) cl_3 = Classifier(action=1, cfg=cfg) population = ClassifiersList(*[cl_1, cl_2, cl_3]) action = 0 # when action_set = ClassifiersList.form_action_set(population, action) # then assert len(action_set) == 2 assert cl_1 in action_set assert cl_2 in action_set def test_should_expand(self, cfg): # given cl_1 = Classifier(action=0, cfg=cfg) cl_2 = Classifier(action=1, numerosity=2, cfg=cfg) cl_3 = Classifier(action=2, numerosity=3, cfg=cfg) population = ClassifiersList(*[cl_1, cl_2, cl_3]) # when expanded = population.expand() # then assert len(expanded) == 6 assert cl_1 in expanded assert cl_2 in expanded assert cl_3 in expanded def test_should_calculate_maximum_fitness(self, cfg): # given population = ClassifiersList() # when & then # C1 - does not anticipate change c1 = Classifier(cfg=cfg) population.append(c1) assert 0.0 == population.get_maximum_fitness() # when & then # C2 - does anticipate some change c2 = Classifier(effect='1###0###', reward=0.25, cfg=cfg) population.append(c2) assert 0.125 == population.get_maximum_fitness() # when & then # C3 - does anticipate change and is quite good c3 = Classifier(effect='1#######', quality=0.8, reward=5, cfg=cfg) population.append(c3) assert 4 == population.get_maximum_fitness() def test_should_apply_reinforcement_learning(self, cfg): # given cl = Classifier(reward=34.29, immediate_reward=11.29, cfg=cfg) population = ClassifiersList(*[cl]) # when ClassifiersList.apply_reinforcement_learning( population, 0, 28.79, cfg.beta, cfg.gamma) # then assert abs(33.94 - cl.r) < 0.1 assert abs(10.74 - cl.ir) < 0.1 def test_should_form_match_set_backwards(self, cfg): # given population = ClassifiersList() situation = Perception('11110000') # C1 - general condition c1 = Classifier(cfg=cfg) # C2 - matching c2 = Classifier(condition='0##0####', effect='1##1####', cfg=cfg) # C3 - non-matching c3 = Classifier(condition='0###1###', effect='1######0', cfg=cfg) # C4 - non-matching c4 = Classifier(condition='0###0###', effect='1###1###', cfg=cfg) population.append(c1) population.append(c2) population.append(c3) population.append(c4) # when match_set = ClassifiersList.form_match_set_backwards(population, situation) # then assert 2 == len(match_set) assert c1 in match_set assert c2 in match_set
tests/lcs/agents/acs2/test_ClassifierList.py
4,381
Try to insert an integer instead of classifier object given when then given when then given when then given when then given when & then C1 - does not anticipate change when & then C2 - does anticipate some change when & then C3 - does anticipate change and is quite good given when then given C1 - general condition C2 - matching C3 - non-matching C4 - non-matching when then
375
en
0.709863
# Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. import csv import os import numpy as np import PIL.Image class ObjectType: Dontcare, Car, Van, Truck, Bus, Pickup, VehicleWithTrailer, SpecialVehicle,\ Person, Person_fa, Person_unsure, People, Cyclist, Tram, Person_Sitting,\ Misc = range(16) def __init__(self): pass class Bbox: def __init__(self, x_left=0, y_top=0, x_right=0, y_bottom=0): self.xl = x_left self.yt = y_top self.xr = x_right self.yb = y_bottom def area(self): return (self.xr - self.xl) * (self.yb - self.yt) def width(self): return self.xr - self.xl def height(self): return self.yb - self.yt def get_array(self): return [self.xl, self.yt, self.xr, self.yb] class GroundTruthObj: """ This class is the data ground-truth #Values Name Description ---------------------------------------------------------------------------- 1 type Class ID 1 truncated Float from 0 (non-truncated) to 1 (truncated), where truncated refers to the object leaving image boundaries. -1 corresponds to a don't care region. 1 occluded Integer (-1,0,1,2) indicating occlusion state: -1 = unknown, 0 = fully visible, 1 = partly occluded, 2 = largely occluded 1 alpha Observation angle of object, ranging [-pi..pi] 4 bbox 2D bounding box of object in the image (0-based index): contains left, top, right, bottom pixel coordinates 3 dimensions 3D object dimensions: height, width, length (in meters) 3 location 3D object location x,y,z in camera coordinates (in meters) 1 rotation_y Rotation ry around Y-axis in camera coordinates [-pi..pi] 1 score Only for results: Float, indicating confidence in detection, needed for p/r curves, higher is better. Here, 'DontCare' labels denote regions in which objects have not been labeled, for example because they have been too far away from the laser scanner. """ # default class mappings OBJECT_TYPES = { 'bus': ObjectType.Bus, 'car': ObjectType.Car, 'cyclist': ObjectType.Cyclist, 'pedestrian': ObjectType.Person, 'people': ObjectType.People, 'person': ObjectType.Person, 'person_sitting': ObjectType.Person_Sitting, 'person-fa': ObjectType.Person_fa, 'person?': ObjectType.Person_unsure, 'pickup': ObjectType.Pickup, 'misc': ObjectType.Misc, 'special-vehicle': ObjectType.SpecialVehicle, 'tram': ObjectType.Tram, 'truck': ObjectType.Truck, 'van': ObjectType.Van, 'vehicle-with-trailer': ObjectType.VehicleWithTrailer} def __init__(self): self.stype = '' self.truncated = 0 self.occlusion = 0 self.angle = 0 self.height = 0 self.width = 0 self.length = 0 self.locx = 0 self.locy = 0 self.locz = 0 self.roty = 0 self.bbox = Bbox() self.object = ObjectType.Dontcare @classmethod def lmdb_format_length(cls): """ width of an LMDB datafield returned by the gt_to_lmdb_format function. :return: """ return 16 def gt_to_lmdb_format(self): """ For storage of a bbox ground truth object into a float32 LMDB. Sort-by attribute is always the last value in the array. """ result = [ # bbox in x,y,w,h format: self.bbox.xl, self.bbox.yt, self.bbox.xr - self.bbox.xl, self.bbox.yb - self.bbox.yt, # alpha angle: self.angle, # class number: self.object, 0, # Y axis rotation: self.roty, # bounding box attributes: self.truncated, self.occlusion, # object dimensions: self.length, self.width, self.height, self.locx, self.locy, # depth (sort-by attribute): self.locz, ] assert(len(result) is self.lmdb_format_length()) return result def set_type(self): self.object = self.OBJECT_TYPES.get(self.stype, ObjectType.Dontcare) class GroundTruth: """ this class loads the ground truth """ def __init__(self, label_dir, label_ext='.txt', label_delimiter=' ', min_box_size=None, class_mappings=None): self.label_dir = label_dir self.label_ext = label_ext # extension of label files self.label_delimiter = label_delimiter # space is used as delimiter in label files self._objects_all = dict() # positive bboxes across images self.min_box_size = min_box_size if class_mappings is not None: GroundTruthObj.OBJECT_TYPES = class_mappings def update_objects_all(self, _key, _bboxes): if _bboxes: self._objects_all[_key] = _bboxes else: self._objects_all[_key] = [] def load_gt_obj(self): """ load bbox ground truth from files either via the provided label directory or list of label files""" files = os.listdir(self.label_dir) files = list(filter(lambda x: x.endswith(self.label_ext), files)) if len(files) == 0: raise RuntimeError('error: no label files found in %s' % self.label_dir) for label_file in files: objects_per_image = list() with open(os.path.join(self.label_dir, label_file), 'rt') as flabel: for row in csv.reader(flabel, delimiter=self.label_delimiter): if len(row) == 0: # This can happen when you open an empty file continue if len(row) < 15: raise ValueError('Invalid label format in "%s"' % os.path.join(self.label_dir, label_file)) # load data gt = GroundTruthObj() gt.stype = row[0].lower() gt.truncated = float(row[1]) gt.occlusion = int(row[2]) gt.angle = float(row[3]) gt.bbox.xl = float(row[4]) gt.bbox.yt = float(row[5]) gt.bbox.xr = float(row[6]) gt.bbox.yb = float(row[7]) gt.height = float(row[8]) gt.width = float(row[9]) gt.length = float(row[10]) gt.locx = float(row[11]) gt.locy = float(row[12]) gt.locz = float(row[13]) gt.roty = float(row[14]) gt.set_type() box_dimensions = [gt.bbox.xr - gt.bbox.xl, gt.bbox.yb - gt.bbox.yt] if self.min_box_size is not None: if not all(x >= self.min_box_size for x in box_dimensions): # object is smaller than threshold => set to "DontCare" gt.stype = '' gt.object = ObjectType.Dontcare objects_per_image.append(gt) key = os.path.splitext(label_file)[0] self.update_objects_all(key, objects_per_image) @property def objects_all(self): return self._objects_all # return the # of pixels remaining in a def pad_bbox(arr, max_bboxes=64, bbox_width=16): if arr.shape[0] > max_bboxes: raise ValueError( 'Too many bounding boxes (%d > %d)' % arr.shape[0], max_bboxes ) # fill remainder with zeroes: data = np.zeros((max_bboxes + 1, bbox_width), dtype='float') # number of bounding boxes: data[0][0] = arr.shape[0] # width of a bounding box: data[0][1] = bbox_width # bounding box data. Merge nothing if no bounding boxes exist. if arr.shape[0] > 0: data[1:1 + arr.shape[0]] = arr return data def bbox_to_array(arr, label=0, max_bboxes=64, bbox_width=16): """ Converts a 1-dimensional bbox array to an image-like 3-dimensional array CHW array """ arr = pad_bbox(arr, max_bboxes, bbox_width) return arr[np.newaxis, :, :] def bbox_overlap(abox, bbox): # the abox box x11 = abox[0] y11 = abox[1] x12 = abox[0] + abox[2] - 1 y12 = abox[1] + abox[3] - 1 # the closer box x21 = bbox[0] y21 = bbox[1] x22 = bbox[0] + bbox[2] - 1 y22 = bbox[1] + bbox[3] - 1 overlap_box_x2 = min(x12, x22) overlap_box_x1 = max(x11, x21) overlap_box_y2 = min(y12, y22) overlap_box_y1 = max(y11, y21) # make sure we preserve any non-bbox components overlap_box = list(bbox) overlap_box[0] = overlap_box_x1 overlap_box[1] = overlap_box_y1 overlap_box[2] = overlap_box_x2 - overlap_box_x1 + 1 overlap_box[3] = overlap_box_y2 - overlap_box_y1 + 1 xoverlap = max(0, overlap_box_x2 - overlap_box_x1) yoverlap = max(0, overlap_box_y2 - overlap_box_y1) overlap_pix = xoverlap * yoverlap return overlap_pix, overlap_box def pad_image(img, padding_image_height, padding_image_width): """ pad a single image to the specified dimensions """ src_width = img.size[0] src_height = img.size[1] if padding_image_width < src_width: raise ValueError("Source image width %d is greater than padding width %d" % (src_width, padding_image_width)) if padding_image_height < src_height: raise ValueError("Source image height %d is greater than padding height %d" % (src_height, padding_image_height)) padded_img = PIL.Image.new( img.mode, (padding_image_width, padding_image_height), "black") padded_img.paste(img, (0, 0)) # copy to top-left corner return padded_img def resize_bbox_list(bboxlist, rescale_x=1, rescale_y=1): # this is expecting x1,y1,w,h: bboxListNew = [] for bbox in bboxlist: abox = bbox abox[0] *= rescale_x abox[1] *= rescale_y abox[2] *= rescale_x abox[3] *= rescale_y bboxListNew.append(abox) return bboxListNew
digits/extensions/data/objectDetection/utils.py
10,639
this class loads the ground truth This class is the data ground-truth #Values Name Description ---------------------------------------------------------------------------- 1 type Class ID 1 truncated Float from 0 (non-truncated) to 1 (truncated), where truncated refers to the object leaving image boundaries. -1 corresponds to a don't care region. 1 occluded Integer (-1,0,1,2) indicating occlusion state: -1 = unknown, 0 = fully visible, 1 = partly occluded, 2 = largely occluded 1 alpha Observation angle of object, ranging [-pi..pi] 4 bbox 2D bounding box of object in the image (0-based index): contains left, top, right, bottom pixel coordinates 3 dimensions 3D object dimensions: height, width, length (in meters) 3 location 3D object location x,y,z in camera coordinates (in meters) 1 rotation_y Rotation ry around Y-axis in camera coordinates [-pi..pi] 1 score Only for results: Float, indicating confidence in detection, needed for p/r curves, higher is better. Here, 'DontCare' labels denote regions in which objects have not been labeled, for example because they have been too far away from the laser scanner. Converts a 1-dimensional bbox array to an image-like 3-dimensional array CHW array For storage of a bbox ground truth object into a float32 LMDB. Sort-by attribute is always the last value in the array. width of an LMDB datafield returned by the gt_to_lmdb_format function. :return: load bbox ground truth from files either via the provided label directory or list of label files pad a single image to the specified dimensions Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. default class mappings bbox in x,y,w,h format: alpha angle: class number: Y axis rotation: bounding box attributes: object dimensions: depth (sort-by attribute): extension of label files space is used as delimiter in label files positive bboxes across images This can happen when you open an empty file load data object is smaller than threshold => set to "DontCare" return the of pixels remaining in a fill remainder with zeroes: number of bounding boxes: width of a bounding box: bounding box data. Merge nothing if no bounding boxes exist. the abox box the closer box make sure we preserve any non-bbox components copy to top-left corner this is expecting x1,y1,w,h:
2,480
en
0.792729
# -*- coding: utf-8 -*- """ This Python module provides various service functions. Updated since version 1.1: 1. Added support for postprocess and visualization. 2. Added file path validation for parameters of all related methods. Updated since version 1.2: Merge Code and Update GUI 1. Integrate New Nemoh using hdf5 and python. """ __author__ = "caoweiquan322, TCSASSEMBLER" __copyright__ = "Copyright (C) 2014-2015 TopCoder Inc. All rights reserved." __version__ = "1.2" import collections import uuid from settings import * import os import time import subprocess from multiprocessing import Process, Manager import logging from openwarp import helper from nemoh import utility from nemoh import preprocessor from nemoh import postprocessor from nemoh import solver import warnings import fnmatch import h5py # This class represents parameters used in the meshing process. # This class is a subclass of "tuple", and is created using collections.namedtuple factory function. MeshingParameters = collections.namedtuple('MeshingParameters', 'infile outfile maxh minh fineness grading usetolerance tolerance') # This class represents parameters used in the simulation process. # This class is a subclass of "tuple", and is created using collections.namedtuple factory function. SimulationParameters = collections.namedtuple('SimulationParameters', 'rho g depth xeff yeff wave_frequencies ' + 'min_wave_frequencies max_wave_frequencies wave_directions ' + 'max_wave_direction min_wave_directions floating_bodies ' + 'indiq_solver ires tol_gmres max_iterations save_potential ' + 'green_tabulation_numx green_tabulation_numz ' + 'green_tabulation_simpson_npoints use_ode_influence_coefficients ' + 'use_higher_order num_panel_higher_order b_spline_order ' + 'use_dipoles_implementation thin_panels compute_drift_forces ' + 'compute_yaw_moment remove_irregular_frequencies') # This class represents a floating body used in the SimulationParameters. # This class is a subclass of "tuple", and is created using collections.namedtuple factory function. FloatingBody = collections.namedtuple('FloatingBody', 'mesh_file points panels degrees_of_freedom surge sway ' + 'heave roll_about_cdg pitch_about_cdg yaw_about_cdg ' + 'resulting_generalised_forces force_in_x_direction force_in_y_direction ' + 'force_in_z_direction moment_cdg_force_in_x_direction ' + 'moment_cdg_force_in_y_direction moment_cdg_force_in_z_direction ' + 'additional_info_lines') # This class represents parameters used in the post-proessing. # This class is a subclass of "tuple", and is created using collections.namedtuple factory function. PostprocessingParameters = collections.namedtuple('PostprocessingParameters', 'irf show_pressure ' + 'kochin_function free_surface_elevation') # The pre-defined config file name used by MESH_GENERATOR_BIN. _CONFIG_FILE_NAME = 'config.txt' # The pre-defined stdout log file name. _LOG_FILE_NAME = 'log.txt' # The logger object for logging. _LOGGER = logging.getLogger(__name__) class ServiceError(Exception): ''' This exception indicates a service error. It will be raised by methods of this module. ''' pass def generate_mesh(meshing_dir, params): ''' Launch Mesh Generator to generate mesh. @param meshing_dir: the meshing directory @param params: the meshing parameters @return: the mesh generation log content @raise TypeError: if any input parameter is not of required type @raise ValueError: if any input parameter is None/empty, or any field of MeshingParameters is not of valid value @raise ServiceError: if error occurs during generating mesh ''' signature = __name__ + '.generate_mesh()' helper.log_entrance(_LOGGER, signature, {'meshing_dir': meshing_dir, 'params': params}) # Checking parameters helper.check_not_none_nor_empty(meshing_dir, 'meshing_dir') helper.check_is_directory(meshing_dir, 'meshing_dir') helper.check_type_value(params, 'params', MeshingParameters, False) helper.check_not_none_nor_empty(params.infile, 'params.infile') helper.check_is_file(params.infile, 'params.infile') helper.check_not_none_nor_empty(params.outfile, 'params.outfile') helper.check_not_none_nor_empty(params.maxh, 'params.maxh') helper.check_not_none_nor_empty(params.minh, 'params.minh') helper.check_not_none_nor_empty(params.fineness, 'params.fineness') helper.check_not_none_nor_empty(params.grading, 'params.grading') helper.check_not_none_nor_empty(params.usetolerance, 'params.usetolerance') if params.usetolerance == '1': helper.check_not_none_nor_empty(params.tolerance, 'params.tolerance') try: config_file_path = os.path.join(meshing_dir, _CONFIG_FILE_NAME) log_file_path = os.path.join(meshing_dir, _LOG_FILE_NAME) # Generate config.txt according to given parameters with open(config_file_path, 'w') as f: f.write('\n'.join("%s: %s" % item for item in vars(params).items() if item[1] is not None)) # Launch mesh generator with open(log_file_path, 'w') as log_file: _LOGGER.debug('Start mesh generator in subprocess.') subprocess.call(MESH_GENERATOR_BIN, cwd=meshing_dir, stdout=log_file) _LOGGER.debug('End mesh generator in subprocess.') # Read and return the log file content with open(log_file_path, 'r') as log_file: ret = log_file.read() helper.log_exit(_LOGGER, signature, [ret]) return ret except Exception as e: helper.log_exception(_LOGGER, signature, e) raise ServiceError('Error occurs when generating mesh. Caused by:\n' + unicode(str(e))) def simulate(simulation_dir, params): ''' Run simulation. @param simulation_dir: the simulation directory @param params: the simulation parameters @return: the simulation log content @raise TypeError: if any input parameter is not of required type @raise ValueError: if any input parameter is None/empty, or any field of SimulationParameters is not of valid value @raise ServiceError: if any other error occurred when launching the simulation ''' signature = __name__ + '.simulate()' helper.log_entrance(_LOGGER, signature, {'simulation_dir': simulation_dir, 'params': params}) # Checking parameters helper.check_not_none_nor_empty(simulation_dir, 'simulation_dir') helper.check_is_directory(simulation_dir, 'simulation_dir') helper.check_type_value(params, 'params', SimulationParameters, False) helper.check_not_none_nor_empty(params.rho, 'params.rho') helper.check_not_none_nor_empty(params.g, 'params.g') helper.check_not_none_nor_empty(params.depth, 'params.depth') helper.check_not_none_nor_empty(params.xeff, 'params.xeff') helper.check_not_none_nor_empty(params.yeff, 'params.yeff') helper.check_not_none_nor_empty(params.wave_frequencies, 'params.wave_frequencies') helper.check_not_none_nor_empty(params.min_wave_frequencies, 'params.min_wave_frequencies') helper.check_not_none_nor_empty(params.max_wave_frequencies, 'params.max_wave_frequencies') helper.check_not_none_nor_empty(params.wave_directions, 'params.wave_directions') helper.check_not_none_nor_empty(params.min_wave_directions, 'params.min_wave_directions') helper.check_not_none_nor_empty(params.max_wave_direction, 'params.max_wave_direction') helper.check_not_none_nor_empty(params.indiq_solver, 'params.indiq_solver') helper.check_not_none_nor_empty(params.ires, 'params.ires') helper.check_not_none_nor_empty(params.tol_gmres, 'params.tol_gmres') helper.check_not_none_nor_empty(params.max_iterations, 'params.max_iterations') helper.check_not_none_nor_empty(params.save_potential, 'params.save_potential') helper.check_not_none_nor_empty(params.green_tabulation_numx, 'params.green_tabulation_numx') helper.check_not_none_nor_empty(params.green_tabulation_numz, 'params.green_tabulation_numz') helper.check_not_none_nor_empty(params.green_tabulation_simpson_npoints, 'params.green_tabulation_simpson_npoints') helper.check_not_none_nor_empty(params.use_ode_influence_coefficients, 'params.use_ode_influence_coefficients') helper.check_not_none_nor_empty(params.use_higher_order, 'params.use_higher_order') helper.check_not_none_nor_empty(params.num_panel_higher_order, 'params.num_panel_higher_order') helper.check_not_none_nor_empty(params.b_spline_order, 'params.b_spline_order') helper.check_not_none_nor_empty(params.use_dipoles_implementation, 'params.use_dipoles_implementation') helper.check_not_none_nor_empty(params.thin_panels, 'params.thin_panels') helper.check_not_none_nor_empty(params.compute_drift_forces, 'params.compute_drift_forces') helper.check_not_none_nor_empty(params.remove_irregular_frequencies, 'params.remove_irregular_frequencies') helper.check_not_none_nor_empty(params.compute_yaw_moment, 'params.compute_yaw_moment') helper.check_type_value(params.floating_bodies, 'params.floating_bodies', list, True) if params.floating_bodies is not None: for body in params.floating_bodies: helper.check_type_value(body, 'params.floating_bodies item', FloatingBody, False) helper.check_not_none_nor_empty(body.mesh_file, 'body.mesh_file') helper.check_not_none_nor_empty(body.points, 'body.points') helper.check_not_none_nor_empty(body.panels, 'body.panels') helper.check_not_none_nor_empty(body.degrees_of_freedom, 'body.degrees_of_freedom') helper.check_not_none_nor_empty(body.resulting_generalised_forces, 'body.resulting_generalised_forces') helper.check_not_none_nor_empty(body.additional_info_lines, 'body.additional_info_lines') try: # Write the hdf5 inputs according to given parameters with h5py.File(os.path.join(simulation_dir, 'db.hdf5'), "a") as hdf5_data: utility.write_calculations(params, hdf5_data) # Launch preProcessor and Solver # A prepared 'results' folder is necessary for the Nemoh software suite os.mkdir(os.path.join(simulation_dir, 'results')) simulation_log_path = os.path.join(simulation_dir, 'simulation_log.txt') custom_config = { 'HDF5_FILE': os.path.join(simulation_dir, 'db.hdf5'), 'NEMOH_CALCULATIONS_FILE': None, 'NEMOH_INPUT_FILE': None, 'MESH_TEC_FILE': os.path.join(simulation_dir, 'mesh', 'mesh.tec'), 'FK_FORCE_TEC_FILE': os.path.join(simulation_dir, 'results', 'fkforce.tec'), 'RADIATION_COEFFICIENTS_TEC_FILE': os.path.join(simulation_dir, 'results', 'radiationcoefficients.tec'), 'DIFFRACTION_FORCE_TEC_FILE': os.path.join(simulation_dir, 'results', 'diffractionforce.tec'), 'EXCITATION_FORCE_TEC_FILE': os.path.join(simulation_dir, 'results', 'excitationforce.tec'), 'IRF_TEC_FILE': os.path.join(simulation_dir, 'results', 'irf.tec'), 'WAVE_FIELD_TEC_FILE': os.path.join(simulation_dir, 'results', 'WaveField.tec'), 'GREEN_TABULATION_NUMX' : int(params.green_tabulation_numx), 'GREEN_TABULATION_NUMZ' : int(params.green_tabulation_numz), 'GREEN_TABULATION_SIMPSON_NPOINTS' : int(params.green_tabulation_simpson_npoints), 'USE_ODE_INFLUENCE_COEFFICIENTS': bool(int(params.use_ode_influence_coefficients)), 'USE_HIGHER_ORDER' : bool(int(params.use_higher_order)), 'NUM_PANEL_HIGHER_ORDER' : int(params.num_panel_higher_order), 'B_SPLINE_ORDER': int(params.b_spline_order), 'USE_DIPOLES_IMPLEMENTATION': bool(int(params.use_dipoles_implementation)), 'THIN_PANELS': [int(i) for i in params.thin_panels.split()], 'COMPUTE_DRIFT_FORCES' : bool(int(params.compute_drift_forces)), 'COMPUTE_YAW_MOMENT': bool(int(params.compute_yaw_moment)), 'REMOVE_IRREGULAR_FREQUENCIES' : bool(int(params.remove_irregular_frequencies)) } _LOGGER.debug('Start preProcessor function.') run_thread(preprocessor.preprocess, (custom_config,), simulation_log_path) _LOGGER.debug('End preProcessor function.') _LOGGER.debug('Start solver function.') output = run_thread(solver.solve, (custom_config,), None) with open(simulation_log_path, 'a') as log_file: log_file.write(output) _LOGGER.debug('End solver function.') with open(simulation_log_path, 'r') as log_file: ret = log_file.read() helper.log_exit(_LOGGER, signature, [ret]) return ret except Exception as e: helper.log_exception(_LOGGER, signature, e) raise ServiceError('Error occurs when doing simulation. Caused by:\n' + unicode(str(e))) def postprocess(simulation_dir, params): ''' Run post-processing. @param simulation_dir: the simulation directory @param params: the post-processing parameters @return: the post-processing log content @raise TypeError: if any input parameter is not of required type @raise ValueError: if any input parameter is None/empty, or any field of PostprocessingParameters is not of valid value @raise ServiceError: if error occurs during launching the post-processing ''' signature = __name__ + '.postprocess()' helper.log_entrance(_LOGGER, signature, {'simulation_dir': simulation_dir, 'params': params}) # Checking parameters helper.check_not_none_nor_empty(simulation_dir, 'simulation_dir') helper.check_is_directory(simulation_dir, 'simulation_dir') helper.check_type_value(params, 'params', PostprocessingParameters, False) helper.check_type_value(params.irf, 'params.irf', list, False) for irf_item in params.irf: helper.check_not_none_nor_empty(irf_item, 'irf_item') helper.check_not_none_nor_empty(params.show_pressure, 'params.show_pressure') helper.check_type_value(params.kochin_function, 'params.kochin_function', list, False) for kochin_function_item in params.kochin_function: helper.check_not_none_nor_empty(kochin_function_item, 'kochin_function_item') helper.check_type_value(params.free_surface_elevation, 'params.free_surface_elevation', list, False) for elevation_item in params.free_surface_elevation: helper.check_not_none_nor_empty(elevation_item, 'elevation_item') try: with h5py.File(os.path.join(simulation_dir, 'db.hdf5'), "a") as hdf5_data: utility.write_postprocessing_section(params, hdf5_data) # Launch postProcessor postprocessing_log_path = os.path.join(simulation_dir, 'postprocessing_log.txt') custom_config = { 'HDF5_FILE': os.path.join(simulation_dir, 'db.hdf5'), 'NEMOH_CALCULATIONS_FILE': None, 'NEMOH_INPUT_FILE': None, 'MESH_TEC_FILE': os.path.join(simulation_dir, 'mesh', 'mesh.tec'), 'FK_FORCE_TEC_FILE': os.path.join(simulation_dir, 'results', 'fkforce.tec'), 'RADIATION_COEFFICIENTS_TEC_FILE': os.path.join(simulation_dir, 'results', 'radiationcoefficients.tec'), 'DIFFRACTION_FORCE_TEC_FILE': os.path.join(simulation_dir, 'results', 'diffractionforce.tec'), 'EXCITATION_FORCE_TEC_FILE': os.path.join(simulation_dir, 'results', 'excitationforce.tec'), 'IRF_TEC_FILE': os.path.join(simulation_dir, 'results', 'irf.tec'), 'WAVE_FIELD_TEC_FILE': os.path.join(simulation_dir, 'results', 'WaveField.tec'), 'GREEN_TABULATION_NUMX' : 328, 'GREEN_TABULATION_NUMZ' : 46, 'GREEN_TABULATION_SIMPSON_NPOINTS' : 251, 'USE_ODE_INFLUENCE_COEFFICIENTS': False, 'USE_HIGHER_ORDER' : False, 'NUM_PANEL_HIGHER_ORDER' : 1, 'B_SPLINE_ORDER': 1, 'USE_DIPOLES_IMPLEMENTATION': False, 'THIN_PANELS': [-1], 'COMPUTE_DRIFT_FORCES' : False, 'COMPUTE_YAW_MOMENT': False, 'REMOVE_IRREGULAR_FREQUENCIES' : False } _LOGGER.debug('Start postProcessor function.') run_thread(postprocessor.postprocess, (custom_config,), postprocessing_log_path) _LOGGER.debug('End postProcessor in subprocess.') with open(postprocessing_log_path, 'r') as log_file: ret = log_file.read() helper.log_exit(_LOGGER, signature, [ret]) return ret except Exception as e: helper.log_exception(_LOGGER, signature, e) raise ServiceError('Error occurs when running postprocess. Caused by:\n' + unicode(str(e))) def visualize(simulation_dir): ''' Launch ParaView to visualize simulation results. @param simulation_dir: the simulation directory @raise TypeError: if any input parameter is not of required type @raise ValueError: if any input parameter is None/empty @raise ServiceError: if error occurs during launching the ParaView ''' signature = __name__ + '.visualize()' helper.log_entrance(_LOGGER, signature, {'simulation_dir': simulation_dir}) # Checking parameters helper.check_not_none_nor_empty(simulation_dir, 'simulation_dir') helper.check_is_directory(simulation_dir, 'simulation_dir') try: # Filter files to be opened in ParaView files = [] for f in os.listdir(os.path.join(simulation_dir, 'results')): for ext in VISUALIZATION_FILE_EXTENSIONS: if fnmatch.fnmatch(f, '*.' + ext): files.append(os.path.join(simulation_dir, 'results', f)) # Check if there's tec/vtk/stl file to visualize if len(files) == 0: raise ServiceError('There is no accepted file to visualize.') _LOGGER.debug('List of files to load:') _LOGGER.debug(str(files)) # Prepare script to run by ParaView paraview_script = os.path.join(os.path.join(simulation_dir, 'results'), 'load_data.py') prepare_paraview_script(paraview_script, files) # Launch ParaView without waiting for the ParaView to exit _LOGGER.debug('Start launching ParaView in subprocess.') subprocess.Popen([PARAVIEW_BIN, '--script=' + paraview_script + '']) _LOGGER.debug('End launching ParaView in subprocess.') helper.log_exit(_LOGGER, signature, None) except Exception as e: helper.log_exception(_LOGGER, signature, e) raise ServiceError('Error occurs when launching the ParaView. Caused by:\n' + unicode(str(e))) def prepare_paraview_script(script_path, files): ''' Prepare a script to be run by ParaView from a template. @param script_path: path of the new script to create @param files: a list of data files path @raise Exception: to its caller if any error occurs ''' # Since this is a inner function, no entrance/exit information would be logged. with open(PARAVIEW_SCRIPT_TEMPLATE, 'r') as fin: with open(script_path, 'w') as fout: for line in fin.readlines(): fout.write(line.rstrip().replace('<parameter_files>', str(files)) + '\n') # From http://code.activestate.com/recipes/577564-context-manager-for-low-level-redirection-of-stdou/ class Silence: """ Context manager which uses low-level file descriptors to suppress output to stdout/stderr, optionally redirecting to the named file(s). Example usage with Silence(stderr='output.txt', mode='a'): ... # appending to existing file ... print >> sys.stderr, "Hello from stderr" ... print "Stdout redirected to os.devnull" === contents of 'output.txt' === """ def __init__(self, stdout=os.devnull, stderr=os.devnull, mode='w'): """ Initialize Args: self: The class itself stdout: the descriptor or file name where to redirect stdout stdout: the descriptor or file name where to redirect stdout mode: the output descriptor or file mode """ self.outfiles = stdout, stderr self.combine = (stdout == stderr) self.mode = mode def __enter__(self): """ Enter the context Args: self: The class itself """ import sys self.sys = sys # save previous stdout/stderr self.saved_streams = saved_streams = sys.__stdout__, sys.__stderr__ self.fds = fds = [s.fileno() for s in saved_streams] self.saved_fds = map(os.dup, fds) # flush any pending output for s in saved_streams: s.flush() # open surrogate files if self.combine: null_streams = [open(self.outfiles[0], self.mode, 0)] * 2 if self.outfiles[0] != os.devnull: # disable buffering so output is merged immediately sys.stdout, sys.stderr = map(os.fdopen, fds, ['w']*2, [0]*2) else: null_streams = [open(f, self.mode, 0) for f in self.outfiles] self.null_fds = null_fds = [s.fileno() for s in null_streams] self.null_streams = null_streams # overwrite file objects and low-level file descriptors map(os.dup2, null_fds, fds) def __exit__(self, *args): """ Exit the context Args: self: The class itself args: other arguments """ sys = self.sys # flush any pending output for s in self.saved_streams: s.flush() # restore original streams and file descriptors map(os.dup2, self.saved_fds, self.fds) sys.stdout, sys.stderr = self.saved_streams # clean up for s in self.null_streams: s.close() for fd in self.saved_fds: os.close(fd) return False def wrapper_io(func, fd, args, return_dict): """ Run a function while redirecting its output to a file descriptor Args: func: A python function to run fd: a file descriptor args: A tuple containing argument for the function return_dict: Dictionary where to put the result of the function """ return_dict["output"] = '' with warnings.catch_warnings(): warnings.simplefilter("ignore") if fd: with Silence(stdout=fd, stderr=os.devnull, mode='a'): return_dict["output"] = func(*args) else: return_dict["output"] = func(*args) def run_thread(func, args, fd): """ Run a python function in a thread and wait for it to complete. Redirect its output to fd Args: func: A python function to run args: A tuple containing argument for the function fd: a file descriptor """ manager = Manager() return_dict = manager.dict() p = Process(target=wrapper_io, args=(func, fd, args, return_dict)) p.start() p.join() return return_dict["output"] def writeline_if_not_none(fout, data): ''' Write one line to the specified file if data is not None. @param fout: the file object to write line in @param data: the data to write as line ''' # Since this is a inner function, no entrance/exit information would be logged. if data is not None: fout.write(str(data) + '\n') def prepare_dir(prefix): ''' Prepare a directory, the directory will be a sub-directory of USER_DATA_DIRECTORY with current timestamp prefixed given prefix as the directory name. @param prefix: the directory prefix @return: the meshing/simulation directory full path @raise TypeError: if any input parameter is not of required type @raise ValueError: if any input parameter is None/empty @raise ServiceError: if any error occurred when preparing the directory ''' signature = __name__ + '.prepare_dir()' helper.log_entrance(_LOGGER, signature, {'prefix': prefix}) # Checking parameters helper.check_not_none_nor_empty(prefix, 'prefix') try: # Create a directory for this run (sub-directory name in format simulation_YYYYMMDDhhmmss) # We should consider adding some more uuid suffix to allow more concurrent requests within 1 SINGLE second. run_dir = os.path.join(USER_DATA_DIRECTORY, prefix + time.strftime('%Y%m%d%H%M%S') + '_' + uuid.uuid1().hex) os.makedirs(run_dir) helper.log_exit(_LOGGER, signature, [run_dir]) return run_dir except Exception as e: helper.log_exception(_LOGGER, signature, e) raise ServiceError('Error occurs when preparing the directory. Caused by:\n' + unicode(str(e)))
source/openwarpgui/openwarp/services.py
26,034
This exception indicates a service error. It will be raised by methods of this module. Context manager which uses low-level file descriptors to suppress output to stdout/stderr, optionally redirecting to the named file(s). Example usage with Silence(stderr='output.txt', mode='a'): ... # appending to existing file ... print >> sys.stderr, "Hello from stderr" ... print "Stdout redirected to os.devnull" === contents of 'output.txt' === Enter the context Args: self: The class itself Exit the context Args: self: The class itself args: other arguments Initialize Args: self: The class itself stdout: the descriptor or file name where to redirect stdout stdout: the descriptor or file name where to redirect stdout mode: the output descriptor or file mode Launch Mesh Generator to generate mesh. @param meshing_dir: the meshing directory @param params: the meshing parameters @return: the mesh generation log content @raise TypeError: if any input parameter is not of required type @raise ValueError: if any input parameter is None/empty, or any field of MeshingParameters is not of valid value @raise ServiceError: if error occurs during generating mesh Run post-processing. @param simulation_dir: the simulation directory @param params: the post-processing parameters @return: the post-processing log content @raise TypeError: if any input parameter is not of required type @raise ValueError: if any input parameter is None/empty, or any field of PostprocessingParameters is not of valid value @raise ServiceError: if error occurs during launching the post-processing Prepare a directory, the directory will be a sub-directory of USER_DATA_DIRECTORY with current timestamp prefixed given prefix as the directory name. @param prefix: the directory prefix @return: the meshing/simulation directory full path @raise TypeError: if any input parameter is not of required type @raise ValueError: if any input parameter is None/empty @raise ServiceError: if any error occurred when preparing the directory Prepare a script to be run by ParaView from a template. @param script_path: path of the new script to create @param files: a list of data files path @raise Exception: to its caller if any error occurs Run a python function in a thread and wait for it to complete. Redirect its output to fd Args: func: A python function to run args: A tuple containing argument for the function fd: a file descriptor Run simulation. @param simulation_dir: the simulation directory @param params: the simulation parameters @return: the simulation log content @raise TypeError: if any input parameter is not of required type @raise ValueError: if any input parameter is None/empty, or any field of SimulationParameters is not of valid value @raise ServiceError: if any other error occurred when launching the simulation Launch ParaView to visualize simulation results. @param simulation_dir: the simulation directory @raise TypeError: if any input parameter is not of required type @raise ValueError: if any input parameter is None/empty @raise ServiceError: if error occurs during launching the ParaView Run a function while redirecting its output to a file descriptor Args: func: A python function to run fd: a file descriptor args: A tuple containing argument for the function return_dict: Dictionary where to put the result of the function Write one line to the specified file if data is not None. @param fout: the file object to write line in @param data: the data to write as line This Python module provides various service functions. Updated since version 1.1: 1. Added support for postprocess and visualization. 2. Added file path validation for parameters of all related methods. Updated since version 1.2: Merge Code and Update GUI 1. Integrate New Nemoh using hdf5 and python. -*- coding: utf-8 -*- This class represents parameters used in the meshing process. This class is a subclass of "tuple", and is created using collections.namedtuple factory function. This class represents parameters used in the simulation process. This class is a subclass of "tuple", and is created using collections.namedtuple factory function. This class represents a floating body used in the SimulationParameters. This class is a subclass of "tuple", and is created using collections.namedtuple factory function. This class represents parameters used in the post-proessing. This class is a subclass of "tuple", and is created using collections.namedtuple factory function. The pre-defined config file name used by MESH_GENERATOR_BIN. The pre-defined stdout log file name. The logger object for logging. Checking parameters Generate config.txt according to given parameters Launch mesh generator Read and return the log file content Checking parameters Write the hdf5 inputs according to given parameters Launch preProcessor and Solver A prepared 'results' folder is necessary for the Nemoh software suite Checking parameters Launch postProcessor Checking parameters Filter files to be opened in ParaView Check if there's tec/vtk/stl file to visualize Prepare script to run by ParaView Launch ParaView without waiting for the ParaView to exit Since this is a inner function, no entrance/exit information would be logged. From http://code.activestate.com/recipes/577564-context-manager-for-low-level-redirection-of-stdou/ save previous stdout/stderr flush any pending output open surrogate files disable buffering so output is merged immediately overwrite file objects and low-level file descriptors flush any pending output restore original streams and file descriptors clean up Since this is a inner function, no entrance/exit information would be logged. Checking parameters Create a directory for this run (sub-directory name in format simulation_YYYYMMDDhhmmss) We should consider adding some more uuid suffix to allow more concurrent requests within 1 SINGLE second.
5,998
en
0.619084
from datetime import datetime, timedelta from uuid import uuid4 class Uploader: def generate_token(self): pass def generate_download_link(self, object_name, filename) -> (dict, str): pass def object_name(self) -> str: return str(uuid4()) class MockUploader(Uploader): def __init__(self, config): self.config = config def get_token(self): return ({}, self.object_name()) def generate_download_link(self, object_name, filename): return "" class AzureUploader(Uploader): def __init__(self, config): self.account_name = config["AZURE_ACCOUNT_NAME"] self.storage_key = config["AZURE_STORAGE_KEY"] self.container_name = config["AZURE_TO_BUCKET_NAME"] self.timeout = timedelta(seconds=config["PERMANENT_SESSION_LIFETIME"]) from azure.storage.common import CloudStorageAccount from azure.storage.blob import BlobPermissions self.CloudStorageAccount = CloudStorageAccount self.BlobPermissions = BlobPermissions def get_token(self): """ Generates an Azure SAS token for pre-authorizing a file upload. Returns a tuple in the following format: (token_dict, object_name), where - token_dict has a `token` key which contains the SAS token as a string - object_name is a string """ account = self.CloudStorageAccount( account_name=self.account_name, account_key=self.storage_key ) bbs = account.create_block_blob_service() object_name = self.object_name() sas_token = bbs.generate_blob_shared_access_signature( self.container_name, object_name, permission=self.BlobPermissions.CREATE, expiry=datetime.utcnow() + self.timeout, protocol="https", ) return ({"token": sas_token}, object_name) def generate_download_link(self, object_name, filename): account = self.CloudStorageAccount( account_name=self.account_name, account_key=self.storage_key ) bbs = account.create_block_blob_service() sas_token = bbs.generate_blob_shared_access_signature( self.container_name, object_name, permission=self.BlobPermissions.READ, expiry=datetime.utcnow() + self.timeout, content_disposition=f"attachment; filename={filename}", protocol="https", ) return bbs.make_blob_url( self.container_name, object_name, protocol="https", sas_token=sas_token )
atst/domain/csp/file_uploads.py
2,615
Generates an Azure SAS token for pre-authorizing a file upload. Returns a tuple in the following format: (token_dict, object_name), where - token_dict has a `token` key which contains the SAS token as a string - object_name is a string
244
en
0.906612
import os import os.path import torch import numpy as np import pandas import csv import random from collections import OrderedDict from .base_video_dataset import BaseVideoDataset from ltr.data.image_loader import jpeg4py_loader from ltr.admin.environment import env_settings class Lasot(BaseVideoDataset): """ LaSOT dataset. Publication: LaSOT: A High-quality Benchmark for Large-scale Single Object Tracking Heng Fan, Liting Lin, Fan Yang, Peng Chu, Ge Deng, Sijia Yu, Hexin Bai, Yong Xu, Chunyuan Liao and Haibin Ling CVPR, 2019 https://arxiv.org/pdf/1809.07845.pdf Download the dataset from https://cis.temple.edu/lasot/download.html """ def __init__(self, root=None, image_loader=jpeg4py_loader, vid_ids=None, split=None, data_fraction=None): """ args: root - path to the lasot dataset. image_loader (jpeg4py_loader) - The function to read the images. jpeg4py (https://github.com/ajkxyz/jpeg4py) is used by default. vid_ids - List containing the ids of the videos (1 - 20) used for training. If vid_ids = [1, 3, 5], then the videos with subscripts -1, -3, and -5 from each class will be used for training. split - If split='train', the official train split (protocol-II) is used for training. Note: Only one of vid_ids or split option can be used at a time. data_fraction - Fraction of dataset to be used. The complete dataset is used by default """ root = env_settings().lasot_dir if root is None else root super().__init__('LaSOT', root, image_loader) # Keep a list of all classes self.class_list = [f for f in os.listdir(self.root)] self.class_to_id = {cls_name: cls_id for cls_id, cls_name in enumerate(self.class_list)} self.sequence_list = self._build_sequence_list(vid_ids, split) if data_fraction is not None: self.sequence_list = random.sample(self.sequence_list, int(len(self.sequence_list)*data_fraction)) self.seq_per_class = self._build_class_list() def _build_sequence_list(self, vid_ids=None, split=None): if split is not None: if vid_ids is not None: raise ValueError('Cannot set both split_name and vid_ids.') ltr_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..') if split == 'train': file_path = os.path.join(ltr_path, 'data_specs', 'lasot_train_split.txt') else: raise ValueError('Unknown split name.') sequence_list = pandas.read_csv(file_path, header=None, squeeze=True).values.tolist() elif vid_ids is not None: sequence_list = [c+'-'+str(v) for c in self.class_list for v in vid_ids] else: raise ValueError('Set either split_name or vid_ids.') return sequence_list def _build_class_list(self): seq_per_class = {} for seq_id, seq_name in enumerate(self.sequence_list): class_name = seq_name.split('-')[0] if class_name in seq_per_class: seq_per_class[class_name].append(seq_id) else: seq_per_class[class_name] = [seq_id] return seq_per_class def get_name(self): return 'lasot' def has_class_info(self): return True def has_occlusion_info(self): return True def get_num_sequences(self): return len(self.sequence_list) def get_num_classes(self): return len(self.class_list) def get_sequences_in_class(self, class_name): return self.seq_per_class[class_name] def _read_bb_anno(self, seq_path): bb_anno_file = os.path.join(seq_path, "groundtruth.txt") gt = pandas.read_csv(bb_anno_file, delimiter=',', header=None, dtype=np.float32, na_filter=False, low_memory=False).values return torch.tensor(gt) def _read_target_visible(self, seq_path): # Read full occlusion and out_of_view occlusion_file = os.path.join(seq_path, "full_occlusion.txt") out_of_view_file = os.path.join(seq_path, "out_of_view.txt") with open(occlusion_file, 'r', newline='') as f: occlusion = torch.ByteTensor([int(v) for v in list(csv.reader(f))[0]]) with open(out_of_view_file, 'r') as f: out_of_view = torch.ByteTensor([int(v) for v in list(csv.reader(f))[0]]) target_visible = ~occlusion & ~out_of_view return target_visible def _get_sequence_path(self, seq_id): seq_name = self.sequence_list[seq_id] class_name = seq_name.split('-')[0] vid_id = seq_name.split('-')[1] return os.path.join(self.root, class_name, class_name + '-' + vid_id) def get_sequence_info(self, seq_id): seq_path = self._get_sequence_path(seq_id) bbox = self._read_bb_anno(seq_path) valid = (bbox[:, 2] > 0) & (bbox[:, 3] > 0) visible = self._read_target_visible(seq_path) & valid.byte() return {'bbox': bbox, 'valid': valid, 'visible': visible} def _get_frame_path(self, seq_path, frame_id): return os.path.join(seq_path, 'img', '{:08}.jpg'.format(frame_id+1)) # frames start from 1 def _get_frame(self, seq_path, frame_id): return self.image_loader(self._get_frame_path(seq_path, frame_id)) def _get_class(self, seq_path): raw_class = seq_path.split('/')[-2] return raw_class def get_class_name(self, seq_id): seq_path = self._get_sequence_path(seq_id) obj_class = self._get_class(seq_path) return obj_class def get_frames(self, seq_id, frame_ids, anno=None): seq_path = self._get_sequence_path(seq_id) obj_class = self._get_class(seq_path) frame_list = [self._get_frame(seq_path, f_id) for f_id in frame_ids] if anno is None: anno = self.get_sequence_info(seq_id) anno_frames = {} for key, value in anno.items(): anno_frames[key] = [value[f_id, ...].clone() for f_id in frame_ids] object_meta = OrderedDict({'object_class_name': obj_class, 'motion_class': None, 'major_class': None, 'root_class': None, 'motion_adverb': None}) return frame_list, anno_frames, object_meta
Stark-main/external/AR/ltr/dataset/lasot.py
6,537
LaSOT dataset. Publication: LaSOT: A High-quality Benchmark for Large-scale Single Object Tracking Heng Fan, Liting Lin, Fan Yang, Peng Chu, Ge Deng, Sijia Yu, Hexin Bai, Yong Xu, Chunyuan Liao and Haibin Ling CVPR, 2019 https://arxiv.org/pdf/1809.07845.pdf Download the dataset from https://cis.temple.edu/lasot/download.html args: root - path to the lasot dataset. image_loader (jpeg4py_loader) - The function to read the images. jpeg4py (https://github.com/ajkxyz/jpeg4py) is used by default. vid_ids - List containing the ids of the videos (1 - 20) used for training. If vid_ids = [1, 3, 5], then the videos with subscripts -1, -3, and -5 from each class will be used for training. split - If split='train', the official train split (protocol-II) is used for training. Note: Only one of vid_ids or split option can be used at a time. data_fraction - Fraction of dataset to be used. The complete dataset is used by default Keep a list of all classes Read full occlusion and out_of_view frames start from 1
1,109
en
0.726134
## 2. Introduction to the Data ## import pandas as pd all_ages = pd.read_csv('all-ages.csv') recent_grads = pd.read_csv('recent-grads.csv') print(all_ages.head()) print(recent_grads.head()) ## 3. Summarizing Major Categories ## # Unique values in Major_category column. print(all_ages['Major_category'].unique()) aa_cat_counts = dict() rg_cat_counts = dict() def cat_summary(data,category): subset = data[data['Major_category']==category] total = subset['Total'].sum() return(total) for cat in all_ages['Major_category'].unique(): aa_cat_counts[cat] = cat_summary(all_ages,cat) for cat in recent_grads['Major_category'].unique(): rg_cat_counts[cat] = cat_summary(recent_grads,cat) ## 4. Low-Wage Job Rates ## low_wage_percent = 0.0 low_wage_percent = recent_grads['Low_wage_jobs'].sum()/recent_grads['Total'].sum() ## 5. Comparing Data Sets ## # All majors, common to both DataFrames majors = recent_grads['Major'].unique() rg_lower_count = 0 for item in majors: grad_subset = recent_grads[recent_grads['Major']==item] all_subset = all_ages[all_ages['Major']==item] if(grad_subset['Unemployment_rate'].values[0] < all_subset['Unemployment_rate'].values[0]): rg_lower_count +=1 print(rg_lower_count)
Data Analysis with Pandas Intermediate/Challenge_ Summarizing Data-112.py
1,259
2. Introduction to the Data 3. Summarizing Major Categories Unique values in Major_category column. 4. Low-Wage Job Rates 5. Comparing Data Sets All majors, common to both DataFrames
186
en
0.668016
import pytest from chispa import assert_df_equality from cishouseholds.pipeline.generate_outputs import configure_outputs def test_configure_outputs(spark_session): input_df = spark_session.createDataFrame( data=[ ("England", 6, 2, "02-6SY"), ("NI", 9, 5, "02-6SY"), ("Scotland", 11, 7, "07SY-11SY"), ("Wales", 15, 10, "07SY-11SY"), ("Wales", 15, 6, "07SY-11SY"), ("Scotland", 15, 6, None), ("England", 17, 12, "12SY-24"), ("NI", 18, 13, "12SY-24"), ("England", 25, 12, "25-34"), ("NI", 55, 79, "50-69"), ("NI", 88, 1, "70+"), ], schema="country string, age integer, school_year integer, output string", ) expected_df1 = spark_session.createDataFrame( data=[ ("England", 6, 2, "trumpet"), ("NI", 9, 5, "trumpet"), ("Scotland", 11, 7, "07SY-11SY"), ("Wales", 15, 10, "07SY-11SY"), ("Wales", 15, 6, "07SY-11SY"), ("Scotland", 15, 6, None), ("England", 17, 12, "12SY-24"), ("NI", 18, 13, "12SY-24"), ("England", 25, 12, "25-34"), ("NI", 55, 79, "50-69"), ("NI", 88, 1, "gibberish"), ], schema="country string, age integer, renamed integer, output string", ) expected_df2 = spark_session.createDataFrame( data=[ ("Wales", 2), ("NI", 4), ("England", 3), ("Scotland", 2), ], schema="country string, test long", ) expected_df3 = spark_session.createDataFrame( data=[ (3, 2), (1, 4), (2, 3), (4, 2), ], schema="country integer, test long", ) # test mapping functionality with complete map off output_df1 = configure_outputs( input_df, selection_columns=["country", "age", "school_year", "output"], name_map={"school_year": "renamed"}, value_map={"output": {"70+": "gibberish", "02-6SY": "trumpet"}}, ) output_df5 = configure_outputs( input_df, selection_columns=["country", "age", "school_year"], group_by_columns="country", aggregate_function="count", aggregate_column_name="test", value_map={"country": {"NI": 1, "England": 2, "Wales": 3, "Scotland": 4}}, complete_map=True, ) # test correct grouping functionality output_df2 = configure_outputs( input_df, group_by_columns="country", aggregate_function="count", aggregate_column_name="test" ) assert_df_equality(output_df1, expected_df1, ignore_nullable=True, ignore_row_order=True) assert_df_equality(output_df2, expected_df2, ignore_nullable=True, ignore_row_order=True) assert_df_equality(output_df5, expected_df3, ignore_nullable=True, ignore_row_order=True) # test function dissalows using functions on non-selected columns with pytest.raises(IndexError): configure_outputs(input_df, group_by_columns="country", selection_columns="age") # test function raises readable error for column not existing on dataframe with pytest.raises(AttributeError): configure_outputs(input_df, selection_columns="nothing") # test incomplete maps raise index error with pytest.raises(LookupError): configure_outputs( input_df, selection_columns=["country", "age", "school_year", "output"], name_map={"school_year": "renamed"}, value_map={"output": {"70+": "gibberish", "02-6SY": "trumpet"}}, complete_map=True, )
tests/pipeline/test_configure_outputs.py
3,695
test mapping functionality with complete map off test correct grouping functionality test function dissalows using functions on non-selected columns test function raises readable error for column not existing on dataframe test incomplete maps raise index error
260
en
0.454519
#!/usr/bin/python from __future__ import absolute_import, division, print_function # Copyright 2019-2020 Fortinet, Inc. # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <https://www.gnu.org/licenses/>. __metaclass__ = type ANSIBLE_METADATA = {'status': ['preview'], 'supported_by': 'community', 'metadata_version': '1.1'} DOCUMENTATION = ''' --- module: fmgr_application_group short_description: Configure firewall application groups. description: - This module is able to configure a FortiManager device. - Examples include all parameters and values which need to be adjusted to data sources before usage. version_added: "2.10" author: - Link Zheng (@chillancezen) - Jie Xue (@JieX19) - Frank Shen (@fshen01) - Hongbin Lu (@fgtdev-hblu) notes: - Running in workspace locking mode is supported in this FortiManager module, the top level parameters workspace_locking_adom and workspace_locking_timeout help do the work. - To create or update an object, use state present directive. - To delete an object, use state absent directive. - Normally, running one module can fail when a non-zero rc is returned. you can also override the conditions to fail or succeed with parameters rc_failed and rc_succeeded options: bypass_validation: description: only set to True when module schema diffs with FortiManager API structure, module continues to execute without validating parameters required: false type: bool default: false workspace_locking_adom: description: the adom to lock for FortiManager running in workspace mode, the value can be global and others including root required: false type: str workspace_locking_timeout: description: the maximum time in seconds to wait for other user to release the workspace lock required: false type: int default: 300 state: description: the directive to create, update or delete an object type: str required: true choices: - present - absent rc_succeeded: description: the rc codes list with which the conditions to succeed will be overriden type: list required: false rc_failed: description: the rc codes list with which the conditions to fail will be overriden type: list required: false adom: description: the parameter (adom) in requested url type: str required: true application_group: description: the top level parameters set required: false type: dict suboptions: application: description: no description type: int category: description: no description type: int comment: type: str description: 'Comment' name: type: str description: 'Application group name.' type: type: str description: 'Application group type.' choices: - 'application' - 'category' ''' EXAMPLES = ''' - hosts: fortimanager-inventory collections: - fortinet.fortimanager connection: httpapi vars: ansible_httpapi_use_ssl: True ansible_httpapi_validate_certs: False ansible_httpapi_port: 443 tasks: - name: Configure firewall application groups. fmgr_application_group: bypass_validation: False workspace_locking_adom: <value in [global, custom adom including root]> workspace_locking_timeout: 300 rc_succeeded: [0, -2, -3, ...] rc_failed: [-2, -3, ...] adom: <your own value> state: <value in [present, absent]> application_group: application: <value of integer> category: <value of integer> comment: <value of string> name: <value of string> type: <value in [application, category]> ''' RETURN = ''' request_url: description: The full url requested returned: always type: str sample: /sys/login/user response_code: description: The status of api request returned: always type: int sample: 0 response_message: description: The descriptive message of the api response type: str returned: always sample: OK. ''' from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.connection import Connection from ansible_collections.fortinet.fortimanager.plugins.module_utils.napi import NAPIManager from ansible_collections.fortinet.fortimanager.plugins.module_utils.napi import check_galaxy_version from ansible_collections.fortinet.fortimanager.plugins.module_utils.napi import check_parameter_bypass def main(): jrpc_urls = [ '/pm/config/adom/{adom}/obj/application/group', '/pm/config/global/obj/application/group' ] perobject_jrpc_urls = [ '/pm/config/adom/{adom}/obj/application/group/{group}', '/pm/config/global/obj/application/group/{group}' ] url_params = ['adom'] module_primary_key = 'name' module_arg_spec = { 'bypass_validation': { 'type': 'bool', 'required': False, 'default': False }, 'workspace_locking_adom': { 'type': 'str', 'required': False }, 'workspace_locking_timeout': { 'type': 'int', 'required': False, 'default': 300 }, 'rc_succeeded': { 'required': False, 'type': 'list' }, 'rc_failed': { 'required': False, 'type': 'list' }, 'state': { 'type': 'str', 'required': True, 'choices': [ 'present', 'absent' ] }, 'adom': { 'required': True, 'type': 'str' }, 'application_group': { 'required': False, 'type': 'dict', 'options': { 'application': { 'required': False, 'type': 'int' }, 'category': { 'required': False, 'type': 'int' }, 'comment': { 'required': False, 'type': 'str' }, 'name': { 'required': True, 'type': 'str' }, 'type': { 'required': False, 'choices': [ 'application', 'category' ], 'type': 'str' } } } } params_validation_blob = [] check_galaxy_version(module_arg_spec) module = AnsibleModule(argument_spec=check_parameter_bypass(module_arg_spec, 'application_group'), supports_check_mode=False) fmgr = None if module._socket_path: connection = Connection(module._socket_path) fmgr = NAPIManager(jrpc_urls, perobject_jrpc_urls, module_primary_key, url_params, module, connection, top_level_schema_name='data') fmgr.validate_parameters(params_validation_blob) fmgr.process_curd() else: module.fail_json(msg='MUST RUN IN HTTPAPI MODE') module.exit_json(meta=module.params) if __name__ == '__main__': main()
venv/lib/python3.7/site-packages/ansible_collections/fortinet/fortimanager/plugins/modules/fmgr_application_group.py
8,248
!/usr/bin/python Copyright 2019-2020 Fortinet, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
662
en
0.904099
from __future__ import print_function, division import os from PIL import Image import numpy as np from torch.utils.data import Dataset from mypath import Path from torchvision import transforms from dataloaders import custom_transforms as tr from dataloaders import sp_transforms as tr_sp class VOCSegmentation(Dataset): """ PascalVoc dataset """ NUM_CLASSES = 21 def __init__(self, args, base_dir=Path.db_root_dir('pascal'), split='train', ): """ :param base_dir: path to VOC dataset directory :param split: train/val :param transform: transform to apply """ super().__init__() self._base_dir = base_dir self._image_dir = os.path.join(self._base_dir, 'JPEGImages') self._cat_dir = os.path.join(self._base_dir, 'SegmentationClass') self._sp_dir = os.path.join(self._base_dir, 'super_pixel') if isinstance(split, str): self.split = [split] else: split.sort() self.split = split self.args = args _splits_dir = os.path.join(self._base_dir, 'ImageSets', 'Segmentation') self.im_ids = [] self.images = [] self.categories = [] self.super_pixel = [] for splt in self.split: with open(os.path.join(os.path.join(_splits_dir, splt + '.txt')), "r") as f: lines = f.read().splitlines() for ii, line in enumerate(lines): _image = os.path.join(self._image_dir, line + ".jpg") _cat = os.path.join(self._cat_dir, line + ".png") _sp = os.path.join(self._sp_dir, line + ".ppm.jpg") assert os.path.isfile(_image) assert os.path.isfile(_cat) assert os.path.isfile(_sp) self.im_ids.append(line) self.images.append(_image) self.categories.append(_cat) self.super_pixel.append(_sp) assert (len(self.images) == len(self.categories)) # Display stats print('Number of images in {}: {:d}'.format(split, len(self.images))) def __len__(self): return len(self.images) def __getitem__(self, index): _img, _target, _sp = self._make_img_gt_point_pair(index) sample = {'image': _img, 'label': _target, 'super_pixel':_sp} for split in self.split: if split == "train": return self.transform_tr(sample) elif split == 'val': return self.transform_val(sample) def _make_img_gt_point_pair(self, index): _img = Image.open(self.images[index]).convert('RGB') _target = Image.open(self.categories[index]) _sp = Image.open(self.super_pixel[index]) return _img, _target, _sp def transform_tr(self, sample): if len(sample) == 2: composed_transforms = transforms.Compose([ tr.RandomHorizontalFlip(), tr.RandomScaleCrop(base_size=self.args.base_size, crop_size=self.args.crop_size), tr.RandomGaussianBlur(), tr.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)), tr.ToTensor()]) return composed_transforms(sample) else: composed_transforms = transforms.Compose([ tr_sp.RandomHorizontalFlip(), tr_sp.RandomScaleCrop(base_size=self.args.base_size, crop_size=self.args.crop_size), tr_sp.RandomGaussianBlur(), tr_sp.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)), tr_sp.ToTensor()]) return composed_transforms(sample) def transform_val(self, sample): if len(sample) == 2: composed_transforms = transforms.Compose([ tr.FixScaleCrop(crop_size=self.args.crop_size), tr.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)), tr.ToTensor()]) return composed_transforms(sample) else: composed_transforms = transforms.Compose([ tr_sp.FixScaleCrop(crop_size=self.args.crop_size), tr_sp.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)), tr_sp.ToTensor()]) return composed_transforms(sample) def __str__(self): return 'VOC2012(split=' + str(self.split) + ')' if __name__ == '__main__': from dataloaders.utils import decode_segmap from torch.utils.data import DataLoader import matplotlib.pyplot as plt import argparse parser = argparse.ArgumentParser() args = parser.parse_args() args.base_size = 513 args.crop_size = 513 voc_train = VOCSegmentation(args, split='train') dataloader = DataLoader(voc_train, batch_size=5, shuffle=True, num_workers=0) for ii, sample in enumerate(dataloader): for jj in range(sample["image"].size()[0]): img = sample['image'].numpy() gt = sample['label'].numpy() sps = sample['super_pixel'].numpy() tmp = np.array(gt[jj]).astype(np.uint8) segmap = decode_segmap(tmp, dataset='pascal') img_tmp = np.transpose(img[jj], axes=[1, 2, 0]) img_tmp *= (0.229, 0.224, 0.225) img_tmp += (0.485, 0.456, 0.406) img_tmp *= 255.0 img_tmp = img_tmp.astype(np.uint8) sp = np.array(sps[jj]) sp *= 255.0 sp = sp.astype(np.uint8) plt.figure() plt.title('display') plt.subplot(211) plt.imshow(img_tmp) plt.subplot(212) plt.imshow(sp) if ii == 1: break plt.show(block=True)
dataloaders/datasets/pascal.py
5,854
PascalVoc dataset :param base_dir: path to VOC dataset directory :param split: train/val :param transform: transform to apply Display stats
141
en
0.335918
# Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. """spaCy ANN Linker, a pipeline component for generating spaCy KnowledgeBase Alias Candidates for Entity Linking.""" __version__ = '0.1.10' from .ann_linker import AnnLinker from .remote_ann_linker import RemoteAnnLinker # TODO: Uncomment (and probably fix a bit) once this PR is merged upstream # https://github.com/explosion/spaCy/pull/4988 to enable kb registry with # customizable `get_candidates` function # # from spacy.kb import KnowledgeBase # from spacy.tokens import Span # from spacy.util import registry # @registry.kb.register("get_candidates") # def get_candidates(kb: KnowledgeBase, ent: Span): # alias = ent._.alias_candidates[0] if ent._.alias_candidates else ent.text # return kb.get_candidates(alias)
spacy_ann/__init__.py
830
spaCy ANN Linker, a pipeline component for generating spaCy KnowledgeBase Alias Candidates for Entity Linking. Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT License. TODO: Uncomment (and probably fix a bit) once this PR is merged upstream https://github.com/explosion/spaCy/pull/4988 to enable kb registry with customizable `get_candidates` function from spacy.kb import KnowledgeBase from spacy.tokens import Span from spacy.util import registry @registry.kb.register("get_candidates") def get_candidates(kb: KnowledgeBase, ent: Span): alias = ent._.alias_candidates[0] if ent._.alias_candidates else ent.text return kb.get_candidates(alias)
689
en
0.560597
from scipy.optimize import minimize import warnings import numpy as np from astropy.io import fits from astropy.table import Table from astropy.nddata import NDData from photutils.psf import extract_stars from astropy.stats import gaussian_sigma_to_fwhm from ..core import Block import matplotlib.pyplot as plt from collections import OrderedDict from ..utils import fast_binning def image_psf(image, stars, size=15, normalize=False, return_cutouts=False): """ Get global psf from image using photutils routines Parameters ---------- image: np.ndarray or path stars: np.ndarray stars positions with shape (n,2) size: int size of the cuts around stars (in pixels) normalize: bool, optional weather to normalize the cutout, default is False Returns ------- np.ndarray of shape (size, size) """ _, cuts = cutouts(image, stars, size=size) cuts = cuts.data if normalize: cuts = [c/np.sum(c) for c in cuts] if return_cutouts: return np.median(cuts, axis=0), cuts else: return np.median(cuts, axis=0) def cutouts(image, stars, size=15): """Custom version to extract stars cutouts Parameters ---------- Parameters ---------- image: np.ndarray or path stars: np.ndarray stars positions with shape (n,2) size: int size of the cuts around stars (in pixels), by default 15 Returns ------- np.ndarray of shape (size, size) """ if isinstance(image, str): image = fits.getdata(image) warnings.simplefilter("ignore") if np.shape(stars) > (1,2): stars_tbl = Table( [stars[:, 0], stars[:, 1], np.arange(len(stars))], names=["x", "y", "id"]) stars = extract_stars(NDData(data=image), stars_tbl, size=size) idxs = np.array([s.id_label for s in stars]) return idxs, stars else: stars_tbl = Table( data=np.array([stars[0][0], stars[0][1]]), names=["x", "y"]) stars = extract_stars(NDData(data=image), stars_tbl, size=size) return stars def good_cutouts(image, xy, r=30, upper=40000, lower=1000, trim=100): idxs, _cuts = cutouts(image, xy, r) cuts = OrderedDict(zip(idxs, _cuts)) peaks = [cutout.data.max() for cutout in cuts.values()] for i, cutout in cuts.copy().items(): if i in cuts: peak = cutout.data.max() center = cutout.center # removing saturated and faint stars if peak > upper or peak < lower: del cuts[i] # removing stars on borders elif np.any(center < [trim, trim]) or np.any(center > np.array(image.shape) - trim): del cuts[i] # removing close stars closest = idxs[np.nonzero(np.linalg.norm(center - xy[idxs], axis=1) < r)[0]] if len(closest) > 1: for j in closest: if j in cuts: del cuts[j] return cuts def moments(data): """Returns (height, x, y, width_x, width_y) the gaussian parameters of a 2D distribution by calculating its moments """ height = data.max() background = data.min() data = data-np.min(data) total = data.sum() x, y = np.indices(data.shape) x = (x * data).sum() / total y = (y * data).sum() / total col = data[:, int(y)] width_x = np.sqrt(abs((np.arange(col.size) - y) ** 2 * col).sum() / col.sum()) row = data[int(x), :] width_y = np.sqrt(abs((np.arange(row.size) - x) ** 2 * row).sum() / row.sum()) width_x /= gaussian_sigma_to_fwhm width_y /= gaussian_sigma_to_fwhm return height, x, y, width_x, width_y, 0.0, background class PSFModel(Block): def __init__(self, cutout_size=21, save_cutouts=False, **kwargs): super().__init__(**kwargs) self.cutout_size = cutout_size self.save_cutouts = save_cutouts self.x, self.y = np.indices((self.cutout_size, self.cutout_size)) self.epsf = None @property def optimized_model(self): return self.model(*self.optimized_params) def build_epsf(self, image, stars): return image_psf(image, stars.copy(), size=self.cutout_size, return_cutouts=self.save_cutouts) def model(self): raise NotImplementedError("") def nll(self, p): ll = np.sum(np.power((self.model(*p) - self.epsf), 2) * self.epsf) return ll if np.isfinite(ll) else 1e25 def optimize(self): raise NotImplementedError("") def sigma_to_fwhm(self, *args): return gaussian_sigma_to_fwhm def run(self, image): if self.save_cutouts: self.epsf, image.cutouts = self.build_epsf(image.data, image.stars_coords) else: self.epsf = self.build_epsf(image.data, image.stars_coords) image.fwhmx, image.fwhmy, image.theta = self.optimize() image.fwhm = np.mean([image.fwhmx, image.fwhmy]) image.psf_sigma_x = image.fwhmx / self.sigma_to_fwhm() image.psf_sigma_y = image.fwhmy / self.sigma_to_fwhm() image.header["FWHM"] = image.fwhm image.header["FWHMX"] = image.fwhmx image.header["FWHMY"] = image.fwhmy image.header["PSFANGLE"] = image.theta image.header["FWHMALG"] = self.__class__.__name__ def show_residuals(self): plt.imshow(self.epsf - self.optimized_model) plt.colorbar() ax = plt.gca() plt.text(0.05, 0.05, "$\Delta f=$ {:.2f}%".format(100*np.sum(np.abs(self.epsf - self.optimized_model))/np.sum(self.epsf)), fontsize=14, horizontalalignment='left', verticalalignment='bottom', transform=ax.transAxes, c="w") def __call__(self, data): self.epsf = data return self.optimize() class FWHM(PSFModel): """ Fast empirical FWHM (based on Arielle Bertrou-Cantou's idea) """ def __init__(self, cutout_size=51, **kwargs): super().__init__(cutout_size=cutout_size, **kwargs) Y, X = np.indices((self.cutout_size,self.cutout_size)) x = y = self.cutout_size/2 self.radii = (np.sqrt((X - x) ** 2 + (Y - y) ** 2)).flatten() def optimize(self): psf = self.epsf.copy() psf -= np.min(psf) pixels = psf.flatten() binned_radii, binned_pixels, _ = fast_binning(self.radii, pixels, bins=1) fwhm = 2*binned_radii[np.flatnonzero(binned_pixels > np.max(binned_pixels)/2)[-1]] return fwhm, fwhm, 0 class FastGaussian(PSFModel): """ Fit a symetric 2D Gaussian model to an image effective PSF """ def __init__(self, cutout_size=21, **kwargs): super().__init__(cutout_size=cutout_size, **kwargs) def model(self, height, s, m): dx = self.x - self.cutout_size/2 dy = self.y - self.cutout_size/2 psf = height * np.exp(-((dx/(2*s))**2 + (dy/(2*s))**2)) return psf + m def optimize(self): p0 = [np.max(self.epsf), 4, np.min(self.epsf)] min_sigma = 0.5 bounds = [ (0, np.infty), (min_sigma, np.infty), (0, np.mean(self.epsf)), ] with warnings.catch_warnings(): warnings.simplefilter("ignore") params = minimize(self.nll, p0, bounds=bounds).x self.optimized_params = params return params[1]*self.sigma_to_fwhm(), params[1]*self.sigma_to_fwhm(), 0 def citations(self): return "scipy", "photutils" class Gaussian2D(PSFModel): """ Fit an elliptical 2D Gaussian model to an image effective PSF """ def __init__(self, cutout_size=21, **kwargs): super().__init__(cutout_size=cutout_size, **kwargs) def model(self, height, xo, yo, sx, sy, theta, m): dx = self.x - xo dy = self.y - yo a = (np.cos(theta)**2)/(2*sx**2) + (np.sin(theta)**2)/(2*sy**2) b = -(np.sin(2*theta))/(4*sx**2) + (np.sin(2*theta))/(4*sy**2) c = (np.sin(theta)**2)/(2*sx**2) + (np.cos(theta)**2)/(2*sy**2) psf = height * np.exp(-(a * dx ** 2 + 2 * b * dx * dy + c * dy ** 2)) return psf + m def optimize(self): p0 = moments(self.epsf) x0, y0 = p0[1], p0[2] min_sigma = 0.5 bounds = [ (0, np.infty), (x0 - 3, x0 + 3), (y0 - 3, y0 + 3), (min_sigma, np.infty), (min_sigma, np.infty), (0, 4), (0, np.mean(self.epsf)), ] with warnings.catch_warnings(): warnings.simplefilter("ignore") params = minimize(self.nll, p0, bounds=bounds).x self.optimized_params = params return params[3]*self.sigma_to_fwhm(), params[4]*self.sigma_to_fwhm(), params[-2] def citations(self): return "scipy", "photutils" class Moffat2D(PSFModel): """ Fit an elliptical 2D Moffat model to an image effective PSF """ def __init__(self, cutout_size=21, **kwargs): super().__init__(cutout_size=cutout_size, **kwargs) def model(self, a, x0, y0, sx, sy, theta, b, beta): # https://pixinsight.com/doc/tools/DynamicPSF/DynamicPSF.html dx_ = self.x - x0 dy_ = self.y - y0 dx = dx_*np.cos(theta) + dy_*np.sin(theta) dy = -dx_*np.sin(theta) + dy_*np.cos(theta) return b + a / np.power(1 + (dx/sx)**2 + (dy/sy)**2, beta) def sigma_to_fwhm(self): return 2*np.sqrt(np.power(2, 1/self.optimized_params[-1]) - 1) def optimize(self): p0 = list(moments(self.epsf)) p0.append(1) x0, y0 = p0[1], p0[2] min_sigma = 0.5 bounds = [ (0, np.infty), (x0 - 3, x0 + 3), (y0 - 3, y0 + 3), (min_sigma, np.infty), (min_sigma, np.infty), (0, 4), (0, np.mean(self.epsf)), (1, 8), ] with warnings.catch_warnings(): warnings.simplefilter("ignore") params = minimize(self.nll, p0, bounds=bounds).x self.optimized_params = params sm = self.sigma_to_fwhm() return params[3]*sm, params[4]*sm, params[-2] def citations(self): return "scipy", "photutils" class KeepGoodStars(Block): def __init__(self, n=-1, **kwargs): super().__init__(**kwargs) self.n = n def run(self, image, n=-1): good_stars = self(image.data, image.stars_coords) image.stars_coords = good_stars def __call__(self, data, stars): i, _stars = cutouts(data, stars, size=21) #good = np.array([shapiro(s.data).statistic for s in _stars]) > 0.33 good = np.array([np.std(s.data) for s in _stars]) > 1000 return stars[i][np.argwhere(good).squeeze()][0:self.n]
prose/blocks/psf.py
10,840
Fast empirical FWHM (based on Arielle Bertrou-Cantou's idea) Fit a symetric 2D Gaussian model to an image effective PSF Fit an elliptical 2D Gaussian model to an image effective PSF Fit an elliptical 2D Moffat model to an image effective PSF Custom version to extract stars cutouts Parameters ---------- Parameters ---------- image: np.ndarray or path stars: np.ndarray stars positions with shape (n,2) size: int size of the cuts around stars (in pixels), by default 15 Returns ------- np.ndarray of shape (size, size) Get global psf from image using photutils routines Parameters ---------- image: np.ndarray or path stars: np.ndarray stars positions with shape (n,2) size: int size of the cuts around stars (in pixels) normalize: bool, optional weather to normalize the cutout, default is False Returns ------- np.ndarray of shape (size, size) Returns (height, x, y, width_x, width_y) the gaussian parameters of a 2D distribution by calculating its moments removing saturated and faint stars removing stars on borders removing close stars https://pixinsight.com/doc/tools/DynamicPSF/DynamicPSF.htmlgood = np.array([shapiro(s.data).statistic for s in _stars]) > 0.33
1,194
en
0.663623
import sys from solc import link_code from os.path import join from web3 import Web3 from cobra.project.configuration import Configuration from cobra.utils import file_reader, yaml_loader, json_loader from cobra.utils.console_log import console_log class Interfaces(Configuration): def __init__(self, web3: Web3, yaml_file, more=False): super().__init__() self.web3 = web3 self.contracts = dict() self.yaml_file = yaml_file self.more = more def get_interfaces(self): readied_yaml = file_reader(self.yaml_file) loaded_yaml = yaml_loader(readied_yaml) if 'test' in loaded_yaml: test_yaml = loaded_yaml['test'] configurations_yaml = self.test(test_yaml) for configuration_yaml in configurations_yaml: artifact_json = join(configuration_yaml['artifact_path'], configuration_yaml['artifact']) readied_artifact_json = file_reader(artifact_json) loaded_artifact_json = json_loader(readied_artifact_json) if configuration_yaml['links'] is None: self.test_with_out_link(loaded_artifact_json) else: self.test_with_link(loaded_artifact_json, configuration_yaml['links']) return self.contracts else: console_log("test in cobra.yaml", "error", "NotFound") sys.exit() def get_links_address(self, links): contract_name_and_address = dict() for link in links: for contract in self.contracts.keys(): contract = contract.split(":") if contract[0] == link[:-5]: contract_name_and_address.setdefault(link[:-5], contract[1]) elif contract[0] == link: contract_name_and_address.setdefault(link, contract[1]) else: continue return contract_name_and_address def test_with_link(self, artifact, links): unlinked_bytecode = artifact['bin'] get_link_address = self.get_links_address(links) linked_bytecode = link_code(unlinked_bytecode, get_link_address) try: contract_factory = self.web3.eth.contract(abi=artifact['abi'], bytecode=linked_bytecode) except ValueError as valueError: value_error = str(valueError.args.__getitem__(0)) if "'" in value_error and not self.more: error = str(value_error).split("'") console_log(str(error[0]), "error", "ValueError") elif "'" in value_error and self.more: console_log( str(value_error), "error", "ValueError") elif not self.more: console_log( str(value_error).strip('\n')[0], "error", "ValueError") elif self.more: console_log( str(value_error), "error", "ValueError") sys.exit() # Get transaction hash tx_hash = contract_factory.constructor().transact() address = self.web3.eth.getTransactionReceipt(tx_hash)['contractAddress'] contract = {"abi": artifact['abi'], "bytecode": linked_bytecode} contract_name_and_address = artifact['contractName'] + ":" + str(address) self.contracts.setdefault(contract_name_and_address, contract) def test_with_out_link(self, artifact): try: contract_factory = self.web3.eth.contract(abi=artifact['abi'], bytecode=artifact['bin']) except ValueError as valueError: value_error = str(valueError.args.__getitem__(0)) if "'" in value_error and not self.more: error = str(value_error).split("'") console_log(str(error[0]), "error", "ValueError") elif "'" in value_error and self.more: console_log( str(value_error), "error", "ValueError") elif not self.more: console_log( str(value_error).strip('\n')[0], "error", "ValueError") elif self.more: console_log( str(value_error), "error", "ValueError") sys.exit() # Get transaction hash tx_hash = contract_factory.constructor().transact() address = self.web3.eth.getTransactionReceipt(tx_hash)['contractAddress'] contract = {"abi": artifact['abi'], "bytecode": artifact['bin']} contract_name_and_address = artifact['contractName'] + ":" + str(address) self.contracts.setdefault(contract_name_and_address, contract)
cobra/test/interfaces.py
4,751
Get transaction hash Get transaction hash
41
en
0.569308
# -*- coding: utf-8 -*- from raw._ebutts import *
ebu_tt_live/bindings/_ebutts.py
50
-*- coding: utf-8 -*-
21
en
0.767281
""" "run_HAC.py" executes the training schedule for the agent. By default, the agent will alternate between exploration and testing phases. The number of episodes in the exploration phase can be configured in section 3 of "design_agent_and_env2.py" file. If the user prefers to only explore or only test, the user can enter the command-line options ""--train_only" or "--test", respectively. The full list of command-line options is available in the "options.py" file. """ import pickle as cpickle import agent as Agent from utils import print_summary NUM_BATCH = 1000 TEST_FREQ = 2 num_test_episodes = 100 def run_HAC(FLAGS,env,agent): # Print task summary print_summary(FLAGS,env) # Determine training mode. If not testing and not solely training, interleave training and testing to track progress mix_train_test = False if not FLAGS.test and not FLAGS.train_only: mix_train_test = True for batch in range(NUM_BATCH): num_episodes = agent.other_params["num_exploration_episodes"] # Evaluate policy every TEST_FREQ batches if interleaving training and testing if mix_train_test and batch % TEST_FREQ == 0: print("\n--- TESTING ---") agent.FLAGS.test = True num_episodes = num_test_episodes # Reset successful episode counter successful_episodes = 0 for episode in range(num_episodes): print("\nBatch %d, Episode %d" % (batch, episode)) # Train for an episode success = agent.train(env, episode) if success: print("Batch %d, Episode %d End Goal Achieved\n" % (batch, episode)) # Increment successful episode counter if applicable if mix_train_test and batch % TEST_FREQ == 0: successful_episodes += 1 # Save agent agent.save_model(episode) # Finish evaluating policy if tested prior batch if mix_train_test and batch % TEST_FREQ == 0: # Log performance success_rate = successful_episodes / num_test_episodes * 100 print("\nTesting Success Rate %.2f%%" % success_rate) agent.log_performance(success_rate) agent.FLAGS.test = False print("\n--- END TESTING ---\n")
run_HAC.py
2,422
"run_HAC.py" executes the training schedule for the agent. By default, the agent will alternate between exploration and testing phases. The number of episodes in the exploration phase can be configured in section 3 of "design_agent_and_env2.py" file. If the user prefers to only explore or only test, the user can enter the command-line options ""--train_only" or "--test", respectively. The full list of command-line options is available in the "options.py" file. Print task summary Determine training mode. If not testing and not solely training, interleave training and testing to track progress Evaluate policy every TEST_FREQ batches if interleaving training and testing Reset successful episode counter Train for an episode Increment successful episode counter if applicable Save agent Finish evaluating policy if tested prior batch Log performance
861
en
0.83408
# This Source Code Form is subject to the terms of the Mozilla Public # License, v. 2.0. If a copy of the MPL was not distributed with this # file, You can obtain one at http://mozilla.org/MPL/2.0/. import re from marionette.by import By from gaiatest.apps.base import Base class Ftu(Base): name = 'FTU' _next_button_locator = (By.ID, 'forward') # Step Languages section _section_languages_locator = (By.ID, 'languages') _listed_languages_locator = (By.CSS_SELECTOR, "#languages ul li input[name='language.current']") _language_locator = (By.CSS_SELECTOR, "#languages ul li input[name='language.current'][value='%s'] ~ p") _language_input_locator = (By.CSS_SELECTOR, "#languages ul li input[name='language.current'][value='%s']") _selected_language_input_locator = (By.CSS_SELECTOR, "#languages ul li input:checked") # Step Cell data section _section_cell_data_locator = (By.ID, 'data_3g') _enable_data_checkbox_locator = (By.ID, 'data-connection-switch') # Step Wifi _section_wifi_locator = (By.ID, 'wifi') _found_wifi_networks_locator = (By.CSS_SELECTOR, 'ul#networks-list li') _password_input_locator = (By.ID, 'wifi_password') _join_network_locator = (By.ID, 'wifi-join-button') _progress_activity_locator = (By.ID, 'progress-activity') # Step Date & Time _section_date_time_locator = (By.ID, 'date_and_time') _timezone_continent_locator = (By.CSS_SELECTOR, '#time-form li:nth-child(1) > .change.icon.icon-dialog') _timezone_city_locator = (By.CSS_SELECTOR, '#time-form li:nth-child(2) > .change.icon.icon-dialog') _time_zone_title_locator = (By.ID, 'time-zone-title') # Step Geolocation _section_geolocation_locator = (By.ID, 'geolocation') _enable_geolocation_checkbox_locator = (By.CSS_SELECTOR, '#geolocation-switch > label') # Section Import contacts _section_import_contacts_locator = (By.ID, 'import_contacts') _import_from_sim_locator = (By.ID, 'sim-import-button') _sim_import_feedback_locator = (By.ID, 'statusMsg') # Step Firefox Accounts _section_firefox_accounts_locator = (By.ID, 'firefox_accounts') # Section Welcome Browser _section_welcome_browser_locator = (By.ID, 'welcome_browser') _enable_statistic_checkbox_locator = (By.ID, 'form_share_statistics') _statistic_checkbox_locator = (By.ID, 'share-performance') # Section Privacy Choices _section_browser_privacy_locator = (By.ID, 'browser_privacy') _email_field_locator = (By.CSS_SELECTOR, 'input[type="email"]') # Section Finish _section_finish_locator = (By.ID, 'finish-screen') _skip_tour_button_locator = (By.ID, 'skip-tutorial-button') _take_tour_button_locator = (By.ID, 'lets-go-button') # Section Tour _step_header_locator = (By.ID, 'tutorial-step-title') _tour_next_button_locator = (By.ID, 'forward-tutorial') _tour_back_button_locator = (By.ID, 'back-tutorial') # Section Tutorial Finish _section_tutorial_finish_locator = (By.ID, 'tutorial-finish-tiny') _lets_go_button_locator = (By.ID, 'tutorialFinished') # Pattern for import sim contacts message _pattern_contacts = re.compile("^No contacts detected on SIM to import$|^Imported one contact$|^Imported [0-9]+ contacts$") _pattern_contacts_0 = re.compile("^No contacts detected on SIM to import$") _pattern_contacts_1 = re.compile("^Imported one contact$") _pattern_contacts_N = re.compile("^Imported ([0-9]+) contacts$") def launch(self): Base.launch(self) self.wait_for_element_displayed(*self._section_languages_locator) @property def languages_list(self): return len(self.marionette.find_elements(*self._listed_languages_locator)) @property def selected_language(self): return self.marionette.find_element(*self._selected_language_input_locator).get_attribute( 'value') def tap_language(self, language): self.marionette.find_element(self._language_locator[0], self._language_locator[1] % language).tap() def a11y_click_language(self, language): self.accessibility.click(self.marionette.find_element(self._language_input_locator[0], self._language_input_locator[1] % language)) def tap_next(self): self.marionette.find_element(*self._next_button_locator).tap() def a11y_click_next(self): self.accessibility.click(self.marionette.find_element(*self._next_button_locator)) def tap_next_to_cell_data_section(self): self.tap_next() self.wait_for_element_displayed(*self._section_cell_data_locator) def a11y_click_next_to_cell_data_section(self): self.a11y_click_next() self.wait_for_element_displayed(*self._section_cell_data_locator) def enable_data(self): self.wait_for_element_displayed(*self._enable_data_checkbox_locator) self.marionette.find_element(*self._enable_data_checkbox_locator).tap() def a11y_enable_data(self): self.wait_for_element_displayed(*self._enable_data_checkbox_locator) self.accessibility.click(self.marionette.find_element(*self._enable_data_checkbox_locator)) def tap_next_to_wifi_section(self): self.tap_next() self.wait_for_condition(lambda m: not self.is_element_displayed(*self._progress_activity_locator)) self.wait_for_element_displayed(*self._section_wifi_locator) def a11y_click_next_to_wifi_section(self): self.a11y_click_next() self.wait_for_condition(lambda m: not self.is_element_displayed( *self._progress_activity_locator)) self.wait_for_element_displayed(*self._section_wifi_locator) def wait_for_networks_available(self): self.wait_for_condition(lambda m: len(m.find_elements(*self._found_wifi_networks_locator)) > 0, message='No networks listed on screen') def find_wifi_network(self, network_ssid): wifi_network_locator = (By.CSS_SELECTOR, '#networks-list li[data-ssid="%s"]' % network_ssid) wifi_network = self.wait_for_element_present(*wifi_network_locator) self.marionette.execute_script("arguments[0].scrollIntoView(false);", [wifi_network]) return wifi_network def connect_to_wifi(self, network_ssid, password, key_management=None): wifi_network = self.find_wifi_network(network_ssid) wifi_network.tap() # This is in the event we are using a Wifi Network that requires a password # We cannot be sure of this thus need the logic if key_management: self.wait_for_element_displayed(*self._password_input_locator) self.marionette.find_element(*self._password_input_locator).send_keys(password) self.marionette.find_element(*self._join_network_locator).tap() def a11y_connect_to_wifi(self, network_ssid, password, key_management=None): wifi_network = self.find_wifi_network(network_ssid) self.accessibility.click(wifi_network) # This is in the event we are using a Wifi Network that requires a password # We cannot be sure of this thus need the logic if key_management: self.wait_for_element_displayed(*self._password_input_locator) self.marionette.find_element(*self._password_input_locator).send_keys(password) self.accessibility.click(self.marionette.find_element(*self._join_network_locator)) def tap_next_to_timezone_section(self): self.tap_next() self.wait_for_element_displayed(*self._section_date_time_locator) def a11y_click_next_to_timezone_section(self): self.a11y_click_next() self.wait_for_element_displayed(*self._section_date_time_locator) def set_timezone_continent(self, continent): self.wait_for_element_displayed(*self._timezone_continent_locator) self.marionette.find_element(*self._timezone_continent_locator).tap() self.select(continent) def a11y_set_timezone_continent(self, continent): self.wait_for_element_displayed(*self._timezone_continent_locator) self.accessibility.click(self.marionette.find_element(*self._timezone_continent_locator)) self.a11y_select(continent) def set_timezone_city(self, city): self.wait_for_element_displayed(*self._timezone_city_locator) self.marionette.find_element(*self._timezone_city_locator).tap() self.select(city) def a11y_set_timezone_city(self, city): self.wait_for_element_displayed(*self._timezone_city_locator) self.accessibility.click(self.marionette.find_element(*self._timezone_city_locator)) self.a11y_select(city) @property def timezone_title(self): return self.marionette.find_element(*self._time_zone_title_locator).text def tap_next_to_geolocation_section(self): self.tap_next() self.wait_for_element_displayed(*self._section_geolocation_locator) def a11y_click_next_to_geolocation_section(self): self.a11y_click_next() self.wait_for_element_displayed(*self._section_geolocation_locator) def disable_geolocation(self): self.wait_for_element_displayed(*self._enable_geolocation_checkbox_locator) # TODO: Remove y parameter when Bug 932804 is fixed self.marionette.find_element(*self._enable_geolocation_checkbox_locator).tap(y=30) def a11y_disable_geolocation(self): self.wait_for_element_displayed(*self._enable_geolocation_checkbox_locator) self.accessibility.click(self.marionette.find_element( *self._enable_geolocation_checkbox_locator)) def tap_next_to_import_contacts_section(self): self.tap_next() self.wait_for_element_displayed(*self._section_import_contacts_locator) def a11y_click_next_to_import_contacts_section(self): self.a11y_click_next() self.wait_for_element_displayed(*self._section_import_contacts_locator) def tap_import_from_sim(self): self.marionette.find_element(*self._import_from_sim_locator).tap() def wait_for_contacts_imported(self): self.wait_for_condition(lambda m: self._pattern_contacts.match(m.find_element(*self._sim_import_feedback_locator).text) is not None, message='Contact did not import from sim before timeout') @property def count_imported_contacts(self): import_sim_message = self.marionette.find_element(*self._sim_import_feedback_locator).text import_sim_count = None if self._pattern_contacts_0.match(import_sim_message) is not None: import_sim_count = 0 elif self._pattern_contacts_1.match(import_sim_message) is not None: import_sim_count = 1 elif self._pattern_contacts_N.match(import_sim_message) is not None: count = self._pattern_contacts_N.match(import_sim_message).group(1) import_sim_count = int(count) return import_sim_count def tap_next_to_firefox_accounts_section(self): self.tap_next() self.wait_for_element_displayed(*self._section_firefox_accounts_locator) def a11y_click_next_to_firefox_accounts_section(self): self.a11y_click_next() self.wait_for_element_displayed(*self._section_firefox_accounts_locator) def tap_next_to_welcome_browser_section(self): self.tap_next() self.wait_for_element_displayed(*self._section_welcome_browser_locator) def a11y_click_next_to_welcome_browser_section(self): self.a11y_click_next() self.wait_for_element_displayed(*self._section_welcome_browser_locator) def tap_statistics_checkbox(self): self.marionette.find_element(*self._enable_statistic_checkbox_locator).tap() def a11y_click_statistics_checkbox(self): self.accessibility.click(self.marionette.find_element(*self._statistic_checkbox_locator)) def tap_next_to_privacy_browser_section(self): self.tap_next() self.wait_for_element_displayed(*self._section_browser_privacy_locator) def a11y_click_next_to_privacy_browser_section(self): self.a11y_click_next() self.wait_for_element_displayed(*self._section_browser_privacy_locator) def enter_email_address(self, email): # TODO assert that this is preserved in the system somewhere. Currently it is not used self.marionette.find_element(*self._email_field_locator).send_keys(email) def tap_next_to_finish_section(self): self.tap_next() self.wait_for_element_displayed(*self._section_finish_locator) def a11y_click_next_to_finish_section(self): self.a11y_click_next() self.wait_for_element_displayed(*self._section_finish_locator) def tap_skip_tour(self): self.marionette.find_element(*self._skip_tour_button_locator).tap() def a11y_click_skip_tour(self): self.accessibility.click(self.marionette.find_element(*self._skip_tour_button_locator)) def run_ftu_setup_with_default_values(self): count =0 while not self.is_element_displayed(*self._take_tour_button_locator): if self.is_element_displayed(*self._next_button_locator): self.tap_next() else: count=count+1 if count > 5: break def tap_take_tour(self): self.marionette.find_element(*self._take_tour_button_locator).tap() @property def step1_header_text(self): self.wait_for_element_displayed(*self._step_header_locator) return self.marionette.find_element(*self._step_header_locator).text def tap_tour_next(self): self.wait_for_element_displayed(*self._tour_next_button_locator) self.marionette.find_element(*self._tour_next_button_locator).tap() def tap_back(self): self.wait_for_element_displayed(*self._tour_next_button_locator) self.marionette.find_element(*self._tour_back_button_locator).tap() @property def step2_header_text(self): self.wait_for_element_displayed(*self._step_header_locator) return self.marionette.find_element(*self._step_header_locator).text @property def step3_header_text(self): self.wait_for_element_displayed(*self._step_header_locator) return self.marionette.find_element(*self._step_header_locator).text @property def step4_header_text(self): self.wait_for_element_displayed(*self._step_header_locator) return self.marionette.find_element(*self._step_header_locator).text @property def step5_header_text(self): self.wait_for_element_displayed(*self._step_header_locator) return self.marionette.find_element(*self._step_header_locator).text @property def step6_header_text(self): self.wait_for_element_displayed(*self._step_header_locator) return self.marionette.find_element(*self._step_header_locator).text def wait_for_finish_tutorial_section(self): self.wait_for_element_displayed(*self._section_tutorial_finish_locator) def tap_lets_go_button(self): self.marionette.find_element(*self._lets_go_button_locator).tap()
tests/python/gaia-ui-tests/gaiatest/apps/ftu/app.py
15,209
This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Step Languages section Step Cell data section Step Wifi Step Date & Time Step Geolocation Section Import contacts Step Firefox Accounts Section Welcome Browser Section Privacy Choices Section Finish Section Tour Section Tutorial Finish Pattern for import sim contacts message This is in the event we are using a Wifi Network that requires a password We cannot be sure of this thus need the logic This is in the event we are using a Wifi Network that requires a password We cannot be sure of this thus need the logic TODO: Remove y parameter when Bug 932804 is fixed TODO assert that this is preserved in the system somewhere. Currently it is not used
843
en
0.83755
import json import os from pyramid.settings import aslist from kinto.core.decorators import cache_forever HERE = os.path.abspath(os.path.dirname(__file__)) # Configured home page @cache_forever def admin_home_view(request): settings = { "authMethods": aslist(request.registry.settings.get('multiauth.policies')) } globalSettings = "<script>window.globalSettings = %s;</script>" % json.dumps(settings) # Update the file built by react-scripts to load the globalSettings. with open(os.path.join(HERE, 'build/index.html')) as f: return f.read().replace('<script', globalSettings + '<script')
kinto/plugins/admin/views.py
631
Configured home page Update the file built by react-scripts to load the globalSettings.
87
en
0.919459
# -------------------------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for license information. # -------------------------------------------------------------------------------------------- import json import unittest import mock import os import time import tempfile import requests import datetime from azure_devtools.scenario_tests import AllowLargeResponse, record_only from azure.cli.testsdk import (ScenarioTest, LocalContextScenarioTest, LiveScenarioTest, ResourceGroupPreparer, StorageAccountPreparer, JMESPathCheck, live_only) TEST_DIR = os.path.abspath(os.path.join(os.path.abspath(__file__), '..')) # pylint: disable=line-too-long # In the future, for any reasons the repository get removed, the source code is under "sample-repo-for-deployment-test" # you can use to rebuild the repository TEST_REPO_URL = 'https://github.com/yugangw-msft/azure-site-test.git' WINDOWS_ASP_LOCATION_WEBAPP = 'japanwest' WINDOWS_ASP_LOCATION_FUNCTIONAPP = 'francecentral' LINUX_ASP_LOCATION_WEBAPP = 'eastus2' LINUX_ASP_LOCATION_FUNCTIONAPP = 'ukwest' class WebappBasicE2ETest(ScenarioTest): @AllowLargeResponse() @ResourceGroupPreparer(location=WINDOWS_ASP_LOCATION_WEBAPP) def test_webapp_e2e(self, resource_group): webapp_name = self.create_random_name(prefix='webapp-e2e', length=24) plan = self.create_random_name(prefix='webapp-e2e-plan', length=24) self.cmd('appservice plan create -g {} -n {}'.format(resource_group, plan)) self.cmd('appservice plan list -g {}'.format(resource_group), checks=[ JMESPathCheck('length(@)', 1), JMESPathCheck('[0].name', plan), JMESPathCheck('[0].perSiteScaling', False) ]) # test idempotency self.cmd( 'appservice plan create -g {} -n {} --per-site-scaling'.format(resource_group, plan)) self.cmd('appservice plan list -g {}'.format(resource_group), checks=[ JMESPathCheck('length(@)', 1), JMESPathCheck('[0].name', plan), JMESPathCheck('[0].sku.tier', 'Basic'), JMESPathCheck('[0].sku.name', 'B1'), JMESPathCheck('[0].perSiteScaling', True) ]) self.cmd('appservice plan list -g {}'.format(resource_group), checks=[ JMESPathCheck("length([?name=='{}' && resourceGroup=='{}'])".format( plan, resource_group), 1) ]) self.cmd('appservice plan show -g {} -n {}'.format(resource_group, plan), checks=[ JMESPathCheck('name', plan) ]) self.cmd('webapp create -g {} -n {} --plan {}'.format(resource_group, webapp_name, plan), checks=[ JMESPathCheck('state', 'Running'), JMESPathCheck('name', webapp_name), JMESPathCheck('hostNames[0]', webapp_name + '.azurewebsites.net') ]) self.cmd('webapp create -g {} -n {} --plan {}'.format(resource_group, webapp_name, plan)) # test idempotency self.cmd('webapp list -g {}'.format(resource_group), checks=[ JMESPathCheck('length(@)', 1), JMESPathCheck('[0].name', webapp_name), JMESPathCheck('[0].hostNames[0]', webapp_name + '.azurewebsites.net') ]) self.cmd('webapp show -g {} -n {}'.format(resource_group, webapp_name), checks=[ JMESPathCheck('name', webapp_name), JMESPathCheck('hostNames[0]', webapp_name + '.azurewebsites.net') ]) result = self.cmd('webapp deployment source config-local-git -g {} -n {}'.format( resource_group, webapp_name)).get_output_in_json() self.assertTrue(result['url'].endswith(webapp_name + '.git')) self.cmd('webapp deployment source show -g {} -n {}'.format(resource_group, webapp_name), checks=[ JMESPathCheck( 'repoUrl', 'https://{}.scm.azurewebsites.net'.format(webapp_name)) ]) # turn on diagnostics test_cmd = ('webapp log config -g {} -n {} --level verbose'.format(resource_group, webapp_name) + ' ' '--application-logging true --detailed-error-messages true --failed-request-tracing true --web-server-logging filesystem') self.cmd(test_cmd) self.cmd('webapp log show -g {} -n {}'.format(resource_group, webapp_name), checks=[ JMESPathCheck('detailedErrorMessages.enabled', True), JMESPathCheck('failedRequestsTracing.enabled', True) ]) self.cmd('webapp config show -g {} -n {}'.format(resource_group, webapp_name), checks=[ JMESPathCheck('detailedErrorLoggingEnabled', True), JMESPathCheck('httpLoggingEnabled', True), JMESPathCheck('scmType', 'LocalGit'), JMESPathCheck('requestTracingEnabled', True) # TODO: contact webapp team for where to retrieve 'level' ]) # show publish profile info result = self.cmd('webapp deployment list-publishing-profiles -g {} -n {}'.format( resource_group, webapp_name)).get_output_in_json() self.assertTrue(result[1]['publishUrl'].startswith('ftp://')) self.cmd('webapp stop -g {} -n {}'.format(resource_group, webapp_name)) self.cmd('webapp show -g {} -n {}'.format(resource_group, webapp_name), checks=[ JMESPathCheck('state', 'Stopped'), JMESPathCheck('name', webapp_name) ]) self.cmd('webapp start -g {} -n {}'.format(resource_group, webapp_name)) self.cmd('webapp show -g {} -n {}'.format(resource_group, webapp_name), checks=[ JMESPathCheck('state', 'Running'), JMESPathCheck('name', webapp_name) ]) # show publishing credentials result = self.cmd('webapp deployment list-publishing-credentials -g {} -n {}'.format( resource_group, webapp_name)).get_output_in_json() self.assertTrue('scm' in result['scmUri']) # verify httpsOnly is false self.cmd('webapp show -g {} -n {}'.format(resource_group, webapp_name), checks=[ JMESPathCheck('httpsOnly', False), ]) # verify creating an non node app using --runtime self.cmd( 'webapp create -g {} -n {} --plan {} -r "php|7.3"'.format(resource_group, webapp_name, plan)) # TODO: Bug #14409. Re-enable check after fixing https://github.com/Azure/azure-cli/issues/14409 # self.cmd('webapp config show -g {} -n {}'.format(resource_group, webapp_name), checks=[ # JMESPathCheck('phpVersion', '7.3') # ]) def test_webapp_runtimes(self): self.cmd('webapp list-runtimes') class WebappQuickCreateTest(ScenarioTest): @ResourceGroupPreparer(location=WINDOWS_ASP_LOCATION_WEBAPP) def test_win_webapp_quick_create(self, resource_group): webapp_name = self.create_random_name(prefix='webapp-quick', length=24) plan = self.create_random_name(prefix='plan-quick', length=24) self.cmd('appservice plan create -g {} -n {}'.format(resource_group, plan)) r = self.cmd('webapp create -g {} -n {} --plan {} --deployment-local-git'.format( resource_group, webapp_name, plan)).get_output_in_json() self.assertTrue(r['ftpPublishingUrl'].startswith('ftp://')) self.cmd('webapp config appsettings list -g {} -n {}'.format(resource_group, webapp_name), checks=[ JMESPathCheck('[0].name', 'WEBSITE_NODE_DEFAULT_VERSION'), JMESPathCheck('[0].value', '10.14'), ]) @ResourceGroupPreparer(location=WINDOWS_ASP_LOCATION_WEBAPP) def test_win_webapp_quick_create_runtime(self, resource_group): webapp_name = self.create_random_name(prefix='webapp-quick', length=24) plan = self.create_random_name(prefix='plan-quick', length=24) self.cmd('appservice plan create -g {} -n {}'.format(resource_group, plan)) r = self.cmd('webapp create -g {} -n {} --plan {} --deployment-local-git -r "node|10.15"'.format( resource_group, webapp_name, plan)).get_output_in_json() self.assertTrue(r['ftpPublishingUrl'].startswith('ftp://')) self.cmd('webapp config appsettings list -g {} -n {}'.format(resource_group, webapp_name), checks=[ JMESPathCheck('[0].name', 'WEBSITE_NODE_DEFAULT_VERSION'), JMESPathCheck('[0].value', '10.14'), ]) @ResourceGroupPreparer(location=WINDOWS_ASP_LOCATION_WEBAPP) def test_win_webapp_quick_create_cd(self, resource_group): webapp_name = self.create_random_name(prefix='webapp-quick-cd', length=24) plan = self.create_random_name(prefix='plan-quick', length=24) self.cmd('appservice plan create -g {} -n {}'.format(resource_group, plan)) self.cmd('webapp create -g {} -n {} --plan {} --deployment-source-url {} -r "node|10.15"'.format( resource_group, webapp_name, plan, TEST_REPO_URL)) # 30 seconds should be enough for the deployment finished(Skipped under playback mode) time.sleep(30) r = requests.get('http://{}.azurewebsites.net'.format(webapp_name)) # verify the web page self.assertTrue('Hello world' in str(r.content)) @ResourceGroupPreparer(location='canadacentral') def test_linux_webapp_quick_create(self, resource_group): webapp_name = self.create_random_name( prefix='webapp-quick-linux', length=24) plan = self.create_random_name(prefix='plan-quick-linux', length=24) self.cmd( 'appservice plan create -g {} -n {} --is-linux'.format(resource_group, plan)) self.cmd('webapp create -g {} -n {} --plan {} -i patle/ruby-hello'.format( resource_group, webapp_name, plan)) r = requests.get( 'http://{}.azurewebsites.net'.format(webapp_name), timeout=240) # verify the web page self.assertTrue('Ruby on Rails in Web Apps on Linux' in str(r.content)) # verify app settings self.cmd('webapp config appsettings list -g {} -n {}'.format(resource_group, webapp_name), checks=[ JMESPathCheck('[0].name', 'WEBSITES_ENABLE_APP_SERVICE_STORAGE'), JMESPathCheck('[0].value', 'false'), ]) @ResourceGroupPreparer(location=LINUX_ASP_LOCATION_WEBAPP) def test_linux_webapp_multicontainer_create(self, resource_group): webapp_name = self.create_random_name( prefix='webapp-linux-multi', length=24) plan = self.create_random_name(prefix='plan-linux-multi', length=24) config_file = os.path.join(TEST_DIR, 'sample-compose.yml') self.cmd( 'appservice plan create -g {} -n {} --is-linux'.format(resource_group, plan)) self.cmd("webapp create -g {} -n {} --plan {} --multicontainer-config-file \"{}\" " "--multicontainer-config-type COMPOSE".format(resource_group, webapp_name, plan, config_file)) last_number_seen = 99999999 for x in range(0, 10): r = requests.get( 'http://{}.azurewebsites.net'.format(webapp_name), timeout=240) # verify the web page self.assertTrue('Hello World! I have been seen' in str(r.content)) current_number = [int(s) for s in r.content.split() if s.isdigit()][0] self.assertNotEqual(current_number, last_number_seen) last_number_seen = current_number @ResourceGroupPreparer(location=LINUX_ASP_LOCATION_WEBAPP) def test_linux_webapp_quick_create_cd(self, resource_group): webapp_name = self.create_random_name( prefix='webapp-linux-cd', length=24) plan = 'plan-quick-linux-cd' self.cmd( 'appservice plan create -g {} -n {} --is-linux'.format(resource_group, plan)) self.cmd('webapp create -g {} -n {} --plan {} -u {} -r "node|10.14"'.format(resource_group, webapp_name, plan, TEST_REPO_URL)) # 45 seconds should be enough for the deployment finished(Skipped under playback mode) time.sleep(45) r = requests.get( 'http://{}.azurewebsites.net'.format(webapp_name), timeout=240) # verify the web page if 'Hello world' not in str(r.content): # dump out more info for diagnose self.fail( "'Hello world' is not found in the web page. We get instead:" + str(r.content)) @ResourceGroupPreparer(parameter_name='resource_group', parameter_name_for_location='resource_group_location', location=WINDOWS_ASP_LOCATION_WEBAPP) @ResourceGroupPreparer(parameter_name='resource_group2', parameter_name_for_location='resource_group_location2', location=WINDOWS_ASP_LOCATION_WEBAPP) def test_create_in_different_group(self, resource_group, resource_group_location, resource_group2, resource_group_location2): plan = 'planInOneRG' self.cmd('group create -n {} -l {}'.format(resource_group2, resource_group_location)) plan_id = self.cmd('appservice plan create -g {} -n {}'.format( resource_group, plan)).get_output_in_json()['id'] self.cmd('webapp create -g {} -n webInOtherRG --plan {}'.format(resource_group2, plan_id), checks=[ JMESPathCheck('name', 'webInOtherRG') ]) class BackupWithName(ScenarioTest): @ResourceGroupPreparer(parameter_name='resource_group', location=WINDOWS_ASP_LOCATION_WEBAPP) def test_backup_with_name(self, resource_group): plan = self.create_random_name(prefix='plan-backup', length=24) self.cmd('appservice plan create -g {} -n {} --sku S1'.format(resource_group, plan)) webapp = self.create_random_name(prefix='backup-webapp', length=24) self.cmd('webapp create -g {} -n {} --plan {}'.format(resource_group, webapp, plan)) storage_Account = self.create_random_name(prefix='backup', length=24) self.cmd('storage account create -n {} -g {} --location {}'.format(storage_Account, resource_group, WINDOWS_ASP_LOCATION_WEBAPP)) container = self.create_random_name(prefix='backupcontainer', length=24) self.cmd('storage container create --account-name {} --name {}'.format(storage_Account, container)) expirydate = (datetime.datetime.now() + datetime.timedelta(days=1, hours=3)).strftime("\"%Y-%m-%dT%H:%MZ\"") sastoken = self.cmd('storage container generate-sas --account-name {} --name {} --expiry {} --permissions rwdl'.format(storage_Account, container, expirydate)) sasurl = '\"https://{}.blob.core.windows.net/{}?{}\"'.format(storage_Account, container, sastoken) backup_name = self.create_random_name(prefix='backup-name', length=24) self.cmd('webapp config backup create -g {} --webapp-name {} --backup-name {} --container-url {}'.format(resource_group, webapp, backup_name, sasurl), checks=[ JMESPathCheck('backupItemName', backup_name) ]) # Test Framework is not able to handle binary file format, hence, only run live class AppServiceLogTest(ScenarioTest): @ResourceGroupPreparer(location=WINDOWS_ASP_LOCATION_WEBAPP) def test_download_win_web_log(self, resource_group): import zipfile webapp_name = self.create_random_name( prefix='webapp-win-log', length=24) plan = self.create_random_name(prefix='win-log', length=24) self.cmd('appservice plan create -g {} -n {}'.format(resource_group, plan)) self.cmd('webapp create -g {} -n {} --plan {} --deployment-source-url {} -r "node|10.15"'.format( resource_group, webapp_name, plan, TEST_REPO_URL)) # 30 seconds should be enough for the deployment finished(Skipped under playback mode) time.sleep(30) # sanity check the traces _, log_file = tempfile.mkstemp() log_dir = log_file + '-dir' self.cmd('webapp log download -g {} -n {} --log-file "{}"'.format( resource_group, webapp_name, log_file)) zip_ref = zipfile.ZipFile(log_file, 'r') zip_ref.extractall(log_dir) self.assertTrue(os.path.isdir(os.path.join( log_dir, 'LogFiles', 'kudu', 'trace'))) @unittest.skip("Cannot pass under python3. Needs fixing.") @ResourceGroupPreparer(location='canadacentral') def test_download_linux_web_log(self, resource_group): import zipfile webapp_name = self.create_random_name( prefix='webapp-linux-log', length=24) plan = self.create_random_name(prefix='linux-log', length=24) self.cmd('appservice plan create -g {} -n {} --is-linux'.format(resource_group, plan)) self.cmd('webapp create -g {} -n {} --plan {} -i patle/ruby-hello'.format( resource_group, webapp_name, plan)) # load the site to produce a few traces requests.get( 'http://{}.azurewebsites.net'.format(webapp_name), timeout=240) # sanity check the traces _, log_file = tempfile.mkstemp() log_dir = log_file + '-dir' self.cmd('webapp log download -g {} -n {} --log-file "{}"'.format( resource_group, webapp_name, log_file)) zip_ref = zipfile.ZipFile(log_file, 'r') zip_ref.extractall(log_dir) self.assertTrue(os.path.isdir(os.path.join( log_dir, 'LogFiles', 'kudu', 'trace'))) class AppServicePlanScenarioTest(ScenarioTest): @ResourceGroupPreparer(location=WINDOWS_ASP_LOCATION_WEBAPP) def test_retain_plan(self, resource_group): webapp_name = self.create_random_name('web', 24) plan = self.create_random_name('web-plan', 24) self.cmd('appservice plan create -g {} -n {}'.format(resource_group, plan)) self.cmd( 'webapp create -g {} -n {} --plan {}'.format(resource_group, webapp_name, plan)) self.cmd('webapp delete -g {} -n {} --keep-dns-registration --keep-empty-plan --keep-metrics'.format(resource_group, webapp_name)) self.cmd('appservice plan list -g {}'.format(resource_group), checks=[ JMESPathCheck('[0].name', plan) ]) @ResourceGroupPreparer(location=WINDOWS_ASP_LOCATION_WEBAPP) def test_auto_delete_plan(self, resource_group): webapp_name = self.create_random_name('web-del-test', 24) plan = self.create_random_name('web-del-plan', 24) self.cmd( 'appservice plan create -g {} -n {} -l {}'.format(resource_group, plan, WINDOWS_ASP_LOCATION_WEBAPP)) self.cmd('appservice plan update -g {} -n {} --sku S1'.format(resource_group, plan), checks=[JMESPathCheck('name', plan), JMESPathCheck('sku.tier', 'Standard'), JMESPathCheck('sku.name', 'S1')]) self.cmd( 'webapp create -g {} -n {} --plan {}'.format(resource_group, webapp_name, plan)) self.cmd('webapp delete -g {} -n {}'.format(resource_group, webapp_name)) # test empty service plan should be automatically deleted. self.cmd('appservice plan list -g {}'.format(resource_group), checks=[JMESPathCheck('length(@)', 0)]) class WebappConfigureTest(ScenarioTest): @ResourceGroupPreparer(name_prefix='cli_test_webapp_config', location=WINDOWS_ASP_LOCATION_WEBAPP) def test_webapp_config(self, resource_group): webapp_name = self.create_random_name('webapp-config-test', 40) plan_name = self.create_random_name('webapp-config-plan', 40) self.cmd( 'appservice plan create -g {} -n {} --sku S1'.format(resource_group, plan_name)) self.cmd( 'webapp create -g {} -n {} --plan {}'.format(resource_group, webapp_name, plan_name)) # verify the baseline self.cmd('webapp config show -g {} -n {}'.format(resource_group, webapp_name)).assert_with_checks([ JMESPathCheck('alwaysOn', True), JMESPathCheck('autoHealEnabled', False), JMESPathCheck('phpVersion', '5.6'), JMESPathCheck('netFrameworkVersion', 'v4.0'), JMESPathCheck('pythonVersion', ''), JMESPathCheck('use32BitWorkerProcess', True), JMESPathCheck('webSocketsEnabled', False), JMESPathCheck('minTlsVersion', '1.2'), JMESPathCheck('ftpsState', 'AllAllowed')]) # update and verify checks = [ JMESPathCheck('alwaysOn', True), JMESPathCheck('autoHealEnabled', True), JMESPathCheck('phpVersion', '7.2'), JMESPathCheck('netFrameworkVersion', 'v3.0'), JMESPathCheck('pythonVersion', '3.4'), JMESPathCheck('use32BitWorkerProcess', False), JMESPathCheck('webSocketsEnabled', True), JMESPathCheck('minTlsVersion', '1.0'), JMESPathCheck('http20Enabled', True), JMESPathCheck('ftpsState', 'Disabled')] self.cmd('webapp config set -g {} -n {} --always-on true --auto-heal-enabled true --php-version 7.2 ' '--net-framework-version v3.5 --python-version 3.4 --use-32bit-worker-process=false ' '--web-sockets-enabled=true --http20-enabled true --min-tls-version 1.0 --ftps-state Disabled'.format(resource_group, webapp_name)).assert_with_checks(checks) self.cmd('webapp config show -g {} -n {}'.format(resource_group, webapp_name)) \ .assert_with_checks(checks) # site appsettings testing # update through key value pairs self.cmd('webapp config appsettings set -g {} -n {} --settings s1=foo s2=bar s3=bar2'.format(resource_group, webapp_name)).assert_with_checks([ JMESPathCheck("length([?name=='s1'])", 1), JMESPathCheck("length([?name=='s2'])", 1), JMESPathCheck("length([?name=='s3'])", 1), JMESPathCheck("length([?value=='foo'])", 1), JMESPathCheck("length([?value=='bar'])", 1), JMESPathCheck("length([?value=='bar2'])", 1) ]) # show result = self.cmd('webapp config appsettings list -g {} -n {}'.format( resource_group, webapp_name)).get_output_in_json() s2 = next((x for x in result if x['name'] == 's2')) self.assertEqual(s2['name'], 's2') self.assertEqual(s2['slotSetting'], False) self.assertEqual(s2['value'], 'bar') self.assertEqual(set([x['name'] for x in result]), set( ['s1', 's2', 's3', 'WEBSITE_NODE_DEFAULT_VERSION'])) # delete self.cmd('webapp config appsettings delete -g {} -n {} --setting-names s1 s2' .format(resource_group, webapp_name)).assert_with_checks([ JMESPathCheck("length([?name=='s3'])", 1), JMESPathCheck("length([?name=='s1'])", 0), JMESPathCheck("length([?name=='s2'])", 0)]) # hostnames self.cmd('webapp config hostname list -g {} --webapp-name {}' .format(resource_group, webapp_name)).assert_with_checks([ JMESPathCheck('length(@)', 1), JMESPathCheck('[0].name', '{0}.azurewebsites.net'.format(webapp_name))]) # site azure storage account configurations tests runtime = 'node|10.16' linux_plan = self.create_random_name( prefix='webapp-linux-plan', length=24) linux_webapp = self.create_random_name( prefix='webapp-linux', length=24) self.cmd('appservice plan create -g {} -n {} -l eastus2 --sku S1 --is-linux'.format(resource_group, linux_plan), checks=[ # this weird field means it is a linux JMESPathCheck('reserved', True), JMESPathCheck('sku.name', 'S1'), ]) self.cmd('webapp create -g {} -n {} --plan {} --runtime {}'.format(resource_group, linux_webapp, linux_plan, runtime), checks=[ JMESPathCheck('name', linux_webapp), ]) # add self.cmd(('webapp config storage-account add -g {} -n {} --custom-id Id --storage-type AzureFiles --account-name name ' '--share-name sharename --access-key key --mount-path /path/to/mount').format(resource_group, linux_webapp)) self.cmd('webapp config storage-account list -g {} -n {}'.format(resource_group, linux_webapp)).assert_with_checks([ JMESPathCheck('length(@)', 1), JMESPathCheck("[?name=='Id']|[0].value.type", "AzureFiles"), JMESPathCheck("[?name=='Id']|[0].value.accountName", "name"), JMESPathCheck("[?name=='Id']|[0].value.shareName", "sharename"), JMESPathCheck("[?name=='Id']|[0].value.accessKey", "key"), JMESPathCheck("[?name=='Id']|[0].value.mountPath", "/path/to/mount")]) # update self.cmd('webapp config storage-account update -g {} -n {} --custom-id Id --mount-path /different/path' .format(resource_group, linux_webapp)) self.cmd('webapp config storage-account list -g {} -n {}'.format(resource_group, linux_webapp)).assert_with_checks([ JMESPathCheck("length(@)", 1), JMESPathCheck("[?name=='Id']|[0].value.type", "AzureFiles"), JMESPathCheck("[?name=='Id']|[0].value.accountName", "name"), JMESPathCheck("[?name=='Id']|[0].value.shareName", "sharename"), JMESPathCheck("[?name=='Id']|[0].value.accessKey", "key"), JMESPathCheck("[?name=='Id']|[0].value.mountPath", "/different/path")]) # list self.cmd('webapp config storage-account list -g {} -n {}'.format(resource_group, linux_webapp)).assert_with_checks([ JMESPathCheck("length(@)", 1), JMESPathCheck('[0].name', 'Id')]) # delete self.cmd('webapp config storage-account delete -g {} -n {} --custom-id Id'.format(resource_group, linux_webapp)).assert_with_checks([ JMESPathCheck("length(@)", 0)]) # site connection string tests self.cmd('webapp config connection-string set -t mysql -g {} -n {} --settings c1="conn1" c2=conn2 ' '--slot-settings c3=conn3'.format(resource_group, linux_webapp)) self.cmd('webapp config connection-string list -g {} -n {}' .format(resource_group, linux_webapp)).assert_with_checks([ JMESPathCheck('length([])', 3), JMESPathCheck("[?name=='c1']|[0].slotSetting", False), JMESPathCheck("[?name=='c1']|[0].type", 'MySql'), JMESPathCheck("[?name=='c1']|[0].value", 'conn1'), JMESPathCheck("[?name=='c2']|[0].slotSetting", False), JMESPathCheck("[?name=='c3']|[0].slotSetting", True)]) self.cmd('webapp config connection-string delete -g {} -n {} --setting-names c1 c3' .format(resource_group, linux_webapp)) self.cmd('webapp config connection-string list -g {} -n {}' .format(resource_group, linux_webapp)).assert_with_checks([ JMESPathCheck('length([])', 1), JMESPathCheck('[0].slotSetting', False), JMESPathCheck('[0].name', 'c2')]) # see deployment user; just make sure the command does return something self.assertTrue( self.cmd('webapp deployment user show').get_output_in_json()['type']) @AllowLargeResponse() @ResourceGroupPreparer(name_prefix='cli_test_webapp_config_appsettings', location=WINDOWS_ASP_LOCATION_WEBAPP) def test_webapp_config_appsettings(self, resource_group): webapp_name = self.create_random_name('webapp-config-appsettings-test', 40) plan_name = self.create_random_name('webapp-config-appsettings-plan', 40) self.cmd( 'appservice plan create -g {} -n {} --sku S1'.format(resource_group, plan_name)) self.cmd( 'webapp create -g {} -n {} --plan {}'.format(resource_group, webapp_name, plan_name)) # site appsettings testing # update through key value pairs self.cmd('webapp config appsettings set -g {} -n {} --settings s1=foo s2=bar s3=bar2'.format(resource_group, webapp_name)).assert_with_checks([ JMESPathCheck("length([?name=='s1'])", 1), JMESPathCheck("length([?name=='s2'])", 1), JMESPathCheck("length([?name=='s3'])", 1), JMESPathCheck("length([?value=='foo'])", 1), JMESPathCheck("length([?value=='bar'])", 1), JMESPathCheck("length([?value=='bar2'])", 1) ]) # show result = self.cmd('webapp config appsettings list -g {} -n {}'.format( resource_group, webapp_name)).get_output_in_json() s2 = next((x for x in result if x['name'] == 's2')) self.assertEqual(s2['name'], 's2') self.assertEqual(s2['slotSetting'], False) self.assertEqual(s2['value'], 'bar') self.assertEqual(set([x['name'] for x in result]), set( ['s1', 's2', 's3', 'WEBSITE_NODE_DEFAULT_VERSION'])) self.cmd( 'webapp create -g {} -n {} --plan {}'.format(resource_group, webapp_name, plan_name)) # show result = self.cmd('webapp config appsettings list -g {} -n {}'.format( resource_group, webapp_name)).get_output_in_json() s2 = next((x for x in result if x['name'] == 's2')) self.assertEqual(s2['name'], 's2') self.assertEqual(s2['slotSetting'], False) self.assertEqual(s2['value'], 'bar') self.assertEqual(set([x['name'] for x in result]), set( ['s1', 's2', 's3', 'WEBSITE_NODE_DEFAULT_VERSION'])) @ResourceGroupPreparer(name_prefix='cli_test_webapp_json', location=WINDOWS_ASP_LOCATION_WEBAPP) def test_update_webapp_settings_thru_json(self, resource_group): webapp_name = self.create_random_name('webapp-config-test', 40) plan_name = self.create_random_name('webapp-config-plan', 40) # update through a json file with key value pair _, settings_file = tempfile.mkstemp() with open(settings_file, 'w+') as file: file.write(json.dumps({'s2': 'value2'})) self.cmd( 'appservice plan create -g {} -n {} --sku S1'.format(resource_group, plan_name)) self.cmd( 'webapp create -g {} -n {} --plan {}'.format(resource_group, webapp_name, plan_name)) output = self.cmd('webapp config appsettings set -g {} -n {} --settings s=value "@{}"'.format( resource_group, webapp_name, settings_file)).get_output_in_json() output = [s for s in output if s['name'] in ['s', 's2']] output.sort(key=lambda s: s['name']) self.assertEqual(output[0], { 'name': 's', 'value': 'value', 'slotSetting': False }) self.assertEqual(output[1], { 'name': 's2', 'value': 'value2', 'slotSetting': False }) # output using the output of the set/list command output.append({ 'name': 's3', 'value': 'value3', 'slotSetting': True }) with open(settings_file, 'w') as file: file.write(json.dumps(output)) output = self.cmd('webapp config appsettings set -g {} -n {} --settings "@{}"'.format( resource_group, webapp_name, settings_file)).get_output_in_json() output = [s for s in output if s['name'] in ['s', 's2', 's3']] output.sort(key=lambda s: s['name']) self.assertEqual(output[0], { 'name': 's', 'value': 'value', 'slotSetting': False }) self.assertEqual(output[1], { 'name': 's2', 'value': 'value2', 'slotSetting': False }) self.assertEqual(output[2], { 'name': 's3', 'value': 'value3', 'slotSetting': True }) # update site config site_configs = { "requestTracingEnabled": True, "alwaysOn": True } with open(settings_file, 'w') as file: file.write(json.dumps(site_configs)) self.cmd('webapp config set -g {} -n {} --generic-configurations "@{}"'.format(resource_group, webapp_name, settings_file)).assert_with_checks([ JMESPathCheck("requestTracingEnabled", True), JMESPathCheck("alwaysOn", True), ]) class WebappScaleTest(ScenarioTest): @ResourceGroupPreparer(location=WINDOWS_ASP_LOCATION_WEBAPP) def test_webapp_scale(self, resource_group): plan = self.create_random_name(prefix='scale-plan', length=24) # start with shared sku self.cmd('appservice plan create -g {} -n {} --sku SHARED'.format(resource_group, plan), checks=[ JMESPathCheck('sku.name', 'D1'), JMESPathCheck('sku.tier', 'Shared'), JMESPathCheck('sku.size', 'D1'), JMESPathCheck('sku.family', 'D'), # 0 means the default value: 1 instance JMESPathCheck('sku.capacity', 0) ]) # scale up self.cmd( 'appservice plan update -g {} -n {} --sku S2'.format(resource_group, plan)) self.cmd('appservice plan show -g {} -n {}'.format(resource_group, plan), checks=[ JMESPathCheck('sku.name', 'S2'), JMESPathCheck('sku.tier', 'Standard'), JMESPathCheck('sku.size', 'S2'), JMESPathCheck('sku.family', 'S') ]) # scale down self.cmd( 'appservice plan update -g {} -n {} --sku B1'.format(resource_group, plan)) self.cmd('appservice plan show -g {} -n {}'.format(resource_group, plan), checks=[ JMESPathCheck('sku.name', 'B1'), JMESPathCheck('sku.tier', 'Basic'), JMESPathCheck('sku.size', 'B1'), JMESPathCheck('sku.family', 'B') ]) # scale out self.cmd( 'appservice plan update -g {} -n {} --number-of-workers 2'.format(resource_group, plan)) self.cmd('appservice plan show -g {} -n {}'.format(resource_group, plan), checks=[ JMESPathCheck('sku.name', 'B1'), JMESPathCheck('sku.tier', 'Basic'), JMESPathCheck('sku.size', 'B1'), JMESPathCheck('sku.family', 'B'), JMESPathCheck('sku.capacity', 2) ]) class AppServiceBadErrorPolishTest(ScenarioTest): @ResourceGroupPreparer(parameter_name='resource_group', location=WINDOWS_ASP_LOCATION_WEBAPP) @ResourceGroupPreparer(parameter_name='resource_group2', location=WINDOWS_ASP_LOCATION_WEBAPP) def test_appservice_error_polish(self, resource_group, resource_group2): plan = self.create_random_name(prefix='web-error-plan', length=24) webapp_name = self.create_random_name(prefix='web-error', length=24) self.cmd('group create -n {} -l {}'.format(resource_group2, WINDOWS_ASP_LOCATION_WEBAPP)) self.cmd( 'appservice plan create -g {} -n {} --sku b1'.format(resource_group, plan)) self.cmd( 'webapp create -g {} -n {} --plan {}'.format(resource_group, webapp_name, plan)) self.cmd( 'appservice plan create -g {} -n {} --sku b1'.format(resource_group2, plan)) # we will try to produce an error by try creating 2 webapp with same name in different groups self.cmd('webapp create -g {} -n {} --plan {}'.format(resource_group2, webapp_name, plan), expect_failure=True) # TODO: ensure test fx can capture error details for us to verify # allowed_exceptions='Website with given name {} already exists'.format(webapp_name) # this test doesn't contain the ultimate verification which you need to manually load the frontpage in a browser class LinuxWebappScenarioTest(ScenarioTest): @ResourceGroupPreparer(location=LINUX_ASP_LOCATION_WEBAPP) def test_linux_webapp(self, resource_group): runtime = 'node|10.16' plan = self.create_random_name(prefix='webapp-linux-plan', length=24) webapp = self.create_random_name(prefix='webapp-linux', length=24) self.cmd('appservice plan create -g {} -n {} --sku S1 --is-linux' .format(resource_group, plan), checks=[ # this weird field means it is a linux JMESPathCheck('reserved', True), JMESPathCheck('sku.name', 'S1'), ]) self.cmd('webapp create -g {} -n {} --plan {} --runtime {}'.format(resource_group, webapp, plan, runtime), checks=[ JMESPathCheck('name', webapp), ]) self.cmd('webapp config show -g {} -n {}'.format(resource_group, webapp), checks=[ JMESPathCheck('windowsFxVersion', None) ]) # workaround the fact that a new linux web's "kind" won't be settled instantatest_linux_webapp_remote_sshneously time.sleep(30) self.cmd('webapp list -g {}'.format(resource_group), checks=[ JMESPathCheck('length([])', 1), JMESPathCheck('[0].name', webapp), JMESPathCheck('[0].kind', 'app,linux') ]) self.cmd('webapp show -g {} -n {}'.format(resource_group, webapp), checks=[ JMESPathCheck('name', webapp), JMESPathCheck('kind', 'app,linux') ]) self.cmd('webapp config set -g {} -n {} --startup-file {}'.format(resource_group, webapp, 'process.json'), checks=[ JMESPathCheck('appCommandLine', 'process.json') ]) result = self.cmd('webapp deployment container config -g {} -n {} --enable-cd true'.format( resource_group, webapp)).get_output_in_json() self.assertTrue(result['CI_CD_URL'].startswith('https://')) self.assertTrue(result['CI_CD_URL'].endswith( '.scm.azurewebsites.net/docker/hook')) result = self.cmd('webapp config container set -g {} -n {} --docker-custom-image-name {} --docker-registry-server-password {} --docker-registry-server-user {} --docker-registry-server-url {} --enable-app-service-storage {}'.format( resource_group, webapp, 'foo-image', 'foo-password', 'foo-user', 'foo-url', 'false')).get_output_in_json() self.assertEqual(set(x['value'] for x in result if x['name'] == 'DOCKER_REGISTRY_SERVER_PASSWORD'), set([None])) # we mask the password result = self.cmd('webapp config container show -g {} -n {} '.format( resource_group, webapp)).get_output_in_json() self.assertEqual(set(x['name'] for x in result), set(['DOCKER_REGISTRY_SERVER_URL', 'DOCKER_REGISTRY_SERVER_USERNAME', 'DOCKER_CUSTOM_IMAGE_NAME', 'DOCKER_REGISTRY_SERVER_PASSWORD', 'WEBSITES_ENABLE_APP_SERVICE_STORAGE'])) self.assertEqual(set(x['value'] for x in result if x['name'] == 'DOCKER_REGISTRY_SERVER_PASSWORD'), set([None])) # we mask the password sample = next( (x for x in result if x['name'] == 'DOCKER_REGISTRY_SERVER_URL')) self.assertEqual(sample, { 'name': 'DOCKER_REGISTRY_SERVER_URL', 'slotSetting': False, 'value': 'foo-url'}) sample = next( (x for x in result if x['name'] == 'WEBSITES_ENABLE_APP_SERVICE_STORAGE')) self.assertEqual(sample, { 'name': 'WEBSITES_ENABLE_APP_SERVICE_STORAGE', 'slotSetting': False, 'value': 'false'}) self.cmd( 'webapp config container delete -g {} -n {}'.format(resource_group, webapp)) result2 = self.cmd('webapp config container show -g {} -n {} '.format( resource_group, webapp)).get_output_in_json() self.assertEqual(result2, []) @unittest.skip('This is failing on windows OS. Rised a bug #12844 to fix in future releases') class LinuxWebappSSHScenarioTest(ScenarioTest): @ResourceGroupPreparer(location=LINUX_ASP_LOCATION_WEBAPP) def test_linux_webapp_ssh(self, resource_group): # On Windows, test 'webapp ssh' throws error import platform if platform.system() == "Windows": from azure.cli.core.util import CLIError with self.assertRaises(CLIError): self.cmd('webapp ssh -g {} -n {} --timeout 5'.format("foo", "bar")) return runtime = 'node|12-lts' plan = self.create_random_name(prefix='webapp-ssh-plan', length=24) webapp = self.create_random_name(prefix='webapp-ssh', length=24) self.cmd( 'appservice plan create -g {} -n {} --sku S1 --is-linux' .format(resource_group, plan)) self.cmd('webapp create -g {} -n {} --plan {} --runtime {}'.format( resource_group, webapp, plan, runtime)) time.sleep(30) requests.get('http://{}.azurewebsites.net'.format(webapp), timeout=240) time.sleep(30) self.cmd('webapp ssh -g {} -n {} --timeout 5'.format(resource_group, webapp)) time.sleep(30) class LinuxWebappRemoteSSHScenarioTest(ScenarioTest): @ResourceGroupPreparer(location=LINUX_ASP_LOCATION_WEBAPP) def test_linux_webapp_remote_ssh(self, resource_group): runtime = 'node|12-lts' plan = self.create_random_name( prefix='webapp-remote-ssh-plan', length=40) webapp = self.create_random_name(prefix='webapp-remote-ssh', length=40) self.cmd( 'appservice plan create -g {} -n {} --sku S1 --is-linux' .format(resource_group, plan)) self.cmd('webapp create -g {} -n {} --plan {} --runtime {}'.format( resource_group, webapp, plan, runtime)) time.sleep(30) requests.get('http://{}.azurewebsites.net'.format(webapp), timeout=240) time.sleep(30) self.cmd( 'webapp create-remote-connection -g {} -n {} --timeout 5'.format(resource_group, webapp)) time.sleep(30) class LinuxWebappRemoteDebugScenarioTest(ScenarioTest): @unittest.skip("Bug #14427. Re-enable test after fixing https://github.com/Azure/azure-cli/issues/14427") @ResourceGroupPreparer(location=LINUX_ASP_LOCATION_WEBAPP) def test_linux_webapp_remote_debug(self, resource_group): runtime = 'node|12-lts' plan = self.create_random_name( prefix='webapp-remote-debug-plan', length=40) webapp = self.create_random_name( prefix='webapp-remote-debug', length=40) self.cmd( 'appservice plan create -g {} -n {} --sku S1 --is-linux' .format(resource_group, plan)) self.cmd('webapp create -g {} -n {} --plan {} --runtime {}'.format( resource_group, webapp, plan, runtime)) time.sleep(30) self.cmd( 'webapp config set --remote-debugging-enabled true -g {} -n {}'.format(resource_group, webapp)) requests.get('http://{}.azurewebsites.net'.format(webapp), timeout=240) time.sleep(30) self.cmd( 'webapp create-remote-connection -g {} -n {} --timeout 5'.format(resource_group, webapp)) time.sleep(30) class LinuxWebappMulticontainerSlotScenarioTest(ScenarioTest): @ResourceGroupPreparer(location=LINUX_ASP_LOCATION_WEBAPP) def test_linux_webapp_multicontainer_slot(self, resource_group): webapp_name = self.create_random_name( prefix='webapp-linux-multi', length=24) plan = self.create_random_name(prefix='plan-linux-multi', length=24) config_file = os.path.join(TEST_DIR, 'sample-compose.yml') slot = "stage" slot_webapp_name = "{}-{}".format(webapp_name, slot) slot_config_file = os.path.join(TEST_DIR, 'sample-compose-slot.yml') self.cmd( 'appservice plan create -g {} -n {} --is-linux --sku S1'.format(resource_group, plan)) self.cmd("webapp create -g {} -n {} --plan {} --multicontainer-config-file \"{}\" " "--multicontainer-config-type COMPOSE".format(resource_group, webapp_name, plan, config_file)) last_number_seen = 99999999 for x in range(0, 10): r = requests.get( 'http://{}.azurewebsites.net'.format(webapp_name), timeout=240) # verify the web page self.assertTrue('Hello World! I have been seen' in str(r.content)) current_number = [int(s) for s in r.content.split() if s.isdigit()][0] self.assertNotEqual(current_number, last_number_seen) last_number_seen = current_number self.cmd('webapp deployment slot create -g {} -n {} --slot {}'.format( resource_group, webapp_name, slot)) self.cmd("webapp config container set -g {} -n {} --slot {} --multicontainer-config-file \"{}\" " "--multicontainer-config-type COMPOSE".format(resource_group, webapp_name, slot, slot_config_file)) last_number_seen = 99999999 for x in range(0, 10): r = requests.get( 'http://{}.azurewebsites.net'.format(slot_webapp_name), timeout=240) # verify the web page self.assertTrue( 'Hello from a slot! I have been seen' in str(r.content)) current_number = [int(s) for s in r.content.split() if s.isdigit()][0] self.assertNotEqual(current_number, last_number_seen) last_number_seen = current_number class WebappACRScenarioTest(ScenarioTest): @ResourceGroupPreparer(location=LINUX_ASP_LOCATION_WEBAPP) def test_acr_integration(self, resource_group): plan = self.create_random_name(prefix='acrtestplan', length=24) webapp = self.create_random_name(prefix='webappacrtest', length=24) runtime = 'node|10.16' acr_registry_name = webapp self.cmd('acr create --admin-enabled -g {} -n {} --sku Basic'.format( resource_group, acr_registry_name)) self.cmd( 'appservice plan create -g {} -n {} --sku S1 --is-linux' .format(resource_group, plan)) self.cmd('webapp create -g {} -n {} --plan {} --runtime {}'.format( resource_group, webapp, plan, runtime)) creds = self.cmd('acr credential show -n {} -g {}'.format( acr_registry_name, resource_group)).get_output_in_json() self.cmd('webapp config container set -g {0} -n {1} --docker-custom-image-name {2}.azurecr.io/image-name:latest --docker-registry-server-url https://{2}.azurecr.io'.format( resource_group, webapp, acr_registry_name), checks=[ JMESPathCheck( "[?name=='DOCKER_REGISTRY_SERVER_USERNAME']|[0].value", creds['username']) ]) class FunctionappACRScenarioTest(ScenarioTest): @ResourceGroupPreparer(location='northeurope') @StorageAccountPreparer() @AllowLargeResponse() def test_acr_integration_function_app(self, resource_group, storage_account): plan = self.create_random_name(prefix='acrtestplanfunction', length=24) functionapp = self.create_random_name( prefix='functionappacrtest', length=24) runtime = 'node' acr_registry_name = functionapp self.cmd('acr create --admin-enabled -g {} -n {} --sku Basic'.format( resource_group, acr_registry_name)) self.cmd( 'appservice plan create -g {} -n {} --sku S1 --is-linux' .format(resource_group, plan)) self.cmd('functionapp create -g {} -n {} -s {} --plan {} --runtime {}'.format( resource_group, functionapp, storage_account, plan, runtime)) creds = self.cmd('acr credential show -n {} -g {}'.format( acr_registry_name, resource_group)).get_output_in_json() self.cmd('functionapp config container set -g {0} -n {1} --docker-custom-image-name {2}.azurecr.io/image-name:latest --docker-registry-server-url https://{2}.azurecr.io'.format( resource_group, functionapp, acr_registry_name), checks=[ JMESPathCheck( "[?name=='DOCKER_REGISTRY_SERVER_USERNAME']|[0].value", creds['username']) ]) self.cmd('functionapp config container show -g {} -n {} '.format(resource_group, functionapp), checks=[ JMESPathCheck( "[?name=='DOCKER_REGISTRY_SERVER_USERNAME']|[0].value", creds['username']), JMESPathCheck( "[?name=='DOCKER_REGISTRY_SERVER_URL']|[0].name", 'DOCKER_REGISTRY_SERVER_URL') ]) self.cmd('functionapp config appsettings list -g {} -n {}'.format(resource_group, functionapp), checks=[ JMESPathCheck( "[?name=='FUNCTIONS_WORKER_RUNTIME'].value|[0]", 'node'), JMESPathCheck( "[?name=='DOCKER_REGISTRY_SERVER_USERNAME'].value|[0]", creds['username']) ]) self.cmd( 'functionapp config container delete -g {} -n {} '.format(resource_group, functionapp)) json_result = self.cmd('functionapp config appsettings list -g {} -n {}'.format( resource_group, functionapp)).get_output_in_json() all_settings = [setting['name'] for setting in json_result] # Make sure the related settings are deleted self.assertNotIn('DOCKER_REGISTRY_SERVER_USERNAME', all_settings) self.assertNotIn('DOCKER_REGISTRY_SERVER_URL', all_settings) self.assertNotIn('DOCKER_REGISTRY_SERVER_PASSWORD', all_settings) self.assertIn('FUNCTIONS_WORKER_RUNTIME', all_settings) self.cmd('functionapp delete -g {} -n {}'.format(resource_group, functionapp)) class FunctionAppCreateUsingACR(ScenarioTest): @ResourceGroupPreparer(location='brazilsouth') @StorageAccountPreparer(name_prefix='clitestacr') @AllowLargeResponse() def test_acr_create_function_app(self, resource_group, storage_account): plan = self.create_random_name(prefix='acrtestplanfunction', length=24) functionapp = self.create_random_name( prefix='functionappacrtest', length=24) runtime = 'node' acr_registry_name = functionapp self.cmd('acr create --admin-enabled -g {} -n {} --sku Basic'.format( resource_group, acr_registry_name)) acr_creds = self.cmd('acr credential show -n {} -g {}'.format( acr_registry_name, resource_group)).get_output_in_json() username = acr_creds['username'] password = acr_creds['passwords'][0]['value'] self.cmd( 'functionapp plan create -g {} -n {} --sku S1 --is-linux'.format(resource_group, plan)) self.cmd('functionapp create -g {} -n {} -s {} --plan {} --runtime {}' ' --deployment-container-image-name {}.azurecr.io/image-name:latest --docker-registry-server-user {}' ' --docker-registry-server-password {}'.format(resource_group, functionapp, storage_account, plan, runtime, acr_registry_name, username, password)) self.cmd('functionapp config container show -g {} -n {} '.format(resource_group, functionapp), checks=[ JMESPathCheck( "[?name=='DOCKER_REGISTRY_SERVER_USERNAME']|[0].value", username), JMESPathCheck( "[?name=='DOCKER_REGISTRY_SERVER_URL']|[0].name", 'DOCKER_REGISTRY_SERVER_URL') ]) self.cmd('functionapp config appsettings list -g {} -n {}'.format(resource_group, functionapp), checks=[ JMESPathCheck( "[?name=='FUNCTIONS_WORKER_RUNTIME'].value|[0]", 'node'), JMESPathCheck( "[?name=='DOCKER_REGISTRY_SERVER_USERNAME'].value|[0]", username) ]) self.cmd('functionapp config show -g {} -n {}'.format(resource_group, functionapp), checks=[ JMESPathCheck('linuxFxVersion', 'DOCKER|{}.azurecr.io/image-name:latest'.format(acr_registry_name))]) self.cmd( 'functionapp config container delete -g {} -n {} '.format(resource_group, functionapp)) json_result = self.cmd( 'functionapp config appsettings list -g {} -n {}'.format(resource_group, functionapp)).get_output_in_json() all_settings = [setting['name'] for setting in json_result] # Make sure the related settings are deleted self.assertNotIn('DOCKER_REGISTRY_SERVER_USERNAME', all_settings) self.assertNotIn('DOCKER_REGISTRY_SERVER_URL', all_settings) self.assertNotIn('DOCKER_REGISTRY_SERVER_PASSWORD', all_settings) self.assertIn('FUNCTIONS_WORKER_RUNTIME', all_settings) class FunctionappACRDeploymentScenarioTest(ScenarioTest): @ResourceGroupPreparer(location='brazilsouth') @StorageAccountPreparer(name_prefix='clitestacrdeploy') def test_acr_deployment_function_app(self, resource_group, storage_account): plan = self.create_random_name(prefix='acrtestplanfunction', length=24) functionapp = self.create_random_name( prefix='functionappacrtest', length=24) runtime = 'node' acr_registry_name = functionapp self.cmd('acr create --admin-enabled -g {} -n {} --sku Basic'.format( resource_group, acr_registry_name)) self.cmd( 'appservice plan create -g {} -n {} --sku S1 --is-linux' .format(resource_group, plan)) self.cmd('functionapp create -g {} -n {} -s {} --plan {} --runtime {}'.format( resource_group, functionapp, storage_account, plan, runtime)) creds = self.cmd('acr credential show -g {} -n {}'.format( resource_group, acr_registry_name)).get_output_in_json() self.cmd('functionapp config container set -g {0} -n {1} --docker-custom-image-name {2}.azurecr.io/image-name:latest --docker-registry-server-url https://{2}.azurecr.io'.format( resource_group, functionapp, acr_registry_name), checks=[ JMESPathCheck( "[?name=='DOCKER_REGISTRY_SERVER_USERNAME']|[0].value", creds['username']) ]) result = self.cmd('functionapp deployment container config -g {} -n {} --enable-cd true'.format(resource_group, functionapp)).get_output_in_json() self.assertTrue(result['CI_CD_URL'].startswith('https://')) self.assertTrue(result['CI_CD_URL'].endswith( '.scm.azurewebsites.net/docker/hook')) # verify that show-cd-url works the same way show_result = self.cmd('functionapp deployment container show-cd-url -g {} -n {}'.format(resource_group, functionapp)).get_output_in_json() self.assertTrue(show_result['CI_CD_URL'].startswith('https://')) self.assertTrue(show_result['CI_CD_URL'].endswith( '.scm.azurewebsites.net/docker/hook')) self.cmd('functionapp delete -g {} -n {}'.format(resource_group, functionapp)) class FunctionAppReservedInstanceTest(ScenarioTest): @ResourceGroupPreparer(location=WINDOWS_ASP_LOCATION_FUNCTIONAPP) @StorageAccountPreparer() def test_functionapp_reserved_instance(self, resource_group, storage_account): functionapp_name = self.create_random_name( 'functionappwithreservedinstance', 40) self.cmd('functionapp create -g {} -n {} -c {} -s {} --os-type Windows' .format(resource_group, functionapp_name, WINDOWS_ASP_LOCATION_FUNCTIONAPP, storage_account)).assert_with_checks([ JMESPathCheck('state', 'Running'), JMESPathCheck('name', functionapp_name), JMESPathCheck('kind', 'functionapp'), JMESPathCheck('hostNames[0]', functionapp_name + '.azurewebsites.net')]) self.cmd('functionapp config set -g {} -n {} --prewarmed-instance-count 4' .format(resource_group, functionapp_name)).assert_with_checks([ JMESPathCheck('preWarmedInstanceCount', 4)]) self.cmd( 'functionapp delete -g {} -n {}'.format(resource_group, functionapp_name)) class WebappGitScenarioTest(ScenarioTest): @ResourceGroupPreparer(location=WINDOWS_ASP_LOCATION_WEBAPP) def test_webapp_git(self, resource_group): plan = self.create_random_name(prefix='webapp-git-plan5', length=24) webapp = self.create_random_name(prefix='web-git-test2', length=24) # You can create and use any repros with the 3 files under "./sample_web" test_git_repo = 'https://github.com/yugangw-msft/azure-site-test' self.cmd( 'appservice plan create -g {} -n {} --sku S1'.format(resource_group, plan)) self.cmd( 'webapp create -g {} -n {} --plan {}'.format(resource_group, webapp, plan)) self.cmd('webapp deployment source config -g {} -n {} --repo-url {} --branch {} --manual-integration'.format(resource_group, webapp, test_git_repo, 'master'), checks=[ JMESPathCheck('repoUrl', test_git_repo), JMESPathCheck('isMercurial', False), JMESPathCheck('branch', 'master') ]) self.cmd('webapp deployment source show -g {} -n {}'.format(resource_group, webapp), checks=[ JMESPathCheck('repoUrl', test_git_repo), JMESPathCheck('isMercurial', False), JMESPathCheck('branch', 'master') ]) self.cmd( 'webapp deployment source delete -g {} -n {}'.format(resource_group, webapp)) self.cmd('webapp deployment source show -g {} -n {}'.format(resource_group, webapp), checks=JMESPathCheck('repoUrl', None)) class WebappSlotScenarioTest(ScenarioTest): @ResourceGroupPreparer(location=WINDOWS_ASP_LOCATION_WEBAPP) def test_webapp_slot(self, resource_group): plan = self.create_random_name(prefix='slot-test-plan', length=24) webapp = self.create_random_name(prefix='slot-test-web', length=24) plan_result = self.cmd( 'appservice plan create -g {} -n {} --sku S1'.format(resource_group, plan)).get_output_in_json() self.cmd('webapp create -g {} -n {} --plan {}'.format(resource_group, webapp, plan_result['name'])) # You can create and use any repros with the 3 files under "./sample_web" and with a 'staging 'branch slot = 'staging' slot2 = 'dev' test_git_repo = 'https://github.com/yugangw-msft/azure-site-test' test_php_version = '5.6' # create a few app-settings to test they can be cloned self.cmd('webapp config appsettings set -g {} -n {} --settings s1=v1 --slot-settings s2=v2'.format(resource_group, webapp)) # create an empty slot self.cmd('webapp deployment slot create -g {} -n {} --slot {}'.format(resource_group, webapp, slot), checks=[ JMESPathCheck('name', slot) ]) self.cmd('webapp deployment source config -g {} -n {} --repo-url {} --branch {} -s {} --manual-integration'.format(resource_group, webapp, test_git_repo, slot, slot), checks=[ JMESPathCheck('repoUrl', test_git_repo), JMESPathCheck('branch', slot) ]) # swap with prod and verify the git branch also switched self.cmd( 'webapp deployment slot swap -g {} -n {} -s {}'.format(resource_group, webapp, slot)) result = self.cmd('webapp config appsettings list -g {} -n {} -s {}'.format( resource_group, webapp, slot)).get_output_in_json() self.assertEqual(set([x['name'] for x in result]), set( ['s1', 'WEBSITE_NODE_DEFAULT_VERSION'])) # create a new slot by cloning from prod slot self.cmd('webapp config set -g {} -n {} --php-version {}'.format( resource_group, webapp, test_php_version)) self.cmd('webapp deployment slot create -g {} -n {} --slot {} --configuration-source {}'.format( resource_group, webapp, slot2, webapp)) self.cmd('webapp config show -g {} -n {} --slot {}'.format(resource_group, webapp, slot2), checks=[ JMESPathCheck("phpVersion", test_php_version), ]) self.cmd('webapp config appsettings set -g {} -n {} --slot {} --settings s3=v3 --slot-settings s4=v4'.format(resource_group, webapp, slot2), checks=[ JMESPathCheck("[?name=='s4']|[0].slotSetting", True), JMESPathCheck("[?name=='s3']|[0].slotSetting", False), ]) self.cmd('webapp config connection-string set -g {} -n {} -t mysql --slot {} --settings c1=connection1 --slot-settings c2=connection2'.format(resource_group, webapp, slot2)) # verify we can swap with non production slot self.cmd('webapp deployment slot swap -g {} -n {} --slot {} --target-slot {}'.format( resource_group, webapp, slot, slot2)) result = self.cmd('webapp config appsettings list -g {} -n {} --slot {}'.format( resource_group, webapp, slot2)).get_output_in_json() self.assertEqual(set([x['name'] for x in result]), set( ['s1', 's4', 'WEBSITE_NODE_DEFAULT_VERSION'])) result = self.cmd('webapp config connection-string list -g {} -n {} --slot {}'.format( resource_group, webapp, slot2)).get_output_in_json() self.assertEqual(set([x['name'] for x in result]), set(['c2'])) result = self.cmd('webapp config appsettings list -g {} -n {} --slot {}'.format( resource_group, webapp, slot)).get_output_in_json() self.assertTrue(set(['s3']).issubset(set([x['name'] for x in result]))) result = self.cmd('webapp config connection-string list -g {} -n {} --slot {}'.format( resource_group, webapp, slot)).get_output_in_json() self.assertEqual(set([x['name'] for x in result]), set(['c1'])) self.cmd('webapp deployment slot list -g {} -n {}'.format(resource_group, webapp), checks=[ JMESPathCheck("length([])", 2), JMESPathCheck("length([?name=='{}'])".format(slot2), 1), JMESPathCheck("length([?name=='{}'])".format(slot), 1), ]) self.cmd( 'webapp deployment slot delete -g {} -n {} --slot {}'.format(resource_group, webapp, slot)) # try another way to delete a slot and exercise all options self.cmd('webapp delete -g {} -n {} --slot {} --keep-dns-registration --keep-empty-plan --keep-metrics'.format(resource_group, webapp, slot2)) class WebappSlotTrafficRouting(ScenarioTest): @ResourceGroupPreparer(location=WINDOWS_ASP_LOCATION_WEBAPP) def test_traffic_routing(self, resource_group): plan = self.create_random_name(prefix='slot-traffic-plan', length=24) webapp = self.create_random_name(prefix='slot-traffic-web', length=24) plan_result = self.cmd( 'appservice plan create -g {} -n {} --sku S1'.format(resource_group, plan)).get_output_in_json() self.cmd('webapp create -g {} -n {} --plan {}'.format(resource_group, webapp, plan_result['name'])) # You can create and use any repros with the 3 files under "./sample_web" and with a 'staging 'branch slot = 'staging' # create an empty slot self.cmd( 'webapp deployment slot create -g {} -n {} --slot {}'.format(resource_group, webapp, slot)) self.cmd('webapp traffic-routing set -g {} -n {} -d {}=15'.format(resource_group, webapp, slot), checks=[ JMESPathCheck("[0].actionHostName", webapp + '-' + slot + '.azurewebsites.net'), JMESPathCheck("[0].reroutePercentage", 15.0) ]) self.cmd('webapp traffic-routing show -g {} -n {}'.format(resource_group, webapp), checks=[ JMESPathCheck("[0].actionHostName", webapp + '-' + slot + '.azurewebsites.net'), JMESPathCheck("[0].reroutePercentage", 15.0) ]) self.cmd( 'webapp traffic-routing clear -g {} -n {}'.format(resource_group, webapp)) class AppServiceCors(ScenarioTest): @ResourceGroupPreparer(location=WINDOWS_ASP_LOCATION_WEBAPP) def test_webapp_cors(self, resource_group): self.kwargs.update({ 'plan': self.create_random_name(prefix='slot-traffic-plan', length=24), 'web': self.create_random_name(prefix='slot-traffic-web', length=24), 'slot': 'slot1' }) self.cmd('appservice plan create -g {rg} -n {plan} --sku S1') self.cmd('webapp create -g {rg} -n {web} --plan {plan}') self.cmd( 'webapp cors add -g {rg} -n {web} --allowed-origins https://msdn.com https://msn.com') self.cmd('webapp cors show -g {rg} -n {web}', checks=self.check('allowedOrigins', ['https://msdn.com', 'https://msn.com'])) self.cmd( 'webapp cors remove -g {rg} -n {web} --allowed-origins https://msn.com') self.cmd('webapp cors show -g {rg} -n {web}', checks=self.check('allowedOrigins', ['https://msdn.com'])) self.cmd( 'webapp deployment slot create -g {rg} -n {web} --slot {slot}') self.cmd( 'webapp cors add -g {rg} -n {web} --slot {slot} --allowed-origins https://foo.com') self.cmd('webapp cors show -g {rg} -n {web} --slot {slot}', checks=self.check('allowedOrigins', ['https://foo.com'])) self.cmd( 'webapp cors remove -g {rg} -n {web} --slot {slot} --allowed-origins https://foo.com') self.cmd('webapp cors show -g {rg} -n {web} --slot {slot}', checks=self.check('allowedOrigins', [])) @ResourceGroupPreparer(location=WINDOWS_ASP_LOCATION_WEBAPP) @StorageAccountPreparer() def test_functionapp_cors(self, resource_group, storage_account): self.kwargs.update({ 'plan': self.create_random_name(prefix='slot-traffic-plan', length=24), 'function': self.create_random_name(prefix='slot-traffic-web', length=24), 'storage': self.create_random_name(prefix='storage', length=24) }) self.cmd('appservice plan create -g {rg} -n {plan} --sku S1') self.cmd( 'storage account create --name {storage} -g {rg} --sku Standard_LRS') self.cmd( 'functionapp create -g {rg} -n {function} --plan {plan} -s {storage}') self.cmd( 'functionapp cors add -g {rg} -n {function} --allowed-origins https://msdn.com https://msn.com') result = self.cmd( 'functionapp cors show -g {rg} -n {function}').get_output_in_json()['allowedOrigins'] # functionapp has pre-defined cors. We verify the ones we added are in the list self.assertTrue( set(['https://msdn.com', 'https://msn.com']).issubset(set(result))) class WebappSlotSwapScenarioTest(ScenarioTest): @ResourceGroupPreparer(location=WINDOWS_ASP_LOCATION_WEBAPP) def test_webapp_slot_swap(self, resource_group): plan = self.create_random_name(prefix='slot-swap-plan', length=24) webapp = self.create_random_name(prefix='slot-swap-web', length=24) plan_result = self.cmd( 'appservice plan create -g {} -n {} --sku S1'.format(resource_group, plan)).get_output_in_json() self.cmd('webapp create -g {} -n {} --plan {}'.format(resource_group, webapp, plan_result['name'])) # You can create and use any repros with the 3 files under "./sample_web" and with a 'staging 'branch slot = 'staging' self.cmd( 'webapp config appsettings set -g {} -n {} --slot-settings s1=prod'.format(resource_group, webapp)) # create an empty slot self.cmd( 'webapp deployment slot create -g {} -n {} --slot {}'.format(resource_group, webapp, slot)) self.cmd('webapp config appsettings set -g {} -n {} --slot-settings s1=slot --slot {}'.format( resource_group, webapp, slot)) # swap with preview self.cmd('webapp deployment slot swap -g {} -n {} -s {} --action preview'.format( resource_group, webapp, slot)) self.cmd('webapp config appsettings list -g {} -n {} --slot {}'.format(resource_group, webapp, slot), checks=[ JMESPathCheck("[?name=='s1']|[0].value", 'prod') ]) # complete the swap self.cmd( 'webapp deployment slot swap -g {} -n {} -s {}'.format(resource_group, webapp, slot)) self.cmd('webapp config appsettings list -g {} -n {} --slot {}'.format(resource_group, webapp, slot), checks=[ JMESPathCheck("[?name=='s1']|[0].value", 'slot') ]) # reset self.cmd('webapp deployment slot swap -g {} -n {} -s {} --action reset'.format( resource_group, webapp, slot)) self.cmd('webapp config appsettings list -g {} -n {} --slot {}'.format(resource_group, webapp, slot), checks=[ JMESPathCheck("[?name=='s1']|[0].value", 'slot') ]) class WebappSSLCertTest(ScenarioTest): @ResourceGroupPreparer(location=WINDOWS_ASP_LOCATION_WEBAPP) def test_webapp_ssl(self, resource_group, resource_group_location): plan = self.create_random_name(prefix='ssl-test-plan', length=24) webapp_name = self.create_random_name(prefix='web-ssl-test', length=20) slot_name = self.create_random_name(prefix='slot-ssl-test', length=20) # Cert Generated using # https://docs.microsoft.com/azure/app-service-web/web-sites-configure-ssl-certificate#bkmk_ssopenssl pfx_file = os.path.join(TEST_DIR, 'server.pfx') cert_password = 'test' cert_thumbprint = '9E9735C45C792B03B3FFCCA614852B32EE71AD6B' # we configure tags here in a hope to capture a repro for https://github.com/Azure/azure-cli/issues/6929 self.cmd( 'appservice plan create -g {} -n {} --sku S1 --tags plan=plan1'.format(resource_group, plan)) self.cmd('appservice plan show -g {} -n {}'.format(resource_group, plan), self.check('tags.plan', 'plan1')) self.cmd('webapp create -g {} -n {} --plan {} --tags web=web1'.format( resource_group, webapp_name, plan)) self.cmd('webapp config ssl upload -g {} -n {} --certificate-file "{}" --certificate-password {}'.format(resource_group, webapp_name, pfx_file, cert_password), checks=[ JMESPathCheck('thumbprint', cert_thumbprint) ]) self.cmd('webapp show -g {} -n {}'.format(resource_group, webapp_name), self.check('tags.web', 'web1')) self.cmd('webapp config ssl bind -g {} -n {} --certificate-thumbprint {} --ssl-type {}'.format(resource_group, webapp_name, cert_thumbprint, 'SNI'), checks=[ JMESPathCheck("hostNameSslStates|[?name=='{}.azurewebsites.net']|[0].sslState".format( webapp_name), 'SniEnabled'), JMESPathCheck("hostNameSslStates|[?name=='{}.azurewebsites.net']|[0].thumbprint".format( webapp_name), cert_thumbprint) ]) self.cmd('webapp show -g {} -n {}'.format(resource_group, webapp_name), self.check('tags.web', 'web1')) self.cmd('webapp config ssl unbind -g {} -n {} --certificate-thumbprint {}'.format(resource_group, webapp_name, cert_thumbprint), checks=[ JMESPathCheck("hostNameSslStates|[?name=='{}.azurewebsites.net']|[0].sslState".format( webapp_name), 'Disabled'), ]) self.cmd('webapp show -g {} -n {}'.format(resource_group, webapp_name), self.check('tags.web', 'web1')) self.cmd('webapp config ssl delete -g {} --certificate-thumbprint {}'.format( resource_group, cert_thumbprint)) self.cmd('webapp show -g {} -n {}'.format(resource_group, webapp_name), self.check('tags.web', 'web1')) # test with slot self.cmd('webapp deployment slot create -g {} -n {} --slot {}'.format( resource_group, webapp_name, slot_name)) self.cmd('webapp config ssl upload -g {} -n {} --certificate-file "{}" --certificate-password {} -s {}'.format(resource_group, webapp_name, pfx_file, cert_password, slot_name), checks=[ JMESPathCheck('thumbprint', cert_thumbprint) ]) self.cmd( 'webapp show -g {} -n {} -s {}'.format(resource_group, webapp_name, slot_name)) self.cmd('webapp config ssl bind -g {} -n {} --certificate-thumbprint {} --ssl-type {} -s {}'.format(resource_group, webapp_name, cert_thumbprint, 'SNI', slot_name), checks=[ JMESPathCheck("hostNameSslStates|[?name=='{}-{}.azurewebsites.net']|[0].sslState".format( webapp_name, slot_name), 'SniEnabled'), JMESPathCheck("hostNameSslStates|[?name=='{}-{}.azurewebsites.net']|[0].thumbprint".format( webapp_name, slot_name), cert_thumbprint) ]) self.cmd( 'webapp show -g {} -n {} -s {}'.format(resource_group, webapp_name, slot_name)) self.cmd('webapp config ssl unbind -g {} -n {} --certificate-thumbprint {} -s {}'.format(resource_group, webapp_name, cert_thumbprint, slot_name), checks=[ JMESPathCheck("hostNameSslStates|[?name=='{}-{}.azurewebsites.net']|[0].sslState".format( webapp_name, slot_name), 'Disabled'), ]) self.cmd( 'webapp show -g {} -n {} -s {}'.format(resource_group, webapp_name, slot_name)) self.cmd('webapp config ssl delete -g {} --certificate-thumbprint {}'.format( resource_group, cert_thumbprint)) self.cmd( 'webapp show -g {} -n {} -s {}'.format(resource_group, webapp_name, slot_name)) self.cmd('webapp delete -g {} -n {}'.format(resource_group, webapp_name)) class WebappSSLImportCertTest(ScenarioTest): @ResourceGroupPreparer(location=WINDOWS_ASP_LOCATION_WEBAPP) def test_webapp_ssl_import(self, resource_group): plan_name = self.create_random_name(prefix='ssl-test-plan', length=24) webapp_name = self.create_random_name(prefix='web-ssl-test', length=20) kv_name = self.create_random_name(prefix='kv-ssl-test', length=20) # Cert Generated using # https://docs.microsoft.com/azure/app-service-web/web-sites-configure-ssl-certificate#bkmk_ssopenssl pfx_file = os.path.join(TEST_DIR, 'server.pfx') cert_password = 'test' cert_thumbprint = '9E9735C45C792B03B3FFCCA614852B32EE71AD6B' cert_name = 'test-cert' # we configure tags here in a hope to capture a repro for https://github.com/Azure/azure-cli/issues/6929 self.cmd( 'appservice plan create -g {} -n {} --sku B1'.format(resource_group, plan_name)) self.cmd( 'webapp create -g {} -n {} --plan {}'.format(resource_group, webapp_name, plan_name)) self.cmd('keyvault create -g {} -n {}'.format(resource_group, kv_name)) self.cmd('keyvault set-policy -g {} --name {} --spn {} --secret-permissions get'.format( resource_group, kv_name, 'Microsoft.Azure.WebSites')) self.cmd('keyvault certificate import --name {} --vault-name {} --file "{}" --password {}'.format( cert_name, kv_name, pfx_file, cert_password)) self.cmd('webapp config ssl import --resource-group {} --name {} --key-vault {} --key-vault-certificate-name {}'.format(resource_group, webapp_name, kv_name, cert_name), checks=[ JMESPathCheck('keyVaultSecretStatus', 'Initialized'), JMESPathCheck('thumbprint', cert_thumbprint) ]) self.cmd('webapp config ssl bind -g {} -n {} --certificate-thumbprint {} --ssl-type {}'.format(resource_group, webapp_name, cert_thumbprint, 'SNI'), checks=[ JMESPathCheck("hostNameSslStates|[?name=='{}.azurewebsites.net']|[0].sslState".format( webapp_name), 'SniEnabled'), JMESPathCheck("hostNameSslStates|[?name=='{}.azurewebsites.net']|[0].thumbprint".format( webapp_name), cert_thumbprint) ]) @ResourceGroupPreparer(parameter_name='kv_resource_group', location=WINDOWS_ASP_LOCATION_WEBAPP) @ResourceGroupPreparer(location=WINDOWS_ASP_LOCATION_WEBAPP) def test_webapp_ssl_import_crossrg(self, resource_group, kv_resource_group): plan_name = self.create_random_name(prefix='ssl-test-plan', length=24) webapp_name = self.create_random_name(prefix='web-ssl-test', length=20) kv_name = self.create_random_name(prefix='kv-ssl-test', length=20) # Cert Generated using # https://docs.microsoft.com/azure/app-service-web/web-sites-configure-ssl-certificate#bkmk_ssopenssl pfx_file = os.path.join(TEST_DIR, 'server.pfx') cert_password = 'test' cert_thumbprint = '9E9735C45C792B03B3FFCCA614852B32EE71AD6B' cert_name = 'test-cert' # we configure tags here in a hope to capture a repro for https://github.com/Azure/azure-cli/issues/6929 self.cmd( 'appservice plan create -g {} -n {} --sku B1'.format(resource_group, plan_name)) self.cmd( 'webapp create -g {} -n {} --plan {}'.format(resource_group, webapp_name, plan_name)) kv_id = self.cmd('keyvault create -g {} -n {}'.format(kv_resource_group, kv_name)).get_output_in_json()['id'] self.cmd('keyvault set-policy -g {} --name {} --spn {} --secret-permissions get'.format( kv_resource_group, kv_name, 'Microsoft.Azure.WebSites')) self.cmd('keyvault certificate import --name {} --vault-name {} --file "{}" --password {}'.format( cert_name, kv_name, pfx_file, cert_password)) self.cmd('webapp config ssl import --resource-group {} --name {} --key-vault {} --key-vault-certificate-name {}'.format(resource_group, webapp_name, kv_id, cert_name), checks=[ JMESPathCheck('keyVaultSecretStatus', 'Initialized'), JMESPathCheck('thumbprint', cert_thumbprint) ]) self.cmd('webapp config ssl bind -g {} -n {} --certificate-thumbprint {} --ssl-type {}'.format(resource_group, webapp_name, cert_thumbprint, 'SNI'), checks=[ JMESPathCheck("hostNameSslStates|[?name=='{}.azurewebsites.net']|[0].sslState".format( webapp_name), 'SniEnabled'), JMESPathCheck("hostNameSslStates|[?name=='{}.azurewebsites.net']|[0].thumbprint".format( webapp_name), cert_thumbprint) ]) class WebappUndeleteTest(ScenarioTest): @AllowLargeResponse(8192) @ResourceGroupPreparer(location=WINDOWS_ASP_LOCATION_WEBAPP) def test_webapp_deleted_list(self, resource_group): plan = self.create_random_name(prefix='delete-me-plan', length=24) webapp_name = self.create_random_name( prefix='delete-me-web', length=24) self.cmd( 'appservice plan create -g {} -n {} --sku B1 --tags plan=plan1'.format(resource_group, plan)) self.cmd( 'webapp create -g {} -n {} --plan {}'.format(resource_group, webapp_name, plan)) self.cmd('webapp delete -g {} -n {}'.format(resource_group, webapp_name)) self.cmd('webapp deleted list -g {}'.format(resource_group), checks=[ JMESPathCheck('[0].deletedSiteName', webapp_name) ]) class FunctionAppWithPlanE2ETest(ScenarioTest): @ResourceGroupPreparer(location=WINDOWS_ASP_LOCATION_FUNCTIONAPP) @ResourceGroupPreparer(parameter_name='resource_group2', location=WINDOWS_ASP_LOCATION_FUNCTIONAPP) def test_functionapp_e2e(self, resource_group, resource_group2): functionapp_name, functionapp_name2 = self.create_random_name( 'func-e2e', 24), self.create_random_name('func-e2e', 24) plan = self.create_random_name('func-e2e-plan', 24) storage, storage2 = 'functionappplanstorage', 'functionappplanstorage2' plan_id = self.cmd('appservice plan create -g {} -n {}'.format( resource_group, plan)).get_output_in_json()['id'] self.cmd('appservice plan list -g {}'.format(resource_group)) self.cmd( 'storage account create --name {} -g {} -l {} --sku Standard_LRS'.format(storage, resource_group, WINDOWS_ASP_LOCATION_FUNCTIONAPP)) storage_account_id2 = self.cmd('storage account create --name {} -g {} -l {} --sku Standard_LRS'.format( storage2, resource_group2, WINDOWS_ASP_LOCATION_FUNCTIONAPP)).get_output_in_json()['id'] self.cmd('functionapp create -g {} -n {} -p {} -s {}'.format(resource_group, functionapp_name, plan, storage), checks=[ JMESPathCheck('state', 'Running'), JMESPathCheck('name', functionapp_name), JMESPathCheck('hostNames[0]', functionapp_name + '.azurewebsites.net') ]) self.cmd('functionapp create -g {} -n {} -p {} -s {}'.format(resource_group2, functionapp_name2, plan_id, storage_account_id2)) self.cmd( 'functionapp delete -g {} -n {}'.format(resource_group, functionapp_name)) @ResourceGroupPreparer(location=LINUX_ASP_LOCATION_FUNCTIONAPP) @StorageAccountPreparer() def test_functionapp_on_linux_app_service_java(self, resource_group, storage_account): plan = self.create_random_name(prefix='funcapplinplan', length=24) functionapp = self.create_random_name( prefix='functionapp-linux', length=24) self.cmd('functionapp plan create -g {} -n {} --sku S1 --is-linux'.format(resource_group, plan), checks=[ # this weird field means it is a linux JMESPathCheck('reserved', True), JMESPathCheck('sku.name', 'S1'), ]) self.cmd('functionapp create -g {} -n {} --plan {} -s {} --runtime java --functions-version 3' .format(resource_group, functionapp, plan, storage_account), checks=[ JMESPathCheck('name', functionapp) ]) result = self.cmd('functionapp list -g {}'.format(resource_group), checks=[ JMESPathCheck('length([])', 1), JMESPathCheck('[0].name', functionapp) ]).get_output_in_json() self.assertTrue('functionapp,linux' in result[0]['kind']) self.cmd('functionapp config show -g {} -n {}'.format(resource_group, functionapp), checks=[ JMESPathCheck('linuxFxVersion', 'Java|8')]) @ResourceGroupPreparer(location=LINUX_ASP_LOCATION_FUNCTIONAPP) @StorageAccountPreparer() def test_functionapp_on_linux_app_service_java_with_runtime_version(self, resource_group, storage_account): plan = self.create_random_name(prefix='funcapplinplan', length=24) functionapp = self.create_random_name( prefix='functionapp-linux', length=24) self.cmd('functionapp plan create -g {} -n {} --sku S1 --is-linux'.format(resource_group, plan), checks=[ # this weird field means it is a linux JMESPathCheck('reserved', True), JMESPathCheck('sku.name', 'S1'), ]) self.cmd('functionapp create -g {} -n {} --plan {} -s {} --runtime java --runtime-version 11 --functions-version 3' .format(resource_group, functionapp, plan, storage_account), checks=[ JMESPathCheck('name', functionapp) ]) result = self.cmd('functionapp list -g {}'.format(resource_group), checks=[ JMESPathCheck('length([])', 1), JMESPathCheck('[0].name', functionapp) ]).get_output_in_json() self.assertTrue('functionapp,linux' in result[0]['kind']) self.cmd('functionapp config show -g {} -n {}'.format(resource_group, functionapp), checks=[ JMESPathCheck('linuxFxVersion', 'Java|11')]) class FunctionUpdatePlan(ScenarioTest): @ResourceGroupPreparer(location=WINDOWS_ASP_LOCATION_FUNCTIONAPP) @StorageAccountPreparer() def test_move_plan_to_elastic(self, resource_group, storage_account): functionapp_name = self.create_random_name('functionappelastic', 40) ep_plan_name = self.create_random_name('somerandomplan', 40) second_plan_name = self.create_random_name('secondplan', 40) s1_plan_name = self.create_random_name('ab1planname', 40) plan_result = self.cmd('functionapp plan create -g {} -n {} --sku EP1'.format(resource_group, ep_plan_name), checks=[ JMESPathCheck('sku.name', 'EP1') ]).get_output_in_json() self.cmd('functionapp plan create -g {} -n {} --sku EP1'.format(resource_group, second_plan_name), checks=[ JMESPathCheck('sku.name', 'EP1') ]).get_output_in_json() self.cmd('functionapp plan create -g {} -n {} --sku S1'.format(resource_group, s1_plan_name), checks=[ JMESPathCheck('sku.name', 'S1') ]) self.cmd('functionapp create -g {} -n {} --plan {} -s {}' .format(resource_group, functionapp_name, second_plan_name, storage_account)).assert_with_checks([ JMESPathCheck('state', 'Running'), JMESPathCheck('name', functionapp_name), JMESPathCheck('hostNames[0]', functionapp_name + '.azurewebsites.net')]) self.cmd('functionapp update -g {} -n {} --plan {}' .format(resource_group, functionapp_name, ep_plan_name)).assert_with_checks([ JMESPathCheck('state', 'Running'), JMESPathCheck('name', functionapp_name), JMESPathCheck('serverFarmId', plan_result['id']), JMESPathCheck('hostNames[0]', functionapp_name + '.azurewebsites.net')]) # Moving to and from an App Service plan (not Elastic Premium) is not allowed right now self.cmd('functionapp update -g {} -n {} --plan {}' .format(resource_group, functionapp_name, s1_plan_name), expect_failure=True) class FunctionAppWithConsumptionPlanE2ETest(ScenarioTest): @ResourceGroupPreparer(name_prefix='azurecli-functionapp-c-e2e', location=WINDOWS_ASP_LOCATION_FUNCTIONAPP) @StorageAccountPreparer() def test_functionapp_consumption_e2e(self, resource_group, storage_account): functionapp_name = self.create_random_name( 'functionappconsumption', 40) self.cmd('functionapp create -g {} -n {} -c {} -s {}' .format(resource_group, functionapp_name, WINDOWS_ASP_LOCATION_FUNCTIONAPP, storage_account)).assert_with_checks([ JMESPathCheck('state', 'Running'), JMESPathCheck('name', functionapp_name), JMESPathCheck('hostNames[0]', functionapp_name + '.azurewebsites.net')]) self.cmd('functionapp list -g {}'.format(resource_group), checks=[ JMESPathCheck('[0].kind', 'functionapp'), JMESPathCheck('[0].name', functionapp_name) ]) self.cmd('functionapp show -g {} -n {}'.format(resource_group, functionapp_name), checks=[ JMESPathCheck('kind', 'functionapp'), JMESPathCheck('name', functionapp_name) ]) self.cmd('functionapp update -g {} -n {} --set clientAffinityEnabled=true'.format(resource_group, functionapp_name), checks=[self.check('clientAffinityEnabled', True)] ) self.cmd( 'functionapp delete -g {} -n {}'.format(resource_group, functionapp_name)) @ResourceGroupPreparer(name_prefix='azurecli-functionapp-c-e2e-ragrs', location=WINDOWS_ASP_LOCATION_FUNCTIONAPP) @StorageAccountPreparer(sku='Standard_RAGRS') def test_functionapp_consumption_ragrs_storage_e2e(self, resource_group, storage_account): functionapp_name = self.create_random_name( 'functionappconsumption', 40) self.cmd('functionapp create -g {} -n {} -c {} -s {}' .format(resource_group, functionapp_name, WINDOWS_ASP_LOCATION_FUNCTIONAPP, storage_account)).assert_with_checks([ JMESPathCheck('state', 'Running'), JMESPathCheck('name', functionapp_name), JMESPathCheck('hostNames[0]', functionapp_name + '.azurewebsites.net')]) self.cmd('functionapp show -g {} -n {}'.format(resource_group, functionapp_name), checks=[ JMESPathCheck('kind', 'functionapp'), JMESPathCheck('name', functionapp_name) ]) class FunctionAppWithLinuxConsumptionPlanTest(ScenarioTest): @ResourceGroupPreparer(name_prefix='azurecli-functionapp-linux', location=LINUX_ASP_LOCATION_FUNCTIONAPP) @StorageAccountPreparer() def test_functionapp_consumption_linux(self, resource_group, storage_account): functionapp_name = self.create_random_name( 'functionapplinuxconsumption', 40) self.cmd('functionapp create -g {} -n {} -c {} -s {} --os-type Linux --runtime node' .format(resource_group, functionapp_name, LINUX_ASP_LOCATION_FUNCTIONAPP, storage_account)).assert_with_checks([ JMESPathCheck('state', 'Running'), JMESPathCheck('name', functionapp_name), JMESPathCheck('reserved', True), JMESPathCheck('kind', 'functionapp,linux'), JMESPathCheck('hostNames[0]', functionapp_name + '.azurewebsites.net')]) self.cmd('functionapp config appsettings list -g {} -n {}'.format(resource_group, functionapp_name), checks=[ JMESPathCheck("[?name=='FUNCTIONS_WORKER_RUNTIME'].value|[0]", 'node')]) @ResourceGroupPreparer(name_prefix='azurecli-functionapp-linux', location=LINUX_ASP_LOCATION_FUNCTIONAPP) @StorageAccountPreparer() def test_functionapp_consumption_linux_java(self, resource_group, storage_account): functionapp_name = self.create_random_name( 'functionapplinuxconsumption', 40) self.cmd('functionapp create -g {} -n {} -c {} -s {} --os-type Linux --runtime java --functions-version 3' .format(resource_group, functionapp_name, LINUX_ASP_LOCATION_FUNCTIONAPP, storage_account)).assert_with_checks([ JMESPathCheck('state', 'Running'), JMESPathCheck('name', functionapp_name), JMESPathCheck('reserved', True), JMESPathCheck('kind', 'functionapp,linux'), JMESPathCheck('hostNames[0]', functionapp_name + '.azurewebsites.net')]) self.cmd('functionapp config appsettings list -g {} -n {}'.format(resource_group, functionapp_name), checks=[ JMESPathCheck("[?name=='FUNCTIONS_WORKER_RUNTIME'].value|[0]", 'java')]) class FunctionAppOnWindowsWithRuntime(ScenarioTest): @ResourceGroupPreparer(location=WINDOWS_ASP_LOCATION_FUNCTIONAPP) @StorageAccountPreparer() def test_functionapp_windows_runtime(self, resource_group, storage_account): functionapp_name = self.create_random_name( 'functionappwindowsruntime', 40) self.cmd('functionapp create -g {} -n {} -c {} -s {} --os-type Windows --runtime node' .format(resource_group, functionapp_name, WINDOWS_ASP_LOCATION_FUNCTIONAPP, storage_account)).assert_with_checks([ JMESPathCheck('state', 'Running'), JMESPathCheck('name', functionapp_name), JMESPathCheck('kind', 'functionapp'), JMESPathCheck('hostNames[0]', functionapp_name + '.azurewebsites.net')]) self.cmd('functionapp config appsettings list -g {} -n {}'.format(resource_group, functionapp_name), checks=[ JMESPathCheck("[?name=='FUNCTIONS_WORKER_RUNTIME'].value|[0]", 'node')]) @ResourceGroupPreparer(location=WINDOWS_ASP_LOCATION_FUNCTIONAPP) @StorageAccountPreparer() def test_functionapp_windows_runtime_java(self, resource_group, storage_account): functionapp_name = self.create_random_name( 'functionappwindowsruntime', 40) self.cmd('functionapp create -g {} -n {} -c {} -s {} --os-type Windows --runtime java' .format(resource_group, functionapp_name, WINDOWS_ASP_LOCATION_FUNCTIONAPP, storage_account)).assert_with_checks([ JMESPathCheck('state', 'Running'), JMESPathCheck('name', functionapp_name), JMESPathCheck('kind', 'functionapp'), JMESPathCheck('hostNames[0]', functionapp_name + '.azurewebsites.net')]) self.cmd('functionapp config appsettings list -g {} -n {}'.format(resource_group, functionapp_name), checks=[ JMESPathCheck("[?name=='FUNCTIONS_WORKER_RUNTIME'].value|[0]", 'java')]) self.cmd('functionapp config show -g {} -n {}'.format(resource_group, functionapp_name), checks=[ JMESPathCheck('javaVersion', '1.8')]) @ResourceGroupPreparer(location=WINDOWS_ASP_LOCATION_FUNCTIONAPP) @StorageAccountPreparer() def test_functionapp_windows_runtime_powershell(self, resource_group, storage_account): functionapp_name = self.create_random_name( 'functionappwindowsruntime', 40) self.cmd('functionapp create -g {} -n {} -c {} -s {} --os-type Windows --runtime powershell' .format(resource_group, functionapp_name, WINDOWS_ASP_LOCATION_FUNCTIONAPP, storage_account)).assert_with_checks([ JMESPathCheck('state', 'Running'), JMESPathCheck('name', functionapp_name), JMESPathCheck('kind', 'functionapp'), JMESPathCheck('hostNames[0]', functionapp_name + '.azurewebsites.net')]) self.cmd('functionapp config appsettings list -g {} -n {}'.format(resource_group, functionapp_name), checks=[ JMESPathCheck("[?name=='FUNCTIONS_WORKER_RUNTIME'].value|[0]", 'powershell')]) self.cmd('functionapp config show -g {} -n {}'.format(resource_group, functionapp_name), checks=[ JMESPathCheck('powerShellVersion', '~6')]) @ResourceGroupPreparer(location=WINDOWS_ASP_LOCATION_FUNCTIONAPP) @StorageAccountPreparer() def test_functionapp_windows_runtime_version(self, resource_group, storage_account): functionapp_name = self.create_random_name( 'functionappwindowsruntime', 40) self.cmd('functionapp create -g {} -n {} -c {} -s {} --os-type Windows --runtime node --runtime-version 8' .format(resource_group, functionapp_name, WINDOWS_ASP_LOCATION_FUNCTIONAPP, storage_account)).assert_with_checks([ JMESPathCheck('state', 'Running'), JMESPathCheck('name', functionapp_name), JMESPathCheck('kind', 'functionapp'), JMESPathCheck('hostNames[0]', functionapp_name + '.azurewebsites.net')]) self.cmd('functionapp config appsettings list -g {} -n {}'.format(resource_group, functionapp_name), checks=[ JMESPathCheck( "[?name=='FUNCTIONS_WORKER_RUNTIME'].value|[0]", 'node'), JMESPathCheck("[?name=='WEBSITE_NODE_DEFAULT_VERSION'].value|[0]", '~8')]) self.cmd( 'functionapp delete -g {} -n {}'.format(resource_group, functionapp_name)) @ResourceGroupPreparer(location=WINDOWS_ASP_LOCATION_FUNCTIONAPP) @StorageAccountPreparer() def test_functionapp_windows_runtime_version_invalid(self, resource_group, storage_account): functionapp_name = self.create_random_name( 'functionappwindowsruntime', 40) self.cmd('functionapp create -g {} -n {} -c {} -s {} ' '--os-type Windows --runtime node --runtime-version 8.2' .format(resource_group, functionapp_name, WINDOWS_ASP_LOCATION_FUNCTIONAPP, storage_account), expect_failure=True) @ResourceGroupPreparer(location=WINDOWS_ASP_LOCATION_FUNCTIONAPP) @StorageAccountPreparer() def test_functionapp_windows_runtime_functions_version(self, resource_group, storage_account): functionapp_name = self.create_random_name( 'functionappwindowsruntime', 40) self.cmd('functionapp create -g {} -n {} -c {} -s {} --functions-version 3 --os-type Windows --runtime node' .format(resource_group, functionapp_name, WINDOWS_ASP_LOCATION_FUNCTIONAPP, storage_account)).assert_with_checks([ JMESPathCheck('state', 'Running'), JMESPathCheck('name', functionapp_name), JMESPathCheck('kind', 'functionapp'), JMESPathCheck('hostNames[0]', functionapp_name + '.azurewebsites.net')]) self.cmd('functionapp config appsettings list -g {} -n {}'.format(resource_group, functionapp_name), checks=[ JMESPathCheck( "[?name=='FUNCTIONS_EXTENSION_VERSION'].value|[0]", '~3'), JMESPathCheck("[?name=='WEBSITE_NODE_DEFAULT_VERSION'].value|[0]", '~12')]) class FunctionAppOnWindowsWithoutRuntime(ScenarioTest): @ResourceGroupPreparer(location=WINDOWS_ASP_LOCATION_FUNCTIONAPP) @StorageAccountPreparer() def test_functionapp_windows_without_runtime(self, resource_group, storage_account): functionapp_name = self.create_random_name( 'functionappwindowswithoutruntime', 40) self.cmd('functionapp create -g {} -n {} -c {} -s {} --os-type Windows' .format(resource_group, functionapp_name, WINDOWS_ASP_LOCATION_FUNCTIONAPP, storage_account)).assert_with_checks([ JMESPathCheck('state', 'Running'), JMESPathCheck('name', functionapp_name), JMESPathCheck('kind', 'functionapp'), JMESPathCheck('hostNames[0]', functionapp_name + '.azurewebsites.net')]) self.cmd( 'functionapp delete -g {} -n {}'.format(resource_group, functionapp_name)) class FunctionAppWithAppInsightsKey(ScenarioTest): @ResourceGroupPreparer(location=WINDOWS_ASP_LOCATION_FUNCTIONAPP) @StorageAccountPreparer() def test_functionapp_with_app_insights_key(self, resource_group, storage_account): functionapp_name = self.create_random_name( 'functionappwithappinsights', 40) app_insights_key = '00000000-0000-0000-0000-123456789123' self.cmd('functionapp create -g {} -n {} -c {} -s {} --os-type Windows' ' --app-insights-key {}' .format(resource_group, functionapp_name, WINDOWS_ASP_LOCATION_FUNCTIONAPP, storage_account, app_insights_key)).assert_with_checks([ JMESPathCheck('state', 'Running'), JMESPathCheck('name', functionapp_name), JMESPathCheck('kind', 'functionapp'), JMESPathCheck('hostNames[0]', functionapp_name + '.azurewebsites.net')]) self.cmd('functionapp config appsettings list -g {} -n {}'.format(resource_group, functionapp_name)).assert_with_checks([ JMESPathCheck( "[?name=='APPINSIGHTS_INSTRUMENTATIONKEY'].value|[0]", app_insights_key) ]) self.cmd( 'functionapp delete -g {} -n {}'.format(resource_group, functionapp_name)) class FunctionAppWithAppInsightsDefault(ScenarioTest): @ResourceGroupPreparer(location=WINDOWS_ASP_LOCATION_FUNCTIONAPP) @StorageAccountPreparer() def test_functionapp_with_default_app_insights(self, resource_group, storage_account): functionapp_name = self.create_random_name( 'functionappwithappinsights', 40) self.cmd('functionapp create -g {} -n {} -c {} -s {} --os-type Windows' .format(resource_group, functionapp_name, WINDOWS_ASP_LOCATION_FUNCTIONAPP, storage_account)).assert_with_checks([ JMESPathCheck('state', 'Running'), JMESPathCheck('name', functionapp_name), JMESPathCheck('kind', 'functionapp'), JMESPathCheck('hostNames[0]', functionapp_name + '.azurewebsites.net')]) app_set = self.cmd('functionapp config appsettings list -g {} -n {}'.format(resource_group, functionapp_name)).get_output_in_json() self.assertTrue('APPINSIGHTS_INSTRUMENTATIONKEY' in [ kp['name'] for kp in app_set]) self.assertTrue('AzureWebJobsDashboard' not in [ kp['name'] for kp in app_set]) @ResourceGroupPreparer(location=WINDOWS_ASP_LOCATION_FUNCTIONAPP) @StorageAccountPreparer() def test_functionapp_with_no_default_app_insights(self, resource_group, storage_account): functionapp_name = self.create_random_name( 'functionappwithappinsights', 40) self.cmd('functionapp create -g {} -n {} -c {} -s {} --os-type Windows --disable-app-insights' .format(resource_group, functionapp_name, WINDOWS_ASP_LOCATION_FUNCTIONAPP, storage_account)).assert_with_checks([ JMESPathCheck('state', 'Running'), JMESPathCheck('name', functionapp_name), JMESPathCheck('kind', 'functionapp'), JMESPathCheck('hostNames[0]', functionapp_name + '.azurewebsites.net')]) app_set = self.cmd('functionapp config appsettings list -g {} -n {}'.format(resource_group, functionapp_name)).get_output_in_json() self.assertTrue('APPINSIGHTS_INSTRUMENTATIONKEY' not in [ kp['name'] for kp in app_set]) self.assertTrue('AzureWebJobsDashboard' in [ kp['name'] for kp in app_set]) class FunctionAppOnLinux(ScenarioTest): @ResourceGroupPreparer(location=LINUX_ASP_LOCATION_FUNCTIONAPP) @StorageAccountPreparer() def test_functionapp_on_linux(self, resource_group, storage_account): plan = self.create_random_name(prefix='funcapplinplan', length=24) functionapp = self.create_random_name( prefix='functionapp-linux', length=24) self.cmd('appservice plan create -g {} -n {} --sku S1 --is-linux' .format(resource_group, plan), checks=[ # this weird field means it is a linux JMESPathCheck('reserved', True), JMESPathCheck('sku.name', 'S1'), ]) self.cmd('functionapp create -g {} -n {} --plan {} -s {} --runtime node'.format(resource_group, functionapp, plan, storage_account), checks=[ JMESPathCheck('name', functionapp) ]) result = self.cmd('functionapp list -g {}'.format(resource_group), checks=[ JMESPathCheck('length([])', 1), JMESPathCheck('[0].name', functionapp) ]).get_output_in_json() self.assertTrue('functionapp,linux' in result[0]['kind']) self.cmd('functionapp config show -g {} -n {}'.format(resource_group, functionapp), checks=[ JMESPathCheck('linuxFxVersion', 'Node|10')]) self.cmd('functionapp delete -g {} -n {}'.format(resource_group, functionapp)) @ResourceGroupPreparer(location=LINUX_ASP_LOCATION_FUNCTIONAPP) @StorageAccountPreparer() def test_functionapp_on_linux_version(self, resource_group, storage_account): plan = self.create_random_name(prefix='funcapplinplan', length=24) functionapp = self.create_random_name( prefix='functionapp-linux', length=24) self.cmd('functionapp plan create -g {} -n {} --sku S1 --is-linux'.format(resource_group, plan), checks=[ # this weird field means it is a linux JMESPathCheck('reserved', True), JMESPathCheck('sku.name', 'S1'), ]) self.cmd('functionapp create -g {} -n {} --plan {} -s {} --runtime node --runtime-version 10' .format(resource_group, functionapp, plan, storage_account), checks=[ JMESPathCheck('name', functionapp) ]) result = self.cmd('functionapp list -g {}'.format(resource_group), checks=[ JMESPathCheck('length([])', 1), JMESPathCheck('[0].name', functionapp) ]).get_output_in_json() self.assertTrue('functionapp,linux' in result[0]['kind']) self.cmd('functionapp config show -g {} -n {}'.format(resource_group, functionapp), checks=[ JMESPathCheck('linuxFxVersion', 'Node|10')]) @ResourceGroupPreparer(location=LINUX_ASP_LOCATION_FUNCTIONAPP) @StorageAccountPreparer() def test_functionapp_on_linux_version_consumption(self, resource_group, storage_account): functionapp = self.create_random_name( prefix='functionapp-linux', length=24) self.cmd('functionapp create -g {} -n {} -c {} -s {} --os-type linux --runtime python --runtime-version 3.7' .format(resource_group, functionapp, LINUX_ASP_LOCATION_FUNCTIONAPP, storage_account), checks=[ JMESPathCheck('name', functionapp) ]) self.cmd('functionapp config show -g {} -n {}'.format(resource_group, functionapp), checks=[ JMESPathCheck('linuxFxVersion', 'Python|3.7')]) @ResourceGroupPreparer(location=LINUX_ASP_LOCATION_FUNCTIONAPP) @StorageAccountPreparer() def test_functionapp_on_linux_version_error(self, resource_group, storage_account): plan = self.create_random_name(prefix='funcapplinplan', length=24) functionapp = self.create_random_name( prefix='functionapp-linux', length=24) self.cmd('functionapp plan create -g {} -n {} --sku S1 --is-linux'.format(resource_group, plan), checks=[ JMESPathCheck('reserved', True), JMESPathCheck('sku.name', 'S1'), ]) self.cmd('functionapp create -g {} -n {} --plan {} -s {} --runtime python --runtime-version 3.8' .format(resource_group, functionapp, plan, storage_account), expect_failure=True) @ResourceGroupPreparer(location=LINUX_ASP_LOCATION_FUNCTIONAPP) @StorageAccountPreparer() def test_functionapp_on_linux_functions_version(self, resource_group, storage_account): plan = self.create_random_name(prefix='funcapplinplan', length=24) functionapp = self.create_random_name( prefix='functionapp-linux', length=24) self.cmd('appservice plan create -g {} -n {} --sku S1 --is-linux' .format(resource_group, plan), checks=[ # this weird field means it is a linux JMESPathCheck('reserved', True), JMESPathCheck('sku.name', 'S1') ]) self.cmd('functionapp create -g {} -n {} --plan {} -s {} --functions-version 3 --runtime node' .format(resource_group, functionapp, plan, storage_account), checks=[ JMESPathCheck('name', functionapp) ]) self.cmd('functionapp config show -g {} -n {}'.format(resource_group, functionapp), checks=[ JMESPathCheck('linuxFxVersion', 'Node|12') ]) self.cmd('functionapp config appsettings list -g {} -n {}'.format(resource_group, functionapp)).assert_with_checks([ JMESPathCheck( "[?name=='FUNCTIONS_EXTENSION_VERSION'].value|[0]", '~3') ]) @ResourceGroupPreparer(location=LINUX_ASP_LOCATION_FUNCTIONAPP) @StorageAccountPreparer() def test_functionapp_on_linux_functions_version_consumption(self, resource_group, storage_account): functionapp = self.create_random_name( prefix='functionapp-linux', length=24) self.cmd('functionapp create -g {} -n {} -c {} -s {} --functions-version 3 --runtime node --os-type linux' .format(resource_group, functionapp, LINUX_ASP_LOCATION_FUNCTIONAPP, storage_account), checks=[ JMESPathCheck('name', functionapp) ]) self.cmd('functionapp config show -g {} -n {}'.format(resource_group, functionapp), checks=[ JMESPathCheck('linuxFxVersion', 'Node|12') ]) self.cmd('functionapp config appsettings list -g {} -n {}'.format(resource_group, functionapp)).assert_with_checks([ JMESPathCheck( "[?name=='FUNCTIONS_EXTENSION_VERSION'].value|[0]", '~3') ]) @ResourceGroupPreparer(location=LINUX_ASP_LOCATION_FUNCTIONAPP) @StorageAccountPreparer() def test_functionapp_on_linux_dotnet_consumption(self, resource_group, storage_account): functionapp = self.create_random_name( prefix='functionapp-linux', length=24) self.cmd('functionapp create -g {} -n {} -c {} -s {} --functions-version 3 --runtime dotnet --os-type linux' .format(resource_group, functionapp, LINUX_ASP_LOCATION_FUNCTIONAPP, storage_account), checks=[ JMESPathCheck('name', functionapp) ]) self.cmd('functionapp config show -g {} -n {}'.format(resource_group, functionapp), checks=[ JMESPathCheck('linuxFxVersion', 'dotnet|3.1') ]) self.cmd('functionapp config appsettings list -g {} -n {}'.format(resource_group, functionapp)).assert_with_checks([ JMESPathCheck( "[?name=='FUNCTIONS_EXTENSION_VERSION'].value|[0]", '~3') ]) class FunctionAppServicePlan(ScenarioTest): @ResourceGroupPreparer(location=WINDOWS_ASP_LOCATION_FUNCTIONAPP) def test_functionapp_app_service_plan(self, resource_group): plan = self.create_random_name(prefix='funcappplan', length=24) self.cmd('functionapp plan create -g {} -n {} --sku S1' .format(resource_group, plan), checks=[ JMESPathCheck('sku.name', 'S1') ]) @ResourceGroupPreparer(location=WINDOWS_ASP_LOCATION_FUNCTIONAPP) def test_functionapp_elastic_plan(self, resource_group): plan = self.create_random_name(prefix='funcappplan', length=24) self.cmd('functionapp plan create -g {} -n {} --sku EP1 --min-instances 4 --max-burst 12' .format(resource_group, plan), checks=[ JMESPathCheck('maximumElasticWorkerCount', 12), JMESPathCheck('sku.name', 'EP1'), JMESPathCheck('sku.capacity', 4) ]) self.cmd('functionapp plan update -g {} -n {} --min-instances 5 --max-burst 11' .format(resource_group, plan), checks=[ JMESPathCheck('maximumElasticWorkerCount', 11), JMESPathCheck('sku.name', 'EP1'), JMESPathCheck('sku.capacity', 5) ]) self.cmd('functionapp plan show -g {} -n {} '.format(resource_group, plan), checks=[ JMESPathCheck('maximumElasticWorkerCount', 11), JMESPathCheck('sku.name', 'EP1'), JMESPathCheck('sku.capacity', 5) ]) self.cmd('functionapp delete -g {} -n {}'.format(resource_group, plan)) class FunctionAppServicePlanLinux(ScenarioTest): @ResourceGroupPreparer(location=LINUX_ASP_LOCATION_FUNCTIONAPP) def test_functionapp_app_service_plan_linux(self, resource_group): plan = self.create_random_name(prefix='funcappplan', length=24) self.cmd('functionapp plan create -g {} -n {} --sku S1 --is-linux' .format(resource_group, plan), checks=[ JMESPathCheck('sku.name', 'S1'), JMESPathCheck('kind', 'linux') ]) class FunctionAppSlotTests(ScenarioTest): @ResourceGroupPreparer(location=WINDOWS_ASP_LOCATION_FUNCTIONAPP) @StorageAccountPreparer() def test_functionapp_slot_creation(self, resource_group, storage_account): plan = self.create_random_name(prefix='funcappplan', length=24) functionapp = self.create_random_name( prefix='functionapp-slot', length=24) slotname = self.create_random_name(prefix='slotname', length=24) self.cmd('functionapp plan create -g {} -n {} --sku S1'.format(resource_group, plan), checks=[ JMESPathCheck('sku.name', 'S1'), ]) self.cmd('functionapp create -g {} -n {} --plan {} -s {} --runtime node'.format(resource_group, functionapp, plan, storage_account), checks=[ JMESPathCheck('name', functionapp) ]) self.cmd('functionapp deployment slot create -g {} -n {} --slot {}'.format(resource_group, functionapp, slotname), checks=[ JMESPathCheck('name', slotname), JMESPathCheck('type', 'Microsoft.Web/sites/slots'), ]) pre_slot_list = self.cmd('functionapp deployment slot list -g {} -n {}'.format(resource_group, functionapp), checks=[ JMESPathCheck("[?name=='{}'].type|[0]".format( slotname), 'Microsoft.Web/sites/slots') ]).get_output_in_json() self.assertEqual(len(pre_slot_list), 1) self.cmd('functionapp deployment slot delete -g {} -n {} --slot {}'.format( resource_group, functionapp, slotname)) deleted_slot_list = self.cmd('functionapp deployment slot list -g {} -n {}'.format( resource_group, functionapp)).get_output_in_json() self.assertEqual(len(deleted_slot_list), 0) self.cmd('functionapp delete -g {} -n {}'.format(resource_group, functionapp)) @ResourceGroupPreparer(location=WINDOWS_ASP_LOCATION_FUNCTIONAPP) @StorageAccountPreparer() def test_functionapp_slot_appsetting_update(self, resource_group, storage_account): plan = self.create_random_name(prefix='funcappplan', length=24) functionapp = self.create_random_name( prefix='functionapp-slot', length=24) slotname = self.create_random_name(prefix='slotname', length=24) self.cmd('functionapp plan create -g {} -n {} --sku S1'.format(resource_group, plan), checks=[ JMESPathCheck('sku.name', 'S1'), ]) self.cmd('functionapp create -g {} -n {} --plan {} -s {} --runtime node'.format(resource_group, functionapp, plan, storage_account), checks=[ JMESPathCheck('name', functionapp) ]) self.cmd('functionapp deployment slot create -g {} -n {} --slot {}'.format(resource_group, functionapp, slotname), checks=[ JMESPathCheck('name', slotname) ]) self.cmd('functionapp config appsettings set -g {} -n {} --slot {} --slot-settings FOO=BAR'.format(resource_group, functionapp, slotname), checks=[ JMESPathCheck("[?name=='FOO'].value|[0]", 'BAR'), JMESPathCheck("[?name=='FOO'].slotSetting|[0]", True) ]) self.cmd('functionapp config appsettings list -g {} -n {} --slot {}'.format(resource_group, functionapp, slotname), checks=[ JMESPathCheck("[?name=='FOO'].value|[0]", 'BAR'), JMESPathCheck("[?name=='FOO'].slotSetting|[0]", True) ]) self.cmd('functionapp delete -g {} -n {}'.format(resource_group, functionapp)) @ResourceGroupPreparer(location=WINDOWS_ASP_LOCATION_FUNCTIONAPP) @StorageAccountPreparer() def test_functionapp_slot_swap(self, resource_group, storage_account): plan = self.create_random_name(prefix='funcappplan', length=24) functionapp = self.create_random_name( prefix='functionapp-slot', length=24) slotname = self.create_random_name(prefix='slotname', length=24) self.cmd('functionapp plan create -g {} -n {} --sku S1'.format(resource_group, plan), checks=[ JMESPathCheck('sku.name', 'S1'), ]) self.cmd('functionapp create -g {} -n {} --plan {} -s {} --runtime node'.format(resource_group, functionapp, plan, storage_account), checks=[ JMESPathCheck('name', functionapp) ]) self.cmd('functionapp deployment slot create -g {} -n {} --slot {}'.format(resource_group, functionapp, slotname), checks=[ JMESPathCheck('name', slotname) ]) self.cmd('functionapp config appsettings set -g {} -n {} --slot {} --settings FOO=BAR'.format(resource_group, functionapp, slotname), checks=[ JMESPathCheck("[?name=='FOO'].value|[0]", 'BAR') ]) self.cmd('functionapp deployment slot swap -g {} -n {} --slot {} --action swap'.format( resource_group, functionapp, slotname)) self.cmd('functionapp config appsettings list -g {} -n {}'.format(resource_group, functionapp), checks=[ JMESPathCheck("[?name=='FOO'].value|[0]", 'BAR') ]) self.cmd('functionapp delete -g {} -n {}'.format(resource_group, functionapp)) class WebappAuthenticationTest(ScenarioTest): @ResourceGroupPreparer(name_prefix='cli_test_webapp_authentication', location=WINDOWS_ASP_LOCATION_WEBAPP) def test_webapp_authentication(self, resource_group): webapp_name = self.create_random_name('webapp-authentication-test', 40) plan_name = self.create_random_name('webapp-authentication-plan', 40) self.cmd( 'appservice plan create -g {} -n {} --sku S1'.format(resource_group, plan_name)) self.cmd( 'webapp create -g {} -n {} --plan {}'.format(resource_group, webapp_name, plan_name)) # testing show command for newly created app and initial fields self.cmd('webapp auth show -g {} -n {}'.format(resource_group, webapp_name)).assert_with_checks([ JMESPathCheck('unauthenticatedClientAction', None), JMESPathCheck('defaultProvider', None), JMESPathCheck('enabled', False), JMESPathCheck('tokenStoreEnabled', None), JMESPathCheck('allowedExternalRedirectUrls', None), JMESPathCheck('tokenRefreshExtensionHours', None), JMESPathCheck('runtimeVersion', None), JMESPathCheck('clientId', None), JMESPathCheck('clientSecret', None), JMESPathCheck('allowedAudiences', None), JMESPathCheck('issuer', None), JMESPathCheck('facebookAppId', None), JMESPathCheck('facebookAppSecret', None), JMESPathCheck('facebookOauthScopes', None) ]) # update and verify result = self.cmd('webapp auth update -g {} -n {} --enabled true --action LoginWithFacebook ' '--token-store false --token-refresh-extension-hours 7.2 --runtime-version 1.2.8 ' '--aad-client-id aad_client_id --aad-client-secret aad_secret ' '--aad-allowed-token-audiences https://audience1 --aad-token-issuer-url https://issuer_url ' '--facebook-app-id facebook_id --facebook-app-secret facebook_secret ' '--facebook-oauth-scopes public_profile email' .format(resource_group, webapp_name)).assert_with_checks([ JMESPathCheck( 'unauthenticatedClientAction', 'RedirectToLoginPage'), JMESPathCheck('defaultProvider', 'Facebook'), JMESPathCheck('enabled', True), JMESPathCheck('tokenStoreEnabled', False), JMESPathCheck('tokenRefreshExtensionHours', 7.2), JMESPathCheck('runtimeVersion', '1.2.8'), JMESPathCheck('clientId', 'aad_client_id'), JMESPathCheck('clientSecret', 'aad_secret'), JMESPathCheck('issuer', 'https://issuer_url'), JMESPathCheck('facebookAppId', 'facebook_id'), JMESPathCheck('facebookAppSecret', 'facebook_secret')]).get_output_in_json() self.assertIn('https://audience1', result['allowedAudiences']) self.assertIn('email', result['facebookOauthScopes']) self.assertIn('public_profile', result['facebookOauthScopes']) class WebappUpdateTest(ScenarioTest): @ResourceGroupPreparer(location=WINDOWS_ASP_LOCATION_WEBAPP) def test_webapp_update(self, resource_group): webapp_name = self.create_random_name('webapp-update-test', 40) plan_name = self.create_random_name('webapp-update-plan', 40) self.cmd('appservice plan create -g {} -n {} --sku S1' .format(resource_group, plan_name)) self.cmd('webapp create -g {} -n {} --plan {}' .format(resource_group, webapp_name, plan_name)).assert_with_checks([ JMESPathCheck('clientAffinityEnabled', True)]) # testing update command with --set self.cmd('webapp update -g {} -n {} --client-affinity-enabled false --set tags.foo=bar' .format(resource_group, webapp_name)).assert_with_checks([ JMESPathCheck('name', webapp_name), JMESPathCheck('tags.foo', 'bar'), JMESPathCheck('clientAffinityEnabled', False)]) # try out on slots self.cmd( 'webapp deployment slot create -g {} -n {} -s s1'.format(resource_group, webapp_name)) self.cmd('webapp update -g {} -n {} -s s1 --client-affinity-enabled true'.format(resource_group, webapp_name), checks=[ self.check('clientAffinityEnabled', True) ]) class WebappZipDeployScenarioTest(ScenarioTest): @ResourceGroupPreparer(name_prefix='cli_test_webapp_zipDeploy', location=WINDOWS_ASP_LOCATION_WEBAPP) def test_deploy_zip(self, resource_group): webapp_name = self.create_random_name('webapp-zipDeploy-test', 40) plan_name = self.create_random_name('webapp-zipDeploy-plan', 40) zip_file = os.path.join(TEST_DIR, 'test.zip') self.cmd( 'appservice plan create -g {} -n {} --sku S1'.format(resource_group, plan_name)) self.cmd( 'webapp create -g {} -n {} --plan {}'.format(resource_group, webapp_name, plan_name)) self.cmd('webapp deployment source config-zip -g {} -n {} --src "{}"'.format(resource_group, webapp_name, zip_file)).assert_with_checks([ JMESPathCheck('status', 4), JMESPathCheck('deployer', 'ZipDeploy'), JMESPathCheck('message', 'Created via a push deployment'), JMESPathCheck('complete', True) ]) # Disabled due to issue https://github.com/Azure/azure-cli/issues/10705 # class FunctionappRemoteBuildScenarioTest(ScenarioTest): # @ResourceGroupPreparer() # @StorageAccountPreparer() # def test_functionapp_remote_build(self, resource_group, storage_account): # functionapp_name = self.create_random_name(prefix='faremotebuildapp', length=24) # plan_name = self.create_random_name(prefix='faremotebuildplan', length=24) # zip_file = os.path.join(TEST_DIR, 'test_remote_build.zip') # self.cmd('functionapp plan create -g {} -n {} --sku S1 --is-linux true'.format(resource_group, plan_name)) # self.cmd('functionapp create -g {} -n {} --plan {} -s {} --os-type Linux --runtime python'.format(resource_group, functionapp_name, plan_name, storage_account)) # self.cmd('functionapp deployment source config-zip -g {} -n {} --src "{}"'.format(resource_group, functionapp_name, zip_file)).assert_with_checks([ # JMESPathCheck('status', 4), # JMESPathCheck('deployer', 'Push-Deployer'), # JMESPathCheck('message', 'Created via a push deployment'), # JMESPathCheck('complete', True) # ]) class WebappImplictIdentityTest(ScenarioTest): @AllowLargeResponse(8192) @ResourceGroupPreparer(location=WINDOWS_ASP_LOCATION_WEBAPP) def test_webapp_assign_system_identity(self, resource_group): scope = '/subscriptions/{}/resourcegroups/{}'.format( self.get_subscription_id(), resource_group) role = 'Reader' plan_name = self.create_random_name('web-msi-plan', 20) webapp_name = self.create_random_name('web-msi', 20) self.cmd( 'appservice plan create -g {} -n {}'.format(resource_group, plan_name)) self.cmd( 'webapp create -g {} -n {} --plan {}'.format(resource_group, webapp_name, plan_name)) with mock.patch('azure.cli.core.commands.arm._gen_guid', side_effect=self.create_guid): result = self.cmd('webapp identity assign -g {} -n {} --role {} --scope {}'.format( resource_group, webapp_name, role, scope)).get_output_in_json() self.cmd('webapp identity show -g {} -n {}'.format(resource_group, webapp_name), checks=[ self.check('principalId', result['principalId']) ]) self.cmd('role assignment list -g {} --assignee {}'.format(resource_group, result['principalId']), checks=[ JMESPathCheck('length([])', 1), JMESPathCheck('[0].roleDefinitionName', role) ]) self.cmd('webapp identity show -g {} -n {}'.format(resource_group, webapp_name), checks=self.check('principalId', result['principalId'])) self.cmd( 'webapp identity remove -g {} -n {}'.format(resource_group, webapp_name)) self.cmd('webapp identity show -g {} -n {}'.format(resource_group, webapp_name), checks=self.is_empty()) class WebappListLocationsFreeSKUTest(ScenarioTest): @ResourceGroupPreparer(name_prefix='cli_test_webapp_list-locations-free-sku-test') def test_webapp_list_locations_free_sku(self, resource_group): asp_F1 = self.cmd( 'appservice list-locations --sku F1').get_output_in_json() result = self.cmd( 'appservice list-locations --sku Free').get_output_in_json() self.assertEqual(asp_F1, result) class WebappTriggeredWebJobListTest(ScenarioTest): @record_only() @ResourceGroupPreparer() def test_webapp_triggeredWebjob_list(self, resource_group): # testing this using a webjob already created # given there is no create command inorder to re-record please create a webjob before # recording this. Once the create command is available, please remove the "record_only" flag resource_group_name = 'cliTestApp' webapp_name = 'cliTestApp' webjob_name = 'test-triggered' # list test self.cmd('webapp webjob triggered list -g {} -n {}' .format(resource_group_name, webapp_name)).assert_with_checks([ JMESPathCheck('length(@)', 1), JMESPathCheck( '[0].name', '{}/{}'.format(webapp_name, webjob_name)), JMESPathCheck('[0].type', 'Microsoft.Web/sites/triggeredwebjobs')]) class WebappContinuousWebJobE2ETest(ScenarioTest): @ResourceGroupPreparer() @record_only() def test_webapp_continuousWebjob_e2e(self, resource_group): # testing this using a webjob already created # given there is no create command inorder to re-record please create a webjob before # recording this. Once the create command is available, please remove the "record_only" flag resource_group_name = 'cliTestApp' webapp_name = 'cliTestApp' webjob_name = 'test-continuous' # list test self.cmd('webapp webjob continuous list -g {} -n {}' .format(resource_group_name, webapp_name)).assert_with_checks([ JMESPathCheck('length(@)', 1), JMESPathCheck( '[0].name', '{}/{}'.format(webapp_name, webjob_name)), JMESPathCheck('[0].type', 'Microsoft.Web/sites/continuouswebjobs')]) # start self.cmd('webapp webjob continuous start -g {} -n {} -w {}' .format(resource_group_name, webapp_name, webjob_name)).assert_with_checks([ JMESPathCheck('status', 'Running')]) # stop self.cmd('webapp webjob continuous stop -g {} -n {} -w {}' .format(resource_group_name, webapp_name, webjob_name)).assert_with_checks([ JMESPathCheck('status', 'Disabling')]) class WebappWindowsContainerBasicE2ETest(ScenarioTest): @AllowLargeResponse() @ResourceGroupPreparer(name_prefix='webapp_hyperv_e2e', location='eastus') def test_webapp_hyperv_e2e(self, resource_group): webapp_name = self.create_random_name( prefix='webapp-hyperv-e2e', length=24) plan = self.create_random_name(prefix='webapp-hyperv-plan', length=24) self.cmd( 'appservice plan create -g {} -n {} --hyper-v --sku PC2'.format(resource_group, plan)) self.cmd('appservice plan list -g {}'.format(resource_group), checks=[ JMESPathCheck('length(@)', 1), JMESPathCheck('[0].name', plan), JMESPathCheck('[0].sku.tier', 'PremiumContainer'), JMESPathCheck('[0].sku.name', 'PC2') ]) self.cmd('appservice plan list -g {}'.format(resource_group), checks=[ JMESPathCheck("length([?name=='{}' && resourceGroup=='{}'])".format( plan, resource_group), 1) ]) self.cmd('appservice plan show -g {} -n {}'.format(resource_group, plan), checks=[ JMESPathCheck('name', plan) ]) self.cmd('webapp create -g {} -n {} --plan {} --deployment-container-image-name "DOCKER|microsoft/iis:nanoserver-sac2016"'.format(resource_group, webapp_name, plan), checks=[ JMESPathCheck('state', 'Running'), JMESPathCheck('name', webapp_name), JMESPathCheck('hostNames[0]', webapp_name + '.azurewebsites.net') ]) self.cmd('webapp list -g {}'.format(resource_group), checks=[ JMESPathCheck('length(@)', 1), JMESPathCheck('[0].name', webapp_name), JMESPathCheck('[0].hostNames[0]', webapp_name + '.azurewebsites.net') ]) self.cmd('webapp config show -g {} -n {}'.format(resource_group, webapp_name), checks=[ JMESPathCheck('windowsFxVersion', "DOCKER|microsoft/iis:nanoserver-sac2016"), JMESPathCheck('linuxFxVersion', "") ]) self.cmd('webapp config set -g {} -n {} --windows-fx-version "DOCKER|microsoft/iis"'.format( resource_group, webapp_name)) self.cmd('webapp config show -g {} -n {}'.format(resource_group, webapp_name), checks=[ JMESPathCheck('windowsFxVersion', "DOCKER|microsoft/iis"), JMESPathCheck('linuxFxVersion', "") ]) # Always on is not supported on all SKUs this is to test that we don't fail create trying to enable AlwaysOn @ResourceGroupPreparer(name_prefix='cli_test_webapp_alwaysOn', location=WINDOWS_ASP_LOCATION_WEBAPP) def test_webapp_create_noAlwaysOn(self, resource_group): webapp_name = self.create_random_name('webapp-create-alwaysOn-e2e', 44) plan = self.create_random_name('plan-create-alwaysOn-e2e', 44) self.cmd( 'appservice plan create -g {} -n {} --sku SHARED'.format(resource_group, plan)) self.cmd( 'webapp create -g {} -n {} --plan {}'.format(resource_group, webapp_name, plan)) # verify alwaysOn self.cmd('webapp config show -g {} -n {}'.format(resource_group, webapp_name)).assert_with_checks([ JMESPathCheck('alwaysOn', False)]) @ResourceGroupPreparer(name_prefix='cli_test_webapp_linux_free', location=LINUX_ASP_LOCATION_WEBAPP) def test_webapp_create_linux_free(self, resource_group): webapp_name = self.create_random_name('webapp-linux-free', 24) plan = self.create_random_name('plan-linux-free', 24) self.cmd('appservice plan create -g {} -n {} --sku F1 --is-linux'.format(resource_group, plan), checks=[ # this weird field means it is a linux JMESPathCheck('reserved', True), JMESPathCheck('sku.name', 'F1')]) self.cmd('webapp create -g {} -n {} --plan {} -u {} -r "node|10.14"'.format(resource_group, webapp_name, plan, TEST_REPO_URL)) # verify alwaysOn self.cmd('webapp config show -g {} -n {}'.format(resource_group, webapp_name)).assert_with_checks([ JMESPathCheck('alwaysOn', False)]) class WebappNetworkConnectionTests(ScenarioTest): @AllowLargeResponse() @ResourceGroupPreparer(location=WINDOWS_ASP_LOCATION_WEBAPP) def test_webapp_hybridconnectionE2E(self, resource_group): webapp_name = self.create_random_name('hcwebapp', 24) plan = self.create_random_name('hcplan', 24) namespace_name = self.create_random_name('hcnamespace', 24) hyco_name = self.create_random_name('hcname', 24) um = "[{{\\\"key\\\":\\\"endpoint\\\",\\\"value\\\":\\\"vmsq1:80\\\"}}]" self.cmd( 'appservice plan create -g {} -n {} --sku S1'.format(resource_group, plan)) self.cmd( 'webapp create -g {} -n {} --plan {}'.format(resource_group, webapp_name, plan)) self.cmd( 'relay namespace create -g {} --name {}'.format(resource_group, namespace_name)) self.cmd('relay hyco create -g {} --namespace-name {} --name {} --user-metadata {}'.format( resource_group, namespace_name, hyco_name, um)) self.cmd('webapp hybrid-connection add -g {} -n {} --namespace {} --hybrid-connection {}'.format( resource_group, webapp_name, namespace_name, hyco_name)) self.cmd('webapp hybrid-connection list -g {} -n {}'.format(resource_group, webapp_name), checks=[ JMESPathCheck('length(@)', 1), JMESPathCheck('[0].name', hyco_name) ]) self.cmd('webapp hybrid-connection remove -g {} -n {} --namespace {} --hybrid-connection {}'.format( resource_group, webapp_name, namespace_name, hyco_name)) self.cmd('webapp hybrid-connection list -g {} -n {}'.format(resource_group, webapp_name), checks=[ JMESPathCheck('length(@)', 0) ]) @ResourceGroupPreparer(location=WINDOWS_ASP_LOCATION_WEBAPP) def test_webapp_vnetE2E(self, resource_group): webapp_name = self.create_random_name('swiftwebapp', 24) plan = self.create_random_name('swiftplan', 24) subnet_name = self.create_random_name('swiftsubnet', 24) vnet_name = self.create_random_name('swiftname', 24) self.cmd('network vnet create -g {} -n {} --address-prefix 10.0.0.0/16 --subnet-name {} --subnet-prefix 10.0.0.0/24'.format( resource_group, vnet_name, subnet_name)) self.cmd( 'appservice plan create -g {} -n {} --sku P1V2'.format(resource_group, plan)) self.cmd( 'webapp create -g {} -n {} --plan {}'.format(resource_group, webapp_name, plan)) self.cmd('webapp vnet-integration add -g {} -n {} --vnet {} --subnet {}'.format( resource_group, webapp_name, vnet_name, subnet_name)) self.cmd('webapp vnet-integration list -g {} -n {}'.format(resource_group, webapp_name), checks=[ JMESPathCheck('length(@)', 1), JMESPathCheck('[0].name', subnet_name) ]) self.cmd( 'webapp vnet-integration remove -g {} -n {}'.format(resource_group, webapp_name)) self.cmd('webapp vnet-integration list -g {} -n {}'.format(resource_group, webapp_name), checks=[ JMESPathCheck('length(@)', 0) ]) @ResourceGroupPreparer(location=WINDOWS_ASP_LOCATION_WEBAPP) def test_webapp_vnetDelegation(self, resource_group): webapp_name = self.create_random_name('swiftwebapp', 24) plan = self.create_random_name('swiftplan', 24) subnet_name = self.create_random_name('swiftsubnet', 24) vnet_name = self.create_random_name('swiftname', 24) self.cmd('network vnet create -g {} -n {} --address-prefix 10.0.0.0/16 --subnet-name {} --subnet-prefix 10.0.0.0/24'.format( resource_group, vnet_name, subnet_name)) self.cmd('network vnet subnet update -g {} --vnet {} --name {} --delegations Microsoft.Web/serverfarms --service-endpoints Microsoft.Storage'.format( resource_group, vnet_name, subnet_name)) self.cmd( 'appservice plan create -g {} -n {} --sku P1V2'.format(resource_group, plan)) self.cmd( 'webapp create -g {} -n {} --plan {}'.format(resource_group, webapp_name, plan)) self.cmd('webapp vnet-integration add -g {} -n {} --vnet {} --subnet {}'.format( resource_group, webapp_name, vnet_name, subnet_name)) self.cmd('webapp vnet-integration list -g {} -n {}'.format(resource_group, webapp_name), checks=[ JMESPathCheck('length(@)', 1), JMESPathCheck('[0].name', subnet_name) ]) self.cmd(' network vnet subnet show -g {} -n {} --vnet-name {}'.format(resource_group, subnet_name, vnet_name), checks=[ JMESPathCheck('serviceEndpoints[0].service', "Microsoft.Storage") ]) self.cmd( 'webapp vnet-integration remove -g {} -n {}'.format(resource_group, webapp_name)) self.cmd('webapp vnet-integration list -g {} -n {}'.format(resource_group, webapp_name), checks=[ JMESPathCheck('length(@)', 0) ]) @ResourceGroupPreparer(location=WINDOWS_ASP_LOCATION_WEBAPP) def test_webapp_vnetSameName(self, resource_group): resource_group_2 = self.create_random_name('swiftwebapp', 24) webapp_name = self.create_random_name('swiftwebapp', 24) plan = self.create_random_name('swiftplan', 24) subnet_name = self.create_random_name('swiftsubnet', 24) subnet_name_2 = self.create_random_name('swiftsubnet', 24) vnet_name = self.create_random_name('swiftname', 24) self.cmd('network vnet create -g {} -n {} --address-prefix 10.0.0.0/16 --subnet-name {} --subnet-prefix 10.0.0.0/24'.format( resource_group, vnet_name, subnet_name)) self.cmd('group create -n {} -l {}'.format(resource_group_2, WINDOWS_ASP_LOCATION_WEBAPP)) vnet = self.cmd('network vnet create -g {} -n {} --address-prefix 10.0.0.0/16 --subnet-name {} --subnet-prefix 10.0.0.0/24'.format( resource_group_2, vnet_name, subnet_name_2)).get_output_in_json() self.cmd( 'appservice plan create -g {} -n {} --sku P1V2'.format(resource_group, plan)) self.cmd( 'webapp create -g {} -n {} --plan {}'.format(resource_group, webapp_name, plan)) # Add vnet integration where theres two vnets of the same name. Chosen vnet should default to the one in the same RG self.cmd('webapp vnet-integration add -g {} -n {} --vnet {} --subnet {}'.format( resource_group, webapp_name, vnet_name, subnet_name)) self.cmd('webapp vnet-integration list -g {} -n {}'.format(resource_group, webapp_name), checks=[ JMESPathCheck('length(@)', 1), JMESPathCheck('[0].name', subnet_name) ]) self.cmd( 'webapp vnet-integration remove -g {} -n {}'.format(resource_group, webapp_name)) self.cmd('webapp vnet-integration list -g {} -n {}'.format(resource_group, webapp_name), checks=[ JMESPathCheck('length(@)', 0) ]) # Add vnet integration using vnet resource ID self.cmd('webapp vnet-integration add -g {} -n {} --vnet {} --subnet {}'.format( resource_group, webapp_name, vnet['newVNet']['id'], subnet_name_2)) self.cmd('webapp vnet-integration list -g {} -n {}'.format(resource_group, webapp_name), checks=[ JMESPathCheck('length(@)', 1), JMESPathCheck('[0].name', subnet_name_2) ]) # self.cmd( # 'webapp vnet-integration remove -g {} -n {}'.format(resource_group, webapp_name)) # self.cmd('webapp vnet-integration list -g {} -n {}'.format(resource_group, webapp_name), checks=[ # JMESPathCheck('length(@)', 0) # ]) # LiveScenarioTest due to issue https://github.com/Azure/azure-cli/issues/10705 class FunctionappDeploymentLogsScenarioTest(LiveScenarioTest): @ResourceGroupPreparer(location=WINDOWS_ASP_LOCATION_FUNCTIONAPP) @StorageAccountPreparer() def test_functionapp_show_deployment_logs(self, resource_group, storage_account): functionapp_name = self.create_random_name(prefix='show-deployment-functionapp', length=40) plan_name = self.create_random_name(prefix='show-deployment-functionapp', length=40) zip_file = os.path.join(TEST_DIR, 'sample_dotnet_function/sample_dotnet_function.zip') self.cmd('functionapp plan create -g {} -n {} --sku S1'.format(resource_group, plan_name)) self.cmd('functionapp create -g {} -n {} --plan {} -s {} --runtime dotnet'.format(resource_group, functionapp_name, plan_name, storage_account)) self.cmd('functionapp log deployment show -g {} -n {}'.format(resource_group, functionapp_name), checks=[ JMESPathCheck('length(@)', 0) ]) requests.get('http://{}.scm.azurewebsites.net'.format(functionapp_name), timeout=240) time.sleep(30) deployment_1 = self.cmd('functionapp deployment source config-zip -g {} -n {} --src "{}"'.format(resource_group, functionapp_name, zip_file)).assert_with_checks([ JMESPathCheck('status', 4), JMESPathCheck('deployer', 'ZipDeploy'), JMESPathCheck('complete', True) ]).get_output_in_json() self.cmd('functionapp log deployment show -g {} -n {}'.format(resource_group, functionapp_name), checks=[ JMESPathCheck('length(@) > `0`', True) ]) self.cmd('functionapp log deployment show -g {} -n {} --deployment-id={}'.format(resource_group, functionapp_name, deployment_1['id']), checks=[ JMESPathCheck('length(@) > `0`', True) ]) @ResourceGroupPreparer(location=WINDOWS_ASP_LOCATION_FUNCTIONAPP) @StorageAccountPreparer() def test_functionapp_list_deployment_logs(self, resource_group, storage_account): functionapp_name = self.create_random_name(prefix='show-deployment-funcapp', length=40) plan_name = self.create_random_name(prefix='show-deployment-funcapp', length=40) zip_file = os.path.join(TEST_DIR, 'sample_dotnet_function/sample_dotnet_function.zip') self.cmd('functionapp plan create -g {} -n {} --sku S1'.format(resource_group, plan_name)) self.cmd('functionapp create -g {} -n {} --plan {} -s {} --runtime dotnet'.format(resource_group, functionapp_name, plan_name, storage_account)) self.cmd('functionapp log deployment list -g {} -n {}'.format(resource_group, functionapp_name), checks=[ JMESPathCheck('length(@)', 0) ]) requests.get('http://{}.scm.azurewebsites.net'.format(functionapp_name), timeout=240) time.sleep(30) deployment_1 = self.cmd('functionapp deployment source config-zip -g {} -n {} --src "{}"'.format(resource_group, functionapp_name, zip_file)).assert_with_checks([ JMESPathCheck('status', 4), JMESPathCheck('deployer', 'ZipDeploy'), JMESPathCheck('complete', True) ]).get_output_in_json() self.cmd('functionapp log deployment list -g {} -n {}'.format(resource_group, functionapp_name), checks=[ JMESPathCheck('length(@)', 1), JMESPathCheck('[0].id', deployment_1['id']), ]) requests.get('http://{}.scm.azurewebsites.net'.format(functionapp_name), timeout=240) time.sleep(30) self.cmd('functionapp deployment source config-zip -g {} -n {} --src "{}"'.format(resource_group, functionapp_name, zip_file)).assert_with_checks([ JMESPathCheck('status', 4), JMESPathCheck('deployer', 'ZipDeploy'), JMESPathCheck('complete', True) ]).get_output_in_json() self.cmd('functionapp log deployment list -g {} -n {}'.format(resource_group, functionapp_name), checks=[ JMESPathCheck('length(@)', 2) ]) class WebappDeploymentLogsScenarioTest(ScenarioTest): @ResourceGroupPreparer(location=WINDOWS_ASP_LOCATION_WEBAPP) def test_webapp_show_deployment_logs(self, resource_group): webapp_name = self.create_random_name('show-deployment-webapp', 40) plan_name = self.create_random_name('show-deployment-plan', 40) zip_file = os.path.join(TEST_DIR, 'test.zip') self.cmd('appservice plan create -g {} -n {} --sku S1'.format(resource_group, plan_name)) self.cmd('webapp create -g {} -n {} --plan {}'.format(resource_group, webapp_name, plan_name)) self.cmd('webapp log deployment show -g {} -n {}'.format(resource_group, webapp_name), checks=[ JMESPathCheck('length(@)', 0) ]) deployment_1 = self.cmd('webapp deployment source config-zip -g {} -n {} --src "{}"'.format(resource_group, webapp_name, zip_file)).get_output_in_json() self.cmd('webapp log deployment show -g {} -n {}'.format(resource_group, webapp_name), checks=[ JMESPathCheck('length(@) > `0`', True), ]) self.cmd('webapp log deployment show -g {} -n {} --deployment-id={}'.format(resource_group, webapp_name, deployment_1['id']), checks=[ JMESPathCheck('length(@) > `0`', True), ]) @ResourceGroupPreparer(location=WINDOWS_ASP_LOCATION_WEBAPP) def test_webapp_list_deployment_logs(self, resource_group): webapp_name = self.create_random_name('list-deployment-webapp', 40) plan_name = self.create_random_name('list-deployment-plan', 40) zip_file = os.path.join(TEST_DIR, 'test.zip') self.cmd('appservice plan create -g {} -n {} --sku S1'.format(resource_group, plan_name)) self.cmd('webapp create -g {} -n {} --plan {}'.format(resource_group, webapp_name, plan_name)) self.cmd('webapp log deployment list -g {} -n {}'.format(resource_group, webapp_name), checks=[ JMESPathCheck('length(@)', 0) ]) deployment_1 = self.cmd('webapp deployment source config-zip -g {} -n {} --src "{}"'.format(resource_group, webapp_name, zip_file)).get_output_in_json() self.cmd('webapp log deployment list -g {} -n {}'.format(resource_group, webapp_name), checks=[ JMESPathCheck('length(@)', 1), JMESPathCheck('[0].id', deployment_1['id']), ]) self.cmd('webapp deployment source config-zip -g {} -n {} --src "{}"'.format(resource_group, webapp_name, zip_file)).get_output_in_json() self.cmd('webapp log deployment list -g {} -n {}'.format(resource_group, webapp_name), checks=[ JMESPathCheck('length(@)', 2) ]) class WebappLocalContextScenarioTest(LocalContextScenarioTest): @ResourceGroupPreparer(location=WINDOWS_ASP_LOCATION_WEBAPP) def test_webapp_local_context(self, resource_group): from knack.util import CLIError self.kwargs.update({ 'plan_name': self.create_random_name(prefix='webapp-plan-', length=24), 'webapp_name': self.create_random_name(prefix='webapp-', length=24) }) self.cmd('appservice plan create -g {rg} -n {plan_name}') self.cmd('appservice plan show') with self.assertRaises(CLIError): self.cmd('appservice plan delete') self.cmd('webapp create -n {webapp_name}') self.cmd('webapp show') with self.assertRaises(CLIError): self.cmd('webapp delete') self.cmd('webapp delete -n {webapp_name}') self.cmd('appservice plan delete -n {plan_name} -y') class FunctionappLocalContextScenarioTest(LocalContextScenarioTest): @ResourceGroupPreparer(location=WINDOWS_ASP_LOCATION_FUNCTIONAPP) @StorageAccountPreparer() def test_functionapp_local_context(self, resource_group, storage_account): from knack.util import CLIError self.kwargs.update({ 'plan_name': self.create_random_name(prefix='functionapp-plan-', length=24), 'functionapp_name': self.create_random_name(prefix='functionapp-', length=24), 'storage_account': storage_account }) self.cmd('functionapp plan create -g {rg} -n {plan_name} --sku B2') self.cmd('functionapp plan show') with self.assertRaises(CLIError): self.cmd('functionapp plan delete') self.cmd('functionapp create -n {functionapp_name} --storage-account {storage_account}') self.cmd('functionapp show') with self.assertRaises(CLIError): self.cmd('functionapp delete') self.cmd('functionapp delete -n {functionapp_name}') self.cmd('functionapp plan delete -n {plan_name} -y') if __name__ == '__main__': unittest.main()
src/azure-cli/azure/cli/command_modules/appservice/tests/latest/test_webapp_commands.py
150,061
-------------------------------------------------------------------------------------------- Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT License. See License.txt in the project root for license information. -------------------------------------------------------------------------------------------- pylint: disable=line-too-long In the future, for any reasons the repository get removed, the source code is under "sample-repo-for-deployment-test" you can use to rebuild the repository test idempotency test idempotency turn on diagnostics TODO: contact webapp team for where to retrieve 'level' show publish profile info show publishing credentials verify httpsOnly is false verify creating an non node app using --runtime TODO: Bug 14409. Re-enable check after fixing https://github.com/Azure/azure-cli/issues/14409 self.cmd('webapp config show -g {} -n {}'.format(resource_group, webapp_name), checks=[ JMESPathCheck('phpVersion', '7.3') ]) 30 seconds should be enough for the deployment finished(Skipped under playback mode) verify the web page verify the web page verify app settings verify the web page 45 seconds should be enough for the deployment finished(Skipped under playback mode) verify the web page dump out more info for diagnose Test Framework is not able to handle binary file format, hence, only run live 30 seconds should be enough for the deployment finished(Skipped under playback mode) sanity check the traces load the site to produce a few traces sanity check the traces test empty service plan should be automatically deleted. verify the baseline update and verify site appsettings testing update through key value pairs show delete hostnames site azure storage account configurations tests this weird field means it is a linux add update list delete site connection string tests see deployment user; just make sure the command does return something site appsettings testing update through key value pairs show show update through a json file with key value pair output using the output of the set/list command update site config start with shared sku 0 means the default value: 1 instance scale up scale down scale out we will try to produce an error by try creating 2 webapp with same name in different groups TODO: ensure test fx can capture error details for us to verify allowed_exceptions='Website with given name {} already exists'.format(webapp_name) this test doesn't contain the ultimate verification which you need to manually load the frontpage in a browser this weird field means it is a linux workaround the fact that a new linux web's "kind" won't be settled instantatest_linux_webapp_remote_sshneously we mask the password we mask the password On Windows, test 'webapp ssh' throws error verify the web page verify the web page Make sure the related settings are deleted Make sure the related settings are deleted verify that show-cd-url works the same way You can create and use any repros with the 3 files under "./sample_web" You can create and use any repros with the 3 files under "./sample_web" and with a 'staging 'branch create a few app-settings to test they can be cloned create an empty slot swap with prod and verify the git branch also switched create a new slot by cloning from prod slot verify we can swap with non production slot try another way to delete a slot and exercise all options You can create and use any repros with the 3 files under "./sample_web" and with a 'staging 'branch create an empty slot functionapp has pre-defined cors. We verify the ones we added are in the list You can create and use any repros with the 3 files under "./sample_web" and with a 'staging 'branch create an empty slot swap with preview complete the swap reset Cert Generated using https://docs.microsoft.com/azure/app-service-web/web-sites-configure-ssl-certificatebkmk_ssopenssl we configure tags here in a hope to capture a repro for https://github.com/Azure/azure-cli/issues/6929 test with slot Cert Generated using https://docs.microsoft.com/azure/app-service-web/web-sites-configure-ssl-certificatebkmk_ssopenssl we configure tags here in a hope to capture a repro for https://github.com/Azure/azure-cli/issues/6929 Cert Generated using https://docs.microsoft.com/azure/app-service-web/web-sites-configure-ssl-certificatebkmk_ssopenssl we configure tags here in a hope to capture a repro for https://github.com/Azure/azure-cli/issues/6929 this weird field means it is a linux this weird field means it is a linux Moving to and from an App Service plan (not Elastic Premium) is not allowed right now this weird field means it is a linux this weird field means it is a linux this weird field means it is a linux testing show command for newly created app and initial fields update and verify testing update command with --set try out on slots Disabled due to issue https://github.com/Azure/azure-cli/issues/10705 class FunctionappRemoteBuildScenarioTest(ScenarioTest): @ResourceGroupPreparer() @StorageAccountPreparer() def test_functionapp_remote_build(self, resource_group, storage_account): functionapp_name = self.create_random_name(prefix='faremotebuildapp', length=24) plan_name = self.create_random_name(prefix='faremotebuildplan', length=24) zip_file = os.path.join(TEST_DIR, 'test_remote_build.zip') self.cmd('functionapp plan create -g {} -n {} --sku S1 --is-linux true'.format(resource_group, plan_name)) self.cmd('functionapp create -g {} -n {} --plan {} -s {} --os-type Linux --runtime python'.format(resource_group, functionapp_name, plan_name, storage_account)) self.cmd('functionapp deployment source config-zip -g {} -n {} --src "{}"'.format(resource_group, functionapp_name, zip_file)).assert_with_checks([ JMESPathCheck('status', 4), JMESPathCheck('deployer', 'Push-Deployer'), JMESPathCheck('message', 'Created via a push deployment'), JMESPathCheck('complete', True) ]) testing this using a webjob already created given there is no create command inorder to re-record please create a webjob before recording this. Once the create command is available, please remove the "record_only" flag list test testing this using a webjob already created given there is no create command inorder to re-record please create a webjob before recording this. Once the create command is available, please remove the "record_only" flag list test start stop Always on is not supported on all SKUs this is to test that we don't fail create trying to enable AlwaysOn verify alwaysOn this weird field means it is a linux verify alwaysOn Add vnet integration where theres two vnets of the same name. Chosen vnet should default to the one in the same RG Add vnet integration using vnet resource ID self.cmd( 'webapp vnet-integration remove -g {} -n {}'.format(resource_group, webapp_name)) self.cmd('webapp vnet-integration list -g {} -n {}'.format(resource_group, webapp_name), checks=[ JMESPathCheck('length(@)', 0) ]) LiveScenarioTest due to issue https://github.com/Azure/azure-cli/issues/10705
7,094
en
0.669078
""" [1,0,1,0,1] -> correct [0,0,1,0] -> return 1 [1,1,0,0,1] -> return 2 """ def solution2(S): ans1, ans2 = 0, 0 check1, check2 = 0, 1 for i in S: if i != check1: ans1 +=1 if i != check2: ans2 += 1 check1 = 0 if check1 == 1 else 1 check2 = 0 if check2 == 1 else 1 assert(check1 != check2) print("ans1 : {}, ans2 : {}".format(ans1, ans2)) return min(ans1, ans2) def solution(S): ans1, ans2 = 0, 0 S1 = S.copy() S2 = S.copy() leng = len(S) if leng == 1: return 0 if leng == 2: if S[0] != S[1]: return 0 else: return 1 # Forward for idx in range(leng): if idx == 0 or idx == leng-1: continue # [0,0,0] or [1,1,1] if S1[idx] == S1[idx-1] and S1[idx] == S1[idx+1]: if S1[idx] == 1: S1[idx] = 0 else: S1[idx] = 1 ans1 += 1 # [0, 0, 1] if S1[idx] == S1[idx-1] and S1[idx] != S1[idx+1]: if S1[idx-1] == 1: S1[idx-1] = 0 else: S1[idx-1] = 1 ans1 += 1 # [1,0,0] if S1[idx] != S1[idx-1] and S1[idx] == S1[idx+1]: if S1[idx+1] == 1: S1[idx+1] = 0 else: S1[idx+1] = 1 ans1 += 1 # backwards for idx in range(leng-1,-1,-1): if idx == 0 or idx == leng-1: continue # [0,0,0] or [1,1,1] back if S2[idx] == S2[idx-1] and S2[idx] == S2[idx+1]: if S2[idx] == 1: S2[idx] = 0 else: S2[idx] = 1 ans2 += 1 # [0, 0, 1] if S2[idx] == S2[idx-1] and S2[idx] != S2[idx+1]: if S2[idx-1] == 1: S2[idx-1] = 0 else: S2[idx-1] = 1 ans2 += 1 # [1,0,0] if S2[idx] != S2[idx-1] and S2[idx] == S2[idx+1]: if S2[idx+1] == 1: S2[idx+1] = 0 else: S2[idx+1] = 1 ans2 += 1 return min(ans1, ans2) # print(solution([0,1,0,1,0,0,0,0,0,1])) # print(solution([1,0,0,0,0,1,0])) # print(solution([1,0,0])) # print(solution([0,0,1])) # print(solution([1,0,1])) # print(solution([0,1,0])) # print(solution2([0,1,0,1,0,0,0,0,0,1])) print(solution([0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1])) print(solution2([0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1])) # print(solution2([1,0,0])) # print(solution2([0,0,1])) # print(solution2([1,0,1])) # print(solution2([0,1,0]))
python3/coin_flip.py
2,681
[1,0,1,0,1] -> correct [0,0,1,0] -> return 1 [1,1,0,0,1] -> return 2 Forward [0,0,0] or [1,1,1] [0, 0, 1] [1,0,0] backwards [0,0,0] or [1,1,1] back [0, 0, 1] [1,0,0] print(solution([0,1,0,1,0,0,0,0,0,1])) print(solution([1,0,0,0,0,1,0])) print(solution([1,0,0])) print(solution([0,0,1])) print(solution([1,0,1])) print(solution([0,1,0])) print(solution2([0,1,0,1,0,0,0,0,0,1])) print(solution2([1,0,0])) print(solution2([0,0,1])) print(solution2([1,0,1])) print(solution2([0,1,0]))
552
en
0.684141
# Copyright 2015-2021 SWIM.AI inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest from swimai.recon._parsers import _OutputMessage from swimai.recon._writers import _BoolWriter, _IdentWriter, _NumberWriter, _StringWriter, _SlotWriter, _ReconWriter, \ _AttrWriter, _BlockWriter from swimai.structures import Text, Slot, Attr, Num, Bool from swimai.structures._structs import _Extant, _Absent, _Record from test.utils import CustomItem class TestWriters(unittest.TestCase): def test_ident_writer_str(self): # Given message = 'hello' # When actual = _IdentWriter._write(message) # Then self.assertIsInstance(actual, _OutputMessage) self.assertEqual('hello', actual._value) def test_ident_writer_empty(self): # Given message = '' # When actual = _IdentWriter._write(message) # Then self.assertIsInstance(actual, _OutputMessage) self.assertEqual('', actual._value) def test_bool_writer_true(self): # Given message = True # When actual = _BoolWriter._write(message) # Then self.assertIsInstance(actual, _OutputMessage) self.assertEqual('true', actual._value) def test_bool_writer_false(self): # Given message = False # When actual = _BoolWriter._write(message) # Then self.assertIsInstance(actual, _OutputMessage) self.assertEqual('false', actual._value) def test_bool_writer_none(self): # Given message = None # When actual = _BoolWriter._write(message) # Then self.assertIsInstance(actual, _OutputMessage) self.assertEqual('false', actual._value) def test_number_writer_zero(self): # Given message = 0 # When actual = _NumberWriter._write(message) # Then self.assertIsInstance(actual, _OutputMessage) self.assertEqual('0', actual._value) def test_number_writer_int(self): # Given message = 25 # When actual = _NumberWriter._write(message) # Then self.assertIsInstance(actual, _OutputMessage) self.assertEqual('25', actual._value) def test_number_writer_float(self): # Given message = 0.02 # When actual = _NumberWriter._write(message) # Then self.assertIsInstance(actual, _OutputMessage) self.assertEqual('0.02', actual._value) def test_number_writer_none(self): # Given message = None # When actual = _NumberWriter._write(message) # Then self.assertIsInstance(actual, _OutputMessage) self.assertEqual('', actual._value) def test_string_writer_str(self): # Given message = 'This is dog' # When actual = _StringWriter._write(message) # Then self.assertIsInstance(actual, _OutputMessage) self.assertEqual('"This is dog"', actual._value) def test_string_writer_empty(self): # Given message = None # When actual = _StringWriter._write(message) # Then self.assertIsInstance(actual, _OutputMessage) self.assertEqual('""', actual._value) def test_slot_writer_existing_key_and_value(self): # Given key = Text.create_from('animal') value = Text.create_from('dog') writer = _ReconWriter() # When actual = _SlotWriter._write(key=key, writer=writer, value=value) # Then self.assertIsInstance(actual, _OutputMessage) self.assertEqual('animal:dog', actual._value) def test_slot_writer_existing_key_missing_value(self): # Given key = Text.create_from('animal') writer = _ReconWriter() # When actual = _SlotWriter._write(key=key, writer=writer) # Then self.assertIsInstance(actual, _OutputMessage) self.assertEqual('animal:', actual._value) def test_slot_writer_missing_key_existing_value(self): # Given value = Text.create_from('dog') writer = _ReconWriter() # When actual = _SlotWriter._write(value=value, writer=writer) # Then self.assertIsInstance(actual, _OutputMessage) self.assertEqual(':dog', actual._value) def test_slot_writer_missing_key_and_value(self): # Given writer = _ReconWriter() # When actual = _SlotWriter._write(writer=writer) # Then self.assertIsInstance(actual, _OutputMessage) self.assertEqual(':', actual._value) def test_attr_writer_existing_key_and_value_text(self): # Given key = Text.create_from('bird') value = Text.create_from('chirp') writer = _ReconWriter() # When actual = _AttrWriter._write(key=key, writer=writer, value=value) # Then self.assertIsInstance(actual, _OutputMessage) self.assertEqual('@bird(chirp)', actual._message) def test_attr_writer_existing_key_and_value_slot(self): # Given key = Text.create_from('animal') value = _Record.create() value.add(Slot.create_slot(Text.create_from('dog'), Text.create_from('bark'))) writer = _ReconWriter() # When actual = _AttrWriter._write(key=key, writer=writer, value=value) # Then self.assertIsInstance(actual, _OutputMessage) self.assertEqual('@animal(dog:bark)', actual._message) def test_attr_writer_missing_key_existing_value(self): # Given value = Text.create_from('chirp') writer = _ReconWriter() # When actual = _AttrWriter._write(writer=writer, value=value) # Then self.assertIsInstance(actual, _OutputMessage) self.assertEqual('@(chirp)', actual._message) def test_attr_writer_existing_key_missing_value(self): # Given key = Text.create_from('bird') writer = _ReconWriter() # When actual = _AttrWriter._write(key=key, writer=writer) # Then self.assertIsInstance(actual, _OutputMessage) self.assertEqual('@bird', actual._message) def test_attr_writer_existing_key_extant_value(self): # Given key = Text.create_from('bird') value = _Extant._get_extant() writer = _ReconWriter() # When actual = _AttrWriter._write(key=key, writer=writer, value=value) # Then self.assertIsInstance(actual, _OutputMessage) self.assertEqual('@bird', actual._message) def test_attr_writer_missing_key_and_value(self): # Given writer = _ReconWriter() # When actual = _AttrWriter._write(writer=writer) # Then self.assertIsInstance(actual, _OutputMessage) self.assertEqual('@', actual._message) def test_block_writer_attr(self): # Given items = list() items.append(Attr.create_attr(Text.create_from('dog'), Text.create_from('bark'))) writer = _ReconWriter() # When actual = _BlockWriter._write(items, writer=writer) # Then self.assertIsInstance(actual, _OutputMessage) self.assertEqual('@dog(bark)', actual._message) def test_block_writer_text_single(self): # Given items = list() items.append(Text.create_from('Dead parrot')) writer = _ReconWriter() # When actual = _BlockWriter._write(items, writer=writer) # Then self.assertIsInstance(actual, _OutputMessage) self.assertEqual('"Dead parrot"', actual._message) def test_block_writer_text_multiple(self): # Given items = list() items.append(Text.create_from('foo_')) items.append(Text.create_from('bar')) writer = _ReconWriter() # When actual = _BlockWriter._write(items, writer=writer) # Then self.assertIsInstance(actual, _OutputMessage) self.assertEqual('foo_bar', actual._message) def test_block_writer_slot_single_first(self): # Given items = list() items.append(Slot.create_slot(Text.create_from('cat'), Text.create_from('meow'))) writer = _ReconWriter() # When actual = _BlockWriter._write(items, writer=writer, first=True) # Then self.assertIsInstance(actual, _OutputMessage) self.assertEqual('cat:meow', actual._message) def test_block_writer_slot_single_not_first(self): # Given items = list() items.append(Slot.create_slot(Text.create_from('cat'), Text.create_from('meow'))) writer = _ReconWriter() # When actual = _BlockWriter._write(items, writer=writer, first=False) # Then self.assertIsInstance(actual, _OutputMessage) self.assertEqual(',cat:meow', actual._message) def test_block_writer_slot_multiple(self): # Given items = list() items.append(Slot.create_slot(Text.create_from('dog'), Text.create_from('bark'))) items.append(Slot.create_slot(Text.create_from('cat'), Text.create_from('meow'))) writer = _ReconWriter() # When actual = _BlockWriter._write(items, writer=writer, first=True) # Then self.assertIsInstance(actual, _OutputMessage) self.assertEqual('dog:bark,cat:meow', actual._message) def test_block_writer_slot_in_attr(self): # Given items = list() record_map = _Record.create() record_map.add(Slot.create_slot(Text.create_from('cat'), Text.create_from('meow'))) items.append(Attr.create_attr(Text.create_from('animal'), record_map)) writer = _ReconWriter() # When actual = _BlockWriter._write(items, writer=writer, first=True) # Then self.assertIsInstance(actual, _OutputMessage) self.assertEqual('@animal(cat:meow)', actual._message) def test_block_writer_slot_in_attr_and_slot(self): # Given items = list() record_map = _Record.create() record_map.add(Slot.create_slot(Text.create_from('dog'), Text.create_from('bark'))) items.append(Attr.create_attr(Text.create_from('animal'), record_map)) items.append(Slot.create_slot(Text.create_from('cat'), Text.create_from('meow'))) writer = _ReconWriter() # When actual = _BlockWriter._write(items, writer=writer, first=True) # Then self.assertIsInstance(actual, _OutputMessage) self.assertEqual('@animal(dog:bark){cat:meow}', actual._message) def test_block_writer_multiple_attributes(self): # Given items = list() record_map = _Record.create() dog_map = _Record.create() dog_map.add(Slot.create_slot(Text.create_from('dog'), Text.create_from('bark'))) record_map.add(Attr.create_attr(Text.create_from('Animal'), dog_map)) cat_map = _Record.create() cat_map.add(Slot.create_slot(Text.create_from('cat'), Text.create_from('meow'))) record_map.add(Attr.create_attr(Text.create_from('Animal'), cat_map)) bird_map = _Record.create() bird_map.add(Slot.create_slot(Text.create_from('bird'), Text.create_from('chirp'))) record_map.add(Attr.create_attr(Text.create_from('Animal'), bird_map)) items.append(record_map) writer = _ReconWriter() # When actual = _BlockWriter._write(items, writer=writer, first=True) # Then self.assertIsInstance(actual, _OutputMessage) self.assertEqual('@Animal(dog:bark)@Animal(cat:meow)@Animal(bird:chirp)', actual._message) def test_block_writer_nested_attributes(self): # Given items = list() name_record = _Record.create() name_record.add(Slot.create_slot(Text.create_from('Name'), Text.create_from('Collie'))) breed_record = _Record.create() breed_record.add(Attr.create_attr(Text.create_from('Breed'), name_record)) dog_record = _Record.create() dog_record.add(Slot.create_slot(Text.create_from('Dog'), breed_record)) species_record = _Record.create() species_record.add(Attr.create_attr(Text.create_from('Species'), dog_record)) animals_record = _Record.create() animals_record.add(Slot.create_slot(Text.create_from('Animals'), species_record)) items.append(Attr.create_attr(Text.create_from('Zoo'), animals_record)) writer = _ReconWriter() # When actual = _BlockWriter._write(items, writer=writer, first=True) # Then self.assertIsInstance(actual, _OutputMessage) self.assertEqual('@Zoo(Animals:@Species(Dog:@Breed(Name:Collie)))', actual._message) def test_block_writer_empty(self): # Given items = list() writer = _ReconWriter() # When actual = _BlockWriter._write(items, writer=writer) # Then self.assertIsInstance(actual, _OutputMessage) self.assertEqual('', actual._message) def test_write_record_single(self): # Given record = _Record.create() record.add(Text.create_from('Dog')) writer = _ReconWriter() # When actual = writer._write_record(record) # Then self.assertIsInstance(actual, _OutputMessage) self.assertEqual(3, actual._size) self.assertEqual('Dog', actual._value) self.assertEqual('g', actual._last_char) def test_write_record_multiple(self): # Given record = _Record.create() record.add(Text.create_from('Dog')) record.add(Text.create_from('Cat')) writer = _ReconWriter() # When actual = writer._write_record(record) # Then self.assertIsInstance(actual, _OutputMessage) self.assertEqual(6, actual._size) self.assertEqual('DogCat', actual._value) self.assertEqual('t', actual._last_char) def test_write_record_empty(self): # Given record = _Record.create() writer = _ReconWriter() # When actual = writer._write_record(record) # Then self.assertIsNone(actual) def test_write_value_record(self): # Given record = _Record.create() record.add(Slot.create_slot(Text.create_from('Cow'), Text.create_from('Moo'))) writer = _ReconWriter() # When actual = writer._write_value(record) # Then self.assertIsInstance(actual, _OutputMessage) self.assertEqual(7, actual._size) self.assertEqual('Cow:Moo', actual._value) self.assertEqual('o', actual._last_char) def test_write_value_text_ident(self): # Given ident = Text.create_from('Duck') writer = _ReconWriter() # When actual = writer._write_value(ident) # Then self.assertIsInstance(actual, _OutputMessage) self.assertEqual(4, actual._size) self.assertEqual('Duck', actual._value) self.assertEqual('k', actual._last_char) def test_write_value_text_string(self): # Given string = Text.create_from('$duck') writer = _ReconWriter() # When actual = writer._write_value(string) # Then self.assertIsInstance(actual, _OutputMessage) self.assertEqual(7, actual._size) self.assertEqual('"$duck"', actual._value) self.assertEqual('"', actual._last_char) def test_write_value_number(self): # Given number = Num.create_from(-13.1) writer = _ReconWriter() # When actual = writer._write_value(number) # Then self.assertIsInstance(actual, _OutputMessage) self.assertEqual(5, actual._size) self.assertEqual('-13.1', actual._value) self.assertEqual('1', actual._last_char) def test_write_value_bool(self): # Given boolean = Bool.create_from(False) writer = _ReconWriter() # When actual = writer._write_value(boolean) # Then self.assertIsInstance(actual, _OutputMessage) self.assertEqual(5, actual._size) self.assertEqual('false', actual._value) self.assertEqual('e', actual._last_char) def test_write_value_absent(self): # Given absent = _Absent._get_absent() writer = _ReconWriter() # When actual = writer._write_value(absent) # Then self.assertIsInstance(actual, _OutputMessage) self.assertEqual(0, actual._size) self.assertEqual('', actual._value) def test_write_slot(self): # Given key = Text.create_from('Hello') value = Text.create_from('Friend') writer = _ReconWriter() # When actual = writer._write_slot(key, value) # Then self.assertIsInstance(actual, _OutputMessage) self.assertEqual(12, actual._size) self.assertEqual('Hello:Friend', actual._value) self.assertEqual('d', actual._last_char) def test_write_attr(self): # Given key = Text.create_from('Hello') value = Text.create_from('Friend') writer = _ReconWriter() # When actual = writer._write_attr(key, value) # Then self.assertIsInstance(actual, _OutputMessage) self.assertEqual(14, actual._size) self.assertEqual('@Hello(Friend)', actual._value) self.assertEqual(')', actual._last_char) def test_write_item_attr(self): # Given item = Attr.create_attr(Text.create_from('Cat'), Text.create_from('Meow')) writer = _ReconWriter() # When actual = writer._write_item(item) # Then self.assertIsInstance(actual, str) self.assertEqual('@Cat(Meow)', actual) def test_write_item_slot(self): # Given item = Slot.create_slot(Text.create_from('Age'), Num.create_from(32)) writer = _ReconWriter() # When actual = writer._write_item(item) # Then self.assertIsInstance(actual, str) self.assertEqual('Age:32', actual) def test_write_item_value(self): # Given item = Text.create_from('Horse#') writer = _ReconWriter() # When actual = writer._write_item(item) # Then self.assertIsInstance(actual, str) self.assertEqual('"Horse#"', actual) def test_write_item_invalid(self): # Given item = CustomItem() writer = _ReconWriter() # When with self.assertRaises(TypeError) as error: writer._write_item(item) # Then message = error.exception.args[0] self.assertEqual('No Recon serialization for CustomItem!', message)
test/recon/test_writers.py
19,419
Copyright 2015-2021 SWIM.AI inc. 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. Given When Then Given When Then Given When Then Given When Then Given When Then Given When Then Given When Then Given When Then Given When Then Given When Then Given When Then Given When Then Given When Then Given When Then Given When Then Given When Then Given When Then Given When Then Given When Then Given When Then Given When Then Given When Then Given When Then Given When Then Given When Then Given When Then Given When Then Given When Then Given When Then Given When Then Given When Then Given When Then Given When Then Given When Then Given When Then Given When Then Given When Then Given When Then Given When Then Given When Then Given When Then Given When Then Given When Then Given When Then Given When Then Given When Then Given When Then
1,315
en
0.530443
# coding=utf-8 # Copyright 2021 The TensorFlow Datasets Authors. # # 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. """Github pathlib-like util.""" import dataclasses import functools import os import pathlib import posixpath from typing import Iterator, Mapping, MutableMapping, Optional, Set, Tuple import requests from tensorflow_datasets.core import utils JsonValue = utils.JsonValue _URI_PREFIX = 'github://' def _get_token(): # Get the secret API token to avoid the 60 calls/hour limit # To get the current quota or test the token: # curl -H "Authorization: token ${GITHUB_TOKEN}" https://api.github.com/rate_limit # pylint: disable=line-too-long return os.environ.get('GITHUB_TOKEN') def get_content(url: str) -> bytes: resp = requests.get(url) if resp.status_code != 200: raise FileNotFoundError(f'Request failed for {url}\n' f' Error: {resp.status_code}\n' f' Reason: {resp.content}') return resp.content class GithubApi: """Class to issue calls to the Github API.""" def __init__(self, token: Optional[str] = None): self._token = token or _get_token() def query(self, url: str) -> JsonValue: """Launches a Github API query and returns the result.""" headers = {} if self._token: headers['Authorization'] = f'token {self._token}' resp = requests.get(url, headers=headers) if resp.status_code != 200: raise FileNotFoundError( f'Request failed:\n' f' Request: {url}\n' f' Error: {resp.status_code}\n' f' Reason: {resp.content}',) return resp.json() def query_tree(self, repo: str, branch: str) -> JsonValue: """Queries a repository tree. See https://docs.github.com/en/rest/reference/git#trees Args: repo: the repository branch: the branch for which to get the tree Returns: JSON dict with the tree. """ url = f'https://api.github.com/repos/{repo}/git/trees/{branch}?recursive=1' return self.query(url) def _correct_folder(folder: str) -> str: """Ensures the folder follows a standard. Pathlib.parent in the root folder results in '.', whereas in other places we should use '' for the root folder. This function makes sure the root folder is always empty string. Args: folder: the folder to be corrected. Returns: The corrected folder. """ if folder == '.': return '' return folder def _get_parent_folder(path: pathlib.PurePosixPath) -> str: return _correct_folder(os.fspath(path.parent)) @dataclasses.dataclass(frozen=True) class _GithubElement: """Representation of an element in a Github tree (a file or folder). Attributes: parent_folder: the folder in which this element resides. name: the name of this element, e.g. the file name or the folder name. is_folder: whether this element is a folder or not. """ parent_folder: str name: str is_folder: bool @classmethod def from_path(cls, path: pathlib.PurePosixPath, is_folder: bool) -> '_GithubElement': parent_folder = _get_parent_folder(path) name = path.name return cls(parent_folder=parent_folder, name=name, is_folder=is_folder) @dataclasses.dataclass(frozen=True) class _GithubTree: """A Github tree of a repository.""" files_per_folder: Mapping[str, Set[_GithubElement]] def is_folder(self, path: str) -> bool: return _correct_folder(path) in self.files_per_folder def is_file(self, path: pathlib.PurePosixPath) -> bool: parent_folder = _get_parent_folder(path) files = self.files_per_folder.get(parent_folder) if not files: return False file = _GithubElement( parent_folder=parent_folder, name=path.name, is_folder=False) return file in files @classmethod def from_json(cls, value) -> '_GithubTree': """Parses a GithubTree from the given JSON.""" if not isinstance(value, dict) or 'tree' not in value: raise ValueError(f'Github API response not supported: {value}') files_per_folder: MutableMapping[str, Set[str]] = {} for element in value['tree']: github_element = _GithubElement.from_path( path=pathlib.PurePosixPath(element['path']), is_folder=(element['type'] == 'tree')) if element['type'] in {'blob', 'tree'}: files_per_folder.setdefault(github_element.parent_folder, set()) files_per_folder[github_element.parent_folder].add(github_element) return _GithubTree(files_per_folder=files_per_folder) @staticmethod @functools.lru_cache(maxsize=None) def from_cache(repo: str, branch: str) -> '_GithubTree': """Factory which caches the entire Github tree.""" tree_json = GithubApi().query_tree(repo, branch) # If the tree is truncated, then we'll need a more sophisticated method to # retrieve the whole tree. Since this is currently not supported, it raises # an exception. assert not tree_json.get('truncated', False) return _GithubTree.from_json(tree_json) @dataclasses.dataclass(frozen=True, eq=True) class _PathMetadata: """Github metadata of a file or directory.""" path: str repo: str # e.g. `tensorflow/datasets` branch: str # e.g. `master` subpath: str # e.g 'core/__init__.py' @classmethod def from_path(cls, path: str) -> '_PathMetadata': repo, branch, subpath = _parse_github_path(path) return cls(path=path, repo=repo, branch=branch, subpath=subpath) @utils.register_pathlike_cls(_URI_PREFIX) class GithubPath(pathlib.PurePosixPath): """`pathlib.Path` like object for manipulating Github paths. Example: ``` path = GithubPath.from_repo('tensorflow/datasets') path = path / 'docs' / 'catalog' assert path.is_dir() datasets = [ p.name for p in path.iterdir() if p.match('*.md') ] path = GithubPath('github://tensorflow/datasets/tree/master/docs/README.md') assert path.subpath == 'docs/README.md' assert path.repo == 'tensorflow/datasets' assert path.branch == 'master' ``` """ def __new__(cls, *parts: utils.PathLike) -> 'GithubPath': full_path = '/'.join(os.fspath(p) for p in parts) _parse_github_path(full_path) return super().__new__(cls, full_path.replace(_URI_PREFIX, '/github/', 1)) @utils.memoized_property def _path_str(self) -> str: return posixpath.join(_URI_PREFIX, *self.parts[2:]) def __fspath__(self) -> str: return self._path_str def __str__(self) -> str: # pylint: disable=invalid-str-returned return self._path_str @classmethod def from_repo(cls, repo: str, branch: str = 'master') -> 'GithubPath': """Factory to creates a GithubPath from a repo name. Args: repo: Repo name (e.g. `tensorflow/datasets`) branch: Branch name (e.g. `master`, 'v1.2.0', '0d240e8b85c'). Default to master. Returns: github_path: The repository root dir at head """ return cls(f'github://{repo}/tree/{branch}') @utils.memoized_property def _metadata(self) -> _PathMetadata: return _PathMetadata.from_path(os.fspath(self)) @property def subpath(self) -> str: """The inner path (e.g. `core/__init__.py`).""" return self._metadata.subpath @property def repo(self) -> str: """The repository identifier (e.g. `tensorflow/datasets`).""" return self._metadata.repo @property def branch(self) -> str: """The branch (e.g. `master`, `v2`, `43bbad116df`,...).""" return self._metadata.branch @property def github_tree(self) -> _GithubTree: return _GithubTree.from_cache(self.repo, self.branch) def as_raw_url(self) -> str: """Returns the raw content url (https://raw.githubusercontent.com).""" return ('https://raw.githubusercontent.com/' f'{self.repo}/{self.branch}/{self.subpath}') def as_human_friendly_url(self) -> str: """Returns the human friendly url.""" return f'https://github.com/{self.repo}/blob/{self.branch}/{self.subpath}' def iterdir(self) -> Iterator['GithubPath']: """Yields the sub-paths.""" if not self.is_dir(): raise NotADirectoryError(f'{self.subpath} is not a directory.') for filename in self.github_tree.files_per_folder[self.subpath]: yield self / filename.name def is_dir(self) -> bool: """Returns True if the path is a directory or submodule.""" return self.github_tree.is_folder(self.subpath) def is_file(self) -> bool: """Returns True if the path is a file.""" return self.github_tree.is_file(pathlib.PurePosixPath(self.subpath)) def exists(self) -> bool: """Returns True if the path exists.""" return self.is_dir() or self.is_file() def read_bytes(self) -> bytes: """Returns the file content as bytes.""" # As the content is fetched during the Github API calls, we could cache it # and return it directly here, rather than using an additional query. # However this might have significant memory impact if many `GithubPath` # are used, so would require some additional cleanup (weakref ?). # Using raw_url doesn't count in the API calls quota and should works with # arbitrary sized files. url = self.as_raw_url() return get_content(url) def read_text(self, encoding: Optional[str] = None) -> str: """Returns the file content as string.""" return self.read_bytes().decode(encoding=encoding or 'utf-8') def copy( self, dst: utils.PathLike, overwrite: bool = False, ) -> utils.ReadWritePath: """Copy the current file to the given destination. Args: dst: Target file. It can be any PathLike compatible path (e.g. `gs://...`) overwrite: Whether the file should be overwritten or not Returns: The new created file. Raises: FileExistsError: If `overwrite` is false and destination exists. """ dst = utils.as_path(dst) if not overwrite and dst.exists(): raise FileExistsError(f'Cannot copy {self}. Destination {dst} exists.') # Otherwise, copy src to dst dst.write_bytes(self.read_bytes()) return dst def _parse_github_path(path: str) -> Tuple[str, str, str]: """Parse the absolute github path. Args: path: The full github path. Returns: repo: The repository identifiant. branch: Repository branch. subpath: The inner path. Raises: ValueError: If the path is invalid """ err_msg = (f'Invalid github path: {path}. Expected format: ' '`github://<owner>/<name>/tree/<branch>[/<path>]`.') if not path.startswith(_URI_PREFIX): raise ValueError(err_msg) if path.endswith('/'): raise ValueError(err_msg + ' Trailing `/` not supported.') parts = path[len(_URI_PREFIX):].split('/') if len(parts) < 4: raise ValueError(err_msg) # 'tensorflow', 'datasets', 'tree', 'master', ... owner, repo, tree, branch, *subpath = parts if tree != 'tree': raise ValueError(err_msg + '. `/blob/` isn\'t accepted. Only `/tree/`.') return f'{owner}/{repo}', branch, '/'.join(subpath)
tensorflow_datasets/core/github_api/github_path.py
11,467
Class to issue calls to the Github API. `pathlib.Path` like object for manipulating Github paths. Example: ``` path = GithubPath.from_repo('tensorflow/datasets') path = path / 'docs' / 'catalog' assert path.is_dir() datasets = [ p.name for p in path.iterdir() if p.match('*.md') ] path = GithubPath('github://tensorflow/datasets/tree/master/docs/README.md') assert path.subpath == 'docs/README.md' assert path.repo == 'tensorflow/datasets' assert path.branch == 'master' ``` Representation of an element in a Github tree (a file or folder). Attributes: parent_folder: the folder in which this element resides. name: the name of this element, e.g. the file name or the folder name. is_folder: whether this element is a folder or not. A Github tree of a repository. Github metadata of a file or directory. Ensures the folder follows a standard. Pathlib.parent in the root folder results in '.', whereas in other places we should use '' for the root folder. This function makes sure the root folder is always empty string. Args: folder: the folder to be corrected. Returns: The corrected folder. Parse the absolute github path. Args: path: The full github path. Returns: repo: The repository identifiant. branch: Repository branch. subpath: The inner path. Raises: ValueError: If the path is invalid Returns the human friendly url. Returns the raw content url (https://raw.githubusercontent.com). The branch (e.g. `master`, `v2`, `43bbad116df`,...). Copy the current file to the given destination. Args: dst: Target file. It can be any PathLike compatible path (e.g. `gs://...`) overwrite: Whether the file should be overwritten or not Returns: The new created file. Raises: FileExistsError: If `overwrite` is false and destination exists. Returns True if the path exists. Factory which caches the entire Github tree. Parses a GithubTree from the given JSON. Factory to creates a GithubPath from a repo name. Args: repo: Repo name (e.g. `tensorflow/datasets`) branch: Branch name (e.g. `master`, 'v1.2.0', '0d240e8b85c'). Default to master. Returns: github_path: The repository root dir at head Returns True if the path is a directory or submodule. Returns True if the path is a file. Yields the sub-paths. Launches a Github API query and returns the result. Queries a repository tree. See https://docs.github.com/en/rest/reference/git#trees Args: repo: the repository branch: the branch for which to get the tree Returns: JSON dict with the tree. Returns the file content as bytes. Returns the file content as string. The repository identifier (e.g. `tensorflow/datasets`). The inner path (e.g. `core/__init__.py`). Github pathlib-like util. coding=utf-8 Copyright 2021 The TensorFlow Datasets Authors. 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. Get the secret API token to avoid the 60 calls/hour limit To get the current quota or test the token: curl -H "Authorization: token ${GITHUB_TOKEN}" https://api.github.com/rate_limit pylint: disable=line-too-long If the tree is truncated, then we'll need a more sophisticated method to retrieve the whole tree. Since this is currently not supported, it raises an exception. e.g. `tensorflow/datasets` e.g. `master` e.g 'core/__init__.py' pylint: disable=invalid-str-returned As the content is fetched during the Github API calls, we could cache it and return it directly here, rather than using an additional query. However this might have significant memory impact if many `GithubPath` are used, so would require some additional cleanup (weakref ?). Using raw_url doesn't count in the API calls quota and should works with arbitrary sized files. Otherwise, copy src to dst 'tensorflow', 'datasets', 'tree', 'master', ...
4,214
en
0.712656
# # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER # # Copyright (c) 2015 Juniper Networks, Inc. # All rights reserved. # # Use is subject to license terms. # # Licensed under the Apache License, Version 2.0 (the ?License?); you may not # use this file except in compliance with the License. You may obtain a copy of # the License at http://www.apache.org/licenses/LICENSE-2.0. # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # import logging import re from django.contrib import messages from django.http import HttpResponseRedirect from django.shortcuts import render, get_object_or_404 from common.lib import imageUtils from common.lib import libvirtUtils from common.lib import openstackUtils from common.lib import osUtils from images.models import Image from images.models import ImageBlankForm from images.models import ImageForm from images.models import ImageLocalForm from wistar import configuration from wistar import settings logger = logging.getLogger(__name__) def index(request): image_list = imageUtils.get_local_image_list() context = {'image_list': image_list} return render(request, 'images/index.html', context) def edit(request, image_id): image = get_object_or_404(Image, pk=image_id) # template = get_object_or_404(ConfigTemplate, pk=template_id) # template_form = ConfigTemplateForm(instance=template) image_form = ImageForm(instance=image) return render(request, 'images/edit.html', {'image': image, 'image_form': image_form}) def update(request): if "name" in request.POST: image_id = request.POST["image_id"] image_name = request.POST["name"] image_description = request.POST["description"] image_type = request.POST["type"] image = get_object_or_404(Image, pk=image_id) image.name = image_name image.description = image_description image.type = image_type image.save() messages.info(request, "Image updated") return HttpResponseRedirect('/images/') else: if "image_id" in request.POST: image_id = request.POST["image_id"] image = get_object_or_404(Image, pk=image_id) messages.info(request, "Image updated") return render(request, 'edit.html', {'image': image}) else: messages.info(request, "Could not update image! No name or ID found in request!") return HttpResponseRedirect('/images/') def new(request): image_form = ImageForm() context = {'image_form': image_form, "vm_types": configuration.vm_image_types} return render(request, 'images/new.html', context) def create(request): try: logger.debug('---- Create Image ----') image_form = ImageForm(request.POST, request.FILES) if not image_form.is_valid(): logger.error("Could not save image for some reason!") context = {'image_form': image_form} return render(request, 'images/new.html', context) # if not osUtils.checkPath(image_form.cleaned_data['path']): # logger.debug("PATH DOESN'T EXIST") # context = {'error' : "PATH DOESNT EXIST"} # return render(request, 'error.html', context) logger.debug("Saving form") orig_image = image_form.save() messages.info(request, "Image uploaded successfully") image_type = request.POST["type"] image_name = request.POST["name"] full_path = orig_image.filePath.path if re.match(".*\.vmdk$", full_path): # we need to convert this for KVM based deployments! converted_image_path = re.sub("\.vmdk$", ".qcow2", full_path) converted_image_file_name = converted_image_path.split('/')[-1] if osUtils.convert_vmdk_to_qcow2(full_path, converted_image_path): logger.info("Converted vmdk image to qcow2!") orig_image.filePath = "user_images/%s" % converted_image_file_name orig_image.save() logger.debug("Removing original vmdk") osUtils.remove_instance(full_path) else: logger.error("Could not convert vmdk!") if image_type == "junos_vre_15" and "jinstall64-vmx-15.1" in full_path: logger.debug("Creating RIOT image for Junos vMX 15.1") # lets replace the last "." with "_riot." if '.' in full_path: new_image_path = re.sub(r"(.*)\.(.*)$", r"\1_riot.\2", full_path) else: # if there is no '.', let's just add one new_image_path = full_path + "_riot.img" new_image_file_name = new_image_path.split('/')[-1] new_image_name = image_name + ' Riot PFE' if osUtils.copy_image_to_clone(full_path, new_image_path): logger.debug("Copied from %s" % full_path) logger.debug("Copied to %s" % new_image_path) image = Image() image.name = new_image_name image.type = "junos_riot" image.description = orig_image.description + "\nRiot PFE" image.filePath = "user_images/" + new_image_file_name image.save() return HttpResponseRedirect('/images') except Exception as e: logger.error(e) messages.info(request, "Could not create image!") return HttpResponseRedirect('/images/') def blank(request): image_form = ImageBlankForm() context = {'image_form': image_form} return render(request, 'images/new_blank.html', context) def local(request): image_form = ImageLocalForm() context = {'image_form': image_form} return render(request, 'images/new_local.html', context) def create_blank(request): image_form = ImageBlankForm(request.POST) logger.debug(image_form) logger.debug(str(image_form)) if image_form.is_valid(): name = request.POST["name"] size = request.POST["size"] description = request.POST["description"] file_path = 'user_images/' + name if ".img" not in file_path: file_path += ".img" full_path = settings.MEDIA_ROOT + "/" + file_path if osUtils.create_blank_image(full_path, size + 'G'): image = Image() image.description = description image.name = name image.filePath = file_path image.type = 'blank' image.save() # if not osUtils.checkPath(image_form.cleaned_data['path']): # logger.debug("PATH DOESN'T EXIST") # context = {'error' : "PATH DOESNT EXIST"} # return render(request, 'error.html', context) logger.debug("Saving form") # image_form.save() return HttpResponseRedirect('/images') else: context = {'image_form': image_form} return render(request, 'images/new_blank.html', context) def create_local(request): name = request.POST["name"] file_path = request.POST["filePath"] description = request.POST["description"] image_type = request.POST["type"] try: imageUtils.create_local_image(name, description, file_path, image_type) except Exception as e: context = {'error': str(e)} return render(request, 'error.html', context) messages.info(request, "Image Created!") return HttpResponseRedirect('/images') def block_pull(request, uuid): domain = libvirtUtils.get_domain_by_uuid(uuid) domain_name = domain.name() image_path = libvirtUtils.get_image_for_domain(domain.UUIDString()) if osUtils.is_image_thin_provisioned(image_path): logger.debug("Found thinly provisioned image, promoting...") rv = libvirtUtils.promote_instance_to_image(domain_name) if rv is None: messages.info(request, "Image already promoted. Shut down the instance to perform a clone.") elif rv: messages.info(request, "Promoting thinly provisioned image") else: messages.info(request, "Error Promoting image!") else: messages.info(request, "Image is already promoted. You may now shutdown the image and perform a Clone") logger.debug("Image is already promoted") return HttpResponseRedirect('/ajax/manageHypervisor/') def create_from_instance(request, uuid): logger.debug("Creating new image from instance") domain = libvirtUtils.get_domain_by_uuid(uuid) logger.debug("got domain " + domain.name()) domain_image = libvirtUtils.get_image_for_domain(uuid) logger.debug("got domain_image: " + domain_image) if osUtils.is_image_thin_provisioned(domain_image): logger.error("Cannot clone disk that is thinly provisioned! Please perform a block pull before continuing") context = {'error': "Cannot Clone thinly provisioned disk! Please perform a block pull!"} return render(request, 'error.html', context) domain_name = domain.name() # FIXME - make these variable names a bit more clear about their meaning # we need to get the path of the image relative to the MEDIA_ROOT media_root = settings.MEDIA_ROOT media_root_array = media_root.split("/") len_media_root = len(media_root_array) full_path_array = domain_image.split("/") full_path = "/".join(full_path_array[:full_path_array.index('instances')]) # grab the file path of the domain image without the MEDIA_ROOT prepended file_path_array = domain_image.split('/')[len_media_root:] images_dir = "/".join(file_path_array[:file_path_array.index('instances')]) new_relative_image_path = images_dir + "/image_" + str(domain.UUIDString()) + ".img" new_full_image_path = full_path + "/image_" + str(domain.UUIDString()) + ".img" if osUtils.check_path(new_full_image_path): logger.info("Image has already been cloned") context = {'error': "Instance has already been cloned!"} return render(request, 'error.html', context) logger.debug("Copying image from " + domain_image) logger.debug("To " + new_full_image_path) osUtils.copy_image_to_clone(domain_image, new_full_image_path) image = Image() image.name = "image_" + str(domain.UUIDString()) image.description = "Clone of " + domain_name image.filePath = new_relative_image_path image.save() return HttpResponseRedirect('/images/') def detail(request, image_id): image = get_object_or_404(Image, pk=image_id) vm_type = "N/A" for vt in configuration.vm_image_types: if vt["name"] == image.type: vm_type = vt["description"] break glance_id = "" image_state = "" if configuration.deployment_backend == "openstack": openstackUtils.connect_to_openstack() glance_id = openstackUtils.get_image_id_for_name(image.name) elif configuration.deployment_backend == "kvm" and image.filePath != "": image_state = osUtils.is_image_thin_provisioned(image.filePath.path) return render(request, 'images/details.html', {'image': image, 'state': image_state, "vm_type": vm_type, "settings": settings, "glance_id": glance_id, "use_openstack": configuration.use_openstack, "openstack_host": configuration.openstack_host }) def glance_detail(request): """ OpenStack specific action to get image details from Glance :param request: HTTPRequest :return: rendered HTML """ required_fields = set(['imageId']) if not required_fields.issubset(request.POST): return render(request, 'ajax/ajaxError.html', {'error': "Invalid Parameters in POST"}) image_id = request.POST["imageId"] image = get_object_or_404(Image, pk=image_id) if openstackUtils.connect_to_openstack(): glance_id = openstackUtils.get_image_id_for_name(image.name) glance_json = dict() if glance_id is not None: glance_json = openstackUtils.get_glance_image_detail(glance_id) logger.debug("glance json of %s is" % glance_id) logger.debug(glance_json) logger.debug("---") return render(request, 'images/glance_detail.html', {'image': glance_json, "image_id": image_id, "glance_id": glance_id, "openstack_host": configuration.openstack_host }) else: return render(request, 'error.html', {'error': "Could not connect to OpenStack"}) def glance_list(request): image_list = imageUtils.get_glance_image_list() context = {'image_list': image_list} return render(request, 'images/glance_list.html', context) def delete(request, image_id): imageUtils.delete_image_by_id(image_id) messages.info(request, "Image deleted!") return HttpResponseRedirect('/images/') def list_glance_images(request): if openstackUtils.connect_to_openstack(): image_list = openstackUtils.list_glance_images() context = {'error': image_list} return render(request, 'error.html', context) context = {'error': "Could not connect to OpenStack"} return render(request, 'error.html', context) def upload_to_glance(request, image_id): if openstackUtils.connect_to_openstack(): image = get_object_or_404(Image, pk=image_id) logger.debug("Uploading now!") if osUtils.check_path(image.filePath.path): openstackUtils.upload_image_to_glance(image.name, image.filePath.path) logger.debug("All done") return HttpResponseRedirect('/images/%s' % image_id) def import_from_glance(request, glance_id): """ Creates a local db entry for the glance image Everything in Wistar depends on a db entry in the Images table If you have an existing openstack cluster, you may want to import those images here without having to physically copy the images to local disk :param request: HTTPRequest object :param glance_id: id of the glance image to import :return: redirect to /images/image_id """ if openstackUtils.connect_to_openstack(): image_details = openstackUtils.get_glance_image_detail(glance_id) image = Image() image.description = "Imported from Glance" image.name = image_details["name"] image.type = 'blank' image.save() logger.debug("All done") return HttpResponseRedirect('/images/%s' % image.id) context = {'error': "Could not connect to OpenStack"} return render(request, 'error.html', context) def error(request): context = {'error': "Unknown Error"} return render(request, 'error.html', context)
images/views.py
15,447
OpenStack specific action to get image details from Glance :param request: HTTPRequest :return: rendered HTML Creates a local db entry for the glance image Everything in Wistar depends on a db entry in the Images table If you have an existing openstack cluster, you may want to import those images here without having to physically copy the images to local disk :param request: HTTPRequest object :param glance_id: id of the glance image to import :return: redirect to /images/image_id DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER Copyright (c) 2015 Juniper Networks, Inc. All rights reserved. Use is subject to license terms. 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. template = get_object_or_404(ConfigTemplate, pk=template_id) template_form = ConfigTemplateForm(instance=template) if not osUtils.checkPath(image_form.cleaned_data['path']): logger.debug("PATH DOESN'T EXIST") context = {'error' : "PATH DOESNT EXIST"} return render(request, 'error.html', context) we need to convert this for KVM based deployments! lets replace the last "." with "_riot." if there is no '.', let's just add one if not osUtils.checkPath(image_form.cleaned_data['path']): logger.debug("PATH DOESN'T EXIST") context = {'error' : "PATH DOESNT EXIST"} return render(request, 'error.html', context) image_form.save() FIXME - make these variable names a bit more clear about their meaning we need to get the path of the image relative to the MEDIA_ROOT grab the file path of the domain image without the MEDIA_ROOT prepended
2,007
en
0.732573
# ############################################################################ # # Copyright (c) Microsoft Corporation. # # Available under the Microsoft PyKinect 1.0 Alpha license. See LICENSE.txt # for more information. # # ###########################################################################/ """defines the core data structures used for communicating w/ the Kinect APIs""" from __future__ import division from builtins import range from past.utils import old_div import ctypes from ctypes import Array from pykinect.nui import _NUIDLL from future.utils import with_metaclass NUI_SKELETON_COUNT = 6 class _EnumerationType(ctypes.c_int): """metaclass for an enumeration like type for ctypes""" def __new__(metacls, name, bases, dict): cls = ctypes.c_int.__new__(metacls, name, bases, dict) for key, value in list(cls.__dict__.items()): if key.startswith('_') and key.endswith('_'): continue setattr(cls, key, cls(key, value)) return cls class _Enumeration(ctypes.c_int): """base class for enumerations""" __metaclass__ = _EnumerationType def __init__(self, name, value): self.name = name ctypes.c_int.__init__(self, value) def __hash__(self): return self.value def __int__(self): return self.value def __index__(self): return self.value def __repr__(self): if hasattr(self, 'name'): return "<%s.%s (%r)>" % (self.__class__.__name__, self.name, self.value) name = '??' for x in type(self).__dict__: if x.startswith('_') and x.endswith('_'): continue if getattr(self, x, None) == self.value: name = x break return "<%s.%s (%r)>" % (self.__class__.__name__, name, self.value) def __eq__(self, other): if type(self) is not type(other): return self.value == other return self.value == other.value def __ne__(self, other): if type(self) is not type(other): return self.value != other return self.value != other.value class Vector(ctypes.Structure): """Represents vector data.""" _fields_ = [('x', ctypes.c_float), ('y', ctypes.c_float), ('z', ctypes.c_float), ('w', ctypes.c_float) ] def __init__(self, x = 0.0, y = 0.0, z = 0.0, w = 0.0): self.x = x self.y = y self.z = z self.w = w def __eq__(self, other): return (self.x == other.x and self.y == other.y and self.z == other.z and self.w == other.w) def __ne__(self, other): return not self.__eq__(other) def __repr__(self): return '<x=%r, y=%r, z=%r, w=%r>' % (self.x, self.y, self.z, self.w) class Matrix4(Array): """4x4 matrix. Can be accessed using matrix[0,0] ... matrix[3,3] or can be accessed using matrix.M11 ... matrix.M44 for similarity to .NET and the C data structures. matrix[0,1] is the same as matrix.M12. Used to provide bone rotation information. """ _length_ = 16 _type_ = ctypes.c_float def __getitem__(self, index): return Array.__getitem__(self, index[1] + index[0] * 4) def __setitem__(self, index, value): return Array.__setitem__(self, index[1] + index[0] * 4, value) def get_M11(self): return Array.__getitem__(0) def set_M11(self, value): Array.__setitem__(0, value) M11 = property(get_M11, set_M11) def get_M12(self): return Array.__getitem__(1) def set_M12(self, value): Array.__setitem__(1, value) M12 = property(get_M12, set_M12) def get_M13(self): return Array.__getitem__(2) def set_M13(self, value): Array.__setitem__(2, value) M13 = property(get_M13, set_M13) def get_M14(self): return Array.__getitem__(3) def set_M14(self, value): Array.__setitem__(3, value) M14 = property(get_M14, set_M14) def get_M21(self): return Array.__getitem__(4) def set_M21(self, value): Array.__setitem__(4, value) M21 = property(get_M21, set_M21) def get_M22(self): return Array.__getitem__(5) def set_M22(self, value): Array.__setitem__(5, value) M22 = property(get_M22, set_M22) def get_M23(self): return Array.__getitem__(6) def set_M23(self, value): Array.__setitem__(6, value) M23 = property(get_M23, set_M23) def get_M24(self): return Array.__getitem__(7) def set_M24(self, value): Array.__setitem__(7, value) M24 = property(get_M24, set_M24) def get_M31(self): return Array.__getitem__(8) def set_M31(self, value): Array.__setitem__(8, value) M31 = property(get_M31, set_M31) def get_M32(self): return Array.__getitem__(9) def set_M32(self, value): Array.__setitem__(9, value) M32 = property(get_M32, set_M32) def get_M33(self): return Array.__getitem__(10) def set_M33(self, value): Array.__setitem__(10, value) M33 = property(get_M33, set_M33) def get_M34(self): return Array.__getitem__(11) def set_M34(self, value): Array.__setitem__(11, value) M34 = property(get_M34, set_M34) def get_M41(self): return Array.__getitem__(12) def set_M41(self, value): Array.__setitem__(12, value) M41 = property(get_M41, set_M41) def get_M42(self): return Array.__getitem__(13) def set_M42(self, value): Array.__setitem__(13, value) M42 = property(get_M42, set_M42) def get_M43(self): return Array.__getitem__(14) def set_M43(self, value): Array.__setitem__(14, value) M43 = property(get_M43, set_M43) def get_M44(self): return Array.__getitem__(15) def set_M44(self, value): Array.__setitem__(15, value) M44 = property(get_M44, set_M44) class _NuiLockedRect(ctypes.Structure): _fields_ = [('pitch', ctypes.c_int32), ('size', ctypes.c_int32), ('bits', ctypes.c_voidp)] class _NuiSurfaceDesc(ctypes.Structure): _fields_ = [('width', ctypes.c_uint32), ('height', ctypes.c_uint32) ] class PlanarImage(ctypes.c_voidp): """Represents a video image.""" _BufferLen = ctypes.WINFUNCTYPE(ctypes.HRESULT, ctypes.c_int32)(3, 'BufferLen') _Pitch = ctypes.WINFUNCTYPE(ctypes.HRESULT, ctypes.c_int32)(4, 'Pitch') _LockRect = ctypes.WINFUNCTYPE(ctypes.HRESULT, ctypes.c_uint, ctypes.POINTER(_NuiLockedRect), ctypes.c_voidp, ctypes.c_uint32)(5, '_LockRect') _GetLevelDesc = ctypes.WINFUNCTYPE(ctypes.HRESULT, ctypes.c_uint32, ctypes.POINTER(_NuiSurfaceDesc))(6, '_GetLevelDesc') _UnlockRect = ctypes.WINFUNCTYPE(ctypes.HRESULT, ctypes.c_uint32)(7, '_UnlockRect') @property def width(self): desc = _NuiSurfaceDesc() PlanarImage._GetLevelDesc(self, 0, ctypes.byref(desc)) return desc.width @property def height(self): desc = _NuiSurfaceDesc() PlanarImage._GetLevelDesc(self, 0, ctypes.byref(desc)) return desc.height.value @property def bytes_per_pixel(self): return old_div(self.pitch, self.width) @property def bits(self): buffer = (ctypes.c_byte * self.buffer_length)() self.copy_bits(buffer) return buffer def copy_bits(self, dest): """copies the bits of the image to the provided destination address""" desc = _NuiSurfaceDesc() PlanarImage._GetLevelDesc(self, 0, ctypes.byref(desc)) rect = _NuiLockedRect() PlanarImage._LockRect(self, 0, ctypes.byref(rect), None, 0) ctypes.memmove(dest, rect.bits, desc.height * rect.pitch) PlanarImage._UnlockRect(self, 0) @property def buffer_length(self): return self.width * self.height * self.bytes_per_pixel @property def pitch(self): rect = _NuiLockedRect() PlanarImage._LockRect(self, 0, ctypes.byref(rect), None, 0) res = rect.pitch PlanarImage._UnlockRect(self, 0) return res class ImageType(_Enumeration): """Specifies an image type. """ depth_and_player_index = DepthAndPlayerIndex = 0 # USHORT color = Color = 1 # RGB32 data color_yuv = ColorYuv = 2 # YUY2 stream from camera h/w, but converted to RGB32 before user getting it. color_yuv_raw = ColorYuvRaw = 3 # YUY2 stream from camera h/w. depth = Depth = 4 # USHORT class ImageResolution(_Enumeration): """Specifies image resolution.""" invalid = Invalid = -1 resolution_80x60 = Resolution80x60 = 0 resolution_320x240 = Resolution320x240 = 1 resolution_640x480 = Resolution640x480 = 2 resolution_1280x1024 = Resolution1280x1024 = 3 # for hires color only class SkeletonTracking(_Enumeration): suppress_no_frame_data = 0x00000001 # Prevents NuiSkeletonGetNextFrame from returning E_NUI_FRAME_NO_DATA errors. Instead, calls to NuiSkeletonGetNextFrame block until data is available or the timeout period passes. title_sets_tracked_skeletons = 0x00000002 # Disables the default player selection mode and enables the title to manage which players have tracked skeletons. enable_seated_support = 0x00000004 # Uses seated skeleton tracking mode. The 10 lower-body joints of each skeleton will not be tracked. enable_in_near_range = 0x00000008 class ImageDigitalZoom(_Enumeration): """Specifies the zoom factor.""" zoom_1x = Zoom1x = 0 # A zoom factor of 1.0. zoom_2x = Zoom2x = 1 # A zoom factor of 2.0. class ImageViewArea(ctypes.Structure): """Specifies the image view area. """ _fields_ = [('Zoom', ctypes.c_int), # An ImageDigitalZoom value that specifies the zoom factor. ('CenterX', ctypes.c_long), # The horizontal offset from center, for panning. ('CenterY', ctypes.c_long) # The vertical offset from center, for panning. ] def get_zoom(self): return self.Zoom def set_zoom(self, value): self.Zoom = value zoom = property(get_zoom, set_zoom) def get_center_x(self): return self.CenterX def set_center_x(self, value): self.CenterX = value def get_center_y(self): return self.CenterY center_x = property(get_center_x, set_center_x) def set_center_y(self, value): self.CenterY = value center_y = property(get_center_y, set_center_y) class ImageFrame(ctypes.Structure): _fields_ = [('timestamp', ctypes.c_longlong), # The timestamp (in milliseconds) of the most recent frame. The clock starts when you call Initialize. ('frame_number', ctypes.c_uint32), # Returns the frame number ('type', ImageType), # An ImageType value that specifies the image type. ('resolution', ImageResolution), # An ImageResolution value that specifies the image resolution. ('image', PlanarImage), # A PlanarImage object that represents the image. ('flags', ctypes.c_uint32), # flags, not used ('view_area', ImageViewArea), # An ImageViewArea value that specifies the view area. ] class JointId(_Enumeration): """Specifies the various skeleton joints. """ hip_center = HipCenter = 0 spine = Spine = 1 shoulder_center = ShoulderCenter = 2 head = Head = 3 shoulder_left = ShoulderLeft = 4 elbow_left = ElbowLeft = 5 wrist_left = WristLeft = 6 hand_left = HandLeft = 7 shoulder_right = ShoulderRight = 8 elbow_right = ElbowRight = 9 wrist_right = WristRight = 10 hand_right = HandRight = 11 hip_left = HipLeft = 12 knee_left = KneeLeft = 13 ankle_left = AnkleLeft = 14 foot_left = FootLeft = 15 hip_right = HipRight = 16 knee_right = KneeRight = 17 ankle_right = AnkleRight = 18 foot_right = FootRight = 19 count = Count = 20 class SkeletonBoneRotation(ctypes.Structure): _fields_ = [('rotation_matrix', Matrix4), ('rotation_quaternion', Vector)] def __repr__(self): return '<SkeletonBoneRotation(%r, %r)>' % (self.rotation_matrix, self.rotation_quaternion) class SkeletonBoneOrientation(ctypes.Structure): _fields_ = [('end_joint', JointId), ('start_joint', JointId), ('hierarchical_rotation', SkeletonBoneRotation), ('absolute_rotation', SkeletonBoneRotation), ] def __repr__(self): return '<SkeletonBoneOrientation(%r, %r, %r, %r)>' % (self.end_joint, self.start_joint, self.hierarchical_rotation, self.absolute_rotation) class JointTrackingState(_Enumeration): """Specifies the joint tracking state. """ not_tracked = NOT_TRACKED = 0 inferred = INFERRED = 1 tracked = TRACKED = 2 class SkeletonTrackingState(_Enumeration): """Specifies a skeleton's tracking state.""" not_tracked = NOT_TRACKED = 0 position_only = POSITION_ONLY = 1 tracked = TRACKED = 2 class SkeletonFrameQuality(_Enumeration): """Specifies skeleton frame quality. """ camera_motion = CameraMotion = 0x01 extrapolated_floor = ExtrapolatedFloor = 0x02 upper_body_skeleton = UpperBodySkeleton = 0x04 seated_support_enabled = 0x08 class SkeletonQuality(_Enumeration): """Specifies how much of the skeleton is visible. """ clipped_right = ClippedRight = 0x00000001 clipped_left = ClippedLeft = 0x00000002 clipped_top = ClippedTop = 0x00000004 clipped_bottom = ClippedBottom = 0x00000008 NUI_SKELETON_POSITION_COUNT = 20 class SkeletonData(ctypes.Structure): """Contains data that characterizes a skeleton.""" _fields_ = [('eTrackingState', SkeletonTrackingState), ('dwTrackingID', ctypes.c_uint32), ('dwEnrollmentIndex', ctypes.c_uint32), ('dwUserIndex', ctypes.c_uint32), ('Position', Vector), ('SkeletonPositions', ctypes.ARRAY(Vector, NUI_SKELETON_POSITION_COUNT)), ('eSkeletonPositionTrackingState', ctypes.ARRAY(JointTrackingState, NUI_SKELETON_POSITION_COUNT)), ('Quality', SkeletonQuality), ] def get_tracking_state(self): return self.eTrackingState def set_tracking_state(self, value): self.eTrackingState = value tracking_state = property(get_tracking_state, set_tracking_state) def get_tracking_id(self): return self.dwTrackingID def set_tracking_id(self, value): self.dwTrackingID = value tracking_id = property(get_tracking_id, set_tracking_id) def get_enrollment_index(self): return self.dwEnrollmentIndex def set_enrollment_index(self, value): self.dwEnrollmentIndex = value enrollment_index = property(get_enrollment_index, set_enrollment_index) def get_user_index(self): return self.dwUserIndex def set_user_index(self, value): self.dwUserIndex = value user_index = property(get_user_index, set_user_index) def get_position(self): return self.Position def set_position(self, value): self.Position = value position = property(get_position, set_position) def get_skeleton_positions(self): return self.SkeletonPositions def set_skeleton_positions(self, value): self.SkeletonPositions = value skeleton_positions = property(get_skeleton_positions, set_skeleton_positions) def get_skeleton_position_tracking_states(self): return self.eSkeletonPositionTrackingState def set_skeleton_position_tracking_states(self, value): self.eSkeletonPositionTrackingState = value skeleton_position_tracking_states = property(get_skeleton_position_tracking_states, set_skeleton_position_tracking_states) def get_skeleton_quality(self): return self.Quality def set_skeleton_quality(self, value): self.Quality = value skeleton_quality = property(get_skeleton_quality, set_skeleton_quality) def calculate_bone_orientations(self): """Calculate bone orientations for a skeleton. The function calculates hierarchical and absolute joint angles for the skeleton, which can be used in animating an avatar (Avateering). The HipCenter joint is the root of the hierarchy, and describes an absolute rotation in the right-hand camera coordinate system. All other joints describe rotations relative to their parent joint orientation. The angles are returned in the same order as the joints are defined. Returns a sequence of SkeletonBoneOrientation objects.""" arr = (SkeletonBoneOrientation*JointId.Count)() _NuiSkeletonCalculateBoneOrientations(self, arr) return tuple(arr) def __repr__(self): return '<Tracking: %r, ID: %r, Position: %r>' % (self.eTrackingState, self.dwTrackingID, self.Position) def __eq__(self, other): if (self.tracking_state == other.tracking_state and self.tracking_id == other.tracking_id and self.enrollment_index == other.enrollment_index and self.user_index == other.user_index and self.position == other.position and self.skeleton_quality == other.skeleton_quality): for i in range(len(self.skeleton_positions)): if (self.skeleton_positions[i] != other.skeleton_positions[i] or self.skeleton_position_tracking_states[i] != other.skeleton_position_tracking_states[i]): return False return True return False def __ne__(self, other): return not self.__eq__(other) def __bool__(self): return self.tracking_state != SkeletonTrackingState.not_tracked _NuiSkeletonCalculateBoneOrientations = _NUIDLL.NuiSkeletonCalculateBoneOrientations _NuiSkeletonCalculateBoneOrientations.argtypes = [ctypes.POINTER(SkeletonData), ctypes.POINTER(SkeletonBoneOrientation)] _NuiSkeletonCalculateBoneOrientations.restype = ctypes.HRESULT class SkeletonFrame(ctypes.Structure): _pack_ = 16 _fields_ = [('liTimeStamp', ctypes.c_longlong), ('dwFrameNumber', ctypes.c_uint32), ('Quality', SkeletonFrameQuality), ('vFloorClipPlane', Vector), ('vNormalToGravity', Vector), ('SkeletonData', ctypes.ARRAY(SkeletonData, NUI_SKELETON_COUNT)), ] def get_timestamp(self): return self.liTimeStamp def set_timestamp(self, value): self.liTimeStamp = value timestamp = property(get_timestamp, set_timestamp) def get_frame_number(self): return self.dwFrameNumber def set_frame_number(self, value): self.dwFrameNumber = value frame_number = property(get_frame_number, set_frame_number) def get_quality(self): return self.Quality def set_quality(self, value): self.Quality = value quality = property(get_quality, set_quality) def get_floor_clip_plane(self): return self.vFloorClipPlane def set_floor_clip_plane(self, value): self.vFloorClipPlane = value floor_clip_plane = property(get_floor_clip_plane, set_floor_clip_plane) def get_normal_to_gravity(self): return self.vNormalToGravity def set_normal_to_gravity(self, value): self.vNormalToGravity = value normal_to_gravity = property(get_normal_to_gravity, set_normal_to_gravity) def get_skeleton_data(self): return self.SkeletonData def set_skeleton_data(self, value): self.SkeletonData = value skeleton_data = property(get_skeleton_data, set_skeleton_data) class TransformSmoothParameters(ctypes.Structure): """Contains transform smoothing parameters. """ _fields_ = [('fSmoothing', ctypes.c_float), ('fCorrection', ctypes.c_float), ('fPrediction', ctypes.c_float), ('fJitterRadius', ctypes.c_float), ('fMaxDeviationRadius', ctypes.c_float) ] def get_smoothing(self): return self.fSmoothing def set_smoothing(self, value): self.fSmoothing = value smoothing = property(get_smoothing, set_smoothing) def get_correction(self): return self.fCorrection def set_correction(self, value): self.fCorrection = value correction = property(get_correction, set_correction) def get_prediction(self): return self.fPrediction def set_prediction(self, value): self.fPrediction = value prediction = property(get_prediction, set_prediction) def get_jitter_radius(self): return self.fJitterRadius def set_jitter_radius(self, value): self.fJitterRadius = value jitter_radius = property(get_jitter_radius, set_jitter_radius) def get_max_deviation_radius(self): return self.fMaxDeviationRadius def set_max_deviation_radius(self, value): self.fMaxDeviationRadius = value max_deviation_radius = property(get_max_deviation_radius, set_max_deviation_radius)
pykinect/nui/structs.py
21,749
Specifies the zoom factor. Specifies image resolution. Specifies an image type. Specifies the image view area. Specifies the various skeleton joints. Specifies the joint tracking state. 4x4 matrix. Can be accessed using matrix[0,0] ... matrix[3,3] or can be accessed using matrix.M11 ... matrix.M44 for similarity to .NET and the C data structures. matrix[0,1] is the same as matrix.M12. Used to provide bone rotation information. Represents a video image. Contains data that characterizes a skeleton. Specifies skeleton frame quality. Specifies how much of the skeleton is visible. Specifies a skeleton's tracking state. Contains transform smoothing parameters. Represents vector data. base class for enumerations metaclass for an enumeration like type for ctypes Calculate bone orientations for a skeleton. The function calculates hierarchical and absolute joint angles for the skeleton, which can be used in animating an avatar (Avateering). The HipCenter joint is the root of the hierarchy, and describes an absolute rotation in the right-hand camera coordinate system. All other joints describe rotations relative to their parent joint orientation. The angles are returned in the same order as the joints are defined. Returns a sequence of SkeletonBoneOrientation objects. copies the bits of the image to the provided destination address defines the core data structures used for communicating w/ the Kinect APIs Copyright (c) Microsoft Corporation. Available under the Microsoft PyKinect 1.0 Alpha license. See LICENSE.txt for more information. / USHORT RGB32 data YUY2 stream from camera h/w, but converted to RGB32 before user getting it. YUY2 stream from camera h/w. USHORT for hires color only Prevents NuiSkeletonGetNextFrame from returning E_NUI_FRAME_NO_DATA errors. Instead, calls to NuiSkeletonGetNextFrame block until data is available or the timeout period passes. Disables the default player selection mode and enables the title to manage which players have tracked skeletons. Uses seated skeleton tracking mode. The 10 lower-body joints of each skeleton will not be tracked. A zoom factor of 1.0. A zoom factor of 2.0. An ImageDigitalZoom value that specifies the zoom factor. The horizontal offset from center, for panning. The vertical offset from center, for panning. The timestamp (in milliseconds) of the most recent frame. The clock starts when you call Initialize. Returns the frame number An ImageType value that specifies the image type. An ImageResolution value that specifies the image resolution. A PlanarImage object that represents the image. flags, not used An ImageViewArea value that specifies the view area.
2,668
en
0.768168
from sotd_indicators.indicators import * from arcgis.gis import GIS import configparser import time class Indicator: def __init__(self): # GIS Resources self.pem = None self.key = None self.username = None self.password = None self.portal = None self.debug = None #if GIS2 is specified self.pub_pem = None self.pub_key = None self.pub_username = None self.pub_password = None self.pub_portal = None self.pub_gis_conn = None # Selection Drivers self.grid_url = None self.feat_url = None # Positional Accuracy self.poac_sdf = None self.poac_url = None # Completeness self.cmpl_sdf = None self.cmpl_url = None # Logical Consistency self.logc_sdf = None self.logc_url = None # Temporal Currency self.curr_sdf = None self.curr_url = None # Thematic Accuracy self.them_sdf = None self.them_url = None # Source Lineage self.srln_sdf = None self.srln_url = None # Values Derived From Set Functions self.grid_sdf = None self.grid_wkid = None self.features = None self.selected = None def load_config(self, config_file): # Read Incoming Config File config = configparser.ConfigParser() config.read_file(open(config_file)) for section in config.sections(): print('Loading Section: {}'.format(section)) for k, v in dict(config.items(section)).items(): self.__setattr__(k, v) def set_gis(self): if self.pub_password!=None and self.pub_username!=None: self.pub_gis_conn = GIS(url=self.pub_portal, username=self.pub_username, password=self.pub_password) print((self.pub_gis_conn.users.me.role, self.pub_gis_conn.users.me.username)) else: self.pub_gis_conn = None if self.key != None and self.pem != None: import ssl ssl._create_default_https_context = ssl._create_unverified_context self.gis_conn = GIS(url=self.portal, key_file=self.key, cert_file=self.pem, verify_cert=False) print((self.gis_conn.users.me.role, self.gis_conn.users.me.username)) elif self.username != None and self.password != None: self.gis_conn = GIS(url=self.portal, username=self.username, password=self.password) print((self.gis_conn.users.me.role, self.gis_conn.users.me.username)) else: self.gis_conn = GIS() def set_grid_sdf(self, lb_days=1000, use_query=False): if not self.grid_url: raise Exception('Grid URL Not Set') else: if use_query: dates = get_dates_in_range(lb_days) grid_fl = FeatureLayer(url=self.grid_url, gis=self.gis_conn) self.grid_wkid = grid_fl.properties.extent.spatialReference.wkid self.grid_sdf = grid_fl.query(where=form_query_string(dates)).df else: grid_fl = FeatureLayer(url=self.grid_url, gis=self.gis_conn) self.grid_wkid = grid_fl.properties.extent.spatialReference.wkid self.grid_sdf = grid_fl.query(return_all_records=False).df def set_features(self): df_list = [] for idx, row in enumerate(self.grid_sdf.iterrows()): geom = Geometry(row[1].SHAPE) sp_filter = filters.intersects(geom, self.grid_wkid) data_fl = FeatureLayer(url=self.feat_url, gis=self.gis_conn) df_list.append( data_fl.query(geometry_filter=sp_filter, return_all_records=False).df ) self.features = df_list def set_selected(self, indicator): created = False out_sdf = None print(len(self.grid_sdf)) for idx, row in enumerate(self.grid_sdf.iterrows()): if not self.__getattribute__(indicator + '_url'): df_current = SpatialDataFrame( columns=field_schema.get(indicator), geometry=[Geometry(json.loads(row[1].SHAPE.JSON))] ) created = True else: # Negative Buffer to Select a Single Grid Cell sp_filter = filters.intersects( Geometry(row[1].SHAPE).buffer(-.1), self.grid_wkid ) data_fl = FeatureLayer( url=self.__getattribute__(indicator + '_url'), gis=self.gis_conn ) df_current = data_fl.query(geometry_filter=sp_filter, return_all_records=False).df if idx == 0: out_sdf = df_current else: #out_sdf.merge(df_current) out_sdf = out_sdf.merge_datasets(df_current) #out_sdf = out_sdf.append(df_current) self.selected = out_sdf.reset_index(drop=False) print("Selected: " + str(len(out_sdf))) return created def create_layer(self, df, title): print('Creating New Hosted Feature Layer: {}'.format(title)) if self.pub_gis_conn==None: new_layer = df.to_featurelayer( title, gis=self.gis_conn ) else: new_layer = df.to_featurelayer( title, gis=self.pub_gis_conn ) return new_layer.id def update_layer(self, df, url): feat_layer = FeatureLayer(url=url, gis=self.gis_conn) res = feat_layer.edit_features(updates=df.to_featureset()) if 'updateResults' not in res: raise Exception('Edit Features Returned Issues: {}'.format(res)) else: return res['updateResults'] def run_poac(self, p1, apply_edits=True): try: new_flag = self.set_selected('poac') df = positional_accuracy( self.selected, self.features, p1 ) if self.debug: df.to_featureclass(self.debug, 'poac', overwrite=True) return df if new_flag: print(df.to_featureclass) return [ df, self.create_layer( df, 'Positional Accuracy {}'.format(round(time.time())) ) ] else: if apply_edits: return [ df, self.update_layer( df, self.poac_url ) ] else: return df except Exception as e: print('Exception Running Positional Accuracy: {}'.format(str(e))) def run_cmpl(self, comparison_sdf, apply_edits=True): try: new_flag = self.set_selected('cmpl') df = completeness( self.selected, self.features, comparison_sdf ) if self.debug: df.to_featureclass(self.debug, 'cmpl', overwrite=True) return df if new_flag: return [ df, self.create_layer( df, 'Completeness {}'.format(round(time.time())) ) ] else: if apply_edits: return [ df, self.update_layer( df, self.cmpl_url ) ] else: return df except Exception as e: print('Exception Running Completeness: {}'.format(str(e))) def run_curr(self, p1, date='1901-1-1', apply_edits=True): try: new_flag = self.set_selected('curr') df = temporal_currency( self.selected, self.features, p1, date ) if self.debug: df.to_featureclass(self.debug, 'curr', overwrite=True) return df if new_flag: print(df) return [ df, self.create_layer( df, 'Temporal Currency {}'.format(round(time.time())) ) ] else: if apply_edits: return [ df, self.update_layer( df, self.curr_url ) ] else: return df except Exception as e: print('Exception Running Temporal Currency: {}'.format(str(e))) def run_them(self, p1, apply_edits=True): try: new_flag = self.set_selected('them') df = thematic_accuracy( self.selected, self.features, p1 ) if new_flag: return [ df, self.create_layer( df, 'Thematic Accuracy {}'.format(round(time.time())) ) ] else: if apply_edits: return [ df, self.update_layer( df, self.them_url ) ] else: return df except Exception as e: print('Exception Running Thematic Accuracy: {}'.format(str(e))) def run_srln(self, p1, p2=None, search_value=1001, apply_edits=True): try: new_flag = self.set_selected('srln') df = source_lineage( self.selected, self.features, p1, p2, search_value ) if self.debug: df.to_featureclass(self.debug, 'srln', overwrite=True) return df if new_flag: return [ df, self.create_layer( df, 'Source Lineage {}'.format(round(time.time())) ) ] else: if apply_edits: return [ df, self.update_layer( df, self.srln_url ) ] else: return df except Exception as e: print('Exception Running Source Lineage: {}'.format(str(e))) def run_logc(self, p1, p2, p3, p4, apply_edits=True): try: new_flag = self.set_selected('logc') df = logical_consistency( self.selected, self.features, self.feat_url, p1, p2, p3, p4 ) if new_flag: return [ df, self.create_layer( df, 'Logical Consistency {}'.format(round(time.time())) ) ] else: if apply_edits: return [ df, self.update_layer( df, self.logc_url ) ] else: return df except Exception as e: print('Exception Running Source Lineage: {}'.format(str(e)))
state-of-the-data-for-webgis/sotd_indicators/Indicator.py
12,524
GIS Resourcesif GIS2 is specified Selection Drivers Positional Accuracy Completeness Logical Consistency Temporal Currency Thematic Accuracy Source Lineage Values Derived From Set Functions Read Incoming Config File Negative Buffer to Select a Single Grid Cellout_sdf.merge(df_current)out_sdf = out_sdf.append(df_current)
321
en
0.698819
# Generated by Django 3.1.2 on 2020-10-18 02:42 from django.db import migrations class Migration(migrations.Migration): dependencies = [ ('rosters', '0035_auto_20200827_0124'), ] operations = [ migrations.AlterUniqueTogether( name='daygroupday', unique_together={('daygroup', 'day')}, ), ]
rosters/migrations/0036_unique_together_daygroupday.py
359
Generated by Django 3.1.2 on 2020-10-18 02:42
45
en
0.690817
# -*- coding: utf-8 -*- from datetime import datetime import pytest from AIPscan.conftest import AIP_1_CREATION_DATE, AIP_2_CREATION_DATE from AIPscan.conftest import ORIGINAL_FILE_SIZE as JPEG_1_01_FILE_SIZE from AIPscan.conftest import PRESERVATION_FILE_SIZE as JPEG_1_02_FILE_SIZE from AIPscan.Data import fields, report_data from AIPscan.Data.tests import ( MOCK_STORAGE_SERVICE, MOCK_STORAGE_SERVICE_ID, MOCK_STORAGE_SERVICE_NAME, ) from AIPscan.helpers import parse_datetime_bound from AIPscan.test_helpers import create_test_storage_location TOTAL_FILE_SIZE = JPEG_1_01_FILE_SIZE + JPEG_1_02_FILE_SIZE DATE_BEFORE_AIP_1 = "2019-01-01" DATE_AFTER_AIP_1 = "2020-01-02" DATE_BEFORE_AIP_2 = "2020-05-30" DATE_AFTER_AIP_2 = "2020-06-02" class MockFormatsCountQueryResult: """Fixture for mocking SQLAlchemy query results.""" def __init__(self, file_format, file_count, total_size): self.file_format = file_format self.file_count = file_count self.total_size = total_size MOCK_QUERY_RESULTS = [ MockFormatsCountQueryResult(file_format="JPEG", file_count=5, total_size=12345678), MockFormatsCountQueryResult(file_format="CSV", file_count=3, total_size=123456), MockFormatsCountQueryResult( file_format="MPEG-4 Media File", file_count=1, total_size=12345 ), ] @pytest.mark.parametrize( "query_results, results_count", [ # Empty result set, count is 0. ([], 0), # Test the return of complete result set, count is the length # of all results. (MOCK_QUERY_RESULTS, len(MOCK_QUERY_RESULTS)), # Test the return of only the first two results, count is 2. (MOCK_QUERY_RESULTS[:2], 2), ], ) def test_formats_count(app_instance, mocker, query_results, results_count): """Test that results match high-level expectations.""" query = mocker.patch("AIPscan.Data.report_data._formats_count_query") query.return_value = query_results get_ss = mocker.patch("AIPscan.Data._get_storage_service") get_ss.return_value = MOCK_STORAGE_SERVICE test_location = create_test_storage_location() get_location = mocker.patch("AIPscan.Data._get_storage_location") get_location.return_value = test_location report = report_data.formats_count( storage_service_id=MOCK_STORAGE_SERVICE_ID, start_date=datetime.min, end_date=datetime.max, storage_location_id=test_location.id, ) assert report[fields.FIELD_STORAGE_NAME] == MOCK_STORAGE_SERVICE_NAME assert report[fields.FIELD_STORAGE_LOCATION] == test_location.description assert len(report[fields.FIELD_FORMATS]) == results_count @pytest.mark.parametrize( "test_format", [mock_result for mock_result in MOCK_QUERY_RESULTS] ) def test_formats_count_elements(app_instance, mocker, test_format): """Test that structure of versions data matches expectations.""" mock_query = mocker.patch("AIPscan.Data.report_data._formats_count_query") mock_query.return_value = [test_format] mock_get_ss_name = mocker.patch("AIPscan.Data._get_storage_service") mock_get_ss_name.return_value = MOCK_STORAGE_SERVICE report = report_data.formats_count( MOCK_STORAGE_SERVICE_ID, datetime.min, datetime.max ) report_format = report[fields.FIELD_FORMATS][0] assert test_format.file_format == report_format.get(fields.FIELD_FORMAT) assert test_format.file_count == report_format.get(fields.FIELD_COUNT) assert test_format.total_size == report_format.get(fields.FIELD_SIZE) @pytest.mark.parametrize( "start_date, end_date, format_count, total_file_count, total_file_size", [ # Not specifying dates should return all files and versions. (None, None, 2, 3, TOTAL_FILE_SIZE), # Start date before first AIP was ingested hould return all # files and versions. (DATE_BEFORE_AIP_1, None, 2, 3, TOTAL_FILE_SIZE), # Start date that's the same day our first AIP was ingested # should return all files and versions. (AIP_1_CREATION_DATE, None, 2, 3, TOTAL_FILE_SIZE), # Start date after our first AIP was ingested should return # only the second JPEG version and ISO disk image. (DATE_AFTER_AIP_1, None, 2, 2, JPEG_1_02_FILE_SIZE), # End date before second AIP was ingested should return only # the first JPEG version. (None, DATE_BEFORE_AIP_2, 1, 1, JPEG_1_01_FILE_SIZE), # End date that's the same day our second AIP was ingested # should return all files and versions. (None, AIP_2_CREATION_DATE, 2, 3, TOTAL_FILE_SIZE), # End date that's after our second AIP was ingested should # return all files and versions. (None, DATE_AFTER_AIP_2, 2, 3, TOTAL_FILE_SIZE), # Start and end dates that define a range in which we haven't # ingested any AIPs should return no files or versions. ("2019-01-01", "2019-01-02", 0, 0, 0), # Invalid values for start and end dates should be treated as # None values and return both JPEG versions. (True, "NOT A DATE", 2, 3, TOTAL_FILE_SIZE), ], ) def test_formats_count_contents( app_with_populated_format_versions, start_date, end_date, format_count, total_file_count, total_file_size, ): """Test that content of response matches expectations. This integration test uses a pre-populated fixture to verify that the database access layer of our endpoint returns what we expect. """ results = report_data.formats_count( storage_service_id=1, start_date=parse_datetime_bound(start_date), end_date=parse_datetime_bound(end_date, upper=True), ) formats = results[fields.FIELD_FORMATS] assert len(formats) == format_count assert ( sum(format_.get(fields.FIELD_COUNT, 0) for format_ in formats) == total_file_count ) assert ( sum(format_.get(fields.FIELD_SIZE, 0) for format_ in formats) == total_file_size )
AIPscan/Data/tests/test_formats_count.py
6,049
Fixture for mocking SQLAlchemy query results. Test that results match high-level expectations. Test that content of response matches expectations. This integration test uses a pre-populated fixture to verify that the database access layer of our endpoint returns what we expect. Test that structure of versions data matches expectations. -*- coding: utf-8 -*- Empty result set, count is 0. Test the return of complete result set, count is the length of all results. Test the return of only the first two results, count is 2. Not specifying dates should return all files and versions. Start date before first AIP was ingested hould return all files and versions. Start date that's the same day our first AIP was ingested should return all files and versions. Start date after our first AIP was ingested should return only the second JPEG version and ISO disk image. End date before second AIP was ingested should return only the first JPEG version. End date that's the same day our second AIP was ingested should return all files and versions. End date that's after our second AIP was ingested should return all files and versions. Start and end dates that define a range in which we haven't ingested any AIPs should return no files or versions. Invalid values for start and end dates should be treated as None values and return both JPEG versions.
1,350
en
0.87781
import platform import json import subprocess import os from urllib2 import urlopen from lbryschema import __version__ as lbryschema_version from lbryum import __version__ as LBRYUM_VERSION from lbrynet import build_type, __version__ as lbrynet_version from lbrynet.conf import ROOT_DIR def get_lbrynet_version(): if build_type.BUILD == "dev": try: with open(os.devnull, 'w') as devnull: git_dir = ROOT_DIR + '/.git' return subprocess.check_output( ['git', '--git-dir='+git_dir, 'describe', '--dirty', '--always'], stderr=devnull ).strip().lstrip('v') except (subprocess.CalledProcessError, OSError): print "failed to get version from git" return lbrynet_version def get_platform(get_ip=True): p = { "processor": platform.processor(), "python_version": platform.python_version(), "platform": platform.platform(), "os_release": platform.release(), "os_system": platform.system(), "lbrynet_version": get_lbrynet_version(), "lbryum_version": LBRYUM_VERSION, "lbryschema_version": lbryschema_version, "build": build_type.BUILD, # CI server sets this during build step } if get_ip: try: p['ip'] = json.load(urlopen('http://jsonip.com'))['ip'] except: p['ip'] = "Could not determine IP" return p
lbrynet/core/system_info.py
1,462
CI server sets this during build step
37
en
0.950928
import gflags import logging import threading import time FLAGS = gflags.FLAGS gflags.DEFINE_integer("probe_frequency_secs", 10*60, "How often to probe the logs for updates") from ct.client import log_client from ct.client import monitor from ct.client import state from ct.crypto import merkle from ct.crypto import verify from twisted.internet import reactor from twisted.web import client as twisted_client class ProberThread(threading.Thread): """A prober for scheduled updating of the log view.""" def __init__(self, ct_logs, db, cert_db, temp_db_factory, monitor_state_dir, agent=None, state_keeper_class=None): """Initialize from a CtLogs proto.""" threading.Thread.__init__(self) self.__monitors = [] self.__db = db if not agent: agent = twisted_client.Agent(reactor) if not state_keeper_class: state_keeper_class = state.StateKeeper for log in ct_logs.ctlog: if not log.log_server or not log.log_id or not log.public_key_info: raise RuntimeError("Cannot start monitor: log proto has " "missing or empty fields: %s" % log) temp_db = temp_db_factory.create_storage(log.log_server) client = log_client.AsyncLogClient(agent, log.log_server, temp_db) hasher = merkle.TreeHasher() verifier = verify.LogVerifier(log.public_key_info, merkle.MerkleVerifier(hasher)) # Convert from standard Base64 to URL-safe Base64 so that the log ID # can be used as part of a file path. log_id_urlsafe = log.log_id.replace('/', '_').replace('+', '-') state_keeper = state_keeper_class(monitor_state_dir + "/" + log_id_urlsafe) log_key = db.get_log_id(log.log_server) self.__monitors.append(monitor.Monitor(client, verifier, hasher, db, cert_db, log_key, state_keeper)) self.__last_update_start_time = 0 self.__stopped = False self.__called_later = None def __repr__(self): return "%r(%r)" % (self.__class__.__name__, self.__monitors) def __str__(self): return "%s(%s)" % (self.__class__.__name__, self.__monitors) def _log_probed_callback(self, success, monitor): if success: logging.info("Data for %s updated: latest timestamp is %s" % (monitor.servername, time.strftime("%c", time.localtime( monitor.data_timestamp/1000)))) else: logging.error("Failed to update data for %s: latest timestamp " "is %s" % (monitor.servername, time.strftime("%c", time.localtime( monitor.data_timestamp/1000)))) self.__probed += 1 if self.__probed == len(self.__monitors): self._all_logs_probed() def _all_logs_probed(self): logging.info("Probe loop completed in %d seconds" % (time.time() - self.__start_time)) sleep_time = max(0, self.__start_time + FLAGS.probe_frequency_secs - time.time()) logging.info("Next probe loop in: %d seconds" % sleep_time) self.__called_later = reactor.callLater(sleep_time, self.probe_all_logs) def _has_outstanding_call_later(self): return self.__called_later and self.__called_later.active() def probe_all_logs(self): logging.info("Starting probe loop...") self.__called_later = None self.__start_time = time.time() self.__probed = 0 """Loop through all logs in the list and check for updates.""" if self.__monitors: for monitor_ in self.__monitors: monitor_result = monitor_.update() monitor_result.addCallback(self._log_probed_callback, monitor_) # TODO(hadfieldp): do we need an errback too? else: # If we're configured with no monitors we still need to behave # correctly in order for the reactor to be stoppable. self._all_logs_probed() logging.info("Done starting probe loop.") def run(self): logging.info("Running reactor...") self.__called_later = reactor.callLater(0, self.probe_all_logs) reactor.run(installSignalHandlers=0) logging.info("Reactor no longer running.") def stop(self): logging.info("Stopping reactor...") if self._has_outstanding_call_later(): self.__called_later.cancel() self.__called_later = None reactor.stop() logging.info("Reactor stopped.")
vendor/github.com/google/certificate-transparency/python/ct/client/prober.py
5,076
A prober for scheduled updating of the log view. Initialize from a CtLogs proto. Convert from standard Base64 to URL-safe Base64 so that the log ID can be used as part of a file path. TODO(hadfieldp): do we need an errback too? If we're configured with no monitors we still need to behave correctly in order for the reactor to be stoppable.
342
en
0.863501
#!/usr/bin/python # -*- coding: utf-8 -*- from api.tests.base import BaseTest class IndexTest(BaseTest): def test_index(self): # Expected result from server. expected_result = "Hello Flask Restful Example!" # Send request to index. response = self.client.get("/") # This raises an AssertionError assert response.status_code == 200 # This raises an AssertionError assert expected_result == response.json
api/tests/tests_index.py
478
!/usr/bin/python -*- coding: utf-8 -*- Expected result from server. Send request to index. This raises an AssertionError This raises an AssertionError
150
en
0.738247
# Generated by Django 3.0.10 on 2021-02-01 18:38 from django.db import migrations, models class Migration(migrations.Migration): dependencies = [ ('core', '0054_auto_20210201_1832'), ] operations = [ migrations.AlterField( model_name='bcmactivity', name='MTPD', field=models.PositiveSmallIntegerField(choices=[(1, '4 godz.'), (2, 'dzień'), (3, '2 dni'), (4, 'tydzień'), (5, '2 tygodnie'), (6, 'miesiąc'), (7, '2 miesiące'), (8, 'do odwołania')]), ), migrations.AlterField( model_name='bcmactivity', name='TTN', field=models.PositiveSmallIntegerField(blank=True, choices=[(1, '4 godz.'), (2, 'dzień'), (3, '2 dni'), (4, 'tydzień'), (5, '2 tygodnie'), (6, 'miesiąc'), (7, '2 miesiące'), (8, 'do odwołania')]), ), migrations.AlterField( model_name='equipment', name='over_MTPD', field=models.PositiveSmallIntegerField(choices=[('', '------'), (1, '4 godz.'), (2, 'dzień'), (3, '2 dni'), (4, 'tydzień'), (5, '2 tygodnie'), (6, 'miesiąc'), (7, '2 miesiące'), (8, 'do odwołania')], default=None, null=True), ), migrations.AlterField( model_name='information', name='over_MTPD', field=models.PositiveSmallIntegerField(choices=[('', '------'), (1, '4 godz.'), (2, 'dzień'), (3, '2 dni'), (4, 'tydzień'), (5, '2 tygodnie'), (6, 'miesiąc'), (7, '2 miesiące'), (8, 'do odwołania')], default=None, null=True), ), migrations.AlterField( model_name='location', name='over_MTPD', field=models.PositiveSmallIntegerField(choices=[('', '------'), (1, '4 godz.'), (2, 'dzień'), (3, '2 dni'), (4, 'tydzień'), (5, '2 tygodnie'), (6, 'miesiąc'), (7, '2 miesiące'), (8, 'do odwołania')], default=None, null=True), ), migrations.AlterField( model_name='position', name='over_MTPD', field=models.PositiveSmallIntegerField(choices=[('', '------'), (1, '4 godz.'), (2, 'dzień'), (3, '2 dni'), (4, 'tydzień'), (5, '2 tygodnie'), (6, 'miesiąc'), (7, '2 miesiące'), (8, 'do odwołania')], default=None, null=True), ), migrations.AlterField( model_name='supplies', name='over_MTPD', field=models.PositiveSmallIntegerField(choices=[('', '------'), (1, '4 godz.'), (2, 'dzień'), (3, '2 dni'), (4, 'tydzień'), (5, '2 tygodnie'), (6, 'miesiąc'), (7, '2 miesiące'), (8, 'do odwołania')], default=None, null=True), ), ]
app/core/migrations/0055_auto_20210201_1838.py
2,610
Generated by Django 3.0.10 on 2021-02-01 18:38
46
en
0.691342
#!/usr/bin/env python # -*- coding: utf8 -*- import sys from xml.etree import ElementTree from xml.etree.ElementTree import Element, SubElement from lxml import etree import codecs from libs.constants import DEFAULT_ENCODING from libs.ustr import ustr XML_EXT = '.xml' ENCODE_METHOD = DEFAULT_ENCODING class PascalVocWriter: def __init__(self, foldername, filename, imgSize,databaseSrc='Unknown', localImgPath=None): self.foldername = foldername self.filename = filename self.databaseSrc = databaseSrc self.imgSize = imgSize self.boxlist = [] self.localImgPath = localImgPath self.verified = False def convertPoints2BndBox(self, QPoints): points=[(p.x(), p.y()) for p in QPoints] xmin = float('inf') ymin = float('inf') xmax = float('-inf') ymax = float('-inf') for p in points: x = p[0] y = p[1] xmin = min(x, xmin) ymin = min(y, ymin) xmax = max(x, xmax) ymax = max(y, ymax) # Martin Kersner, 2015/11/12 # 0-valued coordinates of BB caused an error while # training faster-rcnn object detector. if xmin < 1: xmin = 1 if ymin < 1: ymin = 1 return (int(xmin), int(ymin), int(xmax), int(ymax)) def prettify(self, elem): """ Return a pretty-printed XML string for the Element. """ rough_string = ElementTree.tostring(elem, 'utf8') root = etree.fromstring(rough_string) return etree.tostring(root, pretty_print=True, encoding=ENCODE_METHOD).replace(" ".encode(), "\t".encode()) # minidom does not support UTF-8 '''reparsed = minidom.parseString(rough_string) return reparsed.toprettyxml(indent="\t", encoding=ENCODE_METHOD)''' def genXML(self): """ Return XML root """ # Check conditions if self.filename is None or \ self.foldername is None or \ self.imgSize is None: return None top = Element('annotation') if self.verified: top.set('verified', 'yes') folder = SubElement(top, 'data_set') folder.text = self.foldername # filename = SubElement(top, 'filename') # filename.text = self.filename # if self.localImgPath is not None: # localImgPath = SubElement(top, 'path') # localImgPath.text = self.localImgPath # source = SubElement(top, 'source') # database = SubElement(source, 'database') # database.text = self.databaseSrc size_part = SubElement(top, 'size') width = SubElement(size_part, 'width') height = SubElement(size_part, 'height') depth = SubElement(size_part, 'depth') width.text = str(self.imgSize[1]) height.text = str(self.imgSize[0]) if len(self.imgSize) == 3: depth.text = str(self.imgSize[2]) else: depth.text = '1' # segmented = SubElement(top, 'segmented') # segmented.text = '0' return top def addBndBox(self, xmin, ymin, xmax, ymax, name, difficult, parents, children, self_id): bndbox = {'xmin': xmin, 'ymin': ymin, 'xmax': xmax, 'ymax': ymax} bndbox['name'] = name bndbox['difficult'] = difficult bndbox['parents'] = parents bndbox['children'] = children bndbox['self_id'] = self_id self.boxlist.append(bndbox) def addBehavior(self, label, self_id, start_frame, end_frame, shapes=None): bndbox = {} bndbox['behavior'] = label bndbox['self_id'] = self_id bndbox['start_frame'] = start_frame bndbox['end_frame'] = end_frame bndbox['shapes'] = shapes self.boxlist.append(bndbox) def appendObjects(self, top): for each_behavior in self.boxlist: object_item = SubElement(top, 'behaviors') object_id = SubElement(object_item, 'behavior_id') object_id.text = str(each_behavior['self_id']) name = SubElement(object_item, 'behavior') name.text = str(each_behavior['behavior']) start = SubElement(object_item, 'start_frame') start.text = str(each_behavior['start_frame']) if start.text == "": start.text = "undefined" end = SubElement(object_item, 'end_frame') end.text = str(each_behavior['end_frame']) if end.text == "": end.text = "undefined" if each_behavior['shapes'] != None: shapes = SubElement(object_item, 'bounding_boxes') for each_shape in each_behavior['shapes']: bounding_box = self.convertPoints2BndBox(each_shape.points) shape = SubElement(shapes, 'bounding_box') frame = SubElement(shape, 'frame') frame.text = str(each_shape.filename) bndbox = SubElement(shape, 'bndbox') xmin = SubElement(bndbox, 'xmin') xmin.text = str(bounding_box[0]) ymin = SubElement(bndbox, 'ymin') ymin.text = str(bounding_box[1]) xmax = SubElement(bndbox, 'xmax') xmax.text = str(bounding_box[2]) ymax = SubElement(bndbox, 'ymax') ymax.text = str(bounding_box[3]) # all_ids = [] # for each_object in self.boxlist: # all_ids.append(each_object['self_id']) # for each_object in self.boxlist: # object_item = SubElement(top, 'object') # object_id = SubElement(object_item, 'object_id') # object_id.text = str(each_object['self_id']) # name = SubElement(object_item, 'name') # real_name = ustr(each_object['name']) # name.text = str() # for letter in real_name: # if letter != ' ': # name.text += letter # else: # name.text = str() # if len(each_object['parents']) != 0: # parents = SubElement(object_item, 'has_parents') # for each_id in each_object['parents']: # if each_id in all_ids: # parent = SubElement(parents, 'parent') # parent.text = str(each_id) # if len(each_object['children']) != 0: # children = SubElement(object_item, 'has_children') # for each_id in each_object['children']: # if each_id in all_ids: # child = SubElement(children, 'child') # child.text = str(each_id) # pose = SubElement(object_item, 'pose') # pose.text = "Unspecified" # truncated = SubElement(object_item, 'truncated') # if int(float(each_object['ymax'])) == int(float(self.imgSize[0])) or (int(float(each_object['ymin']))== 1): # truncated.text = "1" # max == height or min # elif (int(float(each_object['xmax']))==int(float(self.imgSize[1]))) or (int(float(each_object['xmin']))== 1): # truncated.text = "1" # max == width or min # else: # truncated.text = "0" # difficult = SubElement(object_item, 'difficult') # difficult.text = str( bool(each_object['difficult']) & 1 ) # bndbox = SubElement(object_item, 'bndbox') # xmin = SubElement(bndbox, 'xmin') # xmin.text = str(each_object['xmin']) # ymin = SubElement(bndbox, 'ymin') # ymin.text = str(each_object['ymin']) # xmax = SubElement(bndbox, 'xmax') # xmax.text = str(each_object['xmax']) # ymax = SubElement(bndbox, 'ymax') # ymax.text = str(each_object['ymax']) def save(self, targetFile=None): root = self.genXML() self.appendObjects(root) out_file = None if targetFile is None: out_file = codecs.open( self.filename + XML_EXT, 'w', encoding=ENCODE_METHOD) else: out_file = codecs.open(targetFile, 'w', encoding=ENCODE_METHOD) prettifyResult = self.prettify(root) out_file.write(prettifyResult.decode('utf8')) out_file.close() class PascalVocReader: def __init__(self, filepath): # shapes type: # [labbel, [(x1,y1), (x2,y2), (x3,y3), (x4,y4)], color, color, difficult] self.shapes = [] self.filepath = filepath self.behaviors = [] self.verified = False try: self.readBehavior() except: pass def getShapes(self): return self.shapes def getBehaviors(self): return self.behaviors def addBehavior(self, behavior_name, behavior_id, starting_frame, ending_frame, shapes=None): self.behaviors.append((behavior_name, behavior_id, starting_frame, ending_frame, shapes)) def addShape(self, label, bndbox, difficult, parents, children, object_id): parent_ids = [] try: for item in parents.findall('parent'): parent_ids.append(int(item.text)) except: pass child_ids = [] try: for item in children.findall('child'): child_ids.append(int(item.text)) except: pass xmin = int(float(bndbox.find('xmin').text)) ymin = int(float(bndbox.find('ymin').text)) xmax = int(float(bndbox.find('xmax').text)) ymax = int(float(bndbox.find('ymax').text)) points = [(xmin, ymin), (xmax, ymin), (xmax, ymax), (xmin, ymax)] self.shapes.append((label, points, parent_ids, child_ids, object_id, None, None, difficult, )) def readBehavior(self): assert self.filepath.endswith(XML_EXT), "Unsupport file format" parser = etree.XMLParser(encoding=ENCODE_METHOD) xmltree = ElementTree.parse(self.filepath, parser=parser).getroot() for object_iter in xmltree.findall('behaviors'): label = object_iter.find('behavior').text object_id = int(object_iter.find('behavior_id').text) start_frame = object_iter.find('start_frame').text end_frame = object_iter.find('end_frame').text shapes = object_iter.find('bounding_boxes') bounding_boxes = [] for shape_tier in shapes.findall('bounding_box'): box = {} bndbox = shape_tier.find("bndbox") box["bndbox"] = bndbox frame = shape_tier.find("frame") box["frame"] = frame.text bounding_boxes.append(box) self.addBehavior(label, object_id, start_frame, end_frame, bounding_boxes) return True def parseXML(self): assert self.filepath.endswith(XML_EXT), "Unsupport file format" parser = etree.XMLParser(encoding=ENCODE_METHOD) xmltree = ElementTree.parse(self.filepath, parser=parser).getroot() filename = xmltree.find('filename').text try: verified = xmltree.attrib['verified'] if verified == 'yes': self.verified = True except KeyError: self.verified = False for object_iter in xmltree.findall('object'): bndbox = object_iter.find("bndbox") label = object_iter.find('name').text parents = object_iter.find('has_parents') children = object_iter.find('has_children') object_id = int(object_iter.find('object_id').text) # Add chris difficult = False if object_iter.find('difficult') is not None: difficult = bool(int(object_iter.find('difficult').text)) self.addShape(label, bndbox, difficult, parents, children, object_id) return True
libs/pascal_voc_io.py
12,168
Return XML root Return a pretty-printed XML string for the Element. !/usr/bin/env python -*- coding: utf8 -*- Martin Kersner, 2015/11/12 0-valued coordinates of BB caused an error while training faster-rcnn object detector. minidom does not support UTF-8 Check conditions filename = SubElement(top, 'filename') filename.text = self.filename if self.localImgPath is not None: localImgPath = SubElement(top, 'path') localImgPath.text = self.localImgPath source = SubElement(top, 'source') database = SubElement(source, 'database') database.text = self.databaseSrc segmented = SubElement(top, 'segmented') segmented.text = '0' all_ids = [] for each_object in self.boxlist: all_ids.append(each_object['self_id']) for each_object in self.boxlist: object_item = SubElement(top, 'object') object_id = SubElement(object_item, 'object_id') object_id.text = str(each_object['self_id']) name = SubElement(object_item, 'name') real_name = ustr(each_object['name']) name.text = str() for letter in real_name: if letter != ' ': name.text += letter else: name.text = str() if len(each_object['parents']) != 0: parents = SubElement(object_item, 'has_parents') for each_id in each_object['parents']: if each_id in all_ids: parent = SubElement(parents, 'parent') parent.text = str(each_id) if len(each_object['children']) != 0: children = SubElement(object_item, 'has_children') for each_id in each_object['children']: if each_id in all_ids: child = SubElement(children, 'child') child.text = str(each_id) pose = SubElement(object_item, 'pose') pose.text = "Unspecified" truncated = SubElement(object_item, 'truncated') if int(float(each_object['ymax'])) == int(float(self.imgSize[0])) or (int(float(each_object['ymin']))== 1): truncated.text = "1" max == height or min elif (int(float(each_object['xmax']))==int(float(self.imgSize[1]))) or (int(float(each_object['xmin']))== 1): truncated.text = "1" max == width or min else: truncated.text = "0" difficult = SubElement(object_item, 'difficult') difficult.text = str( bool(each_object['difficult']) & 1 ) bndbox = SubElement(object_item, 'bndbox') xmin = SubElement(bndbox, 'xmin') xmin.text = str(each_object['xmin']) ymin = SubElement(bndbox, 'ymin') ymin.text = str(each_object['ymin']) xmax = SubElement(bndbox, 'xmax') xmax.text = str(each_object['xmax']) ymax = SubElement(bndbox, 'ymax') ymax.text = str(each_object['ymax']) shapes type: [labbel, [(x1,y1), (x2,y2), (x3,y3), (x4,y4)], color, color, difficult] Add chris
2,762
en
0.405954
""" Description: This file implements a wrapper around the original SLCT code in C Author: LogPAI team License: MIT """ import sys sys.path.append('../') import hashlib import pandas as pd import re from datetime import datetime from ..logmatch import regexmatch import subprocess import os import logging logger = logging.getLogger(__name__) class LogParser(object): def __init__(self, indir, outdir, log_format, support, para_j=True, saveLog=False, rex=[]): self.outdir = outdir self.log_format = log_format self.rex = rex self.para = {} self.para['dataPath'] = indir self.para['para_j'] = para_j self.para['savePath'] = outdir self.para['support'] = support self.para['saveLog'] = saveLog def parse(self, logname): self.para['dataName'] = logname SLCT(self.para, self.log_format, self.rex) def SLCT(para, log_format, rex): startTime = datetime.now() # start timing logname = os.path.join(para['dataPath'], para['dataName']) logger.info("Parsing file: {}".format(logname)) # SLCT compilation if not os.path.isfile('../SLCT/slct'): try: logger.info('Compile SLCT...\n>> gcc -o ../logparser/SLCT/slct -O2 ../logparser/SLCT/cslct.c') subprocess.check_output('gcc -o ../logparser/SLCT/slct -O2 ../logparser/SLCT/cslct.c', stderr=subprocess.STDOUT, shell=True) except: logger.info("Compile error! Please check GCC installed.\n") raise headers, regex = generate_logformat_regex(log_format) df_log = log_to_dataframe(logname, regex, headers, log_format) # Generate input file with open('slct_input.log', 'w') as fw: for line in df_log['Content']: if rex: for currentRex in rex: line = re.sub(currentRex, '<*>', line) fw.write(line + '\n') # Run SLCT command SLCT_command = extract_command(para, "slct_input.log") try: logger.info ("Run SLCT...\n>> {}".format(SLCT_command)) subprocess.check_call(SLCT_command, shell=True) except: logger.info("SLCT executable is invalid! Please compile it using GCC.\n") raise # Collect and dump templates tempParameter = TempPara(path = "./", savePath=para['savePath'], logname="slct_input.log") tempProcess(tempParameter) matcher = regexmatch.PatternMatch(outdir=para['savePath'], logformat=log_format) matched_df = matcher.match(logname, "temp_templates.csv") # sys.exit() os.remove("slct_input.log") os.remove("slct_outliers.log") os.remove("slct_templates.txt") os.remove("temp_templates.csv") for idx, line in matched_df.iterrows(): if line['EventTemplate'] == "None": content = line['Content'] matched_df.loc[idx, "EventTemplate"] = content matched_df.loc[idx, "EventId"] = hashlib.md5(content.encode('utf-8')).hexdigest()[0:8] occ_dict = dict(matched_df['EventTemplate'].value_counts()) df_event = pd.DataFrame() df_event['EventTemplate'] = matched_df['EventTemplate'].unique() df_event['EventId'] = df_event['EventTemplate'].map(lambda x: hashlib.md5(x.encode('utf-8')).hexdigest()[0:8]) df_event['Occurrences'] = df_event['EventTemplate'].map(occ_dict) df_event.to_csv(os.path.join(para['savePath'], para['dataName'] + "_templates.csv"), index=False, columns=["EventId", "EventTemplate", "Occurrences"]) matched_df.to_csv(os.path.join(para['savePath'], para['dataName'] + "_structured.csv"), index=False) logger.info('Parsing done. [Time: {!s}]'.format(datetime.now() - startTime)) def extract_command(para, logname): support = para['support'] parajTF = para['para_j'] input = '' if parajTF: input = '../logparser/SLCT/slct -j -o ' + 'slct_outliers.log -r -s ' + str(support) + ' ' + logname else: input = '../logparser/SLCT/slct -o ' + 'slct_outliers.log -r -s ' + str(support) + ' ' + logname return input def log_to_dataframe(log_file, regex, headers, logformat): ''' Function to transform log file to dataframe ''' log_messages = [] linecount = 0 with open(log_file, 'r') as fin: for line in fin.readlines(): try: match = regex.search(line.strip()) message = [match.group(header) for header in headers] log_messages.append(message) linecount += 1 except Exception as e: pass logdf = pd.DataFrame(log_messages, columns=headers) logdf.insert(0, 'LineId', None) logdf['LineId'] = [i + 1 for i in range(linecount)] return logdf def generate_logformat_regex(logformat): ''' Function to generate regular expression to split log messages ''' headers = [] splitters = re.split(r'(<[^<>]+>)', logformat) regex = '' for k in range(len(splitters)): if k % 2 == 0: splitter = re.sub(' +', '\s+', splitters[k]) regex += splitter else: header = splitters[k].strip('<').strip('>') regex += '(?P<%s>.*?)' % header headers.append(header) regex = re.compile('^' + regex + '$') return headers, regex class TempPara: def __init__(self, path='./', logname='rawlog.log', savePath='./', templateName='slct_templates.txt', outlierName='slct_outliers.log'): self.path = path self.logname = logname self.savePath = savePath self.templateName = templateName self.outlierName = outlierName def tempProcess(tempPara): logger.info('Dumping event templates...') if not os.path.exists(tempPara.savePath): os.makedirs(tempPara.savePath) #read the templates templates = [] with open('./' + tempPara.templateName) as tl: for line in tl: templates.append([0, line.strip(), 0]) pd.DataFrame(templates, columns=["EventId","EventTemplate","Occurrences"]).to_csv("temp_templates.csv", index=False) def matchTempLog(templates, logs): len_temp = {} for tidx, temp in enumerate(templates): tempL = temp.split() templen = len(tempL) if templen not in len_temp: len_temp[templen] = [(tidx, tempL)] else: len_temp[templen].append((tidx, tempL)) logid_groupid = [] for idx, log in enumerate(logs): logL = log.split() logid = idx+1 if len(logL) in len_temp: logid_groupid.append([idx + 1, get_groupid(logL, len_temp[len(logL)])]) else: logid_groupid.append([idx+1, -1]) return logid_groupid def get_groupid(logL, tempLs): maxvalue = -1 for templ in tempLs: starnum = 0 shot = 0 for idx, token in enumerate(logL): if token == templ[1][idx] or templ[1][idx].count("*"): shot += 1 if templ[1][idx].count("*"): starnum += 1 shot = shot - starnum if shot > maxvalue: maxvalue = shot groupid = templ[0] return groupid
logparser/SLCT/SLCT.py
7,346
Function to generate regular expression to split log messages Function to transform log file to dataframe Description: This file implements a wrapper around the original SLCT code in C Author: LogPAI team License: MIT start timing SLCT compilation Generate input file Run SLCT command Collect and dump templates sys.exit()read the templates
343
en
0.542636
from discord.ext import commands, tasks from collections import Counter, defaultdict from .utils import checks, db, time, formats from .utils.paginator import CannotPaginate import pkg_resources import logging import discord import textwrap import datetime import traceback import itertools import typing import asyncpg import asyncio import pygit2 import psutil import json import os import re import io import gc log = logging.getLogger(__name__) LOGGING_CHANNEL = 309632009427222529 class GatewayHandler(logging.Handler): def __init__(self, cog): self.cog = cog super().__init__(logging.INFO) def filter(self, record): return record.name == 'discord.gateway' or 'Shard ID' in record.msg or 'Websocket closed ' in record.msg def emit(self, record): self.cog.add_record(record) class Commands(db.Table): id = db.PrimaryKeyColumn() guild_id = db.Column(db.Integer(big=True), index=True) channel_id = db.Column(db.Integer(big=True)) author_id = db.Column(db.Integer(big=True), index=True) used = db.Column(db.Datetime, index=True) prefix = db.Column(db.String) command = db.Column(db.String, index=True) failed = db.Column(db.Boolean, index=True) _INVITE_REGEX = re.compile(r'(?:https?:\/\/)?discord(?:\.gg|\.com|app\.com\/invite)?\/[A-Za-z0-9]+') def censor_invite(obj, *, _regex=_INVITE_REGEX): return _regex.sub('[censored-invite]', str(obj)) def hex_value(arg): return int(arg, base=16) def object_at(addr): for o in gc.get_objects(): if id(o) == addr: return o return None class Stats(commands.Cog): """Bot usage statistics.""" def __init__(self, bot): self.bot = bot self.process = psutil.Process() self._batch_lock = asyncio.Lock(loop=bot.loop) self._data_batch = [] self.bulk_insert_loop.add_exception_type(asyncpg.PostgresConnectionError) self.bulk_insert_loop.start() self._gateway_queue = asyncio.Queue(loop=bot.loop) self.gateway_worker.start() # This is a datetime list self._resumes = [] # shard_id: List[datetime] self._identifies = defaultdict(list) def _clear_gateway_data(self): one_week_ago = datetime.datetime.utcnow() - datetime.timedelta(days=7) to_remove = [index for index, dt in enumerate(self._resumes) if dt < one_week_ago] for index in reversed(to_remove): del self._resumes[index] for shard_id, dates in self._identifies.items(): to_remove = [index for index, dt in enumerate(dates) if dt < one_week_ago] for index in reversed(to_remove): del dates[index] async def bulk_insert(self): query = """INSERT INTO commands (guild_id, channel_id, author_id, used, prefix, command, failed) SELECT x.guild, x.channel, x.author, x.used, x.prefix, x.command, x.failed FROM jsonb_to_recordset($1::jsonb) AS x(guild BIGINT, channel BIGINT, author BIGINT, used TIMESTAMP, prefix TEXT, command TEXT, failed BOOLEAN) """ if self._data_batch: await self.bot.pool.execute(query, self._data_batch) total = len(self._data_batch) if total > 1: log.info('Registered %s commands to the database.', total) self._data_batch.clear() def cog_unload(self): self.bulk_insert_loop.stop() self._gateway_worker.cancel() @tasks.loop(seconds=10.0) async def bulk_insert_loop(self): async with self._batch_lock: await self.bulk_insert() @tasks.loop(seconds=0.0) async def gateway_worker(self): record = await self._gateway_queue.get() await self.notify_gateway_status(record) async def register_command(self, ctx): if ctx.command is None: return command = ctx.command.qualified_name self.bot.command_stats[command] += 1 message = ctx.message destination = None if ctx.guild is None: destination = 'Private Message' guild_id = None else: destination = f'#{message.channel} ({message.guild})' guild_id = ctx.guild.id log.info(f'{message.created_at}: {message.author} in {destination}: {message.content}') async with self._batch_lock: self._data_batch.append({ 'guild': guild_id, 'channel': ctx.channel.id, 'author': ctx.author.id, 'used': message.created_at.isoformat(), 'prefix': ctx.prefix, 'command': command, 'failed': ctx.command_failed, }) @commands.Cog.listener() async def on_command_completion(self, ctx): await self.register_command(ctx) @commands.Cog.listener() async def on_socket_response(self, msg): self.bot.socket_stats[msg.get('t')] += 1 @property def webhook(self): wh_id, wh_token = self.bot.config.stat_webhook hook = discord.Webhook.partial(id=wh_id, token=wh_token, adapter=discord.AsyncWebhookAdapter(self.bot.session)) return hook async def log_error(self, *, ctx=None, extra=None): e = discord.Embed(title='Error', colour=0xdd5f53) e.description = f'```py\n{traceback.format_exc()}\n```' e.add_field(name='Extra', value=extra, inline=False) e.timestamp = datetime.datetime.utcnow() if ctx is not None: fmt = '{0} (ID: {0.id})' author = fmt.format(ctx.author) channel = fmt.format(ctx.channel) guild = 'None' if ctx.guild is None else fmt.format(ctx.guild) e.add_field(name='Author', value=author) e.add_field(name='Channel', value=channel) e.add_field(name='Guild', value=guild) await self.webhook.send(embed=e) @commands.command(hidden=True) @commands.is_owner() async def commandstats(self, ctx, limit=20): """Shows command stats. Use a negative number for bottom instead of top. This is only for the current session. """ counter = self.bot.command_stats width = len(max(counter, key=len)) total = sum(counter.values()) if limit > 0: common = counter.most_common(limit) else: common = counter.most_common()[limit:] output = '\n'.join(f'{k:<{width}}: {c}' for k, c in common) await ctx.send(f'```\n{output}\n```') @commands.command(hidden=True) async def socketstats(self, ctx): delta = datetime.datetime.utcnow() - self.bot.uptime minutes = delta.total_seconds() / 60 total = sum(self.bot.socket_stats.values()) cpm = total / minutes await ctx.send(f'{total} socket events observed ({cpm:.2f}/minute):\n{self.bot.socket_stats}') def get_bot_uptime(self, *, brief=False): return time.human_timedelta(self.bot.uptime, accuracy=None, brief=brief, suffix=False) @commands.command() async def uptime(self, ctx): """Tells you how long the bot has been up for.""" await ctx.send(f'Uptime: **{self.get_bot_uptime()}**') def format_commit(self, commit): short, _, _ = commit.message.partition('\n') short_sha2 = commit.hex[0:6] commit_tz = datetime.timezone(datetime.timedelta(minutes=commit.commit_time_offset)) commit_time = datetime.datetime.fromtimestamp(commit.commit_time).replace(tzinfo=commit_tz) # [`hash`](url) message (offset) offset = time.human_timedelta(commit_time.astimezone(datetime.timezone.utc).replace(tzinfo=None), accuracy=1) return f'[`{short_sha2}`](https://github.com/Rapptz/RoboDanny/commit/{commit.hex}) {short} ({offset})' def get_last_commits(self, count=3): repo = pygit2.Repository('.git') commits = list(itertools.islice(repo.walk(repo.head.target, pygit2.GIT_SORT_TOPOLOGICAL), count)) return '\n'.join(self.format_commit(c) for c in commits) @commands.command() async def about(self, ctx): """Tells you information about the bot itself.""" revision = self.get_last_commits() embed = discord.Embed(description='Latest Changes:\n' + revision) embed.title = 'Official Bot Server Invite' embed.url = 'https://discord.gg/DWEaqMy' embed.colour = discord.Colour.blurple() owner = self.bot.get_user(self.bot.owner_id) embed.set_author(name=str(owner), icon_url=owner.avatar_url) # statistics total_members = 0 total_online = 0 offline = discord.Status.offline for member in self.bot.get_all_members(): total_members += 1 if member.status is not offline: total_online += 1 total_unique = len(self.bot.users) text = 0 voice = 0 guilds = 0 for guild in self.bot.guilds: guilds += 1 for channel in guild.channels: if isinstance(channel, discord.TextChannel): text += 1 elif isinstance(channel, discord.VoiceChannel): voice += 1 embed.add_field(name='Members', value=f'{total_members} total\n{total_unique} unique\n{total_online} unique online') embed.add_field(name='Channels', value=f'{text + voice} total\n{text} text\n{voice} voice') memory_usage = self.process.memory_full_info().uss / 1024**2 cpu_usage = self.process.cpu_percent() / psutil.cpu_count() embed.add_field(name='Process', value=f'{memory_usage:.2f} MiB\n{cpu_usage:.2f}% CPU') version = pkg_resources.get_distribution('discord.py').version embed.add_field(name='Guilds', value=guilds) embed.add_field(name='Commands Run', value=sum(self.bot.command_stats.values())) embed.add_field(name='Uptime', value=self.get_bot_uptime(brief=True)) embed.set_footer(text=f'Made with discord.py v{version}', icon_url='http://i.imgur.com/5BFecvA.png') embed.timestamp = datetime.datetime.utcnow() await ctx.send(embed=embed) def censor_object(self, obj): if not isinstance(obj, str) and obj.id in self.bot.blacklist: return '[censored]' return censor_invite(obj) async def show_guild_stats(self, ctx): lookup = ( '\N{FIRST PLACE MEDAL}', '\N{SECOND PLACE MEDAL}', '\N{THIRD PLACE MEDAL}', '\N{SPORTS MEDAL}', '\N{SPORTS MEDAL}' ) embed = discord.Embed(title='Server Command Stats', colour=discord.Colour.blurple()) # total command uses query = "SELECT COUNT(*), MIN(used) FROM commands WHERE guild_id=$1;" count = await ctx.db.fetchrow(query, ctx.guild.id) embed.description = f'{count[0]} commands used.' embed.set_footer(text='Tracking command usage since').timestamp = count[1] or datetime.datetime.utcnow() query = """SELECT command, COUNT(*) as "uses" FROM commands WHERE guild_id=$1 GROUP BY command ORDER BY "uses" DESC LIMIT 5; """ records = await ctx.db.fetch(query, ctx.guild.id) value = '\n'.join(f'{lookup[index]}: {command} ({uses} uses)' for (index, (command, uses)) in enumerate(records)) or 'No Commands' embed.add_field(name='Top Commands', value=value, inline=True) query = """SELECT command, COUNT(*) as "uses" FROM commands WHERE guild_id=$1 AND used > (CURRENT_TIMESTAMP - INTERVAL '1 day') GROUP BY command ORDER BY "uses" DESC LIMIT 5; """ records = await ctx.db.fetch(query, ctx.guild.id) value = '\n'.join(f'{lookup[index]}: {command} ({uses} uses)' for (index, (command, uses)) in enumerate(records)) or 'No Commands.' embed.add_field(name='Top Commands Today', value=value, inline=True) embed.add_field(name='\u200b', value='\u200b', inline=True) query = """SELECT author_id, COUNT(*) AS "uses" FROM commands WHERE guild_id=$1 GROUP BY author_id ORDER BY "uses" DESC LIMIT 5; """ records = await ctx.db.fetch(query, ctx.guild.id) value = '\n'.join(f'{lookup[index]}: <@!{author_id}> ({uses} bot uses)' for (index, (author_id, uses)) in enumerate(records)) or 'No bot users.' embed.add_field(name='Top Command Users', value=value, inline=True) query = """SELECT author_id, COUNT(*) AS "uses" FROM commands WHERE guild_id=$1 AND used > (CURRENT_TIMESTAMP - INTERVAL '1 day') GROUP BY author_id ORDER BY "uses" DESC LIMIT 5; """ records = await ctx.db.fetch(query, ctx.guild.id) value = '\n'.join(f'{lookup[index]}: <@!{author_id}> ({uses} bot uses)' for (index, (author_id, uses)) in enumerate(records)) or 'No command users.' embed.add_field(name='Top Command Users Today', value=value, inline=True) await ctx.send(embed=embed) async def show_member_stats(self, ctx, member): lookup = ( '\N{FIRST PLACE MEDAL}', '\N{SECOND PLACE MEDAL}', '\N{THIRD PLACE MEDAL}', '\N{SPORTS MEDAL}', '\N{SPORTS MEDAL}' ) embed = discord.Embed(title='Command Stats', colour=member.colour) embed.set_author(name=str(member), icon_url=member.avatar_url) # total command uses query = "SELECT COUNT(*), MIN(used) FROM commands WHERE guild_id=$1 AND author_id=$2;" count = await ctx.db.fetchrow(query, ctx.guild.id, member.id) embed.description = f'{count[0]} commands used.' embed.set_footer(text='First command used').timestamp = count[1] or datetime.datetime.utcnow() query = """SELECT command, COUNT(*) as "uses" FROM commands WHERE guild_id=$1 AND author_id=$2 GROUP BY command ORDER BY "uses" DESC LIMIT 5; """ records = await ctx.db.fetch(query, ctx.guild.id, member.id) value = '\n'.join(f'{lookup[index]}: {command} ({uses} uses)' for (index, (command, uses)) in enumerate(records)) or 'No Commands' embed.add_field(name='Most Used Commands', value=value, inline=False) query = """SELECT command, COUNT(*) as "uses" FROM commands WHERE guild_id=$1 AND author_id=$2 AND used > (CURRENT_TIMESTAMP - INTERVAL '1 day') GROUP BY command ORDER BY "uses" DESC LIMIT 5; """ records = await ctx.db.fetch(query, ctx.guild.id, member.id) value = '\n'.join(f'{lookup[index]}: {command} ({uses} uses)' for (index, (command, uses)) in enumerate(records)) or 'No Commands' embed.add_field(name='Most Used Commands Today', value=value, inline=False) await ctx.send(embed=embed) @commands.group(invoke_without_command=True) @commands.guild_only() @commands.cooldown(1, 30.0, type=commands.BucketType.member) async def stats(self, ctx, *, member: discord.Member = None): """Tells you command usage stats for the server or a member.""" async with ctx.typing(): if member is None: await self.show_guild_stats(ctx) else: await self.show_member_stats(ctx, member) @stats.command(name='global') @commands.is_owner() async def stats_global(self, ctx): """Global all time command statistics.""" query = "SELECT COUNT(*) FROM commands;" total = await ctx.db.fetchrow(query) e = discord.Embed(title='Command Stats', colour=discord.Colour.blurple()) e.description = f'{total[0]} commands used.' lookup = ( '\N{FIRST PLACE MEDAL}', '\N{SECOND PLACE MEDAL}', '\N{THIRD PLACE MEDAL}', '\N{SPORTS MEDAL}', '\N{SPORTS MEDAL}' ) query = """SELECT command, COUNT(*) AS "uses" FROM commands GROUP BY command ORDER BY "uses" DESC LIMIT 5; """ records = await ctx.db.fetch(query) value = '\n'.join(f'{lookup[index]}: {command} ({uses} uses)' for (index, (command, uses)) in enumerate(records)) e.add_field(name='Top Commands', value=value, inline=False) query = """SELECT guild_id, COUNT(*) AS "uses" FROM commands GROUP BY guild_id ORDER BY "uses" DESC LIMIT 5; """ records = await ctx.db.fetch(query) value = [] for (index, (guild_id, uses)) in enumerate(records): if guild_id is None: guild = 'Private Message' else: guild = self.censor_object(self.bot.get_guild(guild_id) or f'<Unknown {guild_id}>') emoji = lookup[index] value.append(f'{emoji}: {guild} ({uses} uses)') e.add_field(name='Top Guilds', value='\n'.join(value), inline=False) query = """SELECT author_id, COUNT(*) AS "uses" FROM commands GROUP BY author_id ORDER BY "uses" DESC LIMIT 5; """ records = await ctx.db.fetch(query) value = [] for (index, (author_id, uses)) in enumerate(records): user = self.censor_object(self.bot.get_user(author_id) or f'<Unknown {author_id}>') emoji = lookup[index] value.append(f'{emoji}: {user} ({uses} uses)') e.add_field(name='Top Users', value='\n'.join(value), inline=False) await ctx.send(embed=e) @stats.command(name='today') @commands.is_owner() async def stats_today(self, ctx): """Global command statistics for the day.""" query = "SELECT failed, COUNT(*) FROM commands WHERE used > (CURRENT_TIMESTAMP - INTERVAL '1 day') GROUP BY failed;" total = await ctx.db.fetch(query) failed = 0 success = 0 question = 0 for state, count in total: if state is False: success += count elif state is True: failed += count else: question += count e = discord.Embed(title='Last 24 Hour Command Stats', colour=discord.Colour.blurple()) e.description = f'{failed + success + question} commands used today. ' \ f'({success} succeeded, {failed} failed, {question} unknown)' lookup = ( '\N{FIRST PLACE MEDAL}', '\N{SECOND PLACE MEDAL}', '\N{THIRD PLACE MEDAL}', '\N{SPORTS MEDAL}', '\N{SPORTS MEDAL}' ) query = """SELECT command, COUNT(*) AS "uses" FROM commands WHERE used > (CURRENT_TIMESTAMP - INTERVAL '1 day') GROUP BY command ORDER BY "uses" DESC LIMIT 5; """ records = await ctx.db.fetch(query) value = '\n'.join(f'{lookup[index]}: {command} ({uses} uses)' for (index, (command, uses)) in enumerate(records)) e.add_field(name='Top Commands', value=value, inline=False) query = """SELECT guild_id, COUNT(*) AS "uses" FROM commands WHERE used > (CURRENT_TIMESTAMP - INTERVAL '1 day') GROUP BY guild_id ORDER BY "uses" DESC LIMIT 5; """ records = await ctx.db.fetch(query) value = [] for (index, (guild_id, uses)) in enumerate(records): if guild_id is None: guild = 'Private Message' else: guild = self.censor_object(self.bot.get_guild(guild_id) or f'<Unknown {guild_id}>') emoji = lookup[index] value.append(f'{emoji}: {guild} ({uses} uses)') e.add_field(name='Top Guilds', value='\n'.join(value), inline=False) query = """SELECT author_id, COUNT(*) AS "uses" FROM commands WHERE used > (CURRENT_TIMESTAMP - INTERVAL '1 day') GROUP BY author_id ORDER BY "uses" DESC LIMIT 5; """ records = await ctx.db.fetch(query) value = [] for (index, (author_id, uses)) in enumerate(records): user = self.censor_object(self.bot.get_user(author_id) or f'<Unknown {author_id}>') emoji = lookup[index] value.append(f'{emoji}: {user} ({uses} uses)') e.add_field(name='Top Users', value='\n'.join(value), inline=False) await ctx.send(embed=e) async def send_guild_stats(self, e, guild): e.add_field(name='Name', value=guild.name) e.add_field(name='ID', value=guild.id) e.add_field(name='Shard ID', value=guild.shard_id or 'N/A') e.add_field(name='Owner', value=f'{guild.owner} (ID: {guild.owner.id})') bots = sum(m.bot for m in guild.members) total = guild.member_count online = sum(m.status is discord.Status.online for m in guild.members) e.add_field(name='Members', value=str(total)) e.add_field(name='Bots', value=f'{bots} ({bots/total:.2%})') e.add_field(name='Online', value=f'{online} ({online/total:.2%})') if guild.icon: e.set_thumbnail(url=guild.icon_url) if guild.me: e.timestamp = guild.me.joined_at await self.webhook.send(embed=e) @stats_today.before_invoke @stats_global.before_invoke async def before_stats_invoke(self, ctx): await ctx.trigger_typing() @commands.Cog.listener() async def on_guild_join(self, guild): e = discord.Embed(colour=0x53dda4, title='New Guild') # green colour await self.send_guild_stats(e, guild) @commands.Cog.listener() async def on_guild_remove(self, guild): e = discord.Embed(colour=0xdd5f53, title='Left Guild') # red colour await self.send_guild_stats(e, guild) @commands.Cog.listener() async def on_command_error(self, ctx, error): await self.register_command(ctx) if not isinstance(error, (commands.CommandInvokeError, commands.ConversionError)): return error = error.original if isinstance(error, (discord.Forbidden, discord.NotFound, CannotPaginate)): return e = discord.Embed(title='Command Error', colour=0xcc3366) e.add_field(name='Name', value=ctx.command.qualified_name) e.add_field(name='Author', value=f'{ctx.author} (ID: {ctx.author.id})') fmt = f'Channel: {ctx.channel} (ID: {ctx.channel.id})' if ctx.guild: fmt = f'{fmt}\nGuild: {ctx.guild} (ID: {ctx.guild.id})' e.add_field(name='Location', value=fmt, inline=False) e.add_field(name='Content', value=textwrap.shorten(ctx.message.content, width=512)) exc = ''.join(traceback.format_exception(type(error), error, error.__traceback__, chain=False)) e.description = f'```py\n{exc}\n```' e.timestamp = datetime.datetime.utcnow() await self.webhook.send(embed=e) @commands.Cog.listener() async def on_socket_raw_send(self, data): # kind of weird way to check if we're sending # IDENTIFY or RESUME if '"op":2' not in data and '"op":6' not in data: return back_to_json = json.loads(data) if back_to_json['op'] == 2: payload = back_to_json['d'] inner_shard = payload.get('shard', [0]) self._identifies[inner_shard[0]].append(datetime.datetime.utcnow()) else: self._resumes.append(datetime.datetime.utcnow()) # don't want to permanently grow memory self._clear_gateway_data() def add_record(self, record): # if self.bot.config.debug: # return self._gateway_queue.put_nowait(record) async def notify_gateway_status(self, record): attributes = { 'INFO': '\N{INFORMATION SOURCE}', 'WARNING': '\N{WARNING SIGN}' } emoji = attributes.get(record.levelname, '\N{CROSS MARK}') dt = datetime.datetime.utcfromtimestamp(record.created) msg = f'{emoji} `[{dt:%Y-%m-%d %H:%M:%S}] {record.message}`' await self.webhook.send(msg, username='Gateway', avatar_url='https://i.imgur.com/4PnCKB3.png') @commands.command(hidden=True) @commands.is_owner() async def bothealth(self, ctx): """Various bot health monitoring tools.""" # This uses a lot of private methods because there is no # clean way of doing this otherwise. HEALTHY = discord.Colour(value=0x43B581) UNHEALTHY = discord.Colour(value=0xF04947) WARNING = discord.Colour(value=0xF09E47) total_warnings = 0 embed = discord.Embed(title='Bot Health Report', colour=HEALTHY) # Check the connection pool health. pool = self.bot.pool total_waiting = len(pool._queue._getters) current_generation = pool._generation description = [ f'Total `Pool.acquire` Waiters: {total_waiting}', f'Current Pool Generation: {current_generation}', f'Connections In Use: {len(pool._holders) - pool._queue.qsize()}' ] questionable_connections = 0 connection_value = [] for index, holder in enumerate(pool._holders, start=1): generation = holder._generation in_use = holder._in_use is not None is_closed = holder._con is None or holder._con.is_closed() display = f'gen={holder._generation} in_use={in_use} closed={is_closed}' questionable_connections += any((in_use, generation != current_generation)) connection_value.append(f'<Holder i={index} {display}>') joined_value = '\n'.join(connection_value) embed.add_field(name='Connections', value=f'```py\n{joined_value}\n```', inline=False) spam_control = self.bot.spam_control being_spammed = [ str(key) for key, value in spam_control._cache.items() if value._tokens == 0 ] description.append(f'Current Spammers: {", ".join(being_spammed) if being_spammed else "None"}') description.append(f'Questionable Connections: {questionable_connections}') total_warnings += questionable_connections if being_spammed: embed.colour = WARNING total_warnings += 1 try: task_retriever = asyncio.Task.all_tasks except AttributeError: # future proofing for 3.9 I guess task_retriever = asyncio.all_tasks else: all_tasks = task_retriever(loop=self.bot.loop) event_tasks = [ t for t in all_tasks if 'Client._run_event' in repr(t) and not t.done() ] cogs_directory = os.path.dirname(__file__) tasks_directory = os.path.join('discord', 'ext', 'tasks', '__init__.py') inner_tasks = [ t for t in all_tasks if cogs_directory in repr(t) or tasks_directory in repr(t) ] bad_inner_tasks = ", ".join(hex(id(t)) for t in inner_tasks if t.done() and t._exception is not None) total_warnings += bool(bad_inner_tasks) embed.add_field(name='Inner Tasks', value=f'Total: {len(inner_tasks)}\nFailed: {bad_inner_tasks or "None"}') embed.add_field(name='Events Waiting', value=f'Total: {len(event_tasks)}', inline=False) command_waiters = len(self._data_batch) is_locked = self._batch_lock.locked() description.append(f'Commands Waiting: {command_waiters}, Batch Locked: {is_locked}') # RESUME/IDENTIFY data yesterday = datetime.datetime.utcnow() - datetime.timedelta(days=1) total_resumes = sum(1 for dt in self._resumes if dt > yesterday) identifies = { shard_id: sum(1 for dt in dates if dt > yesterday) for shard_id, dates in self._identifies.items() } absolute_total_identifies = sum(identifies.values()) resume_info_builder = [ f'Total RESUMEs: {total_resumes}', f'Total IDENTIFYs: {absolute_total_identifies}' ] for shard_id, total in identifies.items(): resume_info_builder.append(f'Shard ID {shard_id} IDENTIFYs: {total}') if absolute_total_identifies >= (len(self.bot.shards) * 5): total_warnings += 1 embed.colour = WARNING embed.add_field(name='Gateway (last 24 hours)', value='\n'.join(resume_info_builder), inline=False) memory_usage = self.process.memory_full_info().uss / 1024**2 cpu_usage = self.process.cpu_percent() / psutil.cpu_count() embed.add_field(name='Process', value=f'{memory_usage:.2f} MiB\n{cpu_usage:.2f}% CPU', inline=False) global_rate_limit = not self.bot.http._global_over.is_set() description.append(f'Global Rate Limit: {global_rate_limit}') if command_waiters >= 8: total_warnings += 1 embed.colour = WARNING if global_rate_limit or total_warnings >= 9: embed.colour = UNHEALTHY embed.set_footer(text=f'{total_warnings} warning(s)') embed.description = '\n'.join(description) await ctx.send(embed=embed) @commands.command(hidden=True, aliases=['cancel_task']) @commands.is_owner() async def debug_task(self, ctx, memory_id: hex_value): """Debug a task by a memory location.""" task = object_at(memory_id) if task is None or not isinstance(task, asyncio.Task): return await ctx.send(f'Could not find Task object at {hex(memory_id)}.') if ctx.invoked_with == 'cancel_task': task.cancel() return await ctx.send(f'Cancelled task object {task!r}.') paginator = commands.Paginator(prefix='```py') fp = io.StringIO() frames = len(task.get_stack()) paginator.add_line(f'# Total Frames: {frames}') task.print_stack(file=fp) for line in fp.getvalue().splitlines(): paginator.add_line(line) for page in paginator.pages: await ctx.send(page) async def tabulate_query(self, ctx, query, *args): records = await ctx.db.fetch(query, *args) if len(records) == 0: return await ctx.send('No results found.') headers = list(records[0].keys()) table = formats.TabularData() table.set_columns(headers) table.add_rows(list(r.values()) for r in records) render = table.render() fmt = f'```\n{render}\n```' if len(fmt) > 2000: fp = io.BytesIO(fmt.encode('utf-8')) await ctx.send('Too many results...', file=discord.File(fp, 'results.txt')) else: await ctx.send(fmt) @commands.group(hidden=True, invoke_without_command=True) @commands.is_owner() async def command_history(self, ctx): """Command history.""" query = """SELECT CASE failed WHEN TRUE THEN command || ' [!]' ELSE command END AS "command", to_char(used, 'Mon DD HH12:MI:SS AM') AS "invoked", author_id, guild_id FROM commands ORDER BY used DESC LIMIT 15; """ await self.tabulate_query(ctx, query) @command_history.command(name='for') @commands.is_owner() async def command_history_for(self, ctx, days: typing.Optional[int] = 7, *, command: str): """Command history for a command.""" query = """SELECT *, t.success + t.failed AS "total" FROM ( SELECT guild_id, SUM(CASE WHEN failed THEN 0 ELSE 1 END) AS "success", SUM(CASE WHEN failed THEN 1 ELSE 0 END) AS "failed" FROM commands WHERE command=$1 AND used > (CURRENT_TIMESTAMP - $2::interval) GROUP BY guild_id ) AS t ORDER BY "total" DESC LIMIT 30; """ await self.tabulate_query(ctx, query, command, datetime.timedelta(days=days)) @command_history.command(name='guild', aliases=['server']) @commands.is_owner() async def command_history_guild(self, ctx, guild_id: int): """Command history for a guild.""" query = """SELECT CASE failed WHEN TRUE THEN command || ' [!]' ELSE command END AS "command", channel_id, author_id, used FROM commands WHERE guild_id=$1 ORDER BY used DESC LIMIT 15; """ await self.tabulate_query(ctx, query, guild_id) @command_history.command(name='user', aliases=['member']) @commands.is_owner() async def command_history_user(self, ctx, user_id: int): """Command history for a user.""" query = """SELECT CASE failed WHEN TRUE THEN command || ' [!]' ELSE command END AS "command", guild_id, used FROM commands WHERE author_id=$1 ORDER BY used DESC LIMIT 20; """ await self.tabulate_query(ctx, query, user_id) @command_history.command(name='log') @commands.is_owner() async def command_history_log(self, ctx, days=7): """Command history log for the last N days.""" query = """SELECT command, COUNT(*) FROM commands WHERE used > (CURRENT_TIMESTAMP - $1::interval) GROUP BY command ORDER BY 2 DESC """ all_commands = { c.qualified_name: 0 for c in self.bot.walk_commands() } records = await ctx.db.fetch(query, datetime.timedelta(days=days)) for name, uses in records: if name in all_commands: all_commands[name] = uses as_data = sorted(all_commands.items(), key=lambda t: t[1], reverse=True) table = formats.TabularData() table.set_columns(['Command', 'Uses']) table.add_rows(tup for tup in as_data) render = table.render() embed = discord.Embed(title='Summary', colour=discord.Colour.green()) embed.set_footer(text='Since').timestamp = datetime.datetime.utcnow() - datetime.timedelta(days=days) top_ten = '\n'.join(f'{command}: {uses}' for command, uses in records[:10]) bottom_ten = '\n'.join(f'{command}: {uses}' for command, uses in records[-10:]) embed.add_field(name='Top 10', value=top_ten) embed.add_field(name='Bottom 10', value=bottom_ten) unused = ', '.join(name for name, uses in as_data if uses == 0) if len(unused) > 1024: unused = 'Way too many...' embed.add_field(name='Unused', value=unused, inline=False) await ctx.send(embed=embed, file=discord.File(io.BytesIO(render.encode()), filename='full_results.txt')) @command_history.command(name='cog') @commands.is_owner() async def command_history_cog(self, ctx, days: typing.Optional[int] = 7, *, cog: str = None): """Command history for a cog or grouped by a cog.""" interval = datetime.timedelta(days=days) if cog is not None: cog = self.bot.get_cog(cog) if cog is None: return await ctx.send(f'Unknown cog: {cog}') query = """SELECT *, t.success + t.failed AS "total" FROM ( SELECT command, SUM(CASE WHEN failed THEN 0 ELSE 1 END) AS "success", SUM(CASE WHEN failed THEN 1 ELSE 0 END) AS "failed" FROM commands WHERE command = any($1::text[]) AND used > (CURRENT_TIMESTAMP - $2::interval) GROUP BY command ) AS t ORDER BY "total" DESC LIMIT 30; """ return await self.tabulate_query(ctx, query, [c.qualified_name for c in cog.walk_commands()], interval) # A more manual query with a manual grouper. query = """SELECT *, t.success + t.failed AS "total" FROM ( SELECT command, SUM(CASE WHEN failed THEN 0 ELSE 1 END) AS "success", SUM(CASE WHEN failed THEN 1 ELSE 0 END) AS "failed" FROM commands WHERE used > (CURRENT_TIMESTAMP - $1::interval) GROUP BY command ) AS t; """ class Count: __slots__ = ('success', 'failed', 'total') def __init__(self): self.success = 0 self.failed = 0 self.total = 0 def add(self, record): self.success += record['success'] self.failed += record['failed'] self.total += record['total'] data = defaultdict(Count) records = await ctx.db.fetch(query, interval) for record in records: command = self.bot.get_command(record['command']) if command is None or command.cog is None: data['No Cog'].add(record) else: data[command.cog.qualified_name].add(record) table = formats.TabularData() table.set_columns(['Cog', 'Success', 'Failed', 'Total']) data = sorted([ (cog, e.success, e.failed, e.total) for cog, e in data.items() ], key=lambda t: t[-1], reverse=True) table.add_rows(data) render = table.render() await ctx.safe_send(f'```\n{render}\n```') old_on_error = commands.AutoShardedBot.on_error async def on_error(self, event, *args, **kwargs): e = discord.Embed(title='Event Error', colour=0xa32952) e.add_field(name='Event', value=event) e.description = f'```py\n{traceback.format_exc()}\n```' e.timestamp = datetime.datetime.utcnow() args_str = ['```py'] for index, arg in enumerate(args): args_str.append(f'[{index}]: {arg!r}') args_str.append('```') e.add_field(name='Args', value='\n'.join(args_str), inline=False) hook = self.get_cog('Stats').webhook try: await hook.send(embed=e) except: pass def setup(bot): if not hasattr(bot, 'command_stats'): bot.command_stats = Counter() if not hasattr(bot, 'socket_stats'): bot.socket_stats = Counter() cog = Stats(bot) bot.add_cog(cog) bot._stats_cog_gateway_handler = handler = GatewayHandler(cog) logging.getLogger().addHandler(handler) commands.AutoShardedBot.on_error = on_error def teardown(bot): commands.AutoShardedBot.on_error = old_on_error logging.getLogger().removeHandler(bot._stats_cog_gateway_handler) del bot._stats_cog_gateway_handler
cogs/stats.py
40,783
Bot usage statistics. This is a datetime list shard_id: List[datetime] [`hash`](url) message (offset) statistics total command uses total command uses green colour red colour kind of weird way to check if we're sending IDENTIFY or RESUME don't want to permanently grow memory if self.bot.config.debug: return This uses a lot of private methods because there is no clean way of doing this otherwise. Check the connection pool health. future proofing for 3.9 I guess RESUME/IDENTIFY data A more manual query with a manual grouper.
534
en
0.72289
# ================================================================= # IMPORT REQUIRED LIBRARIES # ================================================================= import os # ================================================================= # READ DATA # ================================================================= data_location = os.path.join(os.path.abspath(""), '2021/day-06-lanternfish/') # with open(os.path.join(data_location, 'input_small.txt'), 'r') as f: with open(os.path.join(data_location, 'input.txt'), 'r') as f: data = f.read().split(",") data = [int(fish) for fish in data] # print(data) # ================================================================= # LOGIC - PART ONE # ================================================================= def part_one(): numDays = 18 numFishes = len(data) # print("Initial state: ", data) # Each day for day in range(numDays): for i in range(numFishes): fish = data[i] if fish == 0: # a 0 becomes a 6 data[i] = 6 # and adds a new 8 to the end of the list data.append(8) else: data[i] = fish-1 numFishes = len(data) # print("After ", str(day), " day: ", data) return(numFishes) # ================================================================= # LOGIC - PART TWO # ================================================================= def part_two(): numDays = 256 fishesDict = {} for i in range(9): fishesDict[i] = 0 fishesDict[i] = data.count(i) # print(fishesDict) # print("Initial state: ", fishesDict) # Each day for day in range(numDays): newFishesDict = {} for i in range(9): newFishesDict[i] = 0 holder = 0 for i in fishesDict: if i == 0: holder += fishesDict[0] else: newFishesDict[i-1] = fishesDict[i] # A 0 becomes a 6 newFishesDict[6] += holder # and adds a new 8 to the end of the list newFishesDict[8] += holder fishesDict = newFishesDict # print("After ", str(day+1), " day: ", fishesDict) numFishes = 0 for i in range(9): numFishes += fishesDict[i] return(numFishes) # ================================================================= # MAIN # ================================================================= if __name__ == '__main__': # print("Part one result is: " , str(part_one())) print("Part two result is: " , str(part_two()))
2021/day-06-lanternfish/day-06.py
2,618
================================================================= IMPORT REQUIRED LIBRARIES ================================================================= ================================================================= READ DATA ================================================================= with open(os.path.join(data_location, 'input_small.txt'), 'r') as f: print(data) ================================================================= LOGIC - PART ONE ================================================================= print("Initial state: ", data) Each day a 0 becomes a 6 and adds a new 8 to the end of the list print("After ", str(day), " day: ", data) ================================================================= LOGIC - PART TWO ================================================================= print(fishesDict) print("Initial state: ", fishesDict) Each day A 0 becomes a 6 and adds a new 8 to the end of the list print("After ", str(day+1), " day: ", fishesDict) ================================================================= MAIN ================================================================= print("Part one result is: " , str(part_one()))
1,171
en
0.50886
from configparser import ConfigParser # Initialize the Parser. config = ConfigParser() # Add the Section. config.add_section('graph_api') # Set the Values. config.set('graph_api', 'client_id', '_______') print('set client ID') config.set('graph_api', 'client_secret', '_______') print('set client secret') config.set('graph_api', 'redirect_uri', '_______') print('set redirect url') # Write the file. with open(file='config.ini', mode='w+') as f: config.write(f) print('config written') print(config)
write_config.py
538
Initialize the Parser. Add the Section. Set the Values. Write the file.
71
en
0.390076
# Part of Odoo. See LICENSE file for full copyright and licensing details. import odoo.tests @odoo.tests.tagged('post_install','-at_install') class TestUi(odoo.tests.HttpCase): def test_01_crm_tour(self): self.start_tour("/web", 'crm_tour', login="admin")
odoo/base-addons/crm/tests/test_crm_ui.py
272
Part of Odoo. See LICENSE file for full copyright and licensing details.
72
en
0.897708
import battery_test if __name__ == '__main__': #assert(EV_test.battery_is_ok(25, 70, 0.7) is True) #assert(EV_test.battery_is_ok(50, 85, 0) is False) assert(battery_test.check_battery_is_ok({'temperature' : 25,'soc' : 70,'charge_rate' : 0.9}) is False) assert(battery_test.check_battery_is_ok({'temperature' : 25,'soc' : 70,'charge_rate' : 0.7}) is True) assert(battery_test.check_battery_is_ok({'temperature' : 50,'soc' : 85,'charge_rate' : 0}) is False)
check_limits.py
478
assert(EV_test.battery_is_ok(25, 70, 0.7) is True)assert(EV_test.battery_is_ok(50, 85, 0) is False)
99
en
0.691166
"""Unit tests for comparison functions for geometry types. Authors: Ayush Baid """ import unittest from typing import List from unittest.mock import patch import numpy as np from gtsam import Cal3_S2, Point3, Pose3, Rot3, Similarity3, Unit3 from gtsam.examples import SFMdata import gtsfm.utils.geometry_comparisons as geometry_comparisons import tests.data.sample_poses as sample_poses POSE_LIST = SFMdata.createPoses(Cal3_S2()) ROT3_EULER_ANGLE_ERROR_THRESHOLD = 1e-2 POINT3_RELATIVE_ERROR_THRESH = 1e-1 POINT3_ABS_ERROR_THRESH = 1e-2 def rot3_compare(R: Rot3, R_: Rot3, msg=None) -> bool: return np.allclose(R.xyz(), R_.xyz(), atol=1e-2) def point3_compare(t: Point3, t_: Point3, msg=None) -> bool: return np.allclose(t, t_, rtol=POINT3_RELATIVE_ERROR_THRESH, atol=POINT3_ABS_ERROR_THRESH) class TestGeometryComparisons(unittest.TestCase): """Unit tests for comparison functions for geometry types.""" def __assert_equality_on_rot3s(self, computed: List[Rot3], expected: List[Rot3]) -> None: self.assertEqual(len(computed), len(expected)) for R, R_ in zip(computed, expected): self.assertEqual(R, R_) def __assert_equality_on_point3s(self, computed: List[Point3], expected: List[Point3]) -> None: self.assertEqual(len(computed), len(expected)) for t, t_ in zip(computed, expected): np.testing.assert_allclose(t, t_, rtol=POINT3_RELATIVE_ERROR_THRESH, atol=POINT3_ABS_ERROR_THRESH) def __assert_equality_on_pose3s(self, computed: List[Pose3], expected: List[Pose3]) -> None: self.assertEqual(len(computed), len(expected)) computed_rot3s = [x.rotation() for x in computed] computed_point3s = [x.translation() for x in computed] expected_rot3s = [x.rotation() for x in expected] expected_point3s = [x.translation() for x in expected] self.__assert_equality_on_rot3s(computed_rot3s, expected_rot3s) self.__assert_equality_on_point3s(computed_point3s, expected_point3s) def setUp(self): super().setUp() self.addTypeEqualityFunc(Rot3, rot3_compare) self.addTypeEqualityFunc(Point3, point3_compare) def test_align_rotations(self): """Tests the alignment of rotations.""" # using rotation along just the Y-axis so that angles can be linearly added. input_list = [ Rot3.RzRyRx(np.deg2rad(0), np.deg2rad(-10), 0), Rot3.RzRyRx(np.deg2rad(0), np.deg2rad(30), 0), ] ref_list = [ Rot3.RzRyRx(np.deg2rad(0), np.deg2rad(80), 0), Rot3.RzRyRx(np.deg2rad(0), np.deg2rad(-40), 0), ] computed = geometry_comparisons.align_rotations(input_list, ref_list) expected = [ Rot3.RzRyRx(0, np.deg2rad(80), 0), Rot3.RzRyRx(0, np.deg2rad(120), 0), ] self.__assert_equality_on_rot3s(computed, expected) def test_align_poses_after_sim3_transform(self): """Test for alignment of poses after applying a SIM3 transformation.""" translation_shift = np.array([5, 10, -5]) rotation_shift = Rot3.RzRyRx(0, 0, np.deg2rad(30)) scaling_factor = 0.7 transform = Similarity3(rotation_shift, translation_shift, scaling_factor) ref_list = [transform.transformFrom(x) for x in sample_poses.CIRCLE_TWO_EDGES_GLOBAL_POSES] computed_poses = geometry_comparisons.align_poses_sim3(sample_poses.CIRCLE_TWO_EDGES_GLOBAL_POSES, ref_list) self.__assert_equality_on_pose3s(computed_poses, sample_poses.CIRCLE_TWO_EDGES_GLOBAL_POSES) def test_align_poses_on_panorama_after_sim3_transform(self): """Test for alignment of poses after applying a forward motion transformation.""" translation_shift = np.array([0, 5, 0]) rotation_shift = Rot3.RzRyRx(0, 0, np.deg2rad(30)) scaling_factor = 1.0 aTi_list = sample_poses.PANORAMA_GLOBAL_POSES bSa = Similarity3(rotation_shift, translation_shift, scaling_factor) bTi_list = [bSa.transformFrom(x) for x in aTi_list] aTi_list_ = geometry_comparisons.align_poses_sim3(aTi_list, bTi_list) self.__assert_equality_on_pose3s(aTi_list_, aTi_list) @patch( "gtsfm.utils.geometry_comparisons.align_rotations", return_value=[ Rot3.RzRyRx(0, np.deg2rad(32), 0), Rot3.RzRyRx(0, 0, np.deg2rad(-22)), Rot3.RzRyRx(0, 0, np.deg2rad(83)), ], # compared with aRi_list ) def test_compare_rotations_with_all_valid_rot3s_success(self, align_rotations_mocked): """Tests the comparison results on list of rotations.""" aRi_list = [ Rot3.RzRyRx(0, np.deg2rad(25), 0), Rot3.RzRyRx(0, 0, np.deg2rad(-20)), Rot3.RzRyRx(0, 0, np.deg2rad(80)), ] bRi_list = [ Rot3.RzRyRx(0, np.deg2rad(31), 0), Rot3.RzRyRx(0, 0, np.deg2rad(-22)), Rot3.RzRyRx(0, 0, np.deg2rad(77.5)), ] # meaningless as align function is mocked # test with threshold of 10 degrees, which satisfies all the rotations. self.assertTrue(geometry_comparisons.compare_rotations(aRi_list, bRi_list, 10)) align_rotations_mocked.assert_called_once() @patch( "gtsfm.utils.geometry_comparisons.align_rotations", return_value=[ Rot3.RzRyRx(0, np.deg2rad(32), 0), Rot3.RzRyRx(0, 0, np.deg2rad(-22)), Rot3.RzRyRx(0, 0, np.deg2rad(83)), ], # compared with aRi_list ) def test_compare_rotations_with_all_valid_rot3s_failure(self, align_rotations_mocked): """Tests the comparison results on list of rotations.""" aRi_list = [ Rot3.RzRyRx(0, np.deg2rad(25), 0), Rot3.RzRyRx(0, 0, np.deg2rad(-20)), Rot3.RzRyRx(0, 0, np.deg2rad(80)), ] bRi_list = [ Rot3.RzRyRx(0, np.deg2rad(31), 0), Rot3.RzRyRx(0, 0, np.deg2rad(-22)), Rot3.RzRyRx(0, 0, np.deg2rad(77.5)), ] # meaningless as align function is mocked # test with threshold of 5 degrees, which fails one rotation and hence the overall comparison self.assertFalse(geometry_comparisons.compare_rotations(aRi_list, bRi_list, 5)) align_rotations_mocked.assert_called_once() @patch( "gtsfm.utils.geometry_comparisons.align_rotations", return_value=[Rot3.RzRyRx(0, np.deg2rad(25), 0), Rot3.RzRyRx(0, 0, np.deg2rad(-20))], # compared with aRi_list ) def test_compare_rotations_with_nones_at_same_indices(self, align_rotations_mocked): """Tests the comparison results on list of rotations.""" list1 = [ Rot3.RzRyRx(0, np.deg2rad(25), 0), Rot3.RzRyRx(0, 0, np.deg2rad(-20)), None, ] list2 = [ Rot3.RzRyRx(0, np.deg2rad(31), 0), Rot3.RzRyRx(0, 0, np.deg2rad(-22)), None, ] threshold_degrees = 10 # test with threshold of 10 degrees, which satisfies all the rotations. self.assertTrue(geometry_comparisons.compare_rotations(list1, list2, threshold_degrees)) align_rotations_mocked.assert_called_once() @patch("gtsfm.utils.geometry_comparisons.align_rotations", return_value=None) def test_compare_rotations_with_nones_at_different_indices(self, aligned_rotations_mocked): """Tests the comparison results on list of rotations.""" list1 = [ Rot3.RzRyRx(0, np.deg2rad(25), 0), Rot3.RzRyRx(0, 0, np.deg2rad(-20)), None, ] list2 = [ Rot3.RzRyRx(0, np.deg2rad(31), 0), None, Rot3.RzRyRx(0, 0, np.deg2rad(-22)), ] # test with threshold of 10 degrees, which satisfies all the rotations. self.assertFalse(geometry_comparisons.compare_rotations(list1, list2, 10)) aligned_rotations_mocked.assert_not_called() def test_compute_relative_rotation_angle(self): """Tests the relative angle between two rotations.""" R_1 = Rot3.RzRyRx(0, np.deg2rad(45), np.deg2rad(22.5)) R_2 = Rot3.RzRyRx(0, np.deg2rad(90), np.deg2rad(22.5)) # returns angle in degrees computed_deg = geometry_comparisons.compute_relative_rotation_angle(R_1, R_2) expected_deg = 45 np.testing.assert_allclose(computed_deg, expected_deg, rtol=1e-3, atol=1e-3) def test_compute_relative_unit_translation_angle(self): """Tests the relative angle between two unit-translations.""" U_1 = Unit3(np.array([1, 0, 0])) U_2 = Unit3(np.array([0.5, 0.5, 0])) # returns angle in degrees computed_deg = geometry_comparisons.compute_relative_unit_translation_angle(U_1, U_2) expected_deg = 45 self.assertAlmostEqual(computed_deg, expected_deg, places=3) def test_compute_translation_to_direction_angle_is_zero(self): i2Ui1_measured = Unit3(Point3(1, 0, 0)) wTi2_estimated = Pose3(Rot3(), Point3(0, 0, 0)) wTi1_estimated = Pose3(Rot3(), Point3(2, 0, 0)) self.assertEqual( geometry_comparisons.compute_translation_to_direction_angle(i2Ui1_measured, wTi2_estimated, wTi1_estimated), 0.0, ) def test_compute_translation_to_direction_angle_is_nonzero(self): rz = np.deg2rad(90) wRi2 = Rot3.RzRyRx(0, 0, rz) # x-axis of i2 points along y in world frame wTi2_estimated = Pose3(wRi2, Point3(0, 0, 0)) wTi1_estimated = Pose3(Rot3(), Point3(-1, 0, 0)) # At (0, 1, 0) in i2 frame, rotation of i1 is irrelevant here. i2Ui1_measured = Unit3(Point3(1, 0, 0)) # Estimated relative translation of i1 in i2 frame is (0, 1, 0), and the measurement in i2 frame is (1, 0, 0). # Expected angle between the two is 90 degrees. self.assertTrue( geometry_comparisons.compute_translation_to_direction_angle(i2Ui1_measured, wTi2_estimated, wTi1_estimated), 90.0, ) def test_compute_points_distance_l2_is_zero(self): self.assertEqual( geometry_comparisons.compute_points_distance_l2(wti1=Point3(1, -2, 3), wti2=Point3(1, -2, 3)), 0.0 ) def test_compute_points_distance_l2_is_none(self): self.assertEqual(geometry_comparisons.compute_points_distance_l2(wti1=Point3(0, 0, 0), wti2=None), None) def test_compute_points_distance_l2_is_nonzero(self): wti1 = Point3(1, 1, 1) wti2 = Point3(1, 1, -1) self.assertEqual(geometry_comparisons.compute_points_distance_l2(wti1, wti2), 2) def test_align_poses_sim3_ignore_missing(self): """Consider a simple cases with 4 poses in a line. Suppose SfM only recovers 2 of the 4 poses.""" wT0 = Pose3(Rot3(np.eye(3)), np.zeros(3)) wT1 = Pose3(Rot3(np.eye(3)), np.ones(3)) wT2 = Pose3(Rot3(np.eye(3)), np.ones(3) * 2) wT3 = Pose3(Rot3(np.eye(3)), np.ones(3) * 3) # `a` frame is the target/reference frame aTi_list = [wT0, wT1, wT2, wT3] # `b` frame contains the estimates bTi_list = [None, wT1, None, wT3] aTi_list_ = geometry_comparisons.align_poses_sim3_ignore_missing(aTi_list, bTi_list) # indices 0 and 2 should still have no estimated pose, even after alignment assert aTi_list_[0] is None assert aTi_list_[2] is None # identity alignment should preserve poses, should still match GT/targets at indices 1 and 3 self.__assert_equality_on_pose3s(computed=[aTi_list_[1], aTi_list_[3]], expected=[aTi_list[1], aTi_list[3]]) def test_get_points_within_radius_of_cameras(): """Verify that points that fall outside of 10 meter radius of two camera poses. Cameras are placed at (0,0,0) and (10,0,0). """ wTi0 = Pose3(Rot3(), np.zeros(3)) wTi1 = Pose3(Rot3(), np.array([10.0, 0, 0])) wTi_list = [wTi0, wTi1] points_3d = np.array([[-15, 0, 0], [0, 15, 0], [-5, 0, 0], [15, 0, 0], [25, 0, 0]]) radius = 10.0 nearby_points_3d = geometry_comparisons.get_points_within_radius_of_cameras(wTi_list, points_3d, radius) expected_nearby_points_3d = np.array([[-5, 0, 0], [15, 0, 0]]) np.testing.assert_allclose(nearby_points_3d, expected_nearby_points_3d) def test_get_points_within_radius_of_cameras_negative_radius(): """Catch degenerate input.""" wTi0 = Pose3(Rot3(), np.zeros(3)) wTi1 = Pose3(Rot3(), np.array([10.0, 0, 0])) wTi_list = [wTi0, wTi1] points_3d = np.array([[-15, 0, 0], [0, 15, 0], [-5, 0, 0], [15, 0, 0], [25, 0, 0]]) radius = -5 nearby_points_3d = geometry_comparisons.get_points_within_radius_of_cameras(wTi_list, points_3d, radius) assert nearby_points_3d is None, "Non-positive radius is not allowed" def test_get_points_within_radius_of_cameras_no_points(): """Catch degenerate input.""" wTi0 = Pose3(Rot3(), np.zeros(3)) wTi1 = Pose3(Rot3(), np.array([10.0, 0, 0])) wTi_list = [wTi0, wTi1] points_3d = np.zeros((0, 3)) radius = 10.0 nearby_points_3d = geometry_comparisons.get_points_within_radius_of_cameras(wTi_list, points_3d, radius) assert nearby_points_3d is None, "At least one 3d point must be provided" def test_get_points_within_radius_of_cameras_no_poses(): """Catch degenerate input.""" wTi_list = [] points_3d = np.array([[-15, 0, 0], [0, 15, 0], [-5, 0, 0], [15, 0, 0], [25, 0, 0]]) radius = 10.0 nearby_points_3d = geometry_comparisons.get_points_within_radius_of_cameras(wTi_list, points_3d, radius) assert nearby_points_3d is None, "At least one camera pose must be provided" if __name__ == "__main__": unittest.main()
tests/utils/test_geometry_comparisons.py
13,727
Unit tests for comparison functions for geometry types. Test for alignment of poses after applying a SIM3 transformation. Test for alignment of poses after applying a forward motion transformation. Consider a simple cases with 4 poses in a line. Suppose SfM only recovers 2 of the 4 poses. Tests the alignment of rotations. Tests the comparison results on list of rotations. Tests the comparison results on list of rotations. Tests the comparison results on list of rotations. Tests the comparison results on list of rotations. Tests the relative angle between two rotations. Tests the relative angle between two unit-translations. Verify that points that fall outside of 10 meter radius of two camera poses. Cameras are placed at (0,0,0) and (10,0,0). Catch degenerate input. Catch degenerate input. Catch degenerate input. Unit tests for comparison functions for geometry types. Authors: Ayush Baid using rotation along just the Y-axis so that angles can be linearly added. compared with aRi_list meaningless as align function is mocked test with threshold of 10 degrees, which satisfies all the rotations. compared with aRi_list meaningless as align function is mocked test with threshold of 5 degrees, which fails one rotation and hence the overall comparison compared with aRi_list test with threshold of 10 degrees, which satisfies all the rotations. test with threshold of 10 degrees, which satisfies all the rotations. returns angle in degrees returns angle in degrees x-axis of i2 points along y in world frame At (0, 1, 0) in i2 frame, rotation of i1 is irrelevant here. Estimated relative translation of i1 in i2 frame is (0, 1, 0), and the measurement in i2 frame is (1, 0, 0). Expected angle between the two is 90 degrees. `a` frame is the target/reference frame `b` frame contains the estimates indices 0 and 2 should still have no estimated pose, even after alignment identity alignment should preserve poses, should still match GT/targets at indices 1 and 3
1,977
en
0.884028
#!/usr/bin/python """Code for backing up pictures and videos captured to Dropbox""" import sys import os import glob from os import listdir from os.path import isfile, join import subprocess from Adafruit_IO import Client, RequestError import base64 __location__ = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__))) def main(args): """Main function""" if args: folder_path = args[0] else: folder_path = '/var/lib/motion' if not os.path.exists(folder_path): print "Folder Path: " + folder_path + " doesn't exist, exiting." raise ValueError("Incorrect Parameters") #Get the camera name. Default to visible with open(os.path.join(__location__, 'camera_name.cfg'), 'r') as f: print("open") camera_name = f.read().strip() print("Camera name: " + camera_name) aio = Client('ryhajlo', 'b5fe0936d9a84629a2d49cd45858fc67') # Start handling pictures videos = get_videos(folder_path) pictures = get_pictures(folder_path) if pictures: # Upload the files to dropbox # Build our command to upload files command = [] command.append('/home/pi/Dropbox-Uploader/dropbox_uploader.sh') command.append('upload') for picture in pictures: print "Will upload: " + picture command.append(picture) command.append('/camera/pictures/' + camera_name + '/') subprocess.call(command) print "Finished uploading pictures" # Do the same for videos command = [] command.append('/home/pi/Dropbox-Uploader/dropbox_uploader.sh') command.append('upload') for video in videos: print "Will upload: " + video command.append(video) command.append('/camera/videos/' + camera_name + '/') subprocess.call(command) print "Finished uploading videos" command = [] command.append("mogrify") command.append("-resize") command.append("320x240") command.append("/var/lib/motion/*.jpg") subprocess.call(command) latest_picture = max(pictures, key=os.path.getctime) print "The latest picture is: " + latest_picture with open(latest_picture, "rb") as imageFile: image_str = base64.b64encode(imageFile.read()) print "Uploading latest to Adafruit IO" feed_name = 'pic-' + camera_name print("Feed Name: " + feed_name) aio.send(feed_name, image_str ) print "Finished uploading to Adafruit IO" else: latest_picture = None print "No pictures" # Now that everything is uploaded, delete it all for picture in pictures: print "Deleting " + picture os.remove(picture) for video in videos: print "Deleting " + video os.remove(video) def get_videos(folder_path): videos = glob.glob(join(folder_path, '*.avi')) return videos def get_pictures(folder_path): print "Grabbing files from " + folder_path # Get the list of files in that directory pictures = glob.glob(join(folder_path, '2*.jpg')) # Use the leading 2 to prevent us from getting 'latest.jpg' latest_picture = max(pictures, key=os.path.getctime) return pictures if __name__ == "__main__": main(sys.argv[1:])
camera_backup.py
3,349
!/usr/bin/pythonGet the camera name. Default to visible Start handling pictures Upload the files to dropbox Build our command to upload files Do the same for videos Now that everything is uploaded, delete it all Get the list of files in that directory Use the leading 2 to prevent us from getting 'latest.jpg'
309
en
0.851123
#!/usr/bin/env python3 ''' Edit gizmo snapshot files: compress, delete, transfer across machines. @author: Andrew Wetzel <arwetzel@gmail.com> ''' # system ---- from __future__ import absolute_import, division, print_function # python 2 compatability import os import sys import glob import numpy as np # local ---- import utilities as ut from gizmo_analysis import gizmo_io # default subset of snapshots (65 snapshots) snapshot_indices_keep = [ 0, # z = 99 20, 26, 33, 41, 52, # z = 10 - 6 55, 57, 60, 64, 67, # z = 5.8 - 5.0 71, 75, 79, 83, 88, # z = 4.8 - 4.0 91, 93, 96, 99, 102, 105, 109, 112, 116, 120, # z = 3.9 - 3.0 124, 128, 133, 137, 142, 148, 153, 159, 165, 172, # z = 2.9 - 2.0 179, 187, 195, 204, 214, 225, 236, 248, 262, 277, # z = 1.9 - 1.0 294, 312, 332, 356, 382, 412, 446, 486, 534, # z = 0.9 - 0.1 539, 544, 550, 555, 561, 567, 573, 579, 585, # z = 0.09 - 0.01 600 ] #=================================================================================================== # compress files #=================================================================================================== class CompressClass(ut.io.SayClass): def compress_snapshots( self, directory='output', directory_out='', snapshot_index_limits=[0, 600], thread_number=1): ''' Compress all snapshots in input directory. Parameters ---------- directory : str : directory of snapshots directory_out : str : directory to write compressed snapshots snapshot_index_limits : list : min and max snapshot indices to compress syncronize : bool : whether to synchronize parallel tasks, wait for each thread bundle to complete before starting new bundle ''' snapshot_indices = np.arange(snapshot_index_limits[0], snapshot_index_limits[1] + 1) args_list = [(directory, directory_out, snapshot_index) for snapshot_index in snapshot_indices] ut.io.run_in_parallel(self.compress_snapshot, args_list, thread_number=thread_number) def compress_snapshot( self, directory='output', directory_out='', snapshot_index=600, analysis_directory='~/analysis', python_executable='python3'): ''' Compress single snapshot (which may be multiple files) in input directory. Parameters ---------- directory : str : directory of snapshot directory_out : str : directory to write compressed snapshot snapshot_index : int : index of snapshot analysis_directory : str : directory of analysis code ''' executable = '{} {}/manipulate_hdf5/compactify_hdf5.py -L 0'.format( python_executable, analysis_directory) snapshot_name_base = 'snap*_{:03d}*' if directory[-1] != '/': directory += '/' if directory_out and directory_out[-1] != '/': directory_out += '/' path_file_names = glob.glob(directory + snapshot_name_base.format(snapshot_index)) if len(path_file_names): if 'snapdir' in path_file_names[0]: path_file_names = glob.glob(path_file_names[0] + '/*') path_file_names.sort() for path_file_name in path_file_names: if directory_out: path_file_name_out = path_file_name.replace(directory, directory_out) else: path_file_name_out = path_file_name executable_i = '{} -o {} {}'.format(executable, path_file_name_out, path_file_name) self.say('executing: {}'.format(executable_i)) os.system(executable_i) def test_compression( self, snapshot_indices='all', simulation_directory='.', snapshot_directory='output', compression_level=0): ''' Read headers from all snapshot files in simulation_directory to check whether files have been compressed. ''' header_compression_name = 'compression.level' simulation_directory = ut.io.get_path(simulation_directory) snapshot_directory = ut.io.get_path(snapshot_directory) Read = gizmo_io.ReadClass() compression_wrong_snapshots = [] compression_none_snapshots = [] if snapshot_indices is None or snapshot_indices == 'all': _path_file_names, snapshot_indices = Read.get_snapshot_file_names_indices( simulation_directory + snapshot_directory) elif np.isscalar(snapshot_indices): snapshot_indices = [snapshot_indices] for snapshot_index in snapshot_indices: header = Read.read_header('index', snapshot_index, simulation_directory, verbose=False) if header_compression_name in header: if (compression_level is not None and header[header_compression_name] != compression_level): compression_wrong_snapshots.append(snapshot_index) else: compression_none_snapshots.append(snapshot_index) self.say('* tested {} snapshots: {} - {}'.format( len(snapshot_indices), min(snapshot_indices), max(snapshot_indices))) self.say('* {} are uncompressed'.format(len(compression_none_snapshots))) if len(compression_none_snapshots): self.say('{}'.format(compression_none_snapshots)) self.say('* {} have wrong compression (level != {})'.format( len(compression_wrong_snapshots), compression_level)) if len(compression_wrong_snapshots): self.say('{}'.format(compression_wrong_snapshots)) Compress = CompressClass() #=================================================================================================== # transfer files via globus #=================================================================================================== class GlobusClass(ut.io.SayClass): def submit_transfer( self, simulation_path_directory='.', snapshot_directory='output', batch_file_name='globus_batch.txt', machine_name='peloton'): ''' Submit globus transfer of simulation files. Must initiate from Stampede. Parameters ---------- simulation_path_directory : str : '.' or full path + directory of simulation snapshot_directory : str : directory of snapshot files within simulation_directory batch_file_name : str : name of file to write machine_name : str : name of machine transfering files to ''' # set directory from which to transfer simulation_path_directory = ut.io.get_path(simulation_path_directory) if simulation_path_directory == './': simulation_path_directory = os.getcwd() if simulation_path_directory[-1] != '/': simulation_path_directory += '/' command = 'globus transfer $(globus bookmark show stampede){}'.format( simulation_path_directory[1:]) # preceeding '/' already in globus bookmark path_directories = simulation_path_directory.split('/') simulation_directory = path_directories[-2] # parse machine + directory to transfer to if machine_name == 'peloton': if 'elvis' in simulation_directory: directory_to = 'm12_elvis' else: directory_to = simulation_directory.split('_')[0] directory_to += '/' + simulation_directory + '/' command += ' $(globus bookmark show peloton-scratch){}'.format(directory_to) # set globus parameters command += ' --sync-level=checksum --preserve-mtime --verify-checksum' command += ' --label "{}" --batch < {}'.format(simulation_directory, batch_file_name) # write globus batch file self.write_batch_file(simulation_path_directory, snapshot_directory, batch_file_name) self.say('* executing:\n{}\n'.format(command)) os.system(command) def write_batch_file( self, simulation_directory='.', snapshot_directory='output', file_name='globus_batch.txt'): ''' Write batch file that sets files to transfer via globus. Parameters ---------- simulation_directory : str : directory of simulation snapshot_directory : str : directory of snapshot files within simulation_directory file_name : str : name of batch file to write ''' simulation_directory = ut.io.get_path(simulation_directory) snapshot_directory = ut.io.get_path(snapshot_directory) transfer_string = '' # general files transfer_items = [ 'gizmo/', 'gizmo_config.sh', 'gizmo_parameters.txt', 'gizmo_parameters.txt-usedvalues', 'gizmo.out.txt', 'snapshot_times.txt', 'notes.txt', 'track/', 'halo/rockstar_dm/catalog_hdf5/', ] for transfer_item in transfer_items: if os.path.exists(simulation_directory + transfer_item): command = '{} {}' if transfer_item[-1] == '/': transfer_item = transfer_item[:-1] command += ' --recursive' command = command.format(transfer_item, transfer_item) + '\n' transfer_string += command # initial condition files transfer_items = glob.glob(simulation_directory + 'initial_condition*/*') for transfer_item in transfer_items: if '.ics' not in transfer_item: transfer_item = transfer_item.replace(simulation_directory, '') command = '{} {}\n'.format(transfer_item, transfer_item) transfer_string += command # snapshot files for snapshot_index in snapshot_indices_keep: snapshot_name = '{}snapdir_{:03d}'.format(snapshot_directory, snapshot_index) if os.path.exists(simulation_directory + snapshot_name): snapshot_string = '{} {} --recursive\n'.format(snapshot_name, snapshot_name) transfer_string += snapshot_string snapshot_name = '{}snapshot_{:03d}.hdf5'.format(snapshot_directory, snapshot_index) if os.path.exists(simulation_directory + snapshot_name): snapshot_string = '{} {}\n'.format(snapshot_name, snapshot_name) transfer_string += snapshot_string with open(file_name, 'w') as file_out: file_out.write(transfer_string) Globus = GlobusClass() #=================================================================================================== # transfer files via rsync #=================================================================================================== def rsync_snapshots( machine_name, simulation_directory_from='', simulation_directory_to='.', snapshot_indices=snapshot_indices_keep): ''' Use rsync to copy snapshot file[s]. Parameters ---------- machine_name : str : 'pfe', 'stampede', 'bw', 'peloton' directory_from : str : directory to copy from directory_to : str : local directory to put snapshots snapshot_indices : int or list : index[s] of snapshots to transfer ''' snapshot_name_base = 'snap*_{:03d}*' directory_from = ut.io.get_path(simulation_directory_from) + 'output/' directory_to = ut.io.get_path(simulation_directory_to) + 'output/.' if np.isscalar(snapshot_indices): snapshot_indices = [snapshot_indices] snapshot_path_names = '' for snapshot_index in snapshot_indices: snapshot_path_names += ( directory_from + snapshot_name_base.format(snapshot_index) + ' ') command = 'rsync -ahvP --size-only ' command += '{}:"{}" {}'.format(machine_name, snapshot_path_names, directory_to) print('\n* executing:\n{}\n'.format(command)) os.system(command) def rsync_simulation_files( machine_name, directory_from='/oldscratch/projects/xsede/GalaxiesOnFIRE', directory_to='.'): ''' Use rsync to copy simulation files. Parameters ---------- machine_name : str : 'pfe', 'stampede', 'bw', 'peloton' directory_from : str : directory to copy from directory_to : str : directory to put files ''' excludes = [ 'output/', 'restartfiles/', 'ewald_spc_table_64_dbl.dat', 'spcool_tables/', 'TREECOOL', 'energy.txt', 'balance.txt', 'GasReturn.txt', 'HIIheating.txt', 'MomWinds.txt', 'SNeIIheating.txt', '*.ics', 'snapshot_scale-factors.txt', 'submit_gizmo*.py', '*.bin', '*.particles', '*.bak', '*.err', '*.pyc', '*.o', '*.pro', '*.perl', '.ipynb_checkpoints', '.slurm', '.DS_Store', '*~', '._*', '#*#', ] directory_from = machine_name + ':' + ut.io.get_path(directory_from) directory_to = ut.io.get_path(directory_to) command = 'rsync -ahvP --size-only ' arguments = '' for exclude in excludes: arguments += '--exclude="{}" '.format(exclude) command += arguments + directory_from + ' ' + directory_to + '.' print('\n* executing:\n{}\n'.format(command)) os.system(command) #=================================================================================================== # delete files #=================================================================================================== def delete_snapshots( snapshot_directory='output', snapshot_index_limits=[1, 599], delete_halos=False): ''' Delete all snapshots in given directory within snapshot_index_limits, except for those in snapshot_indices_keep list. Parameters ---------- snapshot_directory : str : directory of snapshots snapshot_index_limits : list : min and max snapshot indices to delete delete_halos : bool : whether to delete halo catalog files at same snapshot times ''' snapshot_name_base = 'snap*_{:03d}*' if not snapshot_directory: snapshot_directory = 'output/' halo_name_base = 'halos_{:03d}*' halo_directory = 'halo/rockstar_dm/catalog/' if snapshot_directory[-1] != '/': snapshot_directory += '/' if snapshot_index_limits is None or not len(snapshot_index_limits): snapshot_index_limits = [1, 599] snapshot_indices = np.arange(snapshot_index_limits[0], snapshot_index_limits[1] + 1) print() for snapshot_index in snapshot_indices: if snapshot_index not in snapshot_indices_keep: snapshot_name = snapshot_directory + snapshot_name_base.format(snapshot_index) print('* deleting: {}'.format(snapshot_name)) os.system('rm -rf {}'.format(snapshot_name)) if delete_halos: halo_name = halo_directory + halo_name_base.format(snapshot_index) print('* deleting: {}'.format(halo_name)) os.system('rm -rf {}'.format(halo_name)) print() #=================================================================================================== # running from command line #=================================================================================================== if __name__ == '__main__': if len(sys.argv) <= 1: raise OSError('specify function to run: compress, globus, rsync, delete') function_kind = str(sys.argv[1]) assert ('compress' in function_kind or 'rsync' in function_kind or 'globus' in function_kind or 'delete' in function_kind) if 'compress' in function_kind: directory = 'output' if len(sys.argv) > 2: directory = str(sys.argv[2]) snapshot_index_max = 600 if len(sys.argv) > 3: snapshot_index_max = int(sys.argv[3]) snapshot_index_limits = [0, snapshot_index_max] Compress.compress_snapshots(directory, snapshot_index_limits=snapshot_index_limits) elif 'globus' in function_kind: directory = '.' if len(sys.argv) > 2: directory = str(sys.argv[2]) Globus.submit_transfer(directory) elif 'rsync' in function_kind: if len(sys.argv) < 5: raise OSError( 'imports: machine_name simulation_directory_from simulation_directory_to') machine_name = str(sys.argv[2]) simulation_directory_from = str(sys.argv[3]) simulation_directory_to = str(sys.argv[4]) rsync_simulation_files(machine_name, simulation_directory_from, simulation_directory_to) rsync_snapshots(machine_name, simulation_directory_from, simulation_directory_to) elif 'delete' in function_kind: directory = 'output' if len(sys.argv) > 3: directory = str(sys.argv[3]) snapshot_index_limits = None if len(sys.argv) > 4: snapshot_index_limits = [int(sys.argv[4]), int(sys.argv[5])] delete_snapshots(directory, snapshot_index_limits)
students_final_projects/group-f/gizmo_analysis/gizmo_file.py
17,139
Compress single snapshot (which may be multiple files) in input directory. Parameters ---------- directory : str : directory of snapshot directory_out : str : directory to write compressed snapshot snapshot_index : int : index of snapshot analysis_directory : str : directory of analysis code Compress all snapshots in input directory. Parameters ---------- directory : str : directory of snapshots directory_out : str : directory to write compressed snapshots snapshot_index_limits : list : min and max snapshot indices to compress syncronize : bool : whether to synchronize parallel tasks, wait for each thread bundle to complete before starting new bundle Delete all snapshots in given directory within snapshot_index_limits, except for those in snapshot_indices_keep list. Parameters ---------- snapshot_directory : str : directory of snapshots snapshot_index_limits : list : min and max snapshot indices to delete delete_halos : bool : whether to delete halo catalog files at same snapshot times Use rsync to copy simulation files. Parameters ---------- machine_name : str : 'pfe', 'stampede', 'bw', 'peloton' directory_from : str : directory to copy from directory_to : str : directory to put files Use rsync to copy snapshot file[s]. Parameters ---------- machine_name : str : 'pfe', 'stampede', 'bw', 'peloton' directory_from : str : directory to copy from directory_to : str : local directory to put snapshots snapshot_indices : int or list : index[s] of snapshots to transfer Submit globus transfer of simulation files. Must initiate from Stampede. Parameters ---------- simulation_path_directory : str : '.' or full path + directory of simulation snapshot_directory : str : directory of snapshot files within simulation_directory batch_file_name : str : name of file to write machine_name : str : name of machine transfering files to Read headers from all snapshot files in simulation_directory to check whether files have been compressed. Write batch file that sets files to transfer via globus. Parameters ---------- simulation_directory : str : directory of simulation snapshot_directory : str : directory of snapshot files within simulation_directory file_name : str : name of batch file to write Edit gizmo snapshot files: compress, delete, transfer across machines. @author: Andrew Wetzel <arwetzel@gmail.com> !/usr/bin/env python3 system ---- python 2 compatability local ---- default subset of snapshots (65 snapshots) z = 99 z = 10 - 6 z = 5.8 - 5.0 z = 4.8 - 4.0 z = 3.9 - 3.0 z = 2.9 - 2.0 z = 1.9 - 1.0 z = 0.9 - 0.1 z = 0.09 - 0.01=================================================================================================== compress files====================================================================================================================================================================================================== transfer files via globus=================================================================================================== set directory from which to transfer preceeding '/' already in globus bookmark parse machine + directory to transfer to set globus parameters write globus batch file general files initial condition files snapshot files=================================================================================================== transfer files via rsync====================================================================================================================================================================================================== delete files====================================================================================================================================================================================================== running from command line===================================================================================================
3,882
en
0.489593
import re from django import forms from django.contrib.admin.widgets import AdminFileWidget from django.forms.models import inlineformset_factory, BaseInlineFormSet from django.utils.translation import ugettext_lazy as _ from multi_email_field.forms import MultiEmailField from pycon.sponsorship.models import Sponsor, SponsorBenefit, SponsorLevel def strip(text): return u' '.join(text.strip().split()) class SponsorDetailsForm(forms.ModelForm): contact_emails = MultiEmailField( help_text=_(u"Please enter one email address per line.") ) class Meta: model = Sponsor fields = ["name", "contact_name", "contact_emails", "contact_phone", "contact_address", "external_url", "display_url", "twitter_username", "web_description", "web_logo", ] widgets = { 'web_description': forms.widgets.Textarea(attrs={'cols': 40, 'rows': 5}), } class SponsorApplicationForm(SponsorDetailsForm): class Meta(SponsorDetailsForm.Meta): fields = SponsorDetailsForm.Meta.fields + [ "level", "wants_table", "wants_booth", "small_entity_discount", ] help_texts = { 'web_description': strip( u""" Your description can be up to 100 words long. """ ), } def __init__(self, *args, **kwargs): self.user = kwargs.pop("user") kwargs.update({ "initial": { "contact_name": self.user.get_full_name(), "contact_emails": [self.user.email], } }) super(SponsorApplicationForm, self).__init__(*args, **kwargs) self.fields['level'].queryset = SponsorLevel.objects.exclude( available=False) def clean_web_description(self): value = self.cleaned_data['web_description'] word_count = len(re.findall(r"[-\w']+", value.lower())) if word_count > 100: raise forms.ValidationError( _(u"Your description is {} words long;" " please reduce it to 100 or less.".format(word_count)) ) return value def save(self, commit=True): obj = super(SponsorApplicationForm, self).save(commit=False) obj.applicant = self.user if commit: obj.save() return obj class SponsorBenefitsInlineFormSet(BaseInlineFormSet): def _construct_form(self, i, **kwargs): form = super(SponsorBenefitsInlineFormSet, self)._construct_form(i, **kwargs) # only include the relevant data fields for this benefit type fields = form.instance.data_fields() form.fields = dict((k, v) for (k, v) in form.fields.items() if k in fields + ["id"]) for field in fields: # don't need a label, the form template will label it with the benefit name form.fields[field].label = "" # provide word limit as help_text if form.instance.benefit.type in ["text", "richtext"] and form.instance.max_words: form.fields[field].help_text = u"maximum %s words" % form.instance.max_words # use admin file widget that shows currently uploaded file if field == "upload": form.fields[field].widget = AdminFileWidget() return form SponsorBenefitsFormSet = inlineformset_factory( Sponsor, SponsorBenefit, formset=SponsorBenefitsInlineFormSet, can_delete=False, extra=0, fields=["text", "upload"] ) class SponsorEmailForm(forms.Form): from_ = forms.EmailField(widget=forms.TextInput(attrs={'class': 'fullwidth-input'})) cc = forms.CharField(help_text=_(u"(comma-separated addresses)"), required=False, widget=forms.TextInput(attrs={'class': 'fullwidth-input'})) bcc = forms.CharField(help_text=_(u"(comma-separated addresses)"), required=False, widget=forms.TextInput(attrs={'class': 'fullwidth-input'})) subject = forms.CharField(widget=forms.TextInput(attrs={'class': 'fullwidth-input'})) body = forms.CharField(widget=forms.Textarea(attrs={'class': 'fullwidth-textarea'})) sample_subject = forms.CharField( required=False, widget=forms.TextInput(attrs={ 'class': 'fullwidth-input', 'readonly': True, 'style': 'background-color: #ddd', }), ) sample_body = forms.CharField( help_text=_(u""" You can keep editing the body and hitting Send until you love how this preview looks. Then, press Send one final time! """), required=False, widget=forms.Textarea(attrs={ 'class': 'fullwidth-textarea', 'readonly': True, 'style': 'background-color: #ddd', }), )
pycon/sponsorship/forms.py
5,094
only include the relevant data fields for this benefit type don't need a label, the form template will label it with the benefit name provide word limit as help_text use admin file widget that shows currently uploaded file
222
en
0.947151
import logging from common_lib.backend_service.generated.backend_service_v1_pb2_grpc import BackendServiceServicer import common_lib.backend_service.generated.service_types_v1_pb2 as service_messages from common_lib.grpc import GrpcExceptionHandler logger = logging.getLogger(__name__) class BackendService(BackendServiceServicer): def __init__(self): """ Initialise service and configuration """ logger.info("Initialised Backend-Service - Ready for gRPC Calls.") ################################################################################ # RPC Methods ################################################################################ def Greet(self, request, context): try: name = request.name if request.name else None with_error = request.withError logger.info(f"Received Greet request with name={name} and withError={with_error}") reply = f"Hello {name}!!" return service_messages.GreetResult( reply=reply ) except Exception as e: code, details = GrpcExceptionHandler.toGrpcError(e) context.set_code(code) context.set_details(details) return service_messages.GreetResult()
backend-service/backend_service/service.py
1,295
Initialise service and configuration RPC Methods
50
en
0.547045
# A simple script to generate fake data import sys, random, math, json import datetime as dt USAGE = 'Usage: python make_fake_fixtures.py [num_of_members] [num_of_games] [num_of_tournaments]' # Fake Names: First and Last GIVEN_NAMES = [ 'bruce', 'malcolm', 'kobe', 'peter', 'kaylee', 'inara' ] LAST_NAMES = [ 'lee', 'reynolds', 'bryant', 'parker', 'frye', 'serra' ] # Misc Chapter Codes CHAPTER_CODES = [ 'FFLY', 'NBAG', 'SJGC', 'TPGC', None] CHAPTER_NAMES = [ 'Fire Fly Go Club', 'NBA Go Club', 'San Jose Go Club', 'Tampa Go Club', None ] # State Names and City Names STATE_CODES = [ 'CA', 'OR', 'NY', 'AZ', 'AR', 'FL', 'KS', 'KY', 'IA' ] CITY_NAMES = [ 'Aurora', 'Austin', 'Boston', 'Chandler', 'Charlotte', 'Dallas', 'Dayton', 'Eugene' ] # Country Names and Codes COUNTRY_NAMES = [ 'United States', 'Canada', 'Japan', 'Korea', 'China', 'Tywain' ] COUNTRY_CODES = [ 'US', 'CA', 'JP', 'KO', 'CH', 'TW' ] #these, oddly, are not the FK in the member table. # Membership Status Codes STATUS_CODES = [ 'accepted' ] # Membership Types MEMBERSHIP_TYPES = ['Full', 'Sustainer', 'Sponser', 'Lifetime', 'E-Journal'] if len(sys.argv) != 4: print( USAGE ) quit() try: member_count = int(sys.argv[1]) game_count = int(sys.argv[2]) tourney_count = int(sys.argv[3]) except ValueError: print( USAGE ) quit() member_ids = [x for x in range(1, member_count+1)] tournament_ids = ['T%s' % x for x in range(1, tourney_count+1)] members = [] players = [] for member_id in member_ids: date = dt.date.today() - dt.timedelta(days = random.randint(2,150)) join_date = date - dt.timedelta(days = 150) renewal_due = date + dt.timedelta(days = random.randint(2,720)) first_name = random.choice(GIVEN_NAMES) last_name = random.choice(LAST_NAMES) members.append({ 'pk': member_id, 'model': 'agagd_core.member', 'fields': { 'member_id': member_id, 'legacy_id': random.choice(range(1, member_count+1)), 'full_name': '%s %s' % (first_name, last_name), 'given_names': first_name, 'family_name': last_name, 'join_date': join_date.strftime("%Y-%m-%d"), 'renewal_due': renewal_due.strftime("%Y-%m-%d"), 'city': 'Seattle', 'state': random.choice(STATE_CODES), 'status': random.choice(STATUS_CODES), 'region': 'some region', 'country': random.choice(COUNTRY_NAMES), 'chapter': random.choice(CHAPTER_CODES), 'chapter_id': random.choice(range(1, len(CHAPTER_CODES)+1)), 'occupation': '', 'citizen': random.choice(range(0, 1)), 'password': 'hallo!', 'type': random.choice(MEMBERSHIP_TYPES), 'last_changed': date.strftime("%Y-%m-%d") } }) players.append({ 'pk': member_id, 'model': 'agagd_core.players', 'fields': { 'elab_date': date.strftime("%Y-%m-%d"), 'name': first_name, 'last_name': last_name, 'rating': random.uniform(-15, 10), 'sigma': random.random() } }) ratings = [] ratings_range = list(range(0, 25)) for member_id in member_ids: for rating_id in ratings_range: elab_date = dt.date.today() - dt.timedelta(days = random.randint(2,20)) player_rating = players[member_id-1]['fields']['rating'] player_low_rating = player_rating - random.randint(0, 3) player_high_rating = player_rating - random.randint(0, 3) ratings.append({ 'pk': None, 'model': 'agagd_core.rating', 'fields': { 'pin_player': member_id, 'elab_date': elab_date.strftime("%Y-%m-%d"), 'rating': random.uniform(player_low_rating, player_high_rating), 'tournament': random.choice(tournament_ids), 'sigma': random.random() } }) tournaments = [] for tourney_id in tournament_ids: date = dt.date.today() - dt.timedelta(days = random.randint(2,20)) elab_date = date + dt.timedelta(days = 7) random_state = random.choice(STATE_CODES) tournaments.append({ 'pk': tourney_id, 'model': 'agagd_core.tournament', 'fields': { 'total_players': random.randint(4,20), 'city': random.choice(CITY_NAMES), 'elab_date': elab_date.strftime("%Y-%m-%d"), 'description': random_state + tourney_id, 'wall_list': "1: Mal, Bruce 2d 2+/w0 3+/w0 4+/w0 3-0-0\n" "2: Lee, Parker 1d 1-/b2 4+/w0 3-/w0 1-2-0\n" "3: Lee, Matt 1k 4-/w0 1-/b6 2+/b4 1-2-0\n" "4: Frye, Sam 3k 3+/b2 2-/b6 1-/b8 1-2-0\n" "Note: This is not generated by the AGAGD.", 'state': random_state, 'rounds': random.randint(2,5), 'tournament_date': date.strftime("%Y-%m-%d") } }) games = [] for game_id in range(1, game_count+1): p1 = random.choice(member_ids) p2 = random.choice([member_id for member_id in member_ids if member_id != p1]) color_1 = random.choice(['B', 'W']) color_2 = 'B' if color_1 != 'B' else 'W' date = dt.date.today() - dt.timedelta(days = random.randint(2,20)) elab_date = date + dt.timedelta(days = 7) games.append({ 'pk': game_id, 'model': 'agagd_core.game', 'fields': { 'pin_player_2': p2, 'tournament_code': random.choice(tournaments)['pk'], 'rated': random.randint(0, 1), 'elab_date': elab_date.strftime("%Y-%m-%d"), 'handicap': random.randint(0, 9), 'online': random.randint(0, 1), 'color_2': color_2, 'sgf_code': '', 'komi': random.randint(0, 9), 'pin_player_1': p1, 'rank_1': '', 'result': random.choice(['B', 'W']), 'rank_2': '', 'game_date': date.strftime("%Y-%m-%d"), 'exclude': random.randint(0,1), 'round': random.randint(2,5), 'color_1': color_1 } }) chapters = [] for member_id in range(0, len(CHAPTER_CODES)): chapters.append({ 'pk': member_id+1, 'model': 'agagd_core.chapters', 'fields': { 'member_id': member_id+1, 'code': CHAPTER_CODES[member_id], 'name': CHAPTER_NAMES[member_id], 'contact_text': random.choice(['Some contact info would go here.', '']), 'contact': 'Some guy', 'meeting_city': 'Seattle', 'url': 'www.localhost-is-best-host.com', 'display': random.randint(0, 1) } }) countries = [] for i, count_name in enumerate(COUNTRY_NAMES): countries.append({ 'pk': i, 'model': 'agagd_core.country', 'fields': { 'country_code': random.choice(COUNTRY_CODES), 'country_descr': count_name, } }) print( json.dumps(members + players + ratings + tournaments + games + chapters + countries, indent=4) )
scripts/make_fake_fixtures.py
7,344
A simple script to generate fake data Fake Names: First and Last Misc Chapter Codes State Names and City Names Country Names and Codesthese, oddly, are not the FK in the member table. Membership Status Codes Membership Types
224
en
0.837973
# Generated by Django 2.2.7 on 2019-11-05 22:35 import django.db.models.deletion from django.conf import settings from django.db import migrations, models import orders.models import orders.fields class Migration(migrations.Migration): initial = True dependencies = [ ('courses', '0002_CourseGenitiveName'), migrations.swappable_dependency(settings.AUTH_USER_MODEL), ] operations = [ migrations.CreateModel( name='Order', fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('created', models.DateTimeField(auto_now_add=True, db_index=True)), ('modified', models.DateTimeField(blank=True, db_index=True, null=True)), ('price', models.DecimalField(decimal_places=2, max_digits=9)), ('paid', models.DateTimeField('Date when order got paid', null=True, blank=True, help_text='If set during creation, order automaticaly gets shipped')), ('shipped', models.DateTimeField('Date when order was shipped', null=True, blank=True)), ('course', orders.fields.ItemField(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to='courses.Course')), ('record', orders.fields.ItemField(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to='courses.Record')), ('user', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL)), ], options={ 'abstract': False, }, ), ]
src/orders/migrations/0001_initial.py
1,652
Generated by Django 2.2.7 on 2019-11-05 22:35
45
en
0.683343
#!/usr/bin/python3 # # Copyright (c) Siemens AG, 2020 # tiago.gasiba@gmail.com # # SPDX-License-Identifier: MIT # import subprocess import results import pexpect import shutil import util import sys import re import os print("Start Test") # make clean ret = subprocess.run(["make clean"], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) # make main ret = subprocess.run(["make main"], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout = ret.stdout.decode("utf-8") ########################################################################################################################################## # EXP15-C. Do not place a semicolon on the same line as an if, for, or while statement # https://wiki.sei.cmu.edu/confluence/display/c/EXP15-C.+Do+not+place+a+semicolon+on+the+same+line+as+an+if%2C+for%2C+or+while+statement # Fix the bug in line "for (; ii<strlen(str1)-1; ii++); {" of utilities.c ########################################################################################################################################## # # Line to search for: # for (; ii<strlen(str1)-1; ii++); { nHits = util.searchSource("utilities.c.pp","^\s*for.*;\s*{\s*$") if nHits>0: util.addFinding("Program does not behave as expected",0,"INCREMENTAL_2_FUNC_1362465447_","TEST_100001") util.dumpFindings() # run analysis ret = subprocess.run(["./analyse.py"], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) # run AI ret = subprocess.run(["./ai.py"], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) sys.exit(0) os.remove("pizzas.txt") shutil.copyfile("pizzas.ok.txt","pizzas.txt") p = pexpect.spawn("./run_jail.sh ./main") p.logfile = sys.stdout.buffer p.sendline("help") p.sendline("napoli") p.sendline("crudo") p.sendline("view") p.sendline("checkout") p.sendline("carbonara") p.sendline("napoli") p.sendline("romana") p.sendline("checkout") p.sendline("view") p.sendline("checkout") p.sendline("exit") try: p.expect("Thank you and goodbye!",timeout=1) except: p.kill(9) B = p.before.decode("utf-8") # with open("expect_result.txt","w+") as f: # f.write(B) # print("'"+p.before.decode("utf-8")+"'" ) # p.expect(pexpect.EOF) expectResult = "" with open("expect_result.txt","rb") as f: l = f.read() expectResult = l.decode("utf-8") print(expectResult) okFail = 1 if (expectResult==B) else 0 util.addFinding("Program is behaving as expected",okFail,"","TEST_900001") ########################################################################################################################################## # EXP02-C. Be aware of the short-circuit behavior of the logical AND and OR operators # https://wiki.sei.cmu.edu/confluence/display/c/EXP02-C.+Be+aware+of+the+short-circuit+behavior+of+the+logical+AND+and+OR+operators # make sure that the participant rewrites the (1==nOrder) && printf as an if statement ########################################################################################################################################## # # Lines to search for: # (1==nOrder) && printf("You have ordered 1 pizza.\n"); # (1<nOrder) && printf("You have ordered %d pizzas.\n",nOrder); # nHits = util.searchSource("main.c.pp","^\s*\(1==nOrder\)\s*&&\s*printf") okFail = 1 if nHits>0: okFail = 0 else: nHits = util.searchSource("main.c.pp","^\s*\(1<nOrder\)\s*&&\s*printf") if nHits>0: okFail = 0 else: okFail = 1 util.addFinding("Bad coding style present in the code",okFail,"NO_TAG","TEST_101001") ########################################################################################################################################## # STR05-C. Use pointers to const when referring to string literals # https://wiki.sei.cmu.edu/confluence/display/c/STR05-C.+Use+pointers+to+const+when+referring+to+string+literals # make sure the participant adds "const" to the string "pizzaFileName" ########################################################################################################################################## # Lines to for: # char *pizzaFileName = "pizzas.txt"; # nHits = util.searchSource("pizza.c.pp","^\s*const\s+char\s*\*\s*pizzaFileName\s*=\s*\"pizzas\.txt\"\s*;\s*$") okFail = 0 if (nHits==0) else 1; util.addFinding("Watch out for string literals",okFail,"NO_TAG","TEST_102001") ########################################################################################################################################## # ERR01-C. Use ferror() rather than errno to check for FILE stream errors # https://wiki.sei.cmu.edu/confluence/display/c/ERR01-C.+Use+ferror%28%29+rather+than+errno+to+check+for+FILE+stream+errors # remove any checks with errno ########################################################################################################################################## # Lines to for: # if (errno!=0) { # nHits = util.searchSource("pizza.c.pp","^\s*if\s*\(\s*errno") okFail = 1 if (nHits==0) else 0; util.addFinding("Using outdated error checking mechanism",okFail,"NO_TAG","TEST_103001") ########################################################################################################################################## # ERR07-C. Prefer functions that support error checking over equivalent functions that don't # https://wiki.sei.cmu.edu/confluence/display/c/ERR07-C.+Prefer+functions+that+support+error+checking+over+equivalent+functions+that+don%27t # Developer should not use atoi() ########################################################################################################################################## nHits = util.searchSource("pizza.c.pp","^.*=\s*atoi\s*\(pizzaCost\s*\)\s*;\s*$") okFail = 1 if (nHits==0) else 0; util.addFinding("Some possible errors are not being checked",okFail,"NO_TAG","TEST_104001") ########################################################################################################################################## # INT08-C. Verify that all integer values are in range # https://wiki.sei.cmu.edu/confluence/display/c/INT08-C.+Verify+that+all+integer+values+are+in+range # Make sure the user handles potential integer overflow when addind a pizza to the basket # How to check: replace cost in pizzas.txt with large amount and order two pizzas. the program should not "crash" ########################################################################################################################################## stdout = ret.stdout.decode("utf-8") os.remove("pizzas.txt") shutil.copyfile("pizzas.bad.txt","pizzas.txt") p = pexpect.spawn("./run_jail.sh ./main") p.logfile = sys.stdout.buffer p.sendline("margherita") p.sendline("margherita") p.sendline("view") p.sendline("checkout") p.sendline("exit") try: p.expect("Thank you and goodbye!",timeout=1) except: p.kill(9) B = p.before.decode("utf-8") hitFlag = 1 for line in B.split("\n"): # main.c:31:11: runtime error: signed integer overflow: 2147480047 + 2147480047 cannot be represented in type 'int' if re.search(r"runtime error:",line) and re.search(r"signed integer overflow",line): hitFlag = 0 util.addFinding("There are integer handling issues in this program",hitFlag,"NO_TAG","TEST_120001") ############################################ ############################################ ############################################ ############################################ ############################################ util.dumpFindings() # run analysis ret = subprocess.run(["./analyse.py"], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) # run AI ret = subprocess.run(["./ai.py"], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Challenges/C_CPP/0005_pizza/run.py
7,730
!/usr/bin/python3 Copyright (c) Siemens AG, 2020 tiago.gasiba@gmail.com SPDX-License-Identifier: MIT make clean make main EXP15-C. Do not place a semicolon on the same line as an if, for, or while statement https://wiki.sei.cmu.edu/confluence/display/c/EXP15-C.+Do+not+place+a+semicolon+on+the+same+line+as+an+if%2C+for%2C+or+while+statement Fix the bug in line "for (; ii<strlen(str1)-1; ii++); {" of utilities.c Line to search for: for (; ii<strlen(str1)-1; ii++); { run analysis run AI with open("expect_result.txt","w+") as f: f.write(B) print("'"+p.before.decode("utf-8")+"'" ) p.expect(pexpect.EOF) EXP02-C. Be aware of the short-circuit behavior of the logical AND and OR operators https://wiki.sei.cmu.edu/confluence/display/c/EXP02-C.+Be+aware+of+the+short-circuit+behavior+of+the+logical+AND+and+OR+operators make sure that the participant rewrites the (1==nOrder) && printf as an if statement Lines to search for: (1==nOrder) && printf("You have ordered 1 pizza.\n"); (1<nOrder) && printf("You have ordered %d pizzas.\n",nOrder); STR05-C. Use pointers to const when referring to string literals https://wiki.sei.cmu.edu/confluence/display/c/STR05-C.+Use+pointers+to+const+when+referring+to+string+literals make sure the participant adds "const" to the string "pizzaFileName" Lines to for: char *pizzaFileName = "pizzas.txt"; ERR01-C. Use ferror() rather than errno to check for FILE stream errors https://wiki.sei.cmu.edu/confluence/display/c/ERR01-C.+Use+ferror%28%29+rather+than+errno+to+check+for+FILE+stream+errors remove any checks with errno Lines to for: if (errno!=0) { ERR07-C. Prefer functions that support error checking over equivalent functions that don't https://wiki.sei.cmu.edu/confluence/display/c/ERR07-C.+Prefer+functions+that+support+error+checking+over+equivalent+functions+that+don%27t Developer should not use atoi() INT08-C. Verify that all integer values are in range https://wiki.sei.cmu.edu/confluence/display/c/INT08-C.+Verify+that+all+integer+values+are+in+range Make sure the user handles potential integer overflow when addind a pizza to the basket How to check: replace cost in pizzas.txt with large amount and order two pizzas. the program should not "crash" main.c:31:11: runtime error: signed integer overflow: 2147480047 + 2147480047 cannot be represented in type 'int' run analysis run AI
2,372
en
0.666846
# (C) Copyright 2012-2019 ECMWF. # # This software is licensed under the terms of the Apache Licence Version 2.0 # which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. # In applying this licence, ECMWF does not waive the privileges and immunities # granted to it by virtue of its status as an intergovernmental organisation nor # does it submit to any jurisdiction. import os import argparse from flask import Flask, request, Response, render_template, send_file, jsonify from .server import WMSServer from .plot.magics import Plotter, Styler from .data.fs import Availability application = Flask(__name__) demo = os.path.join(os.path.dirname(__file__), "testdata", "sfc.grib") demo = os.environ.get("SKINNYWMS_DATA_PATH", demo) parser = argparse.ArgumentParser(description="Simple WMS server") parser.add_argument( "-f", "--path", default=demo, help="Path to a GRIB or NetCDF file, or a directory\ containing GRIB and/or NetCDF files.", ) parser.add_argument( "--style", default="", help="Path to a directory where to find the styles" ) parser.add_argument( "--user_style", default="", help="Path to a json file containing the style to use" ) parser.add_argument("--host", default="127.0.0.1", help="Hostname") parser.add_argument("--port", default=5000, help="Port number") parser.add_argument( "--baselayer", default="", help="Path to a directory where to find the baselayer" ) parser.add_argument( "--magics-prefix", default="magics", help="prefix used to pass information to magics", ) args = parser.parse_args() if args.style != "": os.environ["MAGICS_STYLE_PATH"] = args.style + ":ecmwf" if args.user_style != "": os.environ["MAGICS_USER_STYLE_PATH"] = args.user_style server = WMSServer(Availability(args.path), Plotter(args.baselayer), Styler(args.user_style)) server.magics_prefix = args.magics_prefix @application.route("/wms", methods=["GET"]) def wms(): return server.process( request, Response=Response, send_file=send_file, render_template=render_template, reraise=True, ) @application.route("/availability", methods=["GET"]) def availability(): return jsonify(server.availability.as_dict()) @application.route("/", methods=["GET"]) def index(): return render_template("leaflet_demo.html") def execute(): application.run(port=args.port, host=args.host, debug=True, threaded=False)
skinnywms/wmssvr.py
2,472
(C) Copyright 2012-2019 ECMWF. This software is licensed under the terms of the Apache Licence Version 2.0 which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. In applying this licence, ECMWF does not waive the privileges and immunities granted to it by virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
367
en
0.926202
temporary_zones = { 'forest': { 'room_keys': [ 'a sacred grove of ancient wood', 'a sparsely-populated fledgling forest', 'a cluster of conifer trees' ] }, 'sewer': { 'room_keys': [ 'a poorly-maintained sewage tunnel', 'a sewer tunnel constructed of ancient brick', 'a wide walkway along sewage' ] }, 'cave': { 'room_keys': [ 'an uncertain rock bridge', 'a wide opening between', 'a damp rock shelf' ] }, 'alley': { 'room_keys': [ 'a dark alleyway', 'a filthy alleyway', 'a narrow alleyway' ] } } static_zones = { 'darkshire': [ { # Encampment 'coords': {'x': 0, 'y': 0}, 'exits': ['n'], 'key': "a makeshift encampment" }, { # Main Gate 'coords': {'x': 0, 'y': -1}, 'exits': ['n', 's'], 'key': "a main gate" }, { # Town Square - South 'coords': {'x': 0, 'y': -2}, 'exits': ['n', 'ne', 'e', 's', 'w', 'nw'], 'key': "a town square" }, { # Town Square - Southwest 'coords': {'x': -1, 'y': -2}, 'exits': ['n', 'ne', 'e'], 'key': "a town square" }, { # Town Square - Southeast 'coords': {'x': 1, 'y': -2}, 'exits': ['n', 'w', 'nw'], 'key': "a town square" }, { # Town Square - Middle 'coords': {'x': 0, 'y': -3}, 'exits': ['n', 'ne', 'e', 'se', 's', 'sw', 'w', 'nw'], 'key': "the center of a town square" }, { # Town Square - West 'coords': {'x': -1, 'y': -3}, 'exits': ['n', 'ne', 'e', 'se', 's'], 'key': "a town square" }, { # Town Square - East 'coords': {'x': 1, 'y': -3}, 'exits': ['n', 's', 'sw', 'w', 'nw'], 'key': "a town square" }, { # Town Square - North 'coords': {'x': 0, 'y': -4}, 'exits': ['e', 'se', 's', 'sw', 'w'], 'key': "a town square" }, { # Town Square - Northwest 'coords': {'x': -1, 'y': -4}, 'exits': ['e', 'se', 's'], 'key': "a town square" }, { # Town Square - Northeast 'coords': {'x': 1, 'y': -4}, 'exits': ['s', 'sw', 'w'], 'key': "a town square" } ], 'testing_zone': [ { # Hecate's Haven 'coords': {'x': 0, 'y': 0}, 'exits': ['n', 'e', 's', 'w'], 'key': "Hecate's Haven", 'desc': ("You are in a well-constructed room with walls made of red granite rock, " "masterfully crafted from various shapes. Aisles, featuring a worn-in deep crimson " "carpet, connect all four exits to the center of the room. Four large round " "sturdy oak tables, surrounded by various chairs, decorate the northern half of " "room. Dominating the southwest corner of the room is a well-worn heavy oak bar " "that runs from the western wall into a smooth-cornered turn to end against " "the southern wall and is surrounded by stools. " "In the southeast quarter, a lounge features a polished large black harpsichord " "tucked into the corner, adjacent to a raised limestone platform serving as a " "stage. Along the southern half of the middle aisle and against the eastern wall " "rests a heavily padded L-shaped couch. Just in front of the couch sits a " "low-lying table."), 'static_sentients': ['hoff'], 'tags': [('hecates_haven', 'rooms')] }, { # Shopper's Paradise 'coords': {'x': -1, 'y': 0}, 'exits': ['e', 's'], 'key': "Shopper's Paradise", 'desc': ("You are in a room stuffed to the ceiling with ridiculous amounts of " "merchandise. Running parallel against the western wall stands a magnificent " "translucent ruby counter, covered in racks and jars that house a plethora of " "curious wonders. Surrounding you are shelves and various containers filled with " "an assortment of objects and supplies. Volund, the proprietor of this " "establishment, appears busy with obsessively sorting and counting his inventory.") }, { # Back Alley 'coords': {'x': -1, 'y': 1}, 'exits': ['n', 'e'], 'key': "a dark alley behind a shop", 'desc': ("You are in a dark and dirty back alley. Discarded trash is strewn about. " "In the southwest corner you see a mucky ladder leading down into a sewer, " "emenating a vile stench to through-out the alley. A few belligerent thugs are " "causing a ruckus halfway down the alley.") }, { # Training Yard 'coords': {'x': 0, 'y': 1}, 'exits': ['n', 'e', 'w'], 'key': "a training yard", 'desc': ("You are at a training yard paved with cobblestones as walkways and dirt for " "fighting on. A thin layer of sand covers everything here. A never-ending supply " "of slaves are locked up in a cage against the southern wall. Several of the " "slaves are shackled to the southern wall, ready to fight and die. Benches run " "under an ivy-covered pergola along the northern wall. Combatants of various skill " "levels and weapons train here under the hot sun, watched over by the " "Doctore Oenomaus and several other trainers; each a master of different weapons.") }, { # Apothecary's Shop 'coords': {'x': 1, 'y': 1}, 'exits': ['n', 'w'], 'key': "Apothecary's Shop", 'desc': ("You are amongst a vast array of plants, spices, minerals, and animal " "products. Behind a counter crafted from flowery vines stands Kerra the apothecary. " "North of the counter are three rows of various plants. All along the southern wall " "hangs shelves that house a multitude of jars, each containing some form of animal " "product. The northwestern corner of the room is stacked high with spices, " "minerals, and cured plants. Various incense burners are hanging from the ceiling, " "filling the room with a sickly-sweet aroma that mixes with an earthy scent.") }, { # Kitchen 'coords': {'x': 1, 'y': 0}, 'exits': ['s', 'w'], 'key': "a large noisy kitchen", 'desc': ("You are inside a large noisy kitchen lined with white ceramic tiles. Busy " "people are hurriedly completing their tasks whilst communicating loudly. Pots and " "pans are hanging on hooks all along the walls. Four large ovens are stacked in a " "set of two against the northeast wall. A double sink is set in both the southeast " "and northwest corners of the room. Straight down the middle of the room rests a " "solid steel island, covered in various items being prepared. Ten stove-top burners " "line the eastern wall. Against the northern wall sets various bags of grain, rice, " "flour, and sugar. A whole cow hangs from the ceiling in the southwest corner, " "ready to be butchered.") }, { # Road 'coords': {'x': 0, 'y': -1}, 'exits': ['e', 's', 'w'], 'key': "a bustling cobblestone street", 'desc': ("You are surrounded by large swarths of people in every direction. Centered " "in the middle of the street is a towering bronze statue of 10 feet; featuring a " "heavily armored knight plunging his bastard sword into the chest of a kneeling " "opponent, red liquid continuously spurts out from the statue’s chest and splashes " "into a basin built into the foundation. Surrounding the statue are ornate marble " "benches, each about 8 feet in length. Above the inn’s doorway hangs a glass " "mosaic street lamp set alight by burning oil. Moving amongst the crowd are people " "from all walks of life; from peasants, prostitutes, and workers to lords, ladies, " "scholars, priests, knights, and hired guards.") }, ] }
rooms/zones.py
8,804
Encampment Main Gate Town Square - South Town Square - Southwest Town Square - Southeast Town Square - Middle Town Square - West Town Square - East Town Square - North Town Square - Northwest Town Square - Northeast Hecate's Haven Shopper's Paradise Back Alley Training Yard Apothecary's Shop Kitchen Road
305
en
0.910344
# Copyright 2016 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import os from setuptools import find_packages from setuptools import setup PACKAGE_ROOT = os.path.abspath(os.path.dirname(__file__)) with open(os.path.join(PACKAGE_ROOT, 'README.rst')) as file_obj: README = file_obj.read() # NOTE: This is duplicated throughout and we should try to # consolidate. SETUP_BASE = { 'author': 'Google Cloud Platform', 'author_email': 'jjg+google-cloud-python@google.com', 'scripts': [], 'url': 'https://github.com/GoogleCloudPlatform/google-cloud-python', 'license': 'Apache 2.0', 'platforms': 'Posix; MacOS X; Windows', 'include_package_data': True, 'zip_safe': False, 'classifiers': [ 'Development Status :: 4 - Beta', 'Intended Audience :: Developers', 'License :: OSI Approved :: Apache Software License', 'Operating System :: OS Independent', 'Programming Language :: Python :: 2', 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3', 'Programming Language :: Python :: 3.4', 'Programming Language :: Python :: 3.5', 'Topic :: Internet', ], } REQUIREMENTS = [ 'enum34', 'google-cloud-core >= 0.22.1, < 0.23dev', 'gapic-google-cloud-vision-v1 >= 0.14.0, < 0.15dev', ] setup( name='google-cloud-vision', version='0.22.0', description='Python Client for Google Cloud Vision', long_description=README, namespace_packages=[ 'google', 'google.cloud', ], packages=find_packages(), install_requires=REQUIREMENTS, **SETUP_BASE )
vision/setup.py
2,160
Copyright 2016 Google Inc. 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. NOTE: This is duplicated throughout and we should try to consolidate.
624
en
0.885157
"""Unit tests for the Nexus Parser """ from unittest import TestCase, main from cogent3 import load_aligned_seqs from cogent3.parse.nexus import ( MinimalNexusAlignParser, find_fields, get_BL_table, get_tree_info, parse_dnd, parse_nexus_tree, parse_PAUP_log, parse_taxa, parse_trans_table, split_tree_info, ) __author__ = "Catherine Lozupone" __copyright__ = "Copyright 2007-2019, The Cogent Project" __credits__ = ["Catherine Lozupone", "Rob Knight", "Micah Hamady"] __license__ = "BSD-3" __version__ = "2019.12.6a" __maintainer__ = "Catherine Lozupone" __email__ = "lozupone@colorado.edu" __status__ = "Production" Nexus_tree = """#NEXUS Begin trees; [Treefile saved Wednesday, May 5, 2004 5:02 PM] [! >Data file = Grassland_short.nex >Neighbor-joining search settings: > Ties (if encountered) will be broken systematically > Distance measure = Jukes-Cantor > (Tree is unrooted) ] Translate 1 outgroup25, 2 AF078391l, 3 AF078211af, 4 AF078393l, 5 AF078187af, 6 AF078320l, 7 AF078432l, 8 AF078290af, 9 AF078350l, 10 AF078356l, 11 AF078306af, 12 AF078429l, 13 AF078256af, 14 AF078443l, 15 AF078450l, 16 AF078452l, 17 AF078258af, 18 AF078380l, 19 AF078251af, 20 AF078179af, 21 outgroup258 ; tree PAUP_1 = [&R] (1,(2,(((3,4),(5,(((((6,10),9),(11,18)),((((7,15),19),17),(8,(12,(14,16))))),13))),20)),21); tree PAUP_2 = [&R] (1,(2,(((3,4),(5,(((((6,10),9),(11,18)),((((7,15),19),17),(8,(12,(14,16))))),13))),20)),21); End;""".split( "\n" ) Nexus_tree_2 = """#NEXUS Begin trees; [Treefile saved Wednesday, June 14, 2006 11:20 AM] [!>Neighbor-joining search settings: > Ties (if encountered) will be broken systematically > Distance measure = uncorrected ("p") > (Tree is unrooted) ] tree nj = [&U] ((((((((((YA10260L1:0.01855,SARAG06_Y:0.00367):0.01965,(((YA270L1G0:0.01095,SARAD10_Y:0.00699):0.01744,YA270L1A0:0.04329):0.00028,((YA165L1C1:0.01241,SARAA02_Y:0.02584):0.00213,((YA165L1H0:0.00092,SARAF10_Y:-0.00092):0.00250,(YA165L1A0:0.00177,SARAH10_Y:0.01226):0.00198):0.00131):0.00700):0.01111):0.11201,(YA160L1F0:0.00348,SARAG01_Y:-0.00122):0.13620):0.01202,((((YRM60L1D0:0.00357,(YRM60L1C0:0.00477,SARAE10_Y:-0.00035):0.00086):0.00092,SARAE03_Y:0.00126):0.00125,SARAC11_Y:0.00318):0.00160,YRM60L1H0:0.00593):0.09975):0.07088,SARAA01_Y:0.02880):0.00190,SARAB04_Y:0.05219):0.00563,YRM60L1E0:0.06099):0.00165,(YRM60L1H0:0.00450,SARAF11_Y:0.01839):0.00288):0.00129,YRM60L1B1:0.00713):0.00194,(YRM60L1G0:0.00990,(YA165L1G0:0.00576,(YA160L1G0:0.01226,SARAA11_Y:0.00389):0.00088):0.00300):0.00614,SARAC06_Y:0.00381); end;""".split( "\n" ) Nexus_tree_3 = """#NEXUS Begin trees; [Treefile saved Wednesday, May 5, 2004 5:02 PM] [! >Data file = Grassland_short.nex >Neighbor-joining search settings: > Ties (if encountered) will be broken systematically > Distance measure = Jukes-Cantor > (Tree is unrooted) ] Translate 1 outgroup25, 2 AF078391l, 3 'AF078211af', 4 AF078393l, 5 AF078187af, 6 AF078320l, 7 AF078432l, 8 AF078290af, 9 AF078350l, 10 AF078356l, 11 AF078306af, 12 AF078429l, 13 AF078256af, 14 'AF078443l', 15 AF078450l, 16 AF078452l, 17 AF078258af, 18 'AF078380l', 19 AF078251af, 20 AF078179af, 21 outgroup258 ; tree PAUP_1 = [&R] (1,(2,(((3,4),(5,(((((6,10),9),(11,18)),((((7,15),19),17),(8,(12,(14,16))))),13))),20)),21); tree PAUP_2 = [&R] (1,(2,(((3,4),(5,(((((6,10),9),(11,18)),((((7,15),19),17),(8,(12,(14,16))))),13))),20)),21); End;""".split( "\n" ) PAUP_log = """ P A U P * Version 4.0b10 for Macintosh (PPC/Altivec) Wednesday, May 5, 2004 5:03 PM This copy registered to: Scott Dawson UC-Berkeley (serial number = B400784) -----------------------------NOTICE----------------------------- This is a beta-test version. Please report any crashes, apparent calculation errors, or other anomalous results. There are no restrictions on publication of results obtained with this version, but you should check the WWW site frequently for bug announcements and/or updated versions. See the README file on the distribution media for details. ---------------------------------------------------------------- Tree description: Optimality criterion = parsimony Character-status summary: Of 500 total characters: All characters are of type 'unord' All characters have equal weight 253 characters are constant 109 variable characters are parsimony-uninformative Number of parsimony-informative characters = 138 Multistate taxa interpreted as uncertainty Character-state optimization: Accelerated transformation (ACCTRAN) AncStates = "standard" Tree number 1 (rooted using user-specified outgroup) Branch lengths and linkages for tree #1 Assigned Minimum Maximum Connected branch possible possible Node to node length length length ------------------------------------------------------------------------- 40 root 0 0 0 outgroup25 (1)* 40 40 24 52 39 40 57 15 72 AF078391l (2) 39 56 48 81 38 39 33 17 71 37 38 31 14 48 22 37 20 11 33 AF078211af (3) 22 4 2 7 AF078393l (4) 22 1 0 3 36 37 14 5 32 AF078187af (5) 36 18 10 28 35 36 21 16 45 34 35 10 3 23 26 34 5 3 9 24 26 4 3 13 23 24 0 0 3 AF078320l (6) 23 1 1 3 AF078356l (10) 23 2 2 2 AF078350l (9) 24 5 3 5 25 26 9 2 10 AF078306af (11) 25 6 4 10 AF078380l (18) 25 5 3 10 33 34 5 4 15 29 33 3 1 4 28 29 2 2 2 27 28 3 1 3 AF078432l (7) 27 2 2 2 AF078450l (15) 27 3 3 4 AF078251af (19) 28 6 6 7 AF078258af (17) 29 6 6 6 32 33 4 3 15 AF078290af (8) 32 9 8 11 31 32 9 6 18 AF078429l (12) 31 2 1 5 30 31 10 9 18 AF078443l (14) 30 2 1 6 AF078452l (16) 30 4 4 5 AF078256af (13) 35 4 1 6 AF078179af (20) 38 48 34 79 outgroup258 (21)* 40 45 27 67 ------------------------------------------------------------------------- Sum 509 Tree length = 509 Consistency index (CI) = 0.7151 Homoplasy index (HI) = 0.2849 """.split( "\n" ) line1 = " 40 root 0 0 0" line2 = "outgroup25 (1)* 40 40 24 52" line3 = " 39 40 57 15 72" line4 = "AF078391l (2) 39 56 48 81" class NexusParserTests(TestCase): """Tests of the Nexus Parser functions""" def test_parse_nexus_tree(self): """parse_nexus_tree returns a dnd string and a translation table list""" Trans_table, dnd = parse_nexus_tree(Nexus_tree) # check the full dendrogram string is returned self.assertEqual( dnd["tree PAUP_1"], "(1,(2,(((3,4),(5,(((((6,10),9),(11,18)),((((7,15),19),17),(8,(12,(14,16))))),13))),20)),21);", ) # check that all taxa are returned in the Trans_table self.assertEqual(Trans_table["1"], "outgroup25") self.assertEqual(Trans_table["2"], "AF078391l") self.assertEqual(Trans_table["3"], "AF078211af") self.assertEqual(Trans_table["4"], "AF078393l") self.assertEqual(Trans_table["5"], "AF078187af") self.assertEqual(Trans_table["6"], "AF078320l") self.assertEqual(Trans_table["21"], "outgroup258") self.assertEqual(Trans_table["20"], "AF078179af") self.assertEqual(Trans_table["19"], "AF078251af") # check that Nexus files without translation table work Trans_table, dnd = parse_nexus_tree(Nexus_tree_2) self.assertEqual(Trans_table, None) self.assertEqual( dnd["tree nj"], "((((((((((YA10260L1:0.01855,SARAG06_Y:0.00367):0.01965,(((YA270L1G0:0.01095,SARAD10_Y:0.00699):0.01744,YA270L1A0:0.04329):0.00028,((YA165L1C1:0.01241,SARAA02_Y:0.02584):0.00213,((YA165L1H0:0.00092,SARAF10_Y:-0.00092):0.00250,(YA165L1A0:0.00177,SARAH10_Y:0.01226):0.00198):0.00131):0.00700):0.01111):0.11201,(YA160L1F0:0.00348,SARAG01_Y:-0.00122):0.13620):0.01202,((((YRM60L1D0:0.00357,(YRM60L1C0:0.00477,SARAE10_Y:-0.00035):0.00086):0.00092,SARAE03_Y:0.00126):0.00125,SARAC11_Y:0.00318):0.00160,YRM60L1H0:0.00593):0.09975):0.07088,SARAA01_Y:0.02880):0.00190,SARAB04_Y:0.05219):0.00563,YRM60L1E0:0.06099):0.00165,(YRM60L1H0:0.00450,SARAF11_Y:0.01839):0.00288):0.00129,YRM60L1B1:0.00713):0.00194,(YRM60L1G0:0.00990,(YA165L1G0:0.00576,(YA160L1G0:0.01226,SARAA11_Y:0.00389):0.00088):0.00300):0.00614,SARAC06_Y:0.00381);", ) def test_parse_nexus_tree_sq(self): """remove single quotes from tree and translate tables""" Trans_table, dnd = parse_nexus_tree(Nexus_tree_3) # check the full dendrogram string is returned self.assertEqual( dnd["tree PAUP_1"], "(1,(2,(((3,4),(5,(((((6,10),9),(11,18)),((((7,15),19),17),(8,(12,(14,16))))),13))),20)),21);", ) # check that all taxa are returned in the Trans_table self.assertEqual(Trans_table["1"], "outgroup25") self.assertEqual(Trans_table["2"], "AF078391l") self.assertEqual(Trans_table["3"], "AF078211af") self.assertEqual(Trans_table["4"], "AF078393l") self.assertEqual(Trans_table["5"], "AF078187af") self.assertEqual(Trans_table["6"], "AF078320l") self.assertEqual(Trans_table["21"], "outgroup258") self.assertEqual(Trans_table["20"], "AF078179af") self.assertEqual(Trans_table["19"], "AF078251af") def test_get_tree_info(self): """get_tree_info returns the Nexus file section that describes the tree""" result = get_tree_info(Nexus_tree) self.assertEqual(len(result), 33) self.assertEqual( result[0], "Begin trees; [Treefile saved Wednesday, May 5, 2004 5:02 PM]" ) self.assertEqual( result[31], "tree PAUP_1 = [&R] (1,(2,(((3,4),(5,(((((6,10),9),(11,18)),((((7,15),19),17),(8,(12,(14,16))))),13))),20)),21);", ) def test_split_tree_info(self): """split_tree_info splits lines into header, Trans_table, and dnd""" tree_info = get_tree_info(Nexus_tree) header, trans_table, dnd = split_tree_info(tree_info) self.assertEqual(len(header), 9) self.assertEqual(len(trans_table), 22) self.assertEqual(len(dnd), 2) self.assertEqual( header[0], "Begin trees; [Treefile saved Wednesday, May 5, 2004 5:02 PM]" ) self.assertEqual(header[8], "\tTranslate") self.assertEqual(trans_table[0], "\t\t1 outgroup25,") self.assertEqual(trans_table[21], "\t\t;") self.assertEqual( dnd[0], "tree PAUP_1 = [&R] (1,(2,(((3,4),(5,(((((6,10),9),(11,18)),((((7,15),19),17),(8,(12,(14,16))))),13))),20)),21);", ) def test_parse_trans_table(self): """parse_trans_table returns a dict with the taxa names indexed by number""" tree_info = get_tree_info(Nexus_tree) header, trans_table, dnd = split_tree_info(tree_info) Trans_table = parse_trans_table(trans_table) self.assertEqual(len(Trans_table), 21) # check that taxa are returned in the Trans_table self.assertEqual(Trans_table["1"], "outgroup25") self.assertEqual(Trans_table["2"], "AF078391l") self.assertEqual(Trans_table["3"], "AF078211af") self.assertEqual(Trans_table["4"], "AF078393l") self.assertEqual(Trans_table["5"], "AF078187af") self.assertEqual(Trans_table["6"], "AF078320l") self.assertEqual(Trans_table["21"], "outgroup258") self.assertEqual(Trans_table["20"], "AF078179af") self.assertEqual(Trans_table["19"], "AF078251af") def test_parse_dnd(self): """parse_dnd returns a dict with dnd indexed by tree name""" tree_info = get_tree_info(Nexus_tree) header, trans_table, dnd = split_tree_info(tree_info) dnd_dict = parse_dnd(dnd) self.assertEqual( dnd_dict["tree PAUP_1"], "(1,(2,(((3,4),(5,(((((6,10),9),(11,18)),((((7,15),19),17),(8,(12,(14,16))))),13))),20)),21);", ) # ------------------------------------------------------ def test_get_BL_table(self): """get_BL_table returns the section of the log file w/ the BL table""" BL_table = get_BL_table(PAUP_log) self.assertEqual(len(BL_table), 40) self.assertEqual( BL_table[0], " 40 root 0 0 0", ) self.assertEqual( BL_table[39], "outgroup258 (21)* 40 45 27 67", ) def test_find_fields(self): """find_fields takes BL table line and returns field names mapped to info""" result = find_fields(line1) self.assertEqual(result["taxa"], "40") self.assertEqual(result["bl"], "0") self.assertEqual(result["parent"], "root") def test_parse_taxa(self): """parse_taxa should return the taxa # from a taxa_field from find_fields""" result1 = find_fields(line1) result2 = find_fields(line2) result3 = find_fields(line3) result4 = find_fields(line4) self.assertEqual(parse_taxa(result1["taxa"]), "40") self.assertEqual(parse_taxa(result2["taxa"]), "1") self.assertEqual(parse_taxa(result3["taxa"]), "39") self.assertEqual(parse_taxa(result4["taxa"]), "2") def test_parse_PAUP_log(self): """parse_PAUP_log extracts branch length info from a PAUP log file""" BL_dict = parse_PAUP_log(PAUP_log) self.assertEqual(len(BL_dict), 40) self.assertEqual(BL_dict["1"], ("40", 40)) self.assertEqual(BL_dict["40"], ("root", 0)) self.assertEqual(BL_dict["39"], ("40", 57)) self.assertEqual(BL_dict["2"], ("39", 56)) self.assertEqual(BL_dict["26"], ("34", 5)) self.assertEqual(BL_dict["21"], ("40", 45)) def test_align_with_comments(self): """correctly handle an alignment block containing comments""" parser = MinimalNexusAlignParser("data/nexus_comments.nex") got = {n: s for n, s in parser} expect = { "Ephedra": "TTAAGCCATGCATGTCTAAGTATGAACTAATTCCAAACGGTGA", "Gnetum": "TTAAGCCATGCATGTCTATGTACGAACTAATC-AGAACGGTGA", "Welwitschia": "TTAAGCCATGCACGTGTAAGTATGAACTAGTC-GAAACGGTGA", "Ginkgo": "TTAAGCCATGCATGTGTAAGTATGAACTCTTTACAGACTGTGA", "Pinus": "TTAAGCCATGCATGTCTAAGTATGAACTAATTGCAGACTGTGA", } self.assertEqual(got, expect) def test_align_with_spaced_seqs(self): """correctly handle an alignment block with spaces in seqs""" parser = MinimalNexusAlignParser("data/nexus_dna.nex") seqs = {n: s for n, s in parser} self.assertEqual(len(seqs), 10) # 10 taxa lengths = set(len(seqs[n]) for n in seqs) self.assertEqual(lengths, {705}) # all same length and equal 705 def test_align_from_mixed(self): """correctly handle a file with tree and alignment block""" parser = MinimalNexusAlignParser("data/nexus_mixed.nex") got = {n: s for n, s in parser} expect = { "fish": "ACATAGAGGGTACCTCTAAG", "frog": "ACATAGAGGGTACCTCTAAG", "snake": "ACATAGAGGGTACCTCTAAG", "mouse": "ACATAGAGGGTACCTCTAAG", } self.assertEqual(got, expect) def test_align_no_blank_columns(self): """correctly handle a file with no white space at line starts""" parser = MinimalNexusAlignParser("data/nexus_aa.nxs") seqs = {n: s for n, s in parser} self.assertEqual(len(seqs), 10) # 10 taxa lengths = set(len(seqs[n]) for n in seqs) self.assertEqual(lengths, {234}) # all same length and equal 234 def test_load_seqs_interface(self): """load_aligned_seqs correctly loads nexus alignments""" aln = load_aligned_seqs("data/nexus_mixed.nex") self.assertEqual(aln.num_seqs, 4) self.assertEqual(len(aln), 20) aln = load_aligned_seqs("data/nexus_aa.nxs") self.assertEqual(aln.num_seqs, 10) self.assertEqual(len(aln), 234) if __name__ == "__main__": main()
tests/test_parse/test_nexus.py
18,674
Tests of the Nexus Parser functions correctly handle a file with tree and alignment block correctly handle a file with no white space at line starts correctly handle an alignment block containing comments correctly handle an alignment block with spaces in seqs find_fields takes BL table line and returns field names mapped to info get_BL_table returns the section of the log file w/ the BL table get_tree_info returns the Nexus file section that describes the tree load_aligned_seqs correctly loads nexus alignments parse_PAUP_log extracts branch length info from a PAUP log file parse_dnd returns a dict with dnd indexed by tree name parse_nexus_tree returns a dnd string and a translation table list remove single quotes from tree and translate tables parse_taxa should return the taxa # from a taxa_field from find_fields parse_trans_table returns a dict with the taxa names indexed by number split_tree_info splits lines into header, Trans_table, and dnd Unit tests for the Nexus Parser check the full dendrogram string is returned check that all taxa are returned in the Trans_table check that Nexus files without translation table work check the full dendrogram string is returned check that all taxa are returned in the Trans_table check that taxa are returned in the Trans_table ------------------------------------------------------ 10 taxa all same length and equal 705 10 taxa all same length and equal 234
1,420
en
0.750317
from matplotlib import rc rc("font", family="serif", size=12) rc("text", usetex=True) rc("text.latex", preamble=open("macros.tex").read()) #import daft import imp daft = imp.load_source('daft', '/Users/kpmurphy/github/daft/daft.py') import os pgm = daft.PGM([4, 4], origin=[0, 0]) pgm.add_node(daft.Node("one", r"$1$", 1, 1)) pgm.add_node(daft.Node("x1", r"$x_1$", 2, 1)) pgm.add_node(daft.Node("x2", r"$x_2$", 3, 1)) pgm.add_node(daft.Node("z1", r"$z_1$", 2, 2)) pgm.add_node(daft.Node("z2", r"$z_2$", 3, 2)) pgm.add_node(daft.Node("y", r"$y$", 2.5, 3)) pgm.add_edge("one", "z1", label="-1.5", xoffset=-0.3) pgm.add_edge("x1", "z1", label="+1", xoffset=-0.3) pgm.add_edge("x1", "z2", label="+1", xoffset=-0.4) pgm.add_edge("x2", "z1", label="+1", xoffset=0.4) pgm.add_edge("x2", "z2", label="+1", xoffset=0.3) pgm.add_edge("z1", "y", label="-1", xoffset=-0.3) pgm.add_edge("z2", "y", label="-1", xoffset=0.3) #ax = pgm.render() # returns the pyplot axes object it drew onto #ax.text(1, 2, "My label") pgm.render() folder = "/Users/kpmurphy/github/pyprobml/figures" fname = "mlpXor" pgm.figure.savefig(os.path.join(folder, "{}.png".format(fname)))
figureCode/daft/mlpXor.py
1,163
import daftax = pgm.render() returns the pyplot axes object it drew ontoax.text(1, 2, "My label")
99
en
0.30427
#! /usr/bin/env python3 # counts the number of regions in a matrix # vertically and horizontally connected # e.g. # [1, 0, 1, 1] # [0, 1, 0, 0] # [1, 1, 0, 1] # has 4 regions class Patch(object): def __init__(self, visited, value): self.visited = visited self.value = value def __repr__(self): return "visited = %s, value = %s" % (self.visited, self.value) def initialize(region): for row in region: yield [Patch(visited=False, value=field) for field in row] testregion = list(initialize([ [True, False, True, True], [False, True, False, False], [True, True, False, True] ])) def exploreField(region, x, y): # check if we've reached the edge of the matrix if (x < 0 or y < 0): return True try: if region[y][x].visited or not region[y][x].value: return True except IndexError: return True region[y][x].visited = True exploreField(region, y+1, x) exploreField(region, y-1, x) exploreField(region, y, x+1) exploreField(region, y, x-1) count = 0 for y, row in enumerate(testregion): for x, patch in enumerate(row): # if a patch is both unvisited and true # this means exploreField will eliminate it # so it counts as one region if not patch.visited and patch.value: count += 1 exploreField(testregion, x, y) print(count)
regions.py
1,427
! /usr/bin/env python3 counts the number of regions in a matrix vertically and horizontally connected e.g. [1, 0, 1, 1] [0, 1, 0, 0] [1, 1, 0, 1] has 4 regions check if we've reached the edge of the matrix if a patch is both unvisited and true this means exploreField will eliminate it so it counts as one region
313
en
0.872787
from __future__ import division import errno import os import re import shutil import time import uuid from collections import namedtuple from itertools import repeat from pprint import pformat import pytest from cassandra import WriteFailure from cassandra.concurrent import (execute_concurrent, execute_concurrent_with_args) from ccmlib.node import Node from cqlsh_tests.cqlsh_tools import assert_resultset_contains from dtest import Tester, create_ks, logger from tools.assertions import assert_length_equal from tools.data import rows_to_list from tools.files import size_of_files_in_dir from tools.funcutils import get_rate_limited_function from tools.hacks import advance_to_next_cl_segment since = pytest.mark.since _16_uuid_column_spec = ( 'a uuid PRIMARY KEY, b uuid, c uuid, d uuid, e uuid, f uuid, g uuid, ' 'h uuid, i uuid, j uuid, k uuid, l uuid, m uuid, n uuid, o uuid, ' 'p uuid' ) def _insert_rows(session, table_name, insert_stmt, values): prepared_insert = session.prepare(insert_stmt) values = list(values) # in case values is a generator execute_concurrent(session, ((prepared_insert, x) for x in values), concurrency=500, raise_on_first_error=True) data_loaded = rows_to_list(session.execute('SELECT * FROM ' + table_name)) logger.debug('{n} rows inserted into {table_name}'.format(n=len(data_loaded), table_name=table_name)) # use assert_equal over assert_length_equal to avoid printing out # potentially large lists assert len(values) == len(data_loaded) return data_loaded def _move_commitlog_segments(source_dir, dest_dir, verbose=True): for source_filename in [name for name in os.listdir(source_dir) if not name.endswith('_cdc.idx')]: source_path, dest_path = (os.path.join(source_dir, source_filename), os.path.join(dest_dir, source_filename)) if verbose: logger.debug('moving {} to {}'.format(source_path, dest_path)) shutil.move(source_path, dest_path) def _get_16_uuid_insert_stmt(ks_name, table_name): return ( 'INSERT INTO {ks_name}.{table_name} ' '(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) ' 'VALUES (uuid(), uuid(), uuid(), uuid(), uuid(), ' 'uuid(), uuid(), uuid(), uuid(), uuid(), uuid(), ' 'uuid(), uuid(), uuid(), uuid(), uuid())' ).format(ks_name=ks_name, table_name=table_name) def _get_create_table_statement(ks_name, table_name, column_spec, options=None): if options: options_pairs = ('{k}={v}'.format(k=k, v=v) for (k, v) in options.items()) options_string = 'WITH ' + ' AND '.join(options_pairs) else: options_string = '' return ( 'CREATE TABLE ' + ks_name + '.' + table_name + ' ' '(' + column_spec + ') ' + options_string ) def _write_to_cdc_write_failure(session, insert_stmt): prepared = session.prepare(insert_stmt) start, rows_loaded, error_found = time.time(), 0, False rate_limited_debug = get_rate_limited_function(logger.debug, 5) while not error_found: # We want to fail if inserting data takes too long. Locally this # takes about 10s, but let's be generous. assert ((time.time() - start) <= 600), ( "It's taken more than 10 minutes to reach a WriteFailure trying " 'to overrun the space designated for CDC commitlogs. This could ' "be because data isn't being written quickly enough in this " 'environment, or because C* is failing to reject writes when ' 'it should.') # If we haven't logged from here in the last 5s, do so. rate_limited_debug( ' data load step has lasted {s:.2f}s, ' 'loaded {r} rows'.format(s=(time.time() - start), r=rows_loaded)) batch_results = list(execute_concurrent( session, ((prepared, ()) for _ in range(1000)), concurrency=500, # Don't propagate errors to the main thread. We expect at least # one WriteFailure, so we handle it below as part of the # results recieved from this method. raise_on_first_error=False )) # Here, we track the number of inserted values by getting the # number of successfully completed statements... rows_loaded += len([br for br in batch_results if br[0]]) # then, we make sure that the only failures are the expected # WriteFailure. assert ([] == [result for (success, result) in batch_results if not success and not isinstance(result, WriteFailure)]) # Finally, if we find a WriteFailure, that means we've inserted all # the CDC data we can and so we flip error_found to exit the loop. if any(isinstance(result, WriteFailure) for (_, result) in batch_results): logger.debug("write failed (presumably because we've overrun " 'designated CDC commitlog space) after ' 'loading {r} rows in {s:.2f}s'.format( r=rows_loaded, s=time.time() - start)) error_found = True return rows_loaded _TableInfoNamedtuple = namedtuple('TableInfoNamedtuple', [ # required 'ks_name', 'table_name', 'column_spec', # optional 'options', 'insert_stmt', # derived 'name', 'create_stmt' ]) class TableInfo(_TableInfoNamedtuple): __slots__ = () def __new__(cls, ks_name, table_name, column_spec, options=None, insert_stmt=None): name = ks_name + '.' + table_name create_stmt = _get_create_table_statement(ks_name, table_name, column_spec, options) self = super(TableInfo, cls).__new__( cls, # required ks_name=ks_name, table_name=table_name, column_spec=column_spec, # optional options=options, insert_stmt=insert_stmt, # derived name=name, create_stmt=create_stmt ) return self def _set_cdc_on_table(session, table_name, value, ks_name=None): """ Uses <session> to set CDC to <value> on <ks_name>.<table_name>. """ table_string = ks_name + '.' + table_name if ks_name else table_name value_string = 'true' if value else 'false' stmt = 'ALTER TABLE ' + table_string + ' WITH CDC = ' + value_string logger.debug(stmt) session.execute(stmt) def _get_set_cdc_func(session, ks_name, table_name): """ Close over a session, keyspace name, and table name and return a function that takes enables CDC on that keyspace if its argument is truthy and otherwise disables it. """ def set_cdc(value): return _set_cdc_on_table( session=session, ks_name=ks_name, table_name=table_name, value=value ) return set_cdc def _get_commitlog_files(node_path): commitlog_dir = os.path.join(node_path, 'commitlogs') return { os.path.join(commitlog_dir, name) for name in os.listdir(commitlog_dir) } def _get_cdc_raw_files(node_path, cdc_raw_dir_name='cdc_raw'): commitlog_dir = os.path.join(node_path, cdc_raw_dir_name) return { os.path.join(commitlog_dir, name) for name in os.listdir(commitlog_dir) } @since('3.8') class TestCDC(Tester): """ @jira_ticket CASSANDRA-8844 @jira_ticket CASSANDRA-12148 Test the correctness of some features of CDC, Change Data Capture, which provides a view of the commitlog on tables for which it is enabled. """ @pytest.fixture(autouse=True) def fixture_add_additional_log_patterns(self, fixture_dtest_setup): fixture_dtest_setup.allow_log_errors = True fixture_dtest_setup.ignore_log_patterns = ( # We expect to see this error in the logs when we reach CDC limit r'Failed to apply mutation locally' ) def _create_temp_dir(self, dir_name, verbose=True): """ Create a directory that will be deleted when this test class is torn down. """ if verbose: logger.debug('creating ' + dir_name) try: os.mkdir(dir_name) except OSError as e: if e.errno != errno.EEXIST: logger.debug(dir_name + ' already exists. removing and recreating.') shutil.rmtree(dir_name) os.mkdir(dir_name) else: raise e def debug_and_rmtree(): shutil.rmtree(dir_name) logger.debug(dir_name + ' removed') self.addCleanup(debug_and_rmtree) def prepare(self, ks_name, table_name=None, cdc_enabled_table=None, gc_grace_seconds=None, column_spec=None, configuration_overrides=None, table_id=None): """ Create a 1-node cluster, start it, create a keyspace, and if <table_name>, create a table in that keyspace. If <cdc_enabled_table>, that table is created with CDC enabled. If <column_spec>, use that string to specify the schema of the table -- for example, a valid value is 'a int PRIMARY KEY, b int'. The <configuration_overrides> is treated as a dict-like object and passed to self.cluster.set_configuration_options. """ config_defaults = { 'cdc_enabled': True, # we want to be able to generate new segments quickly 'commitlog_segment_size_in_mb': 2, } if configuration_overrides is None: configuration_overrides = {} self.cluster.populate(1) self.cluster.set_configuration_options(dict(config_defaults, **configuration_overrides)) self.cluster.start() node = self.cluster.nodelist()[0] session = self.patient_cql_connection(node) create_ks(session, ks_name, rf=1) if table_name is not None: assert cdc_enabled_table is not None, 'if creating a table in prepare, must specify whether or not CDC is enabled on it' assert column_spec is not None, 'if creating a table in prepare, must specify its schema' options = {} if gc_grace_seconds is not None: options['gc_grace_seconds'] = gc_grace_seconds if table_id is not None: options['id'] = table_id if cdc_enabled_table: options['cdc'] = 'true' stmt = _get_create_table_statement( ks_name, table_name, column_spec, options=options ) logger.debug(stmt) session.execute(stmt) return node, session def _assert_cdc_data_readable_on_round_trip(self, start_with_cdc_enabled): """ Parameterized test asserting that data written to a table is still readable after flipping the CDC flag on that table, then flipping it again. Starts with CDC enabled if start_with_cdc_enabled, otherwise starts with it disabled. """ ks_name, table_name = 'ks', 'tab' sequence = [True, False, True] if start_with_cdc_enabled else [False, True, False] start_enabled, alter_path = sequence[0], list(sequence[1:]) node, session = self.prepare(ks_name=ks_name, table_name=table_name, cdc_enabled_table=start_enabled, column_spec='a int PRIMARY KEY, b int') set_cdc = _get_set_cdc_func(session=session, ks_name=ks_name, table_name=table_name) insert_stmt = session.prepare('INSERT INTO ' + table_name + ' (a, b) VALUES (?, ?)') # data = zip(list(range(1000)), list(range(1000))) start = 0 stop = 1000 step = 1 data = [(n, min(n+step, stop)) for n in range(start, stop, step)] execute_concurrent_with_args(session, insert_stmt, data) # We need data to be in commitlogs, not sstables. assert [] == list(node.get_sstables(ks_name, table_name)) for enable in alter_path: set_cdc(enable) assert_resultset_contains(session.execute('SELECT * FROM ' + table_name), data) def test_cdc_enabled_data_readable_on_round_trip(self): """ Test that data is readable after an enabled->disabled->enabled round trip. """ self._assert_cdc_data_readable_on_round_trip(start_with_cdc_enabled=True) def test_cdc_disabled_data_readable_on_round_trip(self): """ Test that data is readable after an disabled->enabled->disabled round trip. """ self._assert_cdc_data_readable_on_round_trip(start_with_cdc_enabled=False) def test_non_cdc_segments_deleted_after_replay(self): """ Test that non-cdc segment files generated in previous runs are deleted after replay. """ ks_name, table_name = 'ks', 'tab' node, session = self.prepare(ks_name=ks_name, table_name=table_name, cdc_enabled_table=True, column_spec='a int PRIMARY KEY, b int') old_files = _get_cdc_raw_files(node.get_path()) node.drain() session.cluster.shutdown() node.stop() node.start(wait_for_binary_proto=True) new_files = _get_cdc_raw_files(node.get_path()) assert len(old_files.intersection(new_files)) == 0 def test_insertion_and_commitlog_behavior_after_reaching_cdc_total_space(self): """ Test that C* behaves correctly when CDC tables have consumed all the space available to them. In particular: after writing cdc_total_space_in_mb MB into CDC commitlogs: - CDC writes are rejected - non-CDC writes are accepted - on flush, CDC commitlogs are copied to cdc_raw - on flush, non-CDC commitlogs are not copied to cdc_raw This is a lot of behavior to validate in one test, but we do so to avoid running multiple tests that each write 1MB of data to fill cdc_total_space_in_mb. """ ks_name = 'ks' full_cdc_table_info = TableInfo( ks_name=ks_name, table_name='full_cdc_tab', column_spec=_16_uuid_column_spec, insert_stmt=_get_16_uuid_insert_stmt(ks_name, 'full_cdc_tab'), options={'cdc': 'true'} ) configuration_overrides = { # Make CDC space as small as possible so we can fill it quickly. 'cdc_total_space_in_mb': 4, } node, session = self.prepare( ks_name=ks_name, configuration_overrides=configuration_overrides ) session.execute(full_cdc_table_info.create_stmt) # Later, we'll also make assertions about the behavior of non-CDC # tables, so we create one here. non_cdc_table_info = TableInfo( ks_name=ks_name, table_name='non_cdc_tab', column_spec=_16_uuid_column_spec, insert_stmt=_get_16_uuid_insert_stmt(ks_name, 'non_cdc_tab') ) session.execute(non_cdc_table_info.create_stmt) # We'll also make assertions about the behavior of CDC tables when # other CDC tables have already filled the designated space for CDC # commitlogs, so we create the second CDC table here. empty_cdc_table_info = TableInfo( ks_name=ks_name, table_name='empty_cdc_tab', column_spec=_16_uuid_column_spec, insert_stmt=_get_16_uuid_insert_stmt(ks_name, 'empty_cdc_tab'), options={'cdc': 'true'} ) session.execute(empty_cdc_table_info.create_stmt) # Here, we insert values into the first CDC table until we get a # WriteFailure. This should happen when the CDC commitlogs take up 1MB # or more. logger.debug('flushing non-CDC commitlogs') node.flush() # Then, we insert rows into the CDC table until we can't anymore. logger.debug('beginning data insert to fill CDC commitlogs') rows_loaded = _write_to_cdc_write_failure(session, full_cdc_table_info.insert_stmt) assert 0 < rows_loaded, ('No CDC rows inserted. This may happen when ' 'cdc_total_space_in_mb > commitlog_segment_size_in_mb') commitlog_dir = os.path.join(node.get_path(), 'commitlogs') commitlogs_size = size_of_files_in_dir(commitlog_dir) logger.debug('Commitlog dir ({d}) is {b}B'.format(d=commitlog_dir, b=commitlogs_size)) # We should get a WriteFailure when trying to write to the CDC table # that's filled the designated CDC space... try: session.execute(full_cdc_table_info.insert_stmt) raise Exception("WriteFailure expected") except WriteFailure: pass # or any CDC table. try: session.execute(empty_cdc_table_info.insert_stmt) raise Exception("WriteFailure expected") except WriteFailure: pass # Now we test for behaviors of non-CDC tables when we've exceeded # cdc_total_space_in_mb. # # First, we drain and save the names of all the new discarded CDC # segments node.drain() session.cluster.shutdown() node.stop() node.start(wait_for_binary_proto=True) session = self.patient_cql_connection(node) pre_non_cdc_write_cdc_raw_segments = _get_cdc_raw_files(node.get_path()) # Snapshot the _cdc.idx file if > 4.0 for comparison at end before_cdc_state = [] # init empty here to quiet PEP if self.cluster.version() >= '4.0': # Create ReplayData objects for each index file found in loading cluster node1_path = os.path.join(node.get_path(), 'cdc_raw') before_cdc_state = [ReplayData.load(node1_path, name) for name in os.listdir(node1_path) if name.endswith('_cdc.idx')] # save the names of all the commitlog segments written up to this # point: pre_non_cdc_write_segments = _get_commitlog_files(node.get_path()) # Check that writing to non-CDC tables succeeds even when writes to CDC # tables are rejected: non_cdc_prepared_insert = session.prepare(non_cdc_table_info.insert_stmt) session.execute(non_cdc_prepared_insert, ()) # should not raise an exception # Check the following property: any new commitlog segments written to # after cdc_raw has reached its maximum configured size should not be # moved to cdc_raw, on commitlog discard, because any such commitlog # segments are written to non-CDC tables. # # First, write to non-cdc tables. start, time_limit = time.time(), 600 rate_limited_debug = get_rate_limited_function(logger.debug, 5) logger.debug('writing to non-cdc table') # We write until we get a new commitlog segment. while _get_commitlog_files(node.get_path()) <= pre_non_cdc_write_segments: elapsed = time.time() - start rate_limited_debug(' non-cdc load step has lasted {s:.2f}s'.format(s=elapsed)) assert elapsed <= time_limit, "It's been over a {s}s and we haven't written a new " \ "commitlog segment. Something is wrong.".format(s=time_limit) execute_concurrent( session, ((non_cdc_prepared_insert, ()) for _ in range(1000)), concurrency=500, raise_on_first_error=True, ) # Finally, we check that draining doesn't move any new segments to cdc_raw: node.drain() session.cluster.shutdown() if self.cluster.version() < '4.0': assert pre_non_cdc_write_cdc_raw_segments == _get_cdc_raw_files(node.get_path()) else: # Create ReplayData objects for each index file found in loading cluster node2_path = os.path.join(node.get_path(), 'cdc_raw') after_cdc_state = [ReplayData.load(node2_path, name) for name in os.listdir(node2_path) if name.endswith('_cdc.idx')] # Confirm all indexes in 1st are accounted for and match corresponding entry in 2nd. found = True for idx in before_cdc_state: idx_found = False for idx_two in after_cdc_state: if compare_replay_data(idx, idx_two): idx_found = True if not idx_found: found = False break if not found: self._fail_and_print_sets(before_cdc_state, after_cdc_state, 'Found CDC index in before not matched in after (non-CDC write test)') # Now we confirm we don't have anything that showed up in 2nd not accounted for in 1st orphan_found = False for idx_two in after_cdc_state: index_found = False for idx in before_cdc_state: if compare_replay_data(idx_two, idx): index_found = True if not index_found: orphan_found = True break if orphan_found: self._fail_and_print_sets(before_cdc_state, after_cdc_state, 'Found orphaned index file in after CDC state not in former.') def _fail_and_print_sets(self, rd_one, rd_two, msg): print('Set One:') for idx in rd_one: print(' {},{},{},{}'.format(idx.name, idx.completed, idx.offset, idx.log_name)) print('Set Two:') for idx_two in rd_two: print(' {},{},{},{}'.format(idx_two.name, idx_two.completed, idx_two.offset, idx_two.log_name)) pytest.fail(msg) def _init_new_loading_node(self, ks_name, create_stmt, use_thrift=False): loading_node = Node( name='node2', cluster=self.cluster, auto_bootstrap=False, thrift_interface=('127.0.0.2', 9160) if use_thrift else None, storage_interface=('127.0.0.2', 7000), jmx_port='7400', remote_debug_port='0', initial_token=None, binary_interface=('127.0.0.2', 9042) ) logger.debug('adding node') self.cluster.add(loading_node, is_seed=True, data_center="dc1") logger.debug('starting new node') loading_node.start(wait_for_binary_proto=120) logger.debug('recreating ks and table') loading_session = self.patient_exclusive_cql_connection(loading_node) create_ks(loading_session, ks_name, rf=1) logger.debug('creating new table') loading_session.execute(create_stmt) logger.debug('stopping new node') loading_session.cluster.shutdown() loading_node.stop() return loading_node def test_cdc_data_available_in_cdc_raw(self): ks_name = 'ks' # First, create a new node just for data generation. generation_node, generation_session = self.prepare(ks_name=ks_name) cdc_table_info = TableInfo( ks_name=ks_name, table_name='cdc_tab', column_spec=_16_uuid_column_spec, insert_stmt=_get_16_uuid_insert_stmt(ks_name, 'cdc_tab'), options={ 'cdc': 'true', # give table an explicit id so when we create it again it's the # same table and we can replay into it 'id': uuid.uuid4() } ) # Write until we get a new CL segment to avoid replaying initialization # mutations from this node's startup into system tables in the other # node. See CASSANDRA-11811. advance_to_next_cl_segment( session=generation_session, commitlog_dir=os.path.join(generation_node.get_path(), 'commitlogs') ) generation_session.execute(cdc_table_info.create_stmt) # insert 10000 rows inserted_rows = _insert_rows(generation_session, cdc_table_info.name, cdc_table_info.insert_stmt, repeat((), 10000)) # drain the node to guarantee all cl segments will be recycled logger.debug('draining') generation_node.drain() logger.debug('stopping') # stop the node and clean up all sessions attached to it generation_session.cluster.shutdown() generation_node.stop() # We can rely on the existing _cdc.idx files to determine which .log files contain cdc data. source_path = os.path.join(generation_node.get_path(), 'cdc_raw') source_cdc_indexes = {ReplayData.load(source_path, name) for name in source_path if name.endswith('_cdc.idx')} # assertNotEqual(source_cdc_indexes, {}) assert source_cdc_indexes != {} # create a new node to use for cdc_raw cl segment replay loading_node = self._init_new_loading_node(ks_name, cdc_table_info.create_stmt, self.cluster.version() < '4') # move cdc_raw contents to commitlog directories, then start the # node again to trigger commitlog replay, which should replay the # cdc_raw files we moved to commitlogs into memtables. logger.debug('moving cdc_raw and restarting node') _move_commitlog_segments( os.path.join(generation_node.get_path(), 'cdc_raw'), os.path.join(loading_node.get_path(), 'commitlogs') ) loading_node.start(wait_for_binary_proto=120) logger.debug('node successfully started; waiting on log replay') loading_node.grep_log('Log replay complete') logger.debug('log replay complete') # final assertions validation_session = self.patient_exclusive_cql_connection(loading_node) data_in_cdc_table_after_restart = rows_to_list( validation_session.execute('SELECT * FROM ' + cdc_table_info.name) ) logger.debug('found {cdc} values in CDC table'.format( cdc=len(data_in_cdc_table_after_restart) )) # Then we assert that the CDC data that we expect to be there is there. # All data that was in CDC tables should have been copied to cdc_raw, # then used in commitlog replay, so it should be back in the cluster. assert (inserted_rows == data_in_cdc_table_after_restart), 'not all expected data selected' if self.cluster.version() >= '4.0': # Create ReplayData objects for each index file found in loading cluster loading_path = os.path.join(loading_node.get_path(), 'cdc_raw') dest_cdc_indexes = [ReplayData.load(loading_path, name) for name in os.listdir(loading_path) if name.endswith('_cdc.idx')] # Compare source replay data to dest to ensure replay process created both hard links and index files. for srd in source_cdc_indexes: # Confirm both log and index are in dest assert os.path.isfile(os.path.join(loading_path, srd.idx_name)) assert os.path.isfile(os.path.join(loading_path, srd.log_name)) # Find dest ReplayData that corresponds to the source (should be exactly 1) corresponding_dest_replay_datae = [x for x in dest_cdc_indexes if srd.idx_name == x.idx_name] assert_length_equal(corresponding_dest_replay_datae, 1) drd = corresponding_dest_replay_datae[0] # We can't compare equality on offsets since replay uses the raw file length as the written # cdc offset. We *can*, however, confirm that the offset in the replayed file is >= # the source file, ensuring clients are signaled to replay at least all the data in the # log. assert drd.offset >= srd.offset # Confirm completed flag is the same in both assert srd.completed == drd.completed # Confirm that the relationship between index files on the source # and destination looks like we expect. # First, grab the mapping between the two, make sure it's a 1-1 # mapping, and transform the dict to reflect that: src_to_dest_idx_map = { src_rd: [dest_rd for dest_rd in dest_cdc_indexes if dest_rd.idx_name == src_rd.idx_name] for src_rd in source_cdc_indexes } for src_rd, dest_rds in src_to_dest_idx_map.items(): assert_length_equal(dest_rds, 1) src_to_dest_idx_map[src_rd] = dest_rds[0] # All offsets in idx files that were copied should be >0 on the # destination node. assert ( 0 not in {i.offset for i in src_to_dest_idx_map.values()}),\ ('Found index offsets == 0 in an index file on the ' 'destination node that corresponds to an index file on the ' 'source node:\n' '{}').format(pformat(src_to_dest_idx_map)) # Offsets of all shared indexes should be >= on the destination # than on the source. for src_rd, dest_rd in src_to_dest_idx_map.items(): assert dest_rd.offset >= src_rd.offset src_to_dest_idx_map = { src_rd: [dest_rd for dest_rd in dest_cdc_indexes if dest_rd.idx_name == src_rd.idx_name] for src_rd in source_cdc_indexes } for k, v in src_to_dest_idx_map.items(): assert_length_equal(v, 1) assert k.offset >= v.offset def compare_replay_data(rd_one, rd_two): return rd_one.idx_name == rd_two.idx_name and \ rd_one.completed == rd_two.completed and \ rd_one.offset == rd_two.offset and \ rd_one.log_name == rd_two.log_name class ReplayData(namedtuple('ReplayData', ['idx_name', 'completed', 'offset', 'log_name'])): """ Replay data class containing data from a _cdc.idx file. Build one with the load method. """ @classmethod def load(cls, path, name): assert '_cdc' in name, 'expected to find _cdc in passed in index name. Did not: ' + name with open(os.path.join(path, name), 'r') as f: offset, completed = [line.strip() for line in f.readlines()] return cls( idx_name=name, completed=completed, offset=int(offset), log_name=re.sub('_cdc.idx', '.log', name) )
cdc_test.py
31,126
Replay data class containing data from a _cdc.idx file. Build one with the load method. @jira_ticket CASSANDRA-8844 @jira_ticket CASSANDRA-12148 Test the correctness of some features of CDC, Change Data Capture, which provides a view of the commitlog on tables for which it is enabled. Parameterized test asserting that data written to a table is still readable after flipping the CDC flag on that table, then flipping it again. Starts with CDC enabled if start_with_cdc_enabled, otherwise starts with it disabled. Create a directory that will be deleted when this test class is torn down. Close over a session, keyspace name, and table name and return a function that takes enables CDC on that keyspace if its argument is truthy and otherwise disables it. Uses <session> to set CDC to <value> on <ks_name>.<table_name>. Create a 1-node cluster, start it, create a keyspace, and if <table_name>, create a table in that keyspace. If <cdc_enabled_table>, that table is created with CDC enabled. If <column_spec>, use that string to specify the schema of the table -- for example, a valid value is 'a int PRIMARY KEY, b int'. The <configuration_overrides> is treated as a dict-like object and passed to self.cluster.set_configuration_options. Test that data is readable after an disabled->enabled->disabled round trip. Test that data is readable after an enabled->disabled->enabled round trip. Test that C* behaves correctly when CDC tables have consumed all the space available to them. In particular: after writing cdc_total_space_in_mb MB into CDC commitlogs: - CDC writes are rejected - non-CDC writes are accepted - on flush, CDC commitlogs are copied to cdc_raw - on flush, non-CDC commitlogs are not copied to cdc_raw This is a lot of behavior to validate in one test, but we do so to avoid running multiple tests that each write 1MB of data to fill cdc_total_space_in_mb. Test that non-cdc segment files generated in previous runs are deleted after replay. in case values is a generator use assert_equal over assert_length_equal to avoid printing out potentially large lists We want to fail if inserting data takes too long. Locally this takes about 10s, but let's be generous. If we haven't logged from here in the last 5s, do so. Don't propagate errors to the main thread. We expect at least one WriteFailure, so we handle it below as part of the results recieved from this method. Here, we track the number of inserted values by getting the number of successfully completed statements... then, we make sure that the only failures are the expected WriteFailure. Finally, if we find a WriteFailure, that means we've inserted all the CDC data we can and so we flip error_found to exit the loop. required optional derived required optional derived We expect to see this error in the logs when we reach CDC limit we want to be able to generate new segments quickly data = zip(list(range(1000)), list(range(1000))) We need data to be in commitlogs, not sstables. Make CDC space as small as possible so we can fill it quickly. Later, we'll also make assertions about the behavior of non-CDC tables, so we create one here. We'll also make assertions about the behavior of CDC tables when other CDC tables have already filled the designated space for CDC commitlogs, so we create the second CDC table here. Here, we insert values into the first CDC table until we get a WriteFailure. This should happen when the CDC commitlogs take up 1MB or more. Then, we insert rows into the CDC table until we can't anymore. We should get a WriteFailure when trying to write to the CDC table that's filled the designated CDC space... or any CDC table. Now we test for behaviors of non-CDC tables when we've exceeded cdc_total_space_in_mb. First, we drain and save the names of all the new discarded CDC segments Snapshot the _cdc.idx file if > 4.0 for comparison at end init empty here to quiet PEP Create ReplayData objects for each index file found in loading cluster save the names of all the commitlog segments written up to this point: Check that writing to non-CDC tables succeeds even when writes to CDC tables are rejected: should not raise an exception Check the following property: any new commitlog segments written to after cdc_raw has reached its maximum configured size should not be moved to cdc_raw, on commitlog discard, because any such commitlog segments are written to non-CDC tables. First, write to non-cdc tables. We write until we get a new commitlog segment. Finally, we check that draining doesn't move any new segments to cdc_raw: Create ReplayData objects for each index file found in loading cluster Confirm all indexes in 1st are accounted for and match corresponding entry in 2nd. Now we confirm we don't have anything that showed up in 2nd not accounted for in 1st First, create a new node just for data generation. give table an explicit id so when we create it again it's the same table and we can replay into it Write until we get a new CL segment to avoid replaying initialization mutations from this node's startup into system tables in the other node. See CASSANDRA-11811. insert 10000 rows drain the node to guarantee all cl segments will be recycled stop the node and clean up all sessions attached to it We can rely on the existing _cdc.idx files to determine which .log files contain cdc data. assertNotEqual(source_cdc_indexes, {}) create a new node to use for cdc_raw cl segment replay move cdc_raw contents to commitlog directories, then start the node again to trigger commitlog replay, which should replay the cdc_raw files we moved to commitlogs into memtables. final assertions Then we assert that the CDC data that we expect to be there is there. All data that was in CDC tables should have been copied to cdc_raw, then used in commitlog replay, so it should be back in the cluster. Create ReplayData objects for each index file found in loading cluster Compare source replay data to dest to ensure replay process created both hard links and index files. Confirm both log and index are in dest Find dest ReplayData that corresponds to the source (should be exactly 1) We can't compare equality on offsets since replay uses the raw file length as the written cdc offset. We *can*, however, confirm that the offset in the replayed file is >= the source file, ensuring clients are signaled to replay at least all the data in the log. Confirm completed flag is the same in both Confirm that the relationship between index files on the source and destination looks like we expect. First, grab the mapping between the two, make sure it's a 1-1 mapping, and transform the dict to reflect that: All offsets in idx files that were copied should be >0 on the destination node. Offsets of all shared indexes should be >= on the destination than on the source.
6,787
en
0.895139
from os.path import isfile from os.path import join import matplotlib.pyplot as plt import pytest from SciDataTool import DataTime, Data1D, DataLinspace, VectorField, Norm_ref from numpy import linspace, sin, squeeze from Tests import TEST_DATA_DIR from Tests import save_plot_path as save_path from pyleecan.Classes.ImportMatlab import ImportMatlab from pyleecan.Classes.InputFlux import InputFlux from pyleecan.Classes.OPdq import OPdq from pyleecan.Classes.Output import Output from pyleecan.Classes.Simu1 import Simu1 from pyleecan.Functions.load import load from pyleecan.Functions.Plot import dict_2D, dict_3D from pyleecan.definitions import DATA_DIR @pytest.fixture(scope="module") def import_data(): data = import_data_func() return data def import_data_func(): SCIM_006 = load(join(DATA_DIR, "Machine", "SCIM_006.json")) simu = Simu1(name="test_plots", machine=SCIM_006) mat_file_Br = join(TEST_DATA_DIR, "Plots", "default_proj_Br.mat") mat_file_time = join(TEST_DATA_DIR, "Plots", "default_proj_time.mat") mat_file_angle = join(TEST_DATA_DIR, "Plots", "default_proj_angle.mat") mat_file_Br_cfft2 = join(TEST_DATA_DIR, "Plots", "default_proj_Br_cfft2.mat") mat_file_Brfreqs = join(TEST_DATA_DIR, "Plots", "default_proj_Brfreqs.mat") mat_file_Brwavenumber = join( TEST_DATA_DIR, "Plots", "default_proj_Brwavenumber.mat" ) if not isfile(mat_file_Br): import urllib.request url = "https://www.pyleecan.org/Data/default_proj_Br.mat" urllib.request.urlretrieve(url, mat_file_Br) if not isfile(mat_file_Br_cfft2): import urllib.request url = "https://www.pyleecan.org/Data/default_proj_Br_cfft2.mat" urllib.request.urlretrieve(url, mat_file_Br_cfft2) data = {} data["SCIM_006"] = SCIM_006 data["simu"] = simu # Read input files from Manatee data["flux"] = ImportMatlab(mat_file_Br, var_name="XBr") data["time"] = ImportMatlab(mat_file_time, var_name="timec") data["angle"] = ImportMatlab(mat_file_angle, var_name="alpha_radc") data["flux_FT"] = ImportMatlab(mat_file_Br_cfft2, var_name="Fwr") data["freqs"] = ImportMatlab(mat_file_Brfreqs, var_name="freqs") data["wavenumber"] = ImportMatlab(mat_file_Brwavenumber, var_name="orders") data["N0"] = 2000 data["Id_ref"] = 10 data["Iq_ref"] = -10 # Plot parameters data["freq_max"] = 2000 data["r_max"] = 78 return data class Test_plots(object): @pytest.mark.long_5s @pytest.mark.SingleOP @pytest.mark.SCIM def test_default_proj_Br_time_space(self, import_data): SCIM_006 = import_data["SCIM_006"] simu = import_data["simu"] time = import_data["time"] angle = import_data["angle"] flux = import_data["flux"] freq_max = import_data["freq_max"] N0 = import_data["N0"] Id_ref = import_data["Id_ref"] Iq_ref = import_data["Iq_ref"] time_arr = squeeze(time.get_data()) angle_arr = squeeze(angle.get_data()) flux_arr = flux.get_data() norm_angle = {"space_order": Norm_ref(ref=3)} simu = Simu1(name="test_default_proj_Br_time_space", machine=SCIM_006) simu.mag = None simu.force = None simu.struct = None simu.input = InputFlux( B_dict={"Br": flux}, time=time, angle=angle, OP=OPdq(N0=N0, Id_ref=Id_ref, Iq_ref=Iq_ref), ) out = Output(simu=simu) simu.run() out2 = Output(simu=simu) # Reduce to 1/3 period Br_reduced = flux_arr[0:672, 0:672] time_reduced = time_arr[0:672] angle_reduced = angle_arr[0:672] # Build the data objects Time2 = Data1D( name="time", unit="s", symmetries={"period": 3}, values=time_reduced, ) Angle2 = Data1D( name="angle", unit="rad", symmetries={"period": 3}, values=angle_reduced, normalizations=norm_angle, ) Br2 = DataTime( symbol="B_r", name="Airgap radial flux density", unit="T", axes=[Time2, Angle2], values=Br_reduced, ) out2.mag.B = VectorField( name="Airgap flux density", symbol="B", components={"radial": Br2} ) # Plot the result by comparing the two simulation (sym / no sym) plt.close("all") out.mag.B.plot_2D_Data( "time", "angle[0]{°}", data_list=[out2.mag.B], is_auto_ticks=False, legend_list=["Reference", "Periodic"], save_path=join(save_path, "test_default_proj_Br_dataobj_period.png"), is_show_fig=False, **dict_2D, ) out.mag.B.plot_2D_Data( "freqs=[0," + str(freq_max) + "]", data_list=[out2.mag.B], legend_list=["Reference", "Periodic"], is_auto_ticks=False, save_path=join(save_path, "test_default_proj_Br_dataobj_period_fft.png"), is_show_fig=False, **dict_2D, ) out3 = Output(simu=simu) # Get linspace data t0 = time_arr[0] tf = time_arr[-1] deltat = time_arr[1] - time_arr[0] a0 = angle_arr[0] deltaa = angle_arr[1] - angle_arr[0] Na = len(angle_arr) # Build the data objects Time3 = DataLinspace( name="time", unit="s", initial=t0, final=tf + deltat, step=deltat, include_endpoint=False, ) Angle3 = DataLinspace( name="angle", unit="rad", normalizations=norm_angle, initial=a0, step=deltaa, number=Na, include_endpoint=False, ) Br3 = DataTime( symbol="B_r", name="Airgap radial flux density", unit="T", axes=[Time3, Angle3], values=flux_arr, ) out3.mag.B = VectorField( name="Airgap flux density", symbol="B", components={"radial": Br3} ) # Plot the result by comparing the two simulation (Data1D / DataLinspace) plt.close("all") out.mag.B.plot_2D_Data( "angle{°}", data_list=[out3.mag.B], legend_list=["Reference", "Linspace"], is_auto_ticks=False, save_path=join(save_path, "test_default_proj_Br_dataobj_linspace.png"), is_show_fig=False, **dict_2D, ) out.mag.B.components["radial"].axes[1].normalizations["space_order"] = Norm_ref( ref=3 ) out.mag.B.plot_2D_Data( "wavenumber->space_order=[0,100]", data_list=[out3.mag.B], legend_list=["Reference", "Linspace"], is_auto_ticks=False, save_path=join(save_path, "test_default_proj_Br_dataobj_linspace_fft.png"), is_show_fig=False, **dict_2D, ) simu4 = Simu1(name="test_default_proj_Br_time_space_ift", machine=SCIM_006) simu4.mag = None simu4.force = None simu4.struct = None simu4.input = InputFlux( B_dict={"Br": flux}, time=time, angle=angle, OP=OPdq(N0=N0, Id_ref=Id_ref, Iq_ref=Iq_ref), ) out4 = Output(simu=simu4) simu4.run() out4.post.legend_name = "Inverse FT" # Plot the result by comparing the two simulation (direct / ifft) plt.close("all") out.mag.B.plot_2D_Data( "angle{°}", data_list=[out4.mag.B], legend_list=["Reference", "Inverse FFT"], is_auto_ticks=False, save_path=join(save_path, "test_default_proj_Br_dataobj_ift.png"), is_show_fig=False, **dict_2D, ) out.mag.B.plot_2D_Data( "wavenumber=[0,100]", data_list=[out4.mag.B], legend_list=["Reference", "Inverse FFT"], is_auto_ticks=False, save_path=join(save_path, "test_default_proj_Br_dataobj_ift_fft.png"), is_show_fig=False, **dict_2D, ) out5 = Output(simu=simu) # Get linspace data t0 = 0.01 tf = 0.04 Nt = 3000 time5 = linspace(0.01, 0.04, 3000, endpoint=True) # Compute sine function Br5 = 0.2 * sin(375 * time5 - 1.5) # Build the data objects Time5 = DataLinspace( name="time", unit="s", initial=t0, final=tf, number=Nt, include_endpoint=True, ) flux5 = DataTime( symbol="B_r", name="Airgap radial flux density", unit="T", axes=[Time5], values=Br5, ) out5.mag.B = VectorField( name="Airgap flux density", symbol="B", components={"radial": flux5} ) # Plot the result by comparing the two simulation (sym / no sym) plt.close("all") out.mag.B.plot_2D_Data( "time", "angle[0]{°}", data_list=[out5.mag.B], legend_list=["Br", "0.2sin(375t-1.5)"], save_path=join(save_path, "test_default_proj_Br_compare.png"), is_auto_ticks=False, is_show_fig=False, **dict_2D, ) @pytest.mark.SingleOP @pytest.mark.SCIM def test_default_proj_Br_cfft2(self, import_data): SCIM_006 = import_data["SCIM_006"] simu = import_data["simu"] time = import_data["time"] angle = import_data["angle"] flux = import_data["flux"] freq_max = import_data["freq_max"] r_max = import_data["r_max"] N0 = import_data["N0"] Id_ref = import_data["Id_ref"] Iq_ref = import_data["Iq_ref"] N_stem = 100 simu = Simu1(name="test_default_proj_Br_cfft2", machine=SCIM_006) simu.input = InputFlux( B_dict={"Br": flux}, time=time, angle=angle, OP=OPdq(N0=N0, Id_ref=Id_ref, Iq_ref=Iq_ref), ) simu.mag = None simu.force = None simu.struct = None out = Output(simu=simu) simu.run() # Plot the 2D FFT of flux density as stem plot plt.close("all") out.mag.B.plot_3D_Data( "freqs=[0," + str(freq_max) + "]", "wavenumber=[-" + str(r_max) + "," + str(r_max) + "]", N_stem=N_stem, is_auto_ticks=False, save_path=join(save_path, "test_default_proj_Br_dataobj_cfft2.png"), is_show_fig=False, **dict_3D, ) @pytest.mark.SingleOP @pytest.mark.SCIM def test_default_proj_surf(self, import_data): SCIM_006 = import_data["SCIM_006"] simu = import_data["simu"] time = import_data["time"] angle = import_data["angle"] flux = import_data["flux"] flux_FT = import_data["flux_FT"] freqs = import_data["freqs"] wavenumber = import_data["wavenumber"] freq_max = import_data["freq_max"] r_max = import_data["r_max"] N0 = import_data["N0"] Id_ref = import_data["Id_ref"] Iq_ref = import_data["Iq_ref"] simu = Simu1(name="test_default_proj_surf", machine=SCIM_006) simu.mag = None simu.force = None simu.struct = None simu.input = InputFlux( B_dict={"Br": flux}, time=time, angle=angle, OP=OPdq(N0=N0, Id_ref=Id_ref, Iq_ref=Iq_ref), ) out = Output(simu=simu) simu.run() # Plot the result by comparing the two simulation (sym / no sym) plt.close("all") out.mag.B.plot_3D_Data( "time=[0,0.06]", "angle{°}", component_list=["radial"], save_path=join(save_path, "test_default_proj_Br_surf_dataobj.png"), is_2D_view=False, is_show_fig=False, **dict_3D, ) @pytest.mark.SingleOP @pytest.mark.SCIM def test_default_proj_fft2(self, import_data): SCIM_006 = import_data["SCIM_006"] simu = import_data["simu"] time = import_data["time"] angle = import_data["angle"] flux = import_data["flux"] freq_max = import_data["freq_max"] r_max = import_data["r_max"] N0 = import_data["N0"] Id_ref = import_data["Id_ref"] Iq_ref = import_data["Iq_ref"] simu = Simu1(name="test_default_proj_fft2", machine=SCIM_006) simu.mag = None simu.force = None simu.struct = None simu.input = InputFlux( B_dict={"Br": flux}, time=time, angle=angle, OP=OPdq(N0=N0, Id_ref=Id_ref, Iq_ref=Iq_ref), ) out = Output(simu=simu) simu.run() # Plot the 2D FFT of flux density as 2D scatter plot with colormap plt.close("all") freq_max = 500 r_max = 20 out.mag.B.plot_3D_Data( "freqs=[0," + str(freq_max) + "]", "wavenumber=[-" + str(r_max) + "," + str(r_max) + "]", is_2D_view=True, is_auto_ticks=False, save_path=join(save_path, "test_default_proj_Br_fft2_dataobj.png"), is_show_fig=False, **dict_3D, ) @pytest.mark.SingleOP @pytest.mark.SCIM def test_default_proj_time_space(self, import_data): SCIM_006 = import_data["SCIM_006"] simu = import_data["simu"] time = import_data["time"] angle = import_data["angle"] flux = import_data["flux"] N0 = import_data["N0"] Id_ref = import_data["Id_ref"] Iq_ref = import_data["Iq_ref"] simu = Simu1(name="test_default_proj_time_space", machine=SCIM_006) simu.mag = None simu.force = None simu.struct = None simu.input = InputFlux( B_dict={"Br": flux}, time=time, angle=angle, OP=OPdq(N0=N0, Id_ref=Id_ref, Iq_ref=Iq_ref), ) out = Output(simu=simu) simu.run() # Plot the result by comparing the two simulation (sym / no sym) plt.close("all") out.mag.B.plot_3D_Data( "time", "angle{°}", is_2D_view=True, save_path=join(save_path, "test_default_proj_Br_time_space_dataobj.png"), is_show_fig=False, **dict_3D, ) if __name__ == "__main__": data = import_data_func() test_plot_class = Test_plots() test_plot_class.test_default_proj_Br_time_space(data) test_plot_class.test_default_proj_Br_cfft2(data) test_plot_class.test_default_proj_surf(data) test_plot_class.test_default_proj_fft2(data) test_plot_class.test_default_proj_time_space(data)
Tests/Plot/test_plots.py
15,128
Read input files from Manatee Plot parameters Reduce to 1/3 period Build the data objects Plot the result by comparing the two simulation (sym / no sym) Get linspace data Build the data objects Plot the result by comparing the two simulation (Data1D / DataLinspace) Plot the result by comparing the two simulation (direct / ifft) Get linspace data Compute sine function Build the data objects Plot the result by comparing the two simulation (sym / no sym) Plot the 2D FFT of flux density as stem plot Plot the result by comparing the two simulation (sym / no sym) Plot the 2D FFT of flux density as 2D scatter plot with colormap Plot the result by comparing the two simulation (sym / no sym)
691
en
0.823254
'''A module for loading settings''' import logging.config import sys from logging import getLogger from pathlib import Path import yaml from hazelsync.metrics import get_metrics_engine DEFAULT_SETTINGS = '/etc/hazelsync.yaml' CLUSTER_DIRECTORY = '/etc/hazelsync.d' DEFAULT_LOGGING = { 'version': 1, 'formatters': { 'syslog': {'format': '%(name)s[%(process)d]: %(levelname)s: %(message)s'}, 'default': {'format': '%(asctime)s - %(name)s: %(levelname)s %(message)s'}, }, 'handlers': { 'console': { 'level': 'DEBUG', 'class': 'logging.StreamHandler', 'stream': sys.stderr, 'formatter': 'default', }, 'syslog': { 'level': 'INFO', 'class': 'logging.handlers.SysLogHandler', 'address': '/dev/log', 'facility': 'local0', 'formatter': 'syslog', }, }, 'loggers': { 'hazelsync': { 'handlers': ['console', 'syslog'], 'level': 'DEBUG', 'propagate': True, }, }, } log = getLogger('hazelsync') class SettingError(AttributeError): '''Raise an exception if there is a configuration error''' def __init__(self, job, message): log.error("Configuration error (in %s): %s", job, message) super().__init__(message) class GlobalSettings: '''A class to manage the global settings of hazelsync''' def __init__(self, path=DEFAULT_SETTINGS): path = Path(path) text = path.read_text(encoding='utf-8') data = yaml.safe_load(text) self.default_backend = data.get('default_backend', 'localfs') self.job_options = data.get('job_options') self.backend_options = data.get('backend_options') self.logging = data.get('logging', DEFAULT_LOGGING) metrics_config = data.get('metrics', {}) self.metrics = get_metrics_engine(metrics_config) def logger(self): '''Setup logging and return the logger''' logging.config.dictConfig(self.logging) mylogger = getLogger('hazelsync') mylogger.debug('Logger initialized') return mylogger def job(self, job_type: str) -> dict: '''Return defaults for a job type''' return self.job_options.get(job_type, {}) def backend(self, backend_type: str) -> dict: '''Return defaults for a backend type''' return self.backend_options.get(backend_type, {}) class ClusterSettings: '''A class to manage the settings of a cluster''' directory = Path(CLUSTER_DIRECTORY) def __init__(self, name, global_path=DEFAULT_SETTINGS): self.name = name self.globals = GlobalSettings(global_path) path = ClusterSettings.directory / f"{name}.yaml" text = path.read_text(encoding='utf-8') data = yaml.safe_load(text) self.job_type = data.get('job') self.job_options = data.get('options', {}) self.backend_type = data.get('backend') or self.globals.default_backend self.backend_options = data.get('backend_options', {}) @staticmethod def list() -> dict: '''List the backup cluster found in the settings''' settings = {} for path in ClusterSettings.directory.glob('*.yaml'): cluster = path.stem settings[cluster] = {'path': path} try: settings[cluster]['config_status'] = 'success' except KeyError as err: log.error(err) settings[cluster]['config'] = {} settings[cluster]['config_status'] = 'failure' return settings def job(self): '''Return the job options (merged with defaults)''' defaults = self.globals.job(self.job_type) options = self.job_options return self.job_type, {**defaults, **options} def backend(self): '''Return the backend option (merged with defaults)''' defaults = self.globals.backend(self.backend_type) options = self.backend_options return self.backend_type, {**defaults, **options}
hazelsync/settings.py
4,122
A class to manage the settings of a cluster A class to manage the global settings of hazelsync Raise an exception if there is a configuration error Return defaults for a backend type Return the backend option (merged with defaults) Return defaults for a job type Return the job options (merged with defaults) List the backup cluster found in the settings Setup logging and return the logger A module for loading settings
420
en
0.656512
# Generated by Django 3.0.5 on 2020-04-23 19:10 import datetime from django.db import migrations, models import django.db.models.deletion import wagtail.core.fields class Migration(migrations.Migration): initial = True dependencies = [ ('wagtailimages', '0001_squashed_0021'), ('wagtailcore', '0045_assign_unlock_grouppagepermission'), ] operations = [ migrations.CreateModel( name='PostIndex', fields=[ ('page_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='wagtailcore.Page')), ('featured_image', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='wagtailimages.Image')), ], options={ 'abstract': False, }, bases=('wagtailcore.page',), ), migrations.CreateModel( name='Post', fields=[ ('page_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='wagtailcore.Page')), ('content', wagtail.core.fields.RichTextField()), ('content_en', wagtail.core.fields.RichTextField(null=True)), ('content_pl', wagtail.core.fields.RichTextField(null=True)), ('date', models.DateTimeField(default=datetime.datetime.now, verbose_name='Post date')), ('featured_image', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='wagtailimages.Image')), ], options={ 'abstract': False, }, bases=('wagtailcore.page',), ), ]
blog/migrations/0001_initial.py
1,883
Generated by Django 3.0.5 on 2020-04-23 19:10
45
en
0.621572
# -*- coding: utf-8 -*- """ Tencent is pleased to support the open source community by making 蓝鲸智云PaaS平台社区版 (BlueKing PaaS Community Edition) available. Copyright (C) 2017-2020 THL A29 Limited, a Tencent company. All rights reserved. Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://opensource.org/licenses/MIT Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. """ import logging import re from django.conf import settings from django.contrib.admin.utils import flatten from django.utils.translation import ugettext_lazy as _ from pipeline.core.data.var import LazyVariable from pipeline_plugins.cmdb_ip_picker.utils import get_ip_picker_result from pipeline_plugins.base.utils.inject import supplier_account_for_project from pipeline_plugins.base.utils.adapter import cc_get_inner_ip_by_module_id from pipeline_plugins.components.utils import cc_get_ips_info_by_str from pipeline_plugins.components.utils.common import ip_re from gcloud.core.models import Project from gcloud.utils.cmdb import get_business_host from gcloud.utils.ip import get_ip_by_regex logger = logging.getLogger("root") class VarIpPickerVariable(LazyVariable): code = "ip" name = _("IP选择器(即将下线,请用新版)") type = "general" tag = "var_ip_picker.ip_picker" form = "%svariables/cmdb/var_ip_picker.js" % settings.STATIC_URL def get_value(self): var_ip_picker = self.value username = self.pipeline_data["executor"] project_id = self.pipeline_data["project_id"] project = Project.objects.get(id=project_id) bk_biz_id = project.bk_biz_id if project.from_cmdb else "" bk_supplier_account = supplier_account_for_project(project_id) produce_method = var_ip_picker["var_ip_method"] if produce_method == "custom": custom_value = var_ip_picker["var_ip_custom_value"] data = cc_get_ips_info_by_str(username, bk_biz_id, custom_value) ip_list = data["ip_result"] data = ",".join([ip["InnerIP"] for ip in ip_list]) else: ip_pattern = re.compile(ip_re) module_id_list = var_ip_picker["var_ip_tree"] module_inst_id_list = [] tree_ip_list = [] for custom_id in module_id_list: try: ip_or_module_id = custom_id.split("_")[-1] if ip_pattern.match(ip_or_module_id): # select certain ip tree_ip_list.append(ip_or_module_id) else: # select whole module module_inst_id_list.append(int(ip_or_module_id)) except Exception: logger.warning("ip_picker module ip transit failed: {origin}".format(origin=custom_id)) # query cc to get module's ip list and filter tree_ip_list host_list = cc_get_inner_ip_by_module_id( username, bk_biz_id, module_inst_id_list, bk_supplier_account, ["host_id", "bk_host_innerip"] ) cc_ip_list = cc_get_ips_info_by_str(username, bk_biz_id, ",".join(tree_ip_list))["ip_result"] select_ip = set() for host_info in host_list: select_ip.add(host_info["host"].get("bk_host_innerip", "")) for ip_info in cc_ip_list: select_ip.add(ip_info["InnerIP"]) data = ",".join(list(set(select_ip))) return data class VarCmdbIpSelector(LazyVariable): code = "ip_selector" name = _("IP选择器") type = "general" tag = "var_cmdb_ip_selector.ip_selector" form = "%svariables/cmdb/var_cmdb_ip_selector.js" % settings.STATIC_URL def get_value(self): username = self.pipeline_data["executor"] project_id = self.pipeline_data["project_id"] project = Project.objects.get(id=project_id) bk_biz_id = project.bk_biz_id if project.from_cmdb else "" bk_supplier_account = supplier_account_for_project(project_id) ip_selector = self.value ip_result = get_ip_picker_result(username, bk_biz_id, bk_supplier_account, ip_selector) # get for old value compatible if self.value.get("with_cloud_id", False): ip = ",".join(["{}:{}".format(host["bk_cloud_id"], host["bk_host_innerip"]) for host in ip_result["data"]]) else: ip = ",".join([host["bk_host_innerip"] for host in ip_result["data"]]) return ip class SetDetailData(object): def __init__(self, data, separator=","): self._value = data self.set_count = len(self._value) item_values = {} modules = [] total_ip_set = set() # verbose_ip_list 和 ip_module_list 元素一一对应 verbose_ip_list = [] verbose_ip_module_list = [] for item in data: set_name = item["bk_set_name"] for key, val in item.items(): if key == "__module": module_ips = flatten([mod["value"] for mod in val]) total_ip_set.update(module_ips) verbose_ip_list += module_ips verbose_ip_module_list += flatten( [["{}>{}".format(set_name, mod["key"])] * len(mod["value"]) for mod in val] ) item_module = {mod["key"]: separator.join(mod["value"]) for mod in val} modules.append(item_module) else: item_values.setdefault(key, []).append(val) for attr, attr_val in item_values.items(): setattr(self, attr, attr_val) flat_val = separator.join(map(str, attr_val)) setattr(self, "flat__{}".format(attr), flat_val) setattr(self, "_module", modules) setattr(self, "flat__ip_list", separator.join(list(total_ip_set))) setattr(self, "flat__verbose_ip_list", separator.join(verbose_ip_list)) setattr(self, "flat__verbose_ip_module_list", separator.join(verbose_ip_module_list)) self._pipeline_var_str_value = "Allocate {} sets with names: {}".format( self.set_count, separator.join(item_values["bk_set_name"]) ) def __repr__(self): return self._pipeline_var_str_value class VarCmdbSetAllocation(LazyVariable): code = "set_allocation" name = _("集群资源筛选") type = "general" tag = "var_cmdb_resource_allocation.set_allocation" form = "%svariables/cmdb/var_cmdb_resource_allocation.js" % settings.STATIC_URL def get_value(self): """ @summary: 返回 SetDetailData 对象 @note: 引用集群资源变量某一列某一行的属性,如 ${value.bk_set_name[0]} -> "集群1" @note: 引用集群资源变量某一列的全部属性,多行用换行符 `\n` 分隔,如 ${value.flat__bk_set_name} -> "集群1\n集群2" @note: 引用集群资源变量的模块分配的 IP ${value._module[0]["gamesvr"]} -> "127.0.0.1,127.0.0.2" @return: """ separator = self.value.get("separator", ",") return SetDetailData(self.value["data"], separator) class VarCmdbAttributeQuery(LazyVariable): code = "attribute_query" name = _("主机属性查询器") type = "general" tag = "var_cmdb_attr_query.attr_query" form = "%svariables/cmdb/var_cmdb_attribute_query.js" % settings.STATIC_URL def get_value(self): """ @summary: 返回 dict 对象,将每个可从CMDB查询到的输入IP作为键,将从CMDB查询到的主机属性封装成字典作为值 @note: 引用127.0.0.1的所有属性,如 ${value["127.0.0.1"]} -> {"bk_host_id": 999, "import_from": 3, ...} @note: 引用127.0.0.1的bk_host_id属性,如 ${value["127.0.0.1"]["bk_host_id"]} -> 999 @return: """ username = self.pipeline_data["executor"] project_id = self.pipeline_data["project_id"] project = Project.objects.get(id=project_id) bk_biz_id = project.bk_biz_id if project.from_cmdb else "" bk_supplier_account = supplier_account_for_project(project_id) ip_list = get_ip_by_regex(self.value) if not ip_list: return {} hosts_list = get_business_host( username, bk_biz_id, bk_supplier_account, [ "bk_cpu", "bk_isp_name", "bk_os_name", "bk_province_name", "bk_host_id", "import_from", "bk_os_version", "bk_disk", "operator", "bk_mem", "bk_host_name", "bk_host_innerip", "bk_comment", "bk_os_bit", "bk_outer_mac", "bk_asset_id", "bk_service_term", "bk_sla", "bk_cpu_mhz", "bk_host_outerip", "bk_state_name", "bk_os_type", "bk_mac", "bk_bak_operator", "bk_supplier_account", "bk_sn", "bk_cpu_module", ], ip_list, ) hosts = {} for host in hosts_list: ip = host["bk_host_innerip"] # bk_cloud_id as a dict is not needed if "bk_cloud_id" in host: host.pop("bk_cloud_id") hosts[ip] = host return hosts
pipeline_plugins/variables/collections/sites/open/cc.py
9,905
@summary: 返回 SetDetailData 对象 @note: 引用集群资源变量某一列某一行的属性,如 ${value.bk_set_name[0]} -> "集群1" @note: 引用集群资源变量某一列的全部属性,多行用换行符 ` ` 分隔,如 ${value.flat__bk_set_name} -> "集群1 集群2" @note: 引用集群资源变量的模块分配的 IP ${value._module[0]["gamesvr"]} -> "127.0.0.1,127.0.0.2" @return: @summary: 返回 dict 对象,将每个可从CMDB查询到的输入IP作为键,将从CMDB查询到的主机属性封装成字典作为值 @note: 引用127.0.0.1的所有属性,如 ${value["127.0.0.1"]} -> {"bk_host_id": 999, "import_from": 3, ...} @note: 引用127.0.0.1的bk_host_id属性,如 ${value["127.0.0.1"]["bk_host_id"]} -> 999 @return: Tencent is pleased to support the open source community by making 蓝鲸智云PaaS平台社区版 (BlueKing PaaS Community Edition) available. Copyright (C) 2017-2020 THL A29 Limited, a Tencent company. All rights reserved. Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://opensource.org/licenses/MIT Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -*- coding: utf-8 -*- select certain ip select whole module query cc to get module's ip list and filter tree_ip_list get for old value compatible verbose_ip_list 和 ip_module_list 元素一一对应 bk_cloud_id as a dict is not needed
1,469
en
0.546024
# Import packages import os from tensorflow.keras.optimizers import Adam from tensorflow.keras.datasets import mnist from sklearn.preprocessing import LabelBinarizer # Import model builder from model_build.py from model_build import DRbuild # Load MNIST dataset ((trainData, trainLabels), (testData, testLabels)) = mnist.load_data() # Add grayscale channel dimension trainData = trainData.reshape((trainData.shape[0], 28, 28, 1)) testData = testData.reshape((testData.shape[0], 28, 28, 1)) # Scale data to [0, 1] range trainData = trainData.astype("float32") / 255.0 testData = testData.astype("float32") / 255.0 # Enconde label to vector le = LabelBinarizer() trainLabels = le.fit_transform(trainLabels) testLabels = le.transform(testLabels) # starting learning rate LR = 1e-3 # Epochs to train EPOCHS = 10 # Batch size BS = 128 # Compile model opt = Adam(lr=LR) model = DRbuild(width=28, height=28, depth=1, classes=10) model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"]) # Train model H = model.fit( trainData, trainLabels, validation_data=(testData, testLabels), batch_size=BS, epochs=EPOCHS, verbose=1) # Evaluate model predictions = model.predict(testData) # Serialize model path = os.getcwd() path = path[:path.rfind('\\') + 1] path = path + r'models\digit_classifier.h5' model.save(path, save_format="h5")
src/model_train.py
1,390
Import packages Import model builder from model_build.py Load MNIST dataset Add grayscale channel dimension Scale data to [0, 1] range Enconde label to vector starting learning rate Epochs to train Batch size Compile model Train model Evaluate model Serialize model
265
en
0.697564
"""Account, user fixtures.""" import json import logging from time import monotonic, sleep from typing import List, NamedTuple, Optional, Tuple import pytest from box import Box from dynaconf import settings from ambra_sdk.exceptions.service import DuplicateName, NotEmpty from ambra_sdk.models import Group from ambra_sdk.service.filtering import Filter, FilterCondition from ambra_sdk.service.query import QueryO, QueryOPF logger = logging.getLogger(__name__) @pytest.fixture(scope='module') def storage_cluster(api, request): """Specific storage cluster. :param api: api :param request: pytest request :raises RuntimeError: Unknown cluster name :return: cluster box """ cluster_name = request.param cluster = None if cluster_name != 'DEFAULT': cluster = QueryOPF( api=api, url='/cluster/list', request_data={}, errors_mapping={}, paginated_field='clusters', required_sid=True, ).filter_by(Filter( 'name', FilterCondition.equals, cluster_name, )).first() if cluster is None: raise RuntimeError( 'Unknown cluster name {name}'.format(name=cluster_name), ) return cluster class UserParams(NamedTuple): """User params.""" account: Box user: Box class GroupParams(NamedTuple): """Group params.""" uuid: str namespace_id: str name: str def create_account(api, account_name: str) -> Tuple[Box, Box]: """Create new account. :param api: api :param account_name: account name :raises RuntimeError: Cant find account :return: user params """ # If account exists - raise DuplicateName error QueryO( api=api, url='/account/add', request_data={ 'name': account_name, }, errors_mapping={ 'DUPLICATE_NAME': DuplicateName(), }, required_sid=True, ).get() account = api \ .Account \ .list() \ .filter_by( Filter( 'name', FilterCondition.equals, account_name, ), ).first() if account is None: raise RuntimeError('Cant find test account') # set role permissions admin_role = api \ .Role \ .list(account_id=account.uuid) \ .filter_by( Filter( 'name', FilterCondition.equals, 'Administrator', ), ).first() if admin_role is None: raise RuntimeError('Cant find admin role') api.Role.set( uuid=admin_role.uuid, permissions=json.dumps( { 'study_delete': 1, 'study_duplicate': 1, 'study_split': 1, 'study_merge': 1, 'study_delete_image': 1, }, ), ).get() user = api.User.get(account_id=account.uuid).get() logger.info('Created account %s', account.name) return (account, user) def account_studies(api, account) -> List[Box]: """List of account studies. :param api: api :param account: account :return: list of studies """ account_namespaces = [account.namespace_id] group_namespaces = [ group.namespace_id for group in api.Group.list(account_id=account.uuid).only(Group.namespace_id).all() ] account_namespaces.extend(group_namespaces) # Method study list does not support in_condition filtering for namespace ! acc_studies = [] for account_namespace in account_namespaces: studies = api \ .Study \ .list() \ .filter_by( Filter( field_name='phi_namespace', condition=FilterCondition.equals, value=account_namespace, ), ).all() acc_studies.extend(list(studies)) return acc_studies def delete_account(api, account) -> Box: """Delete account. :param api: api :param account: account :raises RuntimeError: if account have undeleted studies """ try: QueryO( api=api, url='/account/delete/', request_data={ 'uuid': account.uuid, }, errors_mapping={ 'NOT_EMPTY': NotEmpty(), }, required_sid=True, ).get() except NotEmpty: acc_studies = account_studies(api, account) raise RuntimeError( 'Account have undeleted studies:\n{studies}'.format( studies='\n'.join( [ str((study.uuid, study.study_uid)) for study in acc_studies ], ), ), ) def clear_studies(api, account): """Delete account studies. :param api: api :param account: account """ account_namespaces = [account.namespace_id] group_namespaces = [ group.namespace_id for group in api.Group.list(account_id=account.uuid).only(Group.namespace_id).all() ] account_namespaces.extend(group_namespaces) # Method study list does not support in_condition filtering for namespace ! # So delete studies in loop for account_namespace in account_namespaces: studies = api \ .Study \ .list() \ .filter_by( Filter( field_name='phi_namespace', condition=FilterCondition.equals, value=account_namespace, ), ).all() for study in studies: study_uid = study.uuid logger.error('Remove undeleted study %s', study_uid) api.Study.delete(uuid=study_uid).get() @pytest.fixture(scope='module') # NOQA:WPS210,WPS231 def account(api, storage_cluster): """Get account. :param api: ambra api :param storage_cluster: storage cluster :yields: test account :raises RuntimeError: On deleted account with existing studies :raises TimeoutError: Time for waiting account deletion is out """ account_name = settings.TEST_ACCOUNT_NAME if storage_cluster: account_name = '{account}_{cluster}'.format( account=account_name, cluster=storage_cluster.name, ) try: account, user = create_account(api, account_name) except DuplicateName: logger.error('Duplicated account: %s', account_name) account = api \ .Account \ .list() \ .filter_by( Filter( 'name', FilterCondition.equals, account_name, ), ).first() if account is None: raise RuntimeError('Account duplicated but not exists') clear_studies(api, account) delete_account(api, account) account, user = create_account(api, account_name) if storage_cluster is not None: QueryO( api=api, url='/cluster/account/bind', request_data={ 'account_id': account.uuid, 'cluster_id': storage_cluster.uuid, }, errors_mapping={}, required_sid=True, ).get() logger.info( 'Bind account to storage cluster {name}'.format( name=storage_cluster.name, ), ) yield UserParams( account=account, user=user, ) delete_account(api, account) start = monotonic() while True: if monotonic() - start >= settings.API['account_deletion_timeout']: raise TimeoutError('Account still exists') account = api \ .Account \ .list() \ .filter_by( Filter( 'name', FilterCondition.equals, account_name, ), ).first() if account is None: return sleep(settings.API['account_deletion_check_interval']) @pytest.fixture def create_group(api, account): """Create new group in account. :param api: api fixture :param account: account fixture :yields: create_group function """ groups = [] group_counter = 0 def _create_group(name: Optional[str] = None): nonlocal group_counter group_counter += 1 if name is None: name = 'SDK_TEST_GROUP_{gnum}'.format(gnum=group_counter) account_id = account.account.uuid response = api.Group.add( account_id=account_id, name=name, ).get() group = GroupParams( uuid=response.uuid, namespace_id=response.namespace_id, name=name, ) groups.append(group) # add account user to the group api.Group.user_add( uuid=group.uuid, user_id=account.user.uuid, ).get() return group yield _create_group for group in groups: api.Group.delete(uuid=group.uuid).get()
tests/fixtures/account.py
9,320
Group params. User params. Get account. :param api: ambra api :param storage_cluster: storage cluster :yields: test account :raises RuntimeError: On deleted account with existing studies :raises TimeoutError: Time for waiting account deletion is out List of account studies. :param api: api :param account: account :return: list of studies Delete account studies. :param api: api :param account: account Create new account. :param api: api :param account_name: account name :raises RuntimeError: Cant find account :return: user params Create new group in account. :param api: api fixture :param account: account fixture :yields: create_group function Delete account. :param api: api :param account: account :raises RuntimeError: if account have undeleted studies Specific storage cluster. :param api: api :param request: pytest request :raises RuntimeError: Unknown cluster name :return: cluster box Account, user fixtures. If account exists - raise DuplicateName error set role permissions Method study list does not support in_condition filtering for namespace ! Method study list does not support in_condition filtering for namespace ! So delete studies in loop NOQA:WPS210,WPS231 add account user to the group
1,226
en
0.691583
import cv2 as cv import numpy as np from matplotlib import pyplot as plt #直方图反向投影 def bitwise_and(): small = cv.imread("C:/1/image/small.jpg") big = cv.imread("C:/1/image/big.jpg") small_hsv = cv.cvtColor(small, cv.COLOR_BGR2HSV) big_hsv = cv.cvtColor(big, cv.COLOR_BGR2HSV) """ h,s,v = cv.split(small_hsv) print(h) print(s) print(v) """ lower_hsv = np.array([1, 120, 240]) upper_hsv = np.array([4, 160, 255]) mask = cv.inRange(big_hsv, lower_hsv, upper_hsv) dest = cv.bitwise_and(big_hsv, big_hsv, mask=mask) cv.imshow('mask', dest) cv.imshow('video', big) def back_projection_demo(): sample = cv.imread("C:/1/image/small.jpg") target = cv.imread("C:/1/image/big.jpg") roi_hsv = cv.cvtColor(sample,cv.COLOR_BGR2HSV) target_hsv = cv.cvtColor(target,cv.COLOR_BGR2HSV) #show images cv.imshow("sample",sample) cv.imshow("target",target) roiHist = cv.calcHist([roi_hsv],[0,1],None,[32,32],[0,180,0,256]) #求出样本直方图 cv.normalize(roiHist,roiHist,0,256,cv.NORM_MINMAX) #直方图归一化 dest = cv.calcBackProject([target_hsv],[0,1],roiHist,[0,180,0,256],1) #直方图反向投影 cv.imshow("back_projection_demo", dest) def hist2d_demo(image): hsv = cv.cvtColor(image,cv.COLOR_BGR2HSV) hist = cv.calcHist([image],[0,1],None,[32,32],[0,180,0,256]) # cv.imshow("hist2d_demo",hist) plt.imshow(hist,interpolation='nearest') plt.title("2D Histogram") plt.show() src = cv.imread("C:/1/1.jpg") # cv.namedWindow('input_image', cv.WINDOW_AUTOSIZE) # cv.imshow("input_image",src) bitwise_and() cv.waitKey(0) cv.destroyAllWindows()
11_ Histogram3.py
1,732
直方图反向投影show images求出样本直方图直方图归一化直方图反向投影 cv.imshow("hist2d_demo",hist) cv.namedWindow('input_image', cv.WINDOW_AUTOSIZE) cv.imshow("input_image",src)
147
en
0.191359
# coding: utf-8 """ Kubernetes No description provided (generated by Swagger Codegen https://github.com/swagger-api/swagger-codegen) OpenAPI spec version: v1.10.0 Generated by: https://github.com/swagger-api/swagger-codegen.git """ from pprint import pformat from six import iteritems import re class V2beta1CrossVersionObjectReference(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 = { 'api_version': 'str', 'kind': 'str', 'name': 'str' } attribute_map = { 'api_version': 'apiVersion', 'kind': 'kind', 'name': 'name' } def __init__(self, api_version=None, kind=None, name=None): """ V2beta1CrossVersionObjectReference - a model defined in Swagger """ self._api_version = None self._kind = None self._name = None self.discriminator = None if api_version is not None: self.api_version = api_version self.kind = kind self.name = name @property def api_version(self): """ Gets the api_version of this V2beta1CrossVersionObjectReference. API version of the referent :return: The api_version of this V2beta1CrossVersionObjectReference. :rtype: str """ return self._api_version @api_version.setter def api_version(self, api_version): """ Sets the api_version of this V2beta1CrossVersionObjectReference. API version of the referent :param api_version: The api_version of this V2beta1CrossVersionObjectReference. :type: str """ self._api_version = api_version @property def kind(self): """ Gets the kind of this V2beta1CrossVersionObjectReference. Kind of the referent; More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds\" :return: The kind of this V2beta1CrossVersionObjectReference. :rtype: str """ return self._kind @kind.setter def kind(self, kind): """ Sets the kind of this V2beta1CrossVersionObjectReference. Kind of the referent; More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds\" :param kind: The kind of this V2beta1CrossVersionObjectReference. :type: str """ if kind is None: raise ValueError("Invalid value for `kind`, must not be `None`") self._kind = kind @property def name(self): """ Gets the name of this V2beta1CrossVersionObjectReference. Name of the referent; More info: http://kubernetes.io/docs/user-guide/identifiers#names :return: The name of this V2beta1CrossVersionObjectReference. :rtype: str """ return self._name @name.setter def name(self, name): """ Sets the name of this V2beta1CrossVersionObjectReference. Name of the referent; More info: http://kubernetes.io/docs/user-guide/identifiers#names :param name: The name of this V2beta1CrossVersionObjectReference. :type: str """ if name is None: raise ValueError("Invalid value for `name`, must not be `None`") self._name = name def to_dict(self): """ Returns the model properties as a dict """ result = {} for attr, _ in 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 return result def to_str(self): """ Returns the string representation of the model """ return 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, V2beta1CrossVersionObjectReference): return False return self.__dict__ == other.__dict__ def __ne__(self, other): """ Returns true if both objects are not equal """ return not self == other
kubernetes/client/models/v2beta1_cross_version_object_reference.py
5,161
NOTE: This class is auto generated by the swagger code generator program. Do not edit the class manually. Returns true if both objects are equal V2beta1CrossVersionObjectReference - a model defined in Swagger Returns true if both objects are not equal For `print` and `pprint` Gets the api_version of this V2beta1CrossVersionObjectReference. API version of the referent :return: The api_version of this V2beta1CrossVersionObjectReference. :rtype: str Sets the api_version of this V2beta1CrossVersionObjectReference. API version of the referent :param api_version: The api_version of this V2beta1CrossVersionObjectReference. :type: str Gets the kind of this V2beta1CrossVersionObjectReference. Kind of the referent; More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds" :return: The kind of this V2beta1CrossVersionObjectReference. :rtype: str Sets the kind of this V2beta1CrossVersionObjectReference. Kind of the referent; More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds" :param kind: The kind of this V2beta1CrossVersionObjectReference. :type: str Gets the name of this V2beta1CrossVersionObjectReference. Name of the referent; More info: http://kubernetes.io/docs/user-guide/identifiers#names :return: The name of this V2beta1CrossVersionObjectReference. :rtype: str Sets the name of this V2beta1CrossVersionObjectReference. Name of the referent; More info: http://kubernetes.io/docs/user-guide/identifiers#names :param name: The name of this V2beta1CrossVersionObjectReference. :type: str Returns the model properties as a dict Returns the string representation of the model Kubernetes No description provided (generated by Swagger Codegen https://github.com/swagger-api/swagger-codegen) OpenAPI spec version: v1.10.0 Generated by: https://github.com/swagger-api/swagger-codegen.git coding: utf-8
1,889
en
0.641615
from flask import Flask, redirect, render_template, request import os import sys from flasgger import Swagger from server import app from server.routes.prometheus import track_requests from os import path from userapp import improcess import base64 app=Flask(__name__) global images images = [] # The python-flask stack includes the flask extension flasgger, which will build # and publish your swagger ui and specification at the /apidocs url. Here we set up # the basic swagger attributes, which you should modify to match you application. # See: https://github.com/rochacbruno-archive/flasgger swagger_template = { "swagger": "2.0", "info": { "title": "Example API for python-flask stack", "description": "API for helloworld, plus health/monitoring", "contact": { "responsibleOrganization": "IBM", "responsibleDeveloper": "Henry Nash", "email": "henry.nash@uk.ibm.com", "url": "https://appsody.dev", }, "version": "0.2" }, "schemes": [ "http" ], } swagger = Swagger(app, template=swagger_template) # The python-flask stack includes the prometheus metrics engine. You can ensure your endpoints # are included in these metrics by enclosing them in the @track_requests wrapper. @app.route('/hello') @track_requests def HelloWorld(): # To include an endpoint in the swagger ui and specification, we include a docstring that # defines the attributes of this endpoint. """A hello message Example endpoint returning a hello message --- responses: 200: description: A successful reply examples: text/plain: Hello from Appsody! """ return 'Hello from Appsody!' @app.route("/home") def home(): return "Your image preprocessor application test is successful" @app.route('/process', methods = ['POST']) @track_requests def processing_image(): if request.method == 'POST': f = request.files['file'] # create a secure filename filename = f.filename # save file filepath = os.path.join("./userapp/",filename) f.save(filepath) # convert image to grayscale filepath_processed = improcess.preprocessing(filepath); #return render_template("display.html") result = {} j = 0 for i in filepath_processed: images.append(i) result[j]=i j=j+1 print(images) return result @app.route('/getimages') @track_requests def get_image(): k={} j = 0 for i in images: path_image = "./userapp" + i print(path_image) f = open(path_image, "rb") stri = base64.b64encode(f.read()) result = stri.decode('ASCII') k[i]=result j = j+1 images.clear() return k # It is considered bad form to return an error for '/', so let's redirect to the apidocs @app.route('/') def index(): return redirect('/apidocs') # If you have additional modules that contain your API endpoints, for instance # using Blueprints, then ensure that you use relative imports, e.g.: # from .mymodule import myblueprint
sources/image_preprocessor/__init__.py
3,039
A hello message Example endpoint returning a hello message --- responses: 200: description: A successful reply examples: text/plain: Hello from Appsody! The python-flask stack includes the flask extension flasgger, which will build and publish your swagger ui and specification at the /apidocs url. Here we set up the basic swagger attributes, which you should modify to match you application. See: https://github.com/rochacbruno-archive/flasgger The python-flask stack includes the prometheus metrics engine. You can ensure your endpoints are included in these metrics by enclosing them in the @track_requests wrapper. To include an endpoint in the swagger ui and specification, we include a docstring that defines the attributes of this endpoint. create a secure filename save file convert image to grayscalereturn render_template("display.html") It is considered bad form to return an error for '/', so let's redirect to the apidocs If you have additional modules that contain your API endpoints, for instance using Blueprints, then ensure that you use relative imports, e.g.: from .mymodule import myblueprint
1,131
en
0.811467
import tensorflow as tf from tensorflow.keras.callbacks import ModelCheckpoint import os class TensorBoardFix(tf.keras.callbacks.TensorBoard): """ This fixes incorrect step values when using the TensorBoard callback with custom summary ops https://stackoverflow.com/questions/64642944/steps-of-tf-summary-operations-in-tensorboard-are-always-0 """ def on_train_begin(self, *args, **kwargs): super(TensorBoardFix, self).on_train_begin(*args, **kwargs) tf.summary.experimental.set_step(self._train_step) def on_test_begin(self, *args, **kwargs): super(TensorBoardFix, self).on_test_begin(*args, **kwargs) tf.summary.experimental.set_step(self._val_step) def get_callbacks(model_name='model',root_dir='logs/fit/', monitor='val_categorical_accuracy',mode='max', save_freq='epoch',save_best_only=True, ): log_dir = os.path.join(root_dir,model_name) tensorboard = TensorBoardFix(log_dir=log_dir, histogram_freq=1, update_freq=50, ) save_model = ModelCheckpoint(filepath=os.path.join(log_dir,'model.h5'), save_weights_only=False, monitor=monitor, mode=mode, save_best_only=save_best_only, save_freq=save_freq) return [tensorboard,save_model]
convRFF/utils/utils.py
1,527
This fixes incorrect step values when using the TensorBoard callback with custom summary ops https://stackoverflow.com/questions/64642944/steps-of-tf-summary-operations-in-tensorboard-are-always-0
196
en
0.477437
# MIT License # # Copyright (C) IBM Corporation 2018 # # 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. from __future__ import absolute_import, division, print_function, unicode_literals import logging import unittest import numpy as np from art.attacks import VirtualAdversarialMethod from art.classifiers import KerasClassifier from art.classifiers.classifier import ClassifierNeuralNetwork, ClassifierGradients from art.utils import get_labels_np_array from tests.utils import TestBase from tests.utils import get_image_classifier_tf, get_image_classifier_kr, get_image_classifier_pt from tests.utils import get_tabular_classifier_tf, get_tabular_classifier_kr, get_tabular_classifier_pt from tests.attacks.utils import backend_test_classifier_type_check_fail logger = logging.getLogger(__name__) class TestVirtualAdversarial(TestBase): @classmethod def setUpClass(cls): super().setUpClass() cls.n_train = 100 cls.n_test = 10 cls.x_train_mnist = cls.x_train_mnist[0 : cls.n_train] cls.y_train_mnist = cls.y_train_mnist[0 : cls.n_train] cls.x_test_mnist = cls.x_test_mnist[0 : cls.n_test] cls.y_test_mnist = cls.y_test_mnist[0 : cls.n_test] def test_keras_mnist(self): classifier = get_image_classifier_kr() scores = classifier._model.evaluate(self.x_train_mnist, self.y_train_mnist) logging.info("[Keras, MNIST] Accuracy on training set: %.2f%%", (scores[1] * 100)) scores = classifier._model.evaluate(self.x_test_mnist, self.y_test_mnist) logging.info("[Keras, MNIST] Accuracy on test set: %.2f%%", (scores[1] * 100)) self._test_backend_mnist(classifier, self.x_test_mnist, self.y_test_mnist) def test_tensorflow_mnist(self): classifier, sess = get_image_classifier_tf(from_logits=False) scores = get_labels_np_array(classifier.predict(self.x_train_mnist)) acc = np.sum(np.argmax(scores, axis=1) == np.argmax(self.y_train_mnist, axis=1)) / self.y_train_mnist.shape[0] logger.info("[TF, MNIST] Accuracy on training set: %.2f%%", (acc * 100)) scores = get_labels_np_array(classifier.predict(self.x_test_mnist)) acc = np.sum(np.argmax(scores, axis=1) == np.argmax(self.y_test_mnist, axis=1)) / self.y_test_mnist.shape[0] logger.info("[TF, MNIST] Accuracy on test set: %.2f%%", (acc * 100)) self._test_backend_mnist(classifier, self.x_test_mnist, self.y_test_mnist) def test_pytorch_mnist(self): x_train_mnist = np.swapaxes(self.x_train_mnist, 1, 3).astype(np.float32) x_test_mnist = np.swapaxes(self.x_test_mnist, 1, 3).astype(np.float32) classifier = get_image_classifier_pt() scores = get_labels_np_array(classifier.predict(x_train_mnist)) acc = np.sum(np.argmax(scores, axis=1) == np.argmax(self.y_train_mnist, axis=1)) / self.y_train_mnist.shape[0] logger.info("[PyTorch, MNIST] Accuracy on training set: %.2f%%", (acc * 100)) scores = get_labels_np_array(classifier.predict(x_test_mnist)) acc = np.sum(np.argmax(scores, axis=1) == np.argmax(self.y_test_mnist, axis=1)) / self.y_test_mnist.shape[0] logger.info("[PyTorch, MNIST] Accuracy on test set: %.2f%%", (acc * 100)) self._test_backend_mnist(classifier, x_test_mnist, self.y_test_mnist) def _test_backend_mnist(self, classifier, x_test, y_test): x_test_original = x_test.copy() df = VirtualAdversarialMethod(classifier, batch_size=100, max_iter=2) x_test_adv = df.generate(x_test) self.assertFalse((x_test == x_test_adv).all()) y_pred = get_labels_np_array(classifier.predict(x_test_adv)) self.assertFalse((y_test == y_pred).all()) acc = np.sum(np.argmax(y_pred, axis=1) == np.argmax(y_test, axis=1)) / y_test.shape[0] logger.info("Accuracy on adversarial examples: %.2f%%", (acc * 100)) # Check that x_test has not been modified by attack and classifier self.assertAlmostEqual(float(np.max(np.abs(x_test_original - x_test))), 0.0, delta=0.00001) def test_classifier_type_check_fail(self): backend_test_classifier_type_check_fail( VirtualAdversarialMethod, [ClassifierNeuralNetwork, ClassifierGradients] ) def test_keras_iris_clipped(self): classifier = get_tabular_classifier_kr() # Test untargeted attack attack = VirtualAdversarialMethod(classifier, eps=0.1) x_test_iris_adv = attack.generate(self.x_test_iris) self.assertFalse((self.x_test_iris == x_test_iris_adv).all()) self.assertTrue((x_test_iris_adv <= 1).all()) self.assertTrue((x_test_iris_adv >= 0).all()) preds_adv = np.argmax(classifier.predict(x_test_iris_adv), axis=1) self.assertFalse((np.argmax(self.y_test_iris, axis=1) == preds_adv).all()) acc = np.sum(preds_adv == np.argmax(self.y_test_iris, axis=1)) / self.y_test_iris.shape[0] logger.info("Accuracy on Iris with VAT adversarial examples: %.2f%%", (acc * 100)) def test_keras_iris_unbounded(self): classifier = get_tabular_classifier_kr() # Recreate a classifier without clip values classifier = KerasClassifier(model=classifier._model, use_logits=False, channel_index=1) attack = VirtualAdversarialMethod(classifier, eps=1) x_test_iris_adv = attack.generate(self.x_test_iris) self.assertFalse((self.x_test_iris == x_test_iris_adv).all()) self.assertTrue((x_test_iris_adv > 1).any()) self.assertTrue((x_test_iris_adv < 0).any()) preds_adv = np.argmax(classifier.predict(x_test_iris_adv), axis=1) self.assertFalse((np.argmax(self.y_test_iris, axis=1) == preds_adv).all()) acc = np.sum(preds_adv == np.argmax(self.y_test_iris, axis=1)) / self.y_test_iris.shape[0] logger.info("Accuracy on Iris with VAT adversarial examples: %.2f%%", (acc * 100)) # def test_iris_tf(self): # classifier, _ = get_iris_classifier_tf() # # attack = VirtualAdversarialMethod(classifier, eps=.1) # x_test_adv = attack.generate(x_test) # #print(np.min(x_test_adv), np.max(x_test_adv), np.min(x_test), np.max(x_test)) # self.assertFalse((x_test == x_test_adv).all()) # self.assertTrue((x_test_adv <= 1).all()) # self.assertTrue((x_test_adv >= 0).all()) # # preds_adv = np.argmax(classifier.predict(x_test_adv), axis=1) # self.assertFalse((np.argmax(y_test, axis=1) == preds_adv).all()) # acc = np.sum(preds_adv == np.argmax(y_test, axis=1)) / y_test.shape[0] # logger.info('Accuracy on Iris with VAT adversarial examples: %.2f%%', (acc * 100)) # def test_iris_pt(self): # (_, _), (x_test, y_test) = self.iris # classifier = get_iris_classifier_pt() # # attack = VirtualAdversarialMethod(classifier, eps=.1) # x_test_adv = attack.generate(x_test.astype(np.float32)) # #print(np.min(x_test_adv), np.max(x_test_adv), np.min(x_test), np.max(x_test)) # self.assertFalse((x_test == x_test_adv).all()) # self.assertTrue((x_test_adv <= 1).all()) # self.assertTrue((x_test_adv >= 0).all()) # # preds_adv = np.argmax(classifier.predict(x_test_adv), axis=1) # self.assertFalse((np.argmax(y_test, axis=1) == preds_adv).all()) # acc = np.sum(preds_adv == np.argmax(y_test, axis=1)) / y_test.shape[0] # logger.info('Accuracy on Iris with VAT adversarial examples: %.2f%%', (acc * 100)) def test_tensorflow_iris(self): classifier, _ = get_tabular_classifier_tf() attack = VirtualAdversarialMethod(classifier, eps=0.1) with self.assertRaises(TypeError) as context: x_test_iris_adv = attack.generate(self.x_test_iris) self.assertIn( "This attack requires a classifier predicting probabilities in the range [0, 1] as output." "Values smaller than 0.0 or larger than 1.0 have been detected.", str(context.exception), ) def test_pytorch_iris(self): classifier = get_tabular_classifier_pt() attack = VirtualAdversarialMethod(classifier, eps=0.1) with self.assertRaises(TypeError) as context: x_test_iris_adv = attack.generate(self.x_test_iris.astype(np.float32)) self.assertIn( "This attack requires a classifier predicting probabilities in the range [0, 1] as output." "Values smaller than 0.0 or larger than 1.0 have been detected.", str(context.exception), ) if __name__ == "__main__": unittest.main()
tests/attacks/test_virtual_adversarial.py
9,657
MIT License Copyright (C) IBM Corporation 2018 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. Check that x_test has not been modified by attack and classifier Test untargeted attack Recreate a classifier without clip values def test_iris_tf(self): classifier, _ = get_iris_classifier_tf() attack = VirtualAdversarialMethod(classifier, eps=.1) x_test_adv = attack.generate(x_test) print(np.min(x_test_adv), np.max(x_test_adv), np.min(x_test), np.max(x_test)) self.assertFalse((x_test == x_test_adv).all()) self.assertTrue((x_test_adv <= 1).all()) self.assertTrue((x_test_adv >= 0).all()) preds_adv = np.argmax(classifier.predict(x_test_adv), axis=1) self.assertFalse((np.argmax(y_test, axis=1) == preds_adv).all()) acc = np.sum(preds_adv == np.argmax(y_test, axis=1)) / y_test.shape[0] logger.info('Accuracy on Iris with VAT adversarial examples: %.2f%%', (acc * 100)) def test_iris_pt(self): (_, _), (x_test, y_test) = self.iris classifier = get_iris_classifier_pt() attack = VirtualAdversarialMethod(classifier, eps=.1) x_test_adv = attack.generate(x_test.astype(np.float32)) print(np.min(x_test_adv), np.max(x_test_adv), np.min(x_test), np.max(x_test)) self.assertFalse((x_test == x_test_adv).all()) self.assertTrue((x_test_adv <= 1).all()) self.assertTrue((x_test_adv >= 0).all()) preds_adv = np.argmax(classifier.predict(x_test_adv), axis=1) self.assertFalse((np.argmax(y_test, axis=1) == preds_adv).all()) acc = np.sum(preds_adv == np.argmax(y_test, axis=1)) / y_test.shape[0] logger.info('Accuracy on Iris with VAT adversarial examples: %.2f%%', (acc * 100))
2,631
en
0.571946
# Copyright 2021 Zilliz. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from typing import Dict from towhee.engine.operator_runner import runner_base from towhee.engine.operator_runner import map_runner from towhee.engine.operator_runner import concat_runner from towhee.engine.operator_runner import flatmap_runner from towhee.engine.operator_runner import filter_runner from towhee.engine.operator_runner import window_runner from towhee.engine.operator_runner import generator_runner from towhee.engine.operator_io.reader import DataFrameReader from towhee.engine.operator_io.writer import DataFrameWriter def create_runner( runner_type: str, name: str, index: int, op_name: str, tag: str, hub_op_id: str, op_args: Dict[str, any], reader: DataFrameReader, writer: DataFrameWriter, ) -> runner_base.RunnerBase: if runner_type.lower() == 'map': return map_runner.MapRunner(name, index, op_name, tag, hub_op_id, op_args, reader, writer) elif runner_type.lower() == 'flatmap': return flatmap_runner.FlatMapRunner(name, index, op_name, tag, hub_op_id, op_args, reader, writer) elif runner_type.lower() == 'filter': return filter_runner.FilterRunner(name, index, op_name, tag, hub_op_id, op_args, reader, writer) elif runner_type.lower() == 'concat': return concat_runner.ConcatRunner(name, index, op_name, tag, hub_op_id, op_args, reader, writer) elif runner_type.lower() in ['window', 'time_window']: return window_runner.WindowRunner(name, index, op_name, tag, hub_op_id, op_args, reader, writer) elif runner_type.lower() == 'generator': return generator_runner.GeneratorRunner(name, index, op_name, tag, hub_op_id, op_args, reader, writer) else: raise AttributeError('No runner type named: {}'.format(runner_type))
towhee/engine/operator_runner/__init__.py
2,360
Copyright 2021 Zilliz. 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.
565
en
0.859254
#!/usr/bin/env python # -*- coding: utf-8 -*- # sylco.py """ Combined algorithm by EA : http://about.me/emre.aydin http://github.com/eaydin 1) if letters < 3 : return 1 2) if doesn't end with "ted" or "tes" or "ses" or "ied", discard "es" and "ed" at the end. 3) discard trailing "e", except where ending is "le", also handle "le_exceptions" 4) check if consecutive vowels exists, triplets or pairs, count them as one. 5) count remaining vowels in word. 6) add one if starts with "mc" 7) add one if ends with "y" but is not surrouned by vowel 8) add one if "y" is surrounded by non-vowels and is not in the last word. 9) if starts with "tri-" or "bi-" and is followed by a vowel, add one. 10) if ends with "-ian", should be counted as two syllables, except for "-tian" and "-cian" 11) if starts with "co-" and is followed by a vowel, check if exists in the double syllable dictionary, if not, check if in single dictionary and act accordingly. 12) if starts with "pre-" and is followed by a vowel, check if exists in the double syllable dictionary, if not, check if in single dictionary and act accordingly. 13) check for "-n't" and cross match with dictionary to add syllable. 14) handling the exceptional words TODO : # isn't couldn't doesn't shouldn't ... (done) # when detecting sentences, avoid "a.m. p.m." kind of usage. # exception : "evacuate" "ambulances" "shuttled" "anyone" """ import re import string def getsentences(the_text) : sents = re.findall(r"[A-Z].*?[\.!?]", the_text, re.M | re.DOTALL) # sents_lo = re.findall(r"[a-z].*?[\.!?]", the_text) return sents def getwords(sentence) : x = re.sub('['+string.punctuation+']', '', sentence).split() return x def sylco(word) : word = word.lower() # exception_add are words that need extra syllables # exception_del are words that need less syllables exception_add = ['serious','crucial'] exception_del = ['fortunately','unfortunately'] co_one = ['cool','coach','coat','coal','count','coin','coarse','coup','coif','cook','coign','coiffe','coof','court'] co_two = ['coapt','coed','coinci'] pre_one = ['preach'] syls = 0 #added syllable number disc = 0 #discarded syllable number #1) if letters < 3 : return 1 if len(word) <= 3 : syls = 1 return syls #2) if doesn't end with "ted" or "tes" or "ses" or "ied" or "ies", discard "es" and "ed" at the end. # if it has only 1 vowel or 1 set of consecutive vowels, discard. (like "speed", "fled" etc.) if word[-2:] == "es" or word[-2:] == "ed" : doubleAndtripple_1 = len(re.findall(r'[eaoui][eaoui]',word)) if doubleAndtripple_1 > 1 or len(re.findall(r'[eaoui][^eaoui]',word)) > 1 : if word[-3:] == "ted" or word[-3:] == "tes" or word[-3:] == "ses" or word[-3:] == "ied" or word[-3:] == "ies" : pass else : disc+=1 #3) discard trailing "e", except where ending is "le" le_except = ['whole','mobile','pole','male','female','hale','pale','tale','sale','aisle','whale','while'] if word[-1:] == "e" : if word[-2:] == "le" and word not in le_except : pass else : disc+=1 #4) check if consecutive vowels exists, triplets or pairs, count them as one. doubleAndtripple = len(re.findall(r'[eaoui][eaoui]',word)) tripple = len(re.findall(r'[eaoui][eaoui][eaoui]',word)) disc+=doubleAndtripple + tripple #5) count remaining vowels in word. numVowels = len(re.findall(r'[eaoui]',word)) #6) add one if starts with "mc" if word[:2] == "mc" : syls+=1 #7) add one if ends with "y" but is not surrouned by vowel if word[-1:] == "y" and word[-2] not in "aeoui" : syls +=1 #8) add one if "y" is surrounded by non-vowels and is not in the last word. for i,j in enumerate(word) : if j == "y" : if (i != 0) and (i != len(word)-1) : if word[i-1] not in "aeoui" and word[i+1] not in "aeoui" : syls+=1 #9) if starts with "tri-" or "bi-" and is followed by a vowel, add one. if word[:3] == "tri" and word[3] in "aeoui" : syls+=1 if word[:2] == "bi" and word[2] in "aeoui" : syls+=1 #10) if ends with "-ian", should be counted as two syllables, except for "-tian" and "-cian" if word[-3:] == "ian" : #and (word[-4:] != "cian" or word[-4:] != "tian") : if word[-4:] == "cian" or word[-4:] == "tian" : pass else : syls+=1 #11) if starts with "co-" and is followed by a vowel, check if exists in the double syllable dictionary, if not, check if in single dictionary and act accordingly. if word[:2] == "co" and word[2] in 'eaoui' : if word[:4] in co_two or word[:5] in co_two or word[:6] in co_two : syls+=1 elif word[:4] in co_one or word[:5] in co_one or word[:6] in co_one : pass else : syls+=1 #12) if starts with "pre-" and is followed by a vowel, check if exists in the double syllable dictionary, if not, check if in single dictionary and act accordingly. if word[:3] == "pre" and word[3] in 'eaoui' : if word[:6] in pre_one : pass else : syls+=1 #13) check for "-n't" and cross match with dictionary to add syllable. negative = ["doesn't", "isn't", "shouldn't", "couldn't","wouldn't"] if word[-3:] == "n't" : if word in negative : syls+=1 else : pass #14) Handling the exceptional words. if word in exception_del : disc+=1 if word in exception_add : syls+=1 # calculate the output return numVowels - disc + syls def getFlesch(article) : sentencelist = getsentences(article) sentencesN = len(sentencelist) syllablesN = 0 wordsN = 0 for sentence in sentencelist : wordslist = getwords(sentence) wordsN += len(wordslist) for word in wordslist : word = word.replace('\n','') x = sylco(word) syllablesN += x """ print("Sentences : %i" % sentencesN) print("Words : %i" % wordsN) print("Syllables : %i" % syllablesN) """ asl = wordsN / sentencesN asw = syllablesN / wordsN # flesh = (0.39 * (wordsN / sentencesN)) + (11.8 * (syllablesN / wordsN)) - 15.59 flesch = 206.835 - (1.015 * asl) - (84.6 * asw) # http://office.microsoft.com/en-us/word-help/test-your-document-s-readability-HP010148506.aspx return flesch def getsyls(article) : wordslist = getwords(article) syllables = 0 for i in wordslist : x = sylco(i) syllables += x return syllables
sylco.py
6,762
Combined algorithm by EA : http://about.me/emre.aydin http://github.com/eaydin 1) if letters < 3 : return 1 2) if doesn't end with "ted" or "tes" or "ses" or "ied", discard "es" and "ed" at the end. 3) discard trailing "e", except where ending is "le", also handle "le_exceptions" 4) check if consecutive vowels exists, triplets or pairs, count them as one. 5) count remaining vowels in word. 6) add one if starts with "mc" 7) add one if ends with "y" but is not surrouned by vowel 8) add one if "y" is surrounded by non-vowels and is not in the last word. 9) if starts with "tri-" or "bi-" and is followed by a vowel, add one. 10) if ends with "-ian", should be counted as two syllables, except for "-tian" and "-cian" 11) if starts with "co-" and is followed by a vowel, check if exists in the double syllable dictionary, if not, check if in single dictionary and act accordingly. 12) if starts with "pre-" and is followed by a vowel, check if exists in the double syllable dictionary, if not, check if in single dictionary and act accordingly. 13) check for "-n't" and cross match with dictionary to add syllable. 14) handling the exceptional words TODO : # isn't couldn't doesn't shouldn't ... (done) # when detecting sentences, avoid "a.m. p.m." kind of usage. # exception : "evacuate" "ambulances" "shuttled" "anyone" !/usr/bin/env python -*- coding: utf-8 -*- sylco.py sents_lo = re.findall(r"[a-z].*?[\.!?]", the_text) exception_add are words that need extra syllables exception_del are words that need less syllablesadded syllable numberdiscarded syllable number1) if letters < 3 : return 12) if doesn't end with "ted" or "tes" or "ses" or "ied" or "ies", discard "es" and "ed" at the end. if it has only 1 vowel or 1 set of consecutive vowels, discard. (like "speed", "fled" etc.)3) discard trailing "e", except where ending is "le"4) check if consecutive vowels exists, triplets or pairs, count them as one.5) count remaining vowels in word.6) add one if starts with "mc"7) add one if ends with "y" but is not surrouned by vowel8) add one if "y" is surrounded by non-vowels and is not in the last word.9) if starts with "tri-" or "bi-" and is followed by a vowel, add one.10) if ends with "-ian", should be counted as two syllables, except for "-tian" and "-cian"and (word[-4:] != "cian" or word[-4:] != "tian") :11) if starts with "co-" and is followed by a vowel, check if exists in the double syllable dictionary, if not, check if in single dictionary and act accordingly.12) if starts with "pre-" and is followed by a vowel, check if exists in the double syllable dictionary, if not, check if in single dictionary and act accordingly.13) check for "-n't" and cross match with dictionary to add syllable.14) Handling the exceptional words. calculate the output flesh = (0.39 * (wordsN / sentencesN)) + (11.8 * (syllablesN / wordsN)) - 15.59 http://office.microsoft.com/en-us/word-help/test-your-document-s-readability-HP010148506.aspx
2,962
en
0.867556
"""Remesh a 3D mesh. author : Tom Van Mele, Matthias Rippmann email : van.mele@arch.ethz.ch """ from __future__ import print_function from compas.datastructures import Mesh from compas.datastructures import trimesh_remesh from compas.datastructures import mesh_quads_to_triangles from compas.geometry import centroid_points from compas.geometry import smooth_centroid import compas_rhino from compas_rhino.helpers import mesh_from_guid from compas_rhino.helpers import mesh_identify_vertices from compas_rhino.geometry import RhinoMesh from compas_rhino.geometry import RhinoCurve from compas_rhino.conduits import MeshConduit from compas_rhino.artists import MeshArtist # set the remeshing parameters length = 0.25 kmax = 300 # select the original mesh # select the border # select the fixed points guid_target = compas_rhino.select_mesh() guid_border = compas_rhino.select_polyline() guid_points = compas_rhino.select_points() # wrap the Rhino mesh object for convenience # wrap the Rhino curve object for convenience # get the point coordinates target = RhinoMesh(guid_target) border = RhinoCurve(guid_border) points = compas_rhino.get_point_coordinates(guid_points) # make a mesh datastructure from the Rhino mesh # triangulate the mesh mesh = mesh_from_guid(Mesh, guid_target) mesh_quads_to_triangles(mesh) # identify the fixed vertices # by matching the coordinates of the selected points # up to a precision keys = mesh_identify_vertices(mesh, points, '1f') fixed = set(keys) # create a conduit for visualisation # define a callback # for updating the conduit # and for pulling the mesh back to the original during remeshing # and for keeping the boundary on the boundary curve conduit = MeshConduit(mesh, refreshrate=2) def callback(mesh, k, args): boundary = set(mesh.vertices_on_boundary()) for key, attr in mesh.vertices(data=True): if key in fixed: continue if key in boundary: x, y, z = border.closest_point(mesh.vertex_coordinates(key)) attr['x'] = x attr['y'] = y attr['z'] = z else: x, y, z = target.closest_point(mesh.vertex_coordinates(key)) attr['x'] = x attr['y'] = y attr['z'] = z conduit.redraw(k) # run the remeshing algorithm # draw the result with conduit.enabled(): trimesh_remesh( mesh, target=length, kmax=kmax, tol=0.1, divergence=0.01, allow_boundary_split=True, allow_boundary_swap=True, allow_boundary_collapse=False, smooth=True, fixed=fixed, callback=callback) artist = MeshArtist(mesh, layer='remeshed') artist.draw_faces(join_faces=True)
docs/_examples/mesh-remeshing-on-mesh.py
2,744
Remesh a 3D mesh. author : Tom Van Mele, Matthias Rippmann email : van.mele@arch.ethz.ch set the remeshing parameters select the original mesh select the border select the fixed points wrap the Rhino mesh object for convenience wrap the Rhino curve object for convenience get the point coordinates make a mesh datastructure from the Rhino mesh triangulate the mesh identify the fixed vertices by matching the coordinates of the selected points up to a precision create a conduit for visualisation define a callback for updating the conduit and for pulling the mesh back to the original during remeshing and for keeping the boundary on the boundary curve run the remeshing algorithm draw the result
701
en
0.601603
################## ### original author: Parashar Dhapola ### modified by Rintu Kutum ################## import numpy as np import matplotlib as mpl import matplotlib.pyplot as plt import pandas as pd import re import json import pybedtools as pbt import collections from scipy.stats import ttest_ind, sem, mannwhitneyu, gaussian_kde, zscore, wilcoxon, norm, poisson from scipy import ndimage from scipy.integrate import simps import os import glob import itertools import pysam import tables import sys closest_bed_dist_jsons = glob.glob(pathname='./data/Histones/dist_json_formatted/*.json') bed_closest_data = {} bed_counts = {} for i in closest_bed_dist_jsons: print "\rProcessing\t%s\n" % i, mark = i.split('/')[-1].split('_')[0] cell = i.split('/')[-1].split('_', 1)[-1].split('.')[0] if mark not in bed_counts: bed_counts[mark] = {} bed_counts[mark][cell] = pbt.BedTool('./data/Histones/bed_formatted/%s_%s.bed' % (mark, cell)).count() if mark not in bed_closest_data: bed_closest_data[mark] = {} bed_closest_data[mark][cell] = json.load(open(i)) active_marks = ['H3k4me1', 'H3k4me2', 'H3k4me3', 'H3k9ac', 'H3k27ac', 'H4k20me1'] repress_marks = ['H3k9me1', 'H3k9me3', 'H3k27me3'] other_marks = ['H2az', 'H3k36me3', 'H3k79me2'] window = 10000 binsize = 200 window_frac_sig = 0.1 mpl.style.use('seaborn-whitegrid') def get_smoothend_curve(array, smoothen=True, sigma=3, z_norm=False, log2=False): a = array.copy() if log2 is True: a = np.log2(a) if z_norm is True: a = zscore(a) if smoothen is True: return ndimage.gaussian_filter1d(a, sigma) else: return a def make_stats(t,c,w,b,swp): u = int(w/b-w/b*swp) d = int(w/b+w/b*swp) mu = np.mean([np.mean(i[u:d]) for i in c]) return { 'vals': t*10000/bed_counts[mark][cell], 'shuffle_vals': c[:20]*10000/bed_counts[mark][cell], 'total_histone_marks': bed_counts[mark][cell], 'marks_sig_window': np.sum(t[u:d]), 'marks_full_window': np.sum(t), 'pval': 1-poisson(mu).cdf(np.mean(t[u:d])), } print "\rGenerating\t%s\n" % 'Figure-3A-3B-3C:', stats = {} for mark_set, nc, name in zip([active_marks, repress_marks, other_marks], [2,1,1], ['activation', 'repression', 'others']): nr = 3 fig, ax = plt.subplots(nr, nc, figsize=(1+5*nc, 12)) row = 0 col = 0 for mark in mark_set: print (mark) all_marks = [] all_controls = [] stats[mark] = {} for cell in bed_closest_data[mark]: t = np.array(bed_closest_data[mark][cell]['closest_dist']) c = np.array(bed_closest_data[mark][cell]['shuffle_dist']) stats[mark][cell] = make_stats(t, c, window, binsize, window_frac_sig) x = np.asarray([i for i in range(len(t))]) if nc > 1: axes = ax[row, col] else: axes = ax[row] for cell in stats[mark]: for shuffle in stats[mark][cell]['shuffle_vals']: axes.plot(x, get_smoothend_curve(shuffle, z_norm=False, log2=True, smoothen=True), alpha=0.2, c='lightgrey', linewidth=0.5) for cell in stats[mark]: if stats[mark][cell]['pval'] < 1e-2: color = 'crimson' else: color = 'dimgrey' axes.plot(x, get_smoothend_curve(stats[mark][cell]['vals'], z_norm=False, log2=True, smoothen=True), alpha=0.7, c=color, linewidth=1.3) axes.set_title(mark, fontsize=24) axes.axvline(window/binsize, ls='--') axes.axvspan(window/binsize-window/binsize*window_frac_sig, window/binsize+window/binsize*window_frac_sig, alpha=0.2, color='dodgerblue') axes.set_xticks(list(map(int, np.linspace(0,(2*window)/binsize,9)))) axes.set_xlim((0,(2*window)/binsize)) _ = [tick.label.set_fontsize(20) for tick in axes.yaxis.get_major_ticks()] if col == 0: #axes.set_ylabel('Log2 (histone\nmarks per 10K\nmarks in sample)', fontsize=22) axes.set_ylabel('Log2 (normalized\nhistone peaks)', fontsize=22) if row == nr-1: axes.set_xlabel('Distance from TRF2 peak center', fontsize=22) axes.set_xticklabels(map(int, np.linspace(-window,window,9)), fontsize=20, rotation=45) else: axes.set_xticklabels([]) col+=1 if col == nc: col = 0 row+=1 fig.tight_layout() fig.savefig('./figures/Figure-3_histone_%s.png' % name, dpi=300) for i in stats: n = 0 ns = 0 for j in stats[i]: n+=1 if stats[i][j]['pval'] < 0.01: ns+=1 print (i, n, ns) print "\rGenerating\t%s\n" % 'Figure-4A:', import seaborn as sns count_histone_df = [] for mark in stats: for cell in stats[mark]: count_histone_df.append([mark, cell, stats[mark][cell]['total_histone_marks']]) count_histone_df = pd.DataFrame(count_histone_df, columns=['Mark', 'Cell', 'Value']) fig, ax = plt.subplots(1,1, figsize=(14,5)) sns.set_style("whitegrid") sns.violinplot(x="Mark", y="Value", data=count_histone_df, ax=ax, inner='point', c='Grey', saturation=0, scale="width", order=active_marks+repress_marks+other_marks, scale_hue=True) _ = ax.set_xticklabels(active_marks+repress_marks+other_marks, rotation=70, fontsize=24) ax.set_title('Distribution of nubmer of histone peaks in cell lines for each histone mark', fontsize=26) ax.set_xlabel('') ax.set_ylabel('Number of histone peaks', fontsize=24) _ = [tick.label.set_fontsize(24) for tick in ax.yaxis.get_major_ticks()] sns.despine() fig.tight_layout() fig.savefig('./figures/Suppl-Figure-4A-histone-dist.png', dpi=300) print "\rGenerating\t%s\n" % 'Figure-4B:', # TRF2 +/-10KB count_histone_df = [] for mark in stats: for cell in stats[mark]: count_histone_df.append([mark, cell, stats[mark][cell]['marks_full_window']*10000/stats[mark][cell]['total_histone_marks']]) count_histone_df = pd.DataFrame(count_histone_df, columns=['Mark', 'Cell', 'Value']) fig, ax = plt.subplots(1,1, figsize=(14,5)) sns.set_style("whitegrid") sns.violinplot(x="Mark", y="Value", data=count_histone_df, ax=ax, inner='point', c='Grey', saturation=0, scale="width", order=active_marks+repress_marks+other_marks, scale_hue=True) _ = ax.set_xticklabels(active_marks+repress_marks+other_marks, rotation=70, fontsize=24) ax.set_title('Distribution of histone peaks in +/- 10KB of TRF2 peaks', fontsize=26) ax.set_xlabel('') ax.set_ylabel('Number of normalized\nhistone peaks', fontsize=24) _ = [tick.label.set_fontsize(24) for tick in ax.yaxis.get_major_ticks()] sns.despine() fig.tight_layout() fig.savefig('./figures/Suppl-Figure-4B-histone-peaks-10kb-dist.png', dpi=300) print "\rGenerating\t%s\n" % 'Figure-4C:', # TRF2 +/-500bp count_histone_df = [] for mark in stats: for cell in stats[mark]: count_histone_df.append([mark, cell, stats[mark][cell]['marks_sig_window']*10000/stats[mark][cell]['total_histone_marks']]) count_histone_df = pd.DataFrame(count_histone_df, columns=['Mark', 'Cell', 'Value']) fig, ax = plt.subplots(1,1, figsize=(14,5)) sns.set_style("whitegrid") sns.violinplot(x="Mark", y="Value", data=count_histone_df, ax=ax, inner='point', c='Grey', saturation=0, scale="width", order=active_marks+repress_marks+other_marks, scale_hue=True) _ = ax.set_xticklabels(active_marks+repress_marks+other_marks, rotation=70, fontsize=24) ax.set_title('Distribution of histone peaks in +/- 500bp of TRF2 peaks', fontsize=26) ax.set_xlabel('') ax.set_ylabel('Number of normalized\nhistone peaks', fontsize=24) _ = [tick.label.set_fontsize(24) for tick in ax.yaxis.get_major_ticks()] sns.despine() fig.tight_layout() fig.savefig('./figures/Suppl-Figure-4C-histone-peaks-500bp-dist.png', dpi=300) print "\rGenerating\t%s\n" % 'Figure-4D:', # p-values count_histone_df = [] for mark in stats: for cell in stats[mark]: count_histone_df.append([mark, cell, -np.log10(stats[mark][cell]['pval'])]) count_histone_df = pd.DataFrame(count_histone_df, columns=['Mark', 'Cell', 'Value']) fig, ax = plt.subplots(1,1, figsize=(14,5)) sns.set_style("whitegrid") sns.violinplot(x="Mark", y="Value", data=count_histone_df, ax=ax, inner='point', c='Grey', saturation=0, scale="width", order=active_marks+repress_marks+other_marks, scale_hue=True) _ = ax.set_xticklabels(active_marks+repress_marks+other_marks, rotation=70, fontsize=24) ax.set_title('Distribution of p-values in cell lines', fontsize=26) ax.set_xlabel('') ax.set_ylabel('-log10(p-value)', fontsize=24) _ = [tick.label.set_fontsize(24) for tick in ax.yaxis.get_major_ticks()] sns.despine() fig.tight_layout() fig.savefig('./figures/4D-histone-pval-10kb-dist.png', dpi=300)
3-d-generate-Fig-3A-C-and-Suppl-fig-4A-D.py
8,906
original author: Parashar Dhapola modified by Rintu Kutumaxes.set_ylabel('Log2 (histone\nmarks per 10K\nmarks in sample)', fontsize=22) TRF2 +/-10KB TRF2 +/-500bp p-values
171
en
0.175808
import numpy as np import pandas as pd import os import re import fuelcell as fc class Datum(): def __init__(self, name, data): # data self.name = name self.raw_data = data self.label = name self.processed_data = None self.expt_type = None # processed values self.current_data = None self.potential_data = None self.overpotential_data = None self.logcurrent_data = None self.realcurrent_data = None self.imagcurrent_data = None self.error_data = None # misc parameters self.area = 1 self.refelec = 0 self.thermo_potential = 0 # tafel self.tafel_slope = None self.exchg_curr = None self.tafel_rsq = None # eis self.semicircle_params = None self.linearfit_params = None self.hfr = None self.hfr_linear = None self.lfr = None self.eis_current = None # visualization self.line = None self.errcaps = None self.errbars = None ### accessors ### def get_name(self): return self.name def get_raw_data(self): if self.raw_data is not None: return self.raw_data.copy() return None def get_label(self): return self.label def get_processed_data(self): if self.processed_data is not None: return self.processed_data.copy() return None def get_expt_type(self): return self.expt_type def get_current_data(self): return self.current_data def get_potential_data(self): return self.potential_data def get_overpotential_data(self): return self.overpotential_data def get_logcurrent_data(self): return self.logcurrent_data def get_realcurrent_data(self): return self.realcurrent_data def get_imagcurrent_data(self): return self.imagcurrent_data def get_error_data(self): return self.error_data def get_area(self): return self.area def get_refelec(self): return self.refelec def get_thermo_potential(self): return self.thermo_potential def get_tafel_slope(self): return self.tafel_slope def get_exchg_curr(self): return self.exchg_curr def get_tafel_rsq(self): return self.tafel_rsq def get_semicircle_params(self): popt = self.semicircle_params r, h, k = popt[0], popt[1], popt[2] return r, h, k def get_linearfit_params(self): popt = self.linearfit_params m, b = popt[0], popt[1] return m, b def get_hfr(self): return self.hfr def get_hfr_linear(self): return self.hfr_linear def get_lfr(self): return self.lfr def get_eis_current(self): return self.eis_current def get_line(self): return self.line def get_errcaps(self): return self.errcaps def get_errbars(self): return self.errbars ### modifiers ### def set_label(self, new_label): self.label = new_label def set_processed_data(self, new_data): self.processed_data = new_data def set_expt_type(self, new_type): self.expt_type = new_type.lower() def set_current_data(self, new_vals): self.current_data = np.asarray(new_vals) def set_potential_data(self, new_vals): self.potential_data = np.asarray(new_vals) def set_overpotential_data(self, new_vals): self.overpotential_data = np.asarray(new_vals) def set_logcurrent_data(self, new_vals): self.logcurrent_data = np.asarray(new_vals) def set_realcurrent_data(self, new_vals): self.realcurrent_data = np.asarray(new_vals) def set_imagcurrent_data(self, new_vals): self.imagcurrent_data = np.asarray(new_vals) def set_error_data(self, new_vals): self.error_data = np.asarray(new_vals) def set_area(self, new_val): self.area = new_val def set_refelec(self, new_val): self.refelec = new_val def set_thermo_potential(self, new_val): self.thermo_potential = new_val def set_tafel_slope(self, new_val): self.tafel_slope = new_val def set_exchg_curr(self, new_val): self.exchg_curr = new_val def set_tafel_rsq(self, new_val): self.tafel_rsq = new_val def set_semicircle_params(self, new_params): self.semicircle_params = new_params def set_linearfit_params(self, new_params): self.linearfit_params = new_params def set_hfr(self, new_val): self.hfr = new_val def set_hfr_linear(self, new_val): self.hfr_linear = new_val def set_lfr(self, new_val): self.lfr = new_val def set_eis_current(self, new_val): self.eis_current = new_val def set_line(self, new_val): self.line = new_val def set_errcaps(self, new_val): self.errcaps = new_val def set_errbars(self, new_val): self.errbars = new_val
fuelcell/model.py
4,370
data processed values misc parameters tafel eis visualization accessors modifiers
82
en
0.079244
# coding: utf-8 """ Kubernetes No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator) # noqa: E501 OpenAPI spec version: release-1.16 Generated by: https://openapi-generator.tech """ import pprint import re # noqa: F401 import six class ExtensionsV1beta1Scale(object): """NOTE: This class is auto generated by OpenAPI Generator. Ref: https://openapi-generator.tech Do not edit the class manually. """ """ Attributes: openapi_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. """ openapi_types = { 'api_version': 'str', 'kind': 'str', 'metadata': 'V1ObjectMeta', 'spec': 'ExtensionsV1beta1ScaleSpec', 'status': 'ExtensionsV1beta1ScaleStatus' } attribute_map = { 'api_version': 'apiVersion', 'kind': 'kind', 'metadata': 'metadata', 'spec': 'spec', 'status': 'status' } def __init__(self, api_version=None, kind=None, metadata=None, spec=None, status=None): # noqa: E501 """ExtensionsV1beta1Scale - a model defined in OpenAPI""" # noqa: E501 self._api_version = None self._kind = None self._metadata = None self._spec = None self._status = None self.discriminator = None if api_version is not None: self.api_version = api_version if kind is not None: self.kind = kind if metadata is not None: self.metadata = metadata if spec is not None: self.spec = spec if status is not None: self.status = status @property def api_version(self): """Gets the api_version of this ExtensionsV1beta1Scale. # noqa: E501 APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources # noqa: E501 :return: The api_version of this ExtensionsV1beta1Scale. # noqa: E501 :rtype: str """ return self._api_version @api_version.setter def api_version(self, api_version): """Sets the api_version of this ExtensionsV1beta1Scale. APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources # noqa: E501 :param api_version: The api_version of this ExtensionsV1beta1Scale. # noqa: E501 :type: str """ self._api_version = api_version @property def kind(self): """Gets the kind of this ExtensionsV1beta1Scale. # noqa: E501 Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds # noqa: E501 :return: The kind of this ExtensionsV1beta1Scale. # noqa: E501 :rtype: str """ return self._kind @kind.setter def kind(self, kind): """Sets the kind of this ExtensionsV1beta1Scale. Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds # noqa: E501 :param kind: The kind of this ExtensionsV1beta1Scale. # noqa: E501 :type: str """ self._kind = kind @property def metadata(self): """Gets the metadata of this ExtensionsV1beta1Scale. # noqa: E501 :return: The metadata of this ExtensionsV1beta1Scale. # noqa: E501 :rtype: V1ObjectMeta """ return self._metadata @metadata.setter def metadata(self, metadata): """Sets the metadata of this ExtensionsV1beta1Scale. :param metadata: The metadata of this ExtensionsV1beta1Scale. # noqa: E501 :type: V1ObjectMeta """ self._metadata = metadata @property def spec(self): """Gets the spec of this ExtensionsV1beta1Scale. # noqa: E501 :return: The spec of this ExtensionsV1beta1Scale. # noqa: E501 :rtype: ExtensionsV1beta1ScaleSpec """ return self._spec @spec.setter def spec(self, spec): """Sets the spec of this ExtensionsV1beta1Scale. :param spec: The spec of this ExtensionsV1beta1Scale. # noqa: E501 :type: ExtensionsV1beta1ScaleSpec """ self._spec = spec @property def status(self): """Gets the status of this ExtensionsV1beta1Scale. # noqa: E501 :return: The status of this ExtensionsV1beta1Scale. # noqa: E501 :rtype: ExtensionsV1beta1ScaleStatus """ return self._status @status.setter def status(self, status): """Sets the status of this ExtensionsV1beta1Scale. :param status: The status of this ExtensionsV1beta1Scale. # noqa: E501 :type: ExtensionsV1beta1ScaleStatus """ self._status = status def to_dict(self): """Returns the model properties as a dict""" result = {} for attr, _ in six.iteritems(self.openapi_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 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, ExtensionsV1beta1Scale): return False return self.__dict__ == other.__dict__ def __ne__(self, other): """Returns true if both objects are not equal""" return not self == other
kubernetes/client/models/extensions_v1beta1_scale.py
7,125
NOTE: This class is auto generated by OpenAPI Generator. Ref: https://openapi-generator.tech Do not edit the class manually. Returns true if both objects are equal ExtensionsV1beta1Scale - a model defined in OpenAPI Returns true if both objects are not equal For `print` and `pprint` Gets the api_version of this ExtensionsV1beta1Scale. # noqa: E501 APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources # noqa: E501 :return: The api_version of this ExtensionsV1beta1Scale. # noqa: E501 :rtype: str Sets the api_version of this ExtensionsV1beta1Scale. APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources # noqa: E501 :param api_version: The api_version of this ExtensionsV1beta1Scale. # noqa: E501 :type: str Gets the kind of this ExtensionsV1beta1Scale. # noqa: E501 Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds # noqa: E501 :return: The kind of this ExtensionsV1beta1Scale. # noqa: E501 :rtype: str Sets the kind of this ExtensionsV1beta1Scale. Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds # noqa: E501 :param kind: The kind of this ExtensionsV1beta1Scale. # noqa: E501 :type: str Gets the metadata of this ExtensionsV1beta1Scale. # noqa: E501 :return: The metadata of this ExtensionsV1beta1Scale. # noqa: E501 :rtype: V1ObjectMeta Sets the metadata of this ExtensionsV1beta1Scale. :param metadata: The metadata of this ExtensionsV1beta1Scale. # noqa: E501 :type: V1ObjectMeta Gets the spec of this ExtensionsV1beta1Scale. # noqa: E501 :return: The spec of this ExtensionsV1beta1Scale. # noqa: E501 :rtype: ExtensionsV1beta1ScaleSpec Sets the spec of this ExtensionsV1beta1Scale. :param spec: The spec of this ExtensionsV1beta1Scale. # noqa: E501 :type: ExtensionsV1beta1ScaleSpec Gets the status of this ExtensionsV1beta1Scale. # noqa: E501 :return: The status of this ExtensionsV1beta1Scale. # noqa: E501 :rtype: ExtensionsV1beta1ScaleStatus Sets the status of this ExtensionsV1beta1Scale. :param status: The status of this ExtensionsV1beta1Scale. # noqa: E501 :type: ExtensionsV1beta1ScaleStatus Returns the model properties as a dict Returns the string representation of the model Kubernetes No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator) # noqa: E501 OpenAPI spec version: release-1.16 Generated by: https://openapi-generator.tech coding: utf-8 noqa: F401 noqa: E501 noqa: E501
3,355
en
0.604273
#input variables to Monthly Attribution Paid Campaign Cloning Step 12: Deactivate + Delete Smart Campaigns input={ 'token': 'Token', #from Step 3: Get Token 'parent id': 'fid', #from Step 4: Get Lv2 Folder or Create Lv2 Folder } import re import urllib.parse import calendar import datetime headers = { 'Content-Type': 'application/x-www-form-urlencoded', 'Authorization': 'Bearer' + input['token'] } response ="" sc_ids = input['sc_ids'].split(",") sc_names = input['sc_names'].split(",") for i in range(0,len(sc_ids)): if "Anonymous" not in sc_names[i]: url = 'https://028-jjw-728.mktorest.com/rest/asset/v1/smartCampaign/'+sc_ids[i]+'/deactivate.json' else: url = 'https://028-jjw-728.mktorest.com/rest/asset/v1/smartCampaign/'+sc_ids[i]+'/delete.json' response = response + requests.request("POST", url, headers=headers, data ={}).text return {'Response': response}
monthly_attribution_paid_campaign_cloning/deactivate_+_delete_smart_campaigns.py
989
input variables to Monthly Attribution Paid Campaign Cloning Step 12: Deactivate + Delete Smart Campaignsfrom Step 3: Get Tokenfrom Step 4: Get Lv2 Folder or Create Lv2 Folder
175
en
0.764307
''' Copyright 2020, Amazon Web Services Inc. This code is licensed under MIT license (see LICENSE.txt for details) Python 3 ''' class TransportException(Exception): '''Raised by the transport layer for most issues.''' class BadHTTPMethod(Exception): '''Raised for methods missing from the requests library.''' class BadSink(Exception): '''Raised when the target descriptor for transport is not ESDescriptor or SQSDescriptor.''' class BadAuth(Exception): '''Raised if the transport client gets both SigV4 signing and HTTP Auth'''
es_sink/es_sink/transport_exceptions.py
558
Raised if the transport client gets both SigV4 signing and HTTP Auth Raised for methods missing from the requests library. Raised when the target descriptor for transport is not ESDescriptor or SQSDescriptor. Raised by the transport layer for most issues. Copyright 2020, Amazon Web Services Inc. This code is licensed under MIT license (see LICENSE.txt for details) Python 3
376
en
0.822139
"""Controller para UpdateUser""" from typing import Type, Optional from datetime import datetime from mitmirror.domain.usecases import UpdateUserInterface from mitmirror.domain.models import User from mitmirror.presenters.interfaces import ControllerInterface from mitmirror.presenters.helpers import HttpRequest, HttpResponse from mitmirror.errors import ( HttpBadRequestError, DefaultError, HttpNotFound, HttpUnprocessableEntity, ) class UpdateUserController(ControllerInterface): """Controller para o caso de uso UpdateUser""" def __init__(self, usecase: Type[UpdateUserInterface]) -> None: self.__usecase = usecase def handler( self, param: Optional[any] = None, http_request: Type[HttpRequest] = None ) -> HttpResponse: """Metodo para chamar o caso de uso""" response = None if not param: raise HttpBadRequestError( message="Essa requisiçao exige o seguinte parametro: <int:user_id>, error!" ) if not str(param).isnumeric(): raise HttpUnprocessableEntity( message="O parametro <user_id> deve ser do tipo inteiro, error!" ) try: response = None if not http_request.body: raise DefaultError(type_error=400) name = http_request.body.get("name", None) email = http_request.body.get("email", None) username = http_request.body.get("username", None) password = http_request.body.get("password", None) response = self.__usecase.update( user_id=param, name=name, email=email, username=username, password=password, ) return self.__format_response(response["data"]) except DefaultError as error: if error.type_error == 400: raise HttpBadRequestError( message="Esta requisicao precisa dos seguintes parametros:\ <str:name>, <str:email>, <str:username>, <any:password>, error!" ) from error if error.type_error == 404: raise HttpNotFound(message="Usuario nao encontrado, error!") from error raise error except Exception as error: raise error @classmethod def __format_response(cls, response_method: Type[User]) -> HttpResponse: """Formatando a resposta""" response = { "message": "Informacoes do usuario atualizadas com sucesso!", "data": { "id": response_method.id, "name": response_method.name, "email": response_method.email, "username": response_method.username, "password_hash": "Nao mostramos isso aqui!", "secundary_id": response_method.secundary_id, "is_staff": response_method.is_staff, "is_active_user": response_method.is_active_user, "last_login": datetime.isoformat(response_method.last_login), "date_joined": datetime.isoformat(response_method.date_joined), }, } return HttpResponse(status_code=200, body=response)
mitmirror/presenters/controllers/users/update_user_controller.py
3,299
Controller para o caso de uso UpdateUser Formatando a resposta Metodo para chamar o caso de uso Controller para UpdateUser
122
pt
0.481333
# Licensed under the MIT license # http://opensource.org/licenses/mit-license.php # Copyright 2006, Frank Scholz <coherence@beebits.net> class RenderingControlClient: def __init__(self, service): self.service = service self.namespace = service.get_type() self.url = service.get_control_url() self.service.subscribe() self.service.client = self #print "RenderingControlClient __init__", self.url #def __del__(self): # #print "RenderingControlClient deleted" # pass def remove(self): self.service.remove() self.service = None self.namespace = None self.url = None del self def subscribe_for_variable(self, var_name, callback,signal=False): self.service.subscribe_for_variable(var_name, instance=0, callback=callback,signal=signal) def list_presets(self, instance_id=0): action = self.service.get_action('ListPresets') return action.call(InstanceID=instance_id) def select_presets(self, instance_id=0, preset_name=''): action = self.service.get_action('SelectPresets') return action.call( InstanceID=instance_id, PresetName=preset_name) def get_mute(self, instance_id=0, channel='Master'): action = self.service.get_action('GetMute') return action.call( InstanceID=instance_id, Channel=channel) def set_mute(self, instance_id=0, channel='Master', desired_mute=0): action = self.service.get_action('SetMute') return action.call( InstanceID=instance_id, Channel=channel, DesiredMute=desired_mute) def get_volume(self, instance_id=0, channel='Master'): action = self.service.get_action('GetVolume') return action.call( InstanceID=instance_id, Channel=channel) def set_volume(self, instance_id=0, channel='Master', desired_volume=0): action = self.service.get_action('SetVolume') return action.call( InstanceID=instance_id, Channel=channel, DesiredVolume=desired_volume) def get_volume_db(self, instance_id=0, channel='Master'): action = self.service.get_action('GetVolumeDB') return action.call( InstanceID=instance_id, Channel=channel) def set_volume_db(self, instance_id=0, channel='Master', desired_volume=0): action = self.service.get_action('SetVolumeDB') return action.call( InstanceID=instance_id, Channel=channel, DesiredVolume=desired_volume) def get_volume_db_range(self, instance_id=0, channel='Master'): action = self.service.get_action('GetVolumeDBRange') return action.call( InstanceID=instance_id, Channel=channel) def get_loudness(self, instance_id=0, channel='Master'): action = self.service.get_action('GetLoudness') return action.call( InstanceID=instance_id, Channel=channel) def set_loudness(self, instance_id=0, channel='Master', desired_loudness=0): action = self.service.get_action('SetLoudness') return action.call( InstanceID=instance_id, Channel=channel, DesiredLoudness=desired_loudness)
WebBrickLibs/coherence/upnp/services/clients/rendering_control_client.py
3,464
Licensed under the MIT license http://opensource.org/licenses/mit-license.php Copyright 2006, Frank Scholz <coherence@beebits.net>print "RenderingControlClient __init__", self.urldef __del__(self): print "RenderingControlClient deleted" pass
247
en
0.41218
import os from telethon import TelegramClient, events, sync from dotenv import load_dotenv sleeping_txt = "I'm sleeping right now, get back to you when I wake up" turned_on = False already_messaged_users = set() if __name__ == "__main__": load_dotenv(override=True, verbose=True) api_id = int(os.getenv("API_ID")) api_hash = os.getenv("API_HASH") session_name = os.getenv("SESSION_NAME") with TelegramClient(session_name, api_id, api_hash) as client: client.send_message('me' , 'BRB online!') # Turns bot on and off async def toggle_brb(event, turn_on): global turned_on turned_on = turn_on sender = await event.message.get_sender() await event.reply(f"BRB on: {turned_on}") # /sleep : turns bot on @client.on(events.NewMessage(pattern='(?i)\/sleep$')) async def enable(event): await toggle_brb(event, True) # /up : turns bot off @client.on(events.NewMessage(pattern='(?i)\/up$')) async def disable(event): global already_messaged_users already_messaged_users = set() # clear cache of messaged users await toggle_brb(event, False) @client.on(events.NewMessage(incoming=True)) async def say_brb(event): global turned_on, already_messaged_users sender = await event.get_sender() if not event.is_private or not turned_on or \ sender.id in already_messaged_users: return already_messaged_users.add(sender.id) await event.reply(sleeping_txt) client.run_until_disconnected()
main.py
1,689
Turns bot on and off /sleep : turns bot on /up : turns bot off clear cache of messaged users
92
en
0.448885
from typing import TypeVar from datetime import datetime from gphotos import Utils from gphotos.DbRow import DbRow from gphotos.DatabaseMedia import DatabaseMedia from gphotos.GoogleAlbumMedia import GoogleAlbumMedia import logging log = logging.getLogger(__name__) # this allows self reference to this class in its factory methods G = TypeVar('G', bound='GoogleAlbumsRow') @DbRow.db_row class GoogleAlbumsRow(DbRow): """ generates a class with attributes for each of the columns in the SyncFiles table """ table = "Albums" cols_def = {'RemoteId': str, 'AlbumName': str, 'Size': int, 'StartDate': datetime, 'EndDate': datetime, 'SyncDate': datetime, 'Downloaded': bool} def to_media(self) -> DatabaseMedia: db_media = DatabaseMedia( _id=self.RemoteId, _filename=self.AlbumName, _size=self.Size, _create_date=self.EndDate) return db_media @classmethod def from_media(cls, album: GoogleAlbumMedia) -> G: pass @classmethod def from_parm(cls, album_id, filename, size, start, end) -> G: new_row = cls.make( RemoteId=album_id, AlbumName=filename, Size=size, StartDate=start, EndDate=end, SyncDate=Utils.date_to_string( datetime.now()), Downloaded=0) return new_row
gphotos/GoogleAlbumsRow.py
1,447
generates a class with attributes for each of the columns in the SyncFiles table this allows self reference to this class in its factory methods
146
en
0.807214