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.
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
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
turbogears.util.get_package_name()
And
pkg_resources.resource_filename()
Diez
I have an util.py, which I use for the purpose and import in the
controllers.
Is there any problem in this approach?
Sanjay
---- 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
sam.
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.
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
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