Re: Absolute path for static assets/ Get file modified time

2011-11-16 Thread Jay T

 That worked. Thanks !

Jay

On 11/16/11 5:46 PM, Michael Merickel wrote:
You will need to use the pkg_resources api from the Python stdlib. 
Currently (this will be fixed in 1.3) there is no public API for 
computing paths.


import pkg_resources

asset_path = 'mypackage:static/favicon.ico'
package, file = asset_path.split(':', 1)
abs_path = pkg_resources.resource_filename(package, file)
last_access_time = os.stat(abs_path)[8]

On Wed, Nov 16, 2011 at 3:55 PM, Jay T > wrote:


 How do I get the absolute path for static assets ? The reason I
need the absolute path is to determine the last modified time of a
specific file so I can append it to my static resources like JS
and CSS files.
I wrote a Mako template def like this but it fails at : here =
os.path.dirname(__file__)

<%def name="appendFileModifiedTime(path)">
<%
   fmTime = -1
   import os
   here = os.path.dirname(__file__)
   path = os.path.join(here,path)
   if os.path.exists(path):
   fmTime = os.stat(path)[8]
   else:
   fmTime = -999

   path = path +'?%s'%fmTime

   return path
%>


Thanks,
Jay

-- 
You received this message because you are subscribed to the Google

Groups "pylons-discuss" group.
To post to this group, send email to
pylons-discuss@googlegroups.com
.
To unsubscribe from this group, send email to
pylons-discuss+unsubscr...@googlegroups.com
.
For more options, visit this group at
http://groups.google.com/group/pylons-discuss?hl=en.




--

Michael
--
You received this message because you are subscribed to the Google 
Groups "pylons-discuss" group.

To post to this group, send email to pylons-discuss@googlegroups.com.
To unsubscribe from this group, send email to 
pylons-discuss+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en.


--
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to pylons-discuss@googlegroups.com.
To unsubscribe from this group, send email to 
pylons-discuss+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en.



Re: Absolute path for static assets/ Get file modified time

2011-11-16 Thread Michael Merickel
You will need to use the pkg_resources api from the Python stdlib.
Currently (this will be fixed in 1.3) there is no public API for computing
paths.

import pkg_resources

asset_path = 'mypackage:static/favicon.ico'
package, file = asset_path.split(':', 1)
abs_path = pkg_resources.resource_filename(package, file)
last_access_time = os.stat(abs_path)[8]

On Wed, Nov 16, 2011 at 3:55 PM, Jay T  wrote:

>  How do I get the absolute path for static assets ? The reason I need the
> absolute path is to determine the last modified time of a specific file so
> I can append it to my static resources like JS and CSS files.
> I wrote a Mako template def like this but it fails at : here =
> os.path.dirname(__file__)
>
> <%def name="appendFileModifiedTime(**path)">
> <%
>fmTime = -1
>import os
>here = os.path.dirname(__file__)
>path = os.path.join(here,path)
>if os.path.exists(path):
>fmTime = os.stat(path)[8]
>else:
>fmTime = -999
>
>path = path +'?%s'%fmTime
>
>return path
> %>
> 
>
> Thanks,
> Jay
>
> --
> You received this message because you are subscribed to the Google Groups
> "pylons-discuss" group.
> To post to this group, send email to 
> pylons-discuss@googlegroups.**com
> .
> To unsubscribe from this group, send email to pylons-discuss+unsubscribe@*
> *googlegroups.com .
> For more options, visit this group at http://groups.google.com/**
> group/pylons-discuss?hl=en
> .
>
>


-- 

Michael

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to pylons-discuss@googlegroups.com.
To unsubscribe from this group, send email to 
pylons-discuss+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en.



Absolute path for static assets/ Get file modified time

2011-11-16 Thread Jay T
 How do I get the absolute path for static assets ? The reason I need 
the absolute path is to determine the last modified time of a specific 
file so I can append it to my static resources like JS and CSS files.
I wrote a Mako template def like this but it fails at : here = 
os.path.dirname(__file__)


<%def name="appendFileModifiedTime(path)">
<%
fmTime = -1
import os
here = os.path.dirname(__file__)
path = os.path.join(here,path)
if os.path.exists(path):
fmTime = os.stat(path)[8]
else:
fmTime = -999

path = path +'?%s'%fmTime

return path
%>


Thanks,
Jay

--
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to pylons-discuss@googlegroups.com.
To unsubscribe from this group, send email to 
pylons-discuss+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en.



Re: Looping throug lists in a template using a for loop produces a NameError

2011-11-16 Thread Eric Rasmussen
Hello,

The Mako syntax looks right, but you're using a Chameleon file
extension on your template. With the default renderer settings,
Pyramid will attempt to render it as a Chameleon template and won't
recognize the Mako formatting. It should work if you rename the
template to listOfHosts.mako instead.

