Saturday 23 November 2013

Using LaTeX and Kile: .gls file does not exist and how to build one

Recently while working on a LaTeX document in Kile, the KDE LaTeX environment, I found myself working on a glossary that failed on the last step. I had set up a glossary .tex file that I included, put the \usepackage[toc]{glossaries}, /makeglossary and /printglossaries lines into the file and tested one of the glossary entries.

No go.

Kile could not find the .gls file. After some navigating the internets for answers, I came upon this blog post.

http://texblog.org/2007/11/01/glossary-in-latex/

After running the command, it did indeed generate the .gls file I needed. But would it regenerate every time my glossary changed? I kept looking for an answer specific to Kile. After a while, I did find it, but it didn't seem to finish what I needed. Kile has a way for the user to create their own build commands which can be run from inside the application. I'll spare you the details, but here's how to integrate glossary building into Kile:


  1. Go to Settings -> Configure Kile ... -> Tools > Build menu (found on the left-hand side of the Settings window)
  2. Under the Select a Tool pane, click the New button to create new tool.
  3. Name the tool "MakeGlossary", click Next button and Select MakeIndex from the list.
  4. ( Here is where the original post I was looking at dropped off... )
  5. In the General tab, for Command: write "makeindex".
  6. Under Command:, type this into the options:
    '%S.glo' -s '%S.ist' -t '%S.glg' -o '%S.glx'

    - Recognize it from the blog post?
  7. Just to be sure and thorough, click on Advanced Tab. For Source extension, write "glo". For Target extension, write "gls".


I have tested the above and it works for me. It does involve two steps (running MakeGlossary and then PDFLaTeX), though it could be possible to string the two commands together somehow. For now, I have what I want and I hope this blog post helps those looking for those same answers.



Cheers.

Thursday 3 October 2013

Including Dbus in a Python QT4 application

Dbus isn't the most complicated beast in software, but it's notable.

There are a list of things I *don't* like about dbus.

- The C/C++ code is well documented, but good tutorials are absent
- The Python code is poorly documented if it exists, but there are some great tutorials to look off of
- Python Dbus + Qt or GTK requires that the process is forked or put into a daemon in order to allow connections. (Cannot initiate in a GUI application without detachment)

I just spent an hour figuring out that last one. I'm big on documentation, both in my work and when using other people's code. If it isn't there, the difficulties in developing with the software is exponentially greater and my time is wasted.

That said, dbus is amazing when it works and I love that KDE and QT/Maemo have taken great lengths to adopt it. Even the KDEOnWindows project uses dbus, on Windows!



I am trying to create a dbus interface so that my GUI application can talk to different portions of itself without messy imports and passing of variables. I will have a dbus/ folder that contains one module with one class that inherits dbus services and imports respective modules. Each method will pass the call and any arguments it includes to the function, which in turn passes it to the UI.

