Example #1
0
def static_content(environ, start_response):
    path = getpath(environ)

    if not path:  # redirect
        start_response('301 Redirect', [content_type(), ('Location', environ['REQUEST_URI'] + '/')])
        return []

    if path == '/':
        path = 'web/index.html'  # map / to index.html
    else:
        path = ('web/' + path).replace('//', '/')

    if path == 'web/js':  # combined java scripts
        scripts = {}
        for s in resource_listdir('ctplot', 'web/js'):
            scripts[s] = '\n// {}\n\n'.format(s) + resource_string('ctplot', 'web/js/' + s)
        start_response('200 OK', [content_type('combined.js'), cc_cache])
        return [scripts[k] for k in sorted(scripts.keys())]

    if not resource_exists('ctplot', path):  # 404
        start_response('404 Not Found', [content_type()])
        return ['404\n', '{} not found!'.format(path)]

    elif resource_isdir('ctplot', path):  # 403
        start_response('403 Forbidden', [content_type()])
        return ['403 Forbidden']
    else:
        start_response('200 OK', [content_type(path), cc_cache])
        return resource_string('ctplot', path)
Example #2
0
File: cli.py Project: johbo/pip2nix
def scaffold(output, overrides_output, **kwargs):
    import pip2nix

    config = Config()
    if kwargs['configuration']:
        config.load(kwargs['configuration'])
    else:
        config.find_and_load()
    config.merge_cli_options(kwargs)
    # TODO: Config enforces requirements to be specified, find a nicer
    # way to let Config know that we don't need requirements here.
    config.merge_options({'pip2nix': {'requirements': []}})
    config.validate()

    import jinja2
    raw_template = pkg_resources.resource_string(__name__, 'default.nix.j2')
    t = jinja2.Template(raw_template.decode('utf-8'))
    with open(output, 'w') as f:
        f.write(t.render(
            package_name=kwargs['package'],
            pip2nix_version=pip2nix.__version__))

    overrides_template = pkg_resources.resource_string(
        __name__, 'python-packages-overrides.nix.j2')
    t = jinja2.Template(overrides_template.decode('utf-8'))
    with open(overrides_output, 'w') as f:
        f.write(t.render(
            package_name=kwargs['package'],
            pip2nix_version=pip2nix.__version__))
Example #3
0
def create_config(cfg_dict):
    """Create the default director.ini in the current directory based
    on a the template stored in director.templatecfg

    """
    from evasion.director import templatecfg
    from pkg_resources import resource_string

    get_log().debug("Creating initial configuration.")

    # Fill in the template information with viewpoint path:
    cfg_dict['viewpoint_path'] = ''
    try:
        # Attempt to set up the path to the evasion viewpoint XUL app
        # installed as a python package.
        from evasion import viewpoint
    except ImportError:
        pass
    else:
        cfg_dict['viewpoint_path'] = os.path.abspath(viewpoint.__path__[0])

    # Fill in the template information with XUL Browser path:
    t = resource_string(templatecfg.__name__, 'director.cfg.mako')
    render_config(cfg_dict, t, DEFAULT_CONFIG_NAME)

    t = resource_string(templatecfg.__name__, 'servicestation.cfg.mako')
    render_config(cfg_dict, t, DEFAULT_SERVICESTATION_NAME)

    print("Success, '%s' and '%s' created ok." % (DEFAULT_CONFIG_NAME, DEFAULT_SERVICESTATION_NAME))
Example #4
0
    def __init__(self):
        '''
        Constructor
        '''
        # Some physical constants
        # Boltzmann constant
        self.kb = 1.3806488e-23
        # Speed of light
        self.c = 299792458.0
        # Planck constant
        self.h = 6.62606957e-34

        # Load S_n(lambda) for D-illuminant
        data = resource_string(__name__,  'data/SforD-illuminant.txt')

        dtypes = {'names':   ['wavelen',   'S0',  'S1',  'S2'],
                  'formats': [float,      float, float, float]
                  }
        self.D_S_Data = np.loadtxt(io.BytesIO(data),
                                   dtype=dtypes)

        # Load Illuminant F Spectrums
        data = resource_string(__name__,  'data/IlluminantF.txt')

        dtypes = {'names':   ['wavelen',  'F01', 'F02',
                              'F03', 'F04', 'F05', 'F06', 'F07',
                              'F08', 'F09', 'F10', 'F11', 'F12'
                              ],
                  'formats': [float, float, float, float, float,
                              float, float, float, float, float,
                              float, float, float]
                  }
        self.illumF = np.loadtxt(io.BytesIO(data),
                                 dtype=dtypes)
Example #5
0
  def _install(self):
    self.requirements, self.ws = self.egg.working_set()
    parameter_dict = self.computer_partition.getInstanceParameterDict()
    ipv4 = self.getLocalIPv4Address()
    ipv6 = self.getGlobalIPv6Address()

    self.install_mysql_server_configuration(ipv4)

    port = '8900'
    tomcat_home = os.path.join(self.data_root_directory, 'tomcat')
    tomcat_lib = os.path.join(tomcat_home, 'lib')
    xwiki_home = os.path.join(tomcat_home, 'webapps', 'xwiki')
    for src, dst in (
        (self.options['tomcat_location'].strip(), tomcat_home),
        (self.options['xwiki_location'].strip(), xwiki_home),
        ):
      if not os.path.isdir(dst):
        try:
          shutil.copytree(src, dst)
        except:
          shtuil.rmtree(dst)
          raise

    shutil.copy(self.options['jdbc_location'].strip(), os.path.join(tomcat_lib,
      'jdbc.jar'))
    # headless mode
    self._writeFile(os.path.join(tomcat_home, 'bin', 'setenv.sh'), '''#!/bin/sh
export JAVA_OPTS="${JAVA_OPTS} -Djava.awt.headless=true"
''')
    # tomcat wrapper
    catalina = os.path.join(tomcat_home, 'bin', 'catalina.sh')
    # solve "The BASEDIR environment variable is not defined correctly"
    bindir = os.path.join(tomcat_home, 'bin')
    for f in os.listdir(bindir):
      if f.endswith('.sh'):
        os.chmod(os.path.join(bindir, f), 0755)
    tomcat_wrapper = self.createRunningWrapper('xwiki', """#!/bin/sh
export JRE_HOME=%(java_home)s
exec %(catalina)s run
""" % dict(java_home=self.options['java_home'].strip(),
  catalina = catalina))

    tomcat_dict = dict(
      http_address=ipv6,
      http_port=port,
      ajp_address=ipv4,
      ajp_port="8009",
        )
    server_xml = os.path.join(tomcat_home, 'conf', 'server.xml')
    self._writeFile(server_xml, pkg_resources.resource_string(__name__,
      'template/tomcat-server.xml.in') % tomcat_dict)

    hibernate_xml = os.path.join(tomcat_home, 'webapps', 'xwiki', 'WEB-INF', 'hibernate.cfg.xml')
    self._writeFile(hibernate_xml, pkg_resources.resource_string(__name__,
      'template/hibernate.cfg.xml.in') % dict(mysql_port='45678', mysql_ip = ipv4))

    self.computer_partition.setConnectionDict(dict(
      http_connection="http://[%s]:%s/xwiki/" % (ipv6, port),
      ))
    return [server_xml, tomcat_wrapper]
Example #6
0
def resource_string(name, pfx=None):
    """
Attempt to load and return the contents (as a string) of the resource named by
the first argument in the first location of:

# as name in the current directory
# as name in the `pfx` subdirectory of the current directory if provided
# as name relative to the package
# as pfx/name relative to the package

The last two alternatives is used to locate resources distributed in the package.
This includes certain XSLT and XSD files.

:param name: The string name of a resource
:param pfx: An optional prefix to use in searching for name

    """
    if os.path.exists(name):
        with open(name) as fd:
            return fd.read()
    elif pfx and os.path.exists(os.path.join(pfx, name)):
        with open(os.path.join(pfx, name)) as fd:
            return fd.read()
    elif pkg_resources.resource_exists(__name__, name):
        return pkg_resources.resource_string(__name__, name)
    elif pfx and pkg_resources.resource_exists(__name__, "%s/%s" % (pfx, name)):
        return pkg_resources.resource_string(__name__, "%s/%s" % (pfx, name))

    return None
Example #7
0
    def render(self):
        output_dir = os.path.join(self.rootdir, "debian")

        if os.path.exists(output_dir):
            res = raw_input("A debian directory exists. Replace it? [Y/n]: ")
            if res.lower() in ["n", "no"]:
                raise DebianConfigurationException(
                    "Not removing debian directory")
            shutil.rmtree(output_dir)

        os.mkdir(output_dir)

        for template in self.DEBIAN_CONFIGURATION_TEMPLATES:
            filename = os.path.basename(template).replace(".j2", "")
            content = Template(resource_string("make_deb", template)) \
                .render(self.context)

            with open(os.path.join(output_dir, filename), "wb") as f:
                f.write(content)

        # Need to to trigger separately because filename must change
        trigger_content = Template(
            resource_string("make_deb", "resources/debian/triggers.j2")
        ).render(self.context)

        trigger_filename = "%s.triggers" % self.context['name']
        with open(os.path.join(output_dir, trigger_filename), "wb") as f:
            f.write(trigger_content+"\n")
Example #8
0
File: ui.py Project: 8cylinder/sink
    def build_webpage(self):
        html = pk.resource_string(self.res_dir, 'index.html').decode('utf-8')
        js = pk.resource_string(self.res_dir, 'main.js').decode('utf-8')
        jsutil = pk.resource_string(self.res_dir, 'util.js').decode('utf-8')
        css = pk.resource_string(self.res_dir, 'main.css').decode('utf-8')
        logo = pk.resource_string(self.res_dir, 'sink.png')
        logo = base64.b64encode(logo).decode('utf-8')

        # insert js
        html = html.replace('[[js]]', js)
        html = html.replace('[[util_js]]', jsutil)

        # insert css
        html = html.replace('[[css]]', css)

        # insert project title
        project_title = self.data['fields'][1].get('value', 'New Project')
        html = html.replace('[[project_title]]', project_title)

        # insert the form
        self.build_form(self.data['fields'], '')
        form = self.get_form()
        html = html.replace('[[form]]', form)

        # insert logo
        html = html.replace('[[logo]]', logo)

        return html
Example #9
0
 def _test_match(self, game):
     gt = GosuTicker(game)
     data = pkg_resources.resource_string('tests', f'/html/match_{game}.html').decode('utf-8')
     result = pkg_resources.resource_string('tests', f'/html/match_{game}.json').decode('utf-8')
     sel = Selector(text=data)
     matches = list(gt.find_matches(sel))
     assert json.dumps(matches) == result
Example #10
0
    def __init__(self):
        '''
        Constructor
        '''

        # Directory for datafiles
        data = resource_string(
            __name__, "data/ThorlabsMirrorPF20-03-G01_20deg.txt")

        # Load A Thorlabs mirror PF20-03-G01 regular reflectance data
        dtypes = {'names':   ['wavelen',   'p-pol',  's-pol',  'ave'],
                  'formats': [float,      float, float, float]
                  }

        self.PF20_03_G01_deg_20 = np.loadtxt(io.BytesIO(data), dtype=dtypes)

        data = resource_string(
            __name__, "data/ThorlabsMirrorPF20-03-G01_8deg.txt")

        # Load A Thorlabs mirror PF20-03-G01 regular reflectance data
        dtypes = {'names':   ['wavelen',   'p-pol',  's-pol',  'ave'],
                  'formats': [float,      float, float, float]
                  }

        self.PF20_03_G01_deg_8 = np.loadtxt(io.BytesIO(data), dtype=dtypes)
    def openNode(self, platform_id, node_config_filename, stream_definition_filename=DEFAULT_STREAM_DEF_FILENAME):
        """
        Opens up and parses the node configuration files.
        @param platform_id - id to associate with this set of Node Configuration Files
        @param nc_file - yaml file with information about the platform
        @raise NodeConfigurationException
        """
        try:
            node_config_string = resource_string(mi.platform.rsn.__name__, node_config_filename)
            stream_config_string = resource_string(mi.platform.rsn.__name__, stream_definition_filename)
            self._platform_id = platform_id

            node_config = yaml.load(node_config_string)
            stream_definitions = yaml.load(stream_config_string)
            self._node_yaml = NodeYAML.factory(node_config, stream_definitions)
            self._node_yaml.validate()

        except Exception as e:
            import traceback

            traceback.print_exc()
            msg = "%s Cannot parse yaml node specific config file  : %s" % (e, node_config_filename)
            raise NodeConfigurationFileException(msg=msg)
        except IOError as e:
            msg = "%s Cannot open node specific config file  : %s" % (e, node_config_filename)
            raise NodeConfigurationFileException(msg=msg)
Example #12
0
    def student_view(self, context):
        """
        Create a fragment used to display the XBlock to a student.
        `context` is a dictionary used to configure the display (unused)
        Returns a `Fragment` object specifying the HTML, CSS, and JavaScript
        to display.
        """
        provider, embed_code = self.get_embed_code_for_url(self.href)

        # Load the HTML fragment from within the package and fill in the template
        html_str = pkg_resources.resource_string(__name__, "static/html/simplevideo.html")
        frag = Fragment(unicode(html_str).format(self=self, embed_code=embed_code))

	# Load CSS
        css_str = pkg_resources.resource_string(__name__, "static/css/simplevideo.css")
        frag.add_css(unicode(css_str))

# Load JS
        if provider == 'vimeo.com':
            # Load the Froogaloop library from vimeo CDN.
            frag.add_javascript_url("//f.vimeocdn.com/js/froogaloop2.min.js")
            js_str = pkg_resources.resource_string(__name__, "static/js/simplevideo.js")
            frag.add_javascript(unicode(js_str))
            frag.initialize_js('SimpleVideoBlock')

        return frag
  def setUp(self):

    if "NTA_DYNAMIC_CONF_DIR" in os.environ:
      # Remove it to make sure our in-proc tests won't accidentally
      # mess with actual files
      oldNtaDynamicConfDir = os.environ["NTA_DYNAMIC_CONF_DIR"]
      del os.environ["NTA_DYNAMIC_CONF_DIR"]
      self.addCleanup(os.environ.update,
                      dict(NTA_DYNAMIC_CONF_DIR=oldNtaDynamicConfDir))

    self.files = dict()

    tmpDir = tempfile.mkdtemp()
    self.addCleanup(shutil.rmtree, tmpDir)


    with open(os.path.join(tmpDir, 'nupic-default.xml-unittest'), 'w') as fp:
      fp.write(resource_string(__name__, 'conf/nupic-default.xml'))
      self.files['nupic-default.xml'] = fp.name

    with open(os.path.join(tmpDir, 'nupic-site.xml-unittest'), 'w') as fp:
      fp.write(resource_string(__name__, 'conf/nupic-site.xml'))
      self.files['nupic-site.xml'] = fp.name

    with open(os.path.join(tmpDir, 'nupic-custom.xml'), 'w') as fp:
      fp.write(resource_string(__name__, 'conf/nupic-custom.xml'))
      self.files['nupic-custom.xml'] = fp.name

    self.customParam = 'nupic.custom.hello'
    self.customValue = 'world'

    configuration.Configuration.clear()
    def setUp(self):
        super(TabsEditingDescriptorTestCase, self).setUp()
        system = get_test_system()
        system.render_template = Mock(return_value="<div>Test Template HTML</div>")
        self.tabs = [
            {
                'name': "Test_css",
                'template': "tabs/codemirror-edit.html",
                'current': True,
                'css': {
                    'scss': [resource_string(__name__,
                    '../../test_files/test_tabseditingdescriptor.scss')],
                    'css': [resource_string(__name__,
                    '../../test_files/test_tabseditingdescriptor.css')]
                }
            },
            {
                'name': "Subtitles",
                'template': "videoalpha/subtitles.html",
            },
            {
                'name': "Settings",
                'template': "tabs/video-metadata-edit-tab.html"
            }
        ]

        TabsEditingDescriptor.tabs = self.tabs
        self.descriptor = TabsEditingDescriptor(
            runtime=system,
            model_data={})
Example #15
0
  def useComputer(self, computer_id, use_string):
    """Entry point to reporting usage of a computer."""
    #We retrieve XSD model
    try:
      computer_consumption_model = \
        pkg_resources.resource_string(
          'slapos.slap',
          'doc/computer_consumption.xsd')
    except IOError:
      computer_consumption_model = \
        pkg_resources.resource_string(
          __name__,
          '../../../../slapos/slap/doc/computer_consumption.xsd')

    if self._validateXML(use_string, computer_consumption_model):
      vifib_conduit_instance = VifibConduit.VifibConduit()

      #We create the SPL
      vifib_conduit_instance.addNode(
        object=self, 
        xml=use_string, 
        computer_id=computer_id)
    else:
      raise NotImplementedError("XML file sent by the node is not valid !")

    return 'Content properly posted.'
def test_refextract_url(log_in_as_cataloger, api_client):
    schema = load_schema('hep')
    subschema = schema['properties']['references']

    with requests_mock.Mocker() as requests_mocker:
        requests_mocker.register_uri(
            'GET', 'https://arxiv.org/pdf/1612.06414.pdf',
            content=pkg_resources.resource_string(
                __name__, os.path.join('fixtures', '1612.06414.pdf')),
        )
        requests_mocker.register_uri(
            'GET', 'http://test-indexer:9200/records-hep/hep/_search?_source=control_number',
            content=pkg_resources.resource_string(
                __name__, os.path.join('fixtures', 'es_response.json')),
        )

        response = api_client.post(
            '/editor/refextract/url',
            content_type='application/json',
            data=json.dumps({
                'url': 'https://arxiv.org/pdf/1612.06414.pdf',
            }),
        )
        references = json.loads(response.data)

    assert response.status_code == 200
    assert validate(references, subschema) is None
    assert get_value({'references': references}, 'references.reference.publication_info.journal_title')
Example #17
0
 def gen_resjs(self):
     """genarate res.js, should be called after added all resources
     """
     res_ajax = resource_string(__name__, 'res/res-ajax.js').decode("utf-8")
     res_core = resource_string(__name__, 'res/res-core.js').decode("utf-8")
     res_js = res_ajax + os.linesep + res_core
     self._gen_from_template(res_js, self.resjs_name)
    def setUp(self):
        super(TabsEditingDescriptorTestCase, self).setUp()
        system = get_test_descriptor_system()
        system.render_template = Mock(return_value="<div>Test Template HTML</div>")
        self.tabs = [
            {
                'name': "Test_css",
                'template': "tabs/codemirror-edit.html",
                'current': True,
                'css': {
                    'scss': [resource_string(__name__,
                    '../../test_files/test_tabseditingdescriptor.scss')],
                    'css': [resource_string(__name__,
                    '../../test_files/test_tabseditingdescriptor.css')]
                }
            },
            {
                'name': "Subtitles",
                'template': "video/subtitles.html",
            },
            {
                'name': "Settings",
                'template': "tabs/video-metadata-edit-tab.html"
            }
        ]

        TabsEditingDescriptor.tabs = self.tabs
        self.descriptor = system.construct_xblock_from_class(
            TabsEditingDescriptor,
            field_data=DictFieldData({}),
            scope_ids=ScopeIds(None, None, None, None),
        )
Example #19
0
  def _gen_options_reference(self):
    """Generate the options reference rst doc."""
    goals = gen_tasks_options_reference_data()
    filtered_goals = []
    omit_impl_regexps = [re.compile(r) for r in self.get_options().omit_impl_re]
    for g in goals:
      if any(r.match(t['impl']) for r in omit_impl_regexps for t in g.tasks):
        continue
      filtered_goals.append(g)
    glopts = gen_glopts_reference_data()

    # generate the .rst file
    template = resource_string(__name__,
                               os.path.join(self._templates_dir, 'options_reference.mustache'))
    filename = os.path.join(self._outdir, 'options_reference.rst')
    self.context.log.info('Generating {}'.format(filename))
    with safe_open(filename, 'wb') as outfile:
      generator = Generator(template, goals=filtered_goals, glopts=glopts)
      generator.write(outfile)

    # generate the .html file
    template = resource_string(__name__,
                               os.path.join(self._templates_dir, 'oref_html.mustache'))
    filename = os.path.join(self._outdir, 'options_reference.html')
    self.context.log.info('Generating {}'.format(filename))
    with safe_open(filename, 'wb') as outfile:
      generator = Generator(template, goals=filtered_goals, glopts=glopts)
      generator.write(outfile)
Example #20
0
def setup_db(db_uri='test.db'):
    #Import sql schema
    if 'postgres://' in db_uri:

        import psycopg2
        connection = psycopg2.connect(db_uri)
        cursor = connection.cursor()
        cursor.execute(resource_string(__name__, 'schema.sql'))
        connection.commit()
        cursor.close()

    else:

        import sqlite3
        connection = sqlite3.connect(db_uri)
        cursor = connection.cursor()
        schema = resource_string(__name__, 'schema.sql')
        if isinstance(schema, bytes):
            schema = schema.decode("utf-8")
        cursor.executescript(schema)
        connection.commit()
        cursor.close()

    connection.close()

    return True
Example #21
0
def put_file(dest, src, opt_dict=None):
    fh = open(dest, 'w')
    if opt_dict is not None:
        fh.write(pkg_resources.resource_string('webias', src) % opt_dict)
    else:
        fh.write(pkg_resources.resource_string('webias', src))
    fh.close()
Example #22
0
    def create_chart(self, html_path='index.html', data_path='data.json',
                     js_path='rickshaw.min.js', css_path='rickshaw.min.css',
                     html_prefix=''):
        '''Save bearcart output to HTML and JSON.

        Parameters
        ----------
        html_path: string, default 'index.html'
            Path for html output
        data_path: string, default 'data.json'
            Path for data JSON output
        js_path: string, default 'rickshaw.min.js'
            If passed, the Rickshaw javascript library will be saved to the
            path. The file must be named "rickshaw.min.js"
        css_path: string, default 'rickshaw.min.css'
            If passed, the Rickshaw css library will be saved to the
            path. The file must be named "rickshaw.min.css"
        html_prefix: Prefix path to be appended to all the other paths for file
            creation, but not in the generated html file. This is needed if the
            html file does not live in the same folder as the running python
            script.

        Returns
        -------
        HTML, JSON, JS, and CSS

        Example
        --------
        >>>vis.create_chart(html_path='myvis.html', data_path='visdata.json'),
                            js_path='rickshaw.min.js',
                            cs_path='rickshaw.min.css')
        '''

        self.template_vars.update({'data_path': str(data_path),
                                   'js_path': js_path,
                                   'css_path': css_path,
                                   'chart_id': self.chart_id,
                                   'y_axis_id': self.y_axis_id,
                                   'legend_id': self.legend_id,
                                   'slider_id': self.slider_id})

        self._build_graph()
        html = self.env.get_template('bcart_template.html')
        self.HTML = html.render(self.template_vars)

        with open(os.path.join(html_prefix, html_path), 'w') as f:
            f.write(self.HTML)

        with open(os.path.join(html_prefix, data_path), 'w') as f:
            json.dump(self.json_data, f, sort_keys=True, indent=4,
                      separators=(',', ': '))

        if js_path:
            js = resource_string('bearcart', 'rickshaw.min.js')
            with open(os.path.join(html_prefix, js_path), 'w') as f:
                f.write(js)
        if css_path:
            css = resource_string('bearcart', 'rickshaw.min.css')
            with open(os.path.join(html_prefix, css_path), 'w') as f:
                    f.write(css)
    def student_view(self, context):
        """
        Generate the html code to display the XBlock to a student
        `context` is a dictionary used to configure the display (unused).

        Returns a `Fragment` object specifying the HTML, CSS, and JavaScript
        to display.
        """
        provider, embed_code = self.get_embed_code_for_url(self.href)
        
        # Retrieve HTML code for video iframe 
        html_code = pkg_resources.resource_string(__name__, "static/html/cnvideo.html")
        frag = Fragment(unicode(html_code).format(self=self, embed_code=embed_code))
        
        # Load CSS
        css_str = pkg_resources.resource_string(__name__, "static/css/cnvideo.css")
        frag.add_css(css_str)
        
        # Load vimeo JS API and custom js for watching views
        if provider == "vimeo.com":
            frag.add_javascript_url("//f.vimeocdn.com/js/froogaloop2.min.js")
            js_str = pkg_resources.resource_string(__name__, "static/js/cnvideo.js")
            frag.add_javascript(unicode(js_str))
            frag.initialize_js("cnVideoBlock")
        
        return frag
Example #24
0
def run_jenkins_now(
    jenkins_instance, ubuntu_distro, arch, job_name, email, script, script_args, user_name, parameters=None, matrix=None
):

    job_xml = pkg_resources.resource_string("jenkins_tools", "resources/templates/jenkins_template.xml")
    jenkins_conf = pkg_resources.resource_string("jenkins_tools", "resources/templates/jenkins_conf.yaml")
    jc = yaml.load(jenkins_conf)

    # get arguments
    params = {}
    params["UBUNTU_DISTRO"] = ubuntu_distro
    params["ARCH"] = arch
    params["EMAIL"] = email
    params["EMAIL_COMMITTER"] = "false"
    params["SCRIPT"] = script
    params["NODE"] = params["SCRIPT"]
    params["SCRIPT_ARGS"] = " ".join(script_args)
    params["TRIGGER"] = jc["triggers"]["none"]
    params["VCS"] = jc["vcs"]["none"]
    params["TIME"] = str(datetime.datetime.now())
    params["USERNAME"] = user_name
    params["HOSTNAME"] = os.uname()[1]
    params["PARAMETERS"] = ""
    params["MATRIX"] = ""
    params["PROJECT"] = "project"
    if parameters and len(parameters) > 0:
        params["PARAMETERS"] = jc["parameters"]["block"].replace(
            "@(PARAMS)", " ".join([jc["parameters"]["param"].replace("@(PARAM)", p) for p in parameters[0].keys()])
        )
    if matrix:
        axis = ""
        for axis_name, axis_values in matrix.iteritems():
            axis += (
                jc["matrix"]["axis"]
                .replace("@(NAME)", axis_name)
                .replace("@(VALUES)", " ".join([jc["matrix"]["value"].replace("@(VALUE)", v) for v in axis_values]))
            )
        params["MATRIX"] = jc["matrix"]["block"].replace("@(AXIS)", axis)
        params["MATRIX"] = params["MATRIX"].replace("@(NODE)", params["NODE"])
        params["PROJECT"] = "matrix-project"

    # replace @(xxx) in template file
    for key, value in params.iteritems():
        job_xml = job_xml.replace("@(%s)" % key, value)

    # schedule a new job
    if jenkins_instance.job_exists(job_name):
        jenkins_instance.reconfig_job(job_name, job_xml)
        print "Reconfigured job %s" % job_name
    else:
        jenkins_instance.create_job(job_name, job_xml)
        print "Created job %s" % job_name

    # build all jobs
    if not parameters or len(parameters) == 0:
        jenkins_instance.build_job(job_name)
    else:
        for p in parameters:
            jenkins_instance.build_job(job_name, p)
    print "Started job %s" % job_name
Example #25
0
def response_template_to_json(template_name):
    rsc_path = "/".join(("sample_responses", template_name))
    try: # Python 3.3 will return the contents as a bytes-like object.
        file_contents = pkg_resources.resource_string(rsc_pkg, rsc_path).decode("utf-8")
    except AttributeError:
        file_contents = pkg_resources.resource_string(rsc_pkg, rsc_path)
    return json.loads(file_contents)
Example #26
0
    def on_app_start(self, data = None):
        
        # create widget tree ...
        self.builder = Gtk.Builder()
        self.ui_manager = Gtk.UIManager()
        try:
            self.builder.add_from_file(resource_string(__name__, 'stevedore.glade'))
            self.ui_manager.add_ui_from_file(resource_string(__name__, 'ui_builder.xml'))

        except:
            self.builder.add_from_file('../res/stevedore.glade')
            self.ui_manager.add_ui_from_file('../res/ui_builder.xml')

        # connect handlers
        self.builder.connect_signals(self)
        self.ui_manager.insert_action_group(self.builder.get_object('ContainerActionGroup'))
        self.ui_manager.insert_action_group(self.builder.get_object('ImagesActionGroup'))

        # Prepare certain widgets
        self.status_bar = self.builder.get_object('AppStatus')
        self.status_context_id = self.status_bar.get_context_id('main_app')
        self.builder.get_object('StartButton').set_sensitive(False)
        self.builder.get_object('StopButton').set_sensitive(False)
        self.builder.get_object('AttachButton').set_sensitive(False)
        self.builder.get_object('LogButton').set_sensitive(False)
        self.builder.get_object('DeleteContainerButton').set_sensitive(False)

        # Add window to the App and show it
        self.window = self.builder.get_object('MainWindow')
        self.add_window(self.window)
        self.window.show()
        self.set_app_status(u'Not connected to Docker server.')
Example #27
0
    def setUp(self):
        """Setup tests."""
        from inspire.modules.workflows.receivers import precache_holdingpen_row
        from invenio_workflows.receivers import index_holdingpen_record
        from invenio_workflows.signals import workflow_halted, workflow_object_saved

        # Disable the holdingpen caching receiver.
        workflow_halted.disconnect(precache_holdingpen_row)
        workflow_object_saved.disconnect(index_holdingpen_record)

        self.create_registries()
        self.record_oai_arxiv_plots = pkg_resources.resource_string(
            "inspire.testsuite", os.path.join("workflows", "fixtures", "oai_arxiv_record_with_plots.xml")
        )
        self.record_oai_arxiv_accept = pkg_resources.resource_string(
            "inspire.testsuite", os.path.join("workflows", "fixtures", "oai_arxiv_record_to_accept.xml")
        )
        self.some_record = pkg_resources.resource_string(
            "inspire.testsuite", os.path.join("workflows", "fixtures", "some_record.xml")
        )
        self.arxiv_tarball = pkg_resources.resource_stream(
            "inspire.testsuite", os.path.join("workflows", "fixtures", "1407.7587v1")
        )
        self.arxiv_pdf = pkg_resources.resource_stream(
            "inspire.testsuite", os.path.join("workflows", "fixtures", "1407.7587v1.pdf")
        )
        self.arxiv_tarball_accept = pkg_resources.resource_stream(
            "inspire.testsuite", os.path.join("workflows", "fixtures", "1511.01097")
        )
        self.arxiv_pdf_accept = pkg_resources.resource_stream(
            "inspire.testsuite", os.path.join("workflows", "fixtures", "1511.01097v1.pdf")
        )
Example #28
0
def run_jenkins_vcs(jenkins_instance, 
                    ubuntu_distro, arch, name, email, vcs, uri, branch,
                    script, script_args, user_name):

    job_xml = pkg_resources.resource_string('jenkins_tools', 'resources/templates/jenkins_template.xml')
    jenkins_conf = pkg_resources.resource_string('jenkins_tools', 'resources/templates/jenkins_conf.yaml')
    jc = yaml.load(jenkins_conf)

    # get arguments
    params = {}
    params['UBUNTU_DISTRO'] = ubuntu_distro
    params['ARCH'] = arch
    params['EMAIL'] = email
    params['TRIGGER'] = jc['triggers']['vcs']
    params['VCS'] = jc['vcs'][vcs].replace('@(URI)', uri).replace('@(BRANCH)', branch)
    params['SCRIPT'] = script
    params['NODE'] = params['SCRIPT']
    params['TIME'] = str(datetime.datetime.now())
    params['SCRIPT_ARGS'] = ' '.join(script_args)
    params['USERNAME'] = user_name
    params['HOSTNAME'] = os.uname()[1]

    # replace @(xxx) in template file
    for key, value in params.iteritems():
        job_xml = job_xml.replace("@(%s)"%key, value)

    # schedule a new job
    job_name = "%s-%s-%s-%s"%(params['SCRIPT'], name, params['UBUNTU_DISTRO'], params['ARCH'])
    if jenkins_instance.job_exists(job_name):
        jenkins_instance.reconfig_job(job_name, job_xml)
        print "Reconfigured job %s"%job_name
    else:
        jenkins_instance.create_job(job_name, job_xml)
        print "Created job %s"%job_name
    return job_name