Best,
Eric


On Wed, Nov 16, 2011 at 8:43 AM, David Eagle  wrote:
> I'm new to Pyramid and trying to get it to work with me, I've
> installed pyramid-1.2.1 in a virtualenv and I'm trying to loop through
> a simple list within a template but it seems to me that the statement
> "% for item in cisList" does not initialize the "item" variable but if
> I call ${cisList[0]['name']} it prints out the name value of cisList.
> I cannot see that I'm doing anything different from what is described
> in http://docs.pylonsproject.org/docs/pyramid_quick_tutorial.html#list-mako
>
> sample of my code is listed here below and the error it produces
>
> views.py:
> ...
> cisList = [{'name':'test1'},{'name':'test2'}]
>    #print ex.CIsMap
>
>    return {'cisList':cisList}
>
>
> template.pt:
> ...
> 
> % if cisList:
>    % for item in cisList:
>    
>        ${item['name']}
>    
>    %endfor
> % endif
> 
> ...
>
>
>
> chameleon.utils.NameError
> NameError: item
>
>  - Expression: "item['name']"
>  - Filename:   /home/davidoj/workspace/cmdbweb/src/cmdbweb/cmdbweb/
> templates/listOfHosts.pt
>  - Location:   (12:10)
>
>  - Source:     ${item['name']}
>                 
>  - Expression: "
>        ${item['name']}
>    "
>  - Filename:   /home/davidoj/workspace/cmdbweb/src/cmdbweb/cmdbweb/
> templates/listOfHosts.pt
>  - Location:   (11:8)
>
>  - Source:     
>                   ^
>  - Arguments:  cisList: 
>               repeat: {...} (0)
>               renderer_name: cmdbweb:templates/listOfHosts.pt
>               request: 
>               renderer_info: 
>               context: 
>               view: 
>
> --
> You received this message because you are subscribed to the Google Groups 
> "pylons-discuss" group.
> To post to this group, send email to pylons-discuss@googlegroups.com.
> To unsubscribe from this group, send email to 
> pylons-discuss+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/pylons-discuss?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to pylons-discuss@googlegroups.com.
To unsubscribe from this group, send email to 
pylons-discuss+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en.



Looping throug lists in a template using a for loop produces a NameError

2011-11-16 Thread David Eagle
I'm new to Pyramid and trying to get it to work with me, I've
installed pyramid-1.2.1 in a virtualenv and I'm trying to loop through
a simple list within a template but it seems to me that the statement
"% for item in cisList" does not initialize the "item" variable but if
I call ${cisList[0]['name']} it prints out the name value of cisList.
I cannot see that I'm doing anything different from what is described
in http://docs.pylonsproject.org/docs/pyramid_quick_tutorial.html#list-mako

sample of my code is listed here below and the error it produces

views.py:
...
cisList = [{'name':'test1'},{'name':'test2'}]
#print ex.CIsMap

return {'cisList':cisList}


template.pt:
...

% if cisList:
% for item in cisList:

${item['name']}

%endfor
% endif

...



chameleon.utils.NameError
NameError: item

 - Expression: "item['name']"
 - Filename:   /home/davidoj/workspace/cmdbweb/src/cmdbweb/cmdbweb/
templates/listOfHosts.pt
 - Location:   (12:10)

 - Source: ${item['name']}
 
 - Expression: "
${item['name']}
"
 - Filename:   /home/davidoj/workspace/cmdbweb/src/cmdbweb/cmdbweb/
templates/listOfHosts.pt
 - Location:   (11:8)

 - Source: 
   ^
 - Arguments:  cisList: 
   repeat: {...} (0)
   renderer_name: cmdbweb:templates/listOfHosts.pt
   request: 
   renderer_info: 
   context: 
   view: 

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to pylons-discuss@googlegroups.com.
To unsubscribe from this group, send email to 
pylons-discuss+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en.



Re: Multiple transactions within request

2011-11-16 Thread Vlad K.


According to my tests it is, or at least the end result is savepoint 
release being emitted before final commit. For this code:



sp = transaction.savepoint()
... do something
session.flush()

sp2 = transaction.savepoint()
... do something
session.flush()
# Oops, IntegrityError
sp.rollback()

...

transaction.commit()


The following SQL is emitted:

BEGIN (implicit)
...
SAVEPOINT sa_savepoint_1
...
SAVEPOINT sa_savepoint_2
ROLLBACK TO SAVEPOINT sa_savepoint_2
...
RELEASE SAVEPOINT sa_savepoint_1
COMMIT


The test (resulting with the SQL above) is posted here. You can vary the 
params at the beginning to try various combinations with or without the 
Transaction.