Why have dbus classes and modules in one folder? So I can iterate through the folder and add every dbus service without having to manually type it all in; automate it! (It's inspired by the debian and gentoo directory.d/ style of configuration management. I plan to extend this to a plugin system at some point.) Make sure all dbus files are referenced before the mainloop but after application is started.

It seems that walking through a directory and dynamically loading dbus objects still causes dbus to hang. My guess is that every dbus.service object needs to be placed into its own mainloop and importing a module with dbus methods doesn't properly place it into the mainloop.

Sorry for the mess. Dbus services must be added after the QApplication or QCoreApplication is initialized. I was adding them before, then getting this message in the terminal:

QSocketNotifier: Can only be used with threads started with QThread

Create references to dbus objects after application initialization!

For a real-world example, I will launch a dbus service that contains a method for creating a new project. I will name it NewProject and it will have an argument of a name. We will not worry about uniqueness or purpose for now.

------------------------------------------------------------

Import Code:

import dbus
import dbus.service
from dbus.mainloop.qt import DBusQtMainLoop

If this fails, you need to install dbus dependencies. This could be python3-dbus.mainloop.qt, python3-dbus/python-dbus, or python3-pyqt4. You will need to check with your distribution for the proper package names. A search for "dbus" is usually adequate.

Define an Interface Name:

This should reflect your application or purpose. I usually store it as a global variable.

INTERFACE = 'org.qtcide'

Write your class with the dbus methods
class DbusTest(dbus.service.Object):

    def __init__(self):
        busName = dbus.service.BusName(INTERFACE, bus = dbus.SessionBus())
        dbus.service.Object.__init__(self, busName, '/org/qtcide')
        

    @dbus.service.method(INTERFACE,
                        in_signature = 's', out_signature = 's')
    def NewProject(self, name):
        return name

Initialize application and mainloop:
DBusQtMainLoop(set_as_default = True)    
app = QtGui.QApplication(sys.argv)

Keep it looping:
sys.exit(app.exec_())


And you should have a dbus process running. Currently the NewProject method only returns the name you give it, but adding an import and calling a method with that Name is simple, and I shall explain how in the next post.

Forking may have some strange consequences on Windows, as true forking is resource intensive and not native.


For those of you interested, you can find my project I'm basing this tutorial on here: https://github.com/NucleaPeon/QTCIDE

Friday 13 September 2013

How to install your own python modules

Problem: You have python code, such as a library, that you want available for other programs you, or others, may be developing. You also may want to add your code into a debian or rpm package for easy distribution.


Solution: Add a [name].pth file and your package to /usr/local/lib/pythonX.Y/dist-packages.


Important Notes:
  • site-packages is no longer used. Read the site.py module for more information on this.
  • The [name].pth contains a relative or absolute path to the directory or specific python files you want added to the interpreter, each item on its own line. (Comments prefixed with # are also recognized)

So I have this example:

/usr/local/lib/python3.2/dist-packages/mylib.pth
    # This file contains path to my library (relative):
    # Adds every .py file in mylib/ folder
    mylib/
    mylib/static/mylibvars.py



The path /usr/local/lib/python3.2/dist-packages/mylib/ contains library files and /usr/local/lib/python3.2/dist-packages/mylib/static/mylibvars.py is just to show how to specify an individual file.


Once you open up your interpreter, type in the following commands to check your work.


>>> import sys
>>> print(sys.path)

Friday 2 August 2013

Quickly changing python version in gentoo from 2.x to 3.x series

On my gentoo laptop I am upgrading my python from version 2.7 to 3.2 to aid in some development at my job.

Since it's been set up with 2.7, I cannot utilize some important libraries such as python-sqlite because they are not built for the 3.2 target.

Without going into the more complex explanation on why it works, these are the steps to configure your system to utilize python 3.x.

/etc/portage/make.conf:
USE_PYTHON="3.2 3.3 2.7"
PYTHON_TARGETS="python3_2 python3_3"
PYTHON_SINGLE_TARGET="python3_2"
Simple Explanation:

USE_PYTHON tells emerge the list of desired python versions.

PYTHON_TARGETS tells emerge what targets you prefer to build for. Only if a package cannot build for these targets, yet has a version in USE_PYTHON, will it build in a different version -- I *think*.

PYTHON_SINGLE_TARGET tells emerge that if a package can be built for only one target, use that target. My install of Blender requires the single target point to python3_3.

Be sure to set the python version with eselect.

eselect python set [number]

Next step is to update things by calling python-update, no parameters.
Once your eselect python show --ABI shows the 3.x version you specify in PYTHON_SINGLE_TARGET, you can start rebuilding your system.

This process may take a while. All packages that cannot be built in the 3 series must have the python version specified in /etc/portage/package.use. An example of how to do this is so:

echo "[group]/[package] python_targets_python2_[x] python_single_target_python2_[x]" >> /etc/portage/package.use

Do this until you can get through emerge --deep --update --newuse world -av without dependency issues.

Once you complete the emerge, run revdep-rebuild to make sure your packages are in order.

Friday 19 July 2013

Creating an Android Studio Desktop File for launching on your panel or toolbar in GNU/Linux

I spent a while (read: too much time) trying to get Android Studio launching on my KDE panel via an icon.

The program would not launch, even after editing the studio.sh script so the user wouldn't have to press the key. Since I compiled and installed IcedTea Java 7, it would always warn me before launch. Oracle Java is recommended for running Android Studio, but I take some issue with Oracle as a company, so I would prefer not to use it. Plus, on Gentoo, the package manager requires the manual downloading of oracle's java.bin files because they do not allow Gentoo to carry it in their package repository due to copyright or some crap like that.

Anyway, back to the application launching: even when exporting JAVA_HOME in my .bashrc file (which gets run every login shell), the application would fail to run. The fix for this is to manually add the path to the EXEC= line in the .desktop file. Here is mine as an example:

[Desktop Entry]
Comment=
Exec=JDK_HOME="/usr/lib/jvm/icedtea-7/" /home/celestia/Programs/android-studio/bin/studio.sh
Icon=/home/celestia/Pictures/Icons/android.png
Name=AndroidStudio
NoDisplay=false
Path[$e]=
StartupNotify=true
Terminal=0
TerminalOptions=
Type=Application
X-KDE-SubstituteUID=false
X-KDE-Username=

I used the KDE Menu Editor to generate the template, but regardless of how you create your .desktop file, it's the EXEC line you need.
If your paths are set up system-wide, I doubt there will be any errors when you run studio.sh without the JDK_HOME path.

Hope this helps.
Cheers!

Tuesday 9 July 2013

Speedway Revolution 420 RFID Tag Reader in Python

So I recently was working on connecting a piece of software to a reader via a socket.

The vendors had a great script for connecting to it via ruby, as shown in the code below:

require 'socket'

s = TCPSocket.new '192.168.2.165', 14150

while line = s.gets
    puts line.chop
end

s.close


This code is available, you can see it here in the video:
http://learn.impinj.com/articles/en_US/RFID/Reading-Tags-over-TCP-IP-Socket-Using-Speedway-Connect-Software/

The site presents .NET code for connecting as well. However, as I do most of my work in python, I figured I'd keep things simple and convert their script to it.

---

The reason I'm writing this post is because I had one hell of a time getting to the point where it *would* read, because it would keep failing on the connect() method and from then on, I would have a Connection Refused error whenever I ran the python OR the ruby script. If you are having troubles with the Speedway Revolution 420 reader, know this:

* If you get Connection Refused errors, you need to browse to the ReaderConnect software, found at the reader ip address on port 8080. Click the save button, even if you haven't made any changes. This will allow connections to be received again, as I imagine it closes whatever broken connection python has left behind.
* You must use python 2.7 (or 2.x series, though I haven't tested beyond that) in order for it to run. When my code was failing, the header was "#!/usr/bin/env python", which on my linux mint system, would default to python 3. It was only until I *explicitly* stated "#!/usr/bin/env python2.7" that it miraculously worked. Here is the code:

#!/usr/bin/env python2.7

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("192.168.2.165", 14150))

data = s.recv(1024)
data = data.strip('\n')
while 1:
    print data
    data = s.recv(1024)
    data = data.strip('\n')
    
s.shutdown(socket.SHUT_RD)
s.close()


It makes two assumptions:

1. You want to read 1024 bytes. 0 does NOT work, it leaves me with an empty screen because the data variable does not get assigned to anything more than an empty string.
2. If you set the output to Linefeed, or Carriage Return Linefeed, this script will work. If you don't set the line ending value in the Output tab, there could be some issues.

Hopefully this script can help someone, someday.

Friday 7 June 2013

Multiple hosted websites are redirecting to only one of my sites on Apache

I had an interesting problem today.

I am attempting to host 3 different web sites on an apache configuration on a raspberry pi. Individually, I can launch each webpage without issue and have the site show up. However, somehow, when I enabled more than one site at a time, each one on different ports, it would show only one site across all ports.

I tried configuring different sites with different ports, changed <Directory> tags to see if my directories were overriding each other, etc.

Most of the data is stored in /var/www/[site-name], so it was only until I tried adding another site at /var/www/[different-site-name] and have it still happen that I knew it was not a path conflict. I fudged with the apache settings for a while to no effect.

A little voice in my head hinted at one of the configuration files I had used a long time ago, the /etc/apache2/conf.d/virtual.conf file. I checked it out, and it had this to say:


NameVirtualServer *


Now, in my website ports.conf file, I specified lines like this:
NameVirtualServer *:80
NameVirtualServer *:8081
NameVirtualServer *:8082

Somehow it clicked. I disabled the /etc/apache2/conf.d/virtual.conf contents by changing the line to this:

# NameVirtualServer *

saved and restarted apache.

Now everything works as configured.

It may seem like a trivial problem, but it cost me a couple hours of my valuable time and I *could not* find a solution on google. Granted, I didn't scour the bowels of the internet for this small problem.

Hopefully this will help others in my position.

(Performed on a Raspbian installation)

Thursday 25 April 2013

Ubuntu 13.04: First Impressions - fails to install

After going through the livecd process which was smooth and uneventful, when it came to the reboot back into the installed system, all I got was a blinking cursor on a black screen.

This is caused by Grub not being installed properly, for whatever reason.

The solution was for me to mount my filesystem (/dev/sda1 in my case) and use the "grub-install --root-directory=/mount/point/ /dev/sda" command. If I didn't add the --root-directory option, it would fail.

Rebooting again allowed me to log in.

Not impressed Canonical. I'll keep going with this OS for a bit, but when it comes to reliability and usefulness, I stick with Gentoo.

Update:

The problem is with one of the raid packages, as this also occurs on Debian 7 in the same situation. It's not an Ubuntu-specific problem, but upstream. Still, with such a blocker for an install, I don't see how it could have crept through TWO different distribution QA phases.

Wednesday 27 March 2013

Django: Common Errors, variables not appearing

While it's against the django mindset, I have done a few copy-pastes in my time using this framework.

Recently I've been doing some copy-pasting of some modules and found that often I was getting blank lines when retrieving variables.

The problem was that my text editor was fudging with the tab order of some of the lines-- a huge problem in python-based programs. My Meta class was tabbed forward so it was part of the method, not the class. By de-tabbing it, I was able to see my variables as expected.

Sometimes it's the small things.
If such a scenario exists, a better idea that goes along with the DRY principle would be to create one base Form class with the Meta class and have all other classes inherit it appropriately.

Tuesday 19 March 2013

Django: Python Unchained

So I've been doing a lot of work with django lately (https://www.djangoproject.com/) and it has been an excellent MVC so far. Python in particular is very powerful and I've had 3+ years of experience with it, as opposed to CakePHP and Ruby on Rails (PHP/Ruby).

However, MVC's are not without their difficulties, even though they do save time and effort. Documentation has always been the #1 issue when using other people's software, and to this day there have only been a couple bits of software I've used with proper docs. Python and KDE are two big names at the top of my head.

I experienced one such django issue for which there wasn't a clear answer to what I was experiencing, and with the interests of newbies in mind, I will document some of those weird django errors in this blog.


Problem: Django is not displaying a view and gives 404 errors, doesn't report URLs that are defined in the urls.py file.

Solution: Every definition of the urlpatterns variable must include a blank string at the start of the tuple parameter you supply.

Wrong:
urlpatterns = patterns(url(r'^index/', index.index),
)

Right: 
urlpatterns = patterns('',
    url(r'^index/', index.index),
)

In hindsight, it would be a simple fix if I knew what pattern() required. I hope this helps at least one other person.

Cheers.


p.s. I *would* like to format code snippets properly, but apparently Blogger is incapable of doing so without some css/javascript tweaking. =/