setting global turbogears variables.

41 views
Skip to first unread message

SamDonaldson

unread,
Jun 10, 2007, 6:44:41 PM6/10/07
to TurboGears
Hi,

I'd like to be able to set some global server variables such as my
package path so that it's easily accessible from anywhere in my code.
I'd like to be able to store a path to my images directory etc.. How
do I do this. I'm trying to set a variable in my dev.cfg but I can't
see that variable name in any of my controller or model files etc..

Also, do anybody know how to get the package path at all times so that
I don't have to hardcode it? I need to be able to get this from any
controller. Isn't there some variable that gives this to us?

Thanks,

Sam.

Bruce Webber

unread,
Jun 10, 2007, 8:12:17 PM6/10/07
to TurboGears

I can answer the second question. In your controller you can use the
function turbogears.url() to convert a relative URL to an absolute
one. So turbogears.url('/index') would give you the absolute URL of
your '/index' page. In templates tg.url will give you the absolute
root of your site. See:

http://docs.turbogears.org/1.0/GettingStarted/URLs

--
Bruce Webber

SamDonaldson

unread,
Jun 10, 2007, 8:37:40 PM6/10/07
to TurboGears
This is not what I was asking.

I just want the absolute path on my harddrive to where the root of my
turbogears pkg exists? I want this path to be stored in some variable
that is accessible to my code as a global variable at all times. Can
I store this or add this to some turbogears config?

Anybody?

SAm

Diez B. Roggisch

unread,
Jun 11, 2007, 1:28:45 AM6/11/07
to turbo...@googlegroups.com
SamDonaldson schrieb:

turbogears.util.get_package_name()

And

pkg_resources.resource_filename()


Diez

Sanjay

unread,
Jun 11, 2007, 2:31:19 AM6/11/07
to TurboGears
> I'd like to be able to set some global server variables such as my
> package path so that it's easily accessible from anywhere in my code.
> I'd like to be able to store a path to my images directory etc.. How
> do I do this. I'm trying to set a variable in my dev.cfg but I can't
> see that variable name in any of my controller or model files etc..

I have an util.py, which I use for the purpose and import in the
controllers.
Is there any problem in this approach?

Sanjay

Christopher Arndt

unread,
Jun 11, 2007, 5:10:46 AM6/11/07
to turbo...@googlegroups.com
SamDonaldson schrieb:

> I'd like to be able to set some global server variables such as my
> package path so that it's easily accessible from anywhere in my code.
> I'd like to be able to store a path to my images directory etc.. How
> do I do this. I'm trying to set a variable in my dev.cfg but I can't
> see that variable name in any of my controller or model files etc..

---- app.cfg ----
[global]
...
img_dir = "%(top_level_dir)s/static/images"
...
---- end ----

import turbogears
img_dir = turbogears.config.get('img_dir', '/default/image/path')

Chris

SamDonaldson

unread,
Jun 12, 2007, 3:20:25 AM6/12/07
to TurboGears
Right, but hardcoding the img_dir in my dev.cfg will not make this
portable on windows. Is there a way to construct the path in the
dev.cfg using os.path.join etc.. and then storing that in img_dir and
then using it everywhere?

sam.

SamDonaldson

unread,
Jun 12, 2007, 3:44:30 AM6/12/07
to TurboGears
Right, but hardcoding the img_dir in my dev.cfg will not make this
portable on windows. Is there a way to construct the path in the
dev.cfg using os.path.join etc.. and then storing that in img_dir and
then using it everywhere?

Also, if I run my batch tool outside of the turbogears environment
(independently), I need to know the package name. How do I get this
when I run the batch tool? resource_filename says it doesn't know the
package name but when I start tg-admin shell, it does? Argh,
turbogears is starting to become a liability.

sam.

Bruce Webber

unread,
Jun 12, 2007, 1:11:02 PM6/12/07
to TurboGears
On Jun 12, 3:44 am, SamDonaldson <samonder...@gmail.com> wrote:
> Right, but hardcoding the img_dir in my dev.cfg will not make this
> portable on windows. Is there a way to construct the path in the
> dev.cfg using os.path.join etc.. and then storing that in img_dir and
> then using it everywhere?

Regarding having separate configuration files, I find I have to do
this anyway, because my database details are different on the server,
my desktop pc and my laptop. So I have three files called

dev.server.cfg
dev.desktop.cfg
dev.laptop.cfg

These files are all under version control. dev.cfg is *not* under
version control - it has to be manually copied into place from the
appropriate file. This way I can update (or export) from my version
control system into the development environment and not mess up the
configuration file.

-- Bruce

bnf-austin

unread,
Jun 14, 2007, 9:00:25 AM6/14/07
to TurboGears
We use the same concept with the opposite implementation, probably a
matter of preference:
- dev-truth.cfg is under version control
- dev-<userID or serverName>,cfg is the locally customized version of
dev-truth.cfg

That seems to simplify things, you only have one reference document
under version control instead of potentially dozens as your server
farm grows; and each server etc. can have it's locally set
customizations (I use sqlite vs my developers use PostSQL for
example).

RE setting custom path. We do something similar. Basically we can
check for the existence of a filname in a path by calling a method in
the kid template, defined in root.py controller. If it does not exist,
we revert to a default name or path defined within the controller
method. For example:

alphanum_re = re.compile('[^A-Za-z0-9]+')
def default_if_not_exists(filepattern, desired_value,
retvalpattern=None, default='Default', basedir=None,
normalize_filename=True):
if normalize_filename:
desired_value = alphanum_re.sub('_', desired_value)
if basedir == None:
basedir = config.get('static_filter.dir', 'nossatv')
if retvalpattern == None:
retvalpattern = filepattern
log.debug('Looking for %s/%s', basedir, (filepattern %
desired_value))
if os.path.exists(basedir + '/' + (filepattern % desired_value)):
return retvalpattern % desired_value
return retvalpattern % default

Hope this helps,

BNF

Reply all
Reply to author
Forward
0 new messages