https://gist.github.com/1370792






.oO V Oo.


On 11/16/2011 05:07 PM, Michael Merickel wrote:
I will say that after looking at the SQLAlchemy documentation once 
again, it does say "For each begin_nested() call, a corresponding 
rollback() or commit() must be issued." and even from looking at the 
unit tests in zope.sqlalchemy I cannot tell where the required 
commit() is issued per savepoint. This may be worth investigating 
regardless of whether your problem is solved. :-)


http://svn.zope.org/zope.sqlalchemy/trunk/src/zope/sqlalchemy/


--
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to pylons-discuss@googlegroups.com.
To unsubscribe from this group, send email to 
pylons-discuss+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en.



Re: Multiple transactions within request

2011-11-16 Thread Michael Merickel
I will say that after looking at the SQLAlchemy documentation once again,
it does say "For each begin_nested() call, a corresponding rollback() or
commit() must be issued." and even from looking at the unit tests in
zope.sqlalchemy I cannot tell where the required commit() is issued per
savepoint. This may be worth investigating regardless of whether your
problem is solved. :-)

http://svn.zope.org/zope.sqlalchemy/trunk/src/zope/sqlalchemy/

On Wed, Nov 16, 2011 at 6:51 AM, Vlad K.  wrote:

>
> Yes it actually works if I use session.flush() instead of
> transaction.commit() within the subtransaction. So I guess
> transaction.commit() and transaction.abort() close the entire transaction
> unlike session.commit() which when matched against session.begin_nested()
> releases the savepoint but keeps outer transaction intact. Obviously I
> failed to try all combinations before when using savepoint to rollback
> would raise an error (because the transaction was finalized with
> transaction.commit()).
>
> Now looking back at all these tests performed and various combinations, it
> does all make sense and it was my wrong understanding that I could use
> savepoints as "real" subtransactions, ie. commit (to disc) everything
> between savepoint and its commit regardless of the "outer" transaction,
> although the other way around works: outer transaction can be committed if
> "inner" rolled back.
>
> I still don't know how I can manually release the savepoint using
> Transaction, or even if that is necessary at all? Does releasing
> savepoint_1 "return" its code back to the "outer" transaction, so that
> savepoint_2's failure would not rollback even savepoint_1?
>
>
> So basically this entire issue was caused by my lack of experience with
> savepoints and total misunderstanding of how the Transaction package works
> as opposed to using SQLA's session directly.
>
>
> .oO V Oo.
>
>
>
> On 11/16/2011 09:38 AM, Chris McDonough wrote:
>
>> On Tue, 2011-11-15 at 11:44 -0600, Michael Merickel wrote:
>>
>>> It's been hard to follow what has actually been tried, but I just
>>> wanted to point some stuff out about zope.sqlalchemy (the code for it
>>> is literally 1 small file and shouldn't be talked about with such a
>>> scary tone).
>>>
>>>
>>> When doing transaction.savepoint() it returns a savepoint object that
>>> calls "session.begin_nested()". and when you call rollback on that
>>> savepoint object it calls "session.rollback()". I think that is
>>> identical to SQLAlchemy's expected use. For example:
>>>
>>>
>>> sp = transaction.savepoint() # session.begin_nested()
>>> try:
>>> # do stuff
>>> except IntegrityError:
>>> sp.rollback() # session.rollback()
>>>
>>>
>>> # do more stuff
>>> transaction.commit() # or ideally don't commit and let pyramid_tm do
>>> that part for you
>>>
>> Vlad: it'd be useful to know whether this (savepoint.rollback()) doesn't
>> work for you (as opposed to using session.rollback()).
>>
>> - C
>>
>
> --
> You received this message because you are subscribed to the Google Groups
> "pylons-discuss" group.
> To post to this group, send email to 
> pylons-discuss@googlegroups.**com
> .
> To unsubscribe from this group, send email to pylons-discuss+unsubscribe@*
> *googlegroups.com .
> For more options, visit this group at http://groups.google.com/**
> group/pylons-discuss?hl=en
> .
>
>


-- 

Michael

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to pylons-discuss@googlegroups.com.
To unsubscribe from this group, send email to 
pylons-discuss+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en.



Re: Multiple transactions within request

2011-11-16 Thread Vlad K.


Yes it actually works if I use session.flush() instead of 
transaction.commit() within the subtransaction. So I guess 
transaction.commit() and transaction.abort() close the entire 
transaction unlike session.commit() which when matched against 
session.begin_nested() releases the savepoint but keeps outer 
transaction intact. Obviously I failed to try all combinations before 
when using savepoint to rollback would raise an error (because the 
transaction was finalized with transaction.commit()).


