reading srcdir

21 views
Skip to first unread message

Nicolas Dechesne

unread,
Aug 28, 2020, 2:38:32 AM8/28/20
to sphinx-users
hey,

I would like to read the value of srcdir from inside conf.py. My documentation folder is organized such that I need to set the version tag based on the srcdir.

is that possible?

thanks!
 

Matt from Documatt

unread,
Aug 28, 2020, 4:02:59 AM8/28/20
to sphinx...@googlegroups.com
Hello,
in 99,9% cases is scrdir, a directory of conf.py. Conf.py is executed, so you can use builtin __file__ variable:

import os

print(__file__)                     # /home/matt/foo/conf.py
print(os.path.dirname(__file__))    # /home/matt/foo

If you really want to know srcdir value, you need to "hook" into Sphinx using setup() function inside conf.py:
def setup(app):
    print(app.srcdir)               # /home/matt/foo

Matt
https://blog.documatt.com

--
You received this message because you are subscribed to the Google Groups "sphinx-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sphinx-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sphinx-users/60d57327-ceb7-47ed-a6ec-4bf3803f73e4n%40googlegroups.com.

Nicolas Dechesne

unread,
Aug 28, 2020, 4:14:44 AM8/28/20
to sphinx...@googlegroups.com
hi Matt,


On Fri, Aug 28, 2020 at 10:03 AM Matt from Documatt <ma...@documatt.com> wrote:
>
> Hello,
> in 99,9% cases is scrdir, a directory of conf.py. Conf.py is executed, so you can use builtin __file__ variable:
>
> import os
>
> print(__file__) # /home/matt/foo/conf.py
> print(os.path.dirname(__file__)) # /home/matt/foo

yes, I am aware of that. But it doesn't work for me. Let me give more details.

I have the following structure:

.
|-- conf.py
|-- user-manual
|-- test-manual
|-- legacy

I also have a bunch of local static files (extensions, theme, ...).

the 'legacy' folder contains rst files that points to our
documentation *before* the conversion to Sphinx (e.g. old docs).

So I need to run sphinx-build twice, but I want to be able to reuse
the same config file, so I use the '-c' option:

sphinx-build . _build
and
sphinx-build legacy _build/legacy -c .

in conf.py I would like to know 'srcdir' so that I know if I am
building our docs, or in 'legacy' mode.

>
> If you really want to know srcdir value, you need to "hook" into Sphinx using setup() function inside conf.py:
>
> def setup(app):
> print(app.srcdir) # /home/matt/foo

That works, and i get the right srcdir value, but then, how do I set
project variables, such as version or release , from the context of
setup()?

>
> Matt
> https://blog.documatt.com
>
>
> On Fri, Aug 28, 2020 at 8:38 AM Nicolas Dechesne <nicolas....@linaro.org> wrote:
>>
>> hey,
>>
>> I would like to read the value of srcdir from inside conf.py. My documentation folder is organized such that I need to set the version tag based on the srcdir.
>>
>> is that possible?
>>
>> thanks!
>>
>>
>> --
>> You received this message because you are subscribed to the Google Groups "sphinx-users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an email to sphinx-users...@googlegroups.com.
>> To view this discussion on the web visit https://groups.google.com/d/msgid/sphinx-users/60d57327-ceb7-47ed-a6ec-4bf3803f73e4n%40googlegroups.com.
>
> --
> You received this message because you are subscribed to a topic in the Google Groups "sphinx-users" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/sphinx-users/PxeQeuzxS3k/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to sphinx-users...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sphinx-users/CAOGNDW8oQptD%3D8V6ddutsY8vjgKA%3D8sOoEo%2BUK2oCN9_et0HzA%40mail.gmail.com.

Matt from Documatt

unread,
Aug 28, 2020, 4:55:44 AM8/28/20
to sphinx...@googlegroups.com
Okay, in such case, you actually write Sphinx extension. For example, to modify copyright option from extension:

copyright = "acme inc."

def setup(app: Sphinx):
    def change(app, config):
         config.copyright = "foo bar inc."

    app.connect('config-inited', change)

End value of copyright option will be "foo bar inc".

Matt

Nicolas Dechesne

unread,
Aug 28, 2020, 6:05:31 AM8/28/20
to sphinx...@googlegroups.com
On Fri, Aug 28, 2020 at 10:55 AM Matt from Documatt <ma...@documatt.com> wrote:
>
> Okay, in such case, you actually write Sphinx extension. For example, to modify copyright option from extension:
>
> copyright = "acme inc."
>
> def setup(app: Sphinx):
> def change(app, config):
> config.copyright = "foo bar inc."
>
> app.connect('config-inited', change)
>
> End value of copyright option will be "foo bar inc".

thanks, I was able to do that, and it worked for me!
> To view this discussion on the web visit https://groups.google.com/d/msgid/sphinx-users/CAOGNDW-prVhkNZ0OkuuWwfMfw5wCt0pZfO5uteTJimuRjdd-QA%40mail.gmail.com.

Daniel Scott

unread,
Nov 25, 2020, 10:33:09 PM11/25/20
to sphinx...@googlegroups.com
--
Reply all
Reply to author
Forward
0 new messages