def to_xml_by_good(filename, goods_list): target = open(filename, 'w+') # Write XML Header target.write(utility.get_xml_header() + "\n") target.write("<Good_List>\n") # Sets of attributes products = [] # Group by Good for n in range(0, len(goods_list)): row = goods_list[n] country = special_chars.xml_safe( str(row['Country_Name']).strip(), sp_chars) good = special_chars.xml_safe(str(row['Good_Name']).strip(), sp_chars) good_sector = special_chars.xml_safe( str(row['Good_Sector']).strip(), sp_chars) if (good not in products): target.write("\t<Good>\n" + "\t\t<Good_Name>" + good + "</Good_Name>" + "\n") target.write("\t\t<Good_Sector>" + good_sector + "</Good_Sector>" + "\n") countryset = get_country_tuples_for_good(goods_list, good) target.write("\t\t<Countries>\n") for count in range(0, len(countryset)): target.write("\t\t\t<Country>\n") target.write( "\t\t\t\t" + "<Country_Name>" + special_chars.xml_safe( str(countryset[count]['Country_Name']), sp_chars) + "</Country_Name>\n") target.write("\t\t\t\t" + "<Country_ISO2>" + str(countryset[count]['Country_ISO2']) + "</Country_ISO2>\n") target.write("\t\t\t\t" + "<Country_ISO3>" + str(countryset[count]['Country_ISO3']) + "</Country_ISO3>\n") target.write( "\t\t\t\t" + "<Country_Region>" + special_chars.xml_safe( str(countryset[count]['Country_Region']), sp_chars) + "</Country_Region>\n") target.write("\t\t\t\t" + "<Child_Labor>" + str(countryset[count]['Child_Labor']) + "</Child_Labor>\n") target.write("\t\t\t\t" + "<Forced_Labor>" + str(countryset[count]['Forced_Labor']) + "</Forced_Labor>\n") target.write("\t\t\t\t" + "<Forced_Child_Labor>" + str(countryset[count]['Forced_Child_Labor']) + "</Forced_Child_Labor>\n") target.write("\t\t\t</Country>\n") target.write("\t\t</Countries>\n") target.write("\t</Good>\n") products.append(good) target.write("</Good_List>\n") target.close() return
def to_xml_by_country(filename, goods_list): target = open(filename, 'w+') target.write(utility.get_xml_header() + "\n") target.write("<Country_List>\n") countries = [] # Group by Country for n in range(0, len(goods_list)): row = goods_list[n] country = special_chars.xml_safe( str(row['Country_Name']).strip(), sp_chars) iso2 = str(row['Country_ISO2']) iso3 = str(row['Country_ISO3']) region = special_chars.xml_safe(str(row['Country_Region']), sp_chars) good = str(row['Good_Name']).strip() if (country not in countries): target.write("\t<Country>\n" + "\t\t<Country_Name>" + country + "</Country_Name>" + "\n") target.write("\t\t<Country_ISO2>" + iso2 + "</Country_ISO2>" + "\n") target.write("\t\t<Country_ISO3>" + iso3 + "</Country_ISO3>" + "\n") target.write("\t\t<Country_Region>" + region + "</Country_Region>" + "\n") good_tuples = get_good_tuples_for_country(goods_list, country) target.write("\t\t<Goods>\n") for c in range(0, len(good_tuples)): target.write("\t\t\t<Good>\n") target.write("\t\t\t\t\t" + "<Good_Name>" + special_chars.xml_safe( str(good_tuples[c]['Good_Name']), sp_chars) + "</Good_Name>\n") target.write( "\t\t\t\t\t" + "<Good_Sector>" + special_chars.xml_safe( str(good_tuples[c]['Good_Sector']), sp_chars) + "</Good_Sector>\n") target.write("\t\t\t\t\t" + "<Child_Labor>" + str(good_tuples[c]['Child_Labor']) + "</Child_Labor>\n") target.write("\t\t\t\t\t" + "<Forced_Labor>" + str(good_tuples[c]['Forced_Labor']) + "</Forced_Labor>\n") target.write("\t\t\t\t\t" + "<Forced_Child_Labor>" + str(good_tuples[c]['Forced_Child_Labor']) + "</Forced_Child_Labor>\n") target.write("\t\t\t</Good>\n") target.write("\t\t</Goods>\n") target.write("\t</Country>\n") countries.append(country) target.write("</Country_List>\n") target.close() return
def to_xml(filename, data): target = open(filename, 'w+') target.write(utility.get_xml_header()+"\n") target.write("<Goods>\n") for good in data: target.write("\t<Good>\n") target.write("\t\t<Good_Name>"+special_chars.xml_safe(good['Good_Name'],sp_chars)+"</Good_Name>\n") target.write("\t\t<Good_Sector>"+special_chars.xml_safe(good['Good_Sector'],sp_chars)+"</Good_Sector>\n") target.write("\t</Good>\n") target.write("</Goods>\n") target.close() return
def write_record(target, cr, count): ckeys = cr.keys() for n in range(0, len(cr)): kv = cr[ckeys[n]] if (type(kv) == list): target.write( utility.tabs(count) + utility.create_starting_xml_tag(ckeys[n]) + utility.get_newline() ) if (ckeys[n] != "Goods") and (ckeys[n] != "Suggested_Actions"): for l in kv: write_record(target, l, count+1) if (ckeys[n] == "Goods"): for good in cr[ckeys[n]]: target.write( utility.tabs(count+1) + "<Good>" + utility.get_newline() ) target.write( utility.tabs(count+2) + utility.create_starting_xml_tag("Good_Name")) target.write( good["Good_Name"]) target.write( utility.create_closing_xml_tag("Good_Name") + utility.get_newline() ) target.write( utility.tabs(count+2) + utility.create_starting_xml_tag("Child_Labor") ) target.write( good["Child_Labor"] ) target.write( utility.create_closing_xml_tag("Child_Labor") + utility.get_newline() ) target.write( utility.tabs(count+2) + utility.create_starting_xml_tag("Forced_Labor") ) target.write( good["Forced_Labor"] ) target.write( utility.create_closing_xml_tag("Forced_Labor") + utility.get_newline() ) target.write( utility.tabs(count+2) + utility.create_starting_xml_tag("Forced_Child_Labor") ) target.write( good["Forced_Child_Labor"] ) target.write( utility.create_closing_xml_tag("Forced_Child_Labor") + utility.get_newline() ) target.write( utility.tabs(count+1) + "</Good>" + utility.get_newline() ) if (ckeys[n] == "Suggested_Actions"): sga = cr[ckeys[n]] for action in sga: stripped = unicode(utility.unicode_to_str(action['Area']), 'ascii', 'ignore') xmltag = sanitize(utility.unicode_to_str(stripped)) target.write( utility.tabs(count+1) + utility.create_starting_xml_tag(xmltag) + utility.get_newline() ) for act in action['Actions']: target.write(utility.tabs(count+2) + utility.create_starting_xml_tag("Action") + utility.get_newline() ) aname = special_chars.xml_safe(utility.unicode_to_str(act['Name']), sp_chars) ayears = special_chars.xml_safe(utility.unicode_to_str(act['Years']), sp_chars) target.write(utility.tabs(count+3) + utility.create_starting_xml_tag("Name") + aname + utility.create_closing_xml_tag("Name") + utility.get_newline() ) target.write(utility.tabs(count+3) + utility.create_starting_xml_tag("Years") + ayears + utility.create_closing_xml_tag("Years") + utility.get_newline() ) target.write(utility.tabs(count+2) + utility.create_closing_xml_tag("Action") + utility.get_newline() ) target.write( utility.tabs(count+1) + utility.create_closing_xml_tag(xmltag) + utility.get_newline() ) target.write( utility.tabs(count) + utility.create_closing_xml_tag(ckeys[n]) + utility.get_newline() ) else: keyname = utility.to_str(ckeys[n]) start = utility.create_starting_xml_tag(keyname) val = special_chars.xml_safe(utility.to_str(cr[keyname]), sp_chars) end = utility.create_closing_xml_tag(keyname) target.write( utility.tabs(count) + start + val + end + utility.get_newline() ) return
def to_xml(filename, data): target = open(filename, 'w+') target.write(utility.get_xml_header()+"\n") target.write("<Countries>\n") for country in data: target.write("\t<Country>\n") target.write("\t\t<Country_Name>"+special_chars.xml_safe(country['Country_Name'],sp_chars)+"</Country_Name>\n") target.write("\t\t<Country_ISO2>"+str(country['Country_ISO2'])+"</Country_ISO2>\n") target.write("\t\t<Country_ISO3>"+str(country['Country_ISO3'])+"</Country_ISO3>\n") target.write("\t\t<Country_Region>"+special_chars.xml_safe(country['Country_Region'],sp_chars)+"</Country_Region>\n") target.write("\t</Country>\n") target.write("</Countries>\n") target.close() return
def write_record(target, md): target.write(utility.tabs(2)+"<Country_Name>") target.write(special_chars.xml_safe(md['Country_Name'],sp_chars)) target.write("</Country_Name>"+utility.get_newline()) target.write(utility.tabs(2)+"<Country_ISO2>") target.write(str(md['Country_ISO2'])) target.write("</Country_ISO2>"+utility.get_newline()) target.write(utility.tabs(2)+"<Country_ISO3>") target.write(str(md['Country_ISO3'])) target.write("</Country_ISO3>"+utility.get_newline()) target.write(utility.tabs(2)+"<Country_Region>") target.write(special_chars.xml_safe(str(md['Country_Region']),sp_chars)) target.write("</Country_Region>"+utility.get_newline()) return
def write_record(target, md): target.write(utility.tabs(2) + "<Country_Name>") target.write(special_chars.xml_safe(md['Country_Name'], sp_chars)) target.write("</Country_Name>" + utility.get_newline()) target.write(utility.tabs(2) + "<Country_ISO2>") target.write(str(md['Country_ISO2'])) target.write("</Country_ISO2>" + utility.get_newline()) target.write(utility.tabs(2) + "<Country_ISO3>") target.write(str(md['Country_ISO3'])) target.write("</Country_ISO3>" + utility.get_newline()) target.write(utility.tabs(2) + "<Country_Region>") target.write(special_chars.xml_safe(str(md['Country_Region']), sp_chars)) target.write("</Country_Region>" + utility.get_newline()) return
def write_rest(target, country_record, count): mkeys = country_record.keys() for n in range(5, len(mkeys)): currentkey = mkeys[n] opentext = utility.tabs(count) + "<" + currentkey + ">" valuetext = special_chars.xml_safe(str(country_record[currentkey]), sp_chars) closetext = "</" + currentkey + ">" + utility.get_newline() target.write(opentext + valuetext + closetext) return
def write_front(target, country_record, count): target.write( utility.tabs(count) + "<Country_Name>" + special_chars.xml_safe(country_record["Country_Name"], sp_chars) + "</Country_Name>" + utility.get_newline()) target.write( utility.tabs(count) + "<Country_ISO2>" + country_record["Country_ISO2"] + "</Country_ISO2>" + utility.get_newline()) target.write( utility.tabs(count) + "<Country_ISO3>" + country_record["Country_ISO3"] + "</Country_ISO3>" + utility.get_newline()) target.write( utility.tabs(count) + "<Country_Region>" + special_chars.xml_safe(country_record["Country_Region"], sp_chars) + "</Country_Region>" + utility.get_newline()) target.write( utility.tabs(count) + "<Assessment>" + country_record["Assessment"] + "</Assessment>" + utility.get_newline()) return
def to_xml(filename, data): target = open(filename, 'w+') target.write(utility.get_xml_header() + "\n") target.write("<Countries>\n") for country in data: target.write("\t<Country>\n") target.write( "\t\t<Country_Name>" + special_chars.xml_safe(country['Country_Name'], sp_chars) + "</Country_Name>\n") target.write("\t\t<Country_ISO2>" + str(country['Country_ISO2']) + "</Country_ISO2>\n") target.write("\t\t<Country_ISO3>" + str(country['Country_ISO3']) + "</Country_ISO3>\n") target.write( "\t\t<Country_Region>" + special_chars.xml_safe(country['Country_Region'], sp_chars) + "</Country_Region>\n") target.write("\t</Country>\n") target.write("</Countries>\n") target.close() return
def to_xml_by_good(filename, goods_list): target = open(filename, 'w+') # Write XML Header target.write(utility.get_xml_header()+"\n") target.write("<Good_List>\n") # Sets of attributes products = [] # Group by Good for n in range(0, len(goods_list)): row = goods_list[n] country = special_chars.xml_safe(str(row['Country_Name']).strip(),sp_chars) good = special_chars.xml_safe(str(row['Good_Name']).strip(),sp_chars) good_sector = special_chars.xml_safe(str(row['Good_Sector']).strip(),sp_chars) if (good not in products): target.write("\t<Good>\n"+"\t\t<Good_Name>"+good+"</Good_Name>"+"\n") target.write("\t\t<Good_Sector>"+good_sector+"</Good_Sector>"+"\n") countryset = get_country_tuples_for_good(goods_list, good) target.write("\t\t<Countries>\n") for count in range(0, len(countryset)): target.write("\t\t\t<Country>\n") target.write("\t\t\t\t"+"<Country_Name>"+special_chars.xml_safe(str(countryset[count]['Country_Name']),sp_chars)+"</Country_Name>\n") target.write("\t\t\t\t"+"<Country_ISO2>"+str(countryset[count]['Country_ISO2'])+"</Country_ISO2>\n") target.write("\t\t\t\t"+"<Country_ISO3>"+str(countryset[count]['Country_ISO3'])+"</Country_ISO3>\n") target.write("\t\t\t\t"+"<Country_Region>"+special_chars.xml_safe(str(countryset[count]['Country_Region']),sp_chars)+"</Country_Region>\n") target.write("\t\t\t\t"+"<Child_Labor>"+str(countryset[count]['Child_Labor'])+"</Child_Labor>\n") target.write("\t\t\t\t"+"<Forced_Labor>"+str(countryset[count]['Forced_Labor'])+"</Forced_Labor>\n") target.write("\t\t\t\t"+"<Forced_Child_Labor>"+str(countryset[count]['Forced_Child_Labor'])+"</Forced_Child_Labor>\n") target.write("\t\t\t</Country>\n") target.write("\t\t</Countries>\n") target.write("\t</Good>\n") products.append(good) target.write("</Good_List>\n") target.close() return
def to_xml_by_country(filename, goods_list): target = open(filename, 'w+') target.write(utility.get_xml_header()+"\n") target.write("<Country_List>\n") countries = [] # Group by Country for n in range(0, len(goods_list)): row = goods_list[n] country = special_chars.xml_safe(str(row['Country_Name']).strip(), sp_chars) iso2 = str(row['Country_ISO2']) iso3 = str(row['Country_ISO3']) region = special_chars.xml_safe(str(row['Country_Region']), sp_chars) good = str(row['Good_Name']).strip() if (country not in countries): target.write("\t<Country>\n"+"\t\t<Country_Name>"+country+"</Country_Name>"+"\n") target.write("\t\t<Country_ISO2>"+iso2+"</Country_ISO2>"+"\n") target.write("\t\t<Country_ISO3>"+iso3+"</Country_ISO3>"+"\n") target.write("\t\t<Country_Region>"+region+"</Country_Region>"+"\n") good_tuples = get_good_tuples_for_country(goods_list, country) target.write("\t\t<Goods>\n") for c in range(0, len(good_tuples)): target.write("\t\t\t<Good>\n") target.write("\t\t\t\t\t"+"<Good_Name>"+special_chars.xml_safe(str(good_tuples[c]['Good_Name']),sp_chars)+"</Good_Name>\n") target.write("\t\t\t\t\t"+"<Good_Sector>"+special_chars.xml_safe(str(good_tuples[c]['Good_Sector']),sp_chars)+"</Good_Sector>\n") target.write("\t\t\t\t\t"+"<Child_Labor>"+str(good_tuples[c]['Child_Labor'])+"</Child_Labor>\n") target.write("\t\t\t\t\t"+"<Forced_Labor>"+str(good_tuples[c]['Forced_Labor'])+"</Forced_Labor>\n") target.write("\t\t\t\t\t"+"<Forced_Child_Labor>"+str(good_tuples[c]['Forced_Child_Labor'])+"</Forced_Child_Labor>\n") target.write("\t\t\t</Good>\n") target.write("\t\t</Goods>\n") target.write("\t</Country>\n") countries.append(country) target.write("</Country_List>\n") target.close() return
def write_record(target, cr, count): ckeys = cr.keys() for n in range(0, len(cr)): kv = cr[ckeys[n]] if (type(kv) == list): target.write( utility.tabs(count) + utility.create_starting_xml_tag(ckeys[n]) + utility.get_newline()) count += 1 for l in kv: this_key_group = (ckeys[n])[:(len(ckeys[n]) - 1)] if len(kv) > 0: this_key_group = this_key_group.replace("ie", "y") #print this_key_group target.write( utility.tabs(count) + utility.create_starting_xml_tag(this_key_group) + utility.get_newline()) write_record(target, l, count + 1) if len(kv) > 0: target.write( utility.tabs(count) + utility.create_closing_xml_tag(this_key_group) + utility.get_newline()) target.write( utility.tabs(count - 1) + utility.create_closing_xml_tag(ckeys[n]) + utility.get_newline()) else: keyname = utility.to_str(ckeys[n]) start = utility.create_starting_xml_tag(keyname) val = special_chars.xml_safe(utility.to_str(cr[keyname]), sp_chars) end = utility.create_closing_xml_tag(keyname) target.write( utility.tabs(count) + start + val + end + utility.get_newline()) return
def write_record(target, cr, count): ckeys = cr.keys() for n in range(0, len(cr)): kv = cr[ckeys[n]] if (type(kv) == list): target.write( utility.tabs(count) + utility.create_starting_xml_tag(ckeys[n]) + utility.get_newline() ) count += 1 for l in kv: this_key_group = (ckeys[n])[:(len(ckeys[n])-1)] if len(kv) > 0: this_key_group = this_key_group.replace("ie", "y") #print this_key_group target.write( utility.tabs(count) + utility.create_starting_xml_tag(this_key_group) + utility.get_newline()) write_record(target, l, count+1) if len(kv) > 0: target.write( utility.tabs(count) + utility.create_closing_xml_tag(this_key_group) + utility.get_newline()) target.write( utility.tabs(count-1) + utility.create_closing_xml_tag(ckeys[n]) + utility.get_newline() ) else: keyname = utility.to_str(ckeys[n]) start = utility.create_starting_xml_tag(keyname) val = special_chars.xml_safe(utility.to_str(cr[keyname]), sp_chars) end = utility.create_closing_xml_tag(keyname) target.write( utility.tabs(count) + start + val + end + utility.get_newline() ) return
def write_record(target, cr, count): ckeys = cr.keys() for n in range(0, len(cr)): kv = cr[ckeys[n]] if (type(kv) == list): target.write( utility.tabs(count) + utility.create_starting_xml_tag(ckeys[n]) + utility.get_newline()) if (ckeys[n] != "Goods") and (ckeys[n] != "Suggested_Actions"): for l in kv: write_record(target, l, count + 1) if (ckeys[n] == "Goods"): for good in cr[ckeys[n]]: target.write( utility.tabs(count + 1) + "<Good>" + utility.get_newline()) target.write( utility.tabs(count + 2) + utility.create_starting_xml_tag("Good_Name")) target.write(good["Good_Name"]) target.write( utility.create_closing_xml_tag("Good_Name") + utility.get_newline()) target.write( utility.tabs(count + 2) + utility.create_starting_xml_tag("Child_Labor")) target.write(good["Child_Labor"]) target.write( utility.create_closing_xml_tag("Child_Labor") + utility.get_newline()) target.write( utility.tabs(count + 2) + utility.create_starting_xml_tag("Forced_Labor")) target.write(good["Forced_Labor"]) target.write( utility.create_closing_xml_tag("Forced_Labor") + utility.get_newline()) target.write( utility.tabs(count + 2) + utility.create_starting_xml_tag("Forced_Child_Labor")) target.write(good["Forced_Child_Labor"]) target.write( utility.create_closing_xml_tag("Forced_Child_Labor") + utility.get_newline()) target.write( utility.tabs(count + 1) + "</Good>" + utility.get_newline()) if (ckeys[n] == "Suggested_Actions"): sga = cr[ckeys[n]] for action in sga: stripped = unicode(utility.unicode_to_str(action['Area']), 'ascii', 'ignore') xmltag = sanitize(utility.unicode_to_str(stripped)) target.write( utility.tabs(count + 1) + utility.create_starting_xml_tag(xmltag) + utility.get_newline()) for act in action['Actions']: target.write( utility.tabs(count + 2) + utility.create_starting_xml_tag("Action") + utility.get_newline()) aname = special_chars.xml_safe( utility.unicode_to_str(act['Name']), sp_chars) ayears = special_chars.xml_safe( utility.unicode_to_str(act['Years']), sp_chars) target.write( utility.tabs(count + 3) + utility.create_starting_xml_tag("Name") + aname + utility.create_closing_xml_tag("Name") + utility.get_newline()) target.write( utility.tabs(count + 3) + utility.create_starting_xml_tag("Years") + ayears + utility.create_closing_xml_tag("Years") + utility.get_newline()) target.write( utility.tabs(count + 2) + utility.create_closing_xml_tag("Action") + utility.get_newline()) target.write( utility.tabs(count + 1) + utility.create_closing_xml_tag(xmltag) + utility.get_newline()) target.write( utility.tabs(count) + utility.create_closing_xml_tag(ckeys[n]) + utility.get_newline()) else: keyname = utility.to_str(ckeys[n]) start = utility.create_starting_xml_tag(keyname) val = special_chars.xml_safe(utility.to_str(cr[keyname]), sp_chars) end = utility.create_closing_xml_tag(keyname) target.write( utility.tabs(count) + start + val + end + utility.get_newline()) return