forked from oppia/oppia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_expression_parser.py
76 lines (61 loc) · 2.6 KB
/
create_expression_parser.py
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
# Copyright 2019 The Oppia Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS-IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""This script produces the expression parser."""
from __future__ import absolute_import # pylint: disable=import-only-modules
from __future__ import unicode_literals # pylint: disable=import-only-modules
import argparse
import fileinput
import os
import re
import subprocess
import python_utils
from . import common
from . import setup
_PARSER = argparse.ArgumentParser(description="""
Run this script from the oppia root folder:
python -m scripts.create_expression_parser
The root folder MUST be named 'oppia'.
""")
def main(args=None):
"""Produces the expression parser."""
unused_parsed_args = _PARSER.parse_args(args=args)
setup.main(args=[])
expression_parser_definition = os.path.join(
'core', 'templates', 'dev', 'head', 'expressions', 'parser.pegjs')
expression_parser_js = os.path.join(
'core', 'templates', 'dev', 'head', 'expressions',
'expression-parser.service.js')
common.install_npm_library('pegjs', '0.8.0', common.OPPIA_TOOLS_DIR)
subprocess.call([
os.path.join(common.NODE_MODULES_PATH, 'pegjs', 'bin', 'pegjs'),
expression_parser_definition, expression_parser_js])
for line in fileinput.input(files=[expression_parser_js], inplace=True):
# Inside this loop the STDOUT will be redirected to the file,
# expression_parser_js. The end='' is needed to avoid double line
# breaks.
python_utils.PRINT(
re.sub(
r'module\.exports.*$',
'angular.module(\'oppia\').factory('
'\'ExpressionParserService\', [\'$log\', function($log) {',
line), end='')
for line in fileinput.input(files=[expression_parser_js], inplace=True):
# Inside this loop the STDOUT will be redirected to the file,
# expression_parser_js. The end='' is needed to avoid double line
# breaks.
python_utils.PRINT(
re.sub(r'^\}\)\(\);\s*$', '}]);', line), end='')
python_utils.PRINT('Done!')
if __name__ == '__main__':
main()