Pythonic Idiom For Searching An Include Path

T

Tim Daneliuk

Given a program 'foo' that takes a command line argument '-I
includefile', I want to be able to look for 'includefile' in a path
specified in an environment variable, 'FOOPATH'.

I'd like a semantic that says:

"If 'includefile' contains one or more path separator characters,
ignore 'FOOPATH'. If it contains no path separators, look for it in
the paths specified by 'FOOPATH', beginning with the leftmost path
first."

Is there a standard Pythonic idiom for doing this or do I need to cook
up my own.


TIA,
 
C

Chris Rebert

Given a program 'foo' that takes a command line argument '-I
includefile', I want to be able to look for 'includefile' in a path
specified in an environment variable, 'FOOPATH'.

I'd like a semantic that says:

 "If 'includefile' contains one or more path separator characters,
  ignore 'FOOPATH'. If it contains no path separators, look for it in
  the paths specified by 'FOOPATH', beginning with the leftmost path
  first."

Is there a standard Pythonic idiom for doing this or do I need to cook
up my own.

Cook your own. Also, I certainly wouldn't call something this complex
a mere idiom.
You will find optparse, os.sep, and os.walk helpful in writing your code.

Cheers,
Chris
 
N

Nobody

Given a program 'foo' that takes a command line argument '-I
includefile', I want to be able to look for 'includefile' in a path
specified in an environment variable, 'FOOPATH'.

I'd like a semantic that says:

"If 'includefile' contains one or more path separator characters,
ignore 'FOOPATH'. If it contains no path separators, look for it in
the paths specified by 'FOOPATH', beginning with the leftmost path
first."

Is there a standard Pythonic idiom for doing this or do I need to cook
up my own.

There isn't an idiom.

There are a surprising number of choices for such a simple task, e.g.
whether the search path is used for relative paths containing a separator,
whether you stop at the first file which exists or the first file which
meets other criteria (e.g. suitable permissions), whether default
locations come first or last, what happens if a default location is
included in the search path, etc.
 
G

Gregory Ewing

Tim said:
"If 'includefile' contains one or more path separator characters,
ignore 'FOOPATH'.

Are you sure that's exactly what you want? Usually with
such things the distinction is absolute vs. relative,
not whether there is more than one pathname component.
E.g. in a C file,

#include "GL/gl.h"

will search the include path for a directory called
"GL" containing a file called "gl.h".
 
T

Tim Daneliuk

Are you sure that's exactly what you want? Usually with
such things the distinction is absolute vs. relative,
not whether there is more than one pathname component.
E.g. in a C file,

#include "GL/gl.h"

will search the include path for a directory called
"GL" containing a file called "gl.h".

Point well taken, thanks.
 
J

Jorgen Grahn

There isn't an idiom.

There are a surprising number of choices for such a simple task, e.g.
whether the search path is used for relative paths containing a separator,
whether you stop at the first file which exists or the first file which
meets other criteria (e.g. suitable permissions), whether default
locations come first or last, what happens if a default location is
included in the search path, etc.

Another favorite is whether relative paths are relative to your
current directory, or to the location of whatever file this is to be
included /into/.

For an example where it mattered (to C compilers), google for the
paper "recursive make considered harmful". It took compiler writers
decades to realize what the best choice was there.

(By the way, -I commonly means "search this directory for include
files" rather than "include this file". You may want to avoid
confusing people by choosing another letter, like -i.)

/Jorgen
 
T

Tim Daneliuk

Another favorite is whether relative paths are relative to your
current directory, or to the location of whatever file this is to be
included /into/.

For an example where it mattered (to C compilers), google for the
paper "recursive make considered harmful". It took compiler writers
decades to realize what the best choice was there.

(By the way, -I commonly means "search this directory for include
files" rather than "include this file". You may want to avoid
confusing people by choosing another letter, like -i.)

/Jorgen

I just went ahead and implemented my own. It will eventually find it's way
to:

http://www.tundraware.com/Software/tsearchpath/

I'll post on c.l.p.a when the thing is released into the wild.


Thanks to all that took the time to respond...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top