settings.py 2.28 KB
Newer Older
1 2 3 4
'''
Initializing application dependencies
- Sqlite3 database
'''
Shashank Kumar's avatar
Shashank Kumar committed
5
import os
6
import sqlite3
Shashank Kumar's avatar
Shashank Kumar committed
7 8 9
import logging
from zipfile import ZipFile
from io import BytesIO
10

Shashank Kumar's avatar
Shashank Kumar committed
11
import requests
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

DATABASE_FILE = 'new_contributor_wizard.db'
USER_INFOMATION_TABLE = 'USERS'


def initializing_database():
    '''
    Creating database file is not present
    '''
    connection = sqlite3.connect(DATABASE_FILE)
    db_cursor = connection.cursor()
    try:
        db_cursor.execute('''
            CREATE TABLE USERS
            (id VARCHAR(36) PRIMARY KEY,
             email UNIQUE,
             password VARCHAR(64),
             fullname TEXT,
             language TEXT,
             timezone TEXT)
        ''')
        connection.commit()
        return connection
    except sqlite3.OperationalError:
        return connection
Shashank Kumar's avatar
Shashank Kumar committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77


def installing_kivy_garden_package(package_name):
    '''
    Installing garden package and moving it to 'libs/garden/'
    '''
    logging.basicConfig(level=logging.INFO)
    if not package_name.startswith('garden.'):
        package_name = 'garden.' + package_name
    url = 'https://github.com/kivy-garden/{}/archive/master.zip'.format(
        package_name
    )

    compact_package_name = package_name.split('garden.')[1]
    root_directory = os.getcwd()
    existing_garden_packages = os.listdir(root_directory + '/libs/garden')
    if compact_package_name in existing_garden_packages:
        logging.info('Garden: %s exists', compact_package_name)
        return

    logging.info('Garden: Downloading %s ...', url)
    response = requests.get(url)
    if response.status_code != 200:
        logging.info('''Garden: Unable to find the garden package.
                        (error=%s)''', response.status_code)
        return

    data = b''
    for buf in response.iter_content(1024):
        data += buf
    logging.info('Garden: Download done (%s downloaded)', package_name)

    ZipFile(BytesIO(data)).extractall()

    for existing_files_folders in os.listdir(root_directory):
        if 'garden.' in existing_files_folders:
            new_name = root_directory + '/libs/garden/' + compact_package_name
            os.rename(existing_files_folders, new_name)
            success_log = compact_package_name + ' relocated to ' + new_name
            logging.info('Garden: %s', success_log)
            break