Changes between Initial Version and Version 1 of S3/DataImportCLI

01/31/12 07:48:41 (10 years ago)
Dominic König



  • S3/DataImportCLI

    v1 v1  
     2= CSV Data Import from the Web2py Shell =
     4== Importing Data ==
     6It is possible to manually import CSV files from the web2py shell (CLI = command line interpreter, a python shell with the web2py environment).
     8Start the web2py CLI:
     10python -S eden -M
     13Override authorization:
     15auth.override = True
     18Create a resource:
     20resource = s3mgr.define_resource("project", "activity")
     23Import data:
     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=""),  # 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)
     33See errors (error message and error tree are returned in any case, even with ignore_errors=True):
     35print success                 # prints a JSON message with the error message and the JSONified error tree
     36print resource.error          # prints the error message
     38print s3mgr.xml.tostring(resource.error_tree, pretty_print=True) # prints the error tree as XML
     41== Tools to develop and debug XSLT stylesheets ==
     43If you want to test your XSLT stylesheet, you can also use:
     45python static/scripts/tools/ mycsvfile.csv mystylesheet.xsl
     48This converts the CSV file into XML, transforms it with the specified stylesheet and prints the result to the standard output.
     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):
     52python static/scripts/tools/ mycsvfile.csv mystylesheet.xsl > myxmlfile.xml
     55If you want to see the XML before transformation, just omit the stylesheet parameter:
     57python static/scripts/tools/ mycsvfile.csv