Friday, 22 February 2013

Erlang, record and Erlsom with XSD for XML goodness

Below is a simple example which should get you up and running quickly with Erlsom. Grab a copy of Erlsom from Willemdj's github if you need to.

This is our XML, note.xml:

  Tove
  Jani
  Reminder
  Don't forget me this weekend!

With that, head over to xmlforasp.net to get our XSD file. Copy/paste note.xml and select "Separate Complex Types" which worked for me.

This is what our XSD should look like, note.xsd:
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="note" type="noteType" />
  <xsd:complexType name="noteType">
    <xsd:sequence>
      <xsd:element name="to" type="xsd:string" />
      <xsd:element name="from" type="xsd:string" />
      <xsd:element name="heading" type="xsd:string" />
      <xsd:element name="mbody" type="xsd:string" />
    </xsd:sequence>
  </xsd:complexType>
</xsd:schema>

Watch out for the encoding bit as xmlforasp.net will give you utf-16 and Erlsom supports only utf-8.

Next from Erlang CLI, we'll create our hrl file:
erlsom:write_xsd_hrl_file("note.xsd","note.hrl").
Which should generate the note.hrl file and look like this:
-record('noteType', {anyAttribs, 'to', 'from', 'heading', 'mbody'}).
Let's load the note.hrl file:
rr("note.hrl").
Load the note.xml file:
{ok, Xml} = file:read_file("note.xml"). 
[noteType]

