<commit_before>import os
import re

from collections import OrderedDict
from subprocess import Popen, PIPE

from notebook.utils import url_path_join
from notebook.base.handlers import IPythonHandler

from jinja2 import Template
import tornado.ioloop
import tornado.web

modules_template = """
<html><body><form method="POST">
{% for loc, modules in moduleavail.items() %}
  <h2>{{ loc }}</h2>
  <table>
      <tr>
      {% for module in modules %}
      <td><input type="checkbox" name="module" value="{{ module }}" {% if module in modulelist %} checked {% endif %}> {{ module }} </td>
      {% if loop.index % 4 == 0 %}
            </tr><tr>
      {% endif %}
      {% endfor %}
      </tr>
  </table>
{% endfor %}
<input type="submit">
</form></body></html>
"""

def module(command, arguments=""):
    result = Popen('$LMOD_CMD python --terse {} {}'.format(command,
                                                           arguments),
                   shell=True,
                   stdout=PIPE,
                   stderr=PIPE,
                   bufsize=-1)
    if command == 'load' or command == 'unload':
        exec(result.stdout.read())

    return result.stderr.read().decode()

def module_avail():
    string = module('avail')
    list_ = string.split()
    locations = OrderedDict()
    cur_location = None
    for i, entry in enumerate(list_):
        if entry.startswith('/'):
            if entry.startswith('/software6/modulefiles'):
                entry = os.path.relpath(entry, '/software6/modulefiles')
            cur_location = entry
            locations[cur_location] = []
        elif (re.match("[A-Za-z\/]*", entry) and
              i != (len(list_) - 1) and
              list_[i+1].startswith(entry)):
            continue
        else:
            locations[cur_location].append(entry)
    return locations

def module_list():
    return set(module('list').split())

class LmodHandler(IPythonHandler):
    template = Template(modules_template)

    def initialize(self):
        self.module_list = module_list()

    @tornado.web.authenticated
    def get(self):
        self.write(self.template.render(moduleavail=module_avail(),
                                        modulelist=self.module_list))

    @tornado.web.authenticated
    def post(self):
        checked_modules = self.request.arguments['module']
        new_modulelist = set(map(bytes.decode, checked_modules))

        to_load = new_modulelist - self.module_list
        to_unload = self.module_list - new_modulelist

        module('load', " ".join(to_load))
        module('unload', " ".join(to_unload))

        self.module_list = module_list()
        self.write(self.template.render(moduleavail=module_avail(),
                                        modulelist=self.module_list))

def load_jupyter_server_extension(nbapp):
    """
    Called when the extension is loaded.

    Args:
        nb_server_app (NotebookWebApplication): handle to the Notebook webserver instance.
    """
    nbapp.log.info("Loading lmod extension")
    web_app = nbapp.web_app
    host_pattern = '.*$'
    route_pattern = url_path_join(web_app.settings['base_url'], '/lmod')
    web_app.add_handlers(host_pattern, [(route_pattern, LmodHandler)])


if __name__ == "__main__":
    app = tornado.web.Application([ (r"/", LmodHandler), ])<commit_msg>Move lmod commands in their own package<commit_after>import json

import tornado.ioloop
import tornado.web
import lmod

from notebook.utils import url_path_join
from notebook.base.handlers import IPythonHandler

from jinja2 import Template

modules_template = """
<html><body><form method="POST">
{% for loc, modules in moduleavail.items() %}
  <h2>{{ loc }}</h2>
  <table>
      <tr>
      {% for module in modules %}
      <td><input type="checkbox" name="module" value="{{ module }}" {% if module in modulelist %} checked {% endif %}> {{ module }} </td>
      {% if loop.index % 4 == 0 %}
            </tr><tr>
      {% endif %}
      {% endfor %}
      </tr>
  </table>
{% endfor %}
<input type="submit">
</form></body></html>
"""

class LmodActionHandler(IPythonHandler):
    @tornado.web.authenticated
    def get(self, action):
        if action == 'avail':
            self.finish(json.dumps(lmod.module_avail()))
        elif action == 'list':
            self.finish(json.dumps(lmod.module_list()))

    @tornado.web.authenticated
    def post(self, action):
        if action in ('load', 'unload'):
            module = self.get_argument('module', default=None)
            if module:
                lmod.module(action, module)
                self.finish(json.dumps('SUCCESS'))

class LmodHandler(IPythonHandler):
    template = Template(modules_template)

    def initialize(self):
        self.module_list = module_list()

    @tornado.web.authenticated
    def get(self):
        self.write(self.template.render(moduleavail=module_avail(),
                                        modulelist=self.module_list))

    @tornado.web.authenticated
    def post(self):
        checked_modules = self.request.arguments['module']
        new_modulelist = set(map(bytes.decode, checked_modules))

        to_load = new_modulelist - self.module_list
        to_unload = self.module_list - new_modulelist

        lmod.module('load', " ".join(to_load))
        lmod.module('unload', " ".join(to_unload))

        self.module_list = lmod.module_list()
        self.write(self.template.render(moduleavail=module_avail(),
                                        modulelist=self.module_list))


#-----------------------------------------------------------------------------
# URL to handler mappings
#-----------------------------------------------------------------------------
_lmod_action_regex = r"(?P<action>load|unload|avail|list)"

default_handlers = [
    (r"/lmod", LmodHandler),
    (r"/lmod/%s" % (_lmod_action_regex), LmodActionHandler),
]

def load_jupyter_server_extension(nbapp):
    """
    Called when the extension is loaded.

    Args:
        nb_server_app (NotebookWebApplication): handle to the Notebook webserver instance.
    """
    nbapp.log.info("Loading lmod extension")
    web_app = nbapp.web_app
    host_pattern = '.*$'
    for handler in default_handlers:
        route_pattern = url_path_join(web_app.settings['base_url'], handler[0])
        web_app.add_handlers(host_pattern, [(route_pattern, handler[1])])

if __name__ == "__main__":
    app = tornado.web.Application([ (r"/", LmodHandler), ])
{'fraction_non_alphanumeric': 0.15510835913312693, 'fraction_numerical': 0.002631578947368421, 'mean_word_length': 2.4348750664540137, 'pattern_counts': {'":': 2, '<': 40, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1336741', 'n_tokens_mistral': 1983, 'n_tokens_neox': 1885, 'n_words': 395}