def upload_file(): result = {"result": False, "msg": "上传失败"} if request.method == 'POST': files = request.files.get('mp4File') print(files) f = request.files['mp4File'] # f.filename (filepath, ext) = os.path.splitext(f.filename) filename = getCurrentTime() path = os.path.join(os.path.dirname(__file__), 'upload') if not os.path.exists(path): os.makedirs(path) upload_path = os.path.join(os.path.dirname(__file__), 'upload', filename + ext) f.save(upload_path) fileObj = { "fileName": f.filename, "filePath": upload_path, "id": filename } instance = FileList() instance.addFile(fileObj) result["result"] = True result["msg"] = "上传成功" result["path"] = upload_path return result return result
def __init__(self, title, buttons, results, directory=None): GenericDialog.__init__(self, title, buttons, results) if directory == None: directory = os.curdir directory = os.path.abspath(directory) # Path access. self._pathframe = HFrame() self._pathframe.minsize = 200, self._pathframe.minsize[1] self._pathframe.padding = 0 self._pathframe.border = BORDER_NONE self._lblpath = Label("#Path:") self._txtpath = Entry(directory) self._txtpath.minsize = 165, self._txtpath.minsize[1] self._lblpath.widget = self._txtpath self._pathframe.add_child(self._lblpath, self._txtpath) # File list browser. self._filelist = FileList(200, 160, directory) self.content.add_child(self._pathframe, self._filelist) # Align anything on the right. self.main.align = ALIGN_RIGHT # Events. self._txtpath.connect_signal(SIG_INPUT, self._set_directory) self._filelist.connect_signal(SIG_LISTCHANGED, self._set_path)
def __init__(self, session, leftactive, selectedid): Screen.__init__(self, session) HelpableScreen.__init__(self) self.selectedFiles = [] self.selectedid = selectedid path_left = config.plugins.filecommander.path_left_tmp.value or None path_right = config.plugins.filecommander.path_right_tmp.value or None # set filter filter = self.fileFilter() # set current folder self["list_left_head1"] = Label(path_left) self["list_left_head2"] = List() self["list_right_head1"] = Label(path_right) self["list_right_head2"] = List() if leftactive: self["list_left"] = MultiFileSelectList(self.selectedFiles, path_left, matchingPattern=filter) self["list_right"] = FileList(path_right, matchingPattern=filter) self.SOURCELIST = self["list_left"] self.TARGETLIST = self["list_right"] self.onLayoutFinish.append(self.listLeft) else: self["list_left"] = FileList(path_left, matchingPattern=filter) self["list_right"] = MultiFileSelectList(self.selectedFiles, path_right, matchingPattern=filter) self.SOURCELIST = self["list_right"] self.TARGETLIST = self["list_left"] self.onLayoutFinish.append(self.listRight) self["key_red"] = Label(_("Delete")) self["key_green"] = Label(_("Move")) self["key_yellow"] = Label(_("Copy")) self["key_blue"] = Label(_("Skip selection")) self["actions"] = HelpableActionMap(self, ["ChannelSelectBaseActions", "WizardActions", "FileNavigateActions", "MenuActions", "NumberActions", "ColorActions", "InfobarActions"], { "ok": (self.ok, _("Select (source list) or enter directory (target list)")), "back": (self.exit, _("Leave multi-select mode")), "nextMarker": (self.listRight, _("Activate right-hand file list as multi-select source")), "prevMarker": (self.listLeft, _("Activate left-hand file list as multi-select source")), "nextBouquet": (self.listRight, _("Activate right-hand file list as multi-select source")), "prevBouquet": (self.listLeft, _("Activate left-hand file list as multi-select source")), "info": (self.openTasklist, _("Show task list")), "directoryUp": (self.goParentfolder, _("Go to parent directory")), "up": (self.goUp, _("Move up list")), "down": (self.goDown, _("Move down list")), "left": (self.goLeft, _("Page up list")), "right": (self.goRight, _("Page down list")), "red": (self.goRed, _("Delete the selected files or directories")), "green": (self.goGreen, _("Move files/directories to target directory")), "yellow": (self.goYellow, _("Copy files/directories to target directory")), "blue": (self.goBlue, _("Leave multi-select mode")), "0": (self.doRefresh, _("Refresh screen")), "showMovies": (self.ok, _("Select")), }, -1) self.onLayoutFinish.append(self.onLayout)
def deleteFile(id): try: instance = FileList() isSuccess = instance.deleteFile(id) if isSuccess: resultData(True, "", "") return resultData(False, "删除失败", "") except Exception as identifier: print(identifier) return resultData(False, "删除异常")
def __init__ (self, title, buttons, results, directory=None): GenericDialog.__init__ (self, title, buttons, results) if directory == None: directory = os.curdir directory = os.path.abspath (directory) # Path access. self._pathframe = HFrame () self._pathframe.minsize = 200, self._pathframe.minsize[1] self._pathframe.padding = 0 self._pathframe.border = BORDER_NONE self._lblpath = Label ("#Path:") self._txtpath = Entry (directory) self._txtpath.minsize = 165, self._txtpath.minsize[1] self._lblpath.widget = self._txtpath self._pathframe.add_child (self._lblpath, self._txtpath) # File list browser. self._filelist = FileList (200, 160, directory) self.content.add_child (self._pathframe, self._filelist) # Align anything on the right. self.main.align = ALIGN_RIGHT # Events. self._txtpath.connect_signal (SIG_INPUT, self._set_directory) self._filelist.connect_signal (SIG_LISTCHANGED, self._set_path)
def test_DirectoriesEmpty(self): """ UnitTest: Make sure directories are empty on empty constructor """ # Check Empty fl = FileList() self.assertTrue(len(fl.directories) == 0)
def test_DirectoriesMatch(self): """ Unittest: Make sure directories passed into constructor are set """ # Check that it matches directories = ['a', 'b', 'c'] fl = FileList(directories=directories) self.assertEqual(directories, fl.directories)
def __init__(self, config={}): """ Constructor @param[in] config Will be *something*. For now, just a dict. """ ## Store our config (or load it?) self.config = config ## Our file list, created from the configuration self.FileList = FileList( directories=self.config['directories'], exclude_regexps=self.config['exclude_regexps']) ## This will be a class soon, so it can handle removeable media. # For now, it's just a directory. self.Destination = self.config['destination'] ## Copy of our files to backup self.FilesToBackup = None
def get_recurse_filelist(self): file_list = FileList() file_list.path _ self.__analysis_path file_list.extension1 = self.__analysis_extension1 file_list.extension2 = self.__analysis_extension2 file_list.set_recurse_list() self.__analysis_filelist = file_list.file_list
def test_fileSearch(self): """ Unittest: Do some searches on the local directory -- this will fail if files are added or deleted. This test does several tests on a file list generated from this directory. """ # Find python files (2) (minus pyc) # Find all files >= python files. allFL = FileList(directories=['.']) noPycFL = FileList(directories=['.'], exclude_regexps=[r'\.pyc$']) allFiles = allFL.getFiles() noPycFiles = noPycFL.getFiles() # We should have four non .pyc files self.assertEqual(len(noPycFiles), 4) # We could have some pyc files self.assertTrue(len(noPycFiles) <= len(allFiles)) # Make sure our Module and test were found. self.assertIn(os.path.join('.', 'FileList.py'), allFiles) self.assertIn(os.path.join('.', 'test_FileList.py'), allFiles) self.assertIn(os.path.join('.', 'FileList.py'), noPycFiles) self.assertIn(os.path.join('.', 'test_FileList.py'), noPycFiles) print allFL.FileInfo
def __init__(self): """ Create the main window, add the file list and the image display widgets.""" QMainWindow.__init__(self) self.setAttribute(QtCore.Qt.WA_DeleteOnClose) self.setWindowTitle("FITSBrowse") self.main_widget = QWidget(self) layout = QVBoxLayout(self.main_widget) # Default size of the image display is 800x800 and axes are displayed. image_display_figure = StaticImageDisplayWindow(self.main_widget, width=8, height=8, dpi=100) file_list = FileList(image_display_figure) # The layout has the list widget above the image display widget. layout.addWidget(file_list) layout.addWidget(image_display_figure) self.main_widget.setFocus() self.setCentralWidget(self.main_widget)
class FileDialog(GenericDialog): """FileDialog (title, buttons, results, directory=None) -> FileDialog A modal file selection dialog. The FileDialog is a GenericDialog, that allows the user to select files and directories from a filesystem using a FileList widget. It also displays an entry widget to allow quick modifications of the current directory path. The selected files and directories can be retrieved at any moment using the get_filenames() method, which returns them as a list. selection = dialog.get_filenames () Default action (invoked by activate()): See the GenericDialog class. Mnemonic action (invoked by activate_mnemonic()): See the GenericDialog class. Attributes: filelist - The FileList widget displayed on the FileDialog. """ def __init__(self, title, buttons, results, directory=None): GenericDialog.__init__(self, title, buttons, results) if directory == None: directory = os.curdir directory = os.path.abspath(directory) # Path access. self._pathframe = HFrame() self._pathframe.minsize = 200, self._pathframe.minsize[1] self._pathframe.padding = 0 self._pathframe.border = BORDER_NONE self._lblpath = Label("#Path:") self._txtpath = Entry(directory) self._txtpath.minsize = 165, self._txtpath.minsize[1] self._lblpath.widget = self._txtpath self._pathframe.add_child(self._lblpath, self._txtpath) # File list browser. self._filelist = FileList(200, 160, directory) self.content.add_child(self._pathframe, self._filelist) # Align anything on the right. self.main.align = ALIGN_RIGHT # Events. self._txtpath.connect_signal(SIG_INPUT, self._set_directory) self._filelist.connect_signal(SIG_LISTCHANGED, self._set_path) def _set_path(self): """F._set_path () -> None Sets the entry text to the current directory path. """ self._txtpath.text = self.filelist.directory def _set_directory(self): """F._set_directory () -> None Sets the directory to list after a text input """ print "switching directory..." path = os.path.normpath(self._txtpath.text) if os.path.isdir(path): if path != self.filelist.directory: self.filelist.directory = path def get_filenames(self): """F.get_filenames () -> list Gets a list with the selected filenames. """ items = self._filelist.get_selected() directory = self.filelist.directory return [os.path.join(directory, item.text) for item in items] filelist = property(lambda self: self._filelist, doc="The filelist shown on the FileDialog.")
class Backup(): """ Will read it's configuration from *somewhere*. Will create a FileList based on entries in the configuration. Has routines to create a backup at the destination (from the configuration) """ def __init__(self, config={}): """ Constructor @param[in] config Will be *something*. For now, just a dict. """ ## Store our config (or load it?) self.config = config ## Our file list, created from the configuration self.FileList = FileList( directories=self.config['directories'], exclude_regexps=self.config['exclude_regexps']) ## This will be a class soon, so it can handle removeable media. # For now, it's just a directory. self.Destination = self.config['destination'] ## Copy of our files to backup self.FilesToBackup = None def __scanSources(self): """ Scan our file list and grab a reference to the source files, so that they could be displayed later. @param[in] self.FileList - Our file list. @param[out] self.FilestoBackup - for review, and passing to manifest """ self.FilesToBackup = self.FileList.getFiles() def __checkDestination(self): """ Check that destination is writeable by createing and then deleting a file. Failures WILL throw exceptions. Check free space on destination, and store it. @returns self.DestinationSpace """ # Create a quick file on the destination drive, the delete it ## TempFile to use tmpFile = os.path.join(self.Destination, "django.tmp") open(tmpFile, "w").write("testing write") os.remove(tmpFile) # And, check our space self.DestinationSpace = self.__getFreeSpaceMb() print "DEBUG: {} has {}Mb free".format(self.Destination, self.DestinationSpace) def __getFreeSpaceMb(self): """ Return folder free space in Mb """ if platform.system() == "Windows": #yeuch free_bytes = ctypes.c_ulonglong(0) ctypes.windll.kernel32.GetDiskFreeSpaceExW( ctypes.c_wchar_p(self.Destination), None, None, ctypes.pointer(free_bytes)) return free_bytes.value / 1024 / 1024 else: st = os.statvfs(self.Destination) return st.f_bavail * st.f_frsize / 1024 / 1024 def Backup(self): ## Do simple backup -- probably just for testing. self.__checkDestination() self.__scanSources()
from UserInterface import UserInterface from FileList import FileList userInterface = UserInterface() try: #create the gui window userInterface.createTkWindow() except: #if that fails then use terminal input instead print("Interface window Could not be opened... Defaulting to text") userInterface.getTerminalInput() #get the form data from the user input directoryPath = userInterface.directoryPath searchString = userInterface.searchString outputOrder = userInterface.outputOrder files = FileList(directoryPath)
def __init__(self, session, path_left=None): # path_left == "" means device list, whereas path_left == None means saved or default value if path_left is None: if config.plugins.filecommander.savedir_left.value and config.plugins.filecommander.path_left.value and os.path.isdir(config.plugins.filecommander.path_left.value): path_left = config.plugins.filecommander.path_left.value elif config.plugins.filecommander.path_default.value and os.path.isdir(config.plugins.filecommander.path_default.value): path_left = config.plugins.filecommander.path_default.value if config.plugins.filecommander.savedir_right.value and config.plugins.filecommander.path_right.value and os.path.isdir(config.plugins.filecommander.path_right.value): path_right = config.plugins.filecommander.path_right.value elif config.plugins.filecommander.path_default.value and os.path.isdir(config.plugins.filecommander.path_default.value): path_right = config.plugins.filecommander.path_default.value else: path_right = None if path_left and os.path.isdir(path_left) and path_left[-1] != "/": path_left += "/" if path_right and os.path.isdir(path_right) and path_right[-1] != "/": path_right += "/" if path_left == "": path_left = None if path_right == "": path_right = None self.session = session Screen.__init__(self, session) HelpableScreen.__init__(self) # set filter filter = self.fileFilter() self.jobs = 0 self.updateDirs = set() self.containers = [] # set current folder self["list_left_head1"] = Label(path_left) self["list_left_head2"] = List() self["list_right_head1"] = Label(path_right) self["list_right_head2"] = List() self["list_left"] = FileList(path_left, matchingPattern=filter) self["list_right"] = FileList(path_right, matchingPattern=filter) self["key_red"] = Label(_("Delete")) self["key_green"] = Label(_("Move")) self["key_yellow"] = Label(_("Copy")) self["key_blue"] = Label(_("Rename")) self["VKeyIcon"] = Boolean(False) self["actions"] = HelpableActionMap(self, ["ChannelSelectBaseActions", "WizardActions", "FileNavigateActions", "MenuActions", "NumberActions", "ColorActions", "InfobarActions", "InfobarTeletextActions", "InfobarSubtitleSelectionActions"], { "ok": (self.ok, _("Play/view/edit/install/extract/run file or enter directory")), "back": (self.exit, _("Leave File Commander")), "menu": (self.goContext, _("Open settings/actions menu")), "nextMarker": (self.listRight, _("Activate right-hand file list as source")), "prevMarker": (self.listLeft, _("Activate left-hand file list as source")), "nextBouquet": (self.listRight, _("Activate right-hand file list as source")), "prevBouquet": (self.listLeft, _("Activate left-hand file list as source")), "1": (self.gomakeDir, _("Create directory/folder")), "2": (self.gomakeSym, _("Create user-named symbolic link")), "3": (self.gofileStatInfo, _("File/directory status information")), "4": (self.call_change_mode, _("Change execute permissions (755/644)")), "5": (self.goDefaultfolder, _("Go to bookmarked folder")), "6": (self.run_file, self.help_run_file), "7": (self.run_ffprobe, self.help_run_ffprobe), # "8": (self.run_mediainfo, self.help_run_mediainfo), "9": (self.run_hashes, _("Calculate file checksums")), "startTeletext": (self.file_viewer, _("View or edit file (if size < 1MB)")), "info": (self.openTasklist, _("Show task list")), "directoryUp": (self.goParentfolder, _("Go to parent directory")), "up": (self.goUp, _("Move up list")), "down": (self.goDown, _("Move down list")), "left": (self.goLeft, _("Page up list")), "right": (self.goRight, _("Page down list")), "red": (self.goRed, _("Delete file or directory (and all its contents)")), "green": (self.goGreen, _("Move file/directory to target directory")), "yellow": (self.goYellow, _("Copy file/directory to target directory")), "blue": (self.goBlue, _("Rename file/directory")), "0": (self.doRefresh, _("Refresh screen")), "showMovies": (self.listSelect, _("Enter multi-file selection mode")), "subtitleSelection": self.downloadSubtitles, # Unimplemented }, -1) if config.plugins.filecommander.path_left_selected: self.onLayoutFinish.append(self.listLeft) else: self.onLayoutFinish.append(self.listRight) self.onLayoutFinish.append(self.onLayout)
def getFileList(): instance = FileList() obj = resultData(True, "", instance.getList()) return obj
def createAndLoadAnalysis(anaType, runConfig, folder, name=None): """ Creates a new analysis of the specified anaType, with common analysis folder given. If name is specified, use this name, else use the default for this analysis anaType. The created analysis is then loaded and returned. raises AnalysisException_createFail if something goes wrong. """ if not anaType in AnalysisInterface.getTypes(): raise AnalysisException_createFail("Type '" + anaType + "' is not valid") name = AnalysisInterface.getName(anaType, name) if os.path.exists(os.path.join(folder, name)): raise AnalysisException_createFail("Analysis file already created?!?") import acdOpti.AcdOptiRunConfig assert isinstance(runConfig, acdOpti.AcdOptiRunConfig.AcdOptiRunConfig) if anaType == "Dummy": from Dummy import Dummy Dummy.createNew(folder, name) return Dummy(folder, name, runConfig) if anaType == "DummyInput": from DummyInput import DummyInput DummyInput.createNew(folder, name) return DummyInput(folder, name, runConfig) elif anaType == "FileList": from FileList import FileList FileList.createNew(folder,name) return FileList(folder,name,runConfig) elif anaType == "Omega3P_modeInfo": from Omega3P_modeInfo import Omega3P_modeInfo Omega3P_modeInfo.createNew(folder, name) return Omega3P_modeInfo(folder,name,runConfig) elif anaType == "RFpost": from RFpost import RFpost RFpost.createNew(folder, name) return RFpost(folder, name, runConfig) elif anaType == "RFpost_local": from RFpost_local import RFpost_local RFpost_local.createNew(folder, name) return RFpost_local(folder,name,runConfig) elif anaType == "GroupVelocity": from GroupVelocity import GroupVelocity GroupVelocity.createNew(folder, name) return GroupVelocity(folder, name, runConfig) elif anaType == "ScMax": from ScMax import ScMax ScMax.createNew(folder, name) return ScMax(folder, name, runConfig) elif anaType == "ModeFileRecalc": from ModeFileRecalc import ModeFileRecalc ModeFileRecalc.createNew(folder, name) return ModeFileRecalc(folder, name, runConfig) elif anaType == "Metadata": from Metadata import Metadata Metadata.createNew(folder, name) return Metadata(folder,name,runConfig) else: raise AnalysisException_createFail("Unknown analysis type '" + anaType + "'")
def enumFiles(self, dir): fileListCmd = self._fileListCmd % dir fileListStr = self.sendShellCommands(fileListCmd) return FileList.parse(fileListStr, re.compile(self._fileListPattern))
class FileIOTests: '''Base class implementing a general set of file I/O tests useful for testing ancient filesystems. This class is intended to be mixed in to a TestCase class for testing a specific filesystem type. targetDir property specifies root directory in which to perform tests. This property must be set by the subclass. fileList property accumulates a list of FileListEntry objects describing the files created by the test. After the tests are run, these can be used to verify the contents of the filesystem as seen from the legacy OS. testConfig property contains per-test configuration parameters which can be overridden by a subclass if necessary. ''' fileList = FileList() testConfig = obj({ 'ReadWriteFile': { 'FileSizes': [10, 1000, 500000], 'WriteSizes': [ [1, 3, 10], [13, 256, 1000], [513, 4093, 65536], ], 'ReadSizes': [ [1, 3, 10, 512], [1, 3, 10, 512], [11, 1024, 4093, 65536], ] }, 'OverwriteFile': { 'FileSizes': [2011, 30303, 166420], 'StripeSizes': [ [10, 5, 2], [512, 200, 64, 11, 8], [32768, 512, 127, 61, 32], ] }, 'TruncateFile': { 'FileSizes': [16, 65536, 800 * 1024], 'Ratios': [2, 10, 0] }, 'ExtendFile': { 'FileSizes': [2000, 3000, 60000, 199999] }, 'ReadWriteSparseFile': { 'FileSizes': [1024, 10000, 500000], # TODO: 10000000 'SkipSizes': [[32, 100], [512, 127], [32767, 75000], [500000, 1024 * 1024]], 'ReadSizes': [[1, 3, 10, 512], [1, 3, 10, 512], [11, 1024, 4093, 32768], [65535, 256 * 1024 - 1]] }, 'ChangeFileMode': { 'Modes': [ stat.S_IRUSR, stat.S_IWUSR, stat.S_IXUSR, stat.S_IRGRP, stat.S_IWGRP, stat.S_IXGRP, stat.S_IROTH, stat.S_IWOTH, stat.S_IXOTH, stat.S_ISUID | stat.S_IXUSR, stat.S_ISGID | stat.S_IXGRP ] }, 'ChangeDirectoryMode': { 'Modes': [ stat.S_IRUSR, stat.S_IWUSR, stat.S_IXUSR, stat.S_IRGRP, stat.S_IWGRP, stat.S_IXGRP, stat.S_IROTH, stat.S_IWOTH, stat.S_IXOTH, stat.S_ISVTX | stat.S_IXOTH ] }, }) def setUp(self): super().setUp() # generate deterministic random data based on the name of the # test being invoked. random.seed(self.id()) # ---------- Basic File Tests ---------- def test_01_CreateFile(self): '''Create File''' fn = os.path.join(self.targetDir, 'f01') with open(fn, "w+") as f: pass statRes = os.stat(fn) self.assertEqual(statRes.st_size, 0) self.fileList.append( FileListEntry(fn, 'f', size=0, mode=statRes.st_mode, linkCount=1)) def test_02_DeleteFile(self): '''Delete File''' fn = os.path.join(self.targetDir, 'f02') with open(fn, "w+") as f: pass statRes = os.stat(fn) self.assertEqual(statRes.st_size, 0) os.unlink(fn) with self.assertRaises(OSError) as cm: os.stat(fn) def test_03_RenameFile(self): '''Rename File''' fn1 = os.path.join(self.targetDir, 'f03-1') fn2 = os.path.join(self.targetDir, 'f03-2') with open(fn1, "w+") as f1: pass stat1 = os.stat(fn1) self.assertEqual(stat1.st_size, 0) os.rename(fn1, fn2) with self.assertRaises(OSError) as cm: os.stat(fn1) stat2 = os.stat(fn2) self.assertEqual(stat2.st_size, 0) self.assertEqual(stat2.st_mode, stat1.st_mode) # TODO: Test preservation mod time? self.fileList.append( FileListEntry(fn2, 'f', size=0, mode=stat2.st_mode, linkCount=1)) def test_04_CreateFileLink(self): '''Create File Link''' fn1 = os.path.join(self.targetDir, 'f04-1') fn2 = os.path.join(self.targetDir, 'f04-2') fn3 = os.path.join(self.targetDir, 'f04-3') with open(fn1, "w+") as f1: pass stat1 = os.stat(fn1) self.assertEqual(stat1.st_size, 0) os.link(fn1, fn2) stat2 = os.stat(fn2) self.assertEqual(stat2.st_size, 0) stat1 = os.stat(fn1) self.assertEqual(stat1.st_size, 0) os.link(fn2, fn3) stat3 = os.stat(fn3) self.assertEqual(stat3.st_size, 0) stat2 = os.stat(fn2) self.assertEqual(stat2.st_size, 0) self.fileList.append( FileListEntry(fn1, 'f', size=0, mode=stat1.st_mode, linkCount=3)) self.fileList.append( FileListEntry(fn2, 'f', size=0, mode=stat2.st_mode, linkCount=3)) self.fileList.append( FileListEntry(fn3, 'f', size=0, mode=stat3.st_mode, linkCount=3)) def test_05_DeleteFileLink(self): '''Delete File Link''' fn1 = os.path.join(self.targetDir, 'f05-1') fn2 = os.path.join(self.targetDir, 'f05-2') with open(fn1, "w+") as f1: pass stat1 = os.stat(fn1) self.assertEqual(stat1.st_size, 0) os.link(fn1, fn2) os.unlink(fn1) with self.assertRaises(OSError) as cm: os.stat(fn1) stat2 = os.stat(fn2) self.assertEqual(stat2.st_size, 0) self.fileList.append( FileListEntry(fn2, 'f', size=0, mode=stat2.st_mode, linkCount=1)) def test_06_ChangeFileMode(self): '''Change File Mode''' config = self.testConfig.ChangeFileMode for i in range(0, len(config.Modes)): mode = config.Modes[i] fn = os.path.join(self.targetDir, 'f06-%d' % (i + 1)) with open(fn, "w+") as f: pass os.chown(fn, -1, os.getegid() ) # make sure the file's group is our effective gid os.chmod(fn, mode) statRes = os.stat(fn) self.assertEqual(stat.S_IMODE(statRes.st_mode), mode) self.fileList.append( FileListEntry(fn, 'f', size=0, mode=statRes.st_mode, linkCount=1)) def test_07_ChangeFileTimes(self): '''Change File Times''' ts = 157795200 fn = os.path.join(self.targetDir, 'f07-1') with open(fn, "w+") as f: pass os.utime(fn, times=(ts, ts)) statRes = os.stat(fn) self.assertEqual(statRes.st_atime, ts) self.assertEqual(statRes.st_mtime, ts) self.fileList.append( FileListEntry(fn, 'f', size=0, mode=statRes.st_mode, linkCount=1)) def test_08_ChangeFileGroup(self): '''Change File Group''' try: gid = grp.getgrnam('users').gr_gid except KeyError: gid = None if gid not in os.getgroups(): try: gid = grp.getgrnam('everyone').gr_gid except KeyError: gid = None if gid not in os.getgroups(): self.skipTest("Suitable group not found") fn = os.path.join(self.targetDir, 'f08-1') with open(fn, "w+") as f: pass os.chown(fn, -1, gid) statRes = os.stat(fn) self.assertEqual(statRes.st_gid, gid) self.fileList.append( FileListEntry(fn, 'f', size=0, mode=statRes.st_mode, linkCount=1)) # ---------- File Read/Write Tests ---------- def test_10_ReadWriteFile(self): '''Read/Write File''' config = self.testConfig.ReadWriteFile # for each of various file sizes... for i in range(0, len(config.FileSizes)): fileName = os.path.join(self.targetDir, 'f10-%d' % (i + 1)) fileSize = config.FileSizes[i] readSizes = config.ReadSizes[i] writeSizes = config.WriteSizes[i] for writeSize in writeSizes: if os.path.exists(fileName): os.remove(fileName) with ShadowedFile(fileName) as f: # write the file f.writeRandom(fileSize, chunkSize=writeSize) # verify the reported size of the file on disk stat1 = os.stat(fileName) self.assertEqual(stat1.st_size, fileSize) # read and verify the contents of the file, in various size chunks for readSize in readSizes: f.reopen() f.readVerify(chunkSize=readSize) # compute the file's checksum expectedCksum = cksumFile(fileName)[0] # add the file to the list of generated files. self.fileList.append( FileListEntry(fileName, 'f', size=fileSize, linkCount=1, cksum=expectedCksum)) def test_11_OverwriteFile(self): '''Overwrite File''' config = self.testConfig.OverwriteFile # for each of various file sizes... for i in range(0, len(config.FileSizes)): fileName = os.path.join(self.targetDir, 'f11-%d' % (i + 1)) fileSize = config.FileSizes[i] stripeSizes = config.StripeSizes[i] with ShadowedFile(fileName) as f: # write the file f.writeRandom(fileSize) # for each stripe size, overwrite the file in stripes spaced # equidistant from each other. verify the contents of the # file at each pass. for stripeSize in stripeSizes: f.reopen() writePos = 0 while writePos < fileSize: remainingSize = fileSize - writePos writeSize = remainingSize if remainingSize < stripeSize else stripeSize f.writeRandom(writeSize, chunkSize=writeSize) writePos = f.seek(stripeSize, os.SEEK_CUR) # seek to next stripe f.readVerify() # compute the file's final checksum f.seek(0) expectedCksum = cksumStream(f)[0] # add the file to the list of generated files. self.fileList.append( FileListEntry(fileName, 'f', size=fileSize, linkCount=1, cksum=expectedCksum)) def test_12_ExtendFile(self): '''Extend File''' config = self.testConfig.ExtendFile # for each of various file sizes... for i in range(0, len(config.FileSizes)): fileSize = config.FileSizes[i] # create a file of the target size and then extend it to half again # its initial size. fileName = os.path.join(self.targetDir, 'f12-%d' % (i + 1)) with ShadowedFile(fileName) as f: extendSize = int(math.floor(fileSize / 2)) newFileSize = fileSize + extendSize f.writeRandom(fileSize) f.reopen() f.seek(0, whence=os.SEEK_END) f.writeRandom(extendSize) f.close() # verify file size on disk s = os.stat(fileName) self.assertEqual(s.st_size, newFileSize) # verify the file's contents f.open() f.readVerify() # compute the file's final checksum f.seek(0) expectedCksum = cksumStream(f)[0] # add the file to the list of generated files. self.fileList.append( FileListEntry(fileName, 'f', size=newFileSize, linkCount=1, cksum=expectedCksum)) def test_13_TruncateFile(self): '''Truncate File''' config = self.testConfig.TruncateFile # for each of various file sizes... for i in range(0, len(config.FileSizes)): fileSize = config.FileSizes[i] # for each of various truncation ratios... for ratio in config.Ratios: # create a file of the target size and then truncate it based # on the current size ratio fileName = os.path.join(self.targetDir, 'f13-%d-%d' % (i + 1, ratio)) with ShadowedFile(fileName) as f: f.writeRandom(fileSize) f.reopen() newFileSize = int(math.floor(fileSize / ratio)) if ratio > 0 else 0 f.truncate(newFileSize) f.close() # verify file size on disk s = os.stat(fileName) self.assertEqual(s.st_size, newFileSize) # verify the file's contents f.open() f.readVerify() # compute the file's final checksum f.seek(0) expectedCksum = cksumStream(f)[0] # add the file to the list of generated files. self.fileList.append( FileListEntry(fileName, 'f', size=newFileSize, linkCount=1, cksum=expectedCksum)) def test_14_ReadWriteSparseFile(self): '''Read/Write Sparse File''' config = self.testConfig.ReadWriteSparseFile chunkSize = 512 # for each of various file sizes... for i in range(0, len(config.FileSizes)): fileSize = config.FileSizes[i] readSizes = config.ReadSizes[i] skipSizes = config.SkipSizes[i] fileName = os.path.join(self.targetDir, 'f14-%d' % (i + 1)) # for each skip size... for skipSize in skipSizes: if os.path.exists(fileName): os.remove(fileName) with ShadowedFile(fileName) as f: # write the file in 512 byte chunks, skipping skipSize spans # between chunks, up to the target file size, but always # writting the last bytes in the file to ensure the proper length. remainingSize = fileSize while remainingSize > 0: if remainingSize > skipSize: f.seek(skipSize, os.SEEK_CUR) remainingSize -= skipSize else: f.seek(fileSize - 1, os.SEEK_SET) remainingSize = 1 writeSize = remainingSize if remainingSize < chunkSize else chunkSize f.writeRandom(writeSize) remainingSize -= writeSize # verify the reported size of the file on disk stat1 = os.stat(fileName) self.assertEqual(stat1.st_size, fileSize) # read and verify the contents of the file, in various size chunks for readSize in readSizes: f.reopen() f.readVerify(chunkSize=readSize) # compute the file's final checksum expectedCksum = cksumFile(fileName)[0] # add the file to the list of generated files. self.fileList.append( FileListEntry(fileName, 'f', size=fileSize, linkCount=1, cksum=expectedCksum)) # ---------- Directory Tests ---------- def test_20_CreateDirectory(self): '''Create Directory''' dn = os.path.join(self.targetDir, 'd20') os.mkdir(dn) statRes = os.stat(dn) self.assertTrue(stat.S_ISDIR(statRes.st_mode)) self.fileList.append(FileListEntry(dn, 'd', mode=statRes.st_mode)) for i in range(0, 20): fn = os.path.join(dn, "f20-%d" % (i + 1)) with open(fn, "w+") as f: pass statRes = os.stat(fn) self.assertEqual(statRes.st_size, 0) self.fileList.append( FileListEntry(fn, 'f', size=0, mode=statRes.st_mode, linkCount=1)) def test_21_RemoveDirectory(self): '''Remove Directory''' # make directory 1 dn = os.path.join(self.targetDir, 'd21-1') os.mkdir(dn) statRes = os.stat(dn) self.assertTrue(stat.S_ISDIR(statRes.st_mode)) # remove directory 1 os.rmdir(dn) # ensure directory 1 is gone with self.assertRaises(OSError) as cm: os.stat(dn) # make directory 2 dn = os.path.join(self.targetDir, 'd21-2') os.mkdir(dn) statRes = os.stat(dn) self.assertTrue(stat.S_ISDIR(statRes.st_mode)) self.fileList.append(FileListEntry(dn, 'd', mode=statRes.st_mode)) # create file in directory 2 fn = os.path.join(dn, "f21") with open(fn, "w+") as f: pass statRes = os.stat(fn) self.assertEqual(statRes.st_size, 0) self.fileList.append( FileListEntry(fn, 'f', size=0, mode=statRes.st_mode, linkCount=1)) # verify error attempting to remove non-empty directory 2 with self.assertRaises(OSError) as cm: os.rmdir(dn) def test_22_RenameDirectory(self): '''Rename Directory''' dn = os.path.join(self.targetDir, 'd22-1') os.mkdir(dn) statRes = os.stat(dn) self.assertTrue(stat.S_ISDIR(statRes.st_mode)) fn = os.path.join(dn, "f22") with open(fn, "w+") as f: pass statRes = os.stat(fn) self.assertEqual(statRes.st_size, 0) dn2 = os.path.join(self.targetDir, 'd22-2') os.rename(dn, dn2) statRes = os.stat(dn2) self.assertTrue(stat.S_ISDIR(statRes.st_mode)) self.fileList.append(FileListEntry(dn2, 'd', mode=statRes.st_mode)) fn2 = os.path.join(dn2, "f22") statRes = os.stat(fn2) self.assertEqual(statRes.st_size, 0) self.fileList.append( FileListEntry(fn2, 'f', size=0, mode=statRes.st_mode, linkCount=1)) def test_23_CreateNestedDirectories(self): '''Create Nested Directories''' dn = self.targetDir for i in range(0, 10): dn = os.path.join(dn, 'd23-%d' % (i + 1)) os.mkdir(dn) statRes = os.stat(dn) self.assertTrue(stat.S_ISDIR(statRes.st_mode)) self.fileList.append(FileListEntry(dn, 'd', mode=statRes.st_mode)) def test_24_MoveDirectory(self): '''Move Directory''' # make dir 1 dn1 = os.path.join(self.targetDir, 'd24-1') os.mkdir(dn1) # make dir 2 dn2 = os.path.join(self.targetDir, 'd24-2') os.mkdir(dn2) # make dir 3 within dir 1 dn3 = os.path.join(dn1, 'd24-3') os.mkdir(dn3) # make file 1 within dir 1 fn1 = os.path.join(dn1, "f24-1") with open(fn1, "w+") as f: pass # make file 2 within dir 2 fn2 = os.path.join(dn2, "f24-2") with open(fn2, "w+") as f: pass # make file 3 within dir 3 fn3 = os.path.join(dn3, "f24-3") with open(fn3, "w+") as f: pass # move dir 2 under dir 3 and rename newdn2 = os.path.join(dn3, 'd24-2-new') os.rename(dn2, newdn2) # verify dir 2 exists in new location with new name statRes = os.stat(newdn2) self.assertTrue(stat.S_ISDIR(statRes.st_mode)) # verify ".." entry in dir 2 now refers to dir 3 self.assertTrue( os.stat(os.path.join(newdn2, "..")).st_ino, os.stat(dn3).st_ino) # verify that file 2 still exists within dir 2 newfn2 = os.path.join(newdn2, "f24-2") statRes = os.stat(newfn2) self.assertTrue(stat.S_ISREG(statRes.st_mode)) # verify that file 1 still exists withing dir 1 statRes = os.stat(fn1) self.assertTrue(stat.S_ISREG(statRes.st_mode)) # verify error attempting to move dir 1 under dir 2 (i.e. under itself) with self.assertRaises(OSError) as cm: newdn1 = os.path.join(newdn2, 'd24-1-new') os.rename(dn3, newdn1) self.fileList.append(FileListEntry(dn1, 'd')) self.fileList.append(FileListEntry(newdn2, 'd')) self.fileList.append(FileListEntry(dn3, 'd')) self.fileList.append(FileListEntry(fn1, 'f')) self.fileList.append(FileListEntry(newfn2, 'f')) self.fileList.append(FileListEntry(fn3, 'f')) def test_25_ChangeDirectoryMode(self): '''Change Directory Mode''' config = self.testConfig.ChangeDirectoryMode for i in range(0, len(config.Modes)): mode = config.Modes[i] dn = os.path.join(self.targetDir, 'd25-%d' % (i + 1)) os.mkdir(dn) os.chmod(dn, mode) statRes = os.stat(dn) self.assertEqual(stat.S_IMODE(statRes.st_mode), mode) self.fileList.append(FileListEntry(dn, 'd', mode=statRes.st_mode))
def enumFiles(self, dir): fileListCmd = self._fileListCmd % dir fileListStr = self.sendShellCommands(fileListCmd) with open('/tmp/filelist.txt', 'wb') as f: f.write(fileListStr.encode()) return FileList.parse(fileListStr, re.compile(self._fileListPattern))
import os from FileList import FileList import yaml import pprint # for better print # Set Path input_folder_prefix = os.path.join(os.path.dirname(__file__), "txt") input_foldername_prefix = "dayTraining_" output_folder_prefix = os.path.join(os.path.dirname(__file__), "yaml") for i in range(1, 3): input_folder = os.path.join(input_folder_prefix, input_foldername_prefix) + str(i) flist = FileList(input_folder) for item in flist.getFilesPathList(): # for every .txt # print(item) # Read content item_list = [] with open(item, "r") as rf: for row in rf: item_list.append(row) output_yaml = {'annotation': {'filename': '000001.jpg', 'folder': 'VOC2007', 'object': [], 'owner': {'flickrid': 'Fried Camels',
class FileDialog (GenericDialog): """FileDialog (title, buttons, results, directory=None) -> FileDialog A modal file selection dialog. The FileDialog is a GenericDialog, that allows the user to select files and directories from a filesystem using a FileList widget. It also displays an entry widget to allow quick modifications of the current directory path. The selected files and directories can be retrieved at any moment using the get_filenames() method, which returns them as a list. selection = dialog.get_filenames () Default action (invoked by activate()): See the GenericDialog class. Mnemonic action (invoked by activate_mnemonic()): See the GenericDialog class. Attributes: filelist - The FileList widget displayed on the FileDialog. """ def __init__ (self, title, buttons, results, directory=None): GenericDialog.__init__ (self, title, buttons, results) if directory == None: directory = os.curdir directory = os.path.abspath (directory) # Path access. self._pathframe = HFrame () self._pathframe.minsize = 200, self._pathframe.minsize[1] self._pathframe.padding = 0 self._pathframe.border = BORDER_NONE self._lblpath = Label ("#Path:") self._txtpath = Entry (directory) self._txtpath.minsize = 165, self._txtpath.minsize[1] self._lblpath.widget = self._txtpath self._pathframe.add_child (self._lblpath, self._txtpath) # File list browser. self._filelist = FileList (200, 160, directory) self.content.add_child (self._pathframe, self._filelist) # Align anything on the right. self.main.align = ALIGN_RIGHT # Events. self._txtpath.connect_signal (SIG_INPUT, self._set_directory) self._filelist.connect_signal (SIG_LISTCHANGED, self._set_path) def _set_path (self): """F._set_path () -> None Sets the entry text to the current directory path. """ self._txtpath.text = self.filelist.directory def _set_directory (self): """F._set_directory () -> None Sets the directory to list after a text input """ print "switching directory..." path = os.path.normpath (self._txtpath.text) if os.path.isdir (path): if path != self.filelist.directory: self.filelist.directory = path def get_filenames (self): """F.get_filenames () -> list Gets a list with the selected filenames. """ items = self._filelist.get_selected () directory = self.filelist.directory return [os.path.join (directory, item.text) for item in items] filelist = property (lambda self: self._filelist, doc ="The filelist shown on the FileDialog.")