Now looking back at all these tests performed and various combinations, 
it does all make sense and it was my wrong understanding that I could 
use savepoints as "real" subtransactions, ie. commit (to disc) 
everything between savepoint and its commit regardless of the "outer" 
transaction, although the other way around works: outer transaction can 
be committed if "inner" rolled back.


I still don't know how I can manually release the savepoint using 
Transaction, or even if that is necessary at all? Does releasing 
savepoint_1 "return" its code back to the "outer" transaction, so that 
savepoint_2's failure would not rollback even savepoint_1?



So basically this entire issue was caused by my lack of experience with 
savepoints and total misunderstanding of how the Transaction package 
works as opposed to using SQLA's session directly.



.oO V Oo.


On 11/16/2011 09:38 AM, Chris McDonough wrote:

On Tue, 2011-11-15 at 11:44 -0600, Michael Merickel wrote:

It's been hard to follow what has actually been tried, but I just
wanted to point some stuff out about zope.sqlalchemy (the code for it
is literally 1 small file and shouldn't be talked about with such a
scary tone).


When doing transaction.savepoint() it returns a savepoint object that
calls "session.begin_nested()". and when you call rollback on that
savepoint object it calls "session.rollback()". I think that is
identical to SQLAlchemy's expected use. For example:


sp = transaction.savepoint() # session.begin_nested()
try:
 # do stuff
except IntegrityError:
 sp.rollback() # session.rollback()


# do more stuff
transaction.commit() # or ideally don't commit and let pyramid_tm do
that part for you

Vlad: it'd be useful to know whether this (savepoint.rollback()) doesn't
work for you (as opposed to using session.rollback()).

- C


--
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to pylons-discuss@googlegroups.com.
To unsubscribe from this group, send email to 
pylons-discuss+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en.



Re: Multiple transactions within request

2011-11-16 Thread Chris McDonough
On Tue, 2011-11-15 at 11:44 -0600, Michael Merickel wrote:
> It's been hard to follow what has actually been tried, but I just
> wanted to point some stuff out about zope.sqlalchemy (the code for it
> is literally 1 small file and shouldn't be talked about with such a
> scary tone).
> 
> 
> When doing transaction.savepoint() it returns a savepoint object that
> calls "session.begin_nested()". and when you call rollback on that
> savepoint object it calls "session.rollback()". I think that is
> identical to SQLAlchemy's expected use. For example:
> 
> 
> sp = transaction.savepoint() # session.begin_nested()
> try:
> # do stuff
> except IntegrityError:
> sp.rollback() # session.rollback()
> 
> 
> # do more stuff
> transaction.commit() # or ideally don't commit and let pyramid_tm do
> that part for you

Vlad: it'd be useful to know whether this (savepoint.rollback()) doesn't
work for you (as opposed to using session.rollback()).

- C


> 
> On Tue, Nov 15, 2011 at 9:19 AM, Michael Bayer
>  wrote:
> 
> On Nov 15, 2011, at 7:09 AM, Vlad K. wrote:
> 
> >
> >
> > Why I didn't think of this earlier? Transaction complains if
> you use session.commit() or session.begin_nested() directly,
> wants you to use transaction.commit() and
> transaction.savepoint() instead and it just didn't occur to me
> to try session.rollback() nevertheless (and in my mind
> transaction.abort() == session.rollback() which now I see is
> NOT the same), and trying savepoint.rollback() fails, I
> assumed session.rollback() was called by Transaction since the
> SQL debug output clearly shows savepoint rollback being
> emitted, so I went to search for another solution.
> >
> > Aside from me being silly for not trying this before (and it
> is even suggested by the InvalidRequestError!), it is a bit
> illogical to have to use transaction.savepoint() and then use
> session.rollback() instead of savepoint.rollback().
> 
> 
> 
> glad you figured this out.   Now we need to adjust
> zope.sqlalchemy's API and/or documentation so that the
> SAVEPOINT use case is made clear.I would think that since
> SAVEPOINTs can be per-connection,  perhaps zope.sqlalchemy
> would support begin_nested() on individual sessions...or maybe
> not.
> 
> 
> --
> You received this message because you are subscribed to the
> Google Groups "pylons-discuss" group.
> To post to this group, send email to
> pylons-discuss@googlegroups.com.
> To unsubscribe from this group, send email to pylons-discuss
> +unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/pylons-discuss?hl=en.
> 
> 
> 
> 
> 
> 
> -- 
> 
> Michael
> 
> 
> -- 
> You received this message because you are subscribed to the Google
> Groups "pylons-discuss" group.
> To post to this group, send email to pylons-discuss@googlegroups.com.
> To unsubscribe from this group, send email to pylons-discuss
> +unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/pylons-discuss?hl=en.


-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to pylons-discuss@googlegroups.com.
To unsubscribe from this group, send email to 
pylons-discuss+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en.