예제 #1
0
    def update(self) -> None:
        """Method to update the file contents, latex subfile, and metadata file.

        Returns:

        """
        if self._is_modified():
            if self._should_copy_file() is True:
                # Copy file if needed
                shutil.copyfile(Path(Gigantum.get_project_root(), self.metadata.gigantum_relative_path),
                                self.data_filename)

            # Update commit hash in metadata
            kwargs = {"content_hash": self._hash_file(Path(Gigantum.get_project_root(),
                                                           self.metadata.gigantum_relative_path).absolute().as_posix()),
                      "metadata_filename": self.metadata_filename}
            self.write_metadata(**kwargs)

            # Latex subfile
            self.write_subfile()
예제 #2
0
    def _is_modified(self) -> bool:
        """Helper method to check if a file has been modified since the last time you ran .sync()

        Returns:
            true if the file has changed since the last time you ran .sync(), false if it has not
        """
        if self.metadata.content_hash != self._hash_file(Path(Gigantum.get_project_root(),
                                                         self.metadata.gigantum_relative_path).absolute().as_posix()):
            return True
        else:
            return False
예제 #3
0
    def write_subfile(self) -> None:
        """Method to write the Latex subfile

        Returns:
            None
        """
        if not isinstance(self.metadata, DataframeFileMetadata):
            raise ValueError(
                f"Incorrect metadata type loaded: {type(self.metadata)}")

        if pandas is None:
            raise EnvironmentError(
                "Dataframe pickle file support requires pandas. "
                "Please run `pip install gigaleaf[pandas]`")

        subfile_template = Template(
            r"""\documentclass[../../main.tex]{subfiles}

% Subfile autogenerated by gigaleaf
% Gigantum revision: $gigantum_version
% Image content hash: $content_hash
\begin{document}

{$table}

\end{document}
""")

        with open(
                Path(Gigantum.get_project_root(),
                     self.metadata.gigantum_relative_path).absolute().as_posix(
                     ), 'rb') as f:
            df = pandas.read_pickle(f)
            table = df.to_latex(**self.metadata.to_latex_kwargs)

        filename = "gigantum/data/" + Path(
            self.metadata.gigantum_relative_path).name

        subfile_populated = subfile_template.substitute(
            filename=filename,
            gigantum_version=Gigantum.get_current_revision(),
            content_hash=self.metadata.content_hash,
            table=table)

        Path(self.subfile_filename).write_text(subfile_populated)
예제 #4
0
    def link(cls, relative_path: str, **kwargs: Dict[str, Any]) -> None:
        """Method to link a file output in a Gigantum Project to an Overleaf project

        Args:
            relative_path: relative path to the file from the current working dir, e.g. `../output/my_fig.png`
            **kwargs: args specific to each LinkedFile implementation

        Returns:
            None
        """
        file_path = Path(relative_path).resolve()
        if file_path.is_file() is False:
            # File provided does not exist
            raise ValueError(f"The file {file_path} does not exist. Provide a relative path from the working"
                             f"directory to your file. In Jupyter, the working directory is the directory containing "
                             f"your notebook.")

        metadata_filename = cls.get_metadata_filename(relative_path)
        metadata_abs_filename = Path(Gigantum.get_overleaf_root_directory(),
                                     'project', 'gigantum', 'metadata', metadata_filename)

        if metadata_abs_filename.exists() is True:
            # This is an update to the link, so get the current content hash for the file.
            with open(metadata_abs_filename, 'rt') as mf:
                current_metadata: Dict[str, Any] = json.load(mf)
                content_hash = current_metadata['content_hash']
        else:
            # Set content hash to init so it is always detected as "modified" on first link
            content_hash = "init"

        full_kwargs = {
            "gigantum_relative_path": file_path.relative_to(Path(Gigantum.get_project_root()).resolve()).as_posix(),
            "gigantum_version": Gigantum.get_current_revision(),
            "classname": cls.__name__,
            "content_hash": content_hash,
            "metadata_filename": metadata_filename}
        full_kwargs.update(kwargs)

        cls.write_metadata(**full_kwargs)
예제 #5
0
    def test_update_image(self, gigantum_project_fixture):

        gigaleaf = Gigaleaf()

        assert Path(Gigantum.get_overleaf_root_directory(), 'project',
                    'gigantum', 'metadata', 'fig1_png.json').is_file() is False

        gigaleaf.link_image('../output/fig1.png', width='0.8\\textwidth')

        assert Path(Gigantum.get_overleaf_root_directory(), 'project',
                    'gigantum', 'metadata', 'fig1_png.json').is_file() is True

        gigaleaf.sync()

        assert Path(Gigantum.get_overleaf_root_directory(), 'project',
                    'gigantum', 'data', 'fig1.png').is_file() is True

        metadata_file = Path(Gigantum.get_overleaf_root_directory(), 'project',
                             'gigantum', 'metadata', 'fig1_png.json')
        with open(metadata_file, 'rt') as mf:
            data = json.load(mf)

        first_hash = data['content_hash']

        test_dir = Path(__file__).parent.absolute()
        shutil.copyfile(
            Path(test_dir, 'resources', 'fig1.png').as_posix(),
            Path(Gigantum.get_project_root(), 'output', 'fig1.png'))

        gigaleaf.sync()

        metadata_file = Path(Gigantum.get_overleaf_root_directory(), 'project',
                             'gigantum', 'metadata', 'fig1_png.json')
        with open(metadata_file, 'rt') as mf:
            data = json.load(mf)

        assert first_hash != data['content_hash']