mirror of
				https://github.com/aclindsa/moneygo.git
				synced 2025-11-03 18:13:27 -05:00 
			
		
		
		
	scripts/gen_security_list.py: Support python 2
This is to address issue #1
This commit is contained in:
		@@ -2,7 +2,21 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import csv
 | 
					import csv
 | 
				
			||||||
from xml.dom import minidom
 | 
					from xml.dom import minidom
 | 
				
			||||||
from urllib import request
 | 
					import sys
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					if sys.version_info[0] < 3:
 | 
				
			||||||
 | 
					    from urllib2 import urlopen
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # Allow writing utf-8 to stdout
 | 
				
			||||||
 | 
					    import codecs
 | 
				
			||||||
 | 
					    UTF8Writer = codecs.getwriter('utf8')
 | 
				
			||||||
 | 
					    sys.stdout = UTF8Writer(sys.stdout)
 | 
				
			||||||
 | 
					else:
 | 
				
			||||||
 | 
					    from urllib.request import urlopen
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # This is absent, but also unneeded in python3, so just return the string
 | 
				
			||||||
 | 
					    def unicode(s, encoding):
 | 
				
			||||||
 | 
					        return s
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Security(object):
 | 
					class Security(object):
 | 
				
			||||||
    def __init__(self, name, description, number, _type, precision):
 | 
					    def __init__(self, name, description, number, _type, precision):
 | 
				
			||||||
@@ -11,14 +25,18 @@ class Security(object):
 | 
				
			|||||||
        self.number = number
 | 
					        self.number = number
 | 
				
			||||||
        self.type = _type
 | 
					        self.type = _type
 | 
				
			||||||
        self.precision = precision
 | 
					        self.precision = precision
 | 
				
			||||||
    def __str__(self):
 | 
					    def unicode(self):
 | 
				
			||||||
        return """\tSecurity{
 | 
					        s = """\tSecurity{
 | 
				
			||||||
\t\tName: \"%s\",
 | 
					\t\tName: \"%s\",
 | 
				
			||||||
\t\tDescription: \"%s\",
 | 
					\t\tDescription: \"%s\",
 | 
				
			||||||
\t\tSymbol: \"%s\",
 | 
					\t\tSymbol: \"%s\",
 | 
				
			||||||
\t\tPrecision: %d,
 | 
					\t\tPrecision: %d,
 | 
				
			||||||
\t\tType: %s,
 | 
					\t\tType: %s,
 | 
				
			||||||
\t\tAlternateId: \"%s\"},\n""" % (self.name, self.description, self.name, self.precision, self.type, str(self.number))
 | 
					\t\tAlternateId: \"%s\"},\n""" % (self.name, self.description, self.name, self.precision, self.type, str(self.number))
 | 
				
			||||||
 | 
					        try:
 | 
				
			||||||
 | 
					            return unicode(s, 'utf_8')
 | 
				
			||||||
 | 
					        except TypeError:
 | 
				
			||||||
 | 
					            return s
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class SecurityList(object):
 | 
					class SecurityList(object):
 | 
				
			||||||
    def __init__(self, comment):
 | 
					    def __init__(self, comment):
 | 
				
			||||||
@@ -26,10 +44,10 @@ class SecurityList(object):
 | 
				
			|||||||
        self.currencies = {}
 | 
					        self.currencies = {}
 | 
				
			||||||
    def add(self, currency):
 | 
					    def add(self, currency):
 | 
				
			||||||
        self.currencies[currency.number] = currency
 | 
					        self.currencies[currency.number] = currency
 | 
				
			||||||
    def __str__(self):
 | 
					    def unicode(self):
 | 
				
			||||||
        string = "\t// "+self.comment+"\n"
 | 
					        string = "\t// "+self.comment+"\n"
 | 
				
			||||||
        for key in sorted(self.currencies.keys()):
 | 
					        for key in sorted(self.currencies.keys()):
 | 
				
			||||||
            string += str(self.currencies[key])
 | 
					            string += self.currencies[key].unicode()
 | 
				
			||||||
        return string
 | 
					        return string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def process_ccyntry(currency_list, node):
 | 
					def process_ccyntry(currency_list, node):
 | 
				
			||||||
@@ -59,7 +77,7 @@ def process_ccyntry(currency_list, node):
 | 
				
			|||||||
def get_currency_list():
 | 
					def get_currency_list():
 | 
				
			||||||
    currency_list = SecurityList("ISO 4217, from http://www.currency-iso.org/en/home/tables/table-a1.html")
 | 
					    currency_list = SecurityList("ISO 4217, from http://www.currency-iso.org/en/home/tables/table-a1.html")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    with request.urlopen('http://www.currency-iso.org/dam/downloads/lists/list_one.xml') as f:
 | 
					    f = urlopen('http://www.currency-iso.org/dam/downloads/lists/list_one.xml')
 | 
				
			||||||
    xmldoc = minidom.parse(f)
 | 
					    xmldoc = minidom.parse(f)
 | 
				
			||||||
    for isonode in xmldoc.childNodes:
 | 
					    for isonode in xmldoc.childNodes:
 | 
				
			||||||
        if isonode.nodeName == "ISO_4217":
 | 
					        if isonode.nodeName == "ISO_4217":
 | 
				
			||||||
@@ -68,11 +86,12 @@ def get_currency_list():
 | 
				
			|||||||
                    for ccyntrynode in ccytblnode.childNodes:
 | 
					                    for ccyntrynode in ccytblnode.childNodes:
 | 
				
			||||||
                        if ccyntrynode.nodeName == "CcyNtry":
 | 
					                        if ccyntrynode.nodeName == "CcyNtry":
 | 
				
			||||||
                            process_ccyntry(currency_list, ccyntrynode)
 | 
					                            process_ccyntry(currency_list, ccyntrynode)
 | 
				
			||||||
 | 
					    f.close()
 | 
				
			||||||
    return currency_list
 | 
					    return currency_list
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def get_cusip_list(filename):
 | 
					def get_cusip_list(filename):
 | 
				
			||||||
    cusip_list = SecurityList("")
 | 
					    cusip_list = SecurityList("")
 | 
				
			||||||
    with open(filename, newline='') as csvfile:
 | 
					    with open(filename) as csvfile:
 | 
				
			||||||
        csvreader = csv.reader(csvfile, delimiter=',')
 | 
					        csvreader = csv.reader(csvfile, delimiter=',')
 | 
				
			||||||
        for row in csvreader:
 | 
					        for row in csvreader:
 | 
				
			||||||
            cusip = row[0]
 | 
					            cusip = row[0]
 | 
				
			||||||
@@ -87,8 +106,8 @@ def main():
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    print("package main\n")
 | 
					    print("package main\n")
 | 
				
			||||||
    print("var SecurityTemplates = []Security{")
 | 
					    print("var SecurityTemplates = []Security{")
 | 
				
			||||||
    print(str(currency_list))
 | 
					    print(currency_list.unicode())
 | 
				
			||||||
    print(str(cusip_list))
 | 
					    print(cusip_list.unicode())
 | 
				
			||||||
    print("}")
 | 
					    print("}")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if __name__ == "__main__":
 | 
					if __name__ == "__main__":
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user