/socorro/analysis/correlations/addonids.py

https://github.com/luser/socorro · Python · 106 lines · 52 code · 8 blank · 46 comment · 10 complexity · fc2248be6ebdb67f45f73427a6d55038 MD5 · raw file

  1. #!/usr/bin/python
  2. # vim: set shiftwidth=4 tabstop=4 autoindent expandtab:
  3. # ***** BEGIN LICENSE BLOCK *****
  4. # Version: MPL 1.1/GPL 2.0/LGPL 2.1
  5. #
  6. # The contents of this file are subject to the Mozilla Public License Version
  7. # 1.1 (the "License"); you may not use this file except in compliance with
  8. # the License. You may obtain a copy of the License at
  9. # http://www.mozilla.org/MPL/
  10. #
  11. # Software distributed under the License is distributed on an "AS IS" basis,
  12. # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. # for the specific language governing rights and limitations under the
  14. # License.
  15. #
  16. # The Original Code is AddonIds.py.
  17. #
  18. # The Initial Developer of the Original Code is the Mozilla Foundation.
  19. # Portions created by the Initial Developer are Copyright (C) 2009
  20. # the Initial Developer. All Rights Reserved.
  21. #
  22. # Contributor(s):
  23. # L. David Baron <dbaron@dbaron.org>, Mozilla Corporation (original author)
  24. #
  25. # Alternatively, the contents of this file may be used under the terms of
  26. # either the GNU General Public License Version 2 or later (the "GPL"), or
  27. # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  28. # in which case the provisions of the GPL or the LGPL are applicable instead
  29. # of those above. If you wish to allow use of your version of this file only
  30. # under the terms of either the GPL or the LGPL, and not to allow others to
  31. # use your version of this file under the terms of the MPL, indicate your
  32. # decision by deleting the provisions above and replace them with the notice
  33. # and other provisions required by the GPL or the LGPL. If you do not delete
  34. # the provisions above, a recipient may use your version of this file under
  35. # the terms of any one of the MPL, the GPL or the LGPL.
  36. #
  37. # ***** END LICENSE BLOCK *****
  38. import jsondb
  39. import sys
  40. import re
  41. __all__ = [
  42. "info_for_id"
  43. ]
  44. class AddonInfoMap(jsondb.JsonDB):
  45. def put(self, id, name, url):
  46. self._map[id] = { "name": name, "url": url }
  47. def get(self, id):
  48. map = self._map
  49. if not id in map:
  50. return None
  51. entry = map[id]
  52. return AddonInfo(name=entry["name"], url=entry["url"])
  53. class AMOAddonInfoMap(AddonInfoMap):
  54. def get(self, id):
  55. if not id in self._map:
  56. self.check_AMO()
  57. return AddonInfoMap.get(self, id)
  58. def check_AMO(self):
  59. # store result whether it's "None" or not
  60. # FIXME: WRITE ME
  61. pass
  62. class AddonInfo:
  63. def __init__(self, name, url):
  64. self.name = name
  65. self.url = url
  66. # FIXME: There's lots of about:support output in support.mozilla.com
  67. # forums that has additional extension IDs paired with names. It would
  68. # be a good additional source of data.
  69. local_db = AddonInfoMap("addonids-local")
  70. amo_db = AMOAddonInfoMap("addonids-amo")
  71. def info_for_id(id):
  72. return local_db.get(id) or amo_db.get(id)
  73. # When executed directly...
  74. if __name__ == '__main__':
  75. if len(sys.argv) == 1:
  76. # prompt for an addition to the local database.
  77. sys.stdout.write("Addon ID: ")
  78. id = sys.stdin.readline().rstrip("\n")
  79. sys.stdout.write("Name: ")
  80. name = sys.stdin.readline().rstrip("\n")
  81. sys.stdout.write("Homepage: ")
  82. url = sys.stdin.readline().rstrip("\n")
  83. local_db.put(id, name, url)
  84. local_db.write()
  85. elif len(sys.argv) == 2 and sys.argv[1] == "-i":
  86. # or, with -i, import the AMO one
  87. io = open("amo-ids.txt", "r")
  88. for line in io:
  89. # Sometimes the last field is \N instead of a string
  90. line = re.sub(r'\\N$', r'""', line)
  91. [id, number, name] = json.loads("[" + line + "]")
  92. if len(id) > 0:
  93. url = "https://addons.mozilla.org/addon/" + str(number)
  94. amo_db.put(id, name, url)
  95. io.close()
  96. amo_db.write()
  97. else:
  98. raise StandardError("unexpected arguments")