Switching to i3wm from Gnome

i3wm configuration

If you like the tiling window manager like i3wm and you want to switch from Gnome there are somethings missing like the network manager applet, the volume control, add/connect bluetooth devices and in some cases the gnome-keyring to handle passwords, private keys and certificates.

To fix that, there are some config tweaks and programs that would make the switch to i3 better. For this i going to use Fedora 34

[Read More]
linux  i3  shell  fedora 

Timezone lookup using latitude and longitud (part 2)

fastapi test project.

For the fastapi project we are going to use Sqlalchemy and postgresql taking some reference from the fastapi documentation.

# create a virtualenv, with any tool: poetry, pipenv, virtualenv module, etc 
pip install fastapi uvicorn aiofiles SQLAlchemy psycopg2-binary

project structure

.
├── Dockerfile
├── LICENSE
├── main.py
├── README.md
├── requirements.txt
└── timezone_utils
    ├── database.py
    ├── __init__.py
    ├── models.py
    ├── schemas.py
    └── utils.py

1 directory, 10 files

we define the models, schemas and engine for sqlalchemy and the api in the package timezone_utils

[Read More]

Timezone lookup using latitude and longitude (part 1)

fastapi test project.

First timezones are evil, but sometimes you need to work with them. Sometimes is easy to consume some third party api to get timezone information based on a given location using geocoding but there also exists some alternatives.

Then inspired on this blog post and the project Timezone boundary builder decided to create a test project using fastapi and postgis to do the timezone lookup using the latitude and longitude.

The project timezone boundary builder provide in their releases a shapefile that can be imported into a postgresql database with postgis shp2pgsql tool

[Read More]

Flask logging setup

dictConfig, user defined object

Setup a python logging configuration with dictConfig

Logging configuration can be defined on different ways like a dict, ini file, yaml or json somethimes is more flexible to do it on code with python, all depends on the use case.

This small example show how to setup the logging configuration using dictConfig method and how to add an user defined object to the logging filter.

import os
import logging
import socket
from flask import Flask
from logging.config import dictConfig

class ContextFilter(logging.Filter):
    hostname = socket.gethostname()

    def filter(self, record):
        record.hostname = ContextFilter.hostname
        return True

logging_configuration = dict(
    version=1,
    disable_existing_loggers=False,
    formatters={
        "default": {"format": "[%(hostname)s %(asctime)s] %(levelname)s in %(module)s: %(message)s"},
    },
    filters={"hostname_filter": {"()": ContextFilter}},
    handlers={
        "console": {
            "class": "logging.StreamHandler",
            "formatter": "default",
            "filters": ["hostname_filter"],
        }
    },
    root={"handlers": ["console"], "level": os.getenv("LOG_LEVEL", "INFO")},
)

dictConfig(logging_configuration)
app = Flask(__name__)
logger = logging.getLogger(__name__)


@app.route("/")
def home():
    logger.debug("debug message")
    logger.info("info message")
    logger.warning("warning message")
    logger.error("error message")
    logger.critical("critical error message")
    return "Hello World"


if __name__ == "__main__":
    app.run()

The filter hostname_filter use the special key "()" which means that user-defined instantiation is wanted. ContextFilter subclass logging.Filter and overrides the filter method so when key hostname is found it will get his value from the ContextFilter class using socket.gethostname() and the application output will show:

[Read More]

Markdown Blog

Setup Blog using Hugo static site generator, Nginx, LetsEncrypt

For this blog, i’ve setup a linux KVM vps, using as distro Fedora 27. Fedora 27 comes with Selinux enforced security policy by default this is OK for system or apps where you required a high security level, but for my case to serve a static blog and run a couple of services like OpenSSH and Nginx is ok to set the security policy on permissive mode.

changing the policy can be done via command or setup in permanent way on the selinux config in /etc/selinux/config

[Read More]
linux  git  shell  go  markdown  hugo 

Markdown Blog

blog with Hugo

There are many reasons and options to choose a static site generator, but the main reasons are speed and security. Speed because it can allow more control over the content you only load what is really required without additional server side content, for security not having a database that can be compromised and security updates to apply, reduce the attack vectors and the administration work.

There are many options for static site generators created on diferent languages like:

[Read More]
linux  git  shell  go  markdown  hugo  python 

Checking and validating phone numbers

libphonenumber

In applications sometimes is needed check and verify if a phone number is valid or possible if the number have all required digits to be valid for this purpose there are several options to tackle the problem like regular expressions for example to validate E.164 format, library like the [Google libphonenumber](http://Google libphonenumber) and API like the Twillio Lookup for something more complete.

for this post i created a small app for test a demo purpose using the [python port of the Google library](https://github.com/daviddrysdale/python- phonenumbers)

[Read More]

Some basics git commands

git cheatsheet

Basic workflow

# setup the project  
git clone github/project.git  
git branch FeatureX  
git co FeatureX

# make some changes  
git status  
git add .

# commit changes  
git commit -m 'commit message'

# push changes to branch  
git push origin branch-name

# next pull request/merge with master on github or command line  

Sometimes you don’t want to track some files(like local env configuration settings)

# no track file  
git update-index --assume-unchanged Gemfile .ruby-version

# track file again  
git update-index --no-assume-unchanged path/to/file.txt  

About branch

[Read More]
linux  git  shell