Skip to content
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

SourceAnalysis.from_file: Allow passing open file handle #100

Closed
Natureshadow opened this issue Jan 6, 2023 · 6 comments · Fixed by #101
Closed

SourceAnalysis.from_file: Allow passing open file handle #100

Natureshadow opened this issue Jan 6, 2023 · 6 comments · Fixed by #101
Assignees
Milestone

Comments

@Natureshadow
Copy link
Contributor

In addition to only a path, it would be helpful if SourceAnalysis.from_file could be passed an open file handle.

That would help for analyzing code from files not available in the local filesystem directly, like from the object database of a Git repository. Specifically, I am writing a statistical analytics software for full Git repositories, and am reading data directly from Git objects instead of a working tree. Gitpython gives me a filename and a file-like object to read from.

@roskakori roskakori moved this from 🆕 New to 📋 Backlog in Open source projects Jan 8, 2023
@roskakori roskakori added this to the v1.5.2 milestone Jan 8, 2023
@roskakori
Copy link
Owner

Sounds like a good idea.

Can you provide a small, self-contained code example how you would read a (hard coded) example file from e.g. the pygount repository using gitpython?

@Natureshadow
Copy link
Contributor Author

Natureshadow commented Jan 8, 2023

Sure, here you go. To analyze the file pygount/analysis.py from the second-to-last commit:

from tempfile import TemporaryDirectory

import git
from pygount.analysis import SourceAnalysis


with TemporaryDirectory() as d:
    repo = git.Repo.clone_from("https://github.com/roskakori/pygount", d)
    file = repo.head.commit.parents[0].tree / "pygount" / "analysis.py"
    # Now, file.path is the relative path and name of the file, and
    #  file.data_stream is an open file-like object to read() from
    SourceAnalysis.from_file(file.path, fh=file.data_stream)

@Natureshadow
Copy link
Contributor Author

Natureshadow commented Jan 8, 2023

The use-case is supported by my PR #101 . The GitPython example above does not work though, because the GitPython developers have a different understanding of a file-like object than the rest of the work. But I can work around that in my project with the API in #101.

@Natureshadow
Copy link
Contributor Author

Looking at the milestone in this issue: The new version should be 1.6.0, as this introduces a new feature. (cf. https://semver.org/)

@roskakori roskakori linked a pull request Feb 12, 2023 that will close this issue
@roskakori
Copy link
Owner

roskakori commented Feb 12, 2023

Looking at the milestone in this issue: The new version should be 1.6.0, as this introduces a new feature.

True, changed it.

@roskakori
Copy link
Owner

@Natureshadow Thanks for the PR, finally got a look at it. For the most part it looks fine, left some questions as review comments.

@roskakori roskakori self-assigned this Feb 12, 2023
@roskakori roskakori moved this from 📋 Backlog to 🏗 In progress in Open source projects Feb 12, 2023
roskakori added a commit that referenced this issue May 7, 2023
#100 Allow analyzing code from an open file
@github-project-automation github-project-automation bot moved this from 🏗 In progress to ✅ Done in Open source projects May 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants