[Bf-docboard-svn] bf-manual: [7370] trunk/blender_docs/tools_maintenance/change_shortcuts: Added change_shortcuts scripts

Pep noreply at blender.org
Sat Nov 14 00:32:35 CET 2020


Revision: 7370
          https://developer.blender.org/rBM7370
Author:   pepribal
Date:     2020-11-14 00:32:33 +0100 (Sat, 14 Nov 2020)
Log Message:
-----------
Added change_shortcuts scripts

Added Paths:
-----------
    trunk/blender_docs/tools_maintenance/change_shortcuts/
    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_man_shortcuts.py
    trunk/blender_docs/tools_maintenance/change_shortcuts/diff_tr_shortcuts.py
    trunk/blender_docs/tools_maintenance/change_shortcuts/table.csv
    trunk/blender_docs/tools_maintenance/change_shortcuts/table_es.csv

Added: trunk/blender_docs/tools_maintenance/change_shortcuts/change_man_shortcuts.py
===================================================================
--- trunk/blender_docs/tools_maintenance/change_shortcuts/change_man_shortcuts.py	                        (rev 0)
+++ trunk/blender_docs/tools_maintenance/change_shortcuts/change_man_shortcuts.py	2020-11-13 23:32:33 UTC (rev 7370)
@@ -0,0 +1,115 @@
+#! /usr/bin/env python3
+
+# Changes the shortcuts (:kbd:`...`) across all the manual RST
+# files according to the provided CSV table.
+
+import os
+
+n_changes = 0
+table = []
+
+
+def find_vcs_root(dirs=(".svn", ".git"), default=None):
+    """
+    Returns the repo root dir.
+    :param dirs: dirs to look for.
+    :param default: value to return if not found.
+    :return: repo root dir.
+    """
+    prev, test = None, os.path.abspath(os.path.dirname(__file__))
+    while prev != test:
+        if any(os.path.isdir(os.path.join(test, d)) for d in dirs):
+            return test
+        prev, test = test, os.path.abspath(os.path.join(test, os.pardir))
+    return default
+
+
+def st_replace(s):
+    """
+    Replaces the shortcut string, based on the substitution table.
+    :param s: shortcut string inside :kbd:`...`.
+    :return: replaced string.
+    """
+    global table
+    result = s
+    for entry in table:
+        result = result.replace(entry[0], entry[1])
+    return result
+
+
+def line_process(line):
+    """
+    It processes a line from the RST file.
+    :param line: the line from the RST file.
+    :return: the processed (replaced) line.
+    """
+    result = ''
+    lin = line
+    global n_changes
+    while True:
+        # shortcut start search
+        pos = lin.find(':kbd:`')
+        if pos == -1:
+            result += lin
+            break
+        result += lin[:pos+6]
+        lin = lin[pos+6:]
+
+        # shortcut end search
+        pos = lin.find('`')  # we assume it will be found
+        s_in = lin[:pos]  # candidate shortcut string to suffer replacements
+        s_out = st_replace(s_in)
+        result += s_out
+        lin = lin[pos:]
+        if s_in != s_out:
+            n_changes += 1
+    return result
+
+
+def file_process(filename):
+    """
+    It processes the RST file to replace and generates the output file
+    if needed.
+    :param filename: the name of the RST file.
+    :return: nothing.
+    """
+    fin = open(filename, 'rt')
+    fout_list = []
+    global n_changes
+    n_changes = 0
+    for line in fin:
+        lin = line_process(line)
+        fout_list.append(lin)
+    fin.close()
+
+    # In case there were changes, we generate an output file
+    if n_changes > 0:
+        print(filename + ':', n_changes, "change(s).")
+
+        file_out = 'new_' + filename[filename.find('manual'):]
+        os.makedirs(os.path.dirname(file_out), exist_ok=True)
+        fout = open(file_out, 'wt')
+        fout.writelines(fout_list)
+        fout.close()
+
+
+root_path = find_vcs_root()
+
+if not os.path.isfile('table.csv'):
+    print("'table.csv' file not found. "
+          "Script and 'table.csv' must be in the same folder.")
+else:
+    # Substitution table initialization:
+    f = open('table.csv', 'rt')
+    for ln in f:
+        row = ln.strip()
+        if row != '':
+            table.append(row.split(';'))
+    f.close()
+
+    # Main loop:
+    for info_dir in os.walk(os.path.join(root_path, 'manual')):
+        for fname in info_dir[2]:
+            path_full = os.path.join(info_dir[0], fname)
+            if path_full[-4:] == '.rst':
+                file_process(path_full)


