我们从Python开源项目中,提取了以下15个代码示例,用于说明如何使用gi.repository()。
def get_gi_libdir(module, version): statement = """ import gi gi.require_version("GIRepository", "2.0") from gi.repository import GIRepository repo = GIRepository.Repository.get_default() module, version = (%r, %r) repo.require(module, version, GIRepository.RepositoryLoadFlags.IREPOSITORY_LOAD_FLAG_LAZY) print(repo.get_shared_library(module)) """ statement %= (module, version) libs = exec_statement(statement).split(',') for lib in libs: path = findSystemLibrary(lib.strip()) return os.path.normpath(os.path.dirname(path)) raise ValueError("Could not find libdir for %s-%s" % (module, version))
def _gst_available(): """Determine whether Gstreamer and the Python GObject bindings are installed. """ try: import gi except ImportError: return False try: gi.require_version('Gst', '1.0') except (ValueError, AttributeError): return False try: from gi.repository import Gst # noqa except ImportError: return False return True
def is_quartz(): """ Tests to see if Python is currently running with gtk and windowing system is Mac OS-X's "quartz". """ if mac(): try: import gi gi.require_version('Gtk', '3.0') gi.require_version('Gdk', '3.0') from gi.repository import Gtk from gi.repository import Gdk except ImportError: return False return Gdk.Display.get_default().__class__.__name__.endswith("QuartzDisplay") return False
def backend_gtk3agg_internal_check(x): try: import gi except ImportError: return (False, "Requires pygobject to be installed.") try: gi.require_version("Gtk", "3.0") except ValueError: return (False, "Requires gtk3 development files to be installed.") except AttributeError: return (False, "pygobject version too old.") try: from gi.repository import Gtk, Gdk, GObject except (ImportError, RuntimeError): return (False, "Requires pygobject to be installed.") return (True, "version %s.%s.%s" % ( Gtk.get_major_version(), Gtk.get_micro_version(), Gtk.get_minor_version()))
def import_optional(): """Safe module import""" success = AttributeDict() try: gi.require_version('Gst', '1.0') from gi.repository import Gst # noqa: F401 success.gstreamer = True except Exception: success.gstreamer = False logger.warning("Fail to import Gstreamer module") try: from PIL import Image # noqa: F401 success.pillow = True except Exception: success.pillow = False logger.warning("Fail to import Pillow module") return success
def monitor_geometry(): "Return the current monitor geometry" import gi gi.require_version('Gdk', '3.0') from gi.repository import Gdk from collections import namedtuple MonitorGeometry = namedtuple( 'MonitorGeometry', ['left', 'right', 'top', 'bottom', 'width', 'height'], ) display = Gdk.Display.get_default() screen = display.get_default_screen() window = screen.get_active_window() monitor = screen.get_monitor_at_window(window) g = screen.get_monitor_geometry(monitor) right = g.x + g.width bottom = g.y + g.height return MonitorGeometry(g.x, right, g.y, bottom, g.width, g.height)
def git_call(cmd, path): """Calls a command with check_output, raising NotARepo if there is no git repo there. This lets us avoid the race condition of a repo disappearing disappear before we call the command.""" try: proc = Popen(cmd, cwd=path, stdout=PIPE, stderr=PIPE) stdout, stderr = proc.communicate() except OSError: # Git not installed, or repo path doesn't exist or isn't a directory. raise NotARepo(proc.returncode, cmd, output=(stdout + stderr)) else: if proc.returncode: if 'Not a git repository' in stderr.decode('utf8'): raise NotARepo(proc.returncode, cmd, output=(stdout + stderr)) else: raise CalledProcessError(proc.returncode, cmd, output=(stdout + stderr)) return stdout.decode('utf8')
def has_display(): """ Tests to see if Python is currently running with gtk """ # FIXME: currently, Gtk.init_check() requires all strings # in argv, and we might have unicode. temp, sys.argv = sys.argv, sys.argv[:1] try: import gi gi.require_version('Gtk', '3.0') gi.require_version('Gdk', '3.0') from gi.repository import Gtk from gi.repository import Gdk except ImportError: return False try: test = Gtk.init_check(temp) and \ Gdk.Display.get_default() sys.argv = temp return bool(test) except: sys.argv = temp return False # A couple of places add menu accelerators using <alt>, which doesn't # work with Gtk-quartz. <Meta> is the usually correct replacement, but # in one case the key is a number, and <meta>number is used by Spaces # (a mac feature), so we'll use control instead.
def gui(): """Start the graphical user interface.""" try: import gi gi.require_version("Gtk", "3.0") from gi.repository import Gtk except ImportError: print("Sorry, this needs pygobject to be installed!") sys.exit() win = Gtk.Window(title="Set display brightness") ad1 = Gtk.Adjustment(value=get_actual_brightness(), lower=11, upper=255) scale = Gtk.Scale(orientation=Gtk.Orientation.HORIZONTAL, adjustment=ad1) def on_scale_changed(s, _): value = int(s.get_value()) set_brightness(value) scale.connect("button-release-event", on_scale_changed) scale.connect("key_release_event", on_scale_changed) scale.connect("scroll-event", on_scale_changed) scale.set_size_request(350, 50) # Main Container main_container = Gtk.Fixed() main_container.put(scale, 10, 10) # Main Window win.connect("delete-event", Gtk.main_quit) win.connect("destroy", Gtk.main_quit) win.add(main_container) win.resize(400, 50) win.set_position(Gtk.WindowPosition.CENTER) win.show_all() Gtk.main()
def test_gi_gst_binding(pyi_builder): pyi_builder.test_source(''' import gi gi.require_version('Gst', '1.0') from gi.repository import Gst Gst.init(None) print(Gst) ''') ## For PyGObject >= 2.0 # Names of all "gi.repository" packages provided by PyGObject >= 2.0 to be # tested below, typically corresponding to those packages hooked by PyInstaller.
def test_gi2_repository(pyi_builder, repository_name): ''' Test the importability of the `gi.repository` subpackage with the passed name installed with PyGObject >= 2.0 (e.g., `GLib`, corresponding to the `gi.repository.GLib` subpackage). ''' # Test the importability of this subpackage. pyi_builder.test_source(''' import gi gi.require_version('{repository_name}', '2.0') from gi.repository import {repository_name} print({repository_name}) '''.format(repository_name=repository_name))
def get_glib_system_data_dirs(): statement = """ import gi gi.require_version('GLib', '2.0') from gi.repository import GLib print(GLib.get_system_data_dirs()) """ data_dirs = eval_statement(statement) if not data_dirs: logger.error("gi repository 'GIRepository 2.0' not found. " "Please make sure libgirepository-gir2.0 resp. " "lib64girepository-gir2.0 is installed.") # :todo: should we raise a SystemError here? return data_dirs
def __init__(self, player): self.player = player self.logger = logging.getLogger('KeyboardClient') try: import gi gi.require_version('Keybinder', '3.0') from gi.repository import Keybinder Keybinder.init() Keybinder.bind('XF86AudioPlay', self._on_XF86AudioPlay) Keybinder.bind('XF86AudioNext', self._on_XF86AudioNext) Keybinder.bind('XF86AudioPrev', self._on_XF86AudioPrev) except ValueError: self.logger.warning('Keybinder is needed on Linux for MediaKey binding')
def backend_gtk3cairo_internal_check(x): try: import cairocffi except ImportError: try: import cairo except ImportError: return (False, "Requires cairocffi or pycairo to be installed.") try: import gi except ImportError: return (False, "Requires pygobject to be installed.") try: gi.require_version("Gtk", "3.0") except ValueError: return (False, "Requires gtk3 development files to be installed.") except AttributeError: return (False, "pygobject version too old.") try: from gi.repository import Gtk, Gdk, GObject except (RuntimeError, ImportError): return (False, "Requires pygobject to be installed.") return (True, "version %s.%s.%s" % ( Gtk.get_major_version(), Gtk.get_micro_version(), Gtk.get_minor_version()))