mirror of
				https://github.com/avatao-content/baseimage-tutorial-framework
				synced 2025-11-04 07:22:55 +00:00 
			
		
		
		
	Implement finding files from relative paths FileManager
This commit is contained in:
		@@ -1,7 +1,10 @@
 | 
				
			|||||||
 | 
					import logging
 | 
				
			||||||
from functools import wraps
 | 
					from functools import wraps
 | 
				
			||||||
from glob import glob
 | 
					from glob import glob
 | 
				
			||||||
from fnmatch import fnmatchcase
 | 
					from fnmatch import fnmatchcase
 | 
				
			||||||
from os.path import dirname, isdir, isfile, realpath
 | 
					from os.path import dirname, isdir, isfile, realpath, isabs
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					LOG = logging.getLogger(__name__)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def _with_is_allowed(func):
 | 
					def _with_is_allowed(func):
 | 
				
			||||||
@@ -45,6 +48,13 @@ class FileManager: # pylint: disable=too-many-instance-attributes
 | 
				
			|||||||
            for pattern in self.patterns
 | 
					            for pattern in self.patterns
 | 
				
			||||||
        )
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def find_file(self, filename):
 | 
				
			||||||
 | 
					        if not isabs(filename):
 | 
				
			||||||
 | 
					            for filepath in self.files:
 | 
				
			||||||
 | 
					                if filepath.endswith(filename):
 | 
				
			||||||
 | 
					                    return filepath
 | 
				
			||||||
 | 
					        return filename
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @_with_is_allowed
 | 
					    @_with_is_allowed
 | 
				
			||||||
    def read_file(self, filepath): # pylint: disable=no-self-use
 | 
					    def read_file(self, filepath): # pylint: disable=no-self-use
 | 
				
			||||||
        with open(filepath, 'rb', buffering=0) as ifile:
 | 
					        with open(filepath, 'rb', buffering=0) as ifile:
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,7 +2,7 @@
 | 
				
			|||||||
from dataclasses import dataclass
 | 
					from dataclasses import dataclass
 | 
				
			||||||
from secrets import token_urlsafe
 | 
					from secrets import token_urlsafe
 | 
				
			||||||
from os import mkdir, symlink
 | 
					from os import mkdir, symlink
 | 
				
			||||||
from os.path import join, realpath
 | 
					from os.path import join, realpath, basename
 | 
				
			||||||
from pathlib import Path
 | 
					from pathlib import Path
 | 
				
			||||||
from tempfile import TemporaryDirectory
 | 
					from tempfile import TemporaryDirectory
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -86,3 +86,9 @@ def test_regular_ide_actions(context):
 | 
				
			|||||||
        context.manager.write_file(newfile2, content2)
 | 
					        context.manager.write_file(newfile2, content2)
 | 
				
			||||||
        assert context.manager.read_file(newfile1) == content1
 | 
					        assert context.manager.read_file(newfile1) == content1
 | 
				
			||||||
        assert context.manager.read_file(newfile2) == content2
 | 
					        assert context.manager.read_file(newfile2) == content2
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def test_find_file(context):
 | 
				
			||||||
 | 
					    for _ in range(5):
 | 
				
			||||||
 | 
					        file_abs = context.create_random_file(context.subdir, '.txt')
 | 
				
			||||||
 | 
					        file_base = basename(file_abs)
 | 
				
			||||||
 | 
					        assert context.manager.find_file(file_base) == file_abs
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user