Compile the note.xsd file:
{ok, Model} = erlsom:compile_xsd_file("note.xsd").
{ok,{model,[{type,'_document',sequence.............

And save the xml variables to our record:
{ok, Note = #noteType{}, _} = erlsom:scan(Xml, Model).
{ok,{model,[{type,'_document',sequence..............

Check the record:
Note.
#noteType{anyAttribs = [],to = "Tove",from = "Jani"......

Check again:
Note#noteType.from.
"Jani"
If you're building an OTP app and following OTP convention then, note.xml and note.xsd should go in the "priv" folder in your project root folder. You can then call the file like this:
Load priv_dir (replace 'note' with your project name) -
privdir() -> code:priv_dir(note).
Load XSD file names
note_xsd() -> filename:join([privdir(), "note.xsd"]).
If you get stuck, post your questions here or on Google+ and I'll try to help you out.

Wednesday, 30 January 2013

Apple MBP three screen setup

I just received the USB UGA-2K-A DisplayLink adaptor and it's working great! I'm seriously impressed by the no-hassle-plug-n-play. That's the good thing about having lowered expectations, you're pleasantly surprised when it does work.

I used the latest alpha 2.0 version drivers available from DisplayLink's site.

Sweet!
I've got the old mini-DVI-to-VGA plugged into one monitor, the DisplayLink adaptor into the other one and the MBP screen working too.

Friday, 14 December 2012

ITU and their lost battle to charge YouTube

ITU's Final acts of the World Conference on International Telecommunications, Dubai 2012, is out. 

Besides begging for money and trying to stay relevant, the ITU ratified 'Deep Packet Inspection' (DPI). They also managed to insert this gem: "optionally requires DPI systems to support inspection of encrypted traffic “in case of a local availability of the used encryption key(s).” [1]

What that means is that should your ISP magically come into possession of say your private keys, they are allowed to decrypt your email, communications etc. that you used to encrypt it, forever without telling you.

Their shameless money grab was left for last, on page 23 of the Final Acts titled 'International telecommunication service traffic termination and exchange'. It recognises that dedicated phone and data has moved to IP based networks and many Member States are asking for their authorised operating agencies to invoice service providers. In other words, many Governments are asking, on behalf of their state-run-money-losing telecos, to invoice YouTube for sending traffic to their users. 

The sooner these governments accept the fact that state run telcos are worse than state run airlines, the sooner they can stop begging YouTube for money and implement an open platform for all telecommunication firms to flourish.

Source:
[1] https://www.cdt.org/blogs/cdt/2811adoption-traffic-sniffing-standard-fans-wcit-flames

Monday, 1 October 2012

Flask, MySQL, WTForms & SQLAlchemy that works

Quick 'n dirty Flask, WTForms & SQLAlchemy that works.

database.py
#!/usr/bin/env python
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
import sqlalchemy.interfaces

# Without this, MySQL will silently insert invalid values in the
# database, causing very long debugging sessions in the long run
# source: http://www.enricozini.org/2012/tips/sa-sqlmode-traditional/
class DontBeSilly(sqlalchemy.interfaces.PoolListener):
    def connect(self, dbapi_con, connection_record):
        cur = dbapi_con.cursor()
        cur.execute("SET SESSION sql_mode='TRADITIONAL'")
        cur = None

engine = create_engine('mysql+mysqldb://root:banana@127.0.0.1/testdb?charset=utf8&use_unicode=0', convert_unicode=True, listeners=[DontBeSilly()], echo=True)
db_session = scoped_session(sessionmaker(autocommit=False,autoflush=False,bind=engine))
Base = declarative_base()
Base.query = db_session.query_property()
Base.metadata.create_all(bind=engine)
models.py
#!/usr/bin/env python
from sqlalchemy import Column, Integer, String
from flask_application.database import Base
  
class User(Base):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True)
    username = Column(String)
    password = Column(String)
    email = Column(String, unique=True)
    country_id = Column(Integer)

    def __init__(self, username=None, password=None, email=None, country_id):
        self.username = username
        self.password = password
        self.email = email
        self.country_id = country_id

    def __repr__(self):
        return '' % (self.username, self.password, self.email, self.country_id)
forms.py
#!/usr/bin/env python
from flask.ext.wtf import Form, TextField, PasswordField, BooleanField, SelectField
from flask.ext.wtf import Required, Email, EqualTo
    
class user_add_form(Form):
    username = TextField('Username', [Required()])
    password = PasswordField('Password', [Required()])
    confirm = PasswordField('Repeat Password', [Required(), EqualTo('password', message='Passwords must match')])
    email = TextField('Email')
    country_id = SelectField(u'Country', choices=[('1', 'UK - United Kingdom')])
template_add_user.html
{% block content %}

{{ form.username }} {{ form.username.label }}

{{ form.password }} {{ form.password.label }}

{{ form.confirm }} {{ form.confirm.label }}

{{ form.country_id }} {{ form.country_id.label }}

{% endblock content %}
view.py
from flask import Blueprint, request, render_template, flash, g, session, redirect, url_for
from flask_application import app
from flask_application.database import db_session
from flask_application.models import User
from flask_application.forms import user_add_form
from werkzeug.security import generate_password_hash, check_password_hash   

@frontend.route('/users/add', methods=['GET','POST'])
def users_add():
    form = manage_user_add_form(request.form, csrf_enabled=False)
    if form.validate_on_submit():
        new_user = User(
                    form.username.data,
                    generate_password_hash(form.password.data),
                    form.email.data,
                    form.country_id.data
                    )
        db_session.add(new_user)
        db_session.commit()
        
    return render_template('template_add_user.html', form=form)

Monday, 24 September 2012

iPhone Test Field Mode, Serving PLMN Info and Cell ID.

I'm working on a project and needed to validate the Location Area Code and Cell ID in my tests with actual info from my mobile phone. Using an iPhone and going into Test Field Mode did the trick.

Go into Test Field Mode:

  • Dial *3001#12345#*
  • MM Info -> Serving PLMN - Gives you MCC, MNC, RAC and LAC
  • UMTS Cell Environment -> UMTS RR Info - Gives you Cell ID
There's much more information there and it's very handy since you don't need to muck about in an app.

Thursday, 29 March 2012

Android land at Mobile World Congress 2012

A little late but here are some shots from Android land at Mobile World Congress 2012.




An android making little Android statues.



Club Android.



Wednesday, 29 February 2012

MWC: Huawei pretty booth

Huawei has got a very impressive boot here at MWC and some very neat products, but I didn't have much time as I was just passing by on my way to see Android's booth!