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
|
#!/usr/bin/env python
"""
Setup module for wcwidth.
https://github.com/jquast/wcwidth
You may execute setup.py with special arguments:
- ``update``: Updates unicode reference files of the project to latest.
- ``test``: Executes test runner (tox)
"""
from __future__ import print_function
import os
import setuptools
import setuptools.command.test
try:
# py2
from urllib2 import urlopen
except ImportError:
# py3
from urllib.request import urlopen
HERE = os.path.dirname(__file__)
# use chr() for py3.x,
# unichr() for py2.x
try:
_ = unichr(0)
except NameError as err:
if err.args[0] == "name 'unichr' is not defined":
# pylint: disable=C0103,W0622
# Invalid constant name "unichr" (col 8)
# Redefining built-in 'unichr' (col 8)
unichr = chr
else:
raise
class SetupUpdate(setuptools.Command):
""" 'setup.py update' fetches and updates local unicode code tables. """
# pylint: disable=R0904
# Too many public methods (43/20)
description = "Fetch and update unicode code tables"
user_options = []
EAW_URL = ('http://www.unicode.org/Public/UNIDATA/'
'EastAsianWidth.txt')
UCD_URL = ('http://www.unicode.org/Public/UNIDATA/extracted/'
'DerivedGeneralCategory.txt')
EAW_IN = '/usr/share/unicode/EastAsianWidth.txt'
UCD_IN = '/usr/share/unicode/extracted/DerivedGeneralCategory.txt'
EAW_OUT = os.path.join(HERE, 'wcwidth', 'table_wide.py')
ZERO_OUT = os.path.join(HERE, 'wcwidth', 'table_zero.py')
README_RST = os.path.join(HERE, 'README.RST')
README_PATCH_FROM = "the Unicode Standard release files:"
README_PATCH_TO = "Installation"
def initialize_options(self):
"""Override builtin method: no options are available."""
pass
def finalize_options(self):
"""Override builtin method: no options are available."""
pass
def run(self):
"""Update east-asian, combining and zero width tables."""
self._do_east_asian()
self._do_zero_width()
self._do_readme_update()
def _do_readme_update(self):
"""Patch README.rst to reflect the data files used in release."""
import codecs
import glob
# read in,
data_in = codecs.open(
os.path.join(HERE, 'README.rst'), 'r', 'utf8').read()
# search for beginning and end positions,
pos_begin = data_in.find(self.README_PATCH_FROM)
assert pos_begin != -1, (pos_begin, self.README_PATCH_FROM)
pos_begin += len(self.README_PATCH_FROM)
pos_end = data_in.find(self.README_PATCH_TO)
assert pos_end != -1, (pos_end, self.README_PATCH_TO)
glob_pattern = os.path.join(HERE, 'data', '*.txt')
file_descriptions = [
self._describe_file_header(fpath)
for fpath in glob.glob(glob_pattern)]
# patch,
data_out = (
data_in[:pos_begin] +
'\n\n' +
'\n'.join(file_descriptions) +
'\n\n' +
data_in[pos_end:]
)
# write.
print("patching {} ..".format(self.README_RST))
codecs.open(
self.README_RST, 'w', 'utf8').write(data_out)
def _do_east_asian(self):
"""Fetch and update east-asian tables."""
(version, date, values) = self._parse_east_asian(
fname=self.EAW_IN,
properties=(u'W', u'F',)
)
table = self._make_table(values)
self._do_write(self.EAW_OUT, 'WIDE_EASTASIAN', version, date, table)
def _do_zero_width(self):
"""Fetch and update zero width tables."""
(version, date, values) = self._parse_category(
fname=self.UCD_IN,
categories=('Me', 'Mn',)
)
table = self._make_table(values)
self._do_write(self.ZERO_OUT, 'ZERO_WIDTH', version, date, table)
@staticmethod
def _make_table(values):
"""Return a tuple of lookup tables for given values."""
import collections
table = collections.deque()
start, end = values[0], values[0]
for num, value in enumerate(values):
if num == 0:
table.append((value, value,))
continue
start, end = table.pop()
if end == value - 1:
table.append((start, value,))
else:
table.append((start, end,))
table.append((value, value,))
return tuple(table)
@staticmethod
def _do_retrieve(url, fname):
"""Retrieve given url to target filepath fname."""
folder = os.path.dirname(fname)
if not os.path.exists(folder):
os.makedirs(folder)
print("{}/ created.".format(folder))
if not os.path.exists(fname):
with open(fname, 'wb') as fout:
print("retrieving {}.".format(url))
resp = urlopen(url)
fout.write(resp.read())
print("{} saved.".format(fname))
else:
print("re-using artifact {}".format(fname))
return fname
@staticmethod
def _describe_file_header(fpath):
import codecs
header_3 = [line.lstrip('# ').rstrip() for line in
codecs.open(fpath, 'r', 'utf8').readlines()[:3]]
return ('``{0}``\n' # ``EastAsianWidth-8.0.0.txt``
' *{1}*\n' # *2015-02-10, 21:00:00 GMT [KW, LI]*
' {2}\n' # (c) 2016 Unicode(R), Inc.
.format(*header_3))
@staticmethod
def _parse_east_asian(fname, properties=(u'W', u'F',)):
"""Parse unicode east-asian width tables."""
version, date, values = None, None, []
print("parsing {} ..".format(fname))
for line in open(fname, 'rb'):
uline = line.decode('utf-8')
if version is None:
version = uline.split(None, 1)[1].rstrip()
continue
elif date is None:
date = uline.split(':', 1)[1].rstrip()
continue
if uline.startswith('#') or not uline.lstrip():
continue
addrs, details = uline.split(';', 1)
if any(details.startswith(property)
for property in properties):
start, stop = addrs, addrs
if '..' in addrs:
start, stop = addrs.split('..')
values.extend(range(int(start, 16), int(stop, 16) + 1))
return version, date, sorted(values)
@staticmethod
def _parse_category(fname, categories):
"""Parse unicode category tables."""
version, date, values = None, None, []
print("parsing {} ..".format(fname))
for line in open(fname, 'rb'):
uline = line.decode('utf-8')
if version is None:
version = uline.split(None, 1)[1].rstrip()
continue
elif date is None:
date = uline.split(':', 1)[1].rstrip()
continue
if uline.startswith('#') or not uline.lstrip():
continue
addrs, details = uline.split(';', 1)
addrs, details = addrs.rstrip(), details.lstrip()
if any(details.startswith('{} #'.format(value))
for value in categories):
start, stop = addrs, addrs
if '..' in addrs:
start, stop = addrs.split('..')
values.extend(range(int(start, 16), int(stop, 16) + 1))
return version, date, sorted(values)
@staticmethod
def _do_write(fname, variable, version, date, table):
"""Write combining tables to filesystem as python code."""
# pylint: disable=R0914
# Too many local variables (19/15) (col 4)
print("writing {} ..".format(fname))
import unicodedata
import datetime
import string
utc_now = datetime.datetime.utcnow()
indent = 4
with open(fname, 'w') as fout:
fout.write(
'"""{variable_proper} table. Created by setup.py."""\n'
"# Source: {version}\n"
"# Date: {date}\n"
"{variable} = (".format(iso_utc=utc_now.isoformat(),
version=version,
date=date,
variable=variable,
variable_proper=variable.title()))
for start, end in table:
ucs_start, ucs_end = unichr(start), unichr(end)
hex_start, hex_end = ('0x{0:04x}'.format(start),
'0x{0:04x}'.format(end))
try:
name_start = string.capwords(unicodedata.name(ucs_start))
except ValueError:
name_start = u''
try:
name_end = string.capwords(unicodedata.name(ucs_end))
except ValueError:
name_end = u''
fout.write('\n' + (' ' * indent))
fout.write('({0}, {1},),'.format(hex_start, hex_end))
fout.write(' # {0:24s}..{1}'.format(
name_start[:24].rstrip() or '(nil)',
name_end[:24].rstrip()))
fout.write('\n)\n')
print("complete.")
def main():
"""Setup.py entry point."""
import codecs
setuptools.setup(
name='wcwidth',
version='0.1.7',
description=("Measures number of Terminal column cells "
"of wide-character codes"),
long_description=codecs.open(
os.path.join(HERE, 'README.rst'), 'r', 'utf8').read(),
author='Jeff Quast',
author_email='contact@jeffquast.com',
license='MIT',
packages=['wcwidth', 'wcwidth.tests'],
url='https://github.com/jquast/wcwidth',
include_package_data=True,
test_suite='wcwidth.tests',
zip_safe=True,
classifiers=[
'Intended Audience :: Developers',
'Natural Language :: English',
'Development Status :: 3 - Alpha',
'Environment :: Console',
'License :: OSI Approved :: MIT License',
'Operating System :: POSIX',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Topic :: Software Development :: Libraries',
'Topic :: Software Development :: Localization',
'Topic :: Software Development :: Internationalization',
'Topic :: Terminals'
],
keywords=['terminal', 'emulator', 'wcwidth', 'wcswidth', 'cjk',
'combining', 'xterm', 'console', ],
cmdclass={'update': SetupUpdate},
)
if __name__ == '__main__':
main()
|