# 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',
        'ExpressionParserService.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()

# 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()

Refactor ExpressionParserService.js (#7592)