Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Copyright (C) 2011 Dan Cornell / Denim Group www.denimgroup.com
- #
- # The following terms apply to all files associated
- # with the software unless explicitly disclaimed in individual files.
- #
- # The authors hereby grant permission to use, copy, modify, distribute,
- # and license this software and its documentation for any purpose, provided
- # that existing copyright notices are retained in all copies and that this
- # notice is included verbatim in any distributions. No written agreement,
- # license, or royalty fee is required for any of the authorized uses.
- # Modifications to this software may be copyrighted by their authors
- # and need not follow the licensing terms described here, provided that
- # the new terms are clearly indicated on the first page of each file where
- # they apply.
- #
- # IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY
- # FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
- # ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY
- # DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE
- # POSSIBILITY OF SUCH DAMAGE.
- #
- # THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
- # INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
- # FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. THIS SOFTWARE
- # IS PROVIDED ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE
- # NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
- # MODIFICATIONS.
- from datetime import datetime
- import re
- import urllib
- class USBDeviceClass:
- def __init__(self, id=0, name='UNDEFINEDNAME'):
- self.id = id
- self.name = name
- def __str__(self):
- return 'id=' + str(hex(self.id)) + ', name=' + self.name
- class USBDeviceSubclass:
- def __init__(self, usbclass, id=0, name='UNDEFINEDNAME'):
- self.usbclass = usbclass
- self.id = id
- self.name = name
- def __str__(self):
- return 'usbclass=[' + self.usbclass + '], id=' + str(hex(self.id)) + ', name=' + self.name
- class USBDeviceProtocol:
- def __init__(self, usbsubclass, id=0, name='UNDEFINEDNAME'):
- self.usbsubclass = usbsubclass
- self.id = id
- self.name = name
- def __str__(self):
- return 'usbsubclass=[' + self.usbsubclass + '], id=' + str(hex(self.id)) + ', name=' + self.name
- # Config information
- source_url = 'http://www.linux-usb.org/usb.ids'
- print 'About to open URL'
- con = urllib.urlopen(source_url)
- print 'URL opened'
- my_data = con.read()
- con.close()
- print 'Data read'
- debug = open('usb_mapping_generation_debug.txt', 'w')
- debug.write(my_data)
- debug.close()
- pattern = re.compile(r"\n")
- lines = pattern.split(my_data)
- usb_classes = []
- i = 0
- num_lines = len(lines)
- # for line in lines:
- while i < num_lines:
- line = lines[i]
- # print i, line, '\n'
- if line.startswith("C "):
- print 'Found USB class:', line
- whitespace_pattern = re.compile(r"[ \t]+")
- tokens = whitespace_pattern.split(line)
- class_hex = tokens[1]
- class_name = ' '.join(tokens[2:])
- print 'Found class and name:', class_hex, class_name
- usb_classes.append(USBDeviceClass(int(class_hex, 16), class_name))
- i = i + 1
- for usb_class in usb_classes:
- print usb_class
- f = open('usb/_constantsimpl.py', 'w')
- f.write('# USB constant mappings\n')
- f.write('# Generated on: ' + str(datetime.now()) + '\n')
- f.write('# Based on data pulled from URL: ' + source_url + '\n\n\n')
- #f.write('def dev_class_to_string(dev_class_id):\n')
- f.write('dev_string = {\n')
- is_first_line = True
- for current_dev_class in usb_classes:
- if is_first_line:
- f.write('\t' + str(hex(current_dev_class.id)) + ': \'' + current_dev_class.name + '\'')
- else:
- f.write(',\n\t' + str(hex(current_dev_class.id)) + ': \'' + current_dev_class.name + '\'')
- is_first_line = False
- f.write(' }\n\n')
- f.write('def dev_class_to_string(dev_class_id):\n')
- f.write('\ttry:\n')
- f.write('\t\tret_val = dev_string[dev_class_id]\n')
- f.write('\texcept KeyError:\n')
- f.write('\t\tret_val = \'UNDEFINED_USB_DEVICE_CLASS\'\n')
- f.write('\n')
- f.write('\treturn(ret_val)\n')
- f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement