Skip to content

Commit

Permalink
Added logger, config auto initialize with default file 🍩 🍪
Browse files Browse the repository at this point in the history
  • Loading branch information
amitt001 committed Jul 3, 2018
1 parent 6032547 commit 114bd00
Show file tree
Hide file tree
Showing 58 changed files with 79 additions and 22 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Files
*.pyc
bootstrap.min.css
.coverage
*.db
*.log
.coverage
bootstrap.min.css

# Directory
*.egg-info/
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<p align="center"><img src="qolugo/static/logo/logov2.png" alt="pygmy" height="200px"></p>
<p align="center"><img src="pygmyui/static/logo/logov2.png" alt="pygmy" height="200px"></p>

Pygmy
=====
Expand Down Expand Up @@ -55,7 +55,7 @@ Technical Info

- Python 3, Javascript, JQuery, HTML, CSS
- REST API: Flask
- Qolugo: Django(It serves the web user interface)
- Pygmyui: Django(It serves the web user interface)
- DB: PostgreSQL/MySQL/SQLite
- Others: SQLAlchmey, JWT

Expand All @@ -78,11 +78,11 @@ Note:

1. The project has two config files:
- pygmy.cfg: `pygmy/config/pygmy.cfg` rest API and pygmy core settings file
- settings.py: `qolugo/qolugo/settings.py` Django settings file
- settings.py: `pygmyui/pygmyui/settings.py` Django settings file
2. SQLite is default db, if you are using PostgreSQL or MySQL with this project, make sure they are installed into the system.
3. To modify config settings vim `pygmy/config/pygmy.cfg`
4. You can run pygmy shell present in src directory to run the program on terminal. `python shell`
5. By default in `qolugo/qolugo/settings.py` DEBUG is set to True, set it to False in production
5. By default in `pygmyui/pygmyui/settings.py` DEBUG is set to True, set it to False in production

DB Setup:
=========
Expand Down
8 changes: 4 additions & 4 deletions init.d/pyui
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#!/bin/bash
# init.d script for qolugo app
# init.d script for pygmyui app

NAME=qolugo
NAME=pygmyui
PIDFILE=/var/run/$NAME.pid

PORT=8000
APP_DIR=/opt/pygyco/qolugo
APP_DIR=/opt/pygyco/pygmyui
PYTHONPATH='/opt/pygyco/env/bin/gunicorn'
APP_ARGS='--log-file /var/log/pygmy/uierror_logs.log --access-logfile /var/log/pygmy/uiacclogs.log --bind 127.0.0.1:8000 --workers 2 qolugo.wsgi'
APP_ARGS='--log-file /var/log/pygmy/uierror_logs.log --access-logfile /var/log/pygmy/uiacclogs.log --bind 127.0.0.1:8000 --workers 2 pygmyui.wsgi'
APP_STOP_ARGS='--stop $PIDFILE'
APP_RELOAD_ARGS='--reload $PIDFILE'

Expand Down
6 changes: 5 additions & 1 deletion pygmy/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,21 @@ class Configuration:
def __init__(self):
# default sqlite3
# TODO: Take this from cfg files
self.default_config_path = 'pygmy/config/pygmy.cfg'
self.cfg = None
self.debug = False
self.schema = None
self.host = None
self.port = None
self.secret = None
self.logging = None
self.webservice_url = None
self.database = None
self.initialize()

def _read_cfg(self):
self.cfg = ConfigParser()
self.cfg.read(os.environ[_CONFIG_ENV_VAR])
self.cfg.read(os.environ.get(_CONFIG_ENV_VAR, self.default_config_path))

def __getattr__(self, name):
"""Add sections dynamically. With this no need to define
Expand All @@ -46,6 +49,7 @@ def initialize(self):
self.host = self.cfg['pygmy']['host']
self.port = self.cfg['pygmy']['port']
self.secret = self.cfg['pygmy']['flask_secret']
self.logging = self.cfg['logging']
self.webservice_url = "{0}://{1}:{2}".format(
self.schema, self.host, self.port)

Expand Down
5 changes: 5 additions & 0 deletions pygmy/config/pygmy.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,8 @@ jwt_token_blacklist_engine = None

[pygmy_internal]
pygmy_header_key = KJ*57*6)(*&^dh

[logging]
filepath = pygmy/data/pygmy.log
level = DEBUG
format = %%(asctime)s - %%(name)s - %%(levelname)s - %%(message)s
6 changes: 6 additions & 0 deletions pygmy/config/pygmy_test.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,9 @@ jwt_token_blacklist_engine = None

[pygmy_internal]
pygmy_header_key = KJ*57*6)(*&^dh


[logging]
filepath = pygmy/data/pygmy_test.log
level = DEBUG
format = %%(asctime)s - %%(name)s - %%(levelname)s - %%(message)s
1 change: 0 additions & 1 deletion pygmy/core/hashdigest.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,3 @@ def unshorten(self, s):
for i in range(1000):
sh = hd.shorten(1099)
hd.decode(sh)
print(time.time() - t)
41 changes: 41 additions & 0 deletions pygmy/core/logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import logging
import threading

from pygmy.config import config


class Logger:
"""Configure singleton logger for application and return its instance."""
_lock = threading.Lock()
_instance = None

def __new__(cls, filename=None, level=logging.DEBUG, format=None):
log_config = {'level': getattr(logging, level, logging.DEBUG)}
if filename:
log_config.update({'filename': filename})
if format:
log_config.update({'format': format})

with cls._lock:
if cls._instance is None:
logging.basicConfig(**log_config)
logger = logging.getLogger(__name__)
cls._instance = logger
cls._instance.info('Logger created for {}'.format(__name__))
cls._add_stream_handler(level, format)

cls._instance.info('Logging setup done for {}'.format(__name__))
return cls._instance

@classmethod
def _add_stream_handler(cls, level, format):
cls._instance.info('Adding stream handler to logger')
handler = logging.StreamHandler()
handler.setLevel(level)
if format:
formatter = logging.Formatter(format)
handler.setFormatter(formatter)
cls._instance.addHandler(handler)


log = Logger(config.logging['filepath'], config.logging['level'], config.logging['format'])
3 changes: 2 additions & 1 deletion pygmy/database/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from sqlalchemy.ext.declarative import declarative_base

from pygmy.config import config
from pygmy.core.logger import log


class BaseDatabase:
Expand All @@ -25,7 +26,7 @@ def abort(self):

def initialize(self, debug=False):
self.url = config.database['url']
print(self.url)
log.info('DB URL: {}'.format(self.url))
self._prepare(self.url)
self.engine = create_engine(self.url, echo=debug)
session = sessionmaker(bind=self.engine)
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion qolugo/manage.py → pygmyui/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import sys

if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "qolugo.settings")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "pygmyui.settings")
try:
from django.core.management import execute_from_command_line

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions qolugo/qolugo/settings.py → pygmyui/pygmyui/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
'middleware.exception.ExceptionMiddleware'
]

ROOT_URLCONF = 'qolugo.urls'
ROOT_URLCONF = 'pygmyui.urls'

TEMPLATES = [
{
Expand All @@ -79,7 +79,7 @@
PROJECT_DIR = os.path.dirname(__file__)


WSGI_APPLICATION = 'qolugo.wsgi.application'
WSGI_APPLICATION = 'pygmyui.wsgi.application'


# Database
Expand Down
2 changes: 1 addition & 1 deletion qolugo/qolugo/urls.py → pygmyui/pygmyui/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"""
from django.conf import settings
from django.conf.urls import url, include
import qolugo.generic_views as views
import pygmyui.generic_views as views


urlpatterns = [
Expand Down
2 changes: 1 addition & 1 deletion qolugo/qolugo/wsgi.py → pygmyui/pygmyui/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "qolugo.settings")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "pygmyui.settings")

application = get_wsgi_application()
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


PYGMY_API_ARGS = ['gunicorn', '-b 127.0.0.1:9119', '-w 1', 'pygmy.rest.wsgi:app']
QOLUGO_ARGS = ['gunicorn', '-b 127.0.0.1:8000', '-w 1', 'qolugo.wsgi']
PYGMYUI_ARGS = ['gunicorn', '-b 127.0.0.1:8000', '-w 1', 'pygmyui.wsgi']
process = []


Expand All @@ -25,8 +25,8 @@ def print_err(proc, timeout=2):
# Print any configuration error
print_err(process[-1])
print("Starting development server at http://127.0.0.1:8000/")
os.chdir('qolugo')
process.append(subprocess.Popen(QOLUGO_ARGS, stdout=subprocess.PIPE,
os.chdir('pygmyui')
process.append(subprocess.Popen(PYGMYUI_ARGS, stdout=subprocess.PIPE,
stderr=subprocess.PIPE))
# Print any configuration error
print_err(process[-1])
Expand Down
Empty file removed setup.cfg
Empty file.
2 changes: 1 addition & 1 deletion tests/fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class PygmyUiTestServer:

@classmethod
def start_pygmy_ui_server(cls):
command = ['gunicorn','-b 127.0.0.1:8001', '--chdir', 'qolugo', '-w 1', 'qolugo.wsgi']
command = ['gunicorn','-b 127.0.0.1:8001', '--chdir', 'pygmyui', '-w 1', 'pygmyui.wsgi']
cls.pygmyui_proc = subprocess.Popen(
command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# Wait for server to start
Expand Down

0 comments on commit 114bd00

Please sign in to comment.