Parse user's cwl files

This commit is contained in:
Jerome Lelong 2021-02-15 17:08:22 +01:00
parent 71ab03f60d
commit b59f061bed
2 changed files with 58 additions and 14 deletions

View File

@ -1,6 +1,8 @@
# Development scripts # Development scripts
We describe the purpose of the scripts found in directory `dev/`. We describe the purpose of the scripts found in the current directory.
These scripts are actually only frontend to the `pyintel` package, which implements the core mechanisms.
## ctanpkglist.py ## ctanpkglist.py
@ -29,7 +31,21 @@ It parses uni-math symbols from http://milde.users.sourceforge.net/LUCR/Math/dat
## pkgcommand.py ## pkgcommand.py
This script generates intellisense data from [https://github.com/LaTeXing/LaTeX-cwl/](https://github.com/LaTeXing/LaTeX-cwl/). For every package or class, two files are generated: ```
usage: pkgcommand.py [-h] [-o OUTDIR] [-i INFILE [INFILE ...]]
optional arguments:
-h, --help show this help message and exit
-o OUTDIR, --outdir OUTDIR
Directory where to write the JSON files. Default is /Users/jl/devel/LaTeX-
Workshop/data/packages
-i INFILE [INFILE ...], --infile INFILE [INFILE ...]
Files to process. Default is the content of https://github.com/LaTeXing/LaTeX-cwl/o
```
This script generates intellisense data from the files given by `-i` option and writes the generated `.json` files to the directory specified by `-o`. Note that the directory must already exist.
For every package or class, two files are generated:
- a `_cmd.json` file containing the data for the commands defined in the `.cwl` file. Entry example - a `_cmd.json` file containing the data for the commands defined in the `.cwl` file. Entry example

View File

@ -2,6 +2,8 @@ import json
import urllib.request import urllib.request
import zipfile import zipfile
from shutil import copy from shutil import copy
import argparse
import sys
from pathlib import Path from pathlib import Path
from typing import List from typing import List
from pyintel import CwlIntel from pyintel import CwlIntel
@ -14,6 +16,20 @@ UNIMATHSYMBOLS = CWD.joinpath('unimathsymbols.txt').resolve()
COMMANDS_FILE = CWD.joinpath('../data/commands.json').resolve() COMMANDS_FILE = CWD.joinpath('../data/commands.json').resolve()
ENVS_FILE = CWD.joinpath('../data/environments.json').resolve() ENVS_FILE = CWD.joinpath('../data/environments.json').resolve()
OUT_DIR = CWD.joinpath('../data/packages').resolve() OUT_DIR = CWD.joinpath('../data/packages').resolve()
INFILES = None
parser = argparse.ArgumentParser()
parser.add_argument('-o', '--outdir', help='Directory where to write the JSON files. Default is {}'.format(OUT_DIR), type=str)
parser.add_argument('-i', '--infile', help='Files to process. Default is the content of https://github.com/LaTeXing/LaTeX-cwl/', type=str, nargs='+')
args = parser.parse_args()
if args.outdir:
OUT_DIR = Path(args.outdir).expanduser().resolve()
if not OUT_DIR.is_dir():
print('The path passed to --outdir is not a directory: {}'.format(args.outdir))
sys.exit(0)
if args.infile:
INFILES = args.infile
def get_cwl_files() -> List[Path]: def get_cwl_files() -> List[Path]:
@ -30,9 +46,12 @@ def get_cwl_files() -> List[Path]:
files.append(f) files.append(f)
return files return files
def dump_dict(dictionnary, out_json):
if dictionnary != {}:
json.dump(dictionnary, open(out_json, 'w', encoding='utf8'), indent=2, ensure_ascii=False)
def parse_cwl_files():
cwl_files = get_cwl_files() def parse_cwl_files(cwl_files):
cwlIntel = CwlIntel(COMMANDS_FILE, ENVS_FILE, UNIMATHSYMBOLS) cwlIntel = CwlIntel(COMMANDS_FILE, ENVS_FILE, UNIMATHSYMBOLS)
for cwl_file in cwl_files: for cwl_file in cwl_files:
# Skip some files # Skip some files
@ -42,15 +61,24 @@ def parse_cwl_files():
if cwl_file.name in FILES_TO_REMOVE_SPACES_IN: if cwl_file.name in FILES_TO_REMOVE_SPACES_IN:
remove_spaces = True remove_spaces = True
(pkg_cmds, pkg_envs) = cwlIntel.parse_cwl_file(cwl_file, remove_spaces) (pkg_cmds, pkg_envs) = cwlIntel.parse_cwl_file(cwl_file, remove_spaces)
if pkg_envs: dump_dict(pkg_envs, OUT_DIR.joinpath(cwl_file.stem + '_env.json'))
json.dump(pkg_envs, open(OUT_DIR.joinpath(cwl_file.stem + '_env.json'), 'w', encoding='utf8'), indent=2, ensure_ascii=False) dump_dict(pkg_cmds, OUT_DIR.joinpath(cwl_file.stem + '_cmd.json'))
if pkg_cmds != {}:
json.dump(pkg_cmds, open(OUT_DIR.joinpath(cwl_file.stem + '_cmd.json'), 'w', encoding='utf8'), indent=2, ensure_ascii=False)
parse_cwl_files() if __name__ == '__main__':
# Handle aggregated files do_copy = False
for scr in ['scrartcl', 'scrreprt', 'scrbook']: if INFILES is None:
dest = OUT_DIR.joinpath('class-' + scr) cwl_files = get_cwl_files()
copy(OUT_DIR.joinpath('class-scrartcl,scrreprt,scrbook_cmd.json'), dest.as_posix() + '_cmd.json') do_copy = True
copy(OUT_DIR.joinpath('class-scrartcl,scrreprt,scrbook_env.json'), dest.as_posix() + '_env.json') else:
# Convert to an array of Path objects
cwl_files = [Path(f) for f in INFILES]
parse_cwl_files(cwl_files)
if do_copy:
# Handle aggregated files
for scr in ['scrartcl', 'scrreprt', 'scrbook']:
dest = OUT_DIR.joinpath('class-' + scr)
copy(OUT_DIR.joinpath('class-scrartcl,scrreprt,scrbook_cmd.json'), dest.as_posix() + '_cmd.json')
copy(OUT_DIR.joinpath('class-scrartcl,scrreprt,scrbook_env.json'), dest.as_posix() + '_env.json')