How to use generate_filename method in Selene

Best Python code snippet using selene_python

test_generate_filename.py

Source:test_generate_filename.py Github

copy

Full Screen

...20 def get_valid_name(self, name):21 return name22 def get_available_name(self, name, max_length=None):23 return name24 def generate_filename(self, filename):25 """26 This is the method that's important to override when using S3 so that27 os.path() isn't called, which would break S3 keys.28 """29 return self.prefix + self.get_valid_name(filename)30class GenerateFilenameStorageTests(SimpleTestCase):31 def test_storage_dangerous_paths(self):32 candidates = [33 ("/tmp/..", ".."),34 ("/tmp/.", "."),35 ("", ""),36 ]37 s = FileSystemStorage()38 msg = "Could not derive file name from '%s'"39 for file_name, base_name in candidates:40 with self.subTest(file_name=file_name):41 with self.assertRaisesMessage(SuspiciousFileOperation, msg % base_name):42 s.get_available_name(file_name)43 with self.assertRaisesMessage(SuspiciousFileOperation, msg % base_name):44 s.generate_filename(file_name)45 def test_storage_dangerous_paths_dir_name(self):46 candidates = [47 ("tmp/../path", "tmp/.."),48 ("tmp\\..\\path", "tmp/.."),49 ("/tmp/../path", "/tmp/.."),50 ("\\tmp\\..\\path", "/tmp/.."),51 ]52 s = FileSystemStorage()53 for file_name, path in candidates:54 msg = "Detected path traversal attempt in '%s'" % path55 with self.subTest(file_name=file_name):56 with self.assertRaisesMessage(SuspiciousFileOperation, msg):57 s.get_available_name(file_name)58 with self.assertRaisesMessage(SuspiciousFileOperation, msg):59 s.generate_filename(file_name)60 def test_filefield_dangerous_filename(self):61 candidates = [62 ("..", "some/folder/.."),63 (".", "some/folder/."),64 ("", "some/folder/"),65 ("???", "???"),66 ("$.$.$", "$.$.$"),67 ]68 f = FileField(upload_to="some/folder/")69 for file_name, msg_file_name in candidates:70 msg = f"Could not derive file name from '{msg_file_name}'"71 with self.subTest(file_name=file_name):72 with self.assertRaisesMessage(SuspiciousFileOperation, msg):73 f.generate_filename(None, file_name)74 def test_filefield_dangerous_filename_dot_segments(self):75 f = FileField(upload_to="some/folder/")76 msg = "Detected path traversal attempt in 'some/folder/../path'"77 with self.assertRaisesMessage(SuspiciousFileOperation, msg):78 f.generate_filename(None, "../path")79 def test_filefield_generate_filename_absolute_path(self):80 f = FileField(upload_to="some/folder/")81 candidates = [82 "/tmp/path",83 "/tmp/../path",84 ]85 for file_name in candidates:86 msg = f"Detected path traversal attempt in '{file_name}'"87 with self.subTest(file_name=file_name):88 with self.assertRaisesMessage(SuspiciousFileOperation, msg):89 f.generate_filename(None, file_name)90 def test_filefield_generate_filename(self):91 f = FileField(upload_to="some/folder/")92 self.assertEqual(93 f.generate_filename(None, "test with space.txt"),94 os.path.normpath("some/folder/test_with_space.txt"),95 )96 def test_filefield_generate_filename_with_upload_to(self):97 def upload_to(instance, filename):98 return "some/folder/" + filename99 f = FileField(upload_to=upload_to)100 self.assertEqual(101 f.generate_filename(None, "test with space.txt"),102 os.path.normpath("some/folder/test_with_space.txt"),103 )104 def test_filefield_generate_filename_upload_to_overrides_dangerous_filename(self):105 def upload_to(instance, filename):106 return "test.txt"107 f = FileField(upload_to=upload_to)108 candidates = [109 "/tmp/.",110 "/tmp/..",111 "/tmp/../path",112 "/tmp/path",113 "some/folder/",114 "some/folder/.",115 "some/folder/..",116 "some/folder/???",117 "some/folder/$.$.$",118 "some/../test.txt",119 "",120 ]121 for file_name in candidates:122 with self.subTest(file_name=file_name):123 self.assertEqual(f.generate_filename(None, file_name), "test.txt")124 def test_filefield_generate_filename_upload_to_absolute_path(self):125 def upload_to(instance, filename):126 return "/tmp/" + filename127 f = FileField(upload_to=upload_to)128 candidates = [129 "path",130 "../path",131 "???",132 "$.$.$",133 ]134 for file_name in candidates:135 msg = f"Detected path traversal attempt in '/tmp/{file_name}'"136 with self.subTest(file_name=file_name):137 with self.assertRaisesMessage(SuspiciousFileOperation, msg):138 f.generate_filename(None, file_name)139 def test_filefield_generate_filename_upload_to_dangerous_filename(self):140 def upload_to(instance, filename):141 return "/tmp/" + filename142 f = FileField(upload_to=upload_to)143 candidates = ["..", ".", ""]144 for file_name in candidates:145 msg = f"Could not derive file name from '/tmp/{file_name}'"146 with self.subTest(file_name=file_name):147 with self.assertRaisesMessage(SuspiciousFileOperation, msg):148 f.generate_filename(None, file_name)149 def test_filefield_awss3_storage(self):150 """151 Simulate a FileField with an S3 storage which uses keys rather than152 folders and names. FileField and Storage shouldn't have any os.path()153 calls that break the key.154 """155 storage = AWSS3Storage()156 folder = "not/a/folder/"157 f = FileField(upload_to=folder, storage=storage)158 key = "my-file-key\\with odd characters"159 data = ContentFile("test")160 expected_key = AWSS3Storage.prefix + folder + key161 # Simulate call to f.save()162 result_key = f.generate_filename(None, key)163 self.assertEqual(result_key, expected_key)164 result_key = storage.save(result_key, data)165 self.assertEqual(result_key, expected_key)166 # Repeat test with a callable.167 def upload_to(instance, filename):168 # Return a non-normalized path on purpose.169 return folder + filename170 f = FileField(upload_to=upload_to, storage=storage)171 # Simulate call to f.save()172 result_key = f.generate_filename(None, key)173 self.assertEqual(result_key, expected_key)174 result_key = storage.save(result_key, data)...

