Imported Upstream version 2.12.1
[platform/upstream/fontconfig.git] / fc-blanks / fc-blanks.py
index a2f0b95..cc23cde 100755 (executable)
@@ -1,12 +1,36 @@
 #! /usr/bin/python
 
-import urllib2
+from __future__ import absolute_import
+from __future__ import print_function
+try:
+    from urllib2 import urlopen
+    from urllib2 import URLError
+except ImportError:
+    from urllib.request import urlopen
+    from urllib.error import URLError
+
 import sys
+import os
 from lxml import html
+from six.moves import range
 
-fp = urllib2.urlopen('http://unicode.org/cldr/utility/list-unicodeset.jsp?a=[%3AGC%3DZs%3A][%3ADI%3A]&abb=on&ucd=on&esc=on&g')
-data = fp.read()
-fp.close()
+datafile = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'list-unicodeset.html')
+try:
+    fp = urlopen('http://unicode.org/cldr/utility/list-unicodeset.jsp?a=[%3AGC%3DZs%3A][%3ADI%3A]&abb=on&ucd=on&esc=on&g')
+    data = fp.read()
+    fp.close()
+    fp = open(datafile, 'wb');
+    fp.write(data);
+    fp.close();
+except URLError:
+    # fall back reading the static data in repo
+    try:
+        fp = open(datafile)
+        data = fp.read()
+        fp.close()
+    except IOError:
+        sys.stderr.write("Error: No static data to generate the blank data. please make sure the network connection is reachable to Unicode.org\n")
+        sys.exit(1)
 
 dom = html.fromstring(data)
 x = dom.xpath('/html/body/form/p/text()')
@@ -47,12 +71,12 @@ for i in p:
         fescape = False
     elif i >= '0' and i <= '9' or i.lower() >= 'a' and i.lower() <= 'f':
         if fescape == True:
-            raise RuntimeError, "Unexpected escape code"
+            raise RuntimeError("Unexpected escape code")
         if funicode == True:
             v <<= 4
             v += int(i, 16)
         else:
-            raise RuntimeError, "Unable to parse Unicode"
+            raise RuntimeError("Unable to parse Unicode")
     elif i == ' ':
         if fescape == True:
             funicode = True
@@ -71,13 +95,13 @@ for i in p:
         frange = False
     elif i == '-':
         if fescape == True:
-            raise RuntimeError, "Unexpected escape code"
+            raise RuntimeError("Unexpected escape code")
         vbegin = v
         v = 0
         funicode = False
         frange = True
     else:
-        raise RuntimeError, "Unable to parse Unicode: %s" % i
+        raise RuntimeError("Unable to parse Unicode: %s" % i)
 
     if fprocess == True:
         vbegin = 0
@@ -111,22 +135,22 @@ while True:
     s = sys.stdin.readline().rstrip()
     if s == "@@@":
         break
-    print s
+    print(s)
 
-print "static FcChar32 _fcBlanks[%s] = {" % (ncode + 1)
+print("static FcChar32 _fcBlanks[%s] = {" % (ncode + 1))
 k = 0
-for i in sorted(l, key=lambda(a): a[0]):
+for i in sorted(l, key=lambda a: a[0]):
     for j in range(i[0], i[1] + 1):
         if k != 0:
-            print ","
-        print "    0x%04x" % j,
+            print(",")
+        print("    0x%04x" % j, end=' ')
         k += 1
 
-print "};"
-print '''
+print("};")
+print('''
 static FcBlanks fcBlanks = {
     %s,
     -1,
     _fcBlanks
 };
-''' % (ncode + 1)
+''' % (ncode + 1))