Re: Best practice for config files?

2025-05-24 Thread Jason H via Python-list

On 22/05/2025 20:59, Michael F. Stemper wrote:

I recently wrote a program to do some record-keeping for me. I found
myself hard-coding a bunch of different values into it. This didn't
seem right, so I made my first use of configparser.ConfigParser().
Created the configuration file and everything is working fine.

However, I wrote it based on the assumption that the program is
running in the directory where the config file is stored, and has
a specific name. I started having some second thoughts here.

I thought about putting the location of the configuration file in
the configuration file, but that seemed like a non-starter.[1]

Should I specify the location of the config file with a command-line
option, or is requiring the program to be executed in the directory
containing the configuration file considered acceptable practice?



[1] See Tegan Jovanka in _Castrovalva_ for more on this idea.


So, I use an environment variable because my config is shared between Python
and Java auto test frameworks. I think keeping the config adjacent to the
.py files is also workable because a Python program can know where it is:

from pathlib import Path

script_path = Path(__file__).resolve()
script_directory = script_path.parent

print(f"The script is located at: {script_path}")
print(f"The script is located in the directory: {script_directory}")


--
A PICKER OF UNCONSIDERED TRIFLES
--
https://mail.python.org/mailman3//lists/python-list.python.org


Re: Best practice for config files?

2025-05-24 Thread Michael F. Stemper via Python-list

On 22/05/2025 15.27, Stefan Ram wrote:

"Michael F. Stemper"  wrote or quoted:

Should I specify the location of the config file with a command-line
option, or is requiring the program to be executed in the directory
containing the configuration file considered acceptable practice?


   It was me who digged out this "platformdirs" "user_config_dir"
   API using a source code search on my harddisk without any help.
   But then I asked my buddy, the chatbot, to explain how to
   use it, which I include here, followed by some more words
   of my own at the end. Chatbot:


[massive snip]


   On Linux:

~/.config/YourAppName


[another one]


Wow, if that's the best practice, I'll settle for second-best!

Somebody who wished to remain anonymous contacted me via email and
suggested that I could have my cake and eat it, too. I am going
ahead with having a default location for the config file, as well
as a command-line option to specify a different file. Blindingly
obvious!

And the default will not be in the directory in which the program
is being run. Your post reminded me of the existence of $HOME/.config
which is obviously the right place for it.

Thanks for all of the suggestions.

--
Michael F. Stemper
I refuse to believe that a corporation is a person until Texas executes one.
--
https://mail.python.org/mailman3//lists/python-list.python.org


Re: Searching for a file

2025-05-24 Thread Mats Wichmann

On 5/23/25 16:05, Rob Cliffe via Python-list wrote:



On 23/05/2025 18:55, Mats Wichmann wrote:

On 5/22/25 21:04, Rob Cliffe via Python-list wrote:
It occurs to me that it might be useful if Python provided a function 
to search for a file with a given name in various directories (much 
as the import.import_lib function searches for a module in the 
directories in sys.path).

This function would perhaps be best placed in the os.path or os modules.
To start the ball rolling, I offer this version:

consider: os.walk, glob.glob, Path.glob



I have.  None of these are appropriate.
os.walk iterates *recursively* over a *single* directory and its 
subdirectories.
pathlib.Path.glob so far as I can make out (I have never used pathlib) 
does much the same.
glob.glob (so far as I can make out) does a *wildcard* search for 
directories matching a *single* pattern.
My suggestion needs a *non-recursive* search for a *file* in a *list* of 
*non-wildcarded* directories.

Best wishes
Rob Cliffe


They don't give you "search in a list of directories" intrinsically, but 
that's simple loop, bailing out on a match, no?


pathlib's glob method is more like glob.glob than os.walk - though 
there's also a walk method in pathlib that's in the same spirit as 
os.walk. The globs are only recursive if you ask them to be, and if you 
don't want wildcarding, don't include any wildcard characters in the 
pattern.

--
https://mail.python.org/mailman3//lists/python-list.python.org


Re: Searching for a file

2025-05-24 Thread Rob Cliffe via Python-list



On 25/05/2025 00:18, Mats Wichmann wrote:

On 5/23/25 16:05, Rob Cliffe via Python-list wrote:



On 23/05/2025 18:55, Mats Wichmann wrote:

On 5/22/25 21:04, Rob Cliffe via Python-list wrote:
It occurs to me that it might be useful if Python provided a 
function to search for a file with a given name in various 
directories (much as the import.import_lib function searches for a 
module in the directories in sys.path).
This function would perhaps be best placed in the os.path or os 
modules.

To start the ball rolling, I offer this version:

consider: os.walk, glob.glob, Path.glob



I have.  None of these are appropriate.
os.walk iterates *recursively* over a *single* directory and its 
subdirectories.
pathlib.Path.glob so far as I can make out (I have never used 
pathlib) does much the same.
glob.glob (so far as I can make out) does a *wildcard* search for 
directories matching a *single* pattern.
My suggestion needs a *non-recursive* search for a *file* in a *list* 
of *non-wildcarded* directories.

Best wishes
Rob Cliffe


They don't give you "search in a list of directories" intrinsically, 
but that's simple loop, bailing out on a match, no?


pathlib's glob method is more like glob.glob than os.walk - though 
there's also a walk method in pathlib that's in the same spirit as 
os.walk. The globs are only recursive if you ask them to be, and if 
you don't want wildcarding, don't include any wildcard characters in 
the pattern.
Yes, but if I understand correctly, they all start from a single 
directory (and work downwards if required).
My suggestion involved searching a *list* (possibly multiple lists) of 
directories.

Rob
--
https://mail.python.org/mailman3//lists/python-list.python.org


Re: Best practice for config files?

2025-05-24 Thread Mats Wichmann

On 5/23/25 07:35, Chuck Rhode wrote:

On Thu, 22 May 2025 14:59:28 -0500
"Michael F. Stemper"  wrote:


Is requiring the program to be executed in the directory containing
the configuration file considered acceptable practice?


Freedesktop.org proposes a specification for where such things ought
to be located:

+ https://specifications.freedesktop.org/basedir-spec/latest/

Here's how I do it:


def get_xdg_config_home():



 """The configuration directory.



 Normally $HOME/.config.



 """



 result = pathlib.Path.home() / ".config"
 return result


platformdirs is useful to handle this kind of stuff - and extends beyond 
the freedesktop specification to cover Windows as well.  I won't rehash 
the things you can query, since they're on the page:


https://pypi.org/project/platformdirs/
--
https://mail.python.org/mailman3//lists/python-list.python.org


Re: Searching for a file

2025-05-24 Thread Chris Angelico via Python-list
On Sun, 25 May 2025 at 10:05, Rob Cliffe via Python-list
 wrote:
> Yes, but if I understand correctly, they all start from a single
> directory (and work downwards if required).
> My suggestion involved searching a *list* (possibly multiple lists) of
> directories.

for dir in dirs:
try: open(dir + "/" + fn).close()
except FileNotFoundError: pass
else: break

Is this really all that difficult? Not everything has to be in the stdlib.

ChrisA
-- 
https://mail.python.org/mailman3//lists/python-list.python.org