Full Screen

Full Screen

Upload_files.py

Source:Upload_files.py Github

copy

Full Screen

...16 keys = [k['link'] for k in data]17 if obj['link'] in keys:18 return True19 return False20def generate_filename(date, product):21 return product + '_' + get_year(date) + '_' + get_month(date) + '_' + get_day(date) + '.json'22 23def generate_temporal_json(lista_jsons, filename_boto3):24 with open(filename_boto3, "w") as fout:25 json.dump(lista_jsons, fout)26 27def generate_path(date, product):28 return get_year(date) + '/' + get_month(date) + '/' + get_day(date) + '/' + product + '/' + generate_filename(date, product)29class Upload_Files():30 31 def __init__(self, directori = "items"):32 self.list_of_files = os.listdir(directori)33 self.s3_connection = boto3.resource('s3')34 self.bucket = self.s3_connection.Bucket('tracktrend.extracciones')35 self.list_files = [obj.key for obj in self.bucket.objects.all()]36 self.directori = directori37 self.load_all_files()38 39 def load_all_files(self):40 for f in self.list_of_files:41 self.load_single_file(f)42 43 def load_single_file(self, f):44 file = open(self.directori + '/' + f)45 self.classify_single_file(json.load(file), get_product(f))46 47 def classify_single_file(self,list_json, product):48 for obj in list_json:49 date = get_date(obj)50 if date != 'None':51 filename_boto3 = generate_path(date, product)52 if filename_boto3 not in self.list_files:53 self.upload_obj_noexisfile(filename_boto3, obj, product, date)54 else:55 self.upload_obj_exisfile(filename_boto3, obj, product, date)56 57 def upload_obj_noexisfile(self, filename_boto3, obj, product, date):58 generate_temporal_json([obj], generate_filename(date, product))59 self.s3_connection.meta.client.upload_file(generate_filename(date, product), 'tracktrend.extracciones', filename_boto3)60 remove(generate_filename(date, product))61 62 def upload_obj_exisfile(self, filename_boto3, obj, product, date):63 exist_file = self.s3_connection.Object('bucketpruebarichi', filename_boto3)64 data = json.loads(exist_file.get()['Body'].read().decode('utf-8'))65 if not_exists(obj,data):66 data.append(obj)67 generate_temporal_json(data, generate_filename(date, product))68 self.s3_connection.meta.client.upload_file(generate_filename(date, product), 'tracktrend.extracciones', filename_boto3)...

Full Screen

Full Screen

increament_SmartAlarm_SN.py

Source:increament_SmartAlarm_SN.py Github

copy

Full Screen

1#!/usr/bin/env python2import sys3from deal_with_xlsx import Write_excel 4import xlrd5import shutil6import random7from test import SN, no_SN8def generate_xlsx(filename, generate_filename):9 shutil.copy(filename, generate_filename)10 workbook = xlrd.open_workbook(generate_filename)11 sheet = workbook.sheet_names()[0]12 booksheet = workbook.sheet_by_name(sheet)13 14 wr = Write_excel(generate_filename)15 wr.write(1,17,"SN")16# new_workbook = xlsxwriter.Workbook(generate_filename)17# table_name = sheet18# read_worksheet = new_workbook.get_sheet(table_name)19# read_worksheet.write_column("Q1": "SN")20 for row in range(1,booksheet.nrows): #booksheet.nrows列出行数的循环 21# for col_ in range(booksheet.ncols): #booksheet.ncols列出列数的循环22 if int(booksheet.cell(row, 7).value) == 5 or int(booksheet.cell(row, 7).value) == 4: #根据行数和列数进行对应位置坐标找数据23 wr.write(row+1,17,random.sample(SN,1)[0])24 else:25 wr.write(row+1,17,random.sample(no_SN,1)[0])26 wr.close()27 28if __name__=='__main__':29 if len(sys.argv) != 3:30 raise ValueError("""usage: python %s <filename> <generate_filename>""" % sys.argv[0])31 filename = sys.argv[1]32 generate_filename = sys.argv[2]...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Selene automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful