Ejemplo n.º 1
0
    def getFilesByMime(self, mime = 'text', recursive = False):
        '''Find files by a given MIME type in self._path

        @throws InexistentPathError

        @param mime string representing the (or part of the) MIME to be matched
        @param recursive bool, if it's set to True and self_.path is a directory
        the matching will be done recursively,

        @return a Collection object consisting of File objects that match or
        False if self._path is not a directory
        '''

        if self._exists:
            if os.path.isdir(self._path):
                fileCollection = RunCollection()

                if recursive:
                    for root, dirs, files in os.walk(self._path):
                        for f in files:
                            path = os.path.join(root, f)
                            if os.path.isfile(path):
                                f = File(Path(path, self._cache))
                                if -1 != f.getMime().find(mime):
                                    fileCollection.add(f)

                else:
                    entries = os.listdir(self._path)
                    for entry in entries:

                        path = os.path.join(self._path, entry)
                        if os.path.isfile(path):
                            f = File(Path(path, self._cache))
                            if -1 != f.getMime().find(mime):
                                fileCollection.add(f)

                return fileCollection

            else:
                return False
        else:
            raise InexistentPathError(self._path)
Ejemplo n.º 2
0
    def getFilesByMime(self, mime='text', recursive=False):
        '''Find files by a given MIME type in self._path

        @throws InexistentPathError

        @param mime string representing the (or part of the) MIME to be matched
        @param recursive bool, if it's set to True and self_.path is a directory
        the matching will be done recursively,

        @return a Collection object consisting of File objects that match or
        False if self._path is not a directory
        '''

        if self._exists:
            if os.path.isdir(self._path):
                fileCollection = RunCollection()

                if recursive:
                    for root, dirs, files in os.walk(self._path):
                        for f in files:
                            path = os.path.join(root, f)
                            if os.path.isfile(path):
                                f = File(Path(path, self._cache))
                                if -1 != f.getMime().find(mime):
                                    fileCollection.add(f)

                else:
                    entries = os.listdir(self._path)
                    for entry in entries:

                        path = os.path.join(self._path, entry)
                        if os.path.isfile(path):
                            f = File(Path(path, self._cache))
                            if -1 != f.getMime().find(mime):
                                fileCollection.add(f)

                return fileCollection

            else:
                return False
        else:
            raise InexistentPathError(self._path)
Ejemplo n.º 3
0
def main(argv):
    section = ''
    recursive = False

    logLevel = logging.WARNING

    total_files = 0
    total_replacements = 0

    logging.basicConfig(level=logLevel, format='%(levelname)s: %(message)s')

    pathsCache = Cache()

    path = Path(cache=pathsCache)
    configPath = Path(cache=pathsCache)

    try:
        opts, args = getopt.getopt(
            argv, 'hrs:p:c:',
            ['help', 'recursive', 'section=', 'path=', 'config='])
    except getopt.GetoptError as detail:
        logging.error(detail)
    else:
        for opt, arg in opts:
            if opt in ('-h', '--help'):
                print __doc__
                sys.exit(0)

            elif opt in ('-r', '--recursive'):
                recursive = True

            elif opt in ('-s', '--section'):
                section = arg

            elif opt in ('-p', '--path'):
                path.setPath(arg)

            elif opt in ('-c', '--config'):
                configPath.setPath(arg)

    if not section:
        logging.error(err.err_msg.format(3, err.error[3]))
        sys.exit(1)

    if not path.exists():
        logging.error(
            err.err_msg.format(1, err.error[1].format('path', path.getPath())))
        sys.exit(1)

    if not path.hasRights(True, True):
        logging.error(
            err.err_msg.format(
                2, err.error[2].format(path.getPath(), 'read and write')))
        sys.exit(1)

    if not configPath.exists():
        logging.error(
            err.err_msg.format(
                1, err.error[1].format('config path', configPath.getPath())))
        sys.exit(1)

    if not configPath.hasRights():
        logging.error(
            err.err_msg.format(
                2, err.error[2].format(configPath.getPath(), 'read')))
        sys.exit(1)

    configFile = IniConfig(configPath)

    try:
        tokens = configFile.parse(section)
    except ConfigParser.NoSectionError:
        logging.error(err.err_msg.format(5, err.error[5].format(section)))
        sys.exit(1)

    if not tokens:
        logging.error(
            err.err_msg.format(
                4, err.error[4].format(section, configPath.getPath())))
        sys.exit(1)

    starting(section=section,
             path=path.getPath(),
             config=configPath.getPath(),
             r=recursive)

    if os.path.isdir(path.getPath()):
        fileCollection = path.getFilesByMime('text', recursive)

    else:
        fileCollection = RunCollection(File(path))

    if fileCollection:
        total_files = fileCollection.countItems()
        total_replacements = fileCollection.map(File.replace,
                                                postponeException=False,
                                                tokens=tokens)

    ending(total_replacements[0], total_files)
Ejemplo n.º 4
0
def main(argv):
    section = ''
    recursive = False

    logLevel = logging.WARNING

    total_files = 0
    total_replacements = 0

    logging.basicConfig(level=logLevel, format='%(levelname)s: %(message)s')

    pathsCache = Cache()

    path = Path(cache = pathsCache)
    configPath = Path(cache = pathsCache)

    try:
        opts, args = getopt.getopt(argv, 'hrs:p:c:',
                ['help', 'recursive', 'section=', 'path=', 'config='])
    except getopt.GetoptError as detail:
        logging.error(detail)
    else:
        for opt, arg in opts:
            if opt in ('-h', '--help'):
                print __doc__
                sys.exit(0)

            elif opt in ('-r', '--recursive'):
                recursive = True

            elif opt in ('-s', '--section'):
                section = arg

            elif opt in ('-p', '--path'):
                path.setPath(arg)

            elif opt in ('-c', '--config'):
                configPath.setPath(arg)

    if not section:
        logging.error(err.err_msg.format(3, err.error[3]))
        sys.exit(1)

    if not path.exists():
        logging.error(err.err_msg.format(1, err.error[1].format('path',
            path.getPath())))
        sys.exit(1)

    if not path.hasRights(True, True):
        logging.error(err.err_msg.format(2, err.error[2].format(path.getPath(),
            'read and write')))
        sys.exit(1)

    if not configPath.exists():
        logging.error(err.err_msg.format(1, err.error[1].format('config path',
            configPath.getPath())))
        sys.exit(1)

    if not configPath.hasRights():
        logging.error(err.err_msg.format(2,
            err.error[2].format(configPath.getPath(), 'read')))
        sys.exit(1)

    configFile = IniConfig(configPath)

    try:
        tokens = configFile.parse(section)
    except ConfigParser.NoSectionError:
        logging.error(err.err_msg.format(5, err.error[5].format(section)))
        sys.exit(1)

    if not tokens:
        logging.error(err.err_msg.format(4,
            err.error[4].format(section, configPath.getPath())))
        sys.exit(1)

    starting(section=section, path=path.getPath(), config=configPath.getPath(),
            r = recursive)

    if os.path.isdir(path.getPath()):
        fileCollection = path.getFilesByMime('text', recursive)

    else:
        fileCollection = RunCollection(File(path))

    if fileCollection:
        total_files = fileCollection.countItems()
        total_replacements = fileCollection.map(File.replace, postponeException = False, tokens = tokens)

    ending(total_replacements[0], total_files)