Example #29
0
    def init(self, path):
        settings = {}
        t = raw_input('Debug Mode [n]:')
        settings['debug'] = 'True' if t.lower() == 'y' else 'False'
        t = raw_input('Port [5000]:')
        settings['port'] = t if t.strip() else '5000'
        t = raw_input('MySQL Host [localhost]:')
        settings['db_host'] = t if t.strip() else 'localhost'
        t = raw_input('MySQL Port [3306]:')
        settings['db_port'] = t if t.strip() else '3306'
        t = raw_input('MySQL User [ngflaskcurd]:')
        settings['db_user'] = t if t.strip() else 'ngflaskcurd'
        t = raw_input('MySQL Password:'******'db_pwd'] = t
        t = raw_input('MySQL Database Name [ngflaskcurd]:')
        settings['db_db'] = t if t.strip() else 'ngflaskcurd'
        
        a2wsgi = {}
        a2wsgi['dir'] = os.path.abspath(path)

        with open(os.path.join(path, 'setting.py'), 'w') as fd:
            fd.write(pkg_resources.resource_string(
                'ngflaskcurd',
                'config_templates/settings.py') % settings)
            
        with open(os.path.join(path, 'run.py'), 'w') as fd:
            fd.write(Template(pkg_resources.resource_string(
                'ngflaskcurd',
                'wsgi.py')).substitute(a2wsgi))
            
        if not os.path.exists(os.path.join(path, 'log')):
            os.mkdir(os.path.join(path, 'log'))
    def setUp(self):
        self.marcxml = pkg_resources.resource_string('tests',
                                                     os.path.join(
                                                         'fixtures',
                                                         'test_hep_formats.xml')
                                                     )

        self.marcxml_publi_info = pkg_resources.resource_string('tests',
                                                                os.path.join(
                                                                    'fixtures',
                                                                    'test_hep_publi_info.xml')
                                                                )
        record = create_record(self.marcxml)

        record_publi_info = create_record(self.marcxml_publi_info)

        self.hep_record = hep.do(record)

        self.hep_record_publi_info = hep.do(record_publi_info)

        self.sample_cv_latex = {
            'author': 'G.~Aad',
            'title': "{\\bf ``\nSearch for supersymmetry in events containing a same-flavour opposite-sign dilepton pair, jets, and large missing transverse momentum in $\sqrt{s}=8$ TeV $pp$ collisions with the ATLAS detector\n''}",
            'publi_info': ['Eur.\ Phys.\ J.\ C {\\bf 75}, no. 7, 318 (2015)', '[Eur.\ Phys.\ J.\ C {\\bf 75}, no. 10, 463 (2015)]'],
            'url': cfg['CFG_SITE_URL'] + '/record/1351762',
            'date': 'Mar 11, 2015'
        }

        self.sample_cv_latex_publi_info = {
            'publi_info': ['Class.\\ Quant.\\ Grav.\\  {\\bf 15}, 2153 (1998)']
        }
Example #31
0
class CapaDescriptor(CapaFields, RawDescriptor):
    """
    Module implementing problems in the LON-CAPA format,
    as implemented by capa.capa_problem
    """

    module_class = CapaModule

    has_score = True
    template_dir_name = 'problem'
    mako_template = "widgets/problem-edit.html"
    js = {'coffee': [resource_string(__name__, 'js/src/problem/edit.coffee')]}
    js_module_name = "MarkdownEditingDescriptor"
    css = {
        'scss': [
            resource_string(__name__, 'css/editor/edit.scss'),
            resource_string(__name__, 'css/problem/edit.scss')
        ]
    }

    # The capa format specifies that what we call max_attempts in the code
    # is the attribute `attempts`. This will do that conversion
    metadata_translations = dict(RawDescriptor.metadata_translations)
    metadata_translations['attempts'] = 'max_attempts'

    @classmethod
    def filter_templates(cls, template, course):
        """
        Filter template that contains 'latex' from templates.

        Show them only if use_latex_compiler is set to True in
        course settings.
        """
        return (not 'latex' in template['template_id']
                or course.use_latex_compiler)

    def get_context(self):
        _context = RawDescriptor.get_context(self)
        _context.update({
            'markdown': self.markdown,
            'enable_markdown': self.markdown is not None,
            'enable_latex_compiler': self.use_latex_compiler,
        })
        return _context

    # VS[compat]
    # TODO (cpennington): Delete this method once all fall 2012 course are being
    # edited in the cms
    @classmethod
    def backcompat_paths(cls, path):
        return [
            'problems/' + path[8:],
            path[8:],
        ]

    @property
    def non_editable_metadata_fields(self):
        non_editable_fields = super(CapaDescriptor,
                                    self).non_editable_metadata_fields
        non_editable_fields.extend([
            CapaDescriptor.due,
            CapaDescriptor.graceperiod,
            CapaDescriptor.force_save_button,
            CapaDescriptor.markdown,
            CapaDescriptor.text_customization,
            CapaDescriptor.use_latex_compiler,
        ])
        return non_editable_fields

    # Proxy to CapaModule for access to any of its attributes
    answer_available = module_attr('answer_available')
    check_button_name = module_attr('check_button_name')
    check_button_checking_name = module_attr('check_button_checking_name')
    check_problem = module_attr('check_problem')
    choose_new_seed = module_attr('choose_new_seed')
    closed = module_attr('closed')
    get_answer = module_attr('get_answer')
    get_problem = module_attr('get_problem')
    get_problem_html = module_attr('get_problem_html')
    get_state_for_lcp = module_attr('get_state_for_lcp')
    handle_input_ajax = module_attr('handle_input_ajax')
    handle_problem_html_error = module_attr('handle_problem_html_error')
    handle_ungraded_response = module_attr('handle_ungraded_response')
    is_attempted = module_attr('is_attempted')
    is_correct = module_attr('is_correct')
    is_past_due = module_attr('is_past_due')
    is_submitted = module_attr('is_submitted')
    lcp = module_attr('lcp')
    make_dict_of_responses = module_attr('make_dict_of_responses')
    new_lcp = module_attr('new_lcp')
    publish_grade = module_attr('publish_grade')
    rescore_problem = module_attr('rescore_problem')
    reset_problem = module_attr('reset_problem')
    save_problem = module_attr('save_problem')
    set_state_from_lcp = module_attr('set_state_from_lcp')
    should_show_check_button = module_attr('should_show_check_button')
    should_show_reset_button = module_attr('should_show_reset_button')
    should_show_save_button = module_attr('should_show_save_button')
    update_score = module_attr('update_score')
Example #32
0
class CapaModule(CapaMixin, XModule):
    """
    An XModule implementing LonCapa format problems, implemented by way of
    capa.capa_problem.LoncapaProblem

    CapaModule.__init__ takes the same arguments as xmodule.x_module:XModule.__init__
    """
    icon_class = 'problem'

    js = {
        'js': [
            resource_string(__name__, 'js/src/javascript_loader.js'),
            resource_string(__name__, 'js/src/capa/display.js'),
            resource_string(__name__, 'js/src/collapsible.js'),
            resource_string(__name__, 'js/src/capa/imageinput.js'),
            resource_string(__name__, 'js/src/capa/schematic.js'),
        ]
    }

    js_module_name = "Problem"
    css = {'scss': [resource_string(__name__, 'css/capa/display.scss')]}

    def author_view(self, context):
        """
        Renders the Studio preview view.
        """
        return self.student_view(context)

    def handle_ajax(self, dispatch, data):
        """
        This is called by courseware.module_render, to handle an AJAX call.

        `data` is request.POST.

        Returns a json dictionary:
        { 'progress_changed' : True/False,
          'progress' : 'none'/'in_progress'/'done',
          <other request-specific values here > }
        """
        handlers = {
            'hint_button': self.hint_button,
            'problem_get': self.get_problem,
            'problem_check': self.submit_problem,
            'problem_reset': self.reset_problem,
            'problem_save': self.save_problem,
            'problem_show': self.get_answer,
            'score_update': self.update_score,
            'input_ajax': self.handle_input_ajax,
            'ungraded_response': self.handle_ungraded_response
        }

        _ = self.runtime.service(self, "i18n").ugettext

        generic_error_message = _(
            "We're sorry, there was an error with processing your request. "
            "Please try reloading your page and trying again.")

        not_found_error_message = _(
            "The state of this problem has changed since you loaded this page. "
            "Please refresh your page.")

        if dispatch not in handlers:
            return 'Error: {} is not a known capa action'.format(dispatch)

        before = self.get_progress()
        before_attempts = self.attempts

        try:
            result = handlers[dispatch](data)

        except NotFoundError:
            log.info(
                "Unable to find data when dispatching %s to %s for user %s",
                dispatch, self.scope_ids.usage_id, self.scope_ids.user_id)
            _, _, traceback_obj = sys.exc_info()  # pylint: disable=redefined-outer-name
            raise ProcessingError(not_found_error_message), None, traceback_obj

        except Exception:
            log.exception(
                "Unknown error when dispatching %s to %s for user %s",
                dispatch, self.scope_ids.usage_id, self.scope_ids.user_id)
            _, _, traceback_obj = sys.exc_info()  # pylint: disable=redefined-outer-name
            raise ProcessingError(generic_error_message), None, traceback_obj

        after = self.get_progress()
        after_attempts = self.attempts
        progress_changed = (after != before) or (after_attempts !=
                                                 before_attempts)
        curr_score, total_possible = self.get_display_progress()

        result.update({
            'progress_changed': progress_changed,
            'current_score': curr_score,
            'total_possible': total_possible,
            'attempts_used': after_attempts,
        })

        return json.dumps(result, cls=ComplexEncoder)

    @property
    def display_name_with_default(self):
        """
        Constructs the display name for a CAPA problem.

        Default to the display_name if it isn't None or not an empty string,
        else fall back to problem category.
        """
        if self.display_name is None or not self.display_name.strip():
            return self.location.block_type

        return self.display_name
Example #33
0
class CapaDescriptor(CapaFields, RawDescriptor):
    """
    Module implementing problems in the LON-CAPA format,
    as implemented by capa.capa_problem
    """
    INDEX_CONTENT_TYPE = 'CAPA'

    module_class = CapaModule
    resources_dir = None

    has_score = True
    show_in_read_only_mode = True
    template_dir_name = 'problem'
    mako_template = "widgets/problem-edit.html"
    js = {'js': [resource_string(__name__, 'js/src/problem/edit.js')]}
    js_module_name = "MarkdownEditingDescriptor"
    has_author_view = True
    css = {
        'scss': [
            resource_string(__name__, 'css/editor/edit.scss'),
            resource_string(__name__, 'css/problem/edit.scss')
        ]
    }

    # The capa format specifies that what we call max_attempts in the code
    # is the attribute `attempts`. This will do that conversion
    metadata_translations = dict(RawDescriptor.metadata_translations)
    metadata_translations['attempts'] = 'max_attempts'

    @classmethod
    def filter_templates(cls, template, course):
        """
        Filter template that contains 'latex' from templates.

        Show them only if use_latex_compiler is set to True in
        course settings.
        """
        return 'latex' not in template[
            'template_id'] or course.use_latex_compiler

    def get_context(self):
        _context = RawDescriptor.get_context(self)
        _context.update({
            'markdown': self.markdown,
            'enable_markdown': self.markdown is not None,
            'enable_latex_compiler': self.use_latex_compiler,
        })
        return _context

    # VS[compat]
    # TODO (cpennington): Delete this method once all fall 2012 course are being
    # edited in the cms
    @classmethod
    def backcompat_paths(cls, path):
        dog_stats_api.increment(
            DEPRECATION_VSCOMPAT_EVENT,
            tags=["location:capa_descriptor_backcompat_paths"])
        return [
            'problems/' + path[8:],
            path[8:],
        ]

    @property
    def non_editable_metadata_fields(self):
        non_editable_fields = super(CapaDescriptor,
                                    self).non_editable_metadata_fields
        non_editable_fields.extend([
            CapaDescriptor.due,
            CapaDescriptor.graceperiod,
            CapaDescriptor.force_save_button,
            CapaDescriptor.markdown,
            CapaDescriptor.use_latex_compiler,
            CapaDescriptor.show_correctness,
        ])
        return non_editable_fields

    @property
    def problem_types(self):
        """ Low-level problem type introspection for content libraries filtering by problem type """
        try:
            tree = etree.XML(self.data)
        except etree.XMLSyntaxError:
            log.error(
                'Error parsing problem types from xml for capa module {}'.
                format(self.display_name))
            return None  # short-term fix to prevent errors (TNL-5057). Will be more properly addressed in TNL-4525.
        registered_tags = responsetypes.registry.registered_tags()
        return {
            node.tag
            for node in tree.iter() if node.tag in registered_tags
        }

    def index_dictionary(self):
        """
        Return dictionary prepared with module content and type for indexing.
        """
        xblock_body = super(CapaDescriptor, self).index_dictionary()
        # Removing solutions and hints, as well as script and style
        capa_content = re.sub(
            re.compile(
                r"""
                    <solution>.*?</solution> |
                    <script>.*?</script> |
                    <style>.*?</style> |
                    <[a-z]*hint.*?>.*?</[a-z]*hint>
                """, re.DOTALL | re.VERBOSE), "", self.data)
        capa_content = escape_html_characters(capa_content)
        capa_body = {
            "capa_content": capa_content,
            "display_name": self.display_name,
        }
        if "content" in xblock_body:
            xblock_body["content"].update(capa_body)
        else:
            xblock_body["content"] = capa_body
        xblock_body["content_type"] = self.INDEX_CONTENT_TYPE
        xblock_body["problem_types"] = list(self.problem_types)
        return xblock_body

    def has_support(self, view, functionality):
        """
        Override the XBlock.has_support method to return appropriate
        value for the multi-device functionality.
        Returns whether the given view has support for the given functionality.
        """
        if functionality == "multi_device":
            types = self.problem_types  # Avoid calculating this property twice
            return types is not None and all(
                responsetypes.registry.get_class_for_tag(
                    tag).multi_device_support for tag in types)
        return False

    def max_score(self):
        """
        Return the problem's max score
        """
        from capa.capa_problem import LoncapaProblem, LoncapaSystem
        capa_system = LoncapaSystem(
            ajax_url=None,
            anonymous_student_id=None,
            cache=None,
            can_execute_unsafe_code=None,
            get_python_lib_zip=None,
            DEBUG=None,
            filestore=self.runtime.resources_fs,
            i18n=self.runtime.service(self, "i18n"),
            node_path=None,
            render_template=None,
            seed=None,
            STATIC_URL=None,
            xqueue=None,
            matlab_api_key=None,
        )
        lcp = LoncapaProblem(
            problem_text=self.data,
            id=self.location.html_id(),
            capa_system=capa_system,
            capa_module=self,
            state={},
            seed=1,
            minimal_init=True,
        )
        return lcp.get_max_score()

    def generate_report_data(self, user_state_iterator, limit_responses=None):
        """
        Return a list of student responses to this block in a readable way.

        Arguments:
            user_state_iterator: iterator over UserStateClient objects.
                E.g. the result of user_state_client.iter_all_for_block(block_key)

            limit_responses (int|None): maximum number of responses to include.
                Set to None (default) to include all.

        Returns:
            each call returns a tuple like:
            ("username", {
                           "Question": "2 + 2 equals how many?",
                           "Answer": "Four",
                           "Answer ID": "98e6a8e915904d5389821a94e48babcf_10_1"
            })
        """

        from capa.capa_problem import LoncapaProblem, LoncapaSystem

        if self.category != 'problem':
            raise NotImplementedError()

        if limit_responses == 0:
            # Don't even start collecting answers
            return

        capa_system = LoncapaSystem(
            ajax_url=None,
            # TODO set anonymous_student_id to the anonymous ID of the user which answered each problem
            # Anonymous ID is required for Matlab, CodeResponse, and some custom problems that include
            # '$anonymous_student_id' in their XML.
            # For the purposes of this report, we don't need to support those use cases.
            anonymous_student_id=None,
            cache=None,
            can_execute_unsafe_code=lambda: None,
            get_python_lib_zip=lambda: None,
            DEBUG=None,
            filestore=self.runtime.resources_fs,
            i18n=self.runtime.service(self, "i18n"),
            node_path=None,
            render_template=None,
            seed=1,
            STATIC_URL=None,
            xqueue=None,
            matlab_api_key=None,
        )
        _ = capa_system.i18n.ugettext

        count = 0
        for user_state in user_state_iterator:

            if 'student_answers' not in user_state.state:
                continue

            lcp = LoncapaProblem(
                problem_text=self.data,
                id=self.location.html_id(),
                capa_system=capa_system,
                # We choose to run without a fully initialized CapaModule
                capa_module=None,
                state={
                    'done': user_state.state.get('done'),
                    'correct_map': user_state.state.get('correct_map'),
                    'student_answers': user_state.state.get('student_answers'),
                    'has_saved_answers':
                    user_state.state.get('has_saved_answers'),
                    'input_state': user_state.state.get('input_state'),
                    'seed': user_state.state.get('seed'),
                },
                seed=user_state.state.get('seed'),
                # extract_tree=False allows us to work without a fully initialized CapaModule
                # We'll still be able to find particular data in the XML when we need it
                extract_tree=False,
            )

            for answer_id, orig_answers in lcp.student_answers.items():
                # Some types of problems have data in lcp.student_answers that isn't in lcp.problem_data.
                # E.g. formulae do this to store the MathML version of the answer.
                # We exclude these rows from the report because we only need the text-only answer.
                if answer_id.endswith('_dynamath'):
                    continue

                if limit_responses and count >= limit_responses:
                    # End the iterator here
                    return

                question_text = lcp.find_question_label(answer_id)
                answer_text = lcp.find_answer_text(answer_id,
                                                   current_answer=orig_answers)

                count += 1
                yield (user_state.username, {
                    _("Answer ID"): answer_id,
                    _("Question"): question_text,
                    _("Answer"): answer_text,
                })

    # Proxy to CapaModule for access to any of its attributes
    answer_available = module_attr('answer_available')
    submit_button_name = module_attr('submit_button_name')
    submit_button_submitting_name = module_attr(
        'submit_button_submitting_name')
    submit_problem = module_attr('submit_problem')
    choose_new_seed = module_attr('choose_new_seed')
    closed = module_attr('closed')
    get_answer = module_attr('get_answer')
    get_problem = module_attr('get_problem')
    get_problem_html = module_attr('get_problem_html')
    get_state_for_lcp = module_attr('get_state_for_lcp')
    handle_input_ajax = module_attr('handle_input_ajax')
    hint_button = module_attr('hint_button')
    handle_problem_html_error = module_attr('handle_problem_html_error')
    handle_ungraded_response = module_attr('handle_ungraded_response')
    has_submitted_answer = module_attr('has_submitted_answer')
    is_attempted = module_attr('is_attempted')
    is_correct = module_attr('is_correct')
    is_past_due = module_attr('is_past_due')
    is_submitted = module_attr('is_submitted')
    lcp = module_attr('lcp')
    make_dict_of_responses = module_attr('make_dict_of_responses')
    new_lcp = module_attr('new_lcp')
    publish_grade = module_attr('publish_grade')
    rescore = module_attr('rescore')
    reset_problem = module_attr('reset_problem')
    save_problem = module_attr('save_problem')
    set_score = module_attr('set_score')
    set_state_from_lcp = module_attr('set_state_from_lcp')
    should_show_submit_button = module_attr('should_show_submit_button')
    should_show_reset_button = module_attr('should_show_reset_button')
    should_show_save_button = module_attr('should_show_save_button')
    update_score = module_attr('update_score')
