Package s3 :: Module s3xml :: Class S3XML
[frames] | no frames]

Class S3XML

source code

     object --+    
              |    
s3codec.S3Codec --+
                  |
                 S3XML

XML toolkit for S3XRC

Instance Methods
 
__init__(self)
Constructor
source code
 
parse(self, source)
Parse an XML source into an element tree
source code
 
transform(self, tree, stylesheet_path, **args)
Transform an element tree with XSLT
source code
 
envelope(self, tree, stylesheet_path, **args)
Wraps XML contents into an XML envelope like:
source code
 
tree(self, elements, root=None, domain=None, url=None, start=None, limit=None, results=None, maxbounds=False)
Builds a S3XML tree from a list of elements
source code
 
export_uid(self, uid)
Exports UIDs with domain prefix
source code
 
import_uid(self, uid)
Imports UIDs with domain prefixes
source code
 
represent(self, table, f, v)
Get the representation of a field value
source code
 
rmap(self, table, record, fields)
Generates a reference map for a record
source code
 
add_references(self, element, rmap, show_ids=False, lazy=None)
Adds <reference> elements to a <resource>
source code
 
latlon(self, rmap)
Add lat/lon to location references
source code
 
gis_encode(self, resource, record, element, location_data=None)
GIS-encodes the master resource so that it can be transformed into a mappable format.
source code
 
resource(self, parent, table, record, alias=None, fields=None, url=None, lazy=None, llrepr=None, postprocess=None)
Creates a <resource> element from a record
source code
 
get_options(self, table, fields=None, show_uids=False, hierarchy=False)
Get options of option fields in a table as <select>s
source code
 
get_fields(self, prefix, name, parent=None, meta=False, options=False, references=False, labels=False)
Get fields in a table as <fields> element
source code
 
get_struct(self, prefix, name, alias=None, parent=None, meta=False, options=True, references=False)
Get the table structure as XML tree
source code

Inherited from s3codec.S3Codec: decode, encode

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Class Methods
 
select_resources(cls, tree, tablename)
Selects resources from an element tree
source code
 
components(cls, element, names=None)
Selects component elements in a resource element
source code
 
record(cls, table, element, original=None, files=None, skip=None, postprocess=None)
Creates a record (Storage) from a <resource> element and validates it
source code
 
get_field_options(cls, table, fieldname, parent=None, show_uids=False, hierarchy=False)
Get options of a field as <select>
source code
 
json2tree(cls, source, format=None)
Converts JSON into an element tree
source code
 
tree2json(cls, tree, pretty_print=False, native=False, as_dict=False)
Converts an element tree into JSON
source code
 
xls2tree(cls, source, resourcename=None, extra_data=None, hashtags=None, sheet=None, rows=None, cols=None, fields=None, header_row=True)
Convert a table in an XLS (MS Excel) sheet into an ElementTree, consisting of <table name="format">, <row> and <col field="fieldname"> elements (see: csv2tree).
source code
 
csv2tree(cls, source, resourcename=None, extra_data=None, hashtags=None, delimiter=",", quotechar='"')
Convert a table-form CSV source into an element tree, consisting of <table name="format">, <row> and <col field="fieldname"> elements.
source code

Inherited from s3codec.S3Codec: get_codec, xml_decode, xml_encode

Static Methods
 
tostring(tree, xml_declaration=True, pretty_print=True)
Convert an element tree into XML as string
source code
 
represent_user(user_id) source code
 
represent_role(role_id) source code
 
collect_errors(job)
Collect errors from an error tree
source code

Inherited from s3codec.S3Codec: crud_string, json_message

