Пример #1
0
 def test_list_slot(self):
     """ Can we retrieve list items?
     """
     self.assertEqual(get_slot([1,2,3], '0'), 1)
     
     obj = Struct({'objects': ['foo','bar']})
     self.assertEqual(get_slot(obj, 'objects.0'), 'foo')
Пример #2
0
    def from_notebook_node(self, nb, resources=None, **kw):
        # The parent nbconvert_support module imports this module, and
        # nbconvert_support is imported as part of our install scripts, and
        # other fairly basic stuff.
        # By keeping lxml import in this method, we can still import this
        # module even if lxml isn't available, or is missing dependencies, etc.
        # In this way, problems with lxml should only bother people who are
        # actually trying to *use* this.
        import lxml.etree as et
        output, resources = super(EmbedHTMLExporter,
                                  self).from_notebook_node(nb, resources)

        self.path = resources['metadata']['path']

        # Get attachments
        self.attachments = Struct()
        for cell in nb.cells:
            if 'attachments' in cell.keys():
                self.attachments += cell['attachments']

        # Parse HTML and replace <img> tags with the embedded data
        parser = et.HTMLParser()
        root = et.fromstring(output, parser=parser)
        nodes = root.findall(".//img")
        for n in nodes:
            self.replfunc(n)

        # Convert back to HTML
        embedded_output = et.tostring(root.getroottree(),
                                      method="html",
                                      encoding='unicode')

        return embedded_output, resources
    def from_notebook_node(self, nb, resources=None, **kw):
        output, resources = super(
            EmbedHTMLExporter, self).from_notebook_node(nb, resources)

        self.path = resources['metadata']['path']

        # Get attachments
        self.attachments = Struct()
        for cell in nb.cells:
            if 'attachments' in cell.keys():
                self.attachments += cell['attachments']

        # Parse HTML and replace <img> tags with the embedded data
        parser = et.HTMLParser()
        root = et.fromstring(output, parser=parser)
        nodes = root.findall(".//img")
        for n in nodes:
            self.replfunc(n)

        # Convert back to HTML
        embedded_output = et.tostring(root.getroottree(),
                                      method="html",
                                      encoding='unicode')

        return embedded_output, resources
Пример #4
0
 def test_basic_slots(self):
     """ Can we retrieve top-level attributes, perhaps recursively?
     """
     obj = Struct({'x':1, 'y':2})
     self.assertEqual(get_slots(obj, 'x'), 1)
     self.assertEqual(get_slots(obj, ['x','y']), [1,2])
     self.assertEqual(get_slots(obj, {'x':'x', 'y':'y'}),
                      {'x':1, 'y': 2})
     self.assertEqual(get_slots(obj, {'letter': {'x':'x'}}),
                      {'letter': {'x':1}})
Пример #5
0
    def from_notebook_node(self, nb, resources=None, **kw):
        output, resources = super(
            EmbedHTMLExporter, self).from_notebook_node(nb, resources)

        self.path = resources['metadata']['path']
        self.attachments = Struct()
        for cell in nb.cells:
            if 'attachments' in cell.keys():
                self.attachments += cell['attachments']
        regex = re.compile('<img\s+src="([^"]+)"')

        embedded_output = regex.sub(self.replfunc, output)
        return embedded_output, resources
Пример #6
0
 def test_method_not_bound(self):
     """ Check that methods that not bound to the object are not called.
     """
     other = Struct()
     other.getter = types.MethodType(lambda self: 0, other)
     obj = Struct()
     obj.meth = other.getter
     self.assertRaises(AttributeError, lambda: get_slot(obj, 'meth'))
Пример #7
0
    def preprocess_cell(self, cell, resources, index):
        """
        Preprocess cell

        Parameters
        ----------
        cell : NotebookNode cell
            Notebook cell being processed
        resources : dictionary
            Additional resources used in the conversion process.  Allows
            preprocessors to pass variables into the Jinja engine.
        index : int
            Index of the cell being processed (see base.py)
        """
        self.path = resources['metadata']['path']
        self.attachments = getattr(cell, 'attachments', Struct())

        if cell.cell_type == "markdown":
            regex = re.compile('!\[([^"]*)\]\(([^"]+)\)')
            cell.source = regex.sub(self.replfunc_md, cell.source)
            cell.attachments = self.attachments
        return cell, resources
Пример #8
0
    def preprocess_cell(self, cell, resources, index):
        """
        Preprocess cell

        Parameters
        ----------
        cell : NotebookNode cell
            Notebook cell being processed
        resources : dictionary
            Additional resources used in the conversion process.
        index : int
            Index of the cell being processed (see base.py) Not used.
        """
        self.path = resources['metadata']['path']
        self.attachments = getattr(cell, 'attachments', Struct())

        if cell.cell_type == "markdown":
            regex = re.compile('!\[([^"]*)\]\(([^"]+)\)')
            otherregex = re.compile('src\s*=\s*\"([^"]+)\"')
            cell.source = regex.sub(self.replfunc_md, cell.source)
            cell.source = otherregex.sub(self.replfunc_img, cell.source)
            cell.attachments = self.attachments
        return cell, resources
Пример #9
0
 def fake_open(arg):
     open_called_with.append(arg)
     return Struct(show=lambda: show_called_with.append(None))
Пример #10
0
 def test_dict_slot(self):
     """ Can we retrieve dictionary items?
     """
     obj = Struct({'objects': {'id1': 'foo', 'id2': 'bar'}})
     self.assertEqual(get_slot(obj, 'objects.id1'), 'foo')
Пример #11
0
 def test_nested_slot(self):
     """ Can we retrieve nested attributes?
     """
     obj = Struct()
     obj.outer = Struct({'inner': 'foo'})
     self.assertEqual(get_slot(obj, 'outer.inner'), 'foo')
Пример #12
0
 def test_method_slot_args(self):
     """ Check that methods with required arguments are not called.
     """
     obj = Struct()
     obj.meth = types.MethodType(lambda self, x: x, obj)
     self.assertRaises(AttributeError, lambda: get_slot(obj, 'meth'))
Пример #13
0
 def test_method_slot(self):
     """ Can we retrieve top-level methods?
     """
     obj = Struct()
     obj.getter = types.MethodType(lambda self: 0, obj)
     self.assertEqual(get_slot(obj, 'getter'), 0)