Вала на Python и обратно

Я застрял, пытаясь найти путь от Vala/C до Python и обратно. Все мои гугл-фу водят меня по кругу. Я хочу использовать Vala для написания API, а затем применять его из Python (или, возможно, из Gnome Javascript).

Используя Clutter в качестве моего примера (это также может быть виджет GTK + 3), вот мой вопрос: как мне —

Иди туда

Напишите собственного Актера, который при нажатии будет:

  1. Изменение цвета — NB: это делается в обработчике Vala. То есть объект vala связан с событием «отпускание кнопки». Этот обработчик вызывает метод vala: this.set_col('blue');
  2. Попросите его продолжить это событие в Python вместе с некоторыми данными — скажем, я хочу напечатать «Я посинел!» - поэтому мне нужен "синий" в качестве строки.

В Python я бы создал сцену и (каким-то образом — с помощью магии GI) создал своего нового актера. Я делаю все, что нужно для Python, чтобы настроить его, и я подключаюсь к тому же событию «кнопка-релиз» (я думаю..)

а) Будет ли запущен обработчик Vala, а затем Python? (По порядку или вообще.)

б) Должен ли я делать что-то особенное в обработчике Vala — например, возвращать true или, возможно, посылать какой-то новый сигнал для получения Python?

И обратно

Предположим, актера зовут V. Как мне: V.set_col('red') (в Python) запустить метод Vala set_col, передав строку Python? (Подозреваю, что это автомагия под GI, но точно не знаю.)

Вкратце

Vala actor -- event --> handler (in Vala) --> handler (in Python) with data
Vala method <--- method call with args from Python 

Я был бы признателен за любые ссылки и тому подобное, спасибо.


person Donn    schedule 18.04.2013    source источник
comment
На какой конкретный вопрос вы хотите, чтобы мы ответили?   -  person jsalonen    schedule 18.04.2013
comment
jsalonen - 1) Как получить код Vala и кода Python для запуска по сигналу/событию и 2) Как вызвать код Vala из Python. Туда и обратно.   -  person Donn    schedule 18.04.2013
comment
Хорошо, спасибо, это немного проясняет!   -  person jsalonen    schedule 18.04.2013


Ответы (1)


Я понял. Код ниже работает так:

  1. Создайте три файла в каталоге.
  2. «сборка» — это bash-скрипт.
  3. "redsquare.vala" - это Vala, которая станет библиотекой (файл .so)
  4. «test.py» — это код Python3 для использования этой библиотеки.
  5. Install these: ( apt-get bias, sorry )
    • apt-get install libgirepository1.0-dev
    • apt-get install gobject-introspection
    • валак и компания
  6. Запустите ./build и он должен перемолоть вуду и запустить файл test.py.

  7. Вы должны увидеть окно Clutter. Нажмите на красный квадрат и наблюдайте за консолью.

:-D

ХТН.

строить

#!/bin/bash

#
# Script borrowed from Tal Liron at:
# https://github.com/tliron/pygobject-example
#
# I did these to get this script to work:
#
# apt-get install libgirepository1.0-dev
# apt-get install gobject-introspection
#


echo "Cleaning..."

rm -rf tmp
rm -rf lib
rm -rf type
rm -f test

mkdir tmp
mkdir lib
mkdir type

    echo "Building Vala library..."

    # Note 1: Ubuntu package for valac: valac-0.14
    # Note 2: Generates broken gir if --gir= has a directory prefixed to it
    # Note 3: The -X switches for gcc are necessary!
    # Note 4: The generated gir will include GObject-2.0. That gir is
    #         included in Ubuntu package: libgirepository1.0-dev

    valac \
  --pkg clutter-1.0 \
    --library=Palelib \
    --directory=tmp \
    --gir=Palelib-1.0.gir \
    --output=libpalelib.so \
    -X -shared \
    -X -fPIC \
    redsquare.vala

    mv tmp/libpalelib.so lib
    mv tmp/Palelib-1.0.gir type

    # Note: We cannot generate C code and compile in the same call
    #       (We don't need the C code to run the test, but we are curious
    #       as to what Vala is generating. The resulting code will be in
    #       logging.c)
    #valac \
    #--ccode \
    #redsquare.vala


echo "Building typelib..."

# Note 1: Ubuntu package for g-ir-compiler: gobject-introspection
# Note 2: The --shared-library switch is really only necessary when using
#         the gir produced by valac, because it does not include the
#         'shared-library' attribute in <namespace> tag.


g-ir-compiler \
--shared-library=libpalelib.so \
--output=type/Palelib-1.0.typelib \
type/Palelib-1.0.gir

echo "Test Python..."

# Note 1: Ubuntu's default path for typelib files seems to be:
#         /usr/lib/girepository-1.0/.
# Note 2: It is also possible to programmatically change the
#         GI_TYPELIB_PATH environment var in Python (os.environ API).
#         If you do so, make sure to set it before importing from
#         gi.repository.
LD_LIBRARY_PATH=lib \
GI_TYPELIB_PATH=type \
./test.py

красная площадь.вала

namespace Palelib {

    public class RedSquare : Clutter.Actor {

    //private vars
    private Clutter.Canvas _canvas;
    private int[] _col = { 255, 0, 0 };

    //Constructor - Needs to be called explicitly from Python by .new()
    public RedSquare() {
      stdout.printf( "RedSquare constructor.\n" );

      _canvas = new Clutter.Canvas();
      _canvas.set_size(300,300);

      this.set_size(300,300);
      this.set_content( _canvas );

      //Connect to the draw signal.
      _canvas.draw.connect(drawme);

      //Make it reactive and connect to the button-press-event
      this.set_reactive(true);
      this.button_press_event.connect( cleek );
    }

    //Button press signal handler
    private bool cleek ( Clutter.ButtonEvent evt ) {
      stdout.printf("Vala cleek() has run!\n");
      this._col = {0,255,0}; //Just change the colour
      this.redraw("from Vala");
      //return true; //Stops the signal here. Python won't get it.
      return false; //Lets the signal carry on going (to Python).
    }

    //Draws the Cairo art to the canvas
    private bool drawme( Cairo.Context ctx, int w, int h) {
      stdout.printf("drawme test.\n");
      ctx.set_source_rgb(this._col[0],this._col[1],this._col[2]);
      ctx.rectangle(0,0,300,300);
      ctx.fill();
      return true;
    }

    //Redraw - forces invalidate which trips the draw event
    //Am gonna call this directly from Python too!
    public void redraw(string? thing) {
      thing = thing ?? "from null"; //tests for null or else
      stdout.printf( "redraw test %s.\n", thing );

      this._canvas.invalidate();
    }
    } //end RedSquare class
} //end namespace

test.py

#!/usr/bin/env python3

"""
Tests the instance of our Vala actor.

I expect to see a red square on the white stage.
(It can be clicked.)

"""

import sys
from gi.repository import Palelib, Clutter

Clutter.init(sys.argv)
stage = Clutter.Stage()
stage.set_size(800, 400)
stage.set_title("Blah blah")
stage.connect('destroy', lambda x: Clutter.main_quit() )


# Make our Object:
rs = Palelib.RedSquare.new() #Note the .new() call. Yuck.
print(rs)
#print(dir(rs)) # See that it is an Actor object.

rs.set_position(100,100)

stage.add_child(rs)

#Force rs to appear. Calls a Vala method and passes a string.
rs.redraw("from Python")

"""
# Crud for testing:
r1 = Clutter.Rectangle()
r1.set_size(50,50)
r1.set_position(0,0)
damnweird = Clutter.Color.new(0,0,0,255)
r1.set_color( damnweird  )

stage.add_child(r1)
"""



"""
Let's get an event going from Python!
Because the RedSquare actor is *already* listening
to a button-press-event (in Vala) this is the second 
such event it will obey. 

I *think* it happens after the vala cleek() method runs.
If you |return true| in cleek(), then this does NOT run,
so that implies that Python is happening second in the chain.
"""
def gogo( a, evt ):
  print ("Hello from gogo. %s %s" % (a,evt))
rs.connect("button_press_event", gogo)



stage.show_all()
Clutter.main()
person Donn    schedule 20.04.2013
comment
Надеюсь, это законно. У меня есть один вопрос. Сколько раз вся библиотека Clutter загружается в оперативную память? Я запускаю его в Python, но я также запускаю его в моем файле .so? то есть библиотека Clutter и все ее отношения загружаются один или два раза? - person Donn; 20.04.2013