示例#1
0
def test_is_hidden(temp_dir):
    from pydgeot.filesystem import is_hidden

    with open(os.path.join(temp_dir, 'visible'), 'w') as fh:
        assert not is_hidden(fh.name)

    if sys.platform != 'win32':
        with open(os.path.join(temp_dir, '.dotfile'), 'w') as fh:
            assert is_hidden(fh.name)

    if sys.platform == 'win32':
        import ctypes

        with open(os.path.join(temp_dir, 'hidden'), 'w') as fh:
            ctypes.windll.kernel32.SetFileAttributesW(fh.name, 2)
            assert is_hidden(fh.name)
    elif sys.platform == 'darwin':
        import stat

        with open(os.path.join(temp_dir, 'hidden'), 'w') as fh:
            os.chflags(fh.name, os.stat(fh.name).st_flags ^ stat.UF_HIDDEN)
            assert is_hidden(fh.name)
    def prepare(self, path):
        from .dirconfig import DirConfig

        config = DirConfig.get(self.app, path)

        if not config.is_valid:
            return

        if os.path.basename(path) == os.path.basename(config.template):
            self.app.sources.add_source(path)
            path_dir = os.path.dirname(path)
            walk = list(os.walk(path_dir))
            directories = [path for path, dirs, files in walk if not is_hidden(path)]
            directories.append(path_dir)
            for directory in directories:
                self._generate_directories.add(directory)
            return

        self.app.sources.add_source(path)
        if not os.path.basename(path) == config.index:
            target_path = self.app.target_path(path)
            directory = os.path.dirname(path)

            self.app.sources.set_targets(path, [target_path])

            exif_data = self._get_exif_data(path)
            for name, value in exif_data.items():
                self.app.contexts.add_context(path, name, str(value))
            taken_date = datetime.datetime.fromtimestamp(os.stat(path).st_ctime)
            for name in ('DateTime', 'DateTimeOriginal', 'DateTimeDigitized'):
                if name in exif_data:
                    taken_date = datetime.datetime.strptime(exif_data[name], '%Y:%m:%d %H:%M:%S')
            self.app.contexts.add_context(path, 'date', taken_date)

            self._generate_files.add(path)
            self._generate_directories.add(directory)

            parent_config = DirConfig.get(self.app, os.path.dirname(config.path))
            if parent_config.is_valid:
                target_directory = self.app.target_path(os.path.dirname(path))
                if not os.path.exists(target_directory):
                    self._generate_directories.add(parent_config.path)
    def _generate_index(self, directory):
        from .dirconfig import DirConfig

        config = DirConfig.get(self.app, directory)
        if not config.is_valid:
            return

        self.app.log.info('Generating index for \'{}\''.format(directory))

        directories = []
        files = []
        for name in os.listdir(directory):
            path = os.path.join(directory, name)
            if is_hidden(path) or os.path.basename(path) == os.path.basename(config.template):
                continue
            elif os.path.isfile(path):
                files.append(path)
            elif os.path.isdir(path):
                directories.append(path)

        directories = self._contextify_file_list(config, directory, directories)
        files = self._contextify_file_list(config, directory, files)

        target_directory = self.app.target_path(directory)
        target_path = os.path.join(target_directory, config.index)

        os.makedirs(target_directory, exist_ok=True)

        template = self._env.get_template(self.app.relative_path(config.template))
        f = open(target_path, 'w', encoding='utf-8')
        rendered = template.render(
            config=config,
            dir_name=os.path.basename(directory),
            has_parent_dir=(directory != self.app.source_root),
            dirs=directories,
            files=files)
        f.write(rendered)
        f.close()