-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnicode_description_generator.py
More file actions
170 lines (134 loc) · 6.37 KB
/
Unicode_description_generator.py
File metadata and controls
170 lines (134 loc) · 6.37 KB
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
"""
=========================================================
Smart Unicode Description Generator for Wikimedia Commons
=========================================================
[ SETUP & USAGE ]
1. Install reqs: pip install pywikibot
2. Login config: Ensure 'user-config.py' is set up.
3. Purpose : Scans filenames for Unicode codepoints (U+XXXX).
Automatically detects the character and its official
Unicode name. Generates a smart description with options
for custom Font Names and Glyph Styles.
4. Run : Set CONFIGURATION below and execute.
=========================================================
"""
import pywikibot
from pywikibot import pagegenerators
import re
import time
import unicodedata
# ================= CONFIGURATION =================
# 1. Target Categories
TARGET_CATEGORIES = [
'Category:YOUR_CATEGORY_NAME_HERE'
]
# 2. Font & Style Settings
# If FONT_NAME is provided, it will be used directly.
# If left empty (""), it will check EXTRACT_FONT_FROM_FILENAME.
FONT_NAME = "" # e.g., "GNU Unifont" or "Noto Sans Bengali"
# Set to True to extract font name from the filename (e.g., "... in Noto Sans - U+...").
# Only works if FONT_NAME above is left empty.
EXTRACT_FONT_FROM_FILENAME = True
# Optional: Prepends a style to the description (e.g., "Dotted style Bengali letter...").
GLYPH_TYPE = "" # e.g., "Dotted", "Pixelated", "Bold". Leave empty for default.
# 3. Advanced Controls
PROCESS_SUBCATEGORIES = False # Set True to check inside subcategories
SLEEP_TIME = 5 # Seconds to pause after saving
DRY_RUN = False # Set True to test without making real edits
# =================================================
def generate_smart_description(filename):
"""
Extracts Unicode data and builds a smart description based on user configuration.
"""
# 1. Extract the Unicode Hex (Handles U+XXXX, U-XXXX, U+XXXXXX)
code_match = re.search(r'U[-+]([0-9A-Fa-f]{4,6})', filename, re.IGNORECASE)
if not code_match:
return None # Skip if no Unicode pattern is found
hex_str = code_match.group(1).upper()
# 2. Get Character and Official Unicode Name
try:
char = chr(int(hex_str, 16))
char_name = unicodedata.name(char).capitalize()
except ValueError:
char = "?"
char_name = "Unknown Unicode character"
# Avoid printing invisible control characters
char_display = f" ({char})" if not unicodedata.category(char).startswith('C') else ""
# 3. Determine Font Name
final_font_name = ""
if FONT_NAME.strip():
# Priority 1: Use hardcoded font name from config
final_font_name = FONT_NAME.strip()
elif EXTRACT_FONT_FROM_FILENAME:
# Priority 2: Try to extract from filename
font_match = re.search(r'\bin\s+(.*?)\s*(?:-|–|—)?\s*U[-+]', filename, re.IGNORECASE)
if font_match:
extracted_name = font_match.group(1).strip()
final_font_name = re.sub(r'[-–—]\s*$', '', extracted_name).strip()
# 4. Determine Glyph Style Prefix
style_prefix = f"{GLYPH_TYPE.strip()} style " if GLYPH_TYPE.strip() else ""
# 5. Build Final Description String with Compart Link
compart_link = f"[https://www.compart.com/en/unicode/U+{hex_str} U+{hex_str}]"
if final_font_name:
# Format: [Style] Bengali letter ka (ক) in Kalpurush font - [Link]
return f"{style_prefix}{char_name}{char_display} in {final_font_name} font - {compart_link}"
else:
# Format: [Style] Bengali letter ka (ক) - [Link]
return f"{style_prefix}{char_name}{char_display} - {compart_link}"
def main():
try:
site = pywikibot.Site('commons', 'commons')
site.login()
print(f"Logged in as: {site.user()}")
except Exception as e:
print(f"Login Failed: {e}")
return
for cat_name in TARGET_CATEGORIES:
print(f"\n[{'DRY RUN' if DRY_RUN else 'LIVE'}] Processing Category: {cat_name}...")
try:
cat = pywikibot.Category(site, cat_name)
gen = pagegenerators.CategorizedPageGenerator(cat, recurse=PROCESS_SUBCATEGORIES)
except pywikibot.exceptions.InvalidTitleError:
print(f"[ERROR] Invalid category name: {cat_name}")
continue
for page in gen:
if not page.is_filepage():
continue
file_title = page.title(with_ns=False)
# Generate the dynamic description string
smart_desc = generate_smart_description(file_title)
if not smart_desc:
continue
current_text = page.text
# Regex pattern to find |description={{en|...}}
desc_pattern = r'(\|description\s*=\s*\{\{en\|)(.*?)(\}\})'
if re.search(desc_pattern, current_text):
# Replace existing description with the new smart description
new_text = re.sub(desc_pattern, r'\1' + smart_desc + r'\3', current_text)
if new_text != current_text:
if DRY_RUN:
print(f"[-] DRY RUN: {file_title}")
print(f" -> Would update to: '{smart_desc}'")
continue
print(f"[\u2713] Updating: {file_title}")
print(f" -> New Desc: {smart_desc}")
page.text = new_text
try:
page.save(summary=f"Smart description update: {smart_desc}")
time.sleep(SLEEP_TIME)
except pywikibot.exceptions.LockedPageError:
print(f" [SKIP] Page is locked.")
except pywikibot.exceptions.OtherPageSaveError as e:
print(f" [ERROR] Save failed: {e}")
except Exception as e:
print(f" [ERROR] Generic error: {e}")
else:
pass
else:
print(f"[SKIP] No English description template found: {file_title}")
if DRY_RUN:
print("\n\u2705 DRY RUN complete. No files were actually modified.")
else:
print("\n\u2705 Batch processing completed.")
if __name__ == '__main__':
main()