-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathluna_linter.py
executable file
·1356 lines (1144 loc) · 43.6 KB
/
luna_linter.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
import argparse
import re
import os
import sys
import Levenshtein
from luna.translation_db import TranslationDb
from luna.constants import Constants
from luna.ruby_utils import RubyUtils
RubyUtils.ENABLE_PUA_CODES = True
class Color:
RED = '\033[31m'
GREEN = '\033[32m'
YELLOW = '\033[33m'
BLUE = '\033[34m'
MAGENTA = '\033[35m'
CYAN = '\033[36m'
ENDC = '\033[0m'
def __init__(self, color):
self.color = color
def __call__(self, text):
return f"{self.color}{text}{Color.ENDC}"
class LintResult:
def __init__(self, linter, scene_name, page, line, message):
self.linter = linter
self.filename = scene_name
self.page = page
self.line = line
self.message = message
def __repr__(self):
return (
f"{self.linter}: {self.filename}: {self.page}:\n"
f"\t\"{self.line}\"\n\t{self.message}"
)
def ignore_linter(linter_name, line_comment):
# No comment means no lint-off comment
if not line_comment:
return False
# Does the comment contain a lint-off pragma for this linter?
comment = line_comment.lower()
search = f'lint-off:{linter_name}'.lower()
return search in comment
class LintNameMisspellings:
BASE_NAMES = [
"Ahnenerbe",
"Akiha",
"Andou",
"Andrei",
"Aoko",
"Arach",
"Arcueid",
"Argaleon",
"Arihiko",
"Arima",
"Bestino",
"Brunestud",
"Ciel",
"Gallo",
"Godbivouac",
"Gouto",
"Granfatima",
"Hisui",
"Inui",
"Karius",
"Kiara",
"Kisshouin",
"Kohaku",
"Makihisa",
"Mario",
"Messiaen",
"Mio",
"Narbareck",
"Noel",
# "Noi", Doesn't come up much and generates too many false positives
"Roa",
"Saiki",
"Satsuki",
"Seonator",
"Shiki",
"Tohno",
"Vlov",
"Yumizuka",
]
TYPO_EXCLUDE = set([
'Miss', # Triggers false positives on Mio
])
NAME_THRESH = 2
def __init__(self):
# Construct name permutations
# Note that ' is stripped, so posessive and plurals are both covered by
# the postfix 's'
self._names = set()
self._name_freqmaps = {}
for name in self.BASE_NAMES:
self._names.add(name)
self._names.add(name + 's')
for name in self._names:
self._name_freqmaps[name] = self.make_freqmap(name)
def make_freqmap(self, word):
ret = {}
for char in word:
ret[char] = ret.get(char, 0) + 1
return ret
def depunctuate(self, word):
return ''.join([
c for c in word
if (c >= 'A' and c <= 'Z')
or (c >= 'a' and c <= 'z')
])
def multisplit(self, string, sep_list):
ret = []
acc = ""
for c in string:
if c in sep_list:
ret.append(acc)
acc = ""
else:
acc += c
if acc:
ret.append(acc)
return ret
def __call__(self, db, scene_name, pages):
errors = []
for page in pages:
for line, comment in page:
if not line:
continue
if ignore_linter(self.__class__.__name__, comment):
continue
line = RubyUtils.apply_control_codes(line)
for raw_word in self.multisplit(line, ' -―\n'):
word = self.depunctuate(raw_word)
# If it's a correct spelling, skip
if word in self._names or word in self.TYPO_EXCLUDE:
continue
# Check edit distance
for name in self._names:
if Levenshtein.distance(word, name) < self.NAME_THRESH:
errors.append(LintResult(
self.__class__.__name__,
scene_name,
page[0],
line,
f"Is '{word}' supposed to be '{name}'"
))
# Check transpositions
word_freqmap = self.make_freqmap(word)
for name, freqmap in self._name_freqmaps.items():
if word_freqmap == freqmap:
errors.append(LintResult(
self.__class__.__name__,
scene_name,
page[0],
line,
f"Is '{word}' supposed to be '{name}'"
))
return errors
class LintAmericanSpelling:
BRIT_TO_YANK = {
'absent-mindedly': 'absentmindedly',
'absentminded': 'absent-minded',
'afterwards': 'afterward',
'ageing': 'aging',
'anaesthesia': 'anesthesia',
'anaesthetics': 'anesthetics',
'anyways': 'anyway',
'apologise': 'apologize',
'apologised': 'apologized',
'apologising': 'apologizing',
'behaviour': 'behavior',
'behaviours': 'behaviors',
'cafe': 'café',
'cancelled': 'canceled',
'cancelling': 'canceling',
'civilisation': 'civilization',
'cliche': 'cliché',
'colour': 'color',
'colours': 'colors',
'counselling': 'counseling',
'counsellor': 'counselor',
'defence': 'defense',
'defenceless': 'defenseless',
'defences': 'defenses',
'demeanour': 'demeanor',
'endeavour': 'endeavor',
'favour': 'favor',
'favourable': 'favorable',
'favourite': 'favorite',
'focussed': 'focused',
'focussing': 'focusing',
'forwards': 'forward',
'fulfil': 'fulfill',
'fulfilment': 'fulfillment',
'furore': 'furor',
'grey': 'gray',
'grovelling': 'groveling',
'hand-made': 'handmade',
'harbour': 'harbor',
'honour': 'honor',
'honours': 'honors',
'jeez': 'geez',
'judgement': 'judgment',
'labelled': 'labeled',
'laboured': 'labored',
'leaped': 'leapt', # Exception since leaped looks dumb
'levelled': 'leveled',
'licence': 'license',
'licence': 'license',
'lifeform': 'life-form',
'light-hearted': 'lighthearted',
'lightheaded': 'light-headed',
'lightheadedness': 'light-headedness',
'marvelled': 'marveled',
'marvelling': 'marveling',
'mesmerised': 'mesmerized',
'miniscule': 'minuscule',
'mobile phone': 'cell phone',
'modelled': 'modeled',
'naive': 'naïve',
'naivete': 'naïveté',
'neighbourhood': 'neighborhood',
'neighbouring': 'neighboring',
'neighbouring': 'neighboring',
'offence': 'offense',
'realise': 'realize',
'realising': 'realizing',
'revelled': 'reveled',
'rumour': 'rumor',
'rumours': 'rumors',
'saviour': 'savior',
'self-defence': 'self-defense',
'self-defence': 'self-defense',
'shrivelling': 'shriveling',
'signalling': 'signaling',
'skilful': 'skillful',
'skilfully': 'skillfully',
'speciality': 'specialty',
'spiralling': 'spiraling',
'street light': 'streetlight',
'theatre': 'theater',
'towards': 'toward',
'travelled': 'traveled',
'travelling': 'traveling',
'unravelling': 'unraveling',
'washroom': 'bathroom',
'woah': 'whoa',
}
def strip_irrelevant_punct(self, word):
return ''.join([
c for c in word
if (c >= 'A' and c <= 'Z')
or (c >= 'a' and c <= 'z')
or c in '-'
])
def __call__(self, db, scene_name, pages):
errors = []
for page in pages:
for line, comment in page:
if not line:
continue
if ignore_linter(self.__class__.__name__, comment):
continue
for raw_word in line.split(' '):
word = self.strip_irrelevant_punct(raw_word)
if word.lower() in self.BRIT_TO_YANK:
subs = self.BRIT_TO_YANK[word.lower()]
errors.append(LintResult(
self.__class__.__name__,
scene_name,
page[0],
line,
f"Replace '{word}' with '{subs}'"
))
return errors
class LintEmDashes:
# Dangling interruptions are 3x CJK dash
# Em-dashes within sentences are 2x CJK dash
CJK_DASH = '―'
PUNCTUATION = set("\"\' .―")
def __call__(self, db, scene_name, pages):
errors = []
# Grab the actual scripting for this scene so we can detect glue cases
script_cmds = db.lines_for_scene(scene_name)
cmd_idx = 0
while cmd_idx < len(script_cmds):
# Fetch the translation
page_number = script_cmds[cmd_idx].page_number
line = db.tl_line_for_cmd(script_cmds[cmd_idx])
line_text = line.en_text or ''
cmd_idx += 1
# Continue to append any subsequent cmds if they are glued
lint_off = ignore_linter(self.__class__.__name__, line.comment)
while cmd_idx < len(script_cmds) and script_cmds[cmd_idx].is_glued:
line = db.tl_line_for_cmd(script_cmds[cmd_idx])
lint_off = (
lint_off or
ignore_linter(self.__class__.__name__, line.comment)
)
line_text += line.en_text or ''
cmd_idx += 1
# If any of the included lines has a lint-off, skip
if lint_off:
continue
# Save a copy of the original text for messages
raw_line_text = line_text
# If this line isn't translated, we can't lint properly
if not line_text:
continue
# Does this line not contain any u2015?
if self.CJK_DASH not in line_text:
continue
# Strip out any game engine control codes / newlines
line_text = re.sub('%{[\w/]*}', '', line_text)
line_text = re.sub('@.', '', line_text)
line_text = re.sub('\n', ' ', line_text)
# Strip leading spaces since it's valid to have padded triple
# dash lead-ins, as well as any trailing spaces since lines can't
# have trailing whitespace
line_text = line_text.strip()
# Does it consist of _only_ dashes or punctuation?
chars = set(line_text)
if chars.difference(self.PUNCTUATION) == set():
continue
# Strip out any quotes and exclamation marks from this text because
# leading/trailing dashes inside quotes / ?! still count
line_text = ''.join([c for c in line_text if c not in set("\"'?!")])
# If it ends with one, check how many it ends with
# If it _does_ contain dashes, filter them into groups of inter-text
# and post-text
ending_dash_count = 0
if line_text.endswith(self.CJK_DASH):
for i in range(len(line_text), 0, -1):
if line_text[i - 1] == self.CJK_DASH:
ending_dash_count += 1
else:
break
if ending_dash_count != 3:
errors.append(LintResult(
self.__class__.__name__,
scene_name,
page_number,
raw_line_text,
"Line should end with 3x CJK dash, not "
f"'{line_text[-ending_dash_count:]}'"
))
# Now do the same again for starting dashes
starting_dash_count = 0
if line_text.startswith(self.CJK_DASH):
for i in range(len(line_text)):
if line_text[i] == self.CJK_DASH:
starting_dash_count += 1
else:
break
if starting_dash_count != 3:
errors.append(LintResult(
self.__class__.__name__,
scene_name,
page_number,
raw_line_text,
"Line should start with 3x CJK dash, not "
f"'{raw_line_text[:starting_dash_count]}'"
))
# Snip those off so we con't count em again
remaining_text = line_text
if ending_dash_count:
remaining_text = remaining_text[:-ending_dash_count]
if starting_dash_count:
remaining_text = remaining_text[starting_dash_count:]
# Now split that text into any other groups of CJK dashes that exist
dash_groups = []
acc = ''
for c in remaining_text:
if c == self.CJK_DASH:
# Beginning of new dash group
acc += c
continue
# If this ends the group, is the group a 2x?
if acc and len(acc) != 2:
errors.append(LintResult(
self.__class__.__name__,
scene_name,
page_number,
raw_line_text,
"Em-dashes should be represented as 2x CJK dash, "
f"not {len(acc)}"
))
acc = ''
return errors
class LintBannedPhrases:
# Map of (search, case_sensitive) -> replace
BANNED_PHRASES = {
('curry bread', False): 'curry bun',
('head to head', False): 'head-to-head',
('pile driver', False): 'pile bunker',
('piledriver', False): 'pile bunker',
('pile-driver', False): 'pile bunker',
('white woman', False): 'woman in white',
('white avatar', False): 'avatar in white',
('blonde boy', False): 'blond boy',
('face to face', False): 'face-to-face',
('hard-headed', False): 'hardheaded',
('sub-par', False): 'subpar',
('get-up', False): 'getup',
('cold-hearted', False): 'coldhearted',
('sugar coat', False): 'sugarcoat',
('after-effects', False): 'aftereffects',
('off-beat', False): 'offbeat',
('brand new', False): 'brand-new',
('shockwave', False): 'shock wave',
# Need to handle sentence start explicitly on these since we're case-sen
('what on Earth', True): 'what on earth',
('What on Earth', True): 'What on earth',
('how on Earth', True): 'how on earth',
('How on Earth', True): 'How on earth',
('down to Earth', True): 'down to earth',
('Down to Earth', True): 'Down to earth',
# Lore terms that must be capitalized
('acting presbyter', True): 'Acting Presbyter',
('baptismal rite', True): 'Baptismal Rite',
('black key', True): 'Black Key',
('black keys', True): 'Black Keys',
('burial agency', True): 'Burial Agency',
('conceptual weapon', True): 'Conceptual Weapon',
('dead apostle', True): 'Dead Apostle',
(' ether ', True): 'Ether', # Too fragmentary to be effective
('executor', True): 'Executor',
('Grand Magecraft', True): 'grand magecraft',
('hemonomic principle', True): 'Hemonomic Principle',
('holy church', True): 'Holy Church',
('inversion impulse', True): 'Inversion Impulse',
('lifescale', True): 'Lifescale',
('mages association', True): 'Mages Association',
('magic circuit', True): 'Magic Circuit',
('marble phantasm', True): 'Marble Phantasm',
('mystic code ', True): 'Mystic Code',
('mystic eyes', True): 'Mystic Eyes',
('nightkin', True): 'Nightkin',
('numeromancy', True): 'Numeromancy',
('plating effect', True): 'Plating Effect',
('sacrament assembly', True): 'Sacrament Assembly',
('scriptural weapon', True): 'Scriptural Weapon',
('scriptural weapons', True): 'Scriptural Weapons',
('suzerain', True): 'Suzerain',
('true ancestor', True): 'True Ancestor',
# 南口 / 北口 are really more like areas than points in space.
# They should be blended more fluently with the sentence.
('north gate', False): 'plaza north of the station, etc.',
('south gate', False): 'plaza south of the station, etc.',
# Uncomment when everything else has been merged down
('...!', False): 'No ellipses before exclamation marks.',
('...?!', False): 'No ellipses before exclamation marks.',
}
def __call__(self, db, scene_name, pages):
errors = []
for page in pages:
for line, comment in page:
if not line:
continue
if ignore_linter(self.__class__.__name__, comment):
continue
lower_line = line.lower()
for findspec, replace in self.BANNED_PHRASES.items():
needle, case_sensitive = findspec
if needle in (line if case_sensitive else lower_line):
errors.append(LintResult(
self.__class__.__name__,
scene_name,
page[0],
line,
f"Replace '{needle}' with '{replace}'"
))
return errors
class LintInterrobang:
def __call__(self, db, scene_name, pages):
errors = []
for page in pages:
for line, comment in page:
if not line:
continue
if ignore_linter(self.__class__.__name__, comment):
continue
if '!?' in line:
errors.append(LintResult(
self.__class__.__name__,
scene_name,
page[0],
line,
"Replace '!?' with '?!'"
))
return errors
class LintUnclosedQuotes:
def __call__(self, db, scene_name, pages):
# For each page, just do a dumb check that the quote count is matched
errors = []
for page in pages:
# If any of the lines aren't actually translated, just abort
if any([line is None for line, comment in page]):
continue
# If any of the lines in the page contain a lint-off pragma, skip
lint_ignored = any([
ignore_linter(self.__class__.__name__, comment)
for (line, comment) in page
])
if lint_ignored:
continue
raw_text = [line for line, _comment in page if line]
quote_count = len([c for c in ''.join(raw_text) if c == '"'])
if quote_count & 1:
errors.append(LintResult(
self.__class__.__name__,
scene_name,
page[0],
'\n'.join(f"\t> {line}" for line, _comment in page),
f"Found odd number of quotes ({quote_count})"
))
return errors
class LintBrokenFormatting:
def __call__(self, db, scene_name, pages):
errors = []
for page in pages:
for line, comment in page:
if not line:
continue
if ignore_linter(self.__class__.__name__, comment):
continue
try:
RubyUtils.apply_control_codes(
line, enable_asserts=True)
except AssertionError as e:
errors.append(LintResult(
self.__class__.__name__,
scene_name,
page[0],
line,
e.args[0]
))
return errors
class LintChoices:
# Full line is 55 chars, subtract 2 for choice number
MAX_CHOICE_LEN = 53
def __call__(self, db, scene_name, pages):
errors = []
# Get the actual script cmds for this one
script_cmds = db.lines_for_scene(scene_name)
# Filter for choices
choice_cmds = [cmd for cmd in script_cmds if cmd.is_choice]
# Scan for lint breakers
for cmd in choice_cmds:
line = db.tl_line_for_cmd(cmd)
if not line.en_text:
continue
# No leading space?
if line.en_text[0] != ' ':
errors.append(LintResult(
self.__class__.__name__,
scene_name,
cmd.page_number,
line.en_text,
"Choice text must begin with leading space"
))
# Starts with an ellipsis?
if line.en_text.strip().startswith('...'):
errors.append(LintResult(
self.__class__.__name__,
scene_name,
cmd.page_number,
line.en_text,
"Choice text should not begin with ellipsis"
))
# Too long?
# Auto-ignore this one if it's the last choice in the scene, in
# which case it doesn't overflow onto anything
is_last_choice = cmd == choice_cmds[-1]
line_len = RubyUtils.noruby_len(
RubyUtils.apply_control_codes(line.en_text))
is_overlong = line_len > self.MAX_CHOICE_LEN
if not is_last_choice and is_overlong:
errors.append(LintResult(
self.__class__.__name__,
scene_name,
cmd.page_number,
line.en_text,
f"Choice too long, must be < {self.MAX_CHOICE_LEN} chars"
))
return errors
class LintPageOverflow:
MAX_LINES_PER_PAGE = 12
def __init__(self, db):
# Pregenerate text map so we don't incur it on every __call__
self._text_map = db.generate_linebroken_text_map()
def __call__(self, db, scene_name, _pages):
errors = []
# Ignore the orphan line file
if scene_name == 'ORPHANED_LINES':
return errors
# Get the script cmds
script_cmds = db.lines_for_scene(scene_name)
# Paginate
page_cmds = paginate(script_cmds)
# For each page, check if when linebroken it ends up too long
for page in page_cmds:
# Do we skip this page?
is_lint_ignored = any([
ignore_linter(self.__class__.__name__,
db.tl_line_for_cmd(cmd).comment)
for cmd in page
])
if is_lint_ignored:
continue
# Consolidate the page text into one string
page_text = ""
for cmd in page:
# Fetch the matching string
line = self._text_map[cmd.offset]
# Remove any game control codes from the front of the string
while line[0] == '@':
line = line[2:]
# For glued lines, erase the trailing \n on the line before
if cmd.is_glued:
page_text = page_text[:-2] + line
else:
page_text += line
# Don't count the trailing newline
page_text = page_text.rstrip()
page_lines = len(page_text.split("\n"))
if page_lines > self.MAX_LINES_PER_PAGE:
errors.append(LintResult(
self.__class__.__name__,
scene_name,
page[0].page_number,
page_text,
f"Page too long (is {page_lines}, "
f"max is {self.MAX_LINES_PER_PAGE})"
))
return errors
class LintTranslationHoles:
"""
If a file is _mostly_ translated but has some untranslated strings in it
it's possible they got skipped by accident.
"""
LIKELY_TRANSLATED_THRESH = 0.8
def __call__(self, db, scene_name, pages):
# What % of lines in this scene are TL'd?
total_tl_count = 0
total_line_count = 0
for page in pages:
for line, comment in page:
total_line_count += 1
if line:
total_tl_count += 1
# If this file doesn't look translated, or is 100% tl'd, just return
translation_ratio = total_tl_count / total_line_count
mostly_translated = translation_ratio > self.LIKELY_TRANSLATED_THRESH
fully_translated = total_tl_count == total_line_count
if not mostly_translated or fully_translated:
return []
# Go accumulate the lines that aren't TLd, have to actually re-fetch
# from DB to get the line IDs
script_cmds = db.lines_for_scene(scene_name)
untranslated_cmds = []
for cmd in script_cmds:
line = db.tl_line_for_cmd(cmd)
if not line.en_text:
untranslated_cmds.append(cmd)
line_report = (
f'Scene is {translation_ratio*100:.1f}% translated, but has '
f'{total_line_count - total_tl_count} missing lines'
)
for cmd in untranslated_cmds:
if line_report:
line_report += "\n"
line_report += f"\t Sha: {cmd.jp_hash}, Offset: {cmd.offset}"
# If this file is mostly translated but has holes, warn about it
return [
LintResult(
self.__class__.__name__,
scene_name,
None,
f'Scene {scene_name} has translation holes',
line_report
)
]
class LintDanglingCommas:
def __call__(self, db, scene_name, pages):
# QA has a lot of false positives for this, so maybe ignore for now
if scene_name.startswith("QA_"):
return []
# If any of the lines aren't actually translated, just abort
for page in pages:
if any([line is None for line, comment in page]):
return []
# Check to see if the final line of the page ends in a , (or ,")
errors = []
for page in pages:
last_line, last_comment = page[-1]
if ignore_linter(self.__class__.__name__, last_comment):
continue
if last_line.endswith(",") or last_line.endswith(",\""):
errors.append(LintResult(
self.__class__.__name__,
scene_name,
page[0],
last_line,
"Final line ends in trailing ',', replace with CJK dashes '―――'"
))
return errors
class LintConsistency:
def __init__(self):
# Compile regex for consistency check pragmas
self._regex = re.compile(r'LintConsistency:(\d+)')
def __call__(self, db, scene_name, pages):
errors = []
for page in pages:
for line, comment in page:
if not line or not comment:
continue
# Check each referenced consistency point is in fact consistent
for offset in self._regex.findall(comment):
other_jp_hash = db.tl_line_for_offset(int(offset))
# Need to make sure that we handle overrides correctly,
# since fetching those by hash won't work
other_line = db.tl_override_for_offset(int(offset)) or \
db.tl_line_with_hash(other_jp_hash)
if other_line.en_text != line:
errors.append(LintResult(
self.__class__.__name__,
scene_name,
page[0],
line,
f"Line not consistent with offset {offset}:\n"
f"\t{other_line.en_text}"
))
return errors
class LintStartingEllipsis:
PUNCTUATION = set("\"'.?!")
def __call__(self, db, scene_name, pages):
errors = []
for page in pages:
for line, comment in page:
if not line:
continue
if ignore_linter(self.__class__.__name__, comment):
continue
# If there's no ellipsis in this line, skip it
if '...' not in line:
continue
# If it doesn't begin with an ellipsis (potentially in quotes)
# then skip it
starts_with_ellipsis = any([
line.startswith('...'),
line.startswith('"...'),
line.startswith('\'...'),
])
if not starts_with_ellipsis:
continue
# If this line starts with an ellipsis, but consists of nothing
#_more_ than an ellipsis, let it slide
if set(line).difference(self.PUNCTUATION) == set():
continue
# If we got this far, it's a lint error
errors.append(LintResult(
self.__class__.__name__,
scene_name,
page[0],
line,
"Lines should not start with ellipses"
))
return errors
class LintEllipses:
def __call__(self, db, scene_name, pages):
errors = []
for page in pages:
for line, comment in page:
if not line:
continue
if ignore_linter(self.__class__.__name__, comment):
continue
# Test lines for non-multiple-of-three periods
consecutive_dots = 0
for c in line:
if c == '.':
consecutive_dots += 1
if c != '.':
if consecutive_dots > 1 and consecutive_dots % 3 != 0:
errors.append(LintResult(
self.__class__.__name__,
scene_name,
page[0],
line,
"Non-multiple-of-three ellipsis"
))
consecutive_dots = 0
# Handle end-of-line case
if consecutive_dots > 1 and consecutive_dots % 3 != 0:
errors.append(LintResult(
self.__class__.__name__,
scene_name,
page[0],
line,
"Non-multiple-of-three ellipsis"
))
return errors
class LintVerbotenUnicode:
VERBOTEN = {
' ': ' ',
'…': '...',
'“': '"',
'”': '"',
'’': '\'',
'、': ',',
'!': '!',
'?': '?',
}
def __call__(self, db, scene_name, pages):
errors = []
for page in pages:
for line, comment in page:
if not line:
continue
if ignore_linter(self.__class__.__name__, comment):
continue
for find, replace in self.VERBOTEN.items():
if find in line:
errors.append(LintResult(
self.__class__.__name__,
scene_name,
page[0],
line,
f"Replace '{find}' with '{replace}'"
))
return errors
class LintTimeFormat:
def __call__(self, db, scene_name, pages):
errors = []
for page in pages:
for line, comment in page:
if not line:
continue
if ignore_linter(self.__class__.__name__, comment):
continue
# Does this line have a colon-delimited time in it?
matches = re.findall(r"(\d+:\d\d)", line)
if not matches: