| 1 |
"""This module contains functions to write all accompanying files for the |
| 2 |
dictionary, e.g. license or README.""" |
| 3 |
|
| 4 |
import os |
| 5 |
import re |
| 6 |
import shutil |
| 7 |
import sys |
| 8 |
import textwrap |
| 9 |
import xml.dom.minidom |
| 10 |
|
| 11 |
class OutputError(Exception): |
| 12 |
pass |
| 13 |
|
| 14 |
def copy_readme(input_file, output_directory): |
| 15 |
"""When a README is found in the same directory as `input_file`, the file is |
| 16 |
copied to OUTPUT_DIRECTORY. The input file has to be called "README.xxx-yyy" or |
| 17 |
"README.xxx-yyy.EXT", where ext is anarbitrari file extension and xxx-yyy a ISO |
| 18 |
6639-3 three-letter language code.""" |
| 19 |
|
| 20 |
full_input = os.path.abspath(input_file) |
| 21 |
input_file = os.path.basename(input_file) |
| 22 |
input_dir = os.path.dirname(full_input) |
| 23 |
iso_code = re.search('^([a-z]{3}-[a-z]{3})', input_file) |
| 24 |
if not iso_code: |
| 25 |
raise OutputError(('%s does not follow the "xxx-yyy.EXT" ' |
| 26 |
'naming convention.') % input_file) |
| 27 |
iso_code = iso_code.groups()[0] |
| 28 |
readme = None |
| 29 |
for match in (re.search('^README.([a-z]{3}-[a-z]{3}).*', f) |
| 30 |
for f in os.listdir(input_dir)): |
| 31 |
if not match: |
| 32 |
continue |
| 33 |
if match.groups()[0] == iso_code: |
| 34 |
readme = match.string |
| 35 |
break |
| 36 |
if readme: |
| 37 |
shutil.copy(os.path.join(input_dir, readme), |
| 38 |
os.path.join(output_directory, 'README.md')) |
| 39 |
else: |
| 40 |
raise OutputError("No README found.") |
| 41 |
|
| 42 |
|
| 43 |
def mk_makefile(output_path, additional_files=None): |
| 44 |
readme = [f for f in os.listdir(output_path) if f.lower().startswith('readme')] |
| 45 |
if readme: |
| 46 |
if not additional_files: |
| 47 |
additional_files = [] |
| 48 |
additional_files.append(readme[0]) |
| 49 |
|
| 50 |
additional_files = ('' if not additional_files |
| 51 |
else ' '.join(additional_files)) |
| 52 |
with open(os.path.join(output_path, 'Makefile'), 'w', encoding='utf-8') as f: |
| 53 |
f.write("""# The line below is really just a fallback and only works if you have got a copy |
| 54 |
# of the tools directory at this location. It's better to set the environment |
| 55 |
# variable in your shell. |
| 56 |
FREEDICT_TOOLS ?= ../../tools |
| 57 |
DISTFILES = COPYING %s \ |
| 58 |
freedict-P5.xml freedict-P5.rng freedict-P5.dtd freedict-dictionary.css\ |
| 59 |
INSTALL Makefile NEWS |
| 60 |
# do not generate phonemes |
| 61 |
supported_phonetics = |
| 62 |
|
| 63 |
include $(FREEDICT_TOOLS)/mk/dicts.mk |
| 64 |
""" % additional_files) |
| 65 |
|
| 66 |
def reindent_xml(file_name): |
| 67 |
"""Reindent an XML file with Python's minidom package. Please note that this |
| 68 |
will likely use a large amount of main memory.""" |
| 69 |
try: |
| 70 |
tree = xml.dom.minidom.parse(file_name) |
| 71 |
with open(file_name, 'w', encoding='utf-8') as f: |
| 72 |
reindented = tree.toprettyxml(indent=" ") |
| 73 |
# avoid multiple empty lines, inserted by minidom |
| 74 |
f.write('\n'.join(l for l in reindented.split('\n') if l.strip())) |
| 75 |
except MemoryError: |
| 76 |
print("Unable to reindent file using Python's minidom module.") |
| 77 |
if shutil.which('xmllint'): |
| 78 |
print("Using xmllint for reindentation.") |
| 79 |
ret = os.system('xmllint --format {0} > {0}.tmp'.format(file_name)) |
| 80 |
if ret: |
| 81 |
print("Aborting.") |
| 82 |
sys.exit(21) |
| 83 |
with open(file_name + '.tmp', encoding='utf-8') as infile: |
| 84 |
with open(file_name, 'w', encoding='utf-8') as out: |
| 85 |
out.write(infile.read()) |
| 86 |
os.remove(file_name + '.tmp') |
| 87 |
else: |
| 88 |
print('\n'.join(textwrap.wrap(("Please install xmllint as a fallback (this " |
| 89 |
"script will automatically use it) or use some other magic to " |
| 90 |
"increase the available main memory."), 78))) |
| 91 |
sys.exit(9) |
| 92 |
|
| 93 |
|