Navigation Menu

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

Get line number of classes and functions... #268

Closed
jedie opened this issue May 31, 2021 · 10 comments
Closed

Get line number of classes and functions... #268

jedie opened this issue May 31, 2021 · 10 comments

Comments

@jedie
Copy link

jedie commented May 31, 2021

I implement a README generate using pdoc here: boxine/bx_py_utils#76
Example result is currently: https://github.com/boxine/bx_py_utils/blob/auto-doc/README.md

The idea is to add links to the github code view page, e.g.:
https://github.com/boxine/bx_py_utils/blob/auto-doc/bx_py_utils/auto_doc.py#L18

But I can't find a way to get the line number of the functions and classes from pdoc.
In short i do:

module_obj = extract.load_module(module_name)
pdoc_module = Module(module_obj)

for item in pdoc_module.classes:
    # ...

for item in pdoc_module.functions
    # ...

In the end i process a list of pdoc.doc.Class and pdoc.doc.Function instances.
Is it possible to the the line numbers of these instances?!?

@mhils
Copy link
Member

mhils commented May 31, 2021

Would calling inspect.getsourcelines(item.obj) work for you? I think we could generally add a pdoc.doc.Doc.sourcelines property based on that if there is greater interest.

@jedie
Copy link
Author

jedie commented May 31, 2021

A yes inspect.getsourcelines(item.obj) works and i can use: lines, lnum = inspect.findsource(pdoc_item.obj) to get the line number. Great. Thanks.

Maybe i can add a PR here... But if this project doesn't need this information, it makes no sense isn't it?

@mhils
Copy link
Member

mhils commented May 31, 2021

Using pdoc as a library is absolutely an intended use case. :) I've opened #269 with a possible API! 😃

@jedie
Copy link
Author

jedie commented May 31, 2021

The generated README is online here: https://github.com/boxine/bx_py_utils#existing-stuff
And it looks like:


grafik

Another problematic part is to get the relativ path of the source file. I made a helper for this here:

https://github.com/boxine/bx_py_utils/blob/488efb9f9c4993ac97f46af29481bfb90b895251/bx_py_utils/auto_doc.py#L20

But i think i missed something. Think pdoc should hold this path already, isn't it?

@mhils
Copy link
Member

mhils commented May 31, 2021

Doc.source_file is probably what you are looking for?

@jedie
Copy link
Author

jedie commented Jun 1, 2021

No. With this I get the absolute file path. I need the relative path.

@mhils
Copy link
Member

mhils commented Jun 1, 2021

@jedie
Copy link
Author

jedie commented Jun 1, 2021

Yes, i use it in my solution, see above... The problem is: what's the base path of the project?

@mhils
Copy link
Member

mhils commented Jun 2, 2021

I'm not quite sure I understand what you want to achieve with the base path of your Python module here. Let's assume you want to document bx_py_utils.iteration.chunk_iterable. This function's .modulename will be bx_py_utils.iteration, so I would just construct a path like this:

url = f"https://github.com/boxine/bx_py_utils/blob/master/{ x.modulename.replace('.', '/') }.py#L4"

Does that make sense?

@mhils
Copy link
Member

mhils commented Jun 5, 2021

The original issue has been fixed, so I'm closing this here! 😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants