How to use _normalize_name method in localstack

Best Python code snippet using localstack_python

gcloud.py

Source:gcloud.py Github

copy

Full Screen

...108 raise ImproperlyConfigured("Bucket %s does not exist. Buckets "109 "can be automatically created by "110 "setting GS_AUTO_CREATE_BUCKET to "111 "``True``." % name)112 def _normalize_name(self, name):113 """114 Normalizes the name so that paths like /path/to/ignored/../something.txt115 and ./file.txt work. Note that clean_name adds ./ to some paths so116 they need to be fixed here.117 """118 return safe_join('', name)119 def _encode_name(self, name):120 return smart_str(name, encoding=self.file_name_charset)121 def _open(self, name, mode='rb'):122 name = self._normalize_name(clean_name(name))123 file_object = GoogleCloudFile(name, mode, self)124 if not file_object.blob:125 raise IOError(u'File does not exist: %s' % name)126 return file_object127 def _save(self, name, content):128 cleaned_name = clean_name(name)129 name = self._normalize_name(cleaned_name)130 content.name = cleaned_name131 encoded_name = self._encode_name(name)132 file = GoogleCloudFile(encoded_name, 'rw', self)133 file.blob.upload_from_file(content, size=content.size,134 content_type=file.mime_type)135 return cleaned_name136 def delete(self, name):137 name = self._normalize_name(clean_name(name))138 self.bucket.delete_blob(self._encode_name(name))139 def exists(self, name):140 if not name: # root element aka the bucket141 try:142 self.bucket143 return True144 except ImproperlyConfigured:145 return False146 name = self._normalize_name(clean_name(name))147 return bool(self.bucket.get_blob(self._encode_name(name)))148 def listdir(self, name):149 name = self._normalize_name(clean_name(name))150 # for the bucket.list and logic below name needs to end in /151 # But for the root path "" we leave it as an empty string152 if name and not name.endswith('/'):153 name += '/'154 files_list = list(self.bucket.list_blobs(prefix=self._encode_name(name)))155 files = []156 dirs = set()157 base_parts = name.split("/")[:-1]158 for item in files_list:159 parts = item.name.split("/")160 parts = parts[len(base_parts):]161 if len(parts) == 1 and parts[0]:162 # File163 files.append(parts[0])164 elif len(parts) > 1 and parts[0]:165 # Directory166 dirs.add(parts[0])167 return list(dirs), files168 def _get_blob(self, name):169 # Wrap google.cloud.storage's blob to raise if the file doesn't exist170 blob = self.bucket.get_blob(name)171 if blob is None:172 raise NotFound(u'File does not exist: {}'.format(name))173 return blob174 def size(self, name):175 name = self._normalize_name(clean_name(name))176 blob = self._get_blob(self._encode_name(name))177 return blob.size178 def modified_time(self, name):179 name = self._normalize_name(clean_name(name))180 blob = self._get_blob(self._encode_name(name))181 return timezone.make_naive(blob.updated)182 def get_modified_time(self, name):183 name = self._normalize_name(clean_name(name))184 blob = self._get_blob(self._encode_name(name))185 updated = blob.updated186 return updated if setting('USE_TZ') else timezone.make_naive(updated)187 def url(self, name):188 # Preserve the trailing slash after normalizing the path.189 name = self._normalize_name(clean_name(name))190 blob = self._get_blob(self._encode_name(name))191 return blob.public_url192 def get_available_name(self, name, max_length=None):193 if self.file_overwrite:194 name = clean_name(name)195 return name...

Full Screen

Full Screen

storage.py

Source:storage.py Github

copy

Full Screen

