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

Add support for file:// URLs for intersphinx #882

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

idclifford
Copy link

This is a relatively simple update to support file:// urls in intersphinx. If the python library requests-file is available, file support will be added to the requests session.

Copy link
Contributor

@tristanlatr tristanlatr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @idclifford, many thanks for the PR!

I don’t quite understand how this will work since the file path does not carry the information regarding relative to what URL the links should be generated for a given inventory file. Some documentation regarding that new feature would be good, this can be added to docs/source/sphinx-integration.rst.

Also, it would be good to have some test cases that checks:

  • the inventory file loading from command line options
  • the actual links generated when we refer to an object that is defined in a inventory file

Somewhat a generic question, but, is it really worth it to introduce a new dependency just to load a file from disk? Are you using this to load files from nfs shares?

Many Thanks

@tristanlatr
Copy link
Contributor

Hello @idclifford, do you wish to continue working on this feature?

If so, I'de recommend the following approach:

  • Introduce a new repeatable option --intersphinx-file that loads a local inventory file, having the following format: PATH:BASE_URL.
    Where BASE_URL is the base for the generated links, it is mandatory if loading the inventory from a file.

Tell me what you think,

Thanks,

@idclifford
Copy link
Author

Thanks for at least considering the new feature. I had not planned on more extensive developments, but I understand you're not happy with the very basic addition.
I cannot promise to deliver anything soon, but, sure, I can give your suggestion a go.

@tristanlatr
Copy link
Contributor

It's not that I'm not happy, I'm really happy when new contributors emerges. It's just that I need to see this working, at least have a couple of unit tests. I cannot merge your contribution without tests.

Thanks for your understanding,

@idclifford
Copy link
Author

I have updated based on your suggestion.
The unit test is in progress.
Could you perhaps suggest a test that I could copy and modify?

@idclifford
Copy link
Author

I have added some unit tests as requested.

@tristanlatr
Copy link
Contributor

Hello @idclifford, many thanks for the adjustments! This is going in the right direction :)

Here is an example test case that would check the generated links when we load an inventory from a file,

The tests can be added to test_sphinx.py file

def test_generate_then_load_file(tmp_path: Path, 
                                 inv_writer_nolog: sphinx.SphinxInventoryWriter):
    
    from pydoctor.test.test_astbuilder import fromText
    from pydoctor.test.test_epydoc2stan import docstring2html

    # Create a testing inventory file under tmp_path/objects.inv
    src = '''
    class C:
        def __init__(self, a): 
            self.a = a
    '''
    system = fromText(src, modname='mylib').system
    tmp_path.mkdir()
    inv_writer_nolog.generate(system.rootobjects, tmp_path)

    # Then load this inventory file in a new system, include links to elements
    src = '''
    from mylib import C
    class Client:
        "L{C}"
        a: bytes 
        "L{C.a}"
    '''
    system2 = fromText(src, modname='myclient', 
                       systemcls=lambda: model.System(
                           model.Options.from_args(
                               [f'--intersphinx-file={tmp_path / "objects.inv"}']))).system
    
    Client_doc = docstring2html(system2.allobjects['myclient.Client'])
    Client_a_doc = docstring2html(system2.allobjects['myclient.Client.a'])

    assert Client_doc == '<p><a href="???"><p>'
    assert Client_a_doc == '<p><a href="???"><p>'

@idclifford
Copy link
Author

Documentation is still to come.

Copy link
Contributor

@tristanlatr tristanlatr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many thanks for providing unit tests, I have a few questions regarding the usage of the feature and I'de like to read more about it . My principal concern is the fact that it does not seem to be able to generate http link

Comment on lines -146 to +175
def test_fetchIntersphinxInventories_content() -> None:
@pytest.fixture(scope='module')
def tempDir(request: FixtureRequest, tmp_path_factory: TempPathFactory) -> Path:
name = request.module.__name__.split('.')[-1]
return tmp_path_factory.mktemp(f'{name}-cache')


def test_fetchIntersphinxInventories_content(tempDir:Path) -> None:
"""
Download and parse intersphinx inventories for each configured
intersphix.
"""

options = Options.defaults()
options.intersphinx = [
'http://sphinx/objects.inv',
'file:///twisted/index.inv',
]
options.intersphinx = ['http://sphinx/objects.inv']
url_content = {
'http://sphinx/objects.inv': zlib.compress(
b'sphinx.module py:module -1 sp.html -'),
'file:///twisted/index.inv': zlib.compress(
b'twisted.package py:module -1 tm.html -'),
b'sphinx.module py:module -1 sp.html -')
}

root_dir = tempDir
path = root_dir / 'objects.inv'
with open(path, 'wb') as f:
f.write(zlib.compress(b'twisted.package py:module -1 tm.html -'))

with open(root_dir / 'tm.html', "w") as f:
pass

options.intersphinx_file = [path]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please avoid modifying existing unit tests, create new ones that checks only what you are trying to check. Many thanks in advance.

Comment on lines +792 to +793
assert (root_dir / 'module1.html').samefile(inv_reader_nolog.getLink('some.module1'))
assert (root_dir / 'module2.html').samefile(inv_reader_nolog.getLink('other.module2'))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting... so if I understand correctly, your generating links that do not include a http scheme but are paths to files directly. Can you tell me more about your usage of this feature ? are you serving all pydoctor generated pages for many projects under the same folder, so these links are all relative in practice ?

Copy link

codecov bot commented Apr 9, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 93.12%. Comparing base (8d7c5d6) to head (1fa9238).

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #882   +/-   ##
=======================================
  Coverage   93.11%   93.12%           
=======================================
  Files          47       47           
  Lines        8699     8709   +10     
  Branches     1606     1607    +1     
=======================================
+ Hits         8100     8110   +10     
  Misses        337      337           
  Partials      262      262           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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

Successfully merging this pull request may close these issues.

2 participants