Property changes on: trunk/blender_docs/tools_maintenance/change_shortcuts/change_man_shortcuts.py
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:executable
## -0,0 +1 ##
+*
\ No newline at end of property
Added: trunk/blender_docs/tools_maintenance/change_shortcuts/change_tr_shortcuts.py
===================================================================
--- trunk/blender_docs/tools_maintenance/change_shortcuts/change_tr_shortcuts.py	                        (rev 0)
+++ trunk/blender_docs/tools_maintenance/change_shortcuts/change_tr_shortcuts.py	2020-11-13 23:32:33 UTC (rev 7370)
@@ -0,0 +1,142 @@
+#! /usr/bin/env python3
+
+# Changes the shortcuts (:kbd:`...`) 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.
+    :param dirs: dirs to look for.
+    :param default: value to return if not found.
+    :return: repo root dir.
+    """
+    prev, test = None, os.path.abspath(os.path.dirname(__file__))
+    while prev != test:
+        if any(os.path.isdir(os.path.join(test, d)) for d in dirs):
+            return test
+        prev, test = test, os.path.abspath(os.path.join(test, os.pardir))
+    return default
+
+
+def st_replace(s):
+    """
+    Replaces the shortcut string, based on the substitution table.
+    :param s: shortcut string inside :kbd:`...`.
+    :return: replaced string.
+    """
+    global table
+    result = s
+    for entry in table:
+        result = result.replace(entry[0], entry[1])
+    return result
+
+
+def line_process(line):
+    """
+    It processes a line of the PO file corresponding to a msgstr.
+    :param line: the line from the PO file.
+    :return: the processed (replaced) line.
+    """
+    result = ''
+    lin = line
+    global n_changes
+    while True:
+        # shortcut start search
+        pos = lin.find(':kbd:`')
+        if pos == -1:
+            result += lin
+            break
+        result += lin[:pos+6]
+        lin = lin[pos+6:]
+
+        # shortcut end search
+        pos = lin.find('`')  # we assume it will be found
+        s_in = lin[:pos]  # candidate shortcut string to suffer replacements
+        s_out = st_replace(s_in)
+        result += s_out
+        lin = lin[pos:]
+        if s_in != s_out:
+            n_changes += 1
+    return result
+
+
+def file_process(filename):
+    """
+    It processes the PO file to translate and generates the output file
+    if needed.
+    :param filename: the name of the PO file.
+    :return: nothing.
+    """
+    fin = open(filename, 'rt')
+    fout_list = []
+    in_msgstr = False
+    global n_changes
+    n_changes = 0
+    for line in fin:
+        if in_msgstr:
+            if line[0] == '"':  # still inside a msgstr
+                lin = line_process(line)
+                fout_list.append(lin)
+            else:
+                in_msgstr = False  # not anymore in a msgstr
+                fout_list.append(line)
+        else:
+            if line[:6] == 'msgstr':  # entering a msgstr
+                in_msgstr = True
+                lin = line_process(line)
+                fout_list.append(lin)
+            else:  # still outside a msgstr
+                fout_list.append(line)
+    fin.close()
+
+    # In case there were changes, we generate an output file
+    if n_changes > 0:
+        print(filename + ':', n_changes, "change(s).")
+        file_out = 'new_' + filename[filename.find('locale'):]
+        os.makedirs(os.path.dirname(file_out), exist_ok=True)
+        fout = open(file_out, 'wt')
+        fout.writelines(fout_list)
+        fout.close()
+
+
+root_path = find_vcs_root()
+
+# CL Arguments:
+if len(sys.argv) != 2:
+    print("""\nUsage: change_tr_shortcuts.py <LANGUAGE>
+
+    Examples: change_tr_shortcuts.py es
+              change_tr_shortcuts.py fr
+
+    Substitution table needed (table_<LANGUAGE>.csv).
+    Script and table must be in the same folder.
+
+    Output files will be created in 'new_locale' subfolder
+    inside the script's folder.\n""")
+elif not os.path.isdir(os.path.join(root_path, 'locale', sys.argv[1])):
+    print('<repo_root>/locale/' + sys.argv[1], 'folder not found')
+elif not os.path.isfile(os.path.join('table_' + sys.argv[1] + '.csv')):
+    print('table_' + sys.argv[1] + '.csv file not found')
+else:
+    # Substitution table initialization:
+    f = open('table_' + sys.argv[1] + '.csv', 'rt')
+    for ln in f:
+        row = ln.strip()
+        if row != '':
+            table.append(row.split(';'))
+    f.close()
+
+    # Main loop:
+    for info_dir in os.walk(os.path.join(root_path, 'locale', sys.argv[1], 'LC_MESSAGES')):
+        for fname in info_dir[2]:
+            path_full = os.path.join(info_dir[0], fname)
+            if path_full[-3:] == '.po':
+                file_process(path_full)


Property changes on: trunk/blender_docs/tools_maintenance/change_shortcuts/change_tr_shortcuts.py
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:executable
## -0,0 +1 ##
+*
\ No newline at end of property
Added: trunk/blender_docs/tools_maintenance/change_shortcuts/diff_man_shortcuts.py
===================================================================
--- trunk/blender_docs/tools_maintenance/change_shortcuts/diff_man_shortcuts.py	                        (rev 0)
+++ trunk/blender_docs/tools_maintenance/change_shortcuts/diff_man_shortcuts.py	2020-11-13 23:32:33 UTC (rev 7370)
@@ -0,0 +1,44 @@
+#! /usr/bin/env python3
+
+# Shows the differences between the original RST manual files
+# and the ones generated by change_man_shortcuts.py.
+
+import os
+import difflib
+
+
+def find_vcs_root(dirs=(".svn", ".git"), default=None):
+    """
+    Returns the repo root dir.
+    :param dirs: dirs to look for.
+    :param default: value to return if not found.
+    :return: repo root dir.
+    """
+    prev, test = None, os.path.abspath(os.path.dirname(__file__))
+    while prev != test:
+        if any(os.path.isdir(os.path.join(test, d)) for d in dirs):
+            return test
+        prev, test = test, os.path.abspath(os.path.join(test, os.pardir))
+    return default
+
+
+root_dir = find_vcs_root()
+
+if not os.path.isdir('new_manual'):
+    print('new_manual folder not found.')
+else:
+    # Main loop:
+    for info_dir in os.walk('new_manual'):
+        for fname in info_dir[2]:
+            path_out = os.path.abspath(os.path.join(info_dir[0], fname))
+            path_in = os.path.join(root_dir, path_out[path_out.find('manual'):])
+            f_out = open(path_out, 'rt')
+            f_in = open(path_in, 'rt')

@@ Diff output truncated at 10240 characters. @@


More information about the Bf-docboard-svn mailing list