Example #34
0
    pass

from . import checklistdb
#from . import canonicalize_path
from . import xmldoc
from . import dc_value


class dummy(object):
    pass


thisdir = os.path.split(sys.modules[dummy.__module__].__file__)[0]

try:
    __install_prefix__ = resource_string(__name__,
                                         'install_prefix.txt').decode('utf-8')
    pass
except (IOError, NameError):
    sys.stderr.write(
        "dc2_misc: error reading install_prefix.txt. Assuming /usr/local.\n")
    __install_prefix__ = "/usr/local"
    pass


def getconfigurlpath():
    return [
        pathname2url(
            os.path.join(__install_prefix__, "share", "limatix", "conf"))
    ]

Example #35
0
class CapaDescriptor(CapaFields, RawDescriptor):
    """
    Module implementing problems in the LON-CAPA format,
    as implemented by capa.capa_problem
    """
    INDEX_CONTENT_TYPE = 'CAPA'

    module_class = CapaModule
    resources_dir = None

    has_score = True
    show_in_read_only_mode = True
    template_dir_name = 'problem'
    mako_template = "widgets/problem-edit.html"
    js = {'js': [resource_string(__name__, 'js/src/problem/edit.js')]}
    js_module_name = "MarkdownEditingDescriptor"
    has_author_view = True
    css = {
        'scss': [
            resource_string(__name__, 'css/editor/edit.scss'),
            resource_string(__name__, 'css/problem/edit.scss')
        ]
    }

    # The capa format specifies that what we call max_attempts in the code
    # is the attribute `attempts`. This will do that conversion
    metadata_translations = dict(RawDescriptor.metadata_translations)
    metadata_translations['attempts'] = 'max_attempts'

    @classmethod
    def filter_templates(cls, template, course):
        """
        Filter template that contains 'latex' from templates.

        Show them only if use_latex_compiler is set to True in
        course settings.
        """
        return 'latex' not in template[
            'template_id'] or course.use_latex_compiler

    def get_context(self):
        _context = RawDescriptor.get_context(self)
        _context.update({
            'markdown': self.markdown,
            'enable_markdown': self.markdown is not None,
            'enable_latex_compiler': self.use_latex_compiler,
        })
        return _context

    # VS[compat]
    # TODO (cpennington): Delete this method once all fall 2012 course are being
    # edited in the cms
    @classmethod
    def backcompat_paths(cls, path):
        dog_stats_api.increment(
            DEPRECATION_VSCOMPAT_EVENT,
            tags=["location:capa_descriptor_backcompat_paths"])
        return [
            'problems/' + path[8:],
            path[8:],
        ]

    @property
    def non_editable_metadata_fields(self):
        non_editable_fields = super(CapaDescriptor,
                                    self).non_editable_metadata_fields
        non_editable_fields.extend([
            CapaDescriptor.due,
            CapaDescriptor.graceperiod,
            CapaDescriptor.force_save_button,
            CapaDescriptor.markdown,
            CapaDescriptor.use_latex_compiler,
            CapaDescriptor.show_correctness,
        ])
        return non_editable_fields

    @property
    def problem_types(self):
        """ Low-level problem type introspection for content libraries filtering by problem type """
        try:
            tree = etree.XML(self.data)
        except etree.XMLSyntaxError:
            log.error(
                'Error parsing problem types from xml for capa module {}'.
                format(self.display_name))
            return None  # short-term fix to prevent errors (TNL-5057). Will be more properly addressed in TNL-4525.
        registered_tags = responsetypes.registry.registered_tags()
        return {
            node.tag
            for node in tree.iter() if node.tag in registered_tags
        }

    def index_dictionary(self):
        """
        Return dictionary prepared with module content and type for indexing.
        """
        xblock_body = super(CapaDescriptor, self).index_dictionary()
        # Removing solutions and hints, as well as script and style
        capa_content = re.sub(
            re.compile(
                r"""
                    <solution>.*?</solution> |
                    <script>.*?</script> |
                    <style>.*?</style> |
                    <[a-z]*hint.*?>.*?</[a-z]*hint>
                """, re.DOTALL | re.VERBOSE), "", self.data)
        capa_content = escape_html_characters(capa_content)
        capa_body = {
            "capa_content": capa_content,
            "display_name": self.display_name,
        }
        if "content" in xblock_body:
            xblock_body["content"].update(capa_body)
        else:
            xblock_body["content"] = capa_body
        xblock_body["content_type"] = self.INDEX_CONTENT_TYPE
        xblock_body["problem_types"] = list(self.problem_types)
        return xblock_body

    def has_support(self, view, functionality):
        """
        Override the XBlock.has_support method to return appropriate
        value for the multi-device functionality.
        Returns whether the given view has support for the given functionality.
        """
        if functionality == "multi_device":
            types = self.problem_types  # Avoid calculating this property twice
            return types is not None and all(
                responsetypes.registry.get_class_for_tag(
                    tag).multi_device_support for tag in types)
        return False

    def max_score(self):
        """
        Return the problem's max score
        """
        from capa.capa_problem import LoncapaProblem, LoncapaSystem
        capa_system = LoncapaSystem(
            ajax_url=None,
            anonymous_student_id=None,
            cache=None,
            can_execute_unsafe_code=None,
            get_python_lib_zip=None,
            DEBUG=None,
            filestore=self.runtime.resources_fs,
            i18n=self.runtime.service(self, "i18n"),
            node_path=None,
            render_template=None,
            seed=None,
            STATIC_URL=None,
            xqueue=None,
            matlab_api_key=None,
        )
        lcp = LoncapaProblem(
            problem_text=self.data,
            id=self.location.html_id(),
            capa_system=capa_system,
            capa_module=self,
            state={},
            seed=1,
            minimal_init=True,
        )
        return lcp.get_max_score()

    # Proxy to CapaModule for access to any of its attributes
    answer_available = module_attr('answer_available')
    submit_button_name = module_attr('submit_button_name')
    submit_button_submitting_name = module_attr(
        'submit_button_submitting_name')
    submit_problem = module_attr('submit_problem')
    choose_new_seed = module_attr('choose_new_seed')
    closed = module_attr('closed')
    get_answer = module_attr('get_answer')
    get_problem = module_attr('get_problem')
    get_problem_html = module_attr('get_problem_html')
    get_state_for_lcp = module_attr('get_state_for_lcp')
    handle_input_ajax = module_attr('handle_input_ajax')
    hint_button = module_attr('hint_button')
    handle_problem_html_error = module_attr('handle_problem_html_error')
    handle_ungraded_response = module_attr('handle_ungraded_response')
    has_submitted_answer = module_attr('has_submitted_answer')
    is_attempted = module_attr('is_attempted')
    is_correct = module_attr('is_correct')
    is_past_due = module_attr('is_past_due')
    is_submitted = module_attr('is_submitted')
    lcp = module_attr('lcp')
    make_dict_of_responses = module_attr('make_dict_of_responses')
    new_lcp = module_attr('new_lcp')
    publish_grade = module_attr('publish_grade')
    rescore = module_attr('rescore')
    reset_problem = module_attr('reset_problem')
    save_problem = module_attr('save_problem')
    set_state_from_lcp = module_attr('set_state_from_lcp')
    should_show_submit_button = module_attr('should_show_submit_button')
    should_show_reset_button = module_attr('should_show_reset_button')
    should_show_save_button = module_attr('should_show_save_button')
    update_score = module_attr('update_score')
    def _generate_report(self, session):
        suite_stop_time = time.time()
        suite_time_delta = suite_stop_time - self.suite_start_time
        numtests = self.passed + self.failed + self.xpassed + self.xfailed
        generated = datetime.datetime.now()

        self.style_css = pkg_resources.resource_string(
            __name__, os.path.join('resources', 'style.css'))
        if PY3:
            self.style_css = self.style_css.decode('utf-8')

        if ANSI:
            ansi_css = [
                '\n/******************************',
                ' * ANSI2HTML STYLES',
                ' ******************************/\n']
            ansi_css.extend([str(r) for r in style.get_styles()])
            self.style_css += '\n'.join(ansi_css)

        # <DF> Add user-provided CSS
        for path in self.config.getoption('css'):
            self.style_css += '\n/******************************'
            self.style_css += '\n * CUSTOM CSS'
            self.style_css += '\n * {}'.format(path)
            self.style_css += '\n ******************************/\n\n'
            with open(path, 'r') as f:
                self.style_css += f.read()

        css_href = '{0}/{1}'.format('assets', 'style.css')
        html_css = html.link(href=css_href, rel='stylesheet',
                             type='text/css')
        if self.self_contained:
            html_css = html.style(raw(self.style_css))

        head = html.head(
            html.meta(charset='utf-8'),
            html.title('Test Report'),
            html_css)

        class Outcome:

            def __init__(self, outcome, total=0, label=None,
                         test_result=None, class_html=None):
                self.outcome = outcome
                self.label = label or outcome
                self.class_html = class_html or outcome
                self.total = total
                self.test_result = test_result or outcome

                self.generate_checkbox()
                self.generate_summary_item()

            def generate_checkbox(self):
                checkbox_kwargs = {'data-test-result':
                                   self.test_result.lower()}
                if self.total == 0:
                    checkbox_kwargs['disabled'] = 'true'

                self.checkbox = html.input(type='checkbox',
                                           checked='true',
                                           onChange='filter_table(this)',
                                           name='filter_checkbox',
                                           class_='filter',
                                           hidden='true',
                                           **checkbox_kwargs)

            def generate_summary_item(self):
                self.summary_item = html.span('{0} {1}'.
                                              format(self.total, self.label),
                                              class_=self.class_html)

        outcomes = [Outcome('passed', self.passed),
                    Outcome('skipped', self.skipped),
                    Outcome('failed', self.failed),
                    Outcome('error', self.errors, label='errors'),
                    Outcome('xfailed', self.xfailed,
                            label='expected failures'),
                    Outcome('xpassed', self.xpassed,
                            label='unexpected passes')]

        if self.rerun is not None:
            outcomes.append(Outcome('rerun', self.rerun))

        summary = [html.p(
            '{0} tests ran in {1:.2f} seconds. '.format(
                numtests, suite_time_delta)),
            html.p('(Un)check the boxes to filter the results.',
                   class_='filter',
                   hidden='true')]

        for i, outcome in enumerate(outcomes, start=1):
            summary.append(outcome.checkbox)
            summary.append(outcome.summary_item)
            if i < len(outcomes):
                summary.append(', ')

        cells = [
            html.th('Result',
                    class_='sortable result initial-sort',
                    col='result'),
            html.th('Test', class_='sortable', col='name'),
            html.th('Duration', class_='sortable numeric', col='duration'),
            html.th('Links')]
        session.config.hook.pytest_html_results_table_header(cells=cells)

        results = [html.h2('Results'), html.table([html.thead(
            html.tr(cells),
            html.tr([
                html.th('No results found. Try to check the filters',
                        colspan=len(cells))],
                    id='not-found-message', hidden='true'),
            id='results-table-head'),
            self.test_logs], id='results-table')]

        main_js = pkg_resources.resource_string(
            __name__, os.path.join('resources', 'main.js'))
        if PY3:
            main_js = main_js.decode('utf-8')

        body = html.body(
            html.script(raw(main_js)),
            html.h1(os.path.basename(self.logfile)),
            html.p('Report generated on {0} at {1} by '.format(
                generated.strftime('%d-%b-%Y'),
                generated.strftime('%H:%M:%S')),
                html.a('pytest-html', href=__pypi_url__),
                ' v{0}'.format(__version__)),
            onLoad='init()')

        body.extend(self._generate_environment(session.config))

        summary_prefix, summary_postfix = [], []
        session.config.hook.pytest_html_results_summary(
            prefix=summary_prefix, summary=summary, postfix=summary_postfix)
        body.extend([html.h2('Summary')] + summary_prefix
                    + summary + summary_postfix)

        body.extend(results)

        doc = html.html(head, body)

        unicode_doc = u'<!DOCTYPE html>\n{0}'.format(doc.unicode(indent=2))
        if PY3:
            # Fix encoding issues, e.g. with surrogates
            unicode_doc = unicode_doc.encode('utf-8',
                                             errors='xmlcharrefreplace')
            unicode_doc = unicode_doc.decode('utf-8')
        return unicode_doc
