-
Notifications
You must be signed in to change notification settings - Fork 10
/
lang_sync
executable file
·1308 lines (1010 loc) · 38.5 KB
/
lang_sync
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 base64
import codecs
import csv
import getopt
import getpass
import io
import os
import re
import sys
import urllib.request
KNOWN_PROJECT_TYPES = ["game-script", "newgrf", "openttd"]
PROJECT_TYPES_TEXT = ['"{}"'.format(pt) for pt in KNOWN_PROJECT_TYPES]
PROJECT_TYPES_TEXT = ", ".join(PROJECT_TYPES_TEXT[:-1]) + " and " + PROJECT_TYPES_TEXT[-1]
hex_pat = re.compile("0x([0-9A-Fa-f][0-9A-Fa-f]?)$")
# Configuration.
project_pat = re.compile("[-A-Za-z0-9_]+$")
def check_project_name(name):
"""
Check whether L{name} can be a valid project name.
@param name: Name to verify.
@type name: C{str}
@return: Whether the given name is valid.
@rtype: C{bool}
"""
return project_pat.match(name.strip())
def check_human_name(name):
"""
Check whether L{name} can be a valid human project name.
@param name: Name to verify.
@type name: C{str}
@return: Whether the given name is valid.
@rtype: C{bool}
"""
return len(name.strip()) != 0
def check_project_url(url):
"""
Check whether the given L{url} is valid.
@param url: Url of the project, may be empty.
@type url: C{str}
@return: Whether the provided url is valid.
@rtype: C{bool}
"""
if url == "":
return True
if url.startswith("http://"):
return True
if url.startswith("https://"):
return True
return False
class UserConfig:
"""
Configuration and actions to do by the program.
@ivar filename: Name of the file containing authentication information, if available.
@type filename: C{str} or C{None}
@ivar user: User name to use, if available.
@type user: C{str} or C{None}
@ivar passwd: Password to use, if available.
@type passwd: C{str} or C{None}
@ivar interactive: Can the program ask for the password interactively? (Default: C{True}).
@type interactive: C{bool}
@ivar base_url: Base url of the authentication realm (the scheme + host), if available.
@type base_url: C{str} or C{None}
@ivar project: Project identifier of Eints, if available.
@type project: C{str} or C{None}
@ivar project_type: Type of the project.
@type project_type: C{str}
@ivar project_desc: Project description, if available.
@type project_desc: C{str} or C{None}
@ivar project_url: Project URL, may be empty.
@type project_url: C{str}
@ivar lang_dir: Directory containing the language files relative to the working directory.
@type lang_dir: C{str}
@ivar unstable_lang_dir: Directory containing the unstable language files relative to the working directory.
@type unstable_lang_dir: C{str}
@ivar lang_ext: Extension used by language files.
@type lang_ext: C{str}
@ivar base_lang: Isocode or grf language id of the base language, if available.
@type base_lang: C{str} or C{None}
@ivar credits_file: If available, path to a file for writing credits for changes in the language files.
@type credits_file: C{str} or C{None}
@ivar verbose: Whether to be verbose in what actions the program performs.
@type verbose: C{bool}
@ivar no_write: Whether to skip writing data onto the disk.
@type no_write: C{bool}
@ivar operations: Upload and download actions to perform.
@type operations: C{list} of C{str}
"""
def __init__(self):
self.filename = None
self.user = None
self.passwd = None
self.interactive = True
self.base_url = None
self.base_lang = None
self.credits_file = None
self.project = None
self.project_type = "**unset**"
self.project_desc = None
self.project_url = ""
self.lang_dir = "lang"
self.unstable_lang_dir = None
self.lang_ext = ".lng"
self.verbose = False
self.no_write = False
self.operations = []
def check(self):
"""
Check whether enough information was provided to the program. If not,
fail with an error message.
"""
if self.base_url is None:
print("Error: Missing base url to the Eints server (add it with --base-url=URL).")
sys.exit(1)
if self.project is None:
print("Error: Missing Eints project identifier (add it with --project=PROJ_ID).")
sys.exit(1)
if not check_project_name(self.project):
print('Error: Project identifier "{}" is not valid.'.format(self.project))
sys.exit(1)
if self.project_desc is not None and not check_human_name(self.project_desc):
print('Error: Project description "{}" is not valid.'.format(self.project_desc))
sys.exit(1)
if not check_project_url(self.project_url):
print('Error: Project url "{}" is not valid.'.format(self.project_url))
sys.exit(1)
if len(self.operations) == 0:
print("Warning: No operations to perform, finishing now.")
sys.exit(0)
user_cfg = None #: Global user configuration.
# Network access code.
def setup_auth():
"""
Setup the authentication for connecting to Eints.
"""
global user_cfg
user, passwd = None, None
# Read the file if provided.
if user_cfg.filename is not None:
if not os.path.isfile(user_cfg.filename):
print('Error: File "{}" does not exist or is not a file.'.format(user_cfg.filename))
sys.exit(1)
with open(user_cfg.filename, "r", encoding="utf-8") as handle:
for line in handle:
line = line.strip()
# Check for a well-known prefix.
if line.startswith("username"):
prefix = "u"
line = line[8:].lstrip()
elif line.startswith("user"):
prefix = "u"
line = line[4:].lstrip()
elif line.startswith("password"):
prefix = "p"
line = line[8:].lstrip()
elif line.startswith("pwd"):
prefix = "p"
line = line[3:].lstrip()
else:
continue # No match for user or password
# Silently drop the : or = delimiter.
if line[0] in ":=":
line = line[1:].lstrip()
if len(line) > 0 and line[0] in ('"', "'") and line[0] == line[-1]:
line = line[1:-1]
if prefix == "u":
user = line
else:
passwd = line
# Close of the file is implicit.
if user is None:
user = user_cfg.user
if user is None:
user = getpass.getuser()
if passwd is None:
passwd = user_cfg.passwd
if passwd is None and user_cfg.interactive:
passwd = getpass.getpass()
if user is not None and passwd is not None:
class ForceHTTPBasicAuthHandler(urllib.request.AbstractBasicAuthHandler, urllib.request.BaseHandler):
auth_header = "Authorization"
# On every request, add an Authorization header
def http_request(self, req):
user, passwd = self.passwd.find_user_password(None, req.full_url)
credentials = "{0}:{1}".format(user, passwd).encode()
auth_str = base64.standard_b64encode(credentials).decode()
req.add_unredirected_header("Authorization", "Basic {}".format(auth_str.strip()))
return req
https_request = http_request
# If both a user and a password, we can usefully setup authentication.
pwd_manager = urllib.request.HTTPPasswordMgr()
pwd_manager.add_password(None, user_cfg.base_url, user, passwd)
auth_handler = ForceHTTPBasicAuthHandler(pwd_manager)
opener = urllib.request.build_opener(auth_handler)
urllib.request.install_opener(opener)
if user_cfg.verbose:
print("Installed authentication.")
print()
return
if user_cfg.verbose:
text = "Skipped installing authentication"
if user is None:
text = text + ", user was not set"
if passwd is None:
text = text + ", password was not set"
text = text + "."
print(text)
print()
def build_url(suffix):
"""
Extend the base url with the provided suffix.
@param suffix: Path to retrieve from, at L{UserConfig.base_url}
@type suffix: C{str}
"""
global user_cfg
assert suffix[0] == "/"
if user_cfg.base_url[-1] == "/":
return user_cfg.base_url + suffix[1:]
else:
return user_cfg.base_url + suffix
def get_data(url):
"""
Read and return the data read from the L{url}.
@param url: Url for the file to retrieve.
@type url: C{str}
@return: Data returned by the L{url} request, if all went well.
@rtype: C{str} or C{None}
"""
try:
with urllib.request.urlopen(url) as resp:
if resp.status != 200:
return None
ctype = resp.getheader("Content-Type")
if ctype is None or ctype != "text/plain; charset=UTF-8":
return None
data = resp.read().decode("utf-8")
return data
except Exception:
print("Error: Failing URL: {}".format(url))
return None
def post_form(url, form_data):
"""
Post the form.
@param url: Url for the file to retrieve.
@type url: C{str}
@param form_data: Form data.
@type form_data: C{dict} of C{str} to C{str}
@return: Whether the from was posted correctly.
@rtype: C{bool}
"""
headers = {"Content-Type": " application/x-www-form-urlencoded; charset=utf-8"}
form = urllib.parse.urlencode(form_data).encode("utf-8")
req = urllib.request.Request(url, form, headers)
# Perform the request, and read the returned data.
with urllib.request.urlopen(req) as resp:
if resp.status != 200:
return False
resp.read()
return True
def post_form_upload(url, form_data, form_fentry, form_fname, fdata):
"""
Post the form with the uploaded files.
@param form_data: Form data.
@type form_data: C{dict} of C{str} to C{str}
@param form_fentry: Form field of the file entry.
@type form_fentry: C{str}
@param form_fname: Name of the file (mostly for extension).
@type form_fname: C{str}
@param fdata: File data.
@type files: C{str}
@return: Whether posting was successful.
@rtype: C{bool}
"""
boundary = "TheBoundaryOfFormDataEntries"
data = []
for field, value in form_data.items():
data.append("--" + boundary)
data.append('Content-Disposition: form-data; name="{}"'.format(field))
data.append("")
data.append(value)
data.append("--" + boundary)
data.append('Content-Disposition: form-data; name="{}"; filename="{}"'.format(form_fentry, form_fname))
data.append("Content-Type: text/plain")
data.append("")
data.append(fdata)
if fdata[-1] != "\n":
data.append("") # Add a \n ourselves
data.append("--" + boundary + "--\n")
# Send the data.
headers = {"Content-Type": " multipart/form-data; charset=utf-8; boundary={}".format(boundary)}
data = "\n".join(data).encode("utf-8")
req = urllib.request.Request(url, data, headers)
# Perform the request, and return the read data.
with urllib.request.urlopen(req) as resp:
if resp.status != 200 or resp.geturl().find("Successfully") < 0:
return False
resp.read()
return True
# Remote language files.
class EintsLanguageInfo:
"""
Language information received from Eints.
@ivar isocode: Isocode of the of the language.
@type isocode: C{str}
@ivar parent_lang: Isocode of the language followed by this language, if it exists.
@type parent_lang: C{str} or C{None} if it is the root language
@ivar last_change: Last change of the language, if it exists.
@type last_change: C{None} or C{str}
"""
def __init__(self, isocode, name, parent_lang, last_change):
self.isocode = isocode
self.name = name
self.parent_lang = parent_lang
self.last_change = last_change
eints_files_cache = None
def get_eints_languages():
"""
Get the list of languages available in Eints.
@return: Available language information for the project as returned by Eints.
@rtype: C{list} of L{EintsLanguageInfo}
"""
global user_cfg, eints_files_cache
if eints_files_cache is not None:
return eints_files_cache
if user_cfg.verbose:
print("Getting available language files from Eints.")
result = []
data = get_data(build_url("/download-list/" + user_cfg.project))
if data is None:
print("Error: Could not download the list of project languages from Eints.")
sys.exit(1)
reader = csv.DictReader(io.StringIO(data), dialect="unix")
for line in reader:
isocode = line.get("isocode")
if isocode is None or len(isocode) == 0:
continue
name = line.get("name", isocode)
plang = line.get("base_isocode")
if plang is None:
continue
if len(plang) == 0:
plang = None
# last_change = line.get("changetime")
# XXX Parse and validate the last change date
el = EintsLanguageInfo(isocode, name, plang, None)
result.append(el)
eints_files_cache = result
if user_cfg.verbose:
if len(eints_files_cache) == 0:
print("No languages found in Eints.")
else:
print("Found remote language files:")
for el in eints_files_cache:
if el.parent_lang is None:
kind = "(base language)"
else:
kind = "(translation of {})".format(el.parent_lang)
print("\tisocode: {} {}".format(el.isocode, kind))
print()
return eints_files_cache
# Local language files.
class LocalLanguageInfo:
"""
Information about a local language file.
@ivar path_name: The path to the file.
@type path_name: C{str}
@ivar isocode: Isocode of the language,
@type isocode: C{str}
"""
def __init__(self, path_name, isocode):
self.path_name = path_name
self.isocode = isocode
utf8_bom = codecs.BOM_UTF8.decode("utf-8")
local_files_cache = None
def get_local_languages():
"""
Get the list of available languages at the local file system.
@return: Found language files.
@rtype: C{list} of L{LocalLanguageInfo}
"""
global user_cfg, local_files_cache
if local_files_cache is not None:
return local_files_cache
if user_cfg.verbose:
print("Getting available language files from the local file system.")
result = []
grflangid_pattern = re.compile("##grflangid\\s+0[xX]([0-9A-Fa-f][0-9A-Fa-f]?)\\s*$")
if not os.path.isdir(user_cfg.lang_dir):
print('Error: "{}" is not a directory. (Is the --lang-dir option correct?)'.format(user_cfg.lang_dir))
sys.exit(1)
if user_cfg.unstable_lang_dir is not None and not os.path.isdir(user_cfg.lang_dir):
print(
'Error: "{}" is not a directory. (Is the --unstable-lang-dir option correct?)'.format(
user_cfg.unstable_lang_dir
)
)
sys.exit(1)
searchpaths = [user_cfg.lang_dir]
if user_cfg.unstable_lang_dir is not None:
searchpaths.append(user_cfg.unstable_lang_dir)
for spath in searchpaths:
for fname in os.listdir(spath):
if not fname.endswith(user_cfg.lang_ext):
continue
path = os.path.join(spath, fname)
if user_cfg.verbose:
print('File "{}" matches extension'.format(path))
# Try reading the grflangid from the file.
grflangid = None
with open(path, "r", encoding="utf-8") as handle:
for line in handle:
if line.startswith(utf8_bom):
line = line[len(utf8_bom) :]
m = grflangid_pattern.match(line)
if m:
grflangid = int(m.group(1), 16)
break
# If not found, try to derive it from the filename.
if grflangid is None:
ldata = get_languagedata_by_filename(fname[: -len(user_cfg.lang_ext)])
else:
ldata = get_languagedata_by_grflangid(grflangid)
if ldata is not None:
result.append(LocalLanguageInfo(path, ldata.isocode))
continue
if user_cfg.verbose:
print('File "{}" matches extension, but cannot derive a language from it.'.format(path))
local_files_cache = result
if user_cfg.verbose:
if len(local_files_cache) == 0:
print("No languages found locally.")
else:
print("Found local language files:")
for ll in local_files_cache:
print("\tisocode: {}, path: {}".format(ll.isocode, ll.path_name))
print()
return local_files_cache
# Upload and download operations.
def perform_operation(op_type, lang_type, credits_handle):
"""
Perform a synchronization operation.
@param op_type: Type of operation, either C{"upload"}, C{"download"}, or C{"create"}.
@type op_type: C{str}
@param lang_type: Type of languages involved, either C{"base"}, C{"translations"}, or C{"project"}.
@type lang_type: C{str}
@param credits_handle: If available, output stream for writing credits of language file changes.
@type credits_handle: C{file} or C{None}
"""
global user_cfg
if op_type == "create":
assert lang_type == "project"
# Project creation operation.
if user_cfg.project_desc is None:
print("Error: Missing project description (add it with --project-desc).")
sys.exit(1)
perform_create_project()
return
# Upload/download operations below.
ll_files = get_local_languages()
el_files = get_eints_languages()
# Find out what the base language is.
blng = user_cfg.base_lang
if blng:
if hex_pat.match(blng):
blng = get_languagedata_by_grflangid(int(blng, 16))
else:
blng = get_languagedata_by_isocode(blng)
for el in el_files:
if el.parent_lang is None:
if blng is not None and blng.isocode != el.isocode:
text = "Error: Base languages do not match, --base-lang claims {} while remote claims {}"
print(text.format(blng.isocode, el.isocode))
sys.exit(1)
blng = get_languagedata_by_isocode(el.isocode)
break
if blng is None:
print("Error: No base language found (neither from Eints nor given with --base-lang)")
sys.exit(1)
# Filter languages.
path_base = None
if lang_type == "base":
ll_files = [ll for ll in ll_files if ll.isocode == blng.isocode]
el_files = [el for el in el_files if el.isocode == blng.isocode]
else:
path_base = next((ll.path_name for ll in ll_files if ll.isocode == blng.isocode), None)
ll_files = [ll for ll in ll_files if ll.isocode != blng.isocode]
el_files = [el for el in el_files if el.isocode != blng.isocode]
if op_type == "upload":
perform_upload(ll_files, lang_type == "base")
else:
perform_download(ll_files, el_files, credits_handle, path_base)
def perform_create_project():
"""
Create a new project in Eints.
"""
global user_cfg
assert user_cfg.project_desc is not None
if user_cfg.project_type not in KNOWN_PROJECT_TYPES:
print('Error: Project type "{}" is not known.'.format(user_cfg.project_type))
sys.exit(1)
fields = {"humanname": user_cfg.project_desc, "url": user_cfg.project_url}
url = build_url("/makeproject/" + user_cfg.project_type + "/" + user_cfg.project)
if user_cfg.no_write:
print('Skipped creation of project "{}".'.format(user_cfg.project))
else:
if not post_form(url, fields):
print('Project creation of "{}" failed.'.format(user_cfg.project))
sys.exit(1)
if user_cfg.verbose:
print('Created project "{}".'.format(user_cfg.project))
def perform_upload(ll_files, is_base):
"""
Perform upload of local files to Eints.
@param ll_files: Local files to upload.
@type ll_files: C{list} of L{LocalLanguageInfo}
@param is_base: Whether uploading the base language.
@type is_base: C{bool}
"""
if is_base:
blng_value = "on"
else:
blng_value = ""
for ll in ll_files:
with open(ll.path_name, "r", encoding="utf-8") as handle:
data = handle.read()
data = data.rstrip()
if len(data) == 0:
print('Warning: Skipping empty "{}".'.format(ll.path_name))
continue
fields = {"override": "", "base_language": blng_value}
fname = os.path.basename(ll.path_name)
if user_cfg.no_write:
if user_cfg.verbose:
print('Skipped upload of "{}"'.format(fname))
ok = True
else:
if user_cfg.verbose:
print('Uploaded "{}" to Eints'.format(fname))
url = build_url("/upload/" + user_cfg.project + "/" + ll.isocode)
ok = post_form_upload(url, fields, "langfile", fname, data)
if not ok:
print('Error: Failed to upload "{}".'.format(ll.path_name))
class LanguageData:
"""
Language information.
@ivar grflangid: NewGRF language ID.
@type grflangid: C{int}
@ivar isocode: ISO code of the language.
@type isocode: C{str}
@ivar filename: Base filename of the language.
@type filename: C{str}
@ivar is_stable: Whether the language is considered stable.
@type is_stable: C{bool}
"""
def __init__(self, grflangid, isocode, filename, is_stable):
self.grflangid = grflangid
self.isocode = isocode
self.filename = filename
self.is_stable = is_stable
language_table = []
filename_pattern = re.compile("[0-9a-zA-Z_]+$")
def get_language_data():
data = get_data(build_url("/language-list"))
if data is None:
print("Error: Could not download the list of language definitions from Eints.")
sys.exit(1)
reader = csv.DictReader(io.StringIO(data), dialect="unix")
for line in reader:
filename = line.get("filename")
if filename is None or not filename_pattern.match(filename):
continue
isocode = line.get("isocode")
if isocode is None or len(isocode) == 0:
continue
grflangid = line.get("grflangid")
if grflangid is None or not hex_pat.match(grflangid):
continue
grflangid = int(grflangid, 16)
is_stable = line.get("is_stable")
if is_stable is None or is_stable not in ["True", "False"]:
continue
is_stable = is_stable == "True"
language_table.append(LanguageData(grflangid, isocode, filename, is_stable))
def get_languagedata_by_isocode(isocode):
"""
Retrieve the language data from the table that matches the L{grflangid} parameter.
@param isocode: Isocode of the language.
@type isocode: C{str}
@return: Language data, if available.
@rtype: L{LanguageData} or C{None}
"""
if len(language_table) == 0:
get_language_data()
for ld in language_table:
if ld.isocode == isocode:
return ld
return None
def get_languagedata_by_grflangid(grflangid):
"""
Retrieve the language data from the table that matches the L{grflangid} parameter.
@param grflangid: NewGRF language ID.
@type grflangid: C{int}
@return: Language data, if available.
@rtype: L{LanguageData} or C{None}
"""
if len(language_table) == 0:
get_language_data()
for ld in language_table:
if ld.grflangid == grflangid:
return ld
return None
def get_languagedata_by_filename(filename):
"""
Retrieve the language data from the table that matches the L{filename} parameter.
@param filename: Filename of the language file.
@type filename: C{str}
@return: Language data, if available.
@rtype: L{LanguageData} or C{None}
"""
if len(language_table) == 0:
get_language_data()
for ld in language_table:
if ld.filename == filename:
return ld
return None
string_pat = re.compile("^([A-Za-z_0-9]+)(\\.[A-Za-z0-9]+)?[ \\t]*:(.*)$")
def parse_string(line):
"""
Parse a string, and return its name and it text if parsing succeeds.
@param line: Line of text containing the string.
@type line: C{str}
@return: A tuple (name, text) or C{None}
@rtype: (C{str}, C{str}) or C{None}
"""
m = string_pat.match(line)
if not m:
return None
if m.group(2) is None:
return m.group(1), m.group(3).rstrip()
else:
return m.group(1) + m.group(2), m.group(3).rstrip()
def load_local_strings(path):
"""
Load the strings of a local file, and store them in a set for fast comparing.
@param path: Path of the file to load.
@type path: C{str}
@return: Strings of the file.
@rtype: C{set} of C{str}
"""
strings = set()
with open(path, "r", encoding="utf-8") as handle:
for line in handle:
value = parse_string(line)
if value is not None:
strings.add(value)
return strings
def parse_annotated_langfile(data):
"""
Parse an annotated language file loaded from eints.
@param data: Downloaded data.
@type data: C{str}
@return: Parsed annotated language file lines, a sequence of tuples (type, <data>).
The following parsed lines are returned:
- ('text', <text-to-copy>)
- ('string', <string-line>, <credits>), where <credits> may be C{None}
@rtype: C{list} of (C{str}, C{str}, [C{str}])
"""
parsed = []
string_text = None
for line in data.splitlines():
if string_text is not None:
if line.startswith("credits:"):
parsed.append(("string", string_text, line[8:]))
string_text = None
continue
parsed.append(("string", string_text, None))
string_text = None
if line.startswith("string:"):
string_text = line[7:] # Temporary store the string, to check for credits at the next line.
continue
if line.startswith("literal:"):
parsed.append(("text", line[8:]))
continue
if line.startswith("pragma:"):
parsed.append(("text", line[7:]))
continue
if line.startswith("gender:") or line.startswith("plural:"):
parsed.append(("text", line[7:]))
continue
if line.startswith("case:"):
parsed.append(("text", line[5:]))
continue
if line.startswith("grflangid:"):
parsed.append(("text", line[10:]))
continue
if string_text is not None:
parsed.append(("string", string_text, None))
return parsed
def collect_credits(strings, parsed):
"""
Find changed language file strings, and collect credits for who changed them.
@param strings: Strings of the local file.
@type strings: C{list} of (C{str}, C{str})
@param parsed: Parsed annotated language file.
@type parsed: C{list} of (C{str}, C{str}, C{str} or C{None})
"""
credits = {}
for p in parsed:
if p[0] != "string":
continue # Not a string.
value = parse_string(p[1])
if value is None or p[2] is None or value in strings:
continue
n = credits.get(p[2])
if n is None:
credits[p[2]] = 1
else:
credits[p[2]] = n + 1
persons = []
for user, count in credits.items():
if count == 1:
persons.append((1, "1 change by " + user))
else:
persons.append((count, str(count) + " changes by " + user))
return ", ".join(p[1] for p in sorted(persons, reverse=True))
def detect_eolstyle(path):
"""
Detect eolstyle of a file.
@param path: Path of the file to rad.
@type path: C{str}
@return: EOL style. '\n', '\r' or '\r\n' or None for unknown (no linebreaks at all).
@rtype: C{str}
"""
with open(path, "r", encoding="utf-8") as f:
f.readline()
if type(f.newlines) == tuple:
return f.newlines[0]
else:
return f.newlines
def write_parsed_lines(parsed, path, eolstyle):
"""
Write a language file previously parsed into lines by L{parse_annotated_langfile} to the local disc.
@param parsed: Parsed lines of the annotated language file.
@type parsed: C{list} of (C{str}, C{str}, C{str} or C{None})
@param path: Path of the file to write.
@type path: C{str}
@param eolstyle: EOL style. '\n', '\r' or '\r\n' or None for native.
@type eolstyle: C{str} or C{None}
"""
with open(path, "w", encoding="utf-8", newline=eolstyle) as handle:
handle.write("\n".join(p[1] for p in parsed))
handle.write("\n")
def perform_download(ll_files, el_files, credits_handle, path_base):
"""
Download Eints files to local files.
@param ll_files: Local files to overwrite.
@type ll_files: C{list} of L{LocalLanguageInfo}
@param el_files: Eints files to download.
@type el_files: C{list} of L{EintsLanguageInfo}
@param credits_handle: If available, output stream for writing credits of language file changes.
@type credits_handle: C{file} or C{None}
@param path_base: Local baselang file.
@type path_base: C{str} or C{None}
"""
local_files = dict((ll.isocode, ll) for ll in ll_files) # Mapping of isocode to local info.