Advertisement
Guest User

generate_usb_mappings.py

a guest
Sep 11th, 2011
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.03 KB | None | 0 0
  1. # Copyright (C) 2011 Dan Cornell / Denim Group www.denimgroup.com
  2. #
  3. # The following terms apply to all files associated
  4. # with the software unless explicitly disclaimed in individual files.
  5. #
  6. # The authors hereby grant permission to use, copy, modify, distribute,
  7. # and license this software and its documentation for any purpose, provided
  8. # that existing copyright notices are retained in all copies and that this
  9. # notice is included verbatim in any distributions. No written agreement,
  10. # license, or royalty fee is required for any of the authorized uses.
  11. # Modifications to this software may be copyrighted by their authors
  12. # and need not follow the licensing terms described here, provided that
  13. # the new terms are clearly indicated on the first page of each file where
  14. # they apply.
  15. #
  16. # IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY
  17. # FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
  18. # ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY
  19. # DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE
  20. # POSSIBILITY OF SUCH DAMAGE.
  21. #
  22. # THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
  23. # INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  24. # FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.  THIS SOFTWARE
  25. # IS PROVIDED ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE
  26. # NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
  27. # MODIFICATIONS.
  28.  
  29. from datetime import datetime
  30. import re
  31. import urllib
  32.  
  33. class USBDeviceClass:
  34.     def __init__(self, id=0, name='UNDEFINEDNAME'):
  35.         self.id = id
  36.         self.name = name
  37.     def __str__(self):
  38.         return 'id=' + str(hex(self.id)) + ', name=' + self.name
  39.  
  40. class USBDeviceSubclass:
  41.     def __init__(self, usbclass, id=0, name='UNDEFINEDNAME'):
  42.         self.usbclass = usbclass
  43.         self.id = id
  44.         self.name = name
  45.     def __str__(self):
  46.         return 'usbclass=[' + self.usbclass + '], id=' + str(hex(self.id)) + ', name=' + self.name
  47.  
  48. class USBDeviceProtocol:
  49.     def __init__(self, usbsubclass, id=0, name='UNDEFINEDNAME'):
  50.         self.usbsubclass = usbsubclass
  51.         self.id = id
  52.         self.name = name
  53.     def __str__(self):
  54.         return 'usbsubclass=[' + self.usbsubclass + '], id=' + str(hex(self.id)) + ', name=' + self.name
  55.  
  56. # Config information
  57. source_url = 'http://www.linux-usb.org/usb.ids'
  58.  
  59. print 'About to open URL'
  60. con = urllib.urlopen(source_url)
  61. print 'URL opened'
  62.  
  63. my_data = con.read()
  64. con.close()
  65.  
  66. print 'Data read'
  67. debug = open('usb_mapping_generation_debug.txt', 'w')
  68. debug.write(my_data)
  69. debug.close()
  70.  
  71. pattern = re.compile(r"\n")
  72. lines = pattern.split(my_data)
  73.  
  74. usb_classes = []
  75.  
  76. i = 0
  77. num_lines = len(lines)
  78.  
  79. # for line in lines:
  80. while i < num_lines:
  81.     line = lines[i]
  82.     # print i, line, '\n'
  83.     if line.startswith("C "):
  84.         print 'Found USB class:', line
  85.         whitespace_pattern = re.compile(r"[ \t]+")
  86.         tokens = whitespace_pattern.split(line)
  87.         class_hex = tokens[1]
  88.         class_name = ' '.join(tokens[2:])
  89.         print 'Found class and name:', class_hex, class_name
  90.         usb_classes.append(USBDeviceClass(int(class_hex, 16), class_name))
  91.     i = i + 1
  92.  
  93. for usb_class in usb_classes:
  94.     print usb_class
  95.  
  96. f = open('usb/_constantsimpl.py', 'w')
  97.  
  98. f.write('# USB constant mappings\n')
  99. f.write('# Generated on: ' + str(datetime.now()) + '\n')
  100. f.write('# Based on data pulled from URL: ' + source_url + '\n\n\n')
  101.  
  102. #f.write('def dev_class_to_string(dev_class_id):\n')
  103. f.write('dev_string = {\n')
  104.  
  105. is_first_line = True
  106.  
  107. for current_dev_class in usb_classes:
  108.     if is_first_line:
  109.         f.write('\t' + str(hex(current_dev_class.id)) + ': \'' + current_dev_class.name + '\'')
  110.     else:
  111.         f.write(',\n\t' + str(hex(current_dev_class.id)) + ': \'' + current_dev_class.name + '\'')
  112.     is_first_line = False
  113.  
  114. f.write(' }\n\n')
  115.  
  116. f.write('def dev_class_to_string(dev_class_id):\n')
  117. f.write('\ttry:\n')
  118. f.write('\t\tret_val = dev_string[dev_class_id]\n')
  119. f.write('\texcept KeyError:\n')
  120. f.write('\t\tret_val = \'UNDEFINED_USB_DEVICE_CLASS\'\n')
  121. f.write('\n')
  122. f.write('\treturn(ret_val)\n')
  123.  
  124. f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement