Categories
PHP

Using PhpStorm from Command Line

I know the title is not so interesting because we all know PhpStorm can be launched from the command line just using the “pstorm” command. Like this:

But I love the word “storm” more than “pstorm”, so I did this in my .zshrc:

How do we open a directory in PhpStorm? Just like the usual:

Ah, that will open the current directory in PS 🙂

But what if the command line tool was not installed properly? What if, while upgrading, the tool broke? Or if like me, you install EAPs all the time to try out new features and the command line tool can’t find the EAP version’s path? Well, in this blog post, I will share the python script that is actually installed as the “pstorm” tool. I will also tell you what to modify in case it can’t find your PhpStorm. And of course, we will see how to install it.

So first, the python script. If it’s installed on your system, you can view the content:

Or to edit it with your preferred text editor (in my case vim):

If you don’t have the script installed, here’s mine:

If you know Python, feel free to read and understand what’s actually doing. Now, this script needs to know two things:

— The path to PhpStorm directory (where the executable is)
— The preferences directory

You should be able to find the first trying to locate where PhpStorm is installed. For the second, it’s actually platform dependent. On OS X, it’s inside the “Home” > “Library” > “Preferences” > “WebIde**” directory.

The preference directories are named with a zero appended to the major version of PhpStorm. So, for PS 7.x it’s WebIde70. For 6.x, it’s WebIde60.

Just configure the two variables in the script. Then copy it to your path, in my case, I have “~/.bin” added to my PATH. So I did this:

The last line makes it executable. Now open a new terminal and try the “pstorm” command.

Categories
Python

Embed a Python REPL in your program

So you love the Django shell? The interactive prompt that has your Django stuff loaded and ready for some quick prototyping? You always wanted to embed something similar in your Python apps too? Well, it’s painlessly simple with the “code” module. It offers us the “InteractiveConsole” class which we can extend to quickly get a REPL which is basically the Python REPL with our customizations. The code is heavily documented. Going through it should be adequate to understand whats going on:

What did we do? We subclassed InteractiveConsole to create our own implementation. And then ran it 🙂

Test run outputs:

Categories
Python

Django: Sending HTML Only Email

Prior to Django 1.7 (currently in dev), we don’t have any option to pass html version of an email to “send_mail()” from django.core.mail module. Even in the newer version, you can pass html template as an alternative but you have to pass a plain text version as well. What if you want to send html version, only? This is what I did –

Now you would pass html as the message parameter. I use django templates with “render_to_string()” function from django.template.loader module.

Do you know any other, perhaps smarter way to accomplish this? Please share in the comments! 🙂