Changes between Initial Version and Version 1 of S3/DataImportCLI


Ignore:
Timestamp:
01/31/12 07:48:41 (13 years ago)
Author:
Dominic König
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • S3/DataImportCLI

    v1 v1  
     1[[TOC]]
     2= CSV Data Import from the Web2py Shell =
     3
     4== Importing Data ==
     5
     6It is possible to manually import CSV files from the web2py shell (CLI = command line interpreter, a python shell with the web2py environment).
     7
     8Start the web2py CLI:
     9{{{
     10python web2py.py -S eden -M
     11}}}
     12
     13Override authorization:
     14{{{
     15auth.override = True
     16}}}
     17
     18Create a resource:
     19{{{
     20resource = s3mgr.define_resource("project", "activity")
     21}}}
     22
     23Import data:
     24{{{
     25success = resource.import_xml(open("filename.csv", "rb"),                   # CSV format requires file handle, XML accepts filename
     26                              format="csv",                                 # "xml", "json" or "csv"
     27                              stylesheet="activity.xsl",                    # filename (with path) is sufficient
     28                              extra_data=dict(Organisation="Example.org"),  # extra columns to be added to each row
     29                              files={"image.png":open("image.png", "rb")),  # file attachments, filenames are the keys of the dict
     30                              ignore_errors=True)                           # skip invalid records (False rolls back the import on error)
     31}}}
     32
     33See errors (error message and error tree are returned in any case, even with ignore_errors=True):
     34{{{
     35print success                 # prints a JSON message with the error message and the JSONified error tree
     36print resource.error          # prints the error message
     37
     38print s3mgr.xml.tostring(resource.error_tree, pretty_print=True) # prints the error tree as XML
     39}}}
     40
     41== Tools to develop and debug XSLT stylesheets ==
     42
     43If you want to test your XSLT stylesheet, you can also use:
     44{{{
     45python static/scripts/tools/csv2xml.py mycsvfile.csv mystylesheet.xsl
     46}}}
     47
     48This converts the CSV file into XML, transforms it with the specified stylesheet and prints the result to the standard output.
     49
     50If you redirect the output into a file, you can also use this script to produce S3XML sources from CSV files (e.g. in order to test web services):
     51{{{
     52python static/scripts/tools/csv2xml.py mycsvfile.csv mystylesheet.xsl > myxmlfile.xml
     53}}}
     54
     55If you want to see the XML before transformation, just omit the stylesheet parameter:
     56{{{
     57python static/scripts/tools/csv2xml.py mycsvfile.csv
     58}}}
     59