...

View Full Version : Reg Expression to Exclude the contents of a Single folder



Mayankode
12-09-2012, 12:21 AM
How can I exclude the contents of a folder using regular expressions.

Here is the code of the appspot app.yaml code.


handlers:
- url: /(.*\.(gif|png|jpg|ico|js|css))
static_files: \1
upload: (.*\.(gif|png|jpg|ico|js|css))

- url: /robots.txt
static_files: robots.txt
upload: robots.txt

- url: /favicon.ico
static_files: favicon.ico
upload: favicon.ico

- url: .*
script: main.py


error_handlers:
- file: default_error.html

- error_code: over_quota
file: over_quota.html
I want any "file not found" 404 error arising in the folder /Codebox to be applied by error_handlers at the bottom of the code. Since the code is read from top and all handlers have to come above the error_handlers, I want to modify the code of the handlers so that they exclude the contents of the folder /Codebox and so that any error arising from this folder will be taken care by the error_handlers.

Please suggest a regexpression to exclude the contents of a single named folder. Thank you.

Mayankode
12-09-2012, 01:14 PM
In other words Here is the directory list


Webroot
-dir_1
-dir_2
--subdir_a
--subdir_b
--subdir_c
...


What will be the regex for all files and folders including those in the webroot but except the contents of subdir_a

Please suggest.

007julien
12-09-2012, 02:26 PM
Try


url: .*(?!subdir_a)But I do not know if this lookahead negative assertion are autorized in this language which is not javascript !

Philip M
12-09-2012, 04:32 PM
Is this code Java? This is the JavaScript forum. Java and Javascript are entirely different programming languages, in spite of the confusingly similar names. Rather like Austria and Australia! Ask a mod to move this thread to the right forum.

AndrewGSW
12-09-2012, 05:09 PM
Is this code Java? This is the JavaScript forum. Java and Javascript are entirely different programming languages, in spite of the confusingly similar names. Rather like Austria and Australia! Ask a mod to move this thread to the right forum.
Java?! It's actually YAML; but this is, more specifically, a (Python) server-configuration file.

AndrewGSW
12-09-2012, 05:12 PM
@Mayancode
Sorry, mis-information deleted!

Mayankode
12-09-2012, 06:09 PM
@julien that is not working and you are right since it is not javascript regex.

@Phillip Thanks, I am aware of the old saying "as car is to carpet, java is to javascript". I just could not find a forum matching its language...

@Andy You are right, this is a python server configuration file... I was wrong in thinking the regex would be similar to javascript.. I have found the documentation here http://docs.python.org/2/library/re.html but I am still not able to make out a code which will let the errors arising in a specific folder to be handled by error_hander... All I want the code to be is to exclude folder /Codebox and its contents in the expressions used in the top part of the code which are handled by handlers

AndrewGSW
12-09-2012, 07:16 PM
- url: /dir_2/subdir_a
script: yourscript.py
# or perhaps

- url: /dir_2/subdir_a/.*
script: yourscript.py
would redirect (as I understand..) requests to this subfolder to a Python script named yourscript.py, which you would need to create. Reference (https://developers.google.com/appengine/docs/python/config/appconfig#Script_Handlers) I'm looking at.

error_handlers:
- file: default_error.html
.. would redirect any 404 (or other) errors to the page default_error.html. You might redirect to a .py or .php (but I assume Python is your server-side language?). The page that is redirected to could contain a script to provide different content according to the page requested.

But this is the extent of my understanding.. and I may be wrong :)

Added If you are looking to exclude a folder then 007julien has provided an example using a negative lookahead assertion. That is to say, "does not have this text". I do not know either, whether such a construct is acceptable within your configuration file.

Mayankode
12-10-2012, 12:54 AM
@Andy the script you suggested would work if I were having a python script (ex: errorhandling.py ) But I am not having any knowledge of python and so I would give up to adopt any alternative.

If only I could make sure non of the regex in the handlers referred to the particular folder /Codebox then I could assign error_handlers:
- file: default_error.html as suggested here https://developers.google.com/appengine/docs/python/config/appconfig#Custom_Error_Responses

AndrewGSW
12-10-2012, 01:19 AM
What do you want it to do when someone visits /Codebox ?

Mayankode
12-10-2012, 01:28 AM
If someone goes to /Codebox/xyz.html then it should show them the page but ANY other address combination inside the Codebox directory such as /Codebox/superman.html should take him to a predefined 404.html page in located in the root directory. Do you think it is possible in the configuration file ?

AndrewGSW
12-10-2012, 01:42 AM
Until you find a proper solution you could perhaps add this code to your default 404 page so that it redirects to your alternative 404:


if (document.referrer.indexOf('Codebox') + 1) {
location.href = "/someother404.html";
}
I do not know if your requirement is possible in the configuration file - never seen it before today :)

Mayankode
12-10-2012, 01:53 AM
:( I dont have the default 404page with me either... the 404 error is just a system generated cluttered python data ! I dont know editing the main.py file either... :( May be we should consider for an alternative...

AndrewGSW
12-10-2012, 02:14 AM
:( I dont have the default 404page with me either... the 404 error is just a system generated cluttered python data ! I dont know editing the main.py file either... :( May be we should consider for an alternative...
Can you not create your own 404 page? That is the whole idea after all..

I wouldn't know if main.py is a general page over which you have no control, or whether you are allowed to create or modify your own version of this (it may not even exist!?).

If you are able to track down your copy of this file (on your server's domain somewhere I assume) then I could have a gander at it. But I suspect that creating your own 404 page is the way to go.

[I thought "lookahead negative assertion" was quite witty, in a reciprocal way?! Maybe its just me :) ]

Mayankode
12-10-2012, 02:45 AM
Here is the code of main.py

and it sits in the root and is editable very well.. Please take a look, I am sure this is the key to our solution...

the filestructure is
Webroot
-index.html (my static homepage)
-gate.html (the static pop up )
-app.yaml
-index.yaml
-main.py
-...
-Codebox
--xyz.html (refered to as treasure)


The main.py code


import os
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template

class MainHandler(webapp.RequestHandler):
def get (self, q):
if q is None:
q = 'index.html'

path = os.path.join (os.path.dirname (__file__), q)
self.response.headers ['Content-Type'] = 'text/html'
self.response.out.write (template.render (path, {}))

def main ():
application = webapp.WSGIApplication ([('/(.*html)?', MainHandler)], debug=True)
util.run_wsgi_app (application)

if __name__ == '__main__':
main ()

AndrewGSW
12-10-2012, 03:07 AM
Sorry, can't help with that main.py - I think it requires knowledge of a google templating system. Although, a wild guess would be..


import os
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template

class MainHandler(webapp.RequestHandler):
def get (self, q):
if q is None:
q = 'youralternativepagehere.html'

path = os.path.join (os.path.dirname (__file__), q)
self.response.headers ['Content-Type'] = 'text/html'
self.response.out.write (template.render (path, {}))

def main ():
application = webapp.WSGIApplication ([('/(Codebox/*.*html)?', MainHandler)], debug=True)
util.run_wsgi_app (application)

if __name__ == '__main__':
main ()
.. but I wouldn't bank on it :)

Mayankode
12-10-2012, 07:05 AM
No Andy, it did not work... :( I will have to spend a lot of time in the documentation then...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum