|
7 | 7 | # Email: mpcabd@gmail.com
|
8 | 8 | # Website: http://mpcabd.xyz
|
9 | 9 |
|
10 |
| -# Ported and tweaked from Java to Python, from Better Arabic Reshaper |
11 |
| -# [https://github.com/agawish/Better-Arabic-Reshaper/] |
12 |
| - |
13 |
| -# Usage: |
14 |
| -# Install python-bidi [https://github.com/MeirKriheli/python-bidi], can be |
15 |
| -# installed from pip `pip install python-bidi`. |
16 |
| - |
17 |
| -# import arabic_reshaper |
18 |
| -# from bidi.algorithm import get_display |
19 |
| -# reshaped_text = arabic_reshaper.reshape('اللغة العربية رائعة') |
20 |
| -# bidi_text = get_display(reshaped_text) |
21 |
| -# Now you can pass `bidi_text` to any function that handles |
22 |
| -# displaying/printing of the text, like writing it to PIL Image or passing it |
23 |
| -# to a PDF generating method. |
24 | 10 | from __future__ import unicode_literals
|
25 | 11 |
|
26 | 12 | import re
|
27 |
| -import os |
28 | 13 |
|
29 |
| -from configparser import ConfigParser |
30 | 14 | from itertools import repeat
|
31 |
| -from pkg_resources import resource_filename |
32 | 15 |
|
33 | 16 | from .ligatures import LIGATURES
|
| 17 | +from .reshaper_config import auto_config |
34 | 18 | from .letters import (UNSHAPED, ISOLATED, TATWEEL, ZWJ, LETTERS_ARABIC,
|
35 | 19 | LETTERS_ARABIC_V2, LETTERS_KURDISH, FINAL,
|
36 | 20 | INITIAL, MEDIAL, connects_with_letters_before_and_after,
|
@@ -73,72 +57,19 @@ class ArabicReshaper(object):
|
73 | 57 | See the default configuration file :file:`default-config.ini` for details
|
74 | 58 | on how to configure your reshaper.
|
75 | 59 | """
|
| 60 | + |
76 | 61 | def __init__(self, configuration=None, configuration_file=None):
|
77 | 62 | super(ArabicReshaper, self).__init__()
|
78 | 63 |
|
79 |
| - configuration_files = [ |
80 |
| - resource_filename(__name__, 'default-config.ini') |
81 |
| - ] |
82 |
| - |
83 |
| - if not os.path.exists(configuration_files[0]): |
84 |
| - raise Exception( |
85 |
| - ('Default configuration file {} not found,' + |
86 |
| - ' check the module installation.').format( |
87 |
| - configuration_files[0], |
88 |
| - ) |
89 |
| - ) |
90 |
| - |
91 |
| - loaded_from_envvar = False |
92 |
| - |
93 |
| - if not configuration_file: |
94 |
| - configuration_file = os.getenv( |
95 |
| - 'PYTHON_ARABIC_RESHAPER_CONFIGURATION_FILE' |
96 |
| - ) |
97 |
| - if configuration_file: |
98 |
| - loaded_from_envvar = True |
99 |
| - |
100 |
| - if configuration_file: |
101 |
| - if not os.path.exists(configuration_file): |
102 |
| - raise Exception( |
103 |
| - 'Configuration file {} not found{}.'.format( |
104 |
| - configuration_file, |
105 |
| - loaded_from_envvar and ( |
106 |
| - ' it is set in your environment variable ' + |
107 |
| - 'PYTHON_ARABIC_RESHAPER_CONFIGURATION_FILE' |
108 |
| - ) or '' |
109 |
| - ) |
110 |
| - ) |
111 |
| - configuration_files.append(configuration_file) |
112 |
| - |
113 |
| - configuration_parser = ConfigParser() |
114 |
| - configuration_parser.read( |
115 |
| - configuration_files |
116 |
| - ) |
117 |
| - |
118 |
| - if configuration: |
119 |
| - configuration_parser.read_dict({ |
120 |
| - 'ArabicReshaper': configuration |
121 |
| - }) |
122 |
| - |
123 |
| - if 'ArabicReshaper' not in configuration_parser: |
124 |
| - raise ValueError( |
125 |
| - 'Invalid configuration: ' |
126 |
| - 'A section with the name ArabicReshaper was not found' |
127 |
| - ) |
128 |
| - |
129 |
| - configuration = configuration_parser['ArabicReshaper'] |
130 |
| - self.configuration = configuration |
| 64 | + self.configuration = auto_config(configuration, configuration_file) |
131 | 65 | self.language = self.configuration.get('language')
|
132 | 66 |
|
133 |
| - |
134 | 67 | if self.language == 'ArabicV2':
|
135 | 68 | self.letters = LETTERS_ARABIC_V2
|
136 | 69 | elif self.language == 'Kurdish':
|
137 | 70 | self.letters = LETTERS_KURDISH
|
138 | 71 | else:
|
139 | 72 | self.letters = LETTERS_ARABIC
|
140 |
| - |
141 |
| - |
142 | 73 |
|
143 | 74 | @property
|
144 | 75 | def _ligatures_re(self):
|
@@ -215,15 +146,15 @@ def reshape(self, text):
|
215 | 146 | previous_letter = output[-1]
|
216 | 147 | if previous_letter[FORM] == NOT_SUPPORTED:
|
217 | 148 | output.append((letter, isolated_form))
|
218 |
| - elif not connects_with_letter_before(letter,self.letters): |
| 149 | + elif not connects_with_letter_before(letter, self.letters): |
219 | 150 | output.append((letter, isolated_form))
|
220 | 151 | elif not connects_with_letter_after(
|
221 |
| - previous_letter[LETTER],self.letters): |
| 152 | + previous_letter[LETTER], self.letters): |
222 | 153 | output.append((letter, isolated_form))
|
223 | 154 | elif (previous_letter[FORM] == FINAL and not
|
224 | 155 | connects_with_letters_before_and_after(
|
225 |
| - previous_letter[LETTER],self.letters |
226 |
| - )): |
| 156 | + previous_letter[LETTER], self.letters |
| 157 | + )): |
227 | 158 | output.append((letter, isolated_form))
|
228 | 159 | elif previous_letter[FORM] == isolated_form:
|
229 | 160 | output[-1] = (
|
|
0 commit comments