[Bf-docboard-svn] bf-manual: [7379] trunk/blender_docs/tools_maintenance/change_shortcuts: change_shortcuts scripts update
Pep
noreply at blender.org
Sun Nov 15 18:37:02 CET 2020
Revision: 7379
https://developer.blender.org/rBM7379
Author: pepribal
Date: 2020-11-15 18:37:01 +0100 (Sun, 15 Nov 2020)
Log Message:
-----------
change_shortcuts scripts update
Modified Paths:
--------------
trunk/blender_docs/tools_maintenance/change_shortcuts/change_man_shortcuts.py
trunk/blender_docs/tools_maintenance/change_shortcuts/change_tr_shortcuts.py
trunk/blender_docs/tools_maintenance/change_shortcuts/diff_tr_shortcuts.py
trunk/blender_docs/tools_maintenance/change_shortcuts/find_man_shortcuts.py
trunk/blender_docs/tools_maintenance/change_shortcuts/find_tr_shortcuts.py
trunk/blender_docs/tools_maintenance/change_shortcuts/list_man_shortcuts.py
trunk/blender_docs/tools_maintenance/change_shortcuts/list_tr_shortcuts.py
trunk/blender_docs/tools_maintenance/change_shortcuts/readme.txt
trunk/blender_docs/tools_maintenance/change_shortcuts/table.csv
trunk/blender_docs/tools_maintenance/change_shortcuts/table_es.csv
Added Paths:
-----------
trunk/blender_docs/tools_maintenance/change_shortcuts/table_check.py
Modified: trunk/blender_docs/tools_maintenance/change_shortcuts/change_man_shortcuts.py
===================================================================
--- trunk/blender_docs/tools_maintenance/change_shortcuts/change_man_shortcuts.py 2020-11-15 08:31:08 UTC (rev 7378)
+++ trunk/blender_docs/tools_maintenance/change_shortcuts/change_man_shortcuts.py 2020-11-15 17:37:01 UTC (rev 7379)
@@ -5,10 +5,7 @@
import os
-n_changes = 0
-table = []
-
def find_vcs_root(dirs=(".svn", ".git"), default=None):
"""
Returns the repo root dir.
@@ -30,58 +27,56 @@
:param s: shortcut string inside :kbd:`...`.
:return: replaced string.
"""
- global table
result = s
- for entry in table:
+ for entry in st_replace.table:
result = result.replace(entry[0], entry[1])
return result
-def line_process(line, prefix=':kbd:'):
+def line_process(line, prefix):
"""
It processes a line from the RST file.
:param line: the line from the RST file.
:param prefix: type of element (:kbd:, :menuitem:,...)
- :return: the processed (replaced) line.
+ :return: the processed (replaced) line and the number of changes made.
"""
result = ''
- lin = line
- global n_changes
+ n_changes = 0
while True:
- # shortcut start search
- pos = lin.find(prefix + '`')
- if pos == -1:
- result += lin
+ # Prefix search:
+ pos = line.find(prefix + '`')
+ if pos != -1:
+ result += line[:pos + len(prefix) + 1]
+ line = line[pos + len(prefix) + 1:]
+ else:
+ result += line
break
- result += lin[:pos+len(prefix)+1]
- lin = lin[pos+len(prefix)+1:]
-
- # shortcut end search
- pos = lin.find('`') # we assume it will be found
- s_in = lin[:pos] # candidate shortcut string to suffer replacements
+ # Shortcut contents search
+ pos = line.find('`') # we assume it will be found
+ s_in = line[:pos] # candidate shortcut string to suffer replacements
s_out = st_replace(s_in)
result += s_out
- lin = lin[pos:]
+ line = line[pos:]
if s_in != s_out:
n_changes += 1
- return result
+ return result, n_changes
-def file_process(filename):
+def file_process(filename, prefix):
"""
It processes the RST file to replace and generates the output file
if needed.
:param filename: the name of the RST file.
+ :param prefix: prefix to search (like ':kbd:' or ':menuselection:').
:return: nothing.
"""
fin = open(filename, 'rt')
fout_list = []
- global n_changes
n_changes = 0
for line in fin:
- lin = line_process(line) # :kbd: shortcuts
- lin = line_process(lin, ':menuitem:') # :menuitem: menu items
- fout_list.append(lin)
+ line_out, n = line_process(line, prefix)
+ n_changes += n
+ fout_list.append(line_out)
fin.close()
# In case there were changes, we generate an output file
@@ -104,13 +99,15 @@
print("'table.csv' file not found. "
"Script and 'table.csv' must be in the same folder.")
else: # All OK
- # Substitution table initialization:
+ # Substitution table initialization. It will be stored as a "static"
+ # variable of st_replace() function:
+ st_replace.table = []
f = open('table.csv', 'rt')
for ln in f:
row = ln.strip()
if row != '' and row[0] != '#':
r = row.split(';')
- table.append((r[0].strip(), r[1].strip()))
+ st_replace.table.append((r[0].strip(), r[1].strip()))
f.close()
# Main loop:
@@ -118,4 +115,5 @@
for fname in info_dir[2]:
path_full = os.path.join(info_dir[0], fname)
if path_full[-4:] == '.rst':
- file_process(path_full)
+ file_process(path_full, ':kbd:')
+ file_process(path_full, ':menuselection:')
Modified: trunk/blender_docs/tools_maintenance/change_shortcuts/change_tr_shortcuts.py
===================================================================
--- trunk/blender_docs/tools_maintenance/change_shortcuts/change_tr_shortcuts.py 2020-11-15 08:31:08 UTC (rev 7378)
+++ trunk/blender_docs/tools_maintenance/change_shortcuts/change_tr_shortcuts.py 2020-11-15 17:37:01 UTC (rev 7379)
@@ -3,15 +3,13 @@
# Changes the shortcuts (:kbd:`...`) and menu items (:menuselection:`...`)
# across all the translated PO files of a spcecific language, according to
# the provided CSV table.
+#
# Language must be indicated.
import os
import sys
-n_changes = 0
-table = []
-
def find_vcs_root(dirs=(".svn", ".git"), default=None):
"""
Returns the repo root dir.
@@ -33,72 +31,89 @@
:param s: shortcut string inside :kbd:`...`.
:return: replaced string.
"""
- global table
result = s
- for entry in table:
+ for entry in st_replace.table:
result = result.replace(entry[0], entry[1])
return result
-def line_process(line, prefix=':kbd:'):
+def line_process(line, line_prev, prefix):
"""
It processes a line from the PO file.
:param line: the line from the PO file.
- :param prefix: type of element (:kbd:, :menuitem:,...)
- :return: the processed (replaced) line.
+ :param line_prev: previous line in a multi-line 'msgstr'.
+ :param prefix: prefix to search (like ':kbd:' or ':menuselection:').
+ :return: the processed (replaced) line and the number of changes made.
"""
result = ''
- lin = line
- global n_changes
+ n_changes = 0
+
+ # Prefix at the end of the previous line?:
+ if line_prev[-(len(prefix) + 2): -2] == prefix: # ...:prefix:"\n
+ assert line[0:2] == '"`'
+ result += '"`'
+ line = line[2:]
+ pos = line.find('`') # we assume it will be found
+ s_in = line[:pos] # candidate shortcut string to suffer replacements
+ s_out = st_replace(s_in)
+ result += s_out
+ line = line[pos:]
+ if s_in != s_out:
+ n_changes += 1
+
+ # Rest of prefixes (if any) are fully included in the line:
while True:
- # shortcut start search
- pos = lin.find(prefix + '`')
- if pos == -1:
- result += lin
+ # Prefix search:
+ pos = line.find(prefix + '`')
+ if pos != -1:
+ result += line[:pos + len(prefix) + 1]
+ line = line[pos + len(prefix) + 1:]
+ else:
+ result += line
break
- result += lin[:pos+len(prefix)+1]
- lin = lin[pos+len(prefix)+1:]
-
- # shortcut end search
- pos = lin.find('`') # we assume it will be found
- s_in = lin[:pos] # candidate shortcut string to suffer replacements
+ # Shortcut end search
+ pos = line.find('`') # we assume it will be found
+ s_in = line[:pos] # candidate shortcut string to suffer replacements
s_out = st_replace(s_in)
result += s_out
- lin = lin[pos:]
+ line = line[pos:]
if s_in != s_out:
n_changes += 1
- return result
+ return result, n_changes
-def file_process(filename):
+def file_process(filename, prefix):
"""
- It processes the PO file to translate and generates the output file
- if needed.
+ It processes the PO file to replace and generates the output file
+ if changes where necessary.
:param filename: the name of the PO file.
+ :param prefix: prefix to search (like ':kbd:' or ':menuselection:').
:return: nothing.
"""
fin = open(filename, 'rt')
- fout_list = []
+ fout_list = [] # text lines of the output file
in_msgstr = False
- global n_changes
n_changes = 0
+ line_prev = '' # last processed line
for line in fin:
if in_msgstr:
if line[0] == '"': # still inside a msgstr
- lin = line_process(line) # :kbd: shortcuts
- lin = line_process(lin, ':menuitem:') # :menuitem: menu items
- fout_list.append(lin)
+ line_out, n = line_process(line, line_prev, prefix)
+ line_prev = line
+ n_changes += n
else:
in_msgstr = False # not anymore in a msgstr
- fout_list.append(line)
+ line_out = line
+ line_prev = ''
else:
if line[:6] == 'msgstr': # entering a msgstr
in_msgstr = True
- lin = line_process(line) # :kbd: shortcuts
- lin = line_process(lin, ':menuitem:') # :menuitem: menu items
- fout_list.append(lin)
+ line_out, n = line_process(line, line_prev, prefix)
+ line_prev = line
+ n_changes += n
else: # still outside a msgstr
- fout_list.append(line)
+ line_out = line
+ fout_list.append(line_out)
fin.close()
# In case there were changes, we generate an output file
@@ -134,13 +149,15 @@
print("'table_" + sys.argv[1] + ".csv' file not found. "
"Script and table must be in the same folder.")
else: # All OK
- # Substitution table initialization:
+ # Substitution table initialization. It will be stored as a "static"
+ # variable of st_replace() function:
+ st_replace.table = []
f = open('table_' + sys.argv[1] + '.csv', 'rt')
for ln in f:
row = ln.strip()
if row != '' and row[0] != '#':
r = row.split(';')
- table.append((r[0].strip(), r[1].strip()))
+ st_replace.table.append((r[0].strip(), r[1].strip()))
f.close()
# Main loop:
@@ -148,4 +165,5 @@
for fname in info_dir[2]:
path_full = os.path.join(info_dir[0], fname)
if path_full[-3:] == '.po':
- file_process(path_full)
+ file_process(path_full, ':kbd:')
+ file_process(path_full, 'menuselection')
Modified: trunk/blender_docs/tools_maintenance/change_shortcuts/diff_tr_shortcuts.py
===================================================================
--- trunk/blender_docs/tools_maintenance/change_shortcuts/diff_tr_shortcuts.py 2020-11-15 08:31:08 UTC (rev 7378)
+++ trunk/blender_docs/tools_maintenance/change_shortcuts/diff_tr_shortcuts.py 2020-11-15 17:37:01 UTC (rev 7379)
@@ -2,6 +2,7 @@
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-docboard-svn
mailing list