Coverage for src/botadi/mokadi/mokadi_action_slides.py: 88%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# -*- coding: utf-8 -*-
2"""
3@file
4@brief Defines an action for Mokadi.
5"""
6import os
7from .mokadi_action import MokadiAction
8from .mokadi_info import MokadiInfo
9from .mokadi_exceptions import MokadiException
10from .pptx_helper import pptx_enumerate_text
11from .mokadi_helper import parse_string_int
14class MokadiActionSlides(MokadiAction):
15 """
16 Action present slides.
17 """
19 def __init__(self, folder, fLOG=None):
20 """
21 Constructor.
23 @param folder folder where to look for presentation
24 """
25 MokadiAction.__init__(self, fLOG=fLOG)
26 self._folder = folder
27 if not os.path.exists(folder):
28 raise FileNotFoundError(folder)
30 def can_do(self, interpreted, message):
31 """
32 Tells is the class.
34 @param interpreted interpreted message
35 @param message message
36 @return true if the class can process the message
37 """
38 if len(interpreted) < 2:
39 return False
40 word = interpreted[1]
41 for word in interpreted[1:3]:
42 if word[1] == ":presentation:":
43 return True
44 return False
46 def process_interpreted_message(self, interpretation, message):
47 """
48 Processes the interpreted message.
50 @param interpretation interpretation
51 @param message original message
52 @return iterator on Info
53 """
54 import pptx
56 done = False
57 interpretation0 = interpretation
58 interpretation = [_ for _ in interpretation if _[1] != ":numero:"]
59 if len(interpretation) == 4:
60 if interpretation[1][1] == ":verb_voir:" and interpretation[2][1] == ":presentation:":
61 pres = list(self.enumerate_listdir())
62 yield MokadiInfo("ok", "Il y a {0} présentations.".format(len(pres)))
63 for name in pres:
64 yield MokadiInfo("ok", name)
65 done = True
66 elif len(interpretation) == 7:
67 if interpretation[1][0] == "lire" and interpretation[2][1] == ":presentation:" and \
68 interpretation[3][1] == ":int:" and interpretation[4][1] == ":slide:" and \
69 interpretation[5][1] == ":int:":
70 presn = parse_string_int(interpretation[3][0])
71 slide = parse_string_int(interpretation[5][0])
72 pres = list(self.enumerate_listdir())
73 if presn <= 0 or presn > len(pres):
74 yield MokadiInfo("error", error="La présentation {0} n'existe pas.".format(presn))
75 done = True
76 else:
77 name = os.path.join(self._folder, pres[presn - 1])
78 self.fLOG("[MokadiActionSlides] open '%s' from '%s'" % (
79 os.path.split(name)[-1], os.path.dirname(name)))
80 ptx = pptx.Presentation(name)
81 if slide <= 0 or slide > len(ptx.slides):
82 yield MokadiInfo("error", error="La présentation ne contient pas le transparent {0}.".format(slide))
83 done = True
84 else:
85 for islide, _, __, text in pptx_enumerate_text(ptx):
86 if islide == slide - 1:
87 yield MokadiInfo("ok", text)
88 done = True
89 if not done:
90 raise MokadiException(
91 "Unable to interpret '{0}'\n{1} - {2}.".format(message, len(interpretation0), interpretation))
93 def enumerate_listdir(self):
94 """
95 Return all presentations in a folder.
97 @return list of presentation
98 """
99 for name in os.listdir(self._folder):
100 if os.path.splitext(name)[-1] == ".pptx":
101 yield name