Skip to content

Instantly share code, notes, and snippets.

@polymorf
Created October 9, 2018 21:42
Show Gist options
  • Save polymorf/b3cd3457c433100778c6815008d2ede4 to your computer and use it in GitHub Desktop.
Save polymorf/b3cd3457c433100778c6815008d2ede4 to your computer and use it in GitHub Desktop.
Antidote D-Bus API service to review files
#!/usr/bin/env python2
# coding: utf8
import gobject
import dbus
import dbus.service
import codecs
from polytools import *
from dbus.mainloop.glib import DBusGMainLoop
import sys
import os
OPATH = "/com/druide/antidote/dbus/api/v1/AntidoteFileCorrector"
IFACE = "com.druide.antidote.dbus.api"
BUS_NAME = "com.druide.antidote.dbus.api.v1.AntidoteFileCorrector"
class AntidoteFileCorrector(dbus.service.Object):
def __init__(self, filename, output_file):
self.filename = filename
self.output_file = output_file
f = codecs.open(filename, encoding='utf-8')
self.data_texte = f.read()
f.close()
bus = dbus.SessionBus()
bus.request_name(BUS_NAME)
bus_name = dbus.service.BusName(BUS_NAME, bus=bus)
dbus.service.Object.__init__(self, bus_name, OPATH)
@dbus.service.method("com.druide.antidote.dbus.api", in_signature="", out_signature="")
def ActiveDocument(self):
#info("%s()" % (sys._getframe().f_code.co_name))
self.actif=True
@dbus.service.method("com.druide.antidote.dbus.api", in_signature="iisb", out_signature="")
def CorrigeDansTexteur(self, leDebut, laFin, laChaine, automatique):
#info("%s(%d, %d, %s, %d)" % (sys._getframe().f_code.co_name, leDebut, laFin, repr(laChaine), automatique))
if leDebut == -1 or laFin == -1:
return
self.data_texte = self.data_texte[:leDebut] + laChaine + self.data_texte[laFin:]
@dbus.service.method("com.druide.antidote.dbus.api", in_signature="ii", out_signature="s")
def DonneBloc(self, leDebut, laFin):
#info("%s(%d,%d)" % (sys._getframe().f_code.co_name, leDebut, laFin))
return self.data_texte[leDebut:laFin]
@dbus.service.method("com.druide.antidote.dbus.api", in_signature="", out_signature="i")
def DonneDebutSelection(self):
#info("%s()" % (sys._getframe().f_code.co_name))
return 0
@dbus.service.method("com.druide.antidote.dbus.api", in_signature="", out_signature="i")
def DonneFinDocument(self):
#info("%s()" % (sys._getframe().f_code.co_name))
return len(self.data_texte)
@dbus.service.method("com.druide.antidote.dbus.api", in_signature="", out_signature="i")
def DonneFinSelection(self):
#info("%s()" % (sys._getframe().f_code.co_name))
return len(self.data_texte)
@dbus.service.method("com.druide.antidote.dbus.api", in_signature="i", out_signature="i")
def DonnePositionFinBoite(self, unePos):
pos = self.data_texte[unePos:].find('\n\n')
if pos == -1:
#info("%s(%d) = %d" % (sys._getframe().f_code.co_name, unePos, len(self.data_texte)))
return len(self.data_texte)
else:
#info("%s(%d) = %d" % (sys._getframe().f_code.co_name, unePos, unePos + pos))
return unePos + pos
@dbus.service.method("com.druide.antidote.dbus.api", in_signature="", out_signature="s")
def DonneRetourDeCharriot(self):
#info("%s()" % (sys._getframe().f_code.co_name))
return "\n\n"
@dbus.service.method("com.druide.antidote.dbus.api", in_signature="", out_signature="s")
def DonneTitreDocument(self):
#info("%s()" % (sys._getframe().f_code.co_name))
titre="file : %s" % (self.filename)
return titre
@dbus.service.method("com.druide.antidote.dbus.api", in_signature="", out_signature="b")
def PermetsRetourDeCharriot(self):
#info("%s()" % (sys._getframe().f_code.co_name))
return True
@dbus.service.method("com.druide.antidote.dbus.api", in_signature="", out_signature="")
def RetourneAuTexteur(self):
#info("%s()" % (sys._getframe().f_code.co_name))
pass
@dbus.service.method("com.druide.antidote.dbus.api", in_signature="", out_signature="")
def RompsLienCorrecteur(self):
#info("%s()" % (sys._getframe().f_code.co_name))
pass
@dbus.service.method("com.druide.antidote.dbus.api", in_signature="", out_signature="")
def RompsLienTexteur(self):
info("%s()" % (sys._getframe().f_code.co_name))
f = codecs.open(self.output_file, encoding='utf-8', mode='w+')
f.write(self.data_texte)
f.close()
sys.exit(0)
@dbus.service.method("com.druide.antidote.dbus.api", in_signature="ii", out_signature="")
def SelectionneIntervalle(self, leDebut, laFin):
#info("%s(%d, %d)" % (sys._getframe().f_code.co_name, leDebut, laFin))
#bloc = self.data_texte[leDebut:laFin]
#print "====\n%s\n====" % bloc
pass
if __name__ == "__main__":
DBusGMainLoop(set_as_default=True)
loop = gobject.MainLoop()
a = AntidoteFileCorrector(sys.argv[1], sys.argv[2])
# start antidote and bind on it
os.system("Antidote9 --module API_DBus:v1.0 --identificateur AntidoteFileCorrector --outil C &")
loop.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment