All sorts of nitty-gritty technical things.

cURL – File Upload with Form Post on the Commandline

Q: How can use cURL to emulate a form post with a file upload?
A: It took me a bit of time to figure this out but it is quite straight forward really, you just need to use (possibly multiple) --form (or -F) arguments like this:

 
curl -F appID=1234 -F name=Emulate -F [email protected] http://www.abc.com/na
 

This will emulate a form post to

http://www.abc.com/na

with two named data items (appID and name) and one file upload, the file upload will be named ‘appFile’, the local file em.zip will be uploaded with the post, and that should do it!

WordPress – Can’t Approve Comments

If you’re using wordpress and all of a sudden you can’t approve comments, then try deactivating the ‘Cimy Swift SMTP’ module – It worked for us!

 

Python – Get Name of Current Function

Q: How do I get the name of the current python function, I want to output its name as part of some logging information.

 

A: Just import the ‘inspect’ module and call its stack() method to get access to the call stack, for example, to get the name of the current function:

 

[crayon]
import inspect

fn_name = inspect.stack()[0][3]
[/crayon]

 

The first index indicates the stack position, so to get the name of the parent calling function use:

 

[crayon]
import inspect

fn_name = inspect.stack()[1][3]
[/crayon]

 

or to define a function that returns the name of the function from which it was called:

[crayon]
import inspect

def get_current_function():
return inspect.stack()[1][3]
[/crayon]

 

Introspection is one of the really cool features of modern software programming languages!

Microsoft C# Coding Style Guidelines & Visual Studio

These days most software developers need deal with more than one software development technology and programming language on a day to day basis. This often means that they have to switch back and forth between coding styles, with this in mind I noticed that although the MS coding guidelines for C# in .NET recommend K&R style braces for code blocks, e.g:

 


if (sneeze) {
  nose.Blow();
}

 

By default Visual Studio’s auto code formatting stuff will format your code with braces on newlines, e.g.:

 


if (sneeze)
{
  nose.Blow();
}

 

Very interesting indeed, I wonder are there internal coding style conflicts within Microsoft…

Magento – jQuery clash with testimonials, prototype.js & showcase

If you use testimonials on magento and then place them onto a page that contains the prototype extension showcase, you may notice that both the testimonial and showcase animation stops working, and that an absolute snow of javascript errors are generated.

 

If this is happens to you, it may be caused by a jQuery library conflict. The testimonials PHP template directly includes jQuery and this may clash with the prototype library, we found that we could resolve the library conflict by placing good old:

 


jQuery.noConflict();

 

In the file testimonial_advance.phtml just before document/ready, like this…

 

[code]

jQuery.noConflict();

 

jQuery(document).ready(function() {
….
}

[/code]

 

Adding this solved the problem for us.

 

testimonial_advance.phtml should be found in this folder:

 

app/design/frontend/default/default/template/testimonial

 

However, it is not good magento practice to alter this version of the file, instead you should create a testimonials folder under:

 

app/design/frontend/default/[your folder]/template/

 

Then copy testimonial_advance.phtml to this folder and edit it there. Remember to clear the magento cache when testing changes!

SQL Express 2008 – Can’t login using SQL Authentication

In the Software Development game there are always plenty of annoying problems that will just eat up your time, what I really hate is being caught out by the same annoying problem more than once… I have been caught out by this one before so this time I am going to write it down!

 

So the situation is that you just can’t login to Microsoft SQL Express 2008 via the Management Studio or by any other means using the ‘SQL Authentication’ method. You can login using ‘Windows Authentication’ fine, and can then add SQL users and change their passwords and configure their permissions etc. etc. but you can never login with these users…

 

If you are experiencing the above the most likely cause that ‘SQL Authentication’ isn’t enabled at all! And Management Studio doesn’t bother to remind you of this when it lets you add, configure and enable such logins.

 

Well, to check if ‘SQL Authentication’ is enabled, and to enable it if it’s not try the following:

 

Login into Management Studio, right click on your server and choose ‘properties’. Click on the ‘Security’ tab, make sure that the ‘SQL Server and Windows Authentication Mode’ radio button is checked and hit OK.

 

Hopefully all of your SQL logins will work now, ours did!

 

If after you have done this, you still can’t login as ‘sa’, edit the users’s properties and under ‘Status’ tab, make sure that the ‘Login:’ radio button is set to ‘Enabled’ – It may have been disable during installation.

Current Directory / folder of a Batch file run As Administrator

It is a very lucky software developer that doesn’t have to interact with a microsoft windows batch file (.bat) ever now and again, recently my luck ran out and I came across a funny problem:

 

In windows 7, if your batch file does anything interesting (such a registering a COM dll) you may have to run it ‘As Administrator’. When you run it as administrator you may notice that a strange thing happens, it may be run with a different current/working folder to the folder in which it resides – whereas when run normally its current folder is its local folder.

 

This can be a real problem if the batch file needs to reference other files in its folder (such as the COM dll to be registered in my example above…)

 

Well not to worry, it turns out that to change batch file’s current directory to its local directory we just issue this rather esoteric command at the top of the file:

 

pushd %~dp0

 

The pushd bit I get, as for the rest….. well I am just glad it works!

IE8 / .change() jQuery event not firing

This one catches me out time after time and I always forget how to get around it – so I am writing it down this time. Developing software to run well on multiple browsers is actually not that difficult until, that is, good old IE is thrown into the mix! ;-)

 

