Simon, this is helpful.  Thank you.






On Friday, December 27, 2013 8:24:27 PM UTC-8, Simon Ashley wrote:
>
>
>    1. We just do it in the controller as a function . Try an .insert but 
>    .bulk_insert should be fine for individual records.
>    2. You may need a path to find the file.
>
> Following code is cut, pasted and sanitised from a working controller.
> (for illustration only)
>
> ## initiates getting files from a remote server
> def GET():
>     getfile('_000000_S_BTRI')
>     getfile('_ST0000_S_ELST')
>
>
> ## reads remote files and writes to a folder on the local server
> def getfile(f3):
>     import datetime
>     from datetime import date, timedelta
>     import urllib2
>     import contextlib
>     import requests
>     import time
>     f0 = 'http://mcs...../getfile.aspx?filename='
>     dr = 15 #target range
>
>     today = datetime.date.today()
>     for x in range(dr):
>         start = today-timedelta(days=x)
>         f1 = start.strftime('%Y%m%d')
>         path = os.path.join(request.folder, 'data//')
>         f4 = '.xml'
>         file1 = f0 + f1 + f3 + f4
>         try:
>             u = urllib2.urlopen(file1)
>         except urllib2.HTTPError, e:
>             print str(e)
>         else:
>             if not 'alert' in u.read(100):
>                 rf = requests.get(file1)
>                 wf = open(path + f1 + f3 + f4, 'w')
>                 for line in rf:
>                     newline = line
>                     wf.write(newline)
>                 wf.close()
>
>
> # scans folder and writes db records of files found
> def update_PROCESS():
>     import os
>     path = os.path.join(request.folder, 'data\\')
>     p1 = {}
>     d1 = {}
>     d2 = {}
>     for files in os.listdir(path):
>         if files.endswith(".xml"):
>             p1 = path + files
>             dd1 = {'FILE': p1}
>             d1.update(dd1)
>             dd2 = {'PREFIX': p1[-8:-4]}
>             d1.update(dd2)
>             d1.update({'UPDATED':0})
>             d1.update({'RETRIEVED':datetime.now()})
>             db1.PROCESSING.bulk_insert([d1])
>     db.commit()
>
>
> # processes unprocessed files
> def PROCESS():
>     q = db1(db1.PROCESSING.UPDATED == 0).select()
>     if q:
>         for row in q:
>             f1 = row.FILE
>             if row.PREFIX == 'ASUM':
>                 try:
>                     attend(f1)
>                     d1 = db1.PROCESSING.update_or_insert((
> db1.PROCESSING.id == row.id), UPDATED = 1, PROCESSED=datetime.now())
>                 except:
>                     d1 = db1.PROCESSING.update_or_insert((
> db1.PROCESSING.id == row.id), UPDATED = 2)
>                 db1.commit()
>
>
> # reads file, create dict, performs bulk_insert
> def atten(f1):
>     from xml.etree import ElementTree
>     with open(f1, 'rt') as f:
>         try:
>             tree = ElementTree.parse(f)
>         except:
>             pass
>         else:
>             dh = {}
>             dd = {}
>
>             def onlyascii(char):
>                 if ord(char) < 32 or ord(char) > 127: return ''
>                 else: return char
>
>             for node in tree.iter():
>                 if node.tag == 'ATTEN':
>                     dh = node.attrib
>
>                 if node.tag == 'RECORD':
>                     dd = node.attrib
>                     remap = 
> {'DATE':'RECORD_DATE','TOTAL':'RECORD_TOTAL','PUBLIC':'RECORD_PUBLIC','MEMS':'RECORD_MEMS','COSE':'RECORD_COSE'}
>                     dd = dict((remap[key], value) for (key, value) in 
> dd.items())
>                     dd.update(dh)
>                     db1.ATTEN.bulk_insert([dd])
>                     dd = {}
>             db1.commit()
>
>
>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to