def _browseFolder(self, graph, inputFolder, filters): """ Browse filesystem and append a new graph for each files/sequences. If the script runs this function, the graph has at least a reader with a folder as filename parameter. """ items = sequenceParser.browse(inputFolder, sequenceParser.eDetectionDefault, filters) # if the recursivity is enable and there are filters, do a second browse to get the folders if self._recursive and len(filters) > 0: folders = [] itemsWithoutFilters = sequenceParser.browse( inputFolder, sequenceParser.eDetectionDefault) for item in itemsWithoutFilters: if item.getType() == sequenceParser.eTypeFolder: folders.append(item) items = sequenceParser.browse(inputFolder, sequenceParser.eDetectionDefault, filters) + tuple(folders) for item in items: itemType = item.getType() if itemType == sequenceParser.eTypeFile or itemType == sequenceParser.eTypeSequence or itemType == sequenceParser.eTypeLink: # create a new graph newGraph = copy.deepcopy(graph) newGraph.getFirstReader().setFilename( item.getAbsoluteFilepath()) if newGraph.hasWriter(): lastWriter = newGraph.getLastWriter() lastWriter.setFilename( os.path.join(lastWriter.getFilename(), item.getFilename())) # if the user add a filter of output extensions outputExtName, outputExtValue = graph.getLastWriter( ).getArgument('ext') if outputExtName: outputFilename = newGraph.getLastWriter().getFilename() newGraph.getLastWriter().setFilename( outputFilename[:outputFilename.rfind('.') + 1] + outputExtValue) # add the new graph self._graph.append(newGraph) elif itemType == sequenceParser.eTypeFolder: # sam-do --recursive if self._recursive: newGraph = copy.deepcopy(graph) newGraph.getFirstReader().setFilename( item.getAbsoluteFilepath()) if newGraph.hasWriter(): newGraph.getLastWriter().setFilename( os.path.join(graph.getLastWriter().getFilename(), item.getFilename())) self._browseFolder(newGraph, item.getAbsoluteFilepath(), filters)
def _getSequenceItemFromPath(self, inputPath, detectNegative): """ Return an Item (which corresponds to a sequence) from a path. Return None if the operation failed. """ # get input path and name inputSequencePath = os.path.dirname(inputPath) if not inputSequencePath: inputSequencePath = '.' inputSequenceName = os.path.basename(inputPath) # sam-mv --detect-negative detectionMethod = sequenceParser.eDetectionDefault if detectNegative: detectionMethod = detectionMethod | sequenceParser.eDetectionNegative # get input sequence inputItems = sequenceParser.browse(inputSequencePath, detectionMethod, [inputSequenceName]) if not len(inputItems): self.logger.error('No existing file corresponds to the given input sequence: ' + inputPath) return None if len(inputItems) > 1: self.logger.error('Several items ' + str([item.getFilename() for item in inputItems]) + ' correspond to the given input sequence: ' + inputPath) return None # check if the item is a sequence inputItem = inputItems[0] if inputItem.getType() != sequenceParser.eTypeSequence: self.logger.error('Input is not a sequence: ' + inputItem.getFilename()) return None return inputItem
def _getSequenceItemFromPath(self, inputPath, detectNegative): """ Get an Item (which corresponds to a sequence) from a path. """ # get input path and name inputSequencePath = os.path.dirname(inputPath) if not inputSequencePath: inputSequencePath = '.' inputSequenceName = os.path.basename(inputPath) # sam-mv --detect-negative detectionMethod = sequenceParser.eDetectionDefault if detectNegative: detectionMethod = detectionMethod | sequenceParser.eDetectionNegative # get input sequence inputItems = sequenceParser.browse(inputSequencePath, detectionMethod, [inputSequenceName]) if len(inputItems) != 1: self.logger.error('No existing file corresponds to the given input sequence: ' + inputPath) exit(-1) inputItem = inputItems[0] if inputItem.getType() != sequenceParser.eTypeSequence: self.logger.error('Input is not a sequence: ' + inputItem.getFilename()) exit(-1) return inputItem
def _browseFolder(self, graph, inputFolder, filters): """ Browse filesystem and append a new graph for each files/sequences. If the script runs this function, the graph has at least a reader with a folder as filename parameter. """ items = sequenceParser.browse(inputFolder, sequenceParser.eDetectionDefault, filters) for item in items: itemType = item.getType() if itemType == sequenceParser.eTypeFile or itemType == sequenceParser.eTypeSequence or itemType == sequenceParser.eTypeLink: # create a new graph newGraph = copy.deepcopy(graph) newGraph.getFirstReader().setFilename(item.getAbsoluteFilepath()) if newGraph.hasWriter(): lastWriter = newGraph.getLastWriter() lastWriter.setFilename(os.path.join(lastWriter.getFilename(), item.getFilename())) # if the user add a filter of output extensions outputExtName, outputExtValue = graph.getLastWriter().getArgument('ext') if outputExtName: outputFilename = newGraph.getLastWriter().getFilename() newGraph.getLastWriter().setFilename(outputFilename[:outputFilename.rfind('.')+1] + outputExtValue) # add the new graph self._graph.append(newGraph) elif itemType == sequenceParser.eTypeFolder: # sam-do --recursive if self._recursive: newGraph = copy.deepcopy(graph) newGraph.getFirstReader().setFilename(item.getAbsoluteFilepath()) if newGraph.hasWriter(): newGraph.getLastWriter().setFilename(os.path.join(graph.getLastWriter().getFilename(), item.getFilename())) self._browseFolder(newGraph, item.getAbsoluteFilepath(), filters)
def _printItems(self, items, args, detectionMethod, filters, level=0): """ For each items, check if it should be printed, depending on the command line options. """ # sam-ls --script if self._needToPrintCurrentFolder(args): puts(items[0].getFolder() + ':') for item in sorted(items): itemType = item.getType() toPrint = True # sam-ls -d if args.directories and itemType != sequenceParser.eTypeFolder: toPrint = False # sam-ls -f if args.files and itemType != sequenceParser.eTypeFile: toPrint = False # sam-ls -s if args.sequences and itemType != sequenceParser.eTypeSequence: toPrint = False # skip item already printed if self._isAlreadyPrinted(item): toPrint = False # print current item if toPrint: self._printItem(item, args, level) # sam-ls -R if args.recursive and itemType == sequenceParser.eTypeFolder: # sam-ls -L if args.level and args.level <= level: continue try: newFolder = os.path.join(item.getFolder(), item.getFilename()) self.logger.debug('Browse in "' + newFolder + '" with the following filters: ' + str(filters)) newItems = sequenceParser.browse(newFolder, detectionMethod, filters) level += 1 self._printItems(newItems, args, detectionMethod, filters, level) level -= 1 except IOError as e: # Permission denied for example self.logger.warning(e) # sam-ls --script if self._needToPrintCurrentFolder(args): puts(newline=True)
def sequenceParserCompleter(prefix, parsed_args, **kwargs): """ Custom Completer to manage auto competion when looking for sequences. """ directory = '.' if parsed_args.inputs and len(parsed_args.inputs): directory = os.path.abspath(parsed_args.inputs[-1]) items = sequenceParser.browse(directory, sequenceParser.eDetectionDefault) itemsStr = [str(item.getFilename()) for item in items] return itemsStr
def testBrowse(): global root_path items = sequenceParser.browse(root_path) print("items:", items) for item in items: print("item:", item) print("item:", item._folder) print("item:", item._filename) print("item:", item._type) if item._type == sequenceParser.eTypeSequence: print("item seq:", item._sequence.getAbsoluteFirstFilename()) print("item seq:", item._sequence.getFirstTime(), item._sequence.getLastTime(), item._sequence.getDuration(), item._sequence.getStandardPattern())
def _removeItems(self, items, args, detectionMethod, filters): """ For each items, check if it should be removed, depending on the command line options. Return if the operation was a success or not. """ folderItems = [] error = 0 for item in sorted(items): itemType = item.getType() toRemove = True # sam-rm -d if args.directories and itemType != sequenceParser.eTypeFolder: toRemove = False # sam-rm -f if args.files and itemType != sequenceParser.eTypeFile: toRemove = False # sam-rm -s if args.sequences and itemType != sequenceParser.eTypeSequence: toRemove = False # sam-rm -R if args.recursive and itemType == sequenceParser.eTypeFolder: subFolder = os.path.join(item.getFolder(), item.getFilename()) self.logger.debug('Browse in sub folder"' + subFolder + '" with the following filters: ' + str(filters)) newItems = sequenceParser.browse(subFolder, detectionMethod, filters) self._removeItems(newItems, args, detectionMethod, filters) if toRemove: # store folder and delete it after all other elements if itemType == sequenceParser.eTypeFolder: folderItems.insert(0, item) # remove current item else: err = self._removeItem(item, args) if err: error = err # remove folders (which should be empty at this state) for folder in folderItems: err = self._removeItem(folder, args) if err: error = err return error
def testBrowse(): global root_path items = sequenceParser.browse(root_path) print("items:", items) for item in items: print("item:", item) print("item:", item.getFolder()) print("item:", item.getFilename()) print("item:", item.getType()) if item.getType() == sequenceParser.eTypeSequence: seq = item.getSequence() print("item seq:", seq.getFirstFilename()) print("item seq:", seq.getFirstTime(), seq.getLastTime(), seq.getDuration(), seq.getFilenameWithStandardPattern()) for f in seq.getFramesIterable(): print("file:", seq.getFilenameAt(f))
def testBrowse(): global root_path items = sequenceParser.browse(root_path) print("items:", items) for item in items: print("item:", item) print("item:", item.getFolder()) print("item:", item.getFilename()) print("item:", item.getType()) if item.getType() == sequenceParser.eTypeSequence: seq = item.getSequence() print("item seq:", seq.getFirstFilename()) print("item seq:", seq.getFirstTime(), seq.getLastTime(), seq.getDuration(), seq.getStandardPattern()) for f in seq.getFramesIterable(): print("file:", seq.getFilenameAt(f))
def _removeItems(self, items, args, detectionMethod, filters): """ For each items, check if it should be removed, depending on the command line options. """ folderItems = [] error = 0 for item in sorted(items): itemType = item.getType() toRemove = True # sam-rm -d if args.directories and itemType != sequenceParser.eTypeFolder: toRemove = False # sam-rm -f if args.files and itemType != sequenceParser.eTypeFile: toRemove = False # sam-rm -s if args.sequences and itemType != sequenceParser.eTypeSequence: toRemove = False # sam-rm -R if args.recursive and itemType == sequenceParser.eTypeFolder: newItems = sequenceParser.browse(os.path.join(item.getFolder(), item.getFilename()), detectionMethod, filters) self._removeItems(newItems, args, detectionMethod, filters) if toRemove: # store folder and delete it after all other elements if itemType == sequenceParser.eTypeFolder: folderItems.insert(0, item) # remove current item else: err = self._removeItem(item, args) if err: error = err # remove folders (which are empty) for folder in folderItems: err = self._removeItem(folder, args) if err: error = err return error
def printItems(self, items, args, detectionMethod, filters, level=0): """ For each items, check if it should be printed, depending on the command line options. """ for item in sorted(items): itemType = item.getType() toPrint = True # sam-ls -d if args.directories and itemType != sequenceParser.eTypeFolder: toPrint = False # sam-ls -f if args.files and itemType != sequenceParser.eTypeFile: toPrint = False # sam-ls -s if args.sequences and itemType != sequenceParser.eTypeSequence: toPrint = False # print current item if toPrint: self.printItem(item, args, level) # sam-ls -R if args.recursive and itemType == sequenceParser.eTypeFolder: # sam-ls -L if args.level and args.level <= level: continue try: newItems = sequenceParser.browse(os.path.join(item.getFolder(), item.getFilename()), detectionMethod, filters) level += 1 self.printItems(newItems, args, detectionMethod, filters, level) level -= 1 except IOError as e: # Permission denied for example self.logger.warning(e)
def run(self, parser): """ Process the list operation. """ # Parse command-line args = parser.parse_args() # Set sam log level self.setLogLevel(args.verbose) inputs = [] # for each input to scan for inputPath in args.inputs: # if the input is a directory, add it and continue if os.path.isdir(inputPath): inputs.append(inputPath) continue # else split the input to a path and a filename subPath = os.path.dirname(inputPath) if not subPath: subPath = '.' filename = os.path.basename(inputPath) # add the path and the filename as an expression inputs.append(subPath) if filename: args.expression.append(filename) if not inputs: inputs.append(os.getcwd()) # sam-ls -a detectionMethod = sequenceParser.eDetectionDefault if args.all: detectionMethod = sequenceParser.eDetectionDefaultWithDotFile # sam-ls --detect-negative if args.detectNegative: detectionMethod = detectionMethod | sequenceParser.eDetectionNegative # sam-ls --detect-without-holes if args.detectWithoutHoles: detectionMethod = detectionMethod | sequenceParser.eDetectionSequenceWithoutHoles # sam-ls -e filters = [] for expression in args.expression: filters.append(expression) # get list of items for each inputs for inputPath in inputs: items = [] try: self.logger.debug('Browse in "' + inputPath + '" with the following filters: ' + str(filters)) items = sequenceParser.browse(inputPath, detectionMethod, filters) except IOError as e: self.logger.debug('IOError raised: "' + str(e) + '".') # if the given input does not correspond to anything if 'No such file or directory' in str(e): # try to create a sequence from the given input sequence = sequenceParser.Sequence() self.logger.debug('BrowseSequence on "' + inputPath + '".') isSequence = sequenceParser.browseSequence(sequence, inputPath) if isSequence: item = sequenceParser.Item(sequence, os.getcwd()) # check if the sequence contains at least one element if len(item.explode()): items.append(item) # else error else: self.logger.warning(e) continue # else it's not a directory else: self.logger.debug('Try a new browse with the given input name as filter.') # new path to browse newBrowsePath = os.path.dirname(inputPath) if not newBrowsePath: newBrowsePath = '.' # new filter newFilter = [] newFilter.extend(filters) newFilter.append(os.path.basename(inputPath)) # new browse self.logger.debug('Browse in "' + newBrowsePath + '" with the following filters: ' + str(newFilter)) items += sequenceParser.browse(newBrowsePath, detectionMethod, newFilter) if not len(items): self.logger.warning('No items found for input "' + inputPath + '".') else: self.printItems(items, args, detectionMethod, filters)
def run(self, parser): """ Process the list operation. """ # Parse command-line args = parser.parse_args() # Set sam log level self.setLogLevel(args.verbose) # inputs to scan inputs = [] for input in args.inputs: # if exists add the path if os.path.exists(input): inputs.append(input) # else use it as a filter expression else: args.expression.append(input) if not inputs: inputs.append(os.getcwd()) # sam-ls -a detectionMethod = sequenceParser.eDetectionDefault if args.all: detectionMethod = sequenceParser.eDetectionDefaultWithDotFile # sam-ls --detect-negative if args.detectNegative: detectionMethod = detectionMethod | sequenceParser.eDetectionNegative # sam-ls --detect-without-holes if args.detectWithoutHoles: detectionMethod = detectionMethod | sequenceParser.eDetectionSequenceWithoutHoles # sam-ls -e filters = [] for expression in args.expression: filters.append(expression) # get list of items for each inputs for input in inputs: items = [] try: self.logger.info('Launch a browse on "' + input + '" with the following filters: ' + str(filters)) items = sequenceParser.browse(input, detectionMethod, filters) except IOError as e: # if the given input does not correspond to anything if 'No such file or directory' in str(e): # try to create a sequence from the given input sequence = sequenceParser.Sequence() self.logger.info('Launch a browseSequence on "' + input + '".') isSequence = sequenceParser.browseSequence(sequence, input) if isSequence: item = sequenceParser.Item(sequence, os.getcwd()) # check if the sequence contains at least one element if len(item.explode()): items.append(item) # else error else: self.logger.warning(e) continue # else it's not a directory: try a new browse with the given input name as filter else: # new path to browse newBrowsePath = os.path.dirname(input) if not newBrowsePath: newBrowsePath = '.' # new filter newFilter = [] newFilter.extend(filters) newFilter.append(os.path.basename(input)) # new browse self.logger.info('Launch a browse on "' + newBrowsePath + '" with the following filters: ' + str(newFilter)) items += sequenceParser.browse(newBrowsePath, detectionMethod, newFilter) if not len(items): self.logger.warning('No items found for input "' + input + '".') else: self.printItems(items, args, detectionMethod, filters)
def run(self, parser): """ Process the remove operation. """ # Parse command-line args = parser.parse_args() # Set sam log level self.setLogLevel(args.verbose) # check command line if args.range and (args.firstImage is not None or args.lastImage is not None): self.logger.error('You cannot cumulate multiple options to specify the range of sequence.') exit(1) if '.' in args.inputs or '..' in args.inputs: self.logger.error('You cannot remove folders "." or "..".') exit(1) # sam-rm -a detectionMethod = sequenceParser.eDetectionDefault if args.all: detectionMethod = sequenceParser.eDetectionDefaultWithDotFile # sam-rm --detect-negative if args.detectNegative: detectionMethod = detectionMethod | sequenceParser.eDetectionNegative # sam-rm -e filters = [] if args.expression: filters.append(args.expression) # get list of items for each inputs error = 0 for input in args.inputs: items = [] # input is a directory if not os.path.basename(input): items.append(sequenceParser.Item(sequenceParser.eTypeFolder, input)) # else browse directory with a filter, to find the corresponding Item else: # get path to browse pathToBrowse = os.path.dirname(input) if not pathToBrowse: pathToBrowse = '.' # get filter filterToBrowse = [] filterToBrowse.extend(filters) filterToBrowse.append(os.path.basename(input)) # browse items = sequenceParser.browse(pathToBrowse, detectionMethod, filterToBrowse) # print error if no items were found if len(items) == 0: self.logger.error('No file or folders correspond to "' + input + '".') error = 1 continue err = self._removeItems(items, args, detectionMethod, filters) if err: error = err exit(error)
def run(self, parser): """ Process the list operation. """ # Parse command-line args = parser.parse_args() # inputs to scan inputs = [] for input in args.inputs: # if exists add the path if os.path.exists(input): inputs.append(input) # else use it as a filter expression else: args.expression.append(input) if not inputs: inputs.append(os.getcwd()) # sam-ls -a detectionMethod = sequenceParser.eDetectionDefault if args.all: detectionMethod = sequenceParser.eDetectionDefaultWithDotFile # sam-ls --detect-negative if args.detectNegative: detectionMethod = detectionMethod | sequenceParser.eDetectionNegative # sam-ls --detect-without-holes if args.detectWithoutHoles: detectionMethod = detectionMethod | sequenceParser.eDetectionSequenceWithoutHoles # sam-ls -e filters = [] for expression in args.expression: filters.append(expression) # get list of items for each inputs for input in inputs: items = [] try: items = sequenceParser.browse(input, detectionMethod, filters) except IOError as e: # if the given input does not correspond to anything if 'No such file or directory' in str(e): # try to create a sequence from the given input sequence = sequenceParser.Sequence() isSequence = sequenceParser.browseSequence(sequence, input) if isSequence: item = sequenceParser.Item(sequence, os.getcwd()) # check if the sequence contains at least one element if len(item.explode()): items.append(item) # else error else: self.logger.warning(e) continue # else it's not a directory: try a new browse with the given input name as filter else: # new path to browse newBrowsePath = os.path.dirname(input) if not newBrowsePath: newBrowsePath = '.' # new filter newFilter = [] newFilter.extend(filters) newFilter.append(os.path.basename(input)) # new browse items += sequenceParser.browse(newBrowsePath, detectionMethod, newFilter) if not len(items): self.logger.warning('No items found for input "' + input + '".') else: self.printItems(items, args, detectionMethod, filters)
def run(self, parser): """ Process the remove operation. """ # Parse command-line args = parser.parse_args() # Set sam log level # sam-rm --dry-run if args.dryRun: self.setLogLevel(3) # info else: self.setLogLevel(args.verbose) # check command line if args.range and (args.firstImage is not None or args.lastImage is not None): self.logger.error( 'You cannot cumulate multiple options to specify the range of sequence.' ) exit(1) if '.' in args.inputs or '..' in args.inputs: self.logger.error('You cannot remove folders "." or "..".') exit(1) # sam-rm -a detectionMethod = sequenceParser.eDetectionDefault if args.all: detectionMethod = sequenceParser.eDetectionDefaultWithDotFile # sam-rm --detect-negative if args.detectNegative: detectionMethod = detectionMethod | sequenceParser.eDetectionNegative # sam-rm -e filters = [] if args.expression: filters.append(args.expression) # get list of items for each inputs error = 0 for inputPath in args.inputs: items = [] # input is a directory if not os.path.basename(inputPath): items.append( sequenceParser.Item(sequenceParser.eTypeFolder, inputPath)) # else browse directory with a filter, to find the corresponding Item else: # get path to browse pathToBrowse = os.path.dirname(inputPath) if not pathToBrowse: pathToBrowse = '.' # get filter filterToBrowse = [] filterToBrowse.extend(filters) filterToBrowse.append(os.path.basename(inputPath)) # browse self.logger.debug('Browse in "' + pathToBrowse + '" with the following filters: ' + str(filterToBrowse)) items = sequenceParser.browse(pathToBrowse, detectionMethod, filterToBrowse) # print error if no items were found if len(items) == 0: self.logger.error( 'No files, sequences or folders correspond to "' + inputPath + '".') error = 1 continue err = self._removeItems(items, args, detectionMethod, filters) if err: error = err exit(error)
def run(self, parser): """ Process the list operation. """ # Parse command-line args = parser.parse_args() # sam-ls --absolute-path --relative-path if args.absolutePath and args.relativePath: self._displayCommandLineHelp(parser) exit(1) # Set sam log level self.setLogLevel(args.verbose) class InputToBrowse(object): """ Represents an input to browse: a path and a list of filters. """ def __init__(self, inputPath): self.inputPath = inputPath self.filters = [] def addFilter(self, filterToAdd): self.filters.append(filterToAdd) # translate user inputs to a list of InputToBrowse inputsToBrowse = [] for inputPath in args.inputs: # if the input is a directory, add it and continue if os.path.isdir(inputPath): inputsToBrowse.append(InputToBrowse(inputPath)) continue # else split the input to a path and a filename subPath = os.path.dirname(inputPath) if not subPath: subPath = '.' filename = os.path.basename(inputPath) # add the path and the filename as an expression inputToBrowse = InputToBrowse(subPath) inputToBrowse.addFilter(filename) inputsToBrowse.append(inputToBrowse) # if no user input, will browse in the current working directory if not inputsToBrowse: inputsToBrowse.append(InputToBrowse(os.getcwd())) # sam-ls -a detectionMethod = sequenceParser.eDetectionDefault if args.all: detectionMethod = sequenceParser.eDetectionDefaultWithDotFile # sam-ls --detect-negative if args.detectNegative: detectionMethod = detectionMethod | sequenceParser.eDetectionNegative # sam-ls --detect-without-holes if args.detectWithoutHoles: detectionMethod = detectionMethod | sequenceParser.eDetectionSequenceWithoutHoles # sam-ls -e for expression in args.expression: for inputToBrowse in inputsToBrowse: inputToBrowse.addFilter(expression) error = 0 # for each input to browse, print the finding items for inputToBrowse in inputsToBrowse: items = [] inputPath = inputToBrowse.inputPath filters = inputToBrowse.filters try: self.logger.debug('Browse in "' + inputPath + '" with the following filters: ' + str(filters)) items = sequenceParser.browse(inputPath, detectionMethod, filters) except IOError as e: self.logger.debug('IOError raised: "' + str(e) + '".') # if the given input does not correspond to anything if 'No such file or directory' in str(e): # try to create a sequence from the given input sequence = sequenceParser.Sequence() self.logger.debug('BrowseSequence on "' + inputPath + '".') isSequence = sequenceParser.browseSequence( sequence, inputPath) if isSequence: item = sequenceParser.Item(sequence, os.getcwd()) # check if the sequence contains at least one element if len(item.explode()): items.append(item) # else error else: self.logger.warning(e) continue # else it's not a directory else: self.logger.debug( 'Try a new browse with the given input name as filter.' ) # new path to browse newBrowsePath = os.path.dirname(inputPath) if not newBrowsePath: newBrowsePath = '.' # new filter newFilter = [] newFilter.extend(filters) newFilter.append(os.path.basename(inputPath)) # new browse self.logger.debug('Browse in "' + newBrowsePath + '" with the following filters: ' + str(newFilter)) items += sequenceParser.browse(newBrowsePath, detectionMethod, newFilter) if not len(items): self.logger.warning('No items found for input "' + inputPath + '" with the following filters: ' + str(filters)) error = 1 else: self._printItems(items, args, detectionMethod, filters) exit(error)
def run(self, parser): """ Process the list operation. """ # Parse command-line args = parser.parse_args() # sam-ls --absolute-path --relative-path if args.absolutePath and args.relativePath: self._displayCommandLineHelp(parser) exit(1) # Set sam log level self.setLogLevel(args.verbose) class InputToBrowse(object): """ Represents an input to browse: a path and a list of filters. """ def __init__(self, inputPath): self.inputPath = inputPath self.filters = [] def addFilter(self, filterToAdd): self.filters.append(filterToAdd) # translate user inputs to a list of InputToBrowse inputsToBrowse = [] for inputPath in args.inputs: # if the input is a directory, add it and continue if os.path.isdir(inputPath): inputsToBrowse.append(InputToBrowse(inputPath)) continue # else split the input to a path and a filename subPath = os.path.dirname(inputPath) if not subPath: subPath = '.' filename = os.path.basename(inputPath) # add the path and the filename as an expression inputToBrowse = InputToBrowse(subPath) inputToBrowse.addFilter(filename) inputsToBrowse.append(inputToBrowse) # if no user input, will browse in the current working directory if not inputsToBrowse: inputsToBrowse.append(InputToBrowse(os.getcwd())) # sam-ls -a detectionMethod = sequenceParser.eDetectionDefault if args.all: detectionMethod = sequenceParser.eDetectionDefaultWithDotFile # sam-ls --detect-negative if args.detectNegative: detectionMethod = detectionMethod | sequenceParser.eDetectionNegative # sam-ls --detect-without-holes if args.detectWithoutHoles: detectionMethod = detectionMethod | sequenceParser.eDetectionSequenceWithoutHoles # sam-ls -e for expression in args.expression: for inputToBrowse in inputsToBrowse: inputToBrowse.addFilter(expression) error = 0 # for each input to browse, print the finding items for inputToBrowse in inputsToBrowse: items = [] inputPath = inputToBrowse.inputPath filters = inputToBrowse.filters try: self.logger.debug('Browse in "' + inputPath + '" with the following filters: ' + str(filters)) items = sequenceParser.browse(inputPath, detectionMethod, filters) except IOError as e: self.logger.debug('IOError raised: "' + str(e) + '".') # if the given input does not correspond to anything if 'No such file or directory' in str(e): # try to create a sequence from the given input sequence = sequenceParser.Sequence() self.logger.debug('BrowseSequence on "' + inputPath + '".') isSequence = sequenceParser.browseSequence(sequence, inputPath) if isSequence: item = sequenceParser.Item(sequence, os.getcwd()) # check if the sequence contains at least one element if len(item.explode()): items.append(item) # else error else: self.logger.warning(e) continue # else it's not a directory else: self.logger.debug('Try a new browse with the given input name as filter.') # new path to browse newBrowsePath = os.path.dirname(inputPath) if not newBrowsePath: newBrowsePath = '.' # new filter newFilter = [] newFilter.extend(filters) newFilter.append(os.path.basename(inputPath)) # new browse self.logger.debug('Browse in "' + newBrowsePath + '" with the following filters: ' + str(newFilter)) items += sequenceParser.browse(newBrowsePath, detectionMethod, newFilter) if not len(items): self.logger.warning('No items found for input "' + inputPath + '" with the following filters: ' + str(filters)) error = 1 else: self._printItems(items, args, detectionMethod, filters) exit(error)
def browse(self): return sequenceParser.browse(self.path)