@@ -97,3 +97,61 @@ current _prefixes list.
97
97
>>> k = set( x.split(' ')[0] for x in StringIO.StringIO(output.getvalue()).readlines() )
98
98
>>> k == set(ranges._prefixes.keys())
99
99
True
100
+
101
+
102
+ Make an XML file with somre prefix definitions and load that into the
103
+ ranges module.
104
+
105
+ First save the current ranges so we can restore later.
106
+
107
+ >>> save_prefixes = ranges._prefixes
108
+
109
+ Write the XML to a file.
110
+
111
+ >>> import tempfile
112
+ >>> xmlfile = tempfile.NamedTemporaryFile(delete=False)
113
+ >>> xmlfile.write("""<?xml version='1.0' encoding='utf-8'?>
114
+ ... <ISBNRangeMessage>
115
+ ... <MessageSerialNumber>0aad2b046ddd9b30e080cb2b24afc868</MessageSerialNumber>
116
+ ... <MessageDate>Thu, 20 May 2010 18:36:55 GMT</MessageDate>
117
+ ... <EAN.UCCPrefixes><EAN.UCC>
118
+ ... <Prefix>978</Prefix>
119
+ ... <Rules>
120
+ ... <Rule><Range>0000000-5999999</Range><Length>1</Length></Rule>
121
+ ... <Rule><Range>6000000-6499999</Range><Length>3</Length></Rule>
122
+ ... <Rule><Range>6500000-6999999</Range><Length>0</Length></Rule>
123
+ ... </Rules>
124
+ ... </EAN.UCC></EAN.UCCPrefixes>
125
+ ... <RegistrationGroups>
126
+ ... <Group>
127
+ ... <Prefix>978-0</Prefix>
128
+ ... <Rules>
129
+ ... <Rule><Range>0000000-1999999</Range><Length>2</Length></Rule>
130
+ ... <Rule><Range>2000000-6999999</Range><Length>3</Length></Rule>
131
+ ... </Rules>
132
+ ... </Group>
133
+ ... </RegistrationGroups>
134
+ ... </ISBNRangeMessage>
135
+ ... """)
136
+ >>> xmlfile.close()
137
+
138
+ Load the XML file by URL and output it to another string. Check if the
139
+ content of the XML has been
140
+
141
+ >>> import urllib
142
+ >>> ranges.download('file://' + urllib.pathname2url(xmlfile.name))
143
+ >>> import sys
144
+ >>> output = StringIO.StringIO()
145
+ >>> save_stdout = sys.stdout
146
+ >>> sys.stdout = output
147
+ >>> ranges.output()
148
+ >>> sys.stdout = save_stdout
149
+ >>> output = output.getvalue()
150
+ >>> '\n978 0-5 600-649\n' in output and '\n978-0 00-19 200-699\n' in output
151
+ True
152
+
153
+ Restore the original ranges and clean up.
154
+
155
+ >>> ranges._prefixes = save_prefixes
156
+ >>> import os
157
+ >>> os.unlink(xmlfile.name)
0 commit comments