<commit_before>#!/usr/bin/python3 -tt

# Copyright 2012 <NAME>

# 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.

import mparser
import nodes
import environment
import os, sys, platform

class InterpreterException(Exception):
    pass

class InvalidCode(InterpreterException):
    pass

class InvalidArguments(InterpreterException):
    pass

class InterpreterObject():
    
    def __init__(self):
        self.methods = {}

    def method_call(self, method_name, args, kwargs):
        if method_name in self.methods:
            return self.methods[method_name](args, kwargs)
        raise InvalidCode('Unknown method "%s" in object.' % method_name)

class Generator(InterpreterObject):

    def __init__(self, args, kwargs):
        InterpreterObject.__init__(self)
        if len(args) != 1:
            raise InvalidArguments('Generator requires one and only one positional argument')
        if not isinstance(args[0], Executable):
            raise InvalidArguments('First generator argument must be an executable object.')
        self.exe = args[0]
        self.methods.update({'process' : self.process_method})
        self.process_kwargs(kwargs)
    
    def get_exe(self):
        return self.exe

    def process_kwargs(self, kwargs):
        if 'arguments' not in kwargs:
            raise InvalidArguments('Generator must have "arguments" keyword argument.')
        args = kwargs['arguments']
        if isinstance(args, str):
            args = [args]
        if not isinstance(args, list):
            raise InvalidArguments('"Arguments" keyword argument must be a string or a list of strings.')
        for a in args:
            if not isinstance(a, str):
                raise InvalidArguments('A non-string object in "arguments" keyword argument.')
        self.arglist = args
        
        if 'name_rule' not in kwargs:
            raise InvalidArguments('Generator must have "name_rule" keyword argument.')
        rule = kwargs['name_rule']
        if not isinstance(rule, str):
            raise InvalidArguments('"name_rule" keyword argument must be a string.')
        if not '@BASENAME@' in rule:
            raise InvalidArguments('"name_rule" must contain @BASENAME@.')
        self.name_rule = rule

    def get_base_outname(self, inname):
        base = os.path.split(inname)[1]
        return self.name_rule.replace('@BASENAME@', base)

    def process_method(self, args, kwargs):
        if len(kwargs) > 0:
            raise InvalidArguments('Process does not take keyword arguments.')
        if isinstance(args, str):
            args = [args]
        if not isinstance(args, list):
            raise InvalidArguments('Argument to "process" must be a string or a list of strings.')<commit_msg>Prevent slashes in generated names.<commit_after>#!/usr/bin/python3 -tt

# Copyright 2012 <NAME>

# 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.

import mparser
import nodes
import environment
import os, sys, platform

class InterpreterException(Exception):
    pass

class InvalidCode(InterpreterException):
    pass

class InvalidArguments(InterpreterException):
    pass

class InterpreterObject():
    
    def __init__(self):
        self.methods = {}

    def method_call(self, method_name, args, kwargs):
        if method_name in self.methods:
            return self.methods[method_name](args, kwargs)
        raise InvalidCode('Unknown method "%s" in object.' % method_name)

class Generator(InterpreterObject):

    def __init__(self, args, kwargs):
        InterpreterObject.__init__(self)
        if len(args) != 1:
            raise InvalidArguments('Generator requires one and only one positional argument')
        if not isinstance(args[0], Executable):
            raise InvalidArguments('First generator argument must be an executable object.')
        self.exe = args[0]
        self.methods.update({'process' : self.process_method})
        self.process_kwargs(kwargs)
    
    def get_exe(self):
        return self.exe

    def process_kwargs(self, kwargs):
        if 'arguments' not in kwargs:
            raise InvalidArguments('Generator must have "arguments" keyword argument.')
        args = kwargs['arguments']
        if isinstance(args, str):
            args = [args]
        if not isinstance(args, list):
            raise InvalidArguments('"Arguments" keyword argument must be a string or a list of strings.')
        for a in args:
            if not isinstance(a, str):
                raise InvalidArguments('A non-string object in "arguments" keyword argument.')
        self.arglist = args
        
        if 'name_rule' not in kwargs:
            raise InvalidArguments('Generator must have "name_rule" keyword argument.')
        rule = kwargs['name_rule']
        if not isinstance(rule, str):
            raise InvalidArguments('"name_rule" keyword argument must be a string.')
        if not '@BASENAME@' in rule:
            raise InvalidArguments('"name_rule" must contain @BASENAME@.')
        if '/' in rule:
            raise InvalidArguments('"name_rule" must not contain a slash.')
        self.name_rule = rule

    def get_base_outname(self, inname):
        base = os.path.split(inname)[1]
        return self.name_rule.replace('@BASENAME@', base)

    def process_method(self, args, kwargs):
        if len(kwargs) > 0:
            raise InvalidArguments('Process does not take keyword arguments.')
        if isinstance(args, str):
            args = [args]
        if not isinstance(args, list):
            raise InvalidArguments('Argument to "process" must be a string or a list of strings.')
{'fraction_non_alphanumeric': 0.08908482821526299, 'fraction_numerical': 0.004256612952265126, 'mean_word_length': 2.872277810476751, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2794934', 'n_tokens_mistral': 1720, 'n_tokens_neox': 1697, 'n_words': 625}