resource-browser.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #!/usr/bin/python
  2. # Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. # Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. #
  5. # Permission is hereby granted, free of charge, to any person
  6. # obtaining a copy of this software and associated documentation
  7. # files (the "Software"), to deal in the Software without
  8. # restriction, including without limitation the rights to use,
  9. # copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. # copies of the Software, and to permit persons to whom the
  11. # Software is furnished to do so, subject to the following
  12. # conditions:
  13. #
  14. # The above copyright notice and this permission notice shall be
  15. # included in all copies or substantial portions of the Software.
  16. #
  17. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  19. # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  21. # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  22. # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  23. # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  24. # OTHER DEALINGS IN THE SOFTWARE.
  25. import sys
  26. import os
  27. from gi.repository import Gtk
  28. from pycrown import Repository
  29. class ResourceBrowser:
  30. def __init__(self, repository):
  31. self.m_repository = repository
  32. builder = Gtk.Builder()
  33. builder.add_from_file("ui/resource-browser.glade")
  34. self.m_filter_entry = builder.get_object("entry1")
  35. self.m_list_store = builder.get_object("liststore1")
  36. self.m_list_view = builder.get_object("treeview1")
  37. self.m_window = builder.get_object("window1")
  38. self.m_window.set_title(repository.root_path())
  39. renderer = Gtk.CellRendererText()
  40. column = Gtk.TreeViewColumn("Name", renderer, text=0)
  41. self.m_list_view.append_column(column)
  42. # Populate list model
  43. self.update_list_model()
  44. self.m_list_filter = self.m_list_store.filter_new()
  45. self.m_list_filter.set_visible_func(self.visible_func)
  46. self.m_list_view.set_model(self.m_list_filter)
  47. builder.connect_signals(self)
  48. self.m_window.show_all()
  49. Gtk.main()
  50. # Callback
  51. def on_delete(self, *args):
  52. Gtk.main_quit(*args)
  53. def update_list_model(self):
  54. self.m_repository.scan()
  55. self.m_list_store.clear()
  56. for res in self.m_repository.all_resources():
  57. self.m_list_store.append([res])
  58. # Refresh the repository contents
  59. def on_update_button_clicked(self, button):
  60. self.update_list_model()
  61. # We call refilter whenever the user types into the filter entry
  62. def on_filter_entry_text_changed(self, entry):
  63. self.m_list_filter.refilter()
  64. # The callback used to filter resources in the list view
  65. def visible_func(self, model, iter, user_data):
  66. name = str(model.get_value(iter, 0))
  67. # Strip leading and trailing spaces
  68. search_text = self.m_filter_entry.get_text().strip()
  69. # Make the find case-insensitive
  70. name = name.lower()
  71. search_text = search_text.lower()
  72. if (search_text == ""):
  73. return True
  74. if (name.find(search_text) == -1):
  75. return False
  76. return True
  77. #------------------------------------------------------------------------------
  78. def main():
  79. root_path = ""
  80. if (len(sys.argv) != 2):
  81. print("Usage: resource-browser <root-path>")
  82. sys.exit(-1)
  83. root_path = sys.argv[1];
  84. root_path = os.path.abspath(root_path)
  85. if not os.path.exists(root_path):
  86. print("The path does not exist.")
  87. sys.exit(-1)
  88. if (os.path.islink(root_path)):
  89. print("The path is a symbolic link.")
  90. sys.exit(-1)
  91. if not os.path.isdir(root_path):
  92. print("The path has to be a directory.")
  93. sys.exit(-1)
  94. repository = Repository.Repository(root_path)
  95. browser = ResourceBrowser(repository)
  96. main()