Skip to content. | Skip to navigation

Personal tools

Navigation

You are here: Home / Anderson Leeb Blog

Anderson Leeb Blog

parse xml in your unit test

a simple way to parse generic setup xml files in a unit test. use to ensure that what you declare is what you get

Parse XML

The nifty part of this is that you don't have to generate a list of the items you want to compare - you read them directly in from your genericsetup profile:

import xml.etree.ElementTree as ET
import os
...
gs_config = os.path.abspath(
os.path.join(
os.path.dirname( __file__ ),
'..', 'profiles/default/tinymce.xml'))
tree = ET.parse(gs_config)
root = tree.getroot()
expected = []
for elem in root.findall('resourcetypes/linkable/element'):
expected.append(elem.get('value'))

 

Failing broken test

Recently, one of our unit tests for our custom TinyMCE generic setup profile started to fail. This was remarkable as this hadn't been changed in quite some time. As it turned out the test itself was incorrect, and once fixed, it failed. So far so good - failing tests that actually test something are good! The test was failing because it detected that 'Large Plone Folder' was a linkable type. But 'Large Plone Folder' wasn't listed in our TinyMCE generic setup profile, and in fact isn't part of Plone anymore, since Plone 4.0!

Fix the test

What is it exactly that we want to compare? The list of linkable types that the TinyMCE utility returns vs. the list of linkable types specified in our tinymce.xml file.

import xml.etree.ElementTree as ET import os
def test_SOMTinyMCELinkableTypes(self):
tinymce = getUtility(ITinyMCE)
self.maxDiff = None
gs_config = os.path.abspath(
os.path.join(
os.path.dirname( __file__ ),
'..', 'profiles/default/tinymce.xml'))
tree = ET.parse(gs_config)
root = tree.getroot()
expected = []
for elem in root.findall('resourcetypes/linkable/element'):
expected.append(elem.get('value'))
# inserted as part of default TinyMCE
expected.append('Large Plone Folder')
self.assertItemsEqual(tinymce.linkable.split('\n'), tuple(expected))

 

NB:Without the

expected.append('Large Plone Folder')

the comparison fails. So TinyMCE (Products.TinyMCE-1.3.5) inserts Large Plone Folder in there by default.

Apr 09, 2014 06:35 PM

Set SELinux context for file or directory permanently

Setting the context on a file is easy. simply:

$ chcon -u system_u -t <context> <file>

Of course, this only lasts until the system does a relabel. Relabels happen when ever SELinux is updated, or a myriad of other events.

To make the change last through a relabel, if you are using the targeted enforcement, edit the file:

/etc/selinux/targeted/contexts/files/file_contexts

And add a line like:

<regexp path> <context>

For example to make all "www" directories and their sub paths in the /srv directory readable by nginx/apache:

/srv/([^/]*/)?www(/.*)? system_u:object_r:httpd_sys_content_t:s0
Nov 15, 2013 05:45 PM

Apply patch to ESXi host via SSH

  1. scp update to tmp cache location
  2. scp <update filename> root@<esxi host>:/var/tmp/cache/
  3. SSH to the ESXi host
  4. enter Maintenance mode
  5. run the esxupdate command with your filename
  6. esxupdate --bundle=/var/tmp/cache/<update filename> update
  7. Reboot the host
  8. reboot
  9. Exit maintenance mode

Your host is now be updated.  You can verify by checking the build number on the console, or the vSphere Console.

Apr 26, 2013 08:28 PM

Place ESXi into Maintenance mode through SSH

You can enter maintenance mode via SSH using the following command:

vim-cmd /hostsvc/maintenance_mode_enter
Apr 26, 2013 08:25 PM

Enable SSH on ESXi host

By default, SSH is disabled on ESXi.

  1. Launch the vSphere Client
  2. Select the host in the tree-view on the left hand side
  3. Select the “Configuration” tab
  4. Under the “Software” menu, select “Security Profile”
  5. Click the “Properties” link which will open up the “Services Properties” window (see below)
  6. Select the “Remote Tech Support (SSH)” service and then click the “Options” button
  7. Set the “Startup Policy” to “Start automatically” and click “Start”
  8. Click “OK” and “OK” again
Apr 26, 2013 08:25 PM

small javascript to truncate text on word boundaries

Javascript truncation

String.prototype.trunc = function (n){
    var toLong = this.length>n;
    var s_ = toLong ? this.substr(0,n-1) : this;
    s_ = s_.substr(0,s_.lastIndexOf(' '));
    return toLong ? s_  + '...' : s_;
};

$(".text-to-truncate").each(function(){this.innerHTML = this.innerHTML.trunc(num-of-characters)});
Mar 25, 2013 01:20 AM

See managePortletsFallback without the proper viewlet manager

<rules if-not-path="@@manage-portletsinheader @@manage-portletsabovecontent @@manage-portletsbelowcontent @@manage-portletsfooter @@manage-sitefooterportlets @@search">
<after css:theme-children="#content article" if-content='not(//*[@id="portal-column-two"]) and not(//*[@id="portal-column-one"])' >
<a class="managePortletsFallback" href="{$scheme}://{$host}{$path}/@@manage-portlets">
Manage portlets
</a>
</after>
</rules>
Feb 08, 2013 07:11 PM

Configuring Dovecot and Postfix for virtual hosting

As we enter into the rewarding world of hosting other applications, we were requested to host a mail server. In trying to set this up, I stumbled upon the following post that set me on the right path.

 

http://kingofthemongodb.blogspot.com/2013/01/postfix-dovecot-multiple-domains.html

 

Good write-up on how to do virtual email hosting without the expense of cpanel.

Jan 26, 2013 02:25 AM

Use built in theme parameters in Diazo rules

While building the rules.xml file for a project, I needed to insert the value of some of the built in theme parameters as the value for an attribute to a tag. The synax I'm used to from e.g. TAL expressions:

${context}

is a little different than what is used with a Diazo/plone.app.theming theme:

{$path}

The final rule ended up looking like this:

<after css:theme-children="#content article"
if-content='not(//*[@id="portal-column-two"]) and
not(//*[@id="portal-column-one"])' >
<a class="managePortletsFallback" href="{$scheme}://{$host}{$path}/@@manage-portlets">
Manage portlets
</a>
</after>
Dec 14, 2012 06:20 PM

Compound Conditionals in Diazo

Say for instance that you are using Twitter Bootstrap, and you want to map Plone's three column layout into a Bootstrap row. When all three columns are present, the center column should take up, for instance 6 grid columns, and the left and right columns could take three each in a 12 column grid.

Obviously, this works great if all three columns are present, but if one or both of the left and right columns are absent, you end up with a lot of wasted space. When using diazo, you can conditionally modify the class of an HTML element to change this with:

<before css:theme-children="#content article"
        css:if-content="#portal-column-two">
    <xsl:attribute name="class">span12</xsl:attribute>
</before>

Now, this works fine if you only have one possibility, in this case the presence or absence of column 1. Since we have two columns, we can say that if either one is gone, use 9 columns, if both are gone, use 12. which becomes this:

<before css:theme-children="#content article"
        if-content='not(//*[@id="portal-column-two"]) or
                    not(//*[@id="portal-column-one"])' >
    <xsl:attribute name="class">span9</xsl:attribute>
</before>
		
<before css:theme-children="#content article"
        if-content='not(//*[@id="portal-column-two"]) and
                    not(//*[@id="portal-column-one"])' >
    <xsl:attribute name="class">span12</xsl:attribute>
</before>

This allows to use the logical "or" and "and" operators to take care of both cases. Note that the order matters, the and must come after the or.

Dec 12, 2012 01:55 AM

Document Actions

Twitter