Example #37
0
class CapaModule(CapaMixin, XModule):
    """
    An XModule implementing LonCapa format problems, implemented by way of
    capa.capa_problem.LoncapaProblem

    CapaModule.__init__ takes the same arguments as xmodule.x_module:XModule.__init__
    """
    icon_class = 'problem'

    js = {
        'coffee': [
            resource_string(__name__, 'js/src/capa/display.coffee'),
            resource_string(__name__, 'js/src/javascript_loader.coffee'),
        ],
        'js': [
            resource_string(__name__, 'js/src/collapsible.js'),
            resource_string(__name__, 'js/src/capa/imageinput.js'),
            resource_string(__name__, 'js/src/capa/schematic.js'),
        ]
    }

    js_module_name = "Problem"
    css = {'scss': [resource_string(__name__, 'css/capa/display.scss')]}

    def __init__(self, *args, **kwargs):
        """
        Accepts the same arguments as xmodule.x_module:XModule.__init__
        """
        super(CapaModule, self).__init__(*args, **kwargs)

    def handle_ajax(self, dispatch, data):
        """
        This is called by courseware.module_render, to handle an AJAX call.

        `data` is request.POST.

        Returns a json dictionary:
        { 'progress_changed' : True/False,
          'progress' : 'none'/'in_progress'/'done',
          <other request-specific values here > }
        """
        handlers = {
            'problem_get': self.get_problem,
            'problem_check': self.check_problem,
            'problem_reset': self.reset_problem,
            'problem_save': self.save_problem,
            'problem_show': self.get_answer,
            'score_update': self.update_score,
            'input_ajax': self.handle_input_ajax,
            'ungraded_response': self.handle_ungraded_response
        }

        _ = self.runtime.service(self, "i18n").ugettext

        generic_error_message = _(
            "We're sorry, there was an error with processing your request. "
            "Please try reloading your page and trying again.")

        not_found_error_message = _(
            "The state of this problem has changed since you loaded this page. "
            "Please refresh your page.")

        if dispatch not in handlers:
            return 'Error: {} is not a known capa action'.format(dispatch)

        before = self.get_progress()

        try:
            result = handlers[dispatch](data)

        except NotFoundError as err:
            _, _, traceback_obj = sys.exc_info()  # pylint: disable=redefined-outer-name
            raise ProcessingError(not_found_error_message), None, traceback_obj

        except Exception as err:
            _, _, traceback_obj = sys.exc_info()  # pylint: disable=redefined-outer-name
            raise ProcessingError(generic_error_message), None, traceback_obj

        after = self.get_progress()

        result.update({
            'progress_changed': after != before,
            'progress_status': Progress.to_js_status_str(after),
            'progress_detail': Progress.to_js_detail_str(after),
        })

        return json.dumps(result, cls=ComplexEncoder)
Example #38
0
from pkg_resources import resource_string

import boto3
import yaml

from troposphere import s3
from troposphere import Output, Ref, Template

import cloudformation.troposphere.utils as utils

# load config
cfg = yaml.load(resource_string('config', 'dev_config.yml'))

STACK_NAME = cfg['s3']['stack_name']

template = Template()
description = 'S3 Developments Buckets'
template.add_description(description)
# AWSTemplateFormatVersion
template.add_version('2010-09-09')

s3_dev_bucket = template.add_resource(
    s3.Bucket('S3DevBucket', BucketName='nicor-dev', DeletionPolicy='Retain'))

s3_data_bucket = template.add_resource(
    s3.Bucket('S3DataBucket', BucketName='nicor-data',
              DeletionPolicy='Retain'))