The problem is that in Internet Explorer, the .change() event on radio buttons doesn’t get fired properly, or at least it doesn’t behave the same was as it does in the other browsers – it’s a real pain!

 

There is however a relatively easy way to get around the problem, that is to handle the radio’s .click() event, and then, just .blur() and .focus() the radio control – this will cause the change() event to be fired:

 
$(document).ready(function() {
  $('#my-radio-container input:radio').click(function () {
        // Cause the change() event
        // to be fired in IE8 et. al.

        this.blur();

        this.focus();
   });
  $('#my-radio-container input:radio').change(function() {

    // Handle .change() event as normal....

  });

});

 

Remember to replace #my-radio-container with an appropriate selector for your HTML.

 

I am not sure why it works exactly….. but it works well for me!

Scripting GPG – “It is NOT certain that the key belongs to the person named…”

GPG is great software for manually encrypting data using the OpenPGP standard – good encryption tools are more and more becoming essential to software development.

 

However using GPG from a script sometimes presents problems. Why would you want to GPG from a script? Well you might want to encrypt a backup from within your backup script to allow you to safely FTP the backup file to remote storage, for example.

 

Anyway, one such problem results in the following error message appearing, when you try to encrypt using an imported, but unsigned public key:

 

“It is NOT certain that the key belongs to the person named…”

 

GPG then says something along the lines of ‘do you want to continue anyway?’ and tries to read your response from the terminal – the script is unable to answer the question and so the whole process stalls…

 

One way to get around this problem is to manually set the trust level of the imported public key to ‘ultimate’ so that the situation no longer arises, instructions for how to do this can be found here:

 

http://www.somacon.com/p107.php

 

However in some cases this may not be easy or desirable, especially if you don’t have access to the command line – for example, you may be working with shared hosting. You may also want to ensure that the _whole_ process is automatic with no manual steps required.

 

So, another option is to pass the ‘–trust-model always’ argument to gpg, this will force GPG not to issue the warning and not ask for confirmation – so your script should proceed OK. For example:

 

[code]
gpg –encrypt –yes –no-tty –trust-model always –output backup.gpg –recipient [email protected] backup
[/code]

 

Older versions of GPG may not have this parameter, and will report an error when you pass it, in this case the depreciated ‘–always-trust’ argument can be used instead.

 

Be warned that this does reduce security slightly. If somebody somehow maliciously managed to substitute your public key for theirs and then managed to intercept the encrypted data, then they could decrypt it with their private key. However in most cases this is of small concern – but do be aware of the possible dangers of blindly encrypting without requiring signed public keys.

 

Script & Encrypt!

Google App Engine extends time limit for tasks, No more DeadlineExceededError ?

Is DeadlineExceededError a thing of the past?

 

The new version (1.4.0) of the google app engine was announced yesterday, details can be found here:

 

http://googleappengine.blogspot.com/

 

There are some interesting changes and additions, however the one change that really caught my attention is that the time limit for tasks has been increased from 15 seconds to 10 minutes!!

 

The 15 second limit was a real pain when trying to do any sort of even light to medium weight background processing in the GAE – any task that ran to 15s would be killed and the very annoyong DeadlineExceededError would be reported. This sometimes made life really difficult for software developers, forcing them to restructure their code into multiple smaller chained tasks etc. So this is big news!

 

I have run some quick tests this morning and have happily run background tasks for many minutes, so things are looking good so far!