Advertisement
s243a

config-filt-test2 bottle python

Nov 5th, 2018
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.32 KB | None | 0 0
  1. import bottle
  2. from bottle import route, run, Bottle
  3. import sys, os, re, urllib
  4. #fcpHost = "127.0.0.1"
  5. #import fcp
  6. #node = fcp.FCPNode(host=fcpHost, verbosity=fcp.DETAIL)
  7.  
  8. app = Bottle()
  9.  
  10. #m1="'"
  11.  
  12.  
  13. def delim_filter(config):
  14.     bottle.debug(True)
  15.     print("Entering delim_filter")
  16.     ''' Matches a comma separated list of numbers. '''
  17.     aconfig = config or "%20"
  18.     print ("aconfig=" + aconfig)
  19.     delimiter = urllib.unquote(aconfig).decode('utf8')
  20.     print("delimiter=" + delimiter)  
  21.     regexp = r'(?!{a_delim})+({a_delim}(?!{a_delim})+)*'.format(a_delim=re.escape(delimiter))
  22.     print("regexp="+regexp)
  23.     def to_python(match):
  24.         print("Converting Match")
  25.         print("Math=" + match)
  26.         ms = map(urllib.unquote(aconfig).decode,match.split())
  27.         print( "ms=" + ms )
  28.         return ms
  29.  
  30.     def to_url(astr):
  31.         print("Converting to URL")
  32.         print ("astr=" + astr)
  33.         astr2 = delimiter.join(map(urllib.unquote(aconfig).decode,astr))
  34.         print("astr2="+astr2)
  35.         print  astr2
  36.         return astr2
  37.  
  38.     return regexp, to_python, to_url
  39.  
  40. app.router.add_filter('delim', delim_filter)
  41.  
  42. def split_urlArgs(url,delim=" ",
  43.                   decode='All',
  44.                   xfm_de=lambda a: urllib.unquote(a).decode('utf8'),
  45.                   xfm_split=lambda a, b: re.split(a,b), #Typically a is a regular expression and b a string
  46.                   fn_regexp = lambda a: r'(?!{a_delim})+({a_delim}(?!{a_delim})+)*'.format(a_delim=a) ):
  47.     bottle.debug(True)
  48.     ''' Matches a comma separated list of numbers. '''
  49.     print("delimiter=" + delim)  
  50.     if decode == "Args":
  51.         ms = xfm_split(fn_regexp(a),url)
  52.     elif decode == "All":
  53.         url2 = xfm_de(url)    #Decode the URL
  54.         delim2= xfm_de(delim) #Decode the delimitor
  55.         ms = xfm_split(fn_regexp(delim2),url)
  56.     elif decode == "None":
  57.         ms = xfm_split(fn_regexp(delim),url) #SPlit the URL based on a regular expression.
  58.            
  59.     print( "ms=" + "["+",".join(ms) +"]" )    
  60.     return ms
  61.  
  62. def combine_urlArgs(args,delim=" ",
  63.                     encode='All',
  64.                     xfm_encode=lambda a: urllib.quote(a, safe=''),
  65.                     xfm_join=lambda a, b: a.join(b)): #Typically a is the delimiter and b is a list
  66.     print("Converting to URL")
  67.     print ("astr=" + args)
  68.     if decode == "Args":
  69.         astr2 = xfm_join(delim,(map(lambda a: xfm_encode,args)))
  70.     elif decode == "All":
  71.         astr2 = urllib.unquote(xfm_join(delim,args))
  72.     elif decode=="None":
  73.         astr2 = xfm_join(delim,args)      
  74.     print("astr2="+astr2)
  75.     return astr2    
  76.  
  77.  
  78. #@app.route('/<key>/%09/<cmd>/<args:delim:%09>') # %09 == tab
  79. #@app.route('/<key>/%20/<cmd>/<args:delim:%20>') # %20 == ' '
  80. #@app.route('/<key>/ /<cmd>/<args:delim: >') # %20 == ' '
  81. #@app.route('/<key>/%3B/<cmd>/<args:delim:%3B>') # %3B == ';'
  82. #@app.route('/<key>/;/<cmd>/<args:delim:;>') # %3B == ';'
  83. #@app.route('/<key>/%2C/<cmd>/<args:delim:%2C>') # %2C == ','
  84. #@app.route('/<key>/,/<cmd>/<args:delim:,>') # %2C == ','
  85. @app.route('/<key>/<sep>/<cmd>/<args:path>') # %2C == ','
  86. def mystery(key,cmd,args,sep=""):
  87.     bottle.debug(True)
  88.     print("engering mystery")
  89.     print("sep=" + sep)
  90.     if len(sep) > 0 : # True if seperator is a wild card
  91.         args = split_urlArgs(args,sep)
  92.     if key == '1234': # %2C == ','
  93.         print(key)
  94.         print(cmd)
  95.         print(args)
  96.         #delimiter = urllib.unquote(aconfig).decode('utf8')
  97.         #print(delimiter)            
  98.         #os.system("IFS=<a_dilim>".format(a_dilim=dilimiter))
  99.         #os.system("cmd" + "delimiter" + delimiter.join(args))
  100.  
  101.         return ('key=' + key + '<br>' +
  102.              'sep=' + sep + '<br>' +
  103.              'cmd=' + cmd + '<br>' +
  104.              'args='   + "[" + ",".join(args)+"]")
  105. #@app.route('/USK@<hashKey:nocomma>,<ecryptionKey:nocomma>,<encryption:nocomma>/<metaData:path>')
  106. #def hello(hashKey,ecryptionKey,encryption,metaData):
  107. #    
  108. #    mimetype, val1 = node.get(uri)
  109. #    return ('hashKey=' + hashKey + '<br>' +
  110. #             'ecryptionKey=' + ecryptionKey + '<br>' +
  111. #             'encryption=' + encryption + '<br>' +
  112. #             'metaData='   + metaData)
  113. #            
  114. #Regular eressions use a seperate filter; https://bottlepy.org/docs/dev/routing.html
  115. app.run(host='localhost', port=8082, debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement