-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathbuild_page_seg_modes.py
53 lines (43 loc) · 1.5 KB
/
build_page_seg_modes.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
import sys
from itertools import islice
class PageSegMode:
def __init__(self, name, comment):
self.name = name
self.comment = comment
def name_as_enum(self):
return ''.join(n.capitalize() for n in self.name.split("_"))
def name_as_variable(self):
return 'tesseract_sys::TessPageSegMode_' + self.name
name = None
page_seg_modes = []
i = 0;
for line in islice(sys.stdin, 1, None):
if i == 0:
name = line.rstrip('\n')
elif i == 1:
comment = line.rstrip('\n')
page_seg_mode = PageSegMode(name, comment)
page_seg_modes.append(page_seg_mode)
i = (i + 1) % 3
print("// ⚠️ This file is generated")
print("// ⚠️ Regenerate with `make src/page_seg_mode.rs`")
print()
print("use tesseract_sys::TessPageSegMode;")
print()
print("/// Enum representing different PageSegMode options accepted by Tesseract")
print("#[derive(Debug, Clone, Copy, PartialEq, Eq)]")
print("pub enum PageSegMode {")
for page_seg_mode in page_seg_modes:
print(f" /// {page_seg_mode.comment}")
print(f" {page_seg_mode.name_as_enum()},",)
print("}")
print()
print("impl PageSegMode {")
print(" /// Get the page-seg-mode's value as used by Tesseract")
print(" pub fn as_tess_page_seg_mode(&self) -> TessPageSegMode {")
print(" match self {")
for page_seg_mode in page_seg_modes:
print(f" PageSegMode::{page_seg_mode.name_as_enum()} => {page_seg_mode.name_as_variable()},")
print(" }")
print(" }")
print("}")