-
Notifications
You must be signed in to change notification settings - Fork 33
/
plugins_agreg.py
210 lines (169 loc) · 6.85 KB
/
plugins_agreg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import os
import sys
from pathlib import Path
import pygit2
from make_book.src.book import Book
frido_mark_list = []
frido_mark_list.append("% SCRIPT MARK -- DECLARATIVE PART")
frido_mark_list.append("% SCRIPT MARK -- GARDE MES NOTES")
frido_mark_list.append("% SCRIPT MARK -- TOC")
frido_mark_list.append("% SCRIPT MARK -- FRIDO")
frido_mark_list.append("% SCRIPT MARK -- DÉVELOPPEMENTS POSSIBLES")
frido_mark_list.append("% SCRIPT MARK -- FINAL")
book_mark_list = frido_mark_list
research_mark_list = []
research_mark_list.append("% SCRIPT MARK -- DECLARATIVE PART")
research_mark_list.append("% SCRIPT MARK -- GARDE MES NOTES")
research_mark_list.append("% SCRIPT MARK -- TOC")
research_mark_list.append("% SCRIPT MARK -- RESEARCH PART")
research_mark_list.append("% SCRIPT MARK -- FINAL")
mazhe_mark_list = []
mazhe_mark_list.append("% SCRIPT MARK -- DECLARATIVE PART")
mazhe_mark_list.append("% SCRIPT MARK -- GARDE MAZHE")
mazhe_mark_list.append("% SCRIPT MARK -- TOC")
mazhe_mark_list.append("% SCRIPT MARK -- ENGLISH INTRODUCTION")
mazhe_mark_list.append("% SCRIPT MARK -- MOODLE")
mazhe_mark_list.append("% SCRIPT MARK -- INTRO SAGE")
mazhe_mark_list.append("% SCRIPT MARK -- FRIDO")
mazhe_mark_list.append("% SCRIPT MARK -- DÉVELOPPEMENTS POSSIBLES")
mazhe_mark_list.append("% SCRIPT MARK -- OUTILS MATHÉMATIQUES")
mazhe_mark_list.append("% SCRIPT MARK -- RESEARCH PART")
mazhe_mark_list.append("% SCRIPT MARK -- MATLAB")
mazhe_mark_list.append("% SCRIPT MARK -- EXERCICES")
mazhe_mark_list.append("% SCRIPT MARK -- CdI")
mazhe_mark_list.append("% SCRIPT MARK -- FINAL")
class set_filename(object):
def __init__(self, new_output_filename):
self.new_output_filename = new_output_filename
def __call__(self, medicament):
raise DeprecationWarning(
"Use myRequest.new_output_filename='foo.pdf' instead")
medicament.new_output_filename = self.new_output_filename
# TODO : use a partial object from the module 'functools'
# https://pymotw.com/3/functools/
class set_counter(object):
def __init__(self, counter_name, init_value, final_value):
self.counter_name = counter_name
self.init_value = init_value
self.final_value = final_value
def __call__(self, A):
"""
Changes the line
\setcounter{<counter_name>}{<init_value>}
into
\setcounter{<counter_name>}{<final_value>}
"""
u = "\setcounter{{{}}}{{{}}}".format(
self.counter_name, self.init_value)
S = A.replace(u, u.replace(
str(self.init_value), str(self.final_value)))
return S
class set_boolean(object):
def __init__(self, name, value):
self.name = name
self.value = value
def __call__(self, A):
r"""
Changes the line
\boolfalse{<name>}
into
\booltrue{<name>}
or the contrary
"""
true_line = r"\booltrue{{{}}}".format(self.name)
false_line = r"\boolfalse{{{}}}".format(self.name)
if self.value == "true":
S = A.replace(false_line, true_line)
elif self.value == "false":
S = A.replace(true_line, false_line)
else:
raise ValueError("You have to choose between 'true' of 'false'")
return S
class set_pdftitle(object):
def __init__(self, pdftitle):
self.pdftitle = pdftitle
def __call__(self, old_text):
r"""
Changes the line
\newcommand{\pdftitle}{<wathever>}
into
\newcommand{\pdftitle}{<pdftitle>}
@param {str} `old_text` the tex file which will be compiled
@return {str} the tex file (changed) to be compiled
"""
mark = r"\newcommand{\pdftitle}"
new_command_line = r"""\newcommand{\pdftitle}{PDFTITLE}""".replace(
"PDFTITLE", self.pdftitle)
new_text_list = []
for line in old_text.split("\n"):
if line.startswith(mark):
new_text_list.append(new_command_line)
else:
new_text_list.append(line)
return "\n".join(new_text_list)
set_isFrido = set_boolean("isFrido", "true")
def up_to_text(liste, text):
for i, l in enumerate(liste):
if l.startswith(text):
return i
def is_dirty(repo):
status = repo.status()
print("list done")
for filepath, flags in status.items():
if flags != pygit2.GIT_STATUS_CURRENT:
if flags != 16384:
return True
return False
def get_hexsha(repo):
commit = repo.revparse_single('HEAD')
return commit.id
def set_commit_hexsha(A):
repo = pygit2.Repository(os.getcwd())
hexsha = str(get_hexsha(repo))
if is_dirty(repo):
hexsha = hexsha+" -- and slightly more"
u = "\\newcommand{\GitCommitHexsha}{\info{missing information}}"
print(hexsha)
A = A.replace(u, u.replace("missing information", hexsha))
return A
def assert_MonCerveau_first(options):
"""
Check that the reference "MonCerveau" is the first one.
Make the check in the bbl file.
"""
filename = "Inter_frido-mazhe_pytex.bbl"
bbl_filename = options.bibliographie()
if not os.path.exists(bbl_filename):
print(f"Le fichier {bbl_filename} n'existe pas. C'est pas très normal. Si cela persiste à la prochaine compilation, posez-vous des questions.")
return None
bbl_content = open(bbl_filename).read()
bbl_first = bbl_content.find("bibitem")
bbl_second = bbl_content.find("bibitem", bbl_first+1)
text = bbl_content[bbl_first:bbl_second]
if not "MonCerveau" in text:
print("""Il semblerait que la référence bibliographique 'MonCerveau' ne soit pas la première. Il faut corriger ça. En effet, le lecteur doit savoir que lorsqu'il voit la référence [1], ça veut dire 'danger'.
Après modification, le plus simple est de supprimer le fichier {} et de relancer.
""".format(filename))
raise ValueError(
f"The reference 'MonCerveau' is not the first one. The first is one is: {text}")
def split_toc(name, n):
"""
Rewrites the TOC file with adding "(Vol i)" to the chapter name.
With i being the number of the volume in which the
chapter should appears.
@param n : the number of volumes
@param name (string) the name of the document we are speaking about
@return : a function which makes the work
The parameter 'name' serves to distinguish 'frido' from 'book' when
creating the pathname of the toc file.
"""
def _split_doc(options=None):
cwd = os.getcwd()
sys.path.append(os.path.join(cwd, "python"))
toc_filename = f"Inter_{name}-mazhe_pytex.toc"
pdf_filename = f"Inter_{name}-mazhe_pytex.pdf"
toc_path = Path('.').resolve() / toc_filename
pdf_path = Path('.').resolve() / pdf_filename
book = Book(toc_path, pdf_path)
book.rewrite_toc(n)
return _split_doc