...57 if not name: # Empty name is a directory58 return True59 if self.isfile(name):60 return False61 name = self._normalize_name(self._clean_name(name))62 dirlist = self.bucket.list(self._encode_name(name))63 # Check whether the iterator is empty64 for item in dirlist:65 return True66 return False67 def move(self, old_file_name, new_file_name, allow_overwrite=False):68 if self.exists(new_file_name):69 if allow_overwrite:70 self.delete(new_file_name)71 else:72 raise "The destination file '%s' exists and allow_overwrite is False" % new_file_name73 old_key_name = self._encode_name(self._normalize_name(self._clean_name(old_file_name)))74 new_key_name = self._encode_name(self._normalize_name(self._clean_name(new_file_name)))75 k = self.bucket.copy_key(new_key_name, self.bucket.name, old_key_name)76 if not k:77 raise "Couldn't copy '%s' to '%s'" % (old_file_name, new_file_name)78 self.delete(old_file_name)79 def makedirs(self, name):80 self.save(name + "/.folder", ContentFile(""))81 def rmtree(self, name):82 name = self._normalize_name(self._clean_name(name))83 dirlist = self.bucket.list(self._encode_name(name))84 for item in dirlist:85 item.delete()86class GoogleStorageMixin(StorageMixin):87 def isfile(self, name):88 return self.exists(name)89 def isdir(self, name):90 # That's some inefficient implementation...91 # If there are some files having 'name' as their prefix, then92 # the name is considered to be a directory93 if not name: # Empty name is a directory94 return True95 if self.isfile(name):96 return False97 name = self._normalize_name(self._clean_name(name))98 dirlist = self.bucket.list(self._encode_name(name))99 # Check whether the iterator is empty100 for item in dirlist:101 return True102 return False103 def move(self, old_file_name, new_file_name, allow_overwrite=False):104 if self.exists(new_file_name):105 if allow_overwrite:106 self.delete(new_file_name)107 else:108 raise "The destination file '%s' exists and allow_overwrite is False" % new_file_name109 old_key_name = self._encode_name(self._normalize_name(self._clean_name(old_file_name)))110 new_key_name = self._encode_name(self._normalize_name(self._clean_name(new_file_name)))111 k = self.bucket.copy_key(new_key_name, self.bucket.name, old_key_name)112 if not k:113 raise "Couldn't copy '%s' to '%s'" % (old_file_name, new_file_name)114 self.delete(old_file_name)115 def makedirs(self, name):116 self.save(name + "/.folder", ContentFile(""))117 def rmtree(self, name):118 name = self._normalize_name(self._clean_name(name))119 dirlist = self.bucket.list(self._encode_name(name))120 for item in dirlist:...

Full Screen

Full Screen

model.py

Source:model.py Github

copy

Full Screen

...22import re23from google.appengine.ext import db24from google.appengine.ext.db import polymodel25_SEARCH_NAME_REGEX = re.compile('\w+', re.UNICODE)26def _normalize_name(name):27 """Helper used to convert a user entered name in to search compatible string.28 In order to make search as error free as possible, names of info records29 are converted to a simplified utf-8 encoded string that makes prefix searches30 easy. to make searching simpler, it removes all extra punctuation and spaces.31 Examples:32 _normalize_name('Duke Ellington') == 'duke ellington'33 _normalize_name(' Duke Ellington ') == 'duke ellington'34 _normalize_name('Duke-Ellington!') == 'duke ellington'35 _normalize_name('Duke_Ellington') == 'duke ellington'36 _normalize_name(u'Duk\xea Ellington') == 'Duk\xc3\xaa Ellington'37 Args:38 name: Name to convert to search string.39 Returns:40 Lower case, single space separated ByteString of name with punctuation41 removed. Unicode values are converted to UTF-8 encoded string.42 """43 if name is None:44 return None45 elif isinstance(name, str):46 name = name.decode('utf-8')47 # Must explicitly replace '_' because the \w re part does not48 name = name.replace(u'_', u' ')49 names = _SEARCH_NAME_REGEX.findall(name)50 name = ' '.join(names)51 return db.ByteString(name.lower().encode('utf-8'))52class Info(polymodel.PolyModel):53 """Base class for all Info records in Tunes DB.54 Properties:55 name: User friendly name for record.56 encoded_name: Derived from name to allow easy prefix searching. Name is57 transformed using _normalize_name.58 """59 name = db.StringProperty()60 @db.ComputedProperty61 def encoded_name(self):62 return _normalize_name(self.name)63 @classmethod64 def search(cls, name_prefix=None):65 """Create search query based on info record name prefix.66 Args:67 name_prefix: User input name-prefix to search for. If name_prefix68 is empty string or None returns all records of Info sub-class. Records69 are sorted by their encoded name.70 Returns:71 Datastore query pointing to search results.72 """73 name_prefix = _normalize_name(name_prefix)74 query = cls.all().order('encoded_name')75 if name_prefix:76 query.filter('encoded_name >=', db.ByteString(name_prefix))77 # Do not need to worry about name_prefix + '\xff\xff' because not78 # a unicode character.79 query.filter('encoded_name <=', db.ByteString(name_prefix + '\xff'))80 return query81class ArtistInfo(Info):82 """Musician or music group responsible for recording.83 Properties:84 album_count: Number of albums produced by artist.85 albums: Implicit collection of albums produced by artist.86 """87 album_count = db.IntegerProperty(default=0)...

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 localstack 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