def sandbox_file_line_match(self, match, line): """ Map content of file to self.dic['__file'][name]. Raise from loader.exceptions: - SyntaxErrorPL if no group 'file' was found - DirectoryNotFound if trying to load from a nonexistent directory - FileNotFound if the given file do not exists.""" if not match.group('file'): raise SyntaxErrorPL(self.path_parsed_file, line, self.lineno) try: directory, path = get_location(self.directory, match.group('file'), current=self.path_parsed_file) path = join(directory.root, path) name = basename(path) if not match.group('alias') else match.group( 'alias') with open(path, 'r') as f: self.dic['__file'][name] = f.read() except ObjectDoesNotExist: raise DirectoryNotFound(self.path_parsed_file, line, match.group('file'), self.lineno) except FileNotFoundError: raise FileNotFound(self.path_parsed_file, line, path, lineno=self.lineno) except ValueError: raise FileNotFound( self.path_parsed_file, line, match.group('file'), lineno=self.lineno, message="Path from another directory must be absolute")
def from_file_line_match(self, match, line): """ Map (or append) the content if the file corresponding to file) to the key Raise from loader.exceptions: - SyntaxErrorPL if no group 'file' or 'key' was found - SemanticError if trying to append a nonexistent key - DirectoryNotFound if trying to load from a nonexistent directory - FileNotFound if the given file do not exists.""" if not match.group('file') or not match.group( 'key') or not match.group('operator'): raise SyntaxErrorPL(self.path_parsed_file, line, self.lineno) key = match.group('key') op = match.group('operator') # Add warning when overwritting a key if key in self.dic and '+' not in op: self.add_warning("Key '" + key + "' overwritten at line " + str(self.lineno)) try: directory, path = get_location(self.directory, match.group('file'), current=self.path_parsed_file) path = abspath( join(directory.root, path.replace(directory.name + '/', ''))) with open(path, 'r') as f: if '+' in op: if not key in self.dic: raise SemanticError( self.path_parsed_file, line, self.lineno, "Trying to append to non-existent key '" + key + "'.") self.dic[key] += f.read() else: self.dic[key] = f.read() except ObjectDoesNotExist: raise DirectoryNotFound(self.path_parsed_file, line, match.group('file'), self.lineno) except FileNotFoundError: raise FileNotFound(self.path_parsed_file, line, path, lineno=self.lineno) except ValueError: raise FileNotFound( self.path_parsed_file, line, match.group('file'), lineno=self.lineno, message="Path from another directory must be absolute")
def sandbox_file_line_match(self, match, line: str): """ Map content of file to self.dic['__files'][name]. Raise from loader.exceptions: - SyntaxErrorPL if no group 'file' was found - DirectoryNotFound if trying to load from a nonexistent directory - FileNotFound if the given file do not exists.""" try: directory, path = get_location(self.directory, match.group('file'), current=dirname(self.path), parser=self) name = basename(path) if not match.group('alias') else match.group( 'alias') self.dic['__dependencies'].append(path) with open(join(settings.FILEBROWSER_ROOT, directory, path)) as f: self.dic['__files'][name] = f.read() except FileNotFoundError as e: raise FileNotFound(self.path_parsed_file, line, match.group('file'), self.lineno, str(e)) except SyntaxError as e: raise SyntaxErrorPL(self.path_parsed_file, line, self.lineno, str(e))
def from_file_line_match(self, match, line: str): """ Map (or append) the content if the file corresponding to file) to the key Raise from loader.exceptions: - SyntaxErrorPL if no group 'file' or 'key' was found - SemanticError if trying to append a nonexistent key - DirectoryNotFound if trying to load from a nonexistent directory - FileNotFound if the given file do not exists.""" key = match.group('key') op = match.group('operator') try: directory, path = get_location(self.directory, match.group('file'), current=dirname(self.path), parser=self) with open(join(settings.FILEBROWSER_ROOT, directory, path)) as f: if '+' in op: self.dic_add_key(key, f.read(), append=True) elif '-' in op: self.dic_add_key(key, f.read(), prepend=True) else: self.dic_add_key(key, f.read()) except FileNotFoundError as e: raise FileNotFound(self.path_parsed_file, line, match.group('file'), self.lineno, str(e)) except SyntaxError as e: raise SyntaxErrorPL(self.path_parsed_file, line, self.lineno, str(e))
def extends_line_match(self, match, line: str): """ Appends file, line and lineno to self.dic['__extends'] so that it can be later processed by loader.parser. Raise from loader.exceptions - SyntaxErrorPL if no group 'file' was found. - DirectoryNotFound if the directory indicated by the pl couldn't be found""" try: directory, path = get_location(self.directory, match.group('file'), current=dirname(self.path), parser=self) except SyntaxError as e: raise SyntaxErrorPL(self.path_parsed_file, line, self.lineno, str(e)) except FileNotFoundError as e: raise FileNotFound(self.path_parsed_file, line, match.group('file'), self.lineno, str(e)) self.dic['__extends'].append({ 'path': path, 'line': line, 'lineno': self.lineno, 'directory_name': directory })
def process_extends(dic): """ Extends dic with file in dic['__extends']. Return newly extended dic. Raise from loader.exceptions: - DirectoryNotFound if trying to load from a nonexistent directory - FileNotFound if the file do not exists.""" warnings = [] for item in dic['__extends']: try: directory = Directory.objects.get(name=item['directory_name']) ext_dic, warnings_ext = parse_file(directory, item['path'], extending=True) warnings += warnings_ext dic = extends_dict(dic, ext_dic) except ObjectDoesNotExist: raise DirectoryNotFound(dic['__rel_path'], item['line'], item['path'], item['lineno']) except UnknownExtension as e: raise UnknownExtension( e.path, e.name, message=("extending from " + dic['__rel_path'] + " -- unknow extension ")) except FileNotFoundError: raise FileNotFound(dic['__rel_path'], item['line'], join(item['directory_name'], item['path']), lineno=item['lineno']) return dic, warnings
def test_file_not_found_str(self): path = 'first1/second1' line = '3' lineno = '45' path_not_found = 'dir1' message = "File not found" str_message = (path + " " + "at line " + lineno + " -- " + message + " : '" + path_not_found + "'\n" + line) self.assertEqual( str(FileNotFound(path, line, path_not_found, lineno, message)), str_message)
def url_line_match(self, match, line): """ Map value to a download url of a resource. Raise from loader.exceptions: - SyntaxErrorPL if no group 'key' or 'file' was found - DirectoryNotFound if trying to load from a nonexistent directory - FileNotFound if the given file do not exists.""" key = match.group('key') try: directory, path = get_location(self.directory, match.group('file'), current=dirname(self.path), parser=self) url = to_download_url(os.path.join(directory, path)) self.dic_add_key(key, url) except FileNotFoundError as e: raise FileNotFound(self.path_parsed_file, line, match.group('file'), self.lineno, str(e)) except SyntaxError as e: raise SyntaxErrorPL(self.path_parsed_file, line, self.lineno, str(e))
def pl_file_line_match(self, match, line): """ Appends file, line and lineno to self.dic['__pl'] so that it can be later processed by loader.loader. Raise loader.exceptions.SyntaxErrorPL if no group 'file' was found.""" try: directory, path = get_location(self.directory, match.group('file'), current=dirname(self.path), parser=self) except SyntaxError as e: raise SyntaxErrorPL(self.path_parsed_file, line, self.lineno, str(e)) except FileNotFoundError as e: raise FileNotFound(self.path_parsed_file, line, match.group('file'), self.lineno, str(e)) self.dic['__pl'].append({ 'path': path, 'line': line, 'lineno': self.lineno, 'directory_name': directory })