# stack outputs
template.add_output([
    Output('S3DevBucket',
class HtmlDescriptor(HtmlBlock, XmlDescriptor, EditingDescriptor):  # pylint: disable=abstract-method
    """
    Module for putting raw html in a course
    """
    mako_template = "widgets/html-edit.html"
    module_class = HtmlModule
    resources_dir = None
    filename_extension = "xml"
    template_dir_name = "html"
    show_in_read_only_mode = True

    js = {'js': [resource_string(__name__, 'js/src/html/edit.js')]}
    js_module_name = "HTMLEditingDescriptor"
    css = {
        'scss': [
            resource_string(__name__, 'css/editor/edit.scss'),
            resource_string(__name__, 'css/html/edit.scss')
        ]
    }

    # VS[compat] TODO (cpennington): Delete this method once all fall 2012 course
    # are being edited in the cms
    @classmethod
    def backcompat_paths(cls, filepath):
        """
        Get paths for html and xml files.
        """
        if filepath.endswith('.html.xml'):
            filepath = filepath[:-9] + '.html'  # backcompat--look for html instead of xml
        if filepath.endswith('.html.html'):
            filepath = filepath[:
                                -5]  # some people like to include .html in filenames..
        candidates = []
        while os.sep in filepath:
            candidates.append(filepath)
            _, _, filepath = filepath.partition(os.sep)

        # also look for .html versions instead of .xml
        new_candidates = []
        for candidate in candidates:
            if candidate.endswith('.xml'):
                new_candidates.append(candidate[:-4] + '.html')
        return candidates + new_candidates

    @classmethod
    def filter_templates(cls, template, course):
        """
        Filter template that contains 'latex' from templates.

        Show them only if use_latex_compiler is set to True in
        course settings.
        """
        return 'latex' not in template[
            'template_id'] or course.use_latex_compiler

    def get_context(self):
        """
        an override to add in specific rendering context, in this case we need to
        add in a base path to our c4x content addressing scheme
        """
        _context = EditingDescriptor.get_context(self)
        # Add some specific HTML rendering context when editing HTML modules where we pass
        # the root /c4x/ url for assets. This allows client-side substitutions to occur.
        _context.update({
            'base_asset_url':
            StaticContent.get_base_url_path_for_course_assets(
                self.location.course_key),
            'enable_latex_compiler':
            self.use_latex_compiler,
            'editor':
            self.editor
        })
        return _context

    # NOTE: html descriptors are special.  We do not want to parse and
    # export them ourselves, because that can break things (e.g. lxml
    # adds body tags when it exports, but they should just be html
    # snippets that will be included in the middle of pages.

    @classmethod
    def load_definition(cls, xml_object, system, location, id_generator):
        '''Load a descriptor from the specified xml_object:

        If there is a filename attribute, load it as a string, and
        log a warning if it is not parseable by etree.HTMLParser.

        If there is not a filename attribute, the definition is the body
        of the xml_object, without the root tag (do not want <html> in the
        middle of a page)

        Args:
            xml_object: an lxml.etree._Element containing the definition to load
            system: the modulestore system or runtime which caches data
            location: the usage id for the block--used to compute the filename if none in the xml_object
            id_generator: used by other impls of this method to generate the usage_id
        '''
        filename = xml_object.get('filename')
        if filename is None:
            definition_xml = copy.deepcopy(xml_object)
            cls.clean_metadata_from_xml(definition_xml)
            return {'data': stringify_children(definition_xml)}, []
        else:
            # html is special.  cls.filename_extension is 'xml', but
            # if 'filename' is in the definition, that means to load
            # from .html
            # 'filename' in html pointers is a relative path
            # (not same as 'html/blah.html' when the pointer is in a directory itself)
            pointer_path = "{category}/{url_path}".format(
                category='html', url_path=name_to_pathname(location.block_id))
            base = path(pointer_path).dirname()
            # log.debug("base = {0}, base.dirname={1}, filename={2}".format(base, base.dirname(), filename))
            filepath = u"{base}/{name}.html".format(base=base, name=filename)
            # log.debug("looking for html file for {0} at {1}".format(location, filepath))

            # VS[compat]
            # TODO (cpennington): If the file doesn't exist at the right path,
            # give the class a chance to fix it up. The file will be written out
            # again in the correct format.  This should go away once the CMS is
            # online and has imported all current (fall 2012) courses from xml
            if not system.resources_fs.exists(filepath):

                candidates = cls.backcompat_paths(filepath)
                # log.debug("candidates = {0}".format(candidates))
                for candidate in candidates:
                    if system.resources_fs.exists(candidate):
                        filepath = candidate
                        break

            try:
                with system.resources_fs.open(filepath,
                                              encoding='utf-8') as infile:
                    html = infile.read()
                    # Log a warning if we can't parse the file, but don't error
                    if not check_html(html) and len(html) > 0:
                        msg = "Couldn't parse html in {0}, content = {1}".format(
                            filepath, html)
                        log.warning(msg)
                        system.error_tracker("Warning: " + msg)

                    definition = {'data': html}

                    # TODO (ichuang): remove this after migration
                    # for Fall 2012 LMS migration: keep filename (and unmangled filename)
                    definition['filename'] = [filepath, filename]

                    return definition, []

            except ResourceNotFound as err:
                msg = 'Unable to load file contents at path {0}: {1} '.format(
                    filepath, err)
                # add more info and re-raise
                raise Exception(msg), None, sys.exc_info()[2]

    # TODO (vshnayder): make export put things in the right places.

    def definition_to_xml(self, resource_fs):
        ''' Write <html filename="" [meta-attrs="..."]> to filename.xml, and the html
        string to filename.html.
        '''

        # Write html to file, return an empty tag
        pathname = name_to_pathname(self.url_name)
        filepath = u'{category}/{pathname}.html'.format(category=self.category,
                                                        pathname=pathname)

        resource_fs.makedirs(os.path.dirname(filepath), recreate=True)
        with resource_fs.open(filepath, 'wb') as filestream:
            html_data = self.data.encode('utf-8')
            filestream.write(html_data)

        # write out the relative name
        relname = path(pathname).basename()

        elt = etree.Element('html')
        elt.set("filename", relname)
        return elt

    @property
    def non_editable_metadata_fields(self):
        """
        `use_latex_compiler` should not be editable in the Studio settings editor.
        """
        non_editable_fields = super(HtmlDescriptor,
                                    self).non_editable_metadata_fields
        non_editable_fields.append(HtmlDescriptor.use_latex_compiler)
        return non_editable_fields

    def index_dictionary(self):
        xblock_body = super(HtmlDescriptor, self).index_dictionary()
        # Removing script and style
        html_content = re.sub(
            re.compile(
                r"""
                    <script>.*?</script> |
                    <style>.*?</style>
                """, re.DOTALL | re.VERBOSE), "", self.data)
        html_content = escape_html_characters(html_content)
        html_body = {
            "html_content": html_content,
            "display_name": self.display_name,
        }
        if "content" in xblock_body:
            xblock_body["content"].update(html_body)
        else:
            xblock_body["content"] = html_body
        xblock_body["content_type"] = "Text"
        return xblock_body
Example #40
0
    def pytest_sessionfinish(self):
        if not os.path.exists(os.path.dirname(self.logfile)):
            os.makedirs(os.path.dirname(self.logfile))
        logfile = open(self.logfile, 'w', encoding='utf-8')
        suite_stop_time = time.time()
        suite_time_delta = suite_stop_time - self.suite_start_time
        numtests = self.passed + self.failed + self.xpassed + self.xfailed
        generated = datetime.datetime.now()

        style_css = pkg_resources.resource_string(
            __name__, os.path.join('resources', 'style.css'))
        if PY3:
            style_css = style_css.decode('utf-8')

        head = html.head(
            html.meta(charset='utf-8'),
            html.title('Test Report'),
            html.style(raw(style_css)))

        summary = [html.h2('Summary'), html.p(
            '{0} tests ran in {1:.2f} seconds.'.format(
                numtests, suite_time_delta),
            html.br(),
            html.span('{0} passed'.format(
                self.passed), class_='passed'), ', ',
            html.span('{0} skipped'.format(
                self.skipped), class_='skipped'), ', ',
            html.span('{0} failed'.format(
                self.failed), class_='failed'), ', ',
            html.span('{0} errors'.format(
                self.errors), class_='error'), '.',
            html.br(),
            html.span('{0} expected failures'.format(
                self.xfailed), class_='skipped'), ', ',
            html.span('{0} unexpected passes'.format(
                self.xpassed), class_='failed'), '.')]

        results = [html.h2('Results'), html.table([html.thead(
            html.tr([
                html.th('Result',
                        class_='sortable initial-sort result',
                        col='result'),
                html.th('Test', class_='sortable', col='name'),
                html.th('Duration',
                        class_='sortable numeric',
                        col='duration'),
                html.th('Links')]), id='results-table-head'),
            html.tbody(*self.test_logs, id='results-table-body')],
            id='results-table')]

        main_js = pkg_resources.resource_string(
            __name__, os.path.join('resources', 'main.js'))
        if PY3:
            main_js = main_js.decode('utf-8')

        body = html.body(
            html.script(raw(main_js)),
            html.p('Report generated on {0} at {1}'.format(
                generated.strftime('%d-%b-%Y'),
                generated.strftime('%H:%M:%S'))))

        if self.environment is not None:
            body.append(html.h2('Environment'))
            body.append(html.table(
                [html.tr(html.td(e[0]), html.td(e[1])) for e in sorted(
                    self.environment, key=lambda e: e[0]) if e[1]],
                id='environment'))

        body.extend(summary)
        body.extend(results)

        doc = html.html(head, body)

        logfile.write('<!DOCTYPE html>')
        unicode_doc = doc.unicode(indent=2)
        if PY3:
            # Fix encoding issues, e.g. with surrogates
            unicode_doc = unicode_doc.encode('utf-8',
                                             errors='xmlcharrefreplace')
            unicode_doc = unicode_doc.decode('utf-8')
        logfile.write(unicode_doc)
        logfile.close()
Example #41
0
from pkg_resources import resource_string
from redis.client import Script

from sentry.tsdb.base import BaseTSDB
from sentry.utils.dates import to_datetime, to_timestamp
from sentry.utils.redis import check_cluster_versions, get_cluster_from_options
from sentry.utils.versioning import Version
from six.moves import reduce

logger = logging.getLogger(__name__)

SketchParameters = namedtuple('SketchParameters', 'depth width capacity')

CountMinScript = Script(
    None,
    resource_string('sentry', 'scripts/tsdb/cmsketch.lua'),
)


class RedisTSDB(BaseTSDB):
    """
    A time series storage backend for Redis.

    The time series API supports three data types:

        * simple counters
        * distinct counters (number of unique elements seen)
        * frequency tables (a set of items ranked by most frequently observed)

    The backend also supports virtual nodes (``vnodes``) which controls shard
    distribution. This value should be set to the anticipated maximum number of
Example #42
0
def ProcessFile(inputFilename, outDir, languageFilename, templateFilename):
    currentDateTime = strftime("%d/%m/%Y at %H:%M:%S")

    try:
        oneOutputFilePerMsg = language.oneOutputFilePerMsg
    except AttributeError:
        oneOutputFilePerMsg = False

    # open output file now
    if not oneOutputFilePerMsg:
        filename = os.path.basename(inputFilename).split('.')[0]
        outputFilename, outFile = OutputFile(inputFilename, filename, outDir)
        if not outFile:
            return

    # read the input file
    inputData = readFile(inputFilename)

    # if there's no input, return without creating output
    if inputData == 0:
        print("ERROR!  input is empty!")
        return

    # read the template file
    if os.path.isfile(templateFilename):
        with open(templateFilename, 'r') as templateFile:
            template = templateFile.read().splitlines()
    elif os.path.isfile(languageFilename + '/' + templateFilename):
        with open(languageFilename + '/' + templateFilename,
                  'r') as templateFile:
            template = templateFile.read().splitlines()
    else:
        from pkg_resources import resource_string
        try:
            template = resource_string(language.__name__,
                                       templateFilename).decode(
                                           'UTF-8', 'replace').splitlines()
        except FileNotFoundError:
            print("Error opening " + language.__name__ + " " +
                  templateFilename)
            sys.exit(1)

    replacements = {}
    enums = Enums(inputData)
    ids = MsgIDs(inputData)

    PatchStructs(inputData)

    firstTime = True
    if "Messages" in inputData:
        for msg in Messages(inputData):
            msg["ids"] = ids
            try:
                msg["commonSubdir"] = CommonSubdir(inputFilename,
                                                   outDir + "/fake")

                if oneOutputFilePerMsg:
                    outputFilename, outFile = OutputFile(
                        inputFilename, msgShortName(msg), outDir)
                    if not outFile:
                        continue

                replacements["<ENUMERATIONS>"] = language.enums(
                    UsedEnums(msg, enums))
                replacements["<MSGNAME>"] = msgName(msg)
                replacements["<MSGSHORTNAME>"] = msgShortName(msg)
                replacements["<NUMBER_OF_FIELDS>"] = str(numberOfFields(msg))
                replacements["<NUMBER_OF_SUBFIELDS>"] = str(
                    numberOfSubfields(msg))
                undefinedMsgId = "UNDEFINED_MSGID"
                try:
                    undefinedMsgId = language.undefinedMsgId()
                except AttributeError:
                    pass
                try:
                    replacements["<MSGID>"] = language.languageConst(
                        msgID(msg, enums, undefinedMsgId))
                except AttributeError:
                    replacements["<MSGID>"] = str(
                        msgID(msg, enums, undefinedMsgId))
                replacements["<MSGSIZE>"] = str(msgSize(msg))
                replacements["<MSGDESCRIPTION>"] = str(
                    fieldItem(msg, "Description", "")).replace('\n', ' ')
                replacements["<ACCESSORS>"] = "\n".join(
                    language.accessors(msg))
                replacements["<DECLARATIONS>"] = "\n".join(
                    language.declarations(msg))
                replacements["<INIT_CODE>"] = "\n".join(language.initCode(msg))
                replacements["<OUTPUTFILENAME>"] = outputFilename
                replacements["<INPUTFILENAME>"] = inputFilename
                replacements["<TEMPLATEFILENAME>"] = templateFilename
                replacements["<LANGUAGEFILENAME>"] = languageFilename
                replacements["<MESSAGE_PACKAGE>"] = msg[
                    "commonSubdir"].replace('/', '.').replace('\\', '.')
                replacements["<MSGDESCRIPTOR>"] = msgDescriptor(
                    msg, inputFilename)
                replacements["<DATE>"] = currentDateTime
                replacements["<MSGALIAS>"] = msgAlias(msg)
                for line in template:
                    line = DoReplacements(line, msg, replacements, firstTime)
                    outFile.write(line)
                if oneOutputFilePerMsg:
                    outfileLen = outFile.tell()
                    outFile.close()
                else:
                    firstTime = False

            except MessageException as e:
                sys.stderr.write(str(e) + '\n')
                outFile.close()
                os.remove(outputFilename)
                sys.exit(1)
    if not oneOutputFilePerMsg:
        outfileLen = outFile.tell()
        outFile.close()
Example #43
0
#!/usr/bin/env python

from setuptools import setup
import json
from pkg_resources import resource_string
package_json = resource_string('jupyter_skeleton', 'package.json')
npm_package = json.loads(package_json.decode('ascii'))
setup(
      name=npm_package['name'],
      version=npm_package['version'],
      description=npm_package['description'],
      author=npm_package['author'],
      author_email='*****@*****.**',
      url=npm_package['repository']['url'],
      packages=['jupyter_skeleton'],
      package_data={'jupyter_skeleton': ['package.json',
                                         'amd/index.js']},
)
Example #44
0
def get_template(file_name: str) -> jinja2.Template:
    # this_module = __import__('bigsmoke.templates').templates
    template_str: str = resource_string("bigsmoke.templates",
                                        file_name).decode("UTF-8")
    return jinja2.Template(template_str)
Example #45
0
 def update_config(self):
     config_dest = os.path.join(self.jenkins_home, 'config.xml')
     config_dest_file = open(config_dest, 'w')
     config_source = pkg_resources.resource_string(
         'jenkinsapi_tests.systests', 'config.xml')
     config_dest_file.write(config_source.encode('UTF-8'))
Example #46
0
class CapaModule(CapaFields, XModule):
    """
    An XModule implementing LonCapa format problems, implemented by way of
    capa.capa_problem.LoncapaProblem

    CapaModule.__init__ takes the same arguments as xmodule.x_module:XModule.__init__
    """
    icon_class = 'problem'

    js = {
        'coffee': [
            resource_string(__name__, 'js/src/capa/display.coffee'),
            resource_string(__name__, 'js/src/collapsible.coffee'),
            resource_string(__name__, 'js/src/javascript_loader.coffee'),
        ],
        'js': [
            resource_string(__name__, 'js/src/capa/imageinput.js'),
            resource_string(__name__, 'js/src/capa/schematic.js')
        ]
    }

    js_module_name = "Problem"
    css = {'scss': [resource_string(__name__, 'css/capa/display.scss')]}

    def __init__(self, *args, **kwargs):
        """
        Accepts the same arguments as xmodule.x_module:XModule.__init__
        """
        XModule.__init__(self, *args, **kwargs)

        due_date = self.due

        if self.graceperiod is not None and due_date:
            self.close_date = due_date + self.graceperiod
        else:
            self.close_date = due_date

        if self.seed is None:
            self.choose_new_seed()

        # Need the problem location in openendedresponse to send out.  Adding
        # it to the system here seems like the least clunky way to get it
        # there.
        self.system.set('location', self.location.url())

        try:
            # TODO (vshnayder): move as much as possible of this work and error
            # checking to descriptor load time
            self.lcp = self.new_lcp(self.get_state_for_lcp())

            # At this point, we need to persist the randomization seed
            # so that when the problem is re-loaded (to check/view/save)
            # it stays the same.
            # However, we do not want to write to the database
            # every time the module is loaded.
            # So we set the seed ONLY when there is not one set already
            if self.seed is None:
                self.seed = self.lcp.seed

        except Exception as err:
            msg = u'cannot create LoncapaProblem {loc}: {err}'.format(
                loc=self.location.url(), err=err)
            # TODO (vshnayder): do modules need error handlers too?
            # We shouldn't be switching on DEBUG.
            if self.system.DEBUG:
                log.warning(msg)
                # TODO (vshnayder): This logic should be general, not here--and may
                # want to preserve the data instead of replacing it.
                # e.g. in the CMS
                msg = u'<p>{msg}</p>'.format(msg=cgi.escape(msg))
                msg += u'<p><pre>{tb}</pre></p>'.format(
                    tb=cgi.escape(traceback.format_exc()))
                # create a dummy problem with error message instead of failing
                problem_text = (
                    u'<problem><text><span class="inline-error">'
                    u'Problem {url} has an error:</span>{msg}</text></problem>'
                    .format(url=self.location.url(), msg=msg))
                self.lcp = self.new_lcp(self.get_state_for_lcp(),
                                        text=problem_text)
            else:
                # add extra info and raise
                raise Exception(msg), None, sys.exc_info()[2]

            self.set_state_from_lcp()

        assert self.seed is not None

    def choose_new_seed(self):
        """
        Choose a new seed.
        """
        if self.rerandomize == 'never':
            self.seed = 1
        elif self.rerandomize == "per_student" and hasattr(
                self.system, 'seed'):
            # see comment on randomization_bin
            self.seed = randomization_bin(self.system.seed, self.location.url)
        else:
            self.seed = struct.unpack('i', os.urandom(4))[0]

            # So that sandboxed code execution can be cached, but still have an interesting
            # number of possibilities, cap the number of different random seeds.
            self.seed %= MAX_RANDOMIZATION_BINS

    def new_lcp(self, state, text=None):
        """
        Generate a new Loncapa Problem
        """
        if text is None:
            text = self.data

        return LoncapaProblem(
            problem_text=text,
            id=self.location.html_id(),
            state=state,
            seed=self.seed,
            system=self.system,
        )

    def get_state_for_lcp(self):
        """
        Give a dictionary holding the state of the module
        """
        return {
            'done': self.done,
            'correct_map': self.correct_map,
            'student_answers': self.student_answers,
            'input_state': self.input_state,
            'seed': self.seed,
        }

    def set_state_from_lcp(self):
        """
        Set the module's state from the settings in `self.lcp`
        """
        lcp_state = self.lcp.get_state()
        self.done = lcp_state['done']
        self.correct_map = lcp_state['correct_map']
        self.input_state = lcp_state['input_state']
        self.student_answers = lcp_state['student_answers']
        self.seed = lcp_state['seed']

    def get_score(self):
        """
        Access the problem's score
        """
        return self.lcp.get_score()

    def max_score(self):
        """
        Access the problem's max score
        """
        return self.lcp.get_max_score()

    def get_progress(self):
        """
        For now, just return score / max_score
        """
        d = self.get_score()
        score = d['score']
        total = d['total']

        if total > 0:
            if self.weight is not None:
                # scale score and total by weight/total:
                score = score * self.weight / total
                total = self.weight

            try:
                return Progress(score, total)
            except (TypeError, ValueError):
                log.exception("Got bad progress")
                return None
        return None

    def get_html(self):
        """
        Return some html with data about the module
        """
        progress = self.get_progress()
        return self.system.render_template(
            'problem_ajax.html', {
                'element_id': self.location.html_id(),
                'id': self.id,
                'ajax_url': self.system.ajax_url,
                'progress_status': Progress.to_js_status_str(progress),
                'progress_detail': Progress.to_js_detail_str(progress),
            })

    def check_button_name(self):
        """
        Determine the name for the "check" button.

        Usually it is just "Check", but if this is the student's
        final attempt, change the name to "Final Check"
        """
        if self.max_attempts is not None:
            final_check = (self.attempts >= self.max_attempts - 1)
        else:
            final_check = False

        return _("Final Check") if final_check else _("Check")

    def should_show_check_button(self):
        """
        Return True/False to indicate whether to show the "Check" button.
        """
        submitted_without_reset = (self.is_submitted()
                                   and self.rerandomize == "always")

        # If the problem is closed (past due / too many attempts)
        # then we do NOT show the "check" button
        # Also, do not show the "check" button if we're waiting
        # for the user to reset a randomized problem
        if self.closed() or submitted_without_reset:
            return False
        else:
            return True

    def should_show_reset_button(self):
        """
        Return True/False to indicate whether to show the "Reset" button.
        """
        is_survey_question = (self.max_attempts == 0)

        if self.rerandomize in ["always", "onreset"]:

            # If the problem is closed (and not a survey question with max_attempts==0),
            # then do NOT show the reset button.
            # If the problem hasn't been submitted yet, then do NOT show
            # the reset button.
            if (self.closed()
                    and not is_survey_question) or not self.is_submitted():
                return False
            else:
                return True
        # Only randomized problems need a "reset" button
        else:
            return False

    def should_show_save_button(self):
        """
        Return True/False to indicate whether to show the "Save" button.
        """

        # If the user has forced the save button to display,
        # then show it as long as the problem is not closed
        # (past due / too many attempts)
        if self.force_save_button:
            return not self.closed()
        else:
            is_survey_question = (self.max_attempts == 0)
            needs_reset = self.is_submitted() and self.rerandomize == "always"

            # If the student has unlimited attempts, and their answers
            # are not randomized, then we do not need a save button
            # because they can use the "Check" button without consequences.
            #
            # The consequences we want to avoid are:
            # * Using up an attempt (if max_attempts is set)
            # * Changing the current problem, and no longer being
            #   able to view it (if rerandomize is "always")
            #
            # In those cases. the if statement below is false,
            # and the save button can still be displayed.
            #
            if self.max_attempts is None and self.rerandomize != "always":
                return False

            # If the problem is closed (and not a survey question with max_attempts==0),
            # then do NOT show the save button
            # If we're waiting for the user to reset a randomized problem
            # then do NOT show the save button
            elif (self.closed() and not is_survey_question) or needs_reset:
                return False
            else:
                return True

    def handle_problem_html_error(self, err):
        """
        Create a dummy problem to represent any errors.

        Change our problem to a dummy problem containing a warning message to
        display to users. Returns the HTML to show to users

        `err` is the Exception encountered while rendering the problem HTML.
        """
        log.exception(err.message)

        # TODO (vshnayder): another switch on DEBUG.
        if self.system.DEBUG:
            msg = (
                u'[courseware.capa.capa_module] <font size="+1" color="red">'
                u'Failed to generate HTML for problem {url}</font>'.format(
                    url=cgi.escape(self.location.url())))
            msg += u'<p>Error:</p><p><pre>{msg}</pre></p>'.format(
                msg=cgi.escape(err.message))
            msg += u'<p><pre>{tb}</pre></p>'.format(
                tb=cgi.escape(traceback.format_exc()))
            html = msg

        else:
            # We're in non-debug mode, and possibly even in production. We want
            #   to avoid bricking of problem as much as possible

            # Presumably, student submission has corrupted LoncapaProblem HTML.
            #   First, pull down all student answers
            student_answers = self.lcp.student_answers
            answer_ids = student_answers.keys()

            # Some inputtypes, such as dynamath, have additional "hidden" state that
            #   is not exposed to the student. Keep those hidden
            # TODO: Use regex, e.g. 'dynamath' is suffix at end of answer_id
            hidden_state_keywords = ['dynamath']
            for answer_id in answer_ids:
                for hidden_state_keyword in hidden_state_keywords:
                    if answer_id.find(hidden_state_keyword) >= 0:
                        student_answers.pop(answer_id)

            #   Next, generate a fresh LoncapaProblem
            self.lcp = self.new_lcp(None)
            self.set_state_from_lcp()

            # Prepend a scary warning to the student
            warning = '<div class="capa_reset">'\
                      '<h2>Warning: The problem has been reset to its initial state!</h2>'\
                      'The problem\'s state was corrupted by an invalid submission. ' \
                      'The submission consisted of:'\
                      '<ul>'
            for student_answer in student_answers.values():
                if student_answer != '':
                    warning += '<li>' + cgi.escape(student_answer) + '</li>'
            warning += '</ul>'\
                       'If this error persists, please contact the course staff.'\
                       '</div>'

            html = warning
            try:
                html += self.lcp.get_html()
            except Exception:  # Couldn't do it. Give up
                log.exception("Unable to generate html from LoncapaProblem")
                raise

        return html

    def get_problem_html(self, encapsulate=True):
        """
        Return html for the problem.

        Adds check, reset, save buttons as necessary based on the problem config and state.
        """

        try:
            html = self.lcp.get_html()

        # If we cannot construct the problem HTML,
        # then generate an error message instead.
        except Exception as err:
            html = self.handle_problem_html_error(err)

        # The convention is to pass the name of the check button
        # if we want to show a check button, and False otherwise
        # This works because non-empty strings evaluate to True
        if self.should_show_check_button():
            check_button = self.check_button_name()
        else:
            check_button = False

        content = {
            'name': self.display_name_with_default,
            'html': html,
            'weight': self.weight,
        }

        context = {
            'problem': content,
            'id': self.id,
            'check_button': check_button,
            'reset_button': self.should_show_reset_button(),
            'save_button': self.should_show_save_button(),
            'answer_available': self.answer_available(),
            'attempts_used': self.attempts,
            'attempts_allowed': self.max_attempts,
        }

        html = self.system.render_template('problem.html', context)

        if encapsulate:
            html = u'<div id="problem_{id}" class="problem" data-url="{ajax_url}">'.format(
                id=self.location.html_id(),
                ajax_url=self.system.ajax_url) + html + "</div>"

        # now do all the substitutions which the LMS module_render normally does, but
        # we need to do here explicitly since we can get called for our HTML via AJAX
        html = self.system.replace_urls(html)
        if self.system.replace_course_urls:
            html = self.system.replace_course_urls(html)

        if self.system.replace_jump_to_id_urls:
            html = self.system.replace_jump_to_id_urls(html)

        return html

    def handle_ajax(self, dispatch, data):
        """
        This is called by courseware.module_render, to handle an AJAX call.

        `data` is request.POST.

        Returns a json dictionary:
        { 'progress_changed' : True/False,
          'progress' : 'none'/'in_progress'/'done',
          <other request-specific values here > }
        """
        handlers = {
            'problem_get': self.get_problem,
            'problem_check': self.check_problem,
            'problem_reset': self.reset_problem,
            'problem_save': self.save_problem,
            'problem_show': self.get_answer,
            'score_update': self.update_score,
            'input_ajax': self.handle_input_ajax,
            'ungraded_response': self.handle_ungraded_response
        }

        generic_error_message = (
            "We're sorry, there was an error with processing your request. "
            "Please try reloading your page and trying again.")

        not_found_error_message = (
            "The state of this problem has changed since you loaded this page. "
            "Please refresh your page.")

        if dispatch not in handlers:
            return 'Error'

        before = self.get_progress()

        try:
            result = handlers[dispatch](data)

        except NotFoundError as err:
            _, _, traceback_obj = sys.exc_info()
            raise ProcessingError, (not_found_error_message,
                                    err), traceback_obj

        except Exception as err:
            _, _, traceback_obj = sys.exc_info()
            raise ProcessingError, (generic_error_message, err), traceback_obj

        after = self.get_progress()

        result.update({
            'progress_changed': after != before,
            'progress_status': Progress.to_js_status_str(after),
            'progress_detail': Progress.to_js_detail_str(after),
        })

        return json.dumps(result, cls=ComplexEncoder)

    def is_past_due(self):
        """
        Is it now past this problem's due date, including grace period?
        """
        return (self.close_date is not None
                and datetime.datetime.now(UTC()) > self.close_date)

    def closed(self):
        """
        Is the student still allowed to submit answers?
        """
        if self.max_attempts is not None and self.attempts >= self.max_attempts:
            return True
        if self.is_past_due():
            return True

        return False

    def is_submitted(self):
        """
        Used to decide to show or hide RESET or CHECK buttons.

        Means that student submitted problem and nothing more.
        Problem can be completely wrong.
        Pressing RESET button makes this function to return False.
        """
        # used by conditional module
        return self.lcp.done

    def is_attempted(self):
        """
        Has the problem been attempted?

        used by conditional module
        """
        return self.attempts > 0

    def is_correct(self):
        """
        True iff full points
        """
        d = self.get_score()
        return d['score'] == d['total']

    def answer_available(self):
        """
        Is the user allowed to see an answer?
        """
        if self.showanswer == '':
            return False
        elif self.showanswer == "never":
            return False
        elif self.system.user_is_staff:
            # This is after the 'never' check because admins can see the answer
            # unless the problem explicitly prevents it
            return True
        elif self.showanswer == 'attempted':
            return self.attempts > 0
        elif self.showanswer == 'answered':
            # NOTE: this is slightly different from 'attempted' -- resetting the problems
            # makes lcp.done False, but leaves attempts unchanged.
            return self.lcp.done
        elif self.showanswer == 'closed':
            return self.closed()
        elif self.showanswer == 'finished':
            return self.closed() or self.is_correct()

        elif self.showanswer == 'past_due':
            return self.is_past_due()
        elif self.showanswer == 'always':
            return True

        return False

    def update_score(self, data):
        """
        Delivers grading response (e.g. from asynchronous code checking) to
            the capa problem, so its score can be updated

        'data' must have a key 'response' which is a string that contains the
            grader's response

        No ajax return is needed. Return empty dict.
        """
        queuekey = data['queuekey']
        score_msg = data['xqueue_body']
        self.lcp.update_score(score_msg, queuekey)
        self.set_state_from_lcp()
        self.publish_grade()

        return dict()  # No AJAX return is needed

    def handle_ungraded_response(self, data):
        """
        Delivers a response from the XQueue to the capa problem

        The score of the problem will not be updated

        Args:
            - data (dict) must contain keys:
                            queuekey - a key specific to this response
                            xqueue_body - the body of the response
        Returns:
            empty dictionary

        No ajax return is needed, so an empty dict is returned
        """
        queuekey = data['queuekey']
        score_msg = data['xqueue_body']

        # pass along the xqueue message to the problem
        self.lcp.ungraded_response(score_msg, queuekey)
        self.set_state_from_lcp()
        return dict()

    def handle_input_ajax(self, data):
        """
        Handle ajax calls meant for a particular input in the problem

        Args:
            - data (dict) - data that should be passed to the input
        Returns:
            - dict containing the response from the input
        """
        response = self.lcp.handle_input_ajax(data)

        # save any state changes that may occur
        self.set_state_from_lcp()
        return response

    def get_answer(self, data):
        """
        For the "show answer" button.

        Returns the answers: {'answers' : answers}
        """
        event_info = dict()
        event_info['problem_id'] = self.location.url()
        self.system.track_function('showanswer', event_info)
        if not self.answer_available():
            raise NotFoundError('Answer is not available')
        else:
            answers = self.lcp.get_question_answers()
            self.set_state_from_lcp()

        # answers (eg <solution>) may have embedded images
        #   but be careful, some problems are using non-string answer dicts
        new_answers = dict()
        for answer_id in answers:
            try:
                new_answer = {
                    answer_id: self.system.replace_urls(answers[answer_id])
                }
            except TypeError:
                log.debug(
                    u'Unable to perform URL substitution on answers[%s]: %s',
                    answer_id, answers[answer_id])
                new_answer = {answer_id: answers[answer_id]}
            new_answers.update(new_answer)

        return {'answers': new_answers}

    # Figure out if we should move these to capa_problem?
    def get_problem(self, _data):
        """
        Return results of get_problem_html, as a simple dict for json-ing.
        { 'html': <the-html> }

        Used if we want to reconfirm we have the right thing e.g. after
        several AJAX calls.
        """
        return {'html': self.get_problem_html(encapsulate=False)}

    @staticmethod
    def make_dict_of_responses(data):
        """
        Make dictionary of student responses (aka "answers")

        `data` is POST dictionary (Django QueryDict).

        The `data` dict has keys of the form 'x_y', which are mapped
        to key 'y' in the returned dict.  For example,
        'input_1_2_3' would be mapped to '1_2_3' in the returned dict.

        Some inputs always expect a list in the returned dict
        (e.g. checkbox inputs).  The convention is that
        keys in the `data` dict that end with '[]' will always
        have list values in the returned dict.
        For example, if the `data` dict contains {'input_1[]': 'test' }
        then the output dict would contain {'1': ['test'] }
        (the value is a list).

        Some other inputs such as ChoiceTextInput expect a dict of values in the returned
        dict  If the key ends with '{}' then we will assume that the value is a json
        encoded dict and deserialize it.
        For example, if the `data` dict contains {'input_1{}': '{"1_2_1": 1}'}
        then the output dict would contain {'1': {"1_2_1": 1} }
        (the value is a dictionary)

        Raises an exception if:

        -A key in the `data` dictionary does not contain at least one underscore
          (e.g. "input" is invalid, but "input_1" is valid)

        -Two keys end up with the same name in the returned dict.
          (e.g. 'input_1' and 'input_1[]', which both get mapped to 'input_1'
           in the returned dict)
        """
        answers = dict()

        for key in data:
            # e.g. input_resistor_1 ==> resistor_1
            _, _, name = key.partition('_')

            # If key has no underscores, then partition
            # will return (key, '', '')
            # We detect this and raise an error
            if not name:
                raise ValueError(
                    u"{key} must contain at least one underscore".format(
                        key=key))

            else:
                # This allows for answers which require more than one value for
                # the same form input (e.g. checkbox inputs). The convention is that
                # if the name ends with '[]' (which looks like an array), then the
                # answer will be an array.
                # if the name ends with '{}' (Which looks like a dict),
                # then the answer will be a dict
                is_list_key = name.endswith('[]')
                is_dict_key = name.endswith('{}')
                name = name[:-2] if is_list_key or is_dict_key else name

                if is_list_key:
                    val = data.getlist(key)
                elif is_dict_key:
                    try:
                        val = json.loads(data[key])
                    # If the submission wasn't deserializable, raise an error.
                    except (KeyError, ValueError):
                        raise ValueError(
                            u"Invalid submission: {val} for {key}".format(
                                val=data[key], key=key))
                else:
                    val = data[key]

                # If the name already exists, then we don't want
                # to override it.  Raise an error instead
                if name in answers:
                    raise ValueError(
                        u"Key {name} already exists in answers dict".format(
                            name=name))
                else:
                    answers[name] = val

        return answers

    def publish_grade(self):
        """
        Publishes the student's current grade to the system as an event
        """
        score = self.lcp.get_score()
        self.system.publish({
            'event_name': 'grade',
            'value': score['score'],
            'max_value': score['total'],
        })

    def check_problem(self, data):
        """
        Checks whether answers to a problem are correct

        Returns a map of correct/incorrect answers:
          {'success' : 'correct' | 'incorrect' | AJAX alert msg string,
           'contents' : html}
        """
        event_info = dict()
        event_info['state'] = self.lcp.get_state()
        event_info['problem_id'] = self.location.url()

        answers = self.make_dict_of_responses(data)
        event_info['answers'] = convert_files_to_filenames(answers)

        # Too late. Cannot submit
        if self.closed():
            event_info['failure'] = 'closed'
            self.system.track_function('problem_check_fail', event_info)
            raise NotFoundError('Problem is closed')

        # Problem submitted. Student should reset before checking again
        if self.done and self.rerandomize == "always":
            event_info['failure'] = 'unreset'
            self.system.track_function('problem_check_fail', event_info)
            raise NotFoundError(
                'Problem must be reset before it can be checked again')

        # Problem queued. Students must wait a specified waittime before they are allowed to submit
        if self.lcp.is_queued():
            current_time = datetime.datetime.now(UTC())
            prev_submit_time = self.lcp.get_recentmost_queuetime()
            waittime_between_requests = self.system.xqueue['waittime']
            if (current_time - prev_submit_time
                ).total_seconds() < waittime_between_requests:
                msg = u'You must wait at least {wait} seconds between submissions'.format(
                    wait=waittime_between_requests)
                return {
                    'success': msg,
                    'html': ''
                }  # Prompts a modal dialog in ajax callback

        try:
            correct_map = self.lcp.grade_answers(answers)
            self.attempts = self.attempts + 1
            self.lcp.done = True
            self.set_state_from_lcp()

        except (StudentInputError, ResponseError, LoncapaProblemError) as inst:
            log.warning("StudentInputError in capa_module:problem_check",
                        exc_info=True)

            # If the user is a staff member, include
            # the full exception, including traceback,
            # in the response
            if self.system.user_is_staff:
                msg = u"Staff debug info: {tb}".format(
                    tb=cgi.escape(traceback.format_exc()))

            # Otherwise, display just an error message,
            # without a stack trace
            else:
                msg = u"Error: {msg}".format(msg=inst.message)

            return {'success': msg}

        except Exception as err:
            if self.system.DEBUG:
                msg = u"Error checking problem: {}".format(err.message)
                msg += u'\nTraceback:\n{}'.format(traceback.format_exc())
                return {'success': msg}
            raise

        self.publish_grade()

        # success = correct if ALL questions in this problem are correct
        success = 'correct'
        for answer_id in correct_map:
            if not correct_map.is_correct(answer_id):
                success = 'incorrect'

        # NOTE: We are logging both full grading and queued-grading submissions. In the latter,
        #       'success' will always be incorrect
        event_info['correct_map'] = correct_map.get_dict()
        event_info['success'] = success
        event_info['attempts'] = self.attempts
        self.system.track_function('problem_check', event_info)

        if hasattr(self.system, 'psychometrics_handler'
                   ):  # update PsychometricsData using callback
            self.system.psychometrics_handler(self.get_state_for_lcp())

        # render problem into HTML
        html = self.get_problem_html(encapsulate=False)

        return {
            'success': success,
            'contents': html,
        }

    def rescore_problem(self):
        """
        Checks whether the existing answers to a problem are correct.

        This is called when the correct answer to a problem has been changed,
        and the grade should be re-evaluated.

        Returns a dict with one key:
            {'success' : 'correct' | 'incorrect' | AJAX alert msg string }

        Raises NotFoundError if called on a problem that has not yet been
        answered, or NotImplementedError if it's a problem that cannot be rescored.

        Returns the error messages for exceptions occurring while performing
        the rescoring, rather than throwing them.
        """
        event_info = {
            'state': self.lcp.get_state(),
            'problem_id': self.location.url()
        }

        if not self.lcp.supports_rescoring():
            event_info['failure'] = 'unsupported'
            self.system.track_function('problem_rescore_fail', event_info)
            raise NotImplementedError(
                "Problem's definition does not support rescoring")

        if not self.done:
            event_info['failure'] = 'unanswered'
            self.system.track_function('problem_rescore_fail', event_info)
            raise NotFoundError(
                'Problem must be answered before it can be graded again')

        # get old score, for comparison:
        orig_score = self.lcp.get_score()
        event_info['orig_score'] = orig_score['score']
        event_info['orig_total'] = orig_score['total']

        try:
            correct_map = self.lcp.rescore_existing_answers()

        except (StudentInputError, ResponseError, LoncapaProblemError) as inst:
            log.warning("Input error in capa_module:problem_rescore",
                        exc_info=True)
            event_info['failure'] = 'input_error'
            self.system.track_function('problem_rescore_fail', event_info)
            return {'success': u"Error: {0}".format(inst.message)}

        except Exception as err:
            event_info['failure'] = 'unexpected'
            self.system.track_function('problem_rescore_fail', event_info)
            if self.system.DEBUG:
                msg = u"Error checking problem: {0}".format(err.message)
                msg += u'\nTraceback:\n' + traceback.format_exc()
                return {'success': msg}
            raise

        # rescoring should have no effect on attempts, so don't
        # need to increment here, or mark done.  Just save.
        self.set_state_from_lcp()

        self.publish_grade()

        new_score = self.lcp.get_score()
        event_info['new_score'] = new_score['score']
        event_info['new_total'] = new_score['total']

        # success = correct if ALL questions in this problem are correct
        success = 'correct'
        for answer_id in correct_map:
            if not correct_map.is_correct(answer_id):
                success = 'incorrect'

        # NOTE: We are logging both full grading and queued-grading submissions. In the latter,
        #       'success' will always be incorrect
        event_info['correct_map'] = correct_map.get_dict()
        event_info['success'] = success
        event_info['attempts'] = self.attempts
        self.system.track_function('problem_rescore', event_info)

        # psychometrics should be called on rescoring requests in the same way as check-problem
        if hasattr(self.system, 'psychometrics_handler'
                   ):  # update PsychometricsData using callback
            self.system.psychometrics_handler(self.get_state_for_lcp())

        return {'success': success}

    def save_problem(self, data):
        """
        Save the passed in answers.
        Returns a dict { 'success' : bool, 'msg' : message }
        The message is informative on success, and an error message on failure.
        """
        event_info = dict()
        event_info['state'] = self.lcp.get_state()
        event_info['problem_id'] = self.location.url()

        answers = self.make_dict_of_responses(data)
        event_info['answers'] = answers

        # Too late. Cannot submit
        if self.closed() and not self.max_attempts == 0:
            event_info['failure'] = 'closed'
            self.system.track_function('save_problem_fail', event_info)
            return {'success': False, 'msg': "Problem is closed"}

        # Problem submitted. Student should reset before saving
        # again.
        if self.done and self.rerandomize == "always":
            event_info['failure'] = 'done'
            self.system.track_function('save_problem_fail', event_info)
            return {
                'success': False,
                'msg': "Problem needs to be reset prior to save"
            }

        self.lcp.student_answers = answers

        self.set_state_from_lcp()

        self.system.track_function('save_problem_success', event_info)
        msg = "Your answers have been saved"
        if not self.max_attempts == 0:
            msg += " but not graded. Hit 'Check' to grade them."
        return {'success': True, 'msg': msg}

    def reset_problem(self, _data):
        """
        Changes problem state to unfinished -- removes student answers,
        and causes problem to rerender itself.

        Returns a dictionary of the form:
          {'success': True/False,
           'html': Problem HTML string }

        If an error occurs, the dictionary will also have an
        `error` key containing an error message.
        """
        event_info = dict()
        event_info['old_state'] = self.lcp.get_state()
        event_info['problem_id'] = self.location.url()

        if self.closed():
            event_info['failure'] = 'closed'
            self.system.track_function('reset_problem_fail', event_info)
            return {'success': False, 'error': "Problem is closed"}

        if not self.done:
            event_info['failure'] = 'not_done'
            self.system.track_function('reset_problem_fail', event_info)
            return {
                'success': False,
                'error':
                "Refresh the page and make an attempt before resetting."
            }

        if self.rerandomize in ["always", "onreset"]:
            # Reset random number generator seed.
            self.choose_new_seed()

        # Generate a new problem with either the previous seed or a new seed
        self.lcp = self.new_lcp(None)

        # Pull in the new problem seed
        self.set_state_from_lcp()

        event_info['new_state'] = self.lcp.get_state()
        self.system.track_function('reset_problem', event_info)

        return {
            'success': True,
            'html': self.get_problem_html(encapsulate=False)
        }
Example #47
0
def write_default_config(target):
    dirname = os.path.dirname(target)
    if not os.path.exists(dirname): os.makedirs(dirname)
    with open(target, "w") as file:
        file.write(pkg_resources.resource_string(__name__, "antd.cfg"))
Example #48
0
from configobj import ConfigObj
from funcy import merge
from hashlib import sha1
from more_itertools import first
from pkg_resources import resource_string
from schema import Optional, Use, And, Schema, SchemaError

from dxr.exceptions import ConfigError
from dxr.plugins import all_plugins_but_core, core_plugin
from dxr.utils import cd, if_raises


# Format version, signifying the instance format this web frontend code is
# able to serve. Must match exactly; deploy will do nothing until it does.
FORMAT = resource_string('dxr', 'format').strip()

WORKERS_VALIDATOR = And(Use(int),
                        lambda v: v >= 0,
                        error='"workers" must be a non-negative integer.')


class DotSection(object):
    """In the absense of an actual attribute, let attr lookup fall through to
    ``self._section[attr]``."""

    def __getattr__(self, attr):
        if not hasattr(self, '_section'):  # Happens during unpickling
            raise AttributeError(attr)
        try:
            val = self._section[attr]
Example #49
0
def certificate_chain():
  return pkg_resources.resource_string(
      __name__, _CERTIFICATE_CHAIN_RESOURCE_PATH)
def initFrameworkWrapper(frameworkName,
                         frameworkPath,
                         frameworkIdentifier,
                         globals,
                         inlineTab=None,
                         scan_classes=None,
                         frameworkResourceName=None):
    """
    Load the named framework, using the identifier if that has result otherwise
    using the path. Also loads the information in the bridgesupport file (
    either one embedded in the framework or one in a BrigeSupport library
    directory).
    """
    if frameworkResourceName is None:
        frameworkResourceName = frameworkName

    if frameworkIdentifier is None:
        if scan_classes is None:
            bundle = objc.loadBundle(frameworkName,
                                     globals,
                                     bundle_path=frameworkPath)
        else:
            bundle = objc.loadBundle(frameworkName,
                                     globals,
                                     bundle_path=frameworkPath,
                                     scan_classes=scan_classes)

    else:
        try:
            if scan_classes is None:
                bundle = objc.loadBundle(frameworkName,
                                         globals,
                                         bundle_identifier=frameworkIdentifier)

            else:
                bundle = objc.loadBundle(frameworkName,
                                         globals,
                                         bundle_identifier=frameworkIdentifier,
                                         scan_classes=scan_classes)

        except ImportError:
            if scan_classes is None:
                bundle = objc.loadBundle(frameworkName,
                                         globals,
                                         bundle_path=frameworkPath)
            else:
                bundle = objc.loadBundle(frameworkName,
                                         globals,
                                         bundle_path=frameworkPath,
                                         scan_classes=scan_classes)

    # Make the objc module available, because it contains a lot of useful
    # functionality.
    globals['objc'] = objc

    # Explicitly push objc.super into the globals dict, that way super
    # calls will behave as expected in all cases.
    globals['super'] = objc.super

    # Look for metadata in the Python wrapper and prefer that over the
    # data in the framework or in system locations.
    # Needed because the system bridgesupport files are buggy.
    if safe_resource_exists(frameworkResourceName, "PyObjC.bridgesupport"):
        data = pkg_resources.resource_string(frameworkResourceName,
                                             "PyObjC.bridgesupport")
        _parseBridgeSupport(data, globals, frameworkName, inlineTab=inlineTab)
        return bundle

    # Look for metadata in the framework bundle
    path = bundle.pathForResource_ofType_inDirectory_(frameworkName,
                                                      'bridgesupport',
                                                      'BridgeSupport')
    if path is not None:
        dylib_path = bundle.pathForResource_ofType_inDirectory_(
            frameworkName, 'dylib', 'BridgeSupport')
        with open(path, 'rb') as fp:
            data = fp.read()
        if dylib_path is not None:
            _parseBridgeSupport(data,
                                globals,
                                frameworkName,
                                dylib_path=dylib_path)
        else:
            _parseBridgeSupport(data, globals, frameworkName)

        # Check if we have additional metadata bundled with PyObjC
        if safe_resource_exists(frameworkResourceName,
                                "PyObjCOverrides.bridgesupport"):
            data = pkg_resources.resource_string(
                frameworkResourceName, "PyObjCOverrides.bridgesupport")
            _parseBridgeSupport(data,
                                globals,
                                frameworkName,
                                inlineTab=inlineTab)

        return bundle

    # If there is no metadata there look for metadata in the standard Library
    # locations
    fn = frameworkName + '.bridgesupport'
    for dn in BRIDGESUPPORT_DIRECTORIES:
        path = os.path.join(dn, fn)
        if os.path.exists(path):
            with open(path, 'rb') as fp:
                data = fp.read()  # pragma: no branch

            dylib_path = os.path.join(dn, frameworkName + '.dylib')
            if os.path.exists(dylib_path):
                _parseBridgeSupport(data,
                                    globals,
                                    frameworkName,
                                    dylib_path=dylib_path)
            else:
                _parseBridgeSupport(data, globals, frameworkName)

            # Check if we have additional metadata bundled with PyObjC
            if safe_resource_exists(frameworkResourceName,
                                    "PyObjCOverrides.bridgesupport"):
                data = pkg_resources.resource_string(
                    frameworkResourceName, "PyObjCOverrides.bridgesupport")
                _parseBridgeSupport(data,
                                    globals,
                                    frameworkName,
                                    inlineTab=inlineTab)

            return bundle

    return bundle
Example #51
0
def test_root_certificates():
  return pkg_resources.resource_string(
      __name__, _ROOT_CERTIFICATES_RESOURCE_PATH)
Example #52
0
 def template_from_resource(resource):
     # type: (str) -> string.Template
     template_str = pkg_resources.resource_string(
         __name__, f"java/{resource}").decode("utf-8")
     template = string.Template(template_str)
     return template
Example #53
0
import sys as _sys

import pkg_resources as _pkg_resources

from vyper.compiler import compile_code, compile_codes  # noqa: F401

if (_sys.version_info.major, _sys.version_info.minor) < (3, 6):
    # Can't be tested, as our test harness is using python3.6.
    raise Exception("Requires python3.6+")  # pragma: no cover

try:
    __version__ = _pkg_resources.get_distribution("vyper").version
except _pkg_resources.DistributionNotFound:
    __version__ = "0.0.0development"

try:
    __commit__ = _pkg_resources.resource_string(
        "vyper", "vyper_git_version.txt").decode("utf-8")
    __commit__ = __commit__[:7]
except FileNotFoundError:
    __commit__ = "unknown"
Example #54
0
def private_key():
  return pkg_resources.resource_string(__name__, _PRIVATE_KEY_RESOURCE_PATH)
Example #55
0
def get_yaml():
    # with open("doc/karp_api_spec.yaml") as fp:
    #     spec = fp.read()
    spec = pkg_resources.resource_string(
        "karp", "data_files/karp_api_spec.yaml").decode("utf-8")
    return Response(spec, mimetype="text/yaml")
Example #56
0
    def demosdm(self):
        lsid = self.request.form.get('lsid')
        # Run SDM on a species given by lsid (from ALA), followed by a Climate
        # Change projection.
        if self.request.get('REQUEST_METHOD', 'GET').upper() != 'POST':
            raise BadRequest('Request must be POST')
        # Swift params
        swiftsettings = getUtility(IRegistry).forInterface(ISwiftSettings)

        # get parameters
        if not lsid:
            raise BadRequest('Required parameter lsid missing')
        # we have an lsid,.... we can't really verify but at least some
        #                      data is here
        # find rest of parameters
        # FIXME: hardcoded path to environmental datasets

        # Get the future climate for climate change projection
        portal = ploneapi.portal.get()
        dspath = '/'.join([
            defaults.DATASETS_FOLDER_ID, defaults.DATASETS_CLIMATE_FOLDER_ID,
            'australia', 'australia_1km', 'RCP85_ukmo-hadgem1_2085.zip'
        ])
        ds = portal.restrictedTraverse(dspath)
        dsuuid = IUUID(ds)
        dlinfo = IDownloadInfo(ds)
        dsmd = IBCCVLMetadata(ds)
        futureclimatelist = []
        for layer in ('B05', 'B06', 'B13', 'B14'):
            futureclimatelist.append({
                'uuid':
                dsuuid,
                'filename':
                dlinfo['filename'],
                'downloadurl':
                dlinfo['url'],
                'layer':
                layer,
                'type':
                dsmd['layers'][layer]['datatype'],
                'zippath':
                dsmd['layers'][layer]['filename']
            })
        # Climate change projection name
        cc_projection_name = os.path.splitext(dlinfo['filename'])[0]

        # Get the current climate for SDM
        dspath = '/'.join([
            defaults.DATASETS_FOLDER_ID, defaults.DATASETS_CLIMATE_FOLDER_ID,
            'australia', 'australia_1km', 'current.76to05.zip'
        ])
        ds = portal.restrictedTraverse(dspath)
        dsuuid = IUUID(ds)
        dlinfo = IDownloadInfo(ds)
        dsmd = IBCCVLMetadata(ds)
        envlist = []
        for layer in ('B05', 'B06', 'B13', 'B14'):
            envlist.append({
                'uuid': dsuuid,
                'filename': dlinfo['filename'],
                'downloadurl': dlinfo['url'],
                'layer': layer,
                'type': dsmd['layers'][layer]['datatype'],
                'zippath': dsmd['layers'][layer]['filename']
            })

        # FIXME: we don't use a IJobTracker here for now
        # get toolkit and
        func = portal[defaults.TOOLKITS_FOLDER_ID]['demosdm']
        # build job_params:
        job_params = {
            'resolution': IBCCVLMetadata(ds)['resolution'],
            'function': func.getId(),
            'species_occurrence_dataset': {
                'uuid': 'ala_occurrence_dataset',
                'species': u'demoSDM',
                'downloadurl': 'ala://ala?lsid={}'.format(lsid),
            },
            'environmental_datasets': envlist,
            'future_climate_datasets': futureclimatelist,
            'cc_projection_name': cc_projection_name
        }
        # add toolkit parameters: (all default values)
        # get toolkit schema
        schema = loadString(func.schema).schema
        for name, field in getFields(schema).items():
            if field.default is not None:
                job_params[name] = field.default
        # add other default parameters
        job_params.update({
            'rescale_all_models': False,
            'selected_models': 'all',
            'modeling_id': 'bccvl',
        })
        # generate script to run
        script = u'\n'.join([
            resource_string('org.bccvl.compute', 'rscripts/bccvl.R'),
            resource_string('org.bccvl.compute', 'rscripts/eval.R'),
            func.script
        ])
        # where to store results.
        result = {
            'results_dir':
            'swift+{}/wordpress/{}/'.format(swiftsettings.storage_url,
                                            urllib.quote_plus(lsid)),
            'outputs':
            json.loads(func.output)
        }
        # worker hints:
        worker = {
            'script': {
                'name': '{}.R'.format(func.getId()),
                'script': script
            },
            'files': ('species_occurrence_dataset', 'environmental_datasets',
                      'future_climate_datasets')
        }
        # put everything together
        jobdesc = {
            'env': {},
            'params': job_params,
            'worker': worker,
            'result': result,
        }

        # create job
        jobtool = getUtility(IJobUtility)
        job = jobtool.new_job(lsid=lsid,
                              toolkit=IUUID(func),
                              function=func.getId(),
                              type='demosdm')
        # create job context object
        member = ploneapi.user.get_current()
        context = {
            # we use the site object as context
            'context': '/'.join(portal.getPhysicalPath()),
            'jobid': job.id,
            'user': {
                'id': member.getUserName(),
                'email': member.getProperty('email'),
                'fullname': member.getProperty('fullname')
            },
        }

        # all set to go build task chain now
        from org.bccvl.tasks.compute import demo_task
        from org.bccvl.tasks.plone import after_commit_task, HIGH_PRIORITY
        after_commit_task(demo_task, HIGH_PRIORITY, jobdesc, context)
        # let's hope everything works, return result

        # We don't create an experiment object, so we don't count stats here
        # let's do it manually
        getUtility(IStatsUtility).count_experiment(
            user=member.getId(),
            portal_type='demosdm',
        )

        return {
            'state': os.path.join(result['results_dir'], 'state.json'),
            'result': os.path.join(result['results_dir'],
                                   'proj_metadata.json'),
            'jobid': job.id
        }
Example #57
0
def _load(name: str) -> bytes:
    return pkg_resources.resource_string(__name__, name)
Example #58
0
class StubOraService(StubHttpService):
    """
    Stub ORA service.
    """
    HANDLER_CLASS = StubOraHandler

    DUMMY_DATA = {
        'submission_id': 1,
        'submission_key': 'test key',
        'student_response': 'Test response',
        'prompt': 'Test prompt',
        'rubric': pkg_resources.resource_string(__name__, "data/ora_rubric.xml"),
        'max_score': 2,
        'message': 'Successfully saved calibration record.',
        'actual_score': 2,
        'actual_rubric': pkg_resources.resource_string(__name__, "data/ora_graded_rubric.xml"),
        'actual_feedback': 'Great job!',
        'student_sub_count': 1,
        'problem_name': 'test problem',
        'problem_list_num_graded': 1,
        'problem_list_num_pending': 1,
        'problem_list_num_required': 0,
    }

    def __init__(self, *args, **kwargs):
        """
        Initialize student submission state.
        """
        super(StubOraService, self).__init__(*args, **kwargs)

        # Create a dict to map student ID's to their state
        self._students = dict()

        # By default, no problems are available for peer grading
        # You can add to this list using the `register_location` HTTP end-point
        # This is a dict mapping problem locations to problem names
        self.problems = dict()

    def student_state(self, student_id):
        """
        Return the `StudentState` (named tuple) for the student
        with ID `student_id`.  The student state can be modified by the caller.
        """
        # Create the student state if it does not already exist
        if student_id not in self._students:
            student = StudentState()
            self._students[student_id] = student

        # Retrieve the student state
        return self._students[student_id]

    @property
    def problem_list(self):
        """
        Return a list of problems available for peer grading.
        """
        return [{
            'location': location, 'problem_name': name,
            'num_graded': self.DUMMY_DATA['problem_list_num_graded'],
            'num_pending': self.DUMMY_DATA['problem_list_num_pending'],
            'num_required': self.DUMMY_DATA['problem_list_num_required']
            } for location, name in self.problems.items()
        ]

    def register_problem(self, location, name):
        """
        Register a new problem with `location` and `name` for peer grading.
        """
        self.problems[location] = name
Example #59
0
"""ACME utilities for testing."""
import datetime
import itertools
import os
import pkg_resources

import Crypto.PublicKey.RSA

from acme import challenges
from acme import jose
from acme import messages2

KEY = jose.HashableRSAKey(
    Crypto.PublicKey.RSA.importKey(
        pkg_resources.resource_string(
            "acme.jose", os.path.join("testdata", "rsa512_key.pem"))))

# Challenges
SIMPLE_HTTPS = challenges.SimpleHTTP(
    token="evaGxfADs6pSRb2LAv9IZf17Dt3juxGJ+PCt92wr+oA")
DVSNI = challenges.DVSNI(
    r="O*\xb4-\xad\xec\x95>\xed\xa9\r0\x94\xe8\x97\x9c&6\xbf'\xb3"
    "\xed\x9a9nX\x0f'\\m\xe7\x12",
    nonce="a82d5ff8ef740d12881f6d3c2277ab2e")
DNS = challenges.DNS(token="17817c66b60ce2e4012dfad92657527a")
RECOVERY_CONTACT = challenges.RecoveryContact(
    activation_url="https://example.ca/sendrecovery/a5bd99383fb0",
    success_url="https://example.ca/confirmrecovery/bb1b9928932",
    contact="c********[email protected]")
RECOVERY_TOKEN = challenges.RecoveryToken()
POP = challenges.ProofOfPossession(
Example #60
0
def _load_javaparser_launcher_source() -> bytes:
    return pkg_resources.resource_string(__name__, _LAUNCHER_BASENAME)