Example #1
0
def test_extract_symbols_raises_exception_with_non_python_code():
    source = ("=begin A Ruby program :)=end\n"
              "def hello\n"
              "puts 'Hello world'\n"
              "end")
    with nt.assert_raises(SyntaxError):
        code.extract_symbols(source, "hello")
Example #2
0
def test_extract_symbols_raises_exception_with_non_python_code():
    source = ("=begin A Ruby program :)=end\n"
              "def hello\n"
              "puts 'Hello world'\n"
              "end")
    with nt.assert_raises(SyntaxError):
        code.extract_symbols(source, "hello")
Example #3
0
def test_extract_symbols():
    source = """import foo\na = 10\ndef b():\n    return 42\n\n\nclass A: pass\n\n\n"""
    symbols_args = ["a", "b", "A", "A,b", "A,a", "z"]
    expected = [([], ['a']), (["def b():\n    return 42\n"], []),
                (["class A: pass\n"], []),
                (["class A: pass\n", "def b():\n    return 42\n"], []),
                (["class A: pass\n"], ['a']), ([], ['z'])]
    for symbols, exp in zip(symbols_args, expected):
        nt.assert_equal(code.extract_symbols(source, symbols), exp)
Example #4
0
def test_extract_symbols():
    source = """import foo\na = 10\ndef b():\n    return 42\n\n\nclass A: pass\n\n\n"""
    symbols_args = ["a", "b", "A", "A,b", "A,a", "z"]
    expected = [([], ['a']),
                (["def b():\n    return 42\n"], []),
                (["class A: pass\n"], []),
                (["class A: pass\n", "def b():\n    return 42\n"], []),
                (["class A: pass\n"], ['a']),
                ([], ['z'])]
    for symbols, exp in zip(symbols_args, expected):
        nt.assert_equal(code.extract_symbols(source, symbols), exp)
Example #5
0
    def addscript(self, pars='', cell=None):
        """
        This works both as **%addscript** and as **%%addscript**

        This magic command can either take a local filename, element in the
        namespace or history range (see %history),
        or the current cell content


        Usage:

            %addscript  -p project  n1-n2 n3-n4 ... n5 .. n6 ...

            or

            %%addscript -p project
            ...code lines ...


        Options:

            -p <string>         Name of the project where the script will be stored.
                                If not provided, a project with a standard
                                name : `proj` is searched.
            -o <string>         script name
            -s <symbols>        Specify function or classes to load from python
                                source.
            -a                  append to the current script instead of
                                overwriting it.
            -n                  search symbol in the current namespace


        Examples
        --------

        .. sourcecode:: ipython

           In[1]: %addscript myscript.py

           In[2]: %addscript 7-27

           In[3]: %addscript -s MyClass,myfunction myscript.py
           In[4]: %addscript MyClass

           In[5]: %addscript mymodule.myfunction
        """
        opts, args = self.parse_options(pars, 'p:o:s:n:a')

        # append = 'a' in opts
        # mode = 'a' if append else 'w'
        search_ns = 'n' in opts

        if not args and not cell and not search_ns:  # pragma: no cover
            raise UsageError('Missing filename, input history range, '
                             'or element in the user namespace.\n '
                             'If no argument are given then the cell content '
                             'should '
                             'not be empty')
        name = 'script'
        if 'o' in opts:
            name = opts['o']

        proj = 'proj'
        if 'p' in opts:
            proj = opts['p']
        if proj not in self.shell.user_ns:  # pragma: no cover
            raise ValueError('Cannot find any project with name `{}` in the '
                             'namespace.'.format(proj))
        # get the proj object
        projobj = self.shell.user_ns[proj]

        contents = ""
        if search_ns:
            contents += "\n" + self.shell.find_user_code(
                opts['n'], search_ns=search_ns) + "\n"

        args = " ".join(args)
        if args.strip():
            contents += "\n" + self.shell.find_user_code(
                args, search_ns=search_ns) + "\n"

        if 's' in opts:  # pragma: no cover
            try:
                blocks, not_found = extract_symbols(contents, opts['s'])
            except SyntaxError:
                # non python code
                logging.error("Unable to parse the input as valid Python code")
                return

            if len(not_found) == 1:
                warnings.warn('The symbol `%s` was not found' % not_found[0])
            elif len(not_found) > 1:
                warnings.warn('The symbols %s were not found' %
                              get_text_list(not_found, wrap_item_with='`'))

            contents = '\n'.join(blocks)

        if cell:
            contents += "\n" + cell

        # import delayed to avoid circular import error
        from spectrochempy.core.scripts.script import Script

        script = Script(name, content=contents)
        projobj[name] = script

        return "Script {} created.".format(name)
    def addscript(self, pars="", cell=None):
        """
        This works both as **%addscript** and as **%%addscript**.

        This magic command can either take a local filename, element in the
        namespace or history range (see %history),
        or the current cell content.


        Usage:

            %addscript  -p project  n1-n2 n3-n4 ... n5 .. n6 ...

            or

            %%addscript -p project
            ...code lines ...


        Options:

            -p <string>         Name of the project where the script will be stored.
                                If not provided, a project with a standard
                                name : `proj` is searched.
            -o <string>         script name.
            -s <symbols>        Specify function or classes to load from python
                                source.
            -a                  append to the current script instead of
                                overwriting it.
            -n                  Search symbol in the current namespace.


        Examples
        --------

        .. sourcecode:: ipython

           In[1]: %addscript myscript.py

           In[2]: %addscript 7-27

           In[3]: %addscript -s MyClass,myfunction myscript.py
           In[4]: %addscript MyClass

           In[5]: %addscript mymodule.myfunction
        """
        opts, args = self.parse_options(pars, "p:o:s:n:a")

        # append = 'a' in opts
        # mode = 'a' if append else 'w'
        search_ns = "n" in opts

        if not args and not cell and not search_ns:  # pragma: no cover
            raise UsageError("Missing filename, input history range, "
                             "or element in the user namespace.\n "
                             "If no argument are given then the cell content "
                             "should "
                             "not be empty")
        name = "script"
        if "o" in opts:
            name = opts["o"]

        proj = "proj"
        if "p" in opts:
            proj = opts["p"]
        if proj not in self.shell.user_ns:  # pragma: no cover
            raise ValueError(
                f"Cannot find any project with name `{proj}` in the namespace."
            )
        # get the proj object
        projobj = self.shell.user_ns[proj]

        contents = ""
        if search_ns:
            contents += ("\n" + self.shell.find_user_code(
                opts["n"], search_ns=search_ns) + "\n")

        args = " ".join(args)
        if args.strip():
            contents += ("\n" +
                         self.shell.find_user_code(args, search_ns=search_ns) +
                         "\n")

        if "s" in opts:  # pragma: no cover
            try:
                blocks, not_found = extract_symbols(contents, opts["s"])
            except SyntaxError:
                # non python code
                logging.error("Unable to parse the input as valid Python code")
                return None

            if len(not_found) == 1:
                warnings.warn(f"The symbol `{not_found[0]}` was not found")
            elif len(not_found) > 1:
                sym = get_text_list(not_found, wrap_item_with="`")
                warnings.warn(f"The symbols {sym} were not found")

            contents = "\n".join(blocks)

        if cell:
            contents += "\n" + cell

        # import delayed to avoid circular import error
        from spectrochempy.core.scripts.script import Script

        script = Script(name, content=contents)
        projobj[name] = script

        return f"Script {name} created."