Aucune description

helpers.py 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
  2. ### BEGIN LICENSE
  3. # Copyright (C) 2013 Bryan M. Allred <bryan.allred@gmail.com>
  4. # This program is free software: you can redistribute it and/or modify it
  5. # under the terms of the GNU General Public License version 3, as published
  6. # by the Free Software Foundation.
  7. #
  8. # This program is distributed in the hope that it will be useful, but
  9. # WITHOUT ANY WARRANTY; without even the implied warranties of
  10. # MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
  11. # PURPOSE. See the GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License along
  14. # with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ### END LICENSE
  16. ### DO NOT EDIT THIS FILE ###
  17. """Helpers for an Ubuntu application."""
  18. import logging
  19. import os
  20. from . organiccodeconfig import get_data_file
  21. from . Builder import Builder
  22. from locale import gettext as _
  23. def get_builder(builder_file_name):
  24. """Return a fully-instantiated Gtk.Builder instance from specified ui
  25. file
  26. :param builder_file_name: The name of the builder file, without extension.
  27. Assumed to be in the 'ui' directory under the data path.
  28. """
  29. # Look for the ui file that describes the user interface.
  30. ui_filename = get_data_file('ui', '%s.ui' % (builder_file_name,))
  31. if not os.path.exists(ui_filename):
  32. ui_filename = None
  33. builder = Builder()
  34. builder.set_translation_domain('organiccode')
  35. builder.add_from_file(ui_filename)
  36. return builder
  37. # Owais Lone : To get quick access to icons and stuff.
  38. def get_media_file(media_file_name):
  39. media_filename = get_data_file('media', '%s' % (media_file_name,))
  40. if not os.path.exists(media_filename):
  41. media_filename = None
  42. return "file:///"+media_filename
  43. class NullHandler(logging.Handler):
  44. def emit(self, record):
  45. pass
  46. def set_up_logging(opts):
  47. # add a handler to prevent basicConfig
  48. root = logging.getLogger()
  49. null_handler = NullHandler()
  50. root.addHandler(null_handler)
  51. formatter = logging.Formatter("%(levelname)s:%(name)s: %(funcName)s() '%(message)s'")
  52. logger = logging.getLogger('organiccode')
  53. logger_sh = logging.StreamHandler()
  54. logger_sh.setFormatter(formatter)
  55. logger.addHandler(logger_sh)
  56. lib_logger = logging.getLogger('organiccode_lib')
  57. lib_logger_sh = logging.StreamHandler()
  58. lib_logger_sh.setFormatter(formatter)
  59. lib_logger.addHandler(lib_logger_sh)
  60. # Set the logging level to show debug messages.
  61. if opts.verbose:
  62. logger.setLevel(logging.DEBUG)
  63. logger.debug('logging enabled')
  64. if opts.verbose > 1:
  65. lib_logger.setLevel(logging.DEBUG)
  66. def get_help_uri(page=None):
  67. # help_uri from source tree - default language
  68. here = os.path.dirname(__file__)
  69. help_uri = os.path.abspath(os.path.join(here, '..', 'help', 'C'))
  70. if not os.path.exists(help_uri):
  71. # installed so use gnome help tree - user's language
  72. help_uri = 'organiccode'
  73. # unspecified page is the index.page
  74. if page is not None:
  75. help_uri = '%s#%s' % (help_uri, page)
  76. return help_uri
  77. def show_uri(parent, link):
  78. from gi.repository import Gtk # pylint: disable=E0611
  79. screen = parent.get_screen()
  80. Gtk.show_uri(screen, link, Gtk.get_current_event_time())
  81. def alias(alternative_function_name):
  82. '''see http://www.drdobbs.com/web-development/184406073#l9'''
  83. def decorator(function):
  84. '''attach alternative_function_name(s) to function'''
  85. if not hasattr(function, 'aliases'):
  86. function.aliases = []
  87. function.aliases.append(alternative_function_name)
  88. return function
  89. return decorator