<s> """<STR_LIT>""" <EOL> import copy <EOL> import csv <EOL> import json <EOL> import logging <EOL> import os <EOL> import shutil <EOL> import tempfile <EOL> from portalpy import TEXT_BASED_ITEM_TYPES , FILE_BASED_ITEM_TYPES , PortalError , unicode_to_ascii <EOL> ITEM_COPY_PROPERTIES = [ '<STR_LIT:title>' , '<STR_LIT:type>' , '<STR_LIT>' , '<STR_LIT:description>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:name>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:url>' , ] <EOL> GROUP_COPY_PROPERTIES = [ '<STR_LIT:title>' , '<STR_LIT:description>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] <EOL> GROUP_EXTRACT_PROPERTIES = [ '<STR_LIT:id>' ] + GROUP_COPY_PROPERTIES <EOL> clean_temp_files = True <EOL> _log = logging . getLogger ( __name__ ) <EOL> def copy_items ( items , source , target , target_user , target_folder = None , <EOL> relationships = None , work_dir = tempfile . gettempdir ( ) ) : <EOL> """<STR_LIT>""" <EOL> if not target . is_logged_in ( ) : <EOL> raise PortalError ( '<STR_LIT>' ) <EOL> target_folder_id = None <EOL> if target_folder : <EOL> target_folder_id = _get_or_create_folder ( target , target_user , <EOL> target_folder ) <EOL> copy_dir = tempfile . mkdtemp ( prefix = '<STR_LIT>' , <EOL> dir = unicode_to_ascii ( work_dir ) ) <EOL> try : <EOL> copied_items = _copy_items ( items , source , target , target_user , <EOL> target_folder_id , None , copy_dir ) <EOL> if relationships : <EOL> related_items = _copy_relationships ( copied_items , source , target , <EOL> target_user , target_folder_id , relationships , <EOL> copy_dir ) <EOL> copied_items . update ( related_items ) <EOL> finally : <EOL> if clean_temp_files : <EOL> shutil . rmtree ( copy_dir ) <EOL> return copied_items <EOL> def copy_user_contents ( source , source_user , target , target_user , ids = None , <EOL> relationships = None , work_dir = tempfile . gettempdir ( ) ) : <EOL> """<STR_LIT>""" <EOL> if not source . is_logged_in ( ) : <EOL> raise PortalError ( '<STR_LIT>' + '<STR_LIT>' ) <EOL> if not target . is_logged_in ( ) : <EOL> raise PortalError ( '<STR_LIT>' + '<STR_LIT>' ) <EOL> root_items , folders = source . user_contents ( source_user ) <EOL> copy_dir = tempfile . mkdtemp ( prefix = '<STR_LIT>' , <EOL> dir = unicode_to_ascii ( work_dir ) ) <EOL> try : <EOL> copied_items = _copy_items ( root_items , source , target , target_user , None , <EOL> ids , copy_dir ) <EOL> for folder_id , folder_title , items in folders : <EOL> target_folder_id = _get_or_create_folder ( target , target_user , folder_title ) <EOL> copied_folder_items = _copy_items ( items , source , target , target_user , <EOL> target_folder_id , ids , copy_dir ) <EOL> copied_items . update ( copied_folder_items ) <EOL> if relationships : <EOL> related_items = _copy_relationships ( copied_items , source , target , <EOL> target_user , None , relationships , copy_dir ) <EOL> copied_items . update ( related_items ) <EOL> finally : <EOL> if clean_temp_files : <EOL> shutil . rmtree ( copy_dir ) <EOL> return copied_items <EOL> def _copy_items ( items , source , target , target_user , target_folder_id , ids , <EOL> copy_dir ) : <EOL> copied_items = dict ( ) <EOL> for item in items : <EOL> itemid = item [ '<STR_LIT:id>' ] <EOL> if not ids or itemid in ids : <EOL> target_itemid = _copy_item ( item , source , target , target_user , <EOL> target_folder_id , None , copy_dir ) [ <NUM_LIT:0> ] <EOL> if target_itemid : <EOL> copied_items [ itemid ] = target_itemid <EOL> return copied_items <EOL> def _copy_item ( item , source , target , target_user , target_folder_id , <EOL> relationships , copy_dir ) : <EOL> itemid = item [ '<STR_LIT:id>' ] <EOL> item_dir = os . path . join ( copy_dir , itemid ) <EOL> os . makedirs ( item_dir ) <EOL> try : <EOL> target_item = _select_properties ( item , ITEM_COPY_PROPERTIES ) <EOL> data_file = None <EOL> if item [ '<STR_LIT:type>' ] in TEXT_BASED_ITEM_TYPES : <EOL> text = source . item_data ( itemid ) <EOL> if text and len ( text ) > <NUM_LIT:0> : <EOL> target_item [ '<STR_LIT:text>' ] = text <EOL> elif item [ '<STR_LIT:type>' ] in FILE_BASED_ITEM_TYPES : <EOL> data_file = source . item_datad ( itemid , item_dir , item . get ( '<STR_LIT:name>' ) ) <EOL> thumbnail_file = None <EOL> if '<STR_LIT>' in item : <EOL> thumbnail_file = source . item_thumbnaild ( <EOL> itemid , item_dir , unicode_to_ascii ( item [ '<STR_LIT>' ] ) ) <EOL> metadata_file = source . item_metadatad ( itemid , item_dir ) <EOL> target_itemid = target . add_item ( <EOL> target_item , data_file , thumbnail_file , metadata_file , <EOL> target_user , unicode_to_ascii ( target_folder_id ) ) <EOL> if target_itemid : <EOL> _log . info ( '<STR_LIT>' + itemid + '<STR_LIT>' <EOL> + '<STR_LIT>' + target_itemid + '<STR_LIT>' ) <EOL> copied_items = dict ( { itemid : target_itemid } ) <EOL> if relationships : <EOL> related_items = _copy_relationships ( copied_items , source , <EOL> target , target_user , target_folder_id , <EOL> relationships , copy_dir ) <EOL> copied_items . update ( related_items ) <EOL> return target_itemid , copied_items <EOL> else : <EOL> _log . warning ( '<STR_LIT>' + itemid + '<STR_LIT>' + '<STR_LIT>' ) <EOL> except IOError as e : <EOL> _log . warning ( '<STR_LIT>' + itemid + '<STR_LIT>' + '<STR_LIT:(>' + str ( e ) + '<STR_LIT:)>' ) <EOL> finally : <EOL> if clean_temp_files : <EOL> shutil . rmtree ( item_dir ) <EOL> return None , None <EOL> def _copy_relationships ( items , source , target , target_user , target_folder_id , <EOL> relationships , work_dir ) : <EOL> all_copied_rel_items = dict ( ) <EOL> for source_id , target_id in items . items ( ) : <EOL> related_items = source . related_items ( source_id , relationships ) <EOL> for source_rel_item , rel_type , rel_direction in related_items : <EOL> source_rel_id = source_rel_item [ '<STR_LIT:id>' ] <EOL> target_rel_id = items . get ( source_rel_id ) <EOL> if not target_rel_id : <EOL> target_rel_id , copied_rel_items = _copy_item ( source_rel_item , source , target , target_user , <EOL> target_folder_id , relationships , work_dir ) <EOL> if copied_rel_items : <EOL> all_copied_rel_items . update ( copied_rel_items ) <EOL> items . update ( copied_rel_items ) <EOL> if target_rel_id : <EOL> target . add_relationship ( target_user , target_id , target_rel_id , <EOL> rel_type ) <EOL> else : <EOL> _log . warning ( '<STR_LIT>' + source_rel_id + '<STR_LIT>' ) <EOL> return all_copied_rel_items <EOL> def _select_properties ( properties , property_names ) : <EOL> selected = dict ( ) <EOL> for property_name in property_names : <EOL> property_value = properties . get ( property_name ) <EOL> if property_value is not None : <EOL> selected [ property_name ] = unicode_to_ascii ( property_value ) <EOL> return selected <EOL> def _get_or_create_folder ( portal , owner , folder_title ) : <EOL> for folder in portal . folders ( owner ) : <EOL> if folder [ '<STR_LIT:title>' ] == folder_title : <EOL> return folder [ '<STR_LIT:id>' ] <EOL> new_folder = portal . create_folder ( owner , folder_title ) <EOL> if new_folder : <EOL> return new_folder [ '<STR_LIT:id>' ] <EOL> def copy_groups ( groups , source , target , target_owner = None , <EOL> work_dir = tempfile . gettempdir ( ) ) : <EOL> """<STR_LIT>""" <EOL> if not target . is_logged_in ( ) : <EOL> raise PortalError ( '<STR_LIT>' ) <EOL> copy_dir = tempfile . mkdtemp ( prefix = '<STR_LIT>' , dir = work_dir ) <EOL> copied_groups = dict ( ) <EOL> try : <EOL> for group in groups : <EOL> groupid = group [ '<STR_LIT:id>' ] <EOL> group_dir = os . path . join ( copy_dir , groupid ) <EOL> os . makedirs ( group_dir ) <EOL> target_group = _select_properties ( group , GROUP_COPY_PROPERTIES ) <EOL> if target_group [ '<STR_LIT>' ] == '<STR_LIT>' and not target . is_multitenant ( ) : <EOL> target_group [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> elif target_group [ '<STR_LIT>' ] == '<STR_LIT>' and not source . is_multitenant ( ) and target . is_multitenant ( ) and target . is_org ( ) : <EOL> target_group [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> thumbnail_file = None <EOL> if '<STR_LIT>' in group : <EOL> thumbnail_file = source . group_thumbnaild ( <EOL> groupid , group_dir , group [ '<STR_LIT>' ] ) <EOL> target_groupid = target . create_group ( target_group , thumbnail_file ) <EOL> if target_groupid : <EOL> target_username = target . logged_in_user ( ) [ '<STR_LIT:username>' ] <EOL> if target_owner and ( target_owner != target_username ) : <EOL> target . reassign_group ( target_groupid , target_owner ) <EOL> target . leave_group ( target_groupid ) <EOL> copied_groups [ groupid ] = target_groupid <EOL> _log . info ( '<STR_LIT>' + groupid + '<STR_LIT>' <EOL> + '<STR_LIT>' + target_groupid + '<STR_LIT>' ) <EOL> else : <EOL> _log . warning ( '<STR_LIT>' + groupid + '<STR_LIT>' + '<STR_LIT>' ) <EOL> if clean_temp_files : <EOL> shutil . rmtree ( group_dir ) <EOL> finally : <EOL> if clean_temp_files : <EOL> shutil . rmtree ( copy_dir ) <EOL> return copied_groups <EOL> class JSONSerializer ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , data = True , metadata = True , thumbnails = True , indent = None ) : <EOL> """<STR_LIT>""" <EOL> self . data = data <EOL> self . metadata = metadata <EOL> self . thumbnails = thumbnails <EOL> self . indent = indent <EOL> def serialize_groups ( self , groups , path , portal = None ) : <EOL> """<STR_LIT>""" <EOL> base_dir = os . path . abspath ( path ) <EOL> if not os . path . exists ( base_dir ) : <EOL> os . makedirs ( base_dir ) <EOL> elif not os . path . isdir ( base_dir ) : <EOL> base_dir = os . path . dirname ( base_dir ) <EOL> for group in groups : <EOL> group_dir = os . path . join ( base_dir , group [ '<STR_LIT:id>' ] ) <EOL> if not os . path . exists ( group_dir ) : <EOL> os . makedirs ( group_dir ) <EOL> if self . thumbnails : <EOL> if not portal : <EOL> raise PortalError ( '<STR_LIT>' + '<STR_LIT>' ) <EOL> thumbnail = group . get ( '<STR_LIT>' ) <EOL> if thumbnail : <EOL> portal . item_thumbnaild ( group [ '<STR_LIT:id>' ] , group_dir , thumbnail ) <EOL> self . to_file ( group , os . path . join ( group_dir , '<STR_LIT>' ) ) <EOL> def serialize_items ( self , items , path , portal = None ) : <EOL> """<STR_LIT>""" <EOL> base_dir = os . path . abspath ( path ) <EOL> if not os . path . exists ( base_dir ) : <EOL> os . makedirs ( base_dir ) <EOL> elif not os . path . isdir ( base_dir ) : <EOL> base_dir = os . path . dirname ( base_dir ) <EOL> for item in items : <EOL> item_dir = os . path . join ( base_dir , item [ '<STR_LIT:id>' ] ) <EOL> if not os . path . exists ( item_dir ) : <EOL> os . makedirs ( item_dir ) <EOL> if self . thumbnails : <EOL> if not portal : <EOL> raise PortalError ( '<STR_LIT>' + '<STR_LIT>' ) <EOL> thumbnail = item . get ( '<STR_LIT>' ) <EOL> if thumbnail : <EOL> portal . item_thumbnaild ( item [ '<STR_LIT:id>' ] , item_dir , thumbnail ) <EOL> if self . data : <EOL> if not portal : <EOL> raise PortalError ( '<STR_LIT>' + '<STR_LIT>' ) <EOL> if item [ '<STR_LIT:type>' ] in TEXT_BASED_ITEM_TYPES : <EOL> text = portal . item_data ( item [ '<STR_LIT:id>' ] ) <EOL> if text and len ( text ) > <NUM_LIT:0> : <EOL> item [ '<STR_LIT:text>' ] = text <EOL> elif item [ '<STR_LIT:type>' ] in FILE_BASED_ITEM_TYPES : <EOL> data_dir = os . path . join ( item_dir , '<STR_LIT:data>' ) <EOL> if not os . path . exists ( data_dir ) : <EOL> os . makedirs ( data_dir ) <EOL> portal . item_datad ( item [ '<STR_LIT:id>' ] , data_dir , item . get ( '<STR_LIT:name>' ) ) <EOL> if self . metadata : <EOL> if not portal : <EOL> raise PortalError ( '<STR_LIT>' + '<STR_LIT>' ) <EOL> portal . item_metadatad ( item [ '<STR_LIT:id>' ] , item_dir ) <EOL> self . to_file ( item , os . path . join ( item_dir , '<STR_LIT>' ) ) <EOL> def to_file ( self , data , path ) : <EOL> with open ( path , '<STR_LIT:w>' ) as outfile : <EOL> json . dump ( data , outfile , indent = self . indent ) <EOL> class JSONDeserializer ( object ) : <EOL> """<STR_LIT>""" <EOL> def deserialize_groups ( self , path ) : <EOL> """<STR_LIT>""" <EOL> groups = [ ] <EOL> group_reader = csv . DictReader ( open ( path , "<STR_LIT:rb>" ) ) <EOL> for group in group_reader : <EOL> groups . append ( group ) <EOL> return groups <EOL> def deserialize_items ( self , path ) : <EOL> """<STR_LIT>""" <EOL> items = [ ] <EOL> base_dir = os . path . abspath ( path ) <EOL> if not os . path . exists ( base_dir ) : <EOL> _log . warn ( '<STR_LIT>' + base_dir ) <EOL> return <EOL> item_dirs = os . listdir ( path ) <EOL> for item_dir in item_dirs : <EOL> item_path = os . path . join ( base_dir , item_dir , '<STR_LIT>' ) <EOL> item = self . from_file ( item_path ) <EOL> thumbnail_path = None <EOL> thumbnail = item . get ( '<STR_LIT>' ) <EOL> if thumbnail : <EOL> thumbnail_filename = os . path . basename ( thumbnail ) <EOL> thumbnail_path = os . path . join ( base_dir , item_dir , thumbnail_filename ) <EOL> data_path = None <EOL> data_dir = os . path . join ( base_dir , item_dir , '<STR_LIT:data>' ) <EOL> if os . path . exists ( data_dir ) : <EOL> data_filename = item . get ( '<STR_LIT:name>' ) <EOL> if not data_filename : <EOL> data_filename = '<STR_LIT:data>' <EOL> data_path = os . path . join ( data_dir , data_filename ) <EOL> metadata_path = os . path . join ( base_dir , item_dir , '<STR_LIT>' ) <EOL> if not os . path . exists ( metadata_path ) : <EOL> metadata_path = None <EOL> items . append ( ( item , thumbnail_path , data_path , metadata_path ) ) <EOL> return items <EOL> def deserialize_item ( self , path ) : <EOL> """<STR_LIT>""" <EOL> if not os . path . exists ( path ) : <EOL> _log . warn ( '<STR_LIT>' + path ) <EOL> return <EOL> item_path = os . path . join ( path , '<STR_LIT>' ) <EOL> item = self . from_file ( item_path ) <EOL> thumbnail_path = None <EOL> thumbnail = item . get ( '<STR_LIT>' ) <EOL> if thumbnail : <EOL> thumbnail_filename = os . path . basename ( thumbnail ) <EOL> thumbnail_path = os . path . join ( path , thumbnail_filename ) <EOL> data_path = None <EOL> data_dir = os . path . join ( path , '<STR_LIT:data>' ) <EOL> if os . path . exists ( data_dir ) : <EOL> data_filename = item . get ( '<STR_LIT:name>' ) <EOL> if not data_filename : <EOL> data_filename = '<STR_LIT:data>' <EOL> data_path = os . path . join ( data_dir , data_filename ) <EOL> metadata_path = os . path . join ( path , '<STR_LIT>' ) <EOL> if not os . path . exists ( metadata_path ) : <EOL> metadata_path = None <EOL> return ( item , thumbnail_path , data_path , metadata_path ) <EOL> def from_file ( self , path ) : <EOL> with open ( path , '<STR_LIT:r>' ) as infile : <EOL> return json . load ( infile ) <EOL> class CSVSerializer ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , data = True , metadata = True , thumbnails = True ) : <EOL> """<STR_LIT>""" <EOL> self . data = data <EOL> self . metadata = metadata <EOL> self . thumbnails = thumbnails <EOL> def serialize_groups ( self , groups , path , portal = None ) : <EOL> """<STR_LIT>""" <EOL> groups_copy = copy . deepcopy ( groups ) <EOL> field_names = GROUP_EXTRACT_PROPERTIES <EOL> if self . thumbnails : <EOL> if not portal : <EOL> raise PortalError ( '<STR_LIT>' + '<STR_LIT>' ) <EOL> field_names . append ( '<STR_LIT>' ) <EOL> base_dir = os . path . dirname ( path ) <EOL> for i , group in enumerate ( groups ) : <EOL> if '<STR_LIT>' in group : <EOL> group_dir = os . path . join ( base_dir , group [ '<STR_LIT:id>' ] ) <EOL> thumbnail_path = portal . group_thumbnaild ( <EOL> group [ '<STR_LIT:id>' ] , group_dir , group [ '<STR_LIT>' ] ) <EOL> groups_copy [ i ] [ '<STR_LIT>' ] = os . path . relpath ( <EOL> thumbnail_path , base_dir ) <EOL> group_writer = csv . DictWriter ( open ( path , "<STR_LIT:wb>" ) , field_names ) <EOL> group_writer . writeheader ( ) <EOL> group_writer . writerows ( groups_copy ) <EOL> class CSVDeserializer ( object ) : <EOL> """<STR_LIT>""" <EOL> def deserialize_groups ( self , path ) : <EOL> """<STR_LIT>""" <EOL> groups = [ ] <EOL> if not os . path . isfile ( path ) : <EOL> raise PortalError ( '<STR_LIT>' + path ) <EOL> group_reader = csv . DictReader ( open ( path , "<STR_LIT:rb>" ) ) <EOL> for group in group_reader : <EOL> groups . append ( group ) <EOL> return groups <EOL> def deserialize_users ( self , path ) : <EOL> """<STR_LIT>""" <EOL> users = [ ] <EOL> if not os . path . isfile ( path ) : <EOL> raise PortalError ( '<STR_LIT>' + path ) <EOL> user_reader = csv . DictReader ( open ( path , "<STR_LIT:rb>" ) ) <EOL> for user in user_reader : <EOL> users . append ( user ) <EOL> return users <EOL> _known_serializers = { '<STR_LIT>' : CSVSerializer , '<STR_LIT>' : JSONSerializer } <EOL> _known_deserializers = { '<STR_LIT>' : CSVDeserializer , '<STR_LIT>' : JSONDeserializer } <EOL> def _select_deserializer ( f , cls , ** kw ) : <EOL> if not cls : <EOL> cls = _known_deserializers . get ( f ) <EOL> if not cls : <EOL> raise PortalError ( '<STR_LIT>' + f + '<STR_LIT>' ) <EOL> return cls ( ** kw ) <EOL> def _select_serializer ( f , cls , ** kw ) : <EOL> if not cls : <EOL> cls = _known_serializers . get ( f ) <EOL> if not cls : <EOL> raise PortalError ( '<STR_LIT>' + f + '<STR_LIT>' ) <EOL> return cls ( ** kw ) <EOL> def load_users ( portal , path , f = '<STR_LIT>' , cls = None , ** kw ) : <EOL> """<STR_LIT>""" <EOL> if portal . is_multitenant ( ) : <EOL> raise PortalError ( '<STR_LIT>' <EOL> + '<STR_LIT>' ) <EOL> deserializer = _select_deserializer ( f , cls , ** kw ) <EOL> users = deserializer . deserialize_users ( path ) <EOL> for user in users : <EOL> for property in list ( user . keys ( ) ) : <EOL> if user [ property ] is None : <EOL> del user [ property ] <EOL> portal . signup ( user [ '<STR_LIT:username>' ] , user [ '<STR_LIT:password>' ] , <EOL> user [ '<STR_LIT>' ] , user . get ( '<STR_LIT:email>' ) ) <EOL> return users <EOL> def load_groups ( portal , path , f = '<STR_LIT>' , cls = None , ** kw ) : <EOL> """<STR_LIT>""" <EOL> groups , source_ids = [ ] , [ ] <EOL> deserializer = _select_deserializer ( f , cls , ** kw ) <EOL> dgroups = deserializer . deserialize_groups ( path ) <EOL> for dgroup in dgroups : <EOL> source_id = dgroup . pop ( '<STR_LIT:id>' , None ) <EOL> thumbnail = dgroup . pop ( '<STR_LIT>' , None ) <EOL> owner = dgroup . pop ( '<STR_LIT>' , portal . logged_in_user ( ) [ '<STR_LIT:username>' ] ) <EOL> for property in list ( dgroup . keys ( ) ) : <EOL> if dgroup [ property ] is None : <EOL> del dgroup [ property ] <EOL> id = portal . create_group ( dgroup , thumbnail ) <EOL> if id : <EOL> group = dict ( id = id , owner = owner , ** dgroup ) <EOL> groups . append ( group ) <EOL> source_ids . append ( source_id ) <EOL> return groups , source_ids <EOL> def load_items ( portal , path , f = '<STR_LIT>' , cls = None , ** kw ) : <EOL> """<STR_LIT>""" <EOL> items , source_ids = [ ] , [ ] <EOL> deserializer = _select_deserializer ( f , cls , ** kw ) <EOL> ditems = deserializer . deserialize_items ( path ) <EOL> for ditem_tuple in ditems : <EOL> ditem = ditem_tuple [ <NUM_LIT:0> ] <EOL> thumbnail = ditem_tuple [ <NUM_LIT:1> ] <EOL> data = ditem_tuple [ <NUM_LIT:2> ] <EOL> metadata = ditem_tuple [ <NUM_LIT:3> ] <EOL> source_id = ditem . pop ( '<STR_LIT:id>' , None ) <EOL> ditem . pop ( '<STR_LIT>' ) <EOL> ditem . pop ( '<STR_LIT>' ) <EOL> for property in list ( ditem . keys ( ) ) : <EOL> if ditem [ property ] is None : <EOL> del ditem [ property ] <EOL> id = portal . add_item ( ditem , data , thumbnail , metadata ) <EOL> if id : <EOL> item = dict ( id = id , owner = portal . logged_in_user ( ) [ '<STR_LIT:username>' ] , ** ditem ) <EOL> items . append ( item ) <EOL> source_ids . append ( source_id ) <EOL> return items , source_ids <EOL> def load_item ( portal , path , overwrite_id = None , f = '<STR_LIT>' , cls = None , ** kw ) : <EOL> """<STR_LIT>""" <EOL> deserializer = _select_deserializer ( f , cls , ** kw ) <EOL> ditem_tuple = deserializer . deserialize_item ( path ) <EOL> ditem = ditem_tuple [ <NUM_LIT:0> ] <EOL> thumbnail = ditem_tuple [ <NUM_LIT:1> ] <EOL> data = ditem_tuple [ <NUM_LIT:2> ] <EOL> metadata = ditem_tuple [ <NUM_LIT:3> ] <EOL> source_id = ditem . pop ( '<STR_LIT:id>' , None ) <EOL> ditem . pop ( '<STR_LIT>' ) <EOL> ditem . pop ( '<STR_LIT>' ) <EOL> for property in list ( ditem . keys ( ) ) : <EOL> if ditem [ property ] is None : <EOL> del ditem [ property ] <EOL> if not overwrite_id : <EOL> id = portal . add_item ( ditem , data , thumbnail , metadata ) <EOL> else : <EOL> portal . update_item ( overwrite_id , ditem , data , metadata , thumbnail ) <EOL> id = overwrite_id <EOL> if id : <EOL> item = dict ( id = id , owner = portal . logged_in_user ( ) [ '<STR_LIT:username>' ] , ** ditem ) <EOL> return item , source_id <EOL> def save_groups ( portal , groups , path , f = '<STR_LIT>' , cls = None , ** kw ) : <EOL> """<STR_LIT>""" <EOL> serializer = _select_serializer ( f , cls , ** kw ) <EOL> serializer . serialize_groups ( groups , path , portal ) <EOL> def save_items ( portal , items , path , f = '<STR_LIT>' , cls = None , ** kw ) : <EOL> """<STR_LIT>""" <EOL> serializer = _select_serializer ( f , cls , ** kw ) <EOL> serializer . serialize_items ( items , path , portal ) </s>
