Gunbot 7.0.2 and Zeno GUI 1.31 setup on Ubuntu 17.10

TL;DR

In this post I will explain step by step how to install and setup Gunbot 7.0.2 and Zeno GUI 1.31 on Ubuntu 17.10. It also shows how to access the Gunbot GUI and Zeno GUI through a SSH tunnel using PuTTY with port forwarding for enhanced security. A no thrills quick and dirty write-up.

Continue reading

Setting up Mutt to send mail using Gmail with 2FA set

TL;DR

I needed to be able to send mail from the command line on one of my servers. A quick way to do this without setting up a mail server is using Mutt to send mail using Gmail. In this post I will give a short write-up how to install and configure Mutt on Ubuntu. As I have two factory authentication setup on Google an app specific password is required instead of the regular Google account password.

Continue reading

Surface Pro 4 shutsdown when I click the on/off button

When I pressed the on/off on my Surface Pro 4 running Windows 10 it would shut down! Really irritating.

After searching around for a while I found the setting under System Settings that controls the behavior of the on/off button.

  • Open Control Panel > Power Options > System Settings
  • Change the action when the on/off button is pressed to ‘Sleep’.

That’s it. Another problem solved!

Playing with W3C Annotation AKA Web Annotation

Today I was playing around with W3C Annotation AKA Web Annotation. I needed to convert the JSON-LD examples to RDF XML and test XPath expressions. I justed a couple of online tools. Just some quick notes about these tools.

  • Convert JSON-LD to RDF XML: Select JSON-LD as input format and Pretty RDF/XML as output format.
  • Test XPath expressions: Copy and past the XML into the input field. Type the XPath into the XPath input field. Hit Test XPath button and you should see the result.
  • XML Formatter: A XML formatter that lets you specify the number of leading spaces.

You can find the latest information about Web Annotation over here and example code on Github.

Adding a ssh public to Gitlab

I needed to add my ssh public key to Gitlab. Pretty straight forward generating it using PuttyGen and saving the public and private keys. When I pasted they public key into the dialog in Gitlab and hit “Add key” I got the following error.

The form contains the following errors:

  • Key is invalid
  • Key should be a single line
  • Fingerprint has already been taken
  • Fingerprint cannot be generated

I checked the public key and saw that it was not a single line. To correct this I did the following.

  • Deleted the “—- BEGIN SSH2 PUBLIC KEY —-” line
  • Deleted the “—- END SSH2 PUBLIC KEY —-” line
  • Deleted the “Comment: …” line
  • Removed all line ends from the generated key
  • Added “ssh-rsa ” before the key
  • Make sure there is an empty line after the key

That’s it.

Flask application factory pattern and testing

This is a project I setup to show the use the application factory pattern in Flask. I set it up because I was running was stuck while refactoring one of my projects to use the factory pattern. The code would run normally, but my test cases where failing with the following two errors:

RuntimeError: application not registered on db instance and no application bound to current context
RuntimeError: working outside of application context

Most Flask examples don’t use the factory pattern so I spent a lot of time searching around to solve the problem. So I thought I would work it out and share it. Hopefully it saves someone else time.

The problem

Once your project starts to grow, code organization is everything. Flask provides a number of mechanisms for code organization. One of these mechanism’s is blueprints. Combined with the factory pattern provides a nice way to structure and organise code.

Another problem that the factory pattern helps solve is circular dependencies.

Getting the factory pattern to work isn’t hard. Getting it to work correctly, it turned out, was a little harder. The problem I had was caused in the testing code. In the following section I will briefly explain how to setup and use the factory pattern correctly.

Lessons learned

I have added more code than strictly necessary to show the concept of the factory pattern working as a realistic example.

The structure and contents of this example project is:

src
│ .gitignore
│ readme.md
│ manage.py
│ requirements.txt
├── instance
│   sensitive.cfg
├── test_app_factory
│   │ __init__.py
│   │   appliction.py
│   │   config.py
│   │   extensions.py
│   │   models.py
│   ├── helpers
│   │   __init__.py
│   │   misc.py
│   ├── module
│   │   __init__.py
│   │   viws.py
│   ├── static
│   │   favicon-16x16.png
│   │   favicon-32x32.png
│   └── templates
│   index.html
└── tests
    test_basics.py

Okay, what’s important to point out here?

The core of the factory pattern is setup in application.py and extensions.py. All extensions are initialized in extensions.py. If you add additional extensions make sure to add them to the import statement in test_app_factory/__init__.py. This is a convent way to shorten import statements.

The actual heavy lifting is done in application.py. Each part of the application initialization is a separate function, which are called by the main function app_factory. This function takes a string, which specifies the environment the configuration should be loaded for. The configuration is defined in config.py.

The factory pattern in application.py looks like this:

def app_factory(config, name):
app = Flask(...)

...

return app

The function calls a number of functions that load the configuration settings, extensions, blueprints, etc.

Using the factory is really easy, just use the following call:

app = app_factory('TST')

To access the app object in modules after the application has been initialized is done using the proxy provided by Flask:

from flask import current_app as app

Now for the part that was driving met crazy, the testing code. I still do not understand fully why it is the only place in my code that was causing a problem, probably has to do with the way unittest works. Anyway, to get the factory pattern to work you need to add app_context to specific statements. Here is an example.

class TestCase(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.app = app_factory('TST')

    def setUp(self):
        with self.app.app_context():
            self.client = app.test_client()
            db.create_all()

    def tearDown(self):
        with self.app.app_context():
            db.session.remove()
            db.drop_all()

    def test_add_user(self):
        with self.app.app_context():
            db.session.add(User(name='test user', email='[email protected]'))
            db.session.commit()

Conclusion

Finding good examples isn’t always easy. The factory pattern can really help to organize the code and make it more readable and maintainable.

Any suggestions how I can further improve the code? I would love to hear from you!

Inspecting and replaying requests with ngrok

A very cool feature of ngrok is the web interface. With this interface you can inspect requests and replay them. Very handy for debugging purposes.

The web interface can be reached on http://localhost:4040.

In the web interface you can view both the request received and response sent. The default view of the request and response is a summary of the post data, but it can be viewed in raw format or binary format. This is really useful when debugging webhooks.

Another great feature of this web interface is the replay functionality. Instead of having to run through the code again to get to the problem, you can just hit the resend button and the request will be resent.