Best Python code snippet using autotest_python
views.py
Source:views.py
...79 if info.get('type', None) not in ('single', 'multiple'):80 logger.warning('typeæ°æ®é误!')81 return 90, 'typeæ°æ®é误!', None82 if info.get('type', None) == 'single':83 return single_sync(info)84 users = info.get('users', [])85 if len(users) == 0 or not isinstance(users, list):86 logger.warning('multiple模å¼ä¸ï¼usersæ°æ®ä¸ºç©ºææ æ!')87 return 91, 'multiple模å¼ä¸ï¼usersæ°æ®ä¸ºç©ºææ æ!', None88 [single_sync(u) for u in users]89 return 200, 'success', None90def single_sync(info):91 """92 åæ¡æ°æ®åæ¥,åè®®åè93 {94 "is_superuser": false, # å¯é95 "is_staff": false, # å¯é96 "is_active": true, # å¯é97 "username": "admin", # å¿
é¡»98 "permissions": [1,3] # å¿
é¡»99 }100 :param info:101 :return:102 """103 if 'permissions' not in info or 'username' not in info:104 return 100, 'åæ°é误ï¼', None...
51_moodle_create-cohorte.py
Source:51_moodle_create-cohorte.py
1import MySQLdb 2import configparser3import time4import os5import subprocess6class Moodle_updater:7 def __init__(self,state,args):8 list_functions = {9 'add_to_group':self.join_group,10 'remove_from_group':self.drop_group,11 'add_group':self.add_group,12 'gescen_full':self.gesita,13 'gescen_partial':self.gesita,14 'add_generic_users': self.add_generic_group15 }16 self.user = False17 self.password = False18 try:19 self.function_to_execute = list_functions[state]20 except:21 self.function_to_execute = None22 self.args = args23 #def __init__24 25 def connect(self):26 try:27 self.connection=MySQLdb.connect(host="localhost",user=self.user,passwd=self.password,db='moodle')28 return True29 except:30 print("[!] Error connecting to MySQL [!]")31 return False32 #def connect33 def readPassword(self): 34 try:35 config = configparser.ConfigParser()36 config.read('/root/.my.cnf')37 self.user = config.get("mysql","user")38 self.password= config.get("mysql","password")39 except:40 self.user = False41 self.password = False42 #def readPassword43 44 def gesita(self):45 tmp_file = open("/tmp/gesita_import",'r')46 lines_file = tmp_file.readlines()47 for aux_process in lines_file:48 action_list = aux_process.split(';')49 if action_list[0] == 'add':50 self._add_group(action_list[1].rstrip())51 elif action_list[0] == 'join':52 self._join_group(action_list[1],action_list[2].rstrip())53 elif action_list[0] == 'drop':54 self._drop_group(action_list[1],action_list[2].rstrip())55 os.remove('/tmp/gesita_import')56 #def gesita57 58 def add_generic_group(self):59 try:60 self._join_group(self.args['group'],self.args['user']['uid'])61 print("Todo va bien !!!!!!!!!!!!!!!!!")62 except Exception as e:63 print("********************explotando******************")64 print(e)65 def join_group(self):66 self._join_group(self.args['group']['cn'],self.args['user']['uid'])67 #def join_group68 69 def _join_group(self,group_name,user_name):70 self.readPassword()71 if self.connect():72 sql = "Select id from mdl_cohort where name='"+group_name+"'"73 cursor = self.connection.cursor()74 cursor.execute(sql)75 output = cursor.fetchone()76 if output != None and len(output) > 0:77 id_group = output[0]78 sql = "Select id from mdl_user where username='"+user_name+"'"79 cursor.execute(sql)80 output = cursor.fetchone()81 if output != None and len(output) > 0:82 aux_time = time.time()83 id_user = output[0]84 try:85 sql = "INSERT into mdl_cohort_members (cohortid,userid,timeadded) VALUES ("+str(id_group)+","+str(id_user)+","+str(aux_time)+")"86 cursor.execute(sql)87 self.connection.commit()88 except Exception as e:89 print(e)90 cursor.close()91 #def join_group92 93 def drop_group(self):94 self._drop_group(self.args['group']['cn'],self.args['user']['uid'])95 #def drop_group96 97 def _drop_group(self,group_name,user_name):98 self.readPassword()99 if self.connect():100 sql = "Select id from mdl_cohort where name='"+group_name+"'"101 cursor = self.connection.cursor()102 cursor.execute(sql)103 output = cursor.fetchone()104 if output != None and len(output) > 0:105 id_group = output[0]106 sql = "Select id from mdl_user where username='"+user_name+"'"107 cursor.execute(sql)108 output = cursor.fetchone()109 if output != None and len(output) > 0:110 aux_time = time.time()111 id_user = output[0]112 sql = "DELETE FROM mdl_cohort_members WHERE cohortid="+str(id_group)+" AND userid="+str(id_user)113 cursor.execute(sql)114 self.connection.commit()115 cursor.close() 116 #def _drop_group117 118 def add_group(self):119 self._add_group(self.args['group']['cn'])120 #def add_group121 122 def _add_group(self,group_name):123 self.readPassword()124 if self.connect():125 aux_time = time.time()126 cursor = self.connection.cursor()127 sql = "select count(*) from mdl_cohort where name = '"+ group_name +"';"128 cursor.execute(sql)129 output = cursor.fetchone()130 if output == None or (len(output) > 0 and output[0] < 1):131 sql = "INSERT into mdl_cohort(contextid,name,idnumber,description,descriptionformat,component,timecreated,timemodified) VALUES (1,'"+group_name+"','','',1,'',"+str(aux_time)+","+str(aux_time)+")"132 cursor.execute(sql)133 self.connection.commit()134 cursor.close()135 #def _add_group136 137 def execute(self):138 print(self.function_to_execute)139 if self.function_to_execute != None:140 self.function_to_execute()141 #def execute142single_sync=["add_user","add_generic_users","gescen_partial","gescen_full"]143if NEVERLAND_VAR in single_sync:144 subprocess.Popen(["/usr/bin/php","/usr/share/moodle/auth/ldap/cli/sync_users.php"]).communicate()145aux = Moodle_updater(NEVERLAND_VAR,ARGV)...
main.py
Source:main.py
...20 if not args.domain:21 print("Please pass a domain as -d. See --help for more information")22 return23 domain = args.domain24 sync.single_sync(domain)25 elif args.command == "full_sync":26 sync.full_sync()27 else:28 print("Invalid command, choose full_sync or single_sync.")29if __name__ == "__main__":...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!