-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add output files class #1311
Merged
Merged
Add output files class #1311
Changes from all commits
Commits
Show all changes
50 commits
Select commit
Hold shift + click to select a range
e89fbb0
Add a tail method to inspect output files
pmrv 86df6e8
Extract file reading into method
pmrv 460dad9
Use _read_file in tail
pmrv a3fbe13
Move list files to util
pmrv e7f308f
Move read_file to util
pmrv 36d3d90
Extract definition of job archive name
pmrv 9a00f53
Add transparent compression support
pmrv a5965d0
Fix typos and inadvertant recursion in _job_is_compressed/_job_list_f…
pmrv ecde142
Fix slopiness
pmrv 138e525
Add test and even more bugfixes
pmrv 16d04f8
Format black
pyiron-runner e3a2bb5
Add efficient reverse reading with the monty package
pmrv 4e4f44d
More line separator replacements
pmrv 9392e49
Fix typo
pmrv 52a7eeb
Merge remote-tracking branch 'origin/main' into tail
jan-janssen 6a4cf9f
Merge branch 'main' into tail
liamhuber 26e0336
Use system specific linesep
pmrv 7d2089e
Format black
pyiron-runner 8ecbfd1
Enable newline translation in windows tests
pmrv 8db9284
Add a simple file browser
pmrv e1ef8c1
Add output files class
jan-janssen 020c977
Fix working directory bug
jan-janssen 0079986
Merge remote-tracking branch 'origin/main' into output_files
jan-janssen fc8ad6a
Move OutputFiles to GenericJob
jan-janssen c137a6d
rename output files to just files
jan-janssen 3146b33
fix tests
jan-janssen 254dc3c
fix tests for windows
jan-janssen 7af0164
Merge remote-tracking branch 'origin/files' into output_files
jan-janssen 086565c
update monty
jan-janssen c010b5c
fix
jan-janssen b597a57
more fixes
jan-janssen 7b6b94e
Format black
pyiron-runner b02aa2f
fix typing
jan-janssen 98a2db5
Merge remote-tracking branch 'origin/output_files' into output_files
jan-janssen 2ad9391
fix tests
jan-janssen fe1bf2e
fix ipython representation
jan-janssen 5eb2054
Format black
pyiron-runner 36ca620
try to fix tests
jan-janssen 123ee71
Merge remote-tracking branch 'origin/output_files' into output_files
jan-janssen 905bd1a
try to remove \r
jan-janssen 4ee136f
Use working directory rather than job object
jan-janssen 5cca59e
Return files as strings
jan-janssen 7718b98
Format black
pyiron-runner 01a9e70
Merge remote-tracking branch 'origin/main' into output_files
jan-janssen f48201a
Update pyiron_base/jobs/job/core.py
jan-janssen 2f8cd56
Update pyiron_base/jobs/job/extension/files.py
jan-janssen 5ae4693
Update pyiron_base/jobs/job/extension/files.py
jan-janssen 149fa23
fix test
jan-janssen e6691f6
Merge remote-tracking branch 'origin/output_files' into output_files
jan-janssen 2ff2c78
fix genericjob test
jan-janssen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,118 @@ | ||||||||||
import os | ||||||||||
from typing import List | ||||||||||
from pyiron_base.jobs.job.util import ( | ||||||||||
_working_directory_list_files, | ||||||||||
_working_directory_read_file, | ||||||||||
) | ||||||||||
|
||||||||||
|
||||||||||
class FileBrowser: | ||||||||||
""" | ||||||||||
Allows to browse the files in a job directory. | ||||||||||
|
||||||||||
By default this object prints itself as a listing of the job directory and | ||||||||||
the files inside. | ||||||||||
|
||||||||||
>>> job.files | ||||||||||
/path/to/my/job: | ||||||||||
\tpyiron.log | ||||||||||
\terror.out | ||||||||||
|
||||||||||
Access to the names of files is provided with :meth:`.list` | ||||||||||
|
||||||||||
>>> job.files.list() | ||||||||||
['pyiron.log', 'error.out', 'INCAR'] | ||||||||||
|
||||||||||
Access to the contents of files is provided by indexing into this object, | ||||||||||
which returns a list of lines in the file | ||||||||||
|
||||||||||
>>> job.files['error.out'] | ||||||||||
["Oh no\n", "Something went wrong!\n"] | ||||||||||
|
||||||||||
The :meth:`.tail` method prints the last lines of a file to stdout | ||||||||||
|
||||||||||
>>> job.files.tail('error.out', lines=1) | ||||||||||
Something went wrong! | ||||||||||
|
||||||||||
For files that have valid python variable names can also be accessed by | ||||||||||
attribute notation | ||||||||||
|
||||||||||
>>> job.files.INCAR # doctest: +SKIP | ||||||||||
File('INCAR') | ||||||||||
""" | ||||||||||
|
||||||||||
__slots__ = ("_working_directory",) | ||||||||||
|
||||||||||
def __init__(self, working_directory): | ||||||||||
self._working_directory = working_directory | ||||||||||
|
||||||||||
def _get_file_dict(self): | ||||||||||
return { | ||||||||||
f.replace(".", "_"): f | ||||||||||
for f in _working_directory_list_files( | ||||||||||
working_directory=self._working_directory | ||||||||||
) | ||||||||||
} | ||||||||||
|
||||||||||
def __dir__(self): | ||||||||||
return list(self._get_file_dict().keys()) + super().__dir__() | ||||||||||
|
||||||||||
def list(self) -> List[str]: | ||||||||||
""" | ||||||||||
List all files in the working directory of the job. | ||||||||||
""" | ||||||||||
return _working_directory_list_files(working_directory=self._working_directory) | ||||||||||
|
||||||||||
def _ipython_display_(self): | ||||||||||
path = self._job.working_directory + ":" | ||||||||||
files = [ | ||||||||||
"\t" + f | ||||||||||
for f in _working_directory_list_files( | ||||||||||
working_directory=self._working_directory | ||||||||||
) | ||||||||||
] | ||||||||||
print(os.linesep.join([path, *files])) | ||||||||||
|
||||||||||
def tail(self, file: str, lines: int = 100): | ||||||||||
""" | ||||||||||
Print the last lines of a file. | ||||||||||
|
||||||||||
Args: | ||||||||||
file (str): filename | ||||||||||
lines (int): number of lines to print | ||||||||||
|
||||||||||
Raises: | ||||||||||
FileNotFoundError: if the given file does not exist | ||||||||||
""" | ||||||||||
print( | ||||||||||
*_working_directory_read_file( | ||||||||||
working_directory=self._working_directory, file_name=file, tail=lines | ||||||||||
), | ||||||||||
sep="", | ||||||||||
) | ||||||||||
|
||||||||||
def __getitem__(self, item): | ||||||||||
if item not in _working_directory_list_files( | ||||||||||
working_directory=self._working_directory | ||||||||||
): | ||||||||||
raise KeyError(item) | ||||||||||
|
||||||||||
return File(os.path.join(self._working_directory, item)) | ||||||||||
|
||||||||||
def __getattr__(self, item): | ||||||||||
try: | ||||||||||
return self[self._get_file_dict()[item]] | ||||||||||
except KeyError: | ||||||||||
raise AttributeError(item) from None | ||||||||||
|
||||||||||
|
||||||||||
class File(str): | ||||||||||
def tail(self, lines: int = 100): | ||||||||||
print( | ||||||||||
*_working_directory_read_file( | ||||||||||
working_directory=os.path.dirname(self), | ||||||||||
file_name=os.path.basename(self), | ||||||||||
Comment on lines
+113
to
+114
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
See above. |
||||||||||
tail=lines, | ||||||||||
), | ||||||||||
sep="", | ||||||||||
) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We not actually using any string functionality here and I'd rather not let people accidentally perform string operations on this object.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason why I would like to keep the string part in, is that then I can use
os.path.join()
on the object, this is what I need to access the path.