Class Variables
  namespace = "sahana"
  CACHE_TTL = 20
  UID = "uuid"
  MCI = "mci"
  DELETED = "deleted"
  REPLACEDBY = "deleted_rb"
  APPROVED = "approved"
  CTIME = "created_on"
  CUSER = "created_by"
  MTIME = "modified_on"
  MUSER = "modified_by"
  OGROUP = "owned_by_group"
  OUSER = "owned_by_user"
  Lat = "lat"
  Lon = "lon"
  IGNORE_FIELDS = ["id", "deleted_fk", "approved_by", "realm_ent...
  FIELDS_TO_ATTRIBUTES = ["id", "admin", CUSER, MUSER, OGROUP, O...
  ATTRIBUTES_TO_FIELDS = ["admin", CUSER, MUSER, OGROUP, OUSER, ...
  TAG = Storage(col= "col", contents= "contents", data= "data", ...
  ATTRIBUTE = Storage(alias= "alias", attributes= "attributes", ...
  ACTION = Storage(create= "create", read= "read", update= "upda...
  PREFIX = Storage(attribute= "@", options= "$o", reference= "$k...

Inherited from s3codec.S3Codec: CODECS, PY2XML, XML2PY, indices

Properties

Inherited from object: __class__

Method Details

__init__(self)
(Constructor)

source code 

Constructor

Overrides: object.__init__

parse(self, source)

source code 

Parse an XML source into an element tree

Parameters:
  • source - the XML source - can be a file-like object, a filename or a HTTP/HTTPS/FTP URL

transform(self, tree, stylesheet_path, **args)

source code 

Transform an element tree with XSLT

Parameters:
  • tree - the element tree
  • stylesheet_path - pathname of the XSLT stylesheet
  • args - dict of arguments to pass to the stylesheet

envelope(self, tree, stylesheet_path, **args)

source code 

Wraps XML contents into an XML envelope like:

<contents>
    <object>
        <description>Description Text</description>
        <-- tree gets copied in here -->
    </object>
    <!-- can be multiple <object> elements -->
</contents>

@param tree: the element tree or list of trees to wrap, can also
             be a list of tuples (tree, description) in order to
             provide a description for each XML content object
@param stylesheet_path: the path to the XSLT transformation stylesheet
                        to transform the envelope (e.g. into EDXL-DE)
@param args: stylesheet parameters

tostring(tree, xml_declaration=True, pretty_print=True)
Static Method

source code 

Convert an element tree into XML as string

Parameters:
  • tree - the element tree
  • xml_declaration - add an XML declaration to the output
  • pretty_print - provide pretty formatted output

tree(self, elements, root=None, domain=None, url=None, start=None, limit=None, results=None, maxbounds=False)

source code 

Builds a S3XML tree from a list of elements

Parameters:
  • elements - list of <resource> elements
  • root - the root element to link the tree to
  • domain - name of the current domain
  • url - url of the request
  • start - the start record (in server-side pagination)
  • limit - the page size (in server-side pagination)
  • results - number of total available results
  • maxbounds - include maximum Geo-boundaries (lat/lon min/max)

export_uid(self, uid)

source code 

Exports UIDs with domain prefix

Parameters:
  • uid - the UID

import_uid(self, uid)

source code 

Imports UIDs with domain prefixes

Parameters:
  • uid - the UID

represent(self, table, f, v)

source code 

Get the representation of a field value

Parameters:
  • table - the database table
  • f - the field name
  • v - the value

represent_user(user_id)
Static Method

source code 

represent_role(role_id)
Static Method

source code 

rmap(self, table, record, fields)

source code 

Generates a reference map for a record

Parameters:
  • table - the database table
  • record - the record
  • fields - list of reference field names in this table

add_references(self, element, rmap, show_ids=False, lazy=None)

source code 

Adds <reference> elements to a <resource>

Parameters:
  • element - the <resource> element
  • rmap - the reference map for the corresponding record
  • show_ids - insert the record ID as attribute in references

latlon(self, rmap)

source code 

Add lat/lon to location references

Parameters:
  • rmap - the reference map of the tree

gis_encode(self, resource, record, element, location_data=None)

source code 

GIS-encodes the master resource so that it can be transformed into a mappable format.

Parameters:
  • resource - the referencing resource
  • record - the particular record
  • element - the XML element
  • location_data - dictionary of location data from gis.get_location_data()

To Do: Support multiple locations per master resource (e.g. event_event.location)

resource(self, parent, table, record, alias=None, fields=None, url=None, lazy=None, llrepr=None, postprocess=None)

source code 

Creates a <resource> element from a record

Parameters:
  • parent - the parent element in the document tree
  • table - the database table
  • record - the record
  • alias - the resource alias (for disambiguation of components)
  • fields - list of field names to include
  • url - URL of the record
  • lazy - lazy representation map
  • llrepr - lookup list representation method
  • postprocess - post-process hook (xml_post_render)

select_resources(cls, tree, tablename)
Class Method

source code 

Selects resources from an element tree

Parameters:
  • tree - the element tree
  • tablename - table name to search for

components(cls, element, names=None)
Class Method

source code 

Selects component elements in a resource element

record(cls, table, element, original=None, files=None, skip=None, postprocess=None)
Class Method

source code 

Creates a record (Storage) from a <resource> element and validates it

Parameters:
  • table - the database table
  • element - the element
  • original - the original record
  • files - dict of attached upload files
  • postprocess - post-process hook (xml_post_parse)
  • skip - fields to skip

get_field_options(cls, table, fieldname, parent=None, show_uids=False, hierarchy=False)
Class Method

source code 

Get options of a field as <select>

Parameters:
  • table - the Table
  • fieldname - the Field name
  • parent - the parent element in the tree
  • show_uids - include UUIDs in foreign key options
  • hierarchy - include parent ID in foreign key options (if the lookup table is hierarchical)

get_options(self, table, fields=None, show_uids=False, hierarchy=False)

source code 

Get options of option fields in a table as <select>s

Parameters:
  • prefix - the application prefix
  • name - the resource name (without prefix)
  • fields - optional list of fieldnames
  • show_uids - include UIDs in foreign key options
  • hierarchy - include parent IDs in foreign key options (if the lookup table is hierarchical)

get_fields(self, prefix, name, parent=None, meta=False, options=False, references=False, labels=False)

source code 

Get fields in a table as <fields> element

Parameters:
  • prefix - the application prefix
  • name - the resource name (without prefix)
  • parent - the parent element to append the tree to
  • options - include option lists in option fields
  • references - include option lists even in reference fields

get_struct(self, prefix, name, alias=None, parent=None, meta=False, options=True, references=False)

source code 

Get the table structure as XML tree

Parameters:
  • prefix - the application prefix
  • name - the tablename (without prefix)
  • parent - the parent element to append the tree to
  • options - include option lists in option fields
  • references - include option lists even in reference fields
Raises:
  • AttributeError - in case the table doesn't exist

json2tree(cls, source, format=None)
Class Method

source code 

Converts JSON into an element tree

Parameters:
  • source - the JSON source
  • format - name of the XML root element

tree2json(cls, tree, pretty_print=False, native=False, as_dict=False)
Class Method

source code 

Converts an element tree into JSON

Parameters:
  • tree - the element tree
  • pretty_print - indent and insert line breaks into the JSON string to make it human-readable (useful for debug)
  • native - tree is S3XML
  • as_dict - return a JSON-serializable object instead of a string, useful for embedding the data in other structures
Returns:
a JSON string (with as_dict=False), or a JSON-serializable object (with as_dict=True)

collect_errors(job)
Static Method

source code 

Collect errors from an error tree

Parameters:
  • job - the import job, resource or error tree as Element

xls2tree(cls, source, resourcename=None, extra_data=None, hashtags=None, sheet=None, rows=None, cols=None, fields=None, header_row=True)
Class Method

source code 

Convert a table in an XLS (MS Excel) sheet into an ElementTree, consisting of <table name="format">, <row> and <col field="fieldname"> elements (see: csv2tree).

The returned ElementTree can be imported using S3CSV stylesheets (through S3Resource.import_xml()).

Parameters:
  • source - the XLS source (stream, or XLRD book, or None if sheet is an open XLRD sheet)
  • resourcename - the resource name
  • extra_data - dict of extra cols {key:value} to add to each row
  • hashtags - dict of hashtags for extra cols {key:hashtag}
  • sheet - sheet name or index, or an open XLRD sheet (open work sheet overrides source)
  • rows - Rows range, integer (length from 0) or tuple (start, length) - or a tuple (start,) to read all available rows after start
  • cols - Columns range, like "rows"
  • fields - Field map, a dict {index: fieldname} where index is the column index counted from the first column within the specified range, e.g. cols=(2,7), fields={1:"MyField"} means column 3 in the sheet is "MyField", the field map can be omitted to read the field names from the sheet (see "header_row")
  • header_row - the first row contains column headers (if fields is None, they will be used as field names in the output - otherwise they will be ignored)
Returns:
an etree.ElementTree representing the table

csv2tree(cls, source, resourcename=None, extra_data=None, hashtags=None, delimiter=",", quotechar='"')
Class Method

source code 

Convert a table-form CSV source into an element tree, consisting of <table name="format">, <row> and <col field="fieldname"> elements.

Parameters:
  • source - the source (file-like object)
  • resourcename - the resource name
  • extra_data - dict of extra cols {key:value} to add to each row
  • hashtags - dict of hashtags for extra cols {key:hashtag}
  • delimiter - delimiter for values
  • quotechar - quotation character

To Do: add a character encoding parameter to skip the guessing


Class Variable Details

namespace

Value:
"sahana"

CACHE_TTL

Value:
20

UID

Value:
"uuid"

MCI

Value:
"mci"

DELETED

Value:
"deleted"

REPLACEDBY

Value:
"deleted_rb"

APPROVED

Value:
"approved"

CTIME

Value:
"created_on"

CUSER

Value:
"created_by"

MTIME

Value:
"modified_on"

MUSER

Value:
"modified_by"

OGROUP

Value:
"owned_by_group"

OUSER

Value:
"owned_by_user"

Lat

Value:
"lat"

Lon

Value:
"lon"

IGNORE_FIELDS

Value:
["id", "deleted_fk", "approved_by", "realm_entity",]

FIELDS_TO_ATTRIBUTES

Value:
["id", "admin", CUSER, MUSER, OGROUP, OUSER, CTIME, MTIME, UID, MCI, D\
ELETED,]

ATTRIBUTES_TO_FIELDS

Value:
["admin", CUSER, MUSER, OGROUP, OUSER, CTIME, MTIME, MCI, DELETED, APP\
ROVED,]

TAG

Value:
Storage(col= "col", contents= "contents", data= "data", description= "\
description", field= "field", fields= "fields", item= "item", list= "l\
ist", meta= "meta", object= "object", option= "option", options= "opti\
ons", reference= "reference", resource= "resource", root= "s3xml", row\
= "row", select= "select", table= "table",)

ATTRIBUTE

Value:
Storage(alias= "alias", attributes= "attributes", comment= "comment", \
domain= "domain", error= "error", field= "field", filename= "filename"\
, has_options= "has_options", hashtag= "hashtag", id= "id", label= "la\
bel", lat= "lat", latmin= "latmin", latmax= "latmax", limit= "limit", \
llrepr= "llrepr", lon= "lon", lonmin= "lonmin", lonmax= "lonmax", mark\
er= "marker", marker_url= "marker_url", marker_height= "marker_height"\
, marker_width= "marker_width", name= "name", parent= "parent", ref= "\
ref", replaced_by= "replaced_by", resource= "resource", results= "resu\
...

ACTION

Value:
Storage(create= "create", read= "read", update= "update", delete= "del\
ete",)

PREFIX

Value:
Storage(attribute= "@", options= "$o", reference= "$k", resource= "$",\
 text= "$",)