def create_app():
    application = Flask(__name__)

    # db接続処理
    application.config.from_object('config.config.Config')

    init_db(application)

    return application
Example #2
0
def buttons_handle(bot, update, chat_data=None, **kwargs):
    """Hadle user peek dorm inline buttons and show weather"""

    query = update.callback_query
    print(query)
    data = query.data
    db.init_db()
    if data == 'callback_fav':
        bot.send_message(query['message']['chat']['id'],
                         text='✅Добавлено в вибрані✅')
        db.add_to_favorite(
            query['message']['chat']['id'],
            query['message']['text'][14:query['message']['text'].index('п') -
                                     1])
    else:
        if '(' in data:
            city = translator.translate(data[data.index('_') +
                                             1:data.index('(')],
                                        dest='en').text
            print(city)
            wthr = wethr.get_weather(city=city)
            # print(wthr['current_condition'][0]['temp_C'])
            message = f"☂️☂️☂️Погода у {city} прямо зараз☂️☂️☂️\n" \
                      f"☀️Температура повітря зараз - {wthr['current_condition'][0]['temp_C']}°C\n" \
                      f"🌡Відчувається як {wthr['current_condition'][0]['FeelsLikeC']}°C\n" \
                      f"🌬Швидкість вітру {wthr['current_condition'][0]['windspeedKmph']} км/г\n" \
                      f"Кількість опадів - {wthr['current_condition'][0]['precipMM']} мм\n" \
                      f"Вологість - {wthr['current_condition'][0]['humidity']} %"
            bot.send_message(query['message']['chat']['id'],
                             text=message,
                             reply_markup=add_to_favorites_button())

            if not db.get_id(query['message']['chat_id']):
                db.insert_to_db(query['message']['chat']['id'], True)
        else:
            city = translator.translate(data[data.index('_') + 1:],
                                        dest='en').text
            print(city)
            wthr = wethr.get_weather(city=city)
            # print(wthr['current_condition'][0]['temp_C'])
            message = f"☂️☂️☂️Погода у {city} прямо зараз☂️☂️☂️\n" \
                      f"☀️Температура повітря зараз - {wthr['current_condition'][0]['temp_C']}°C\n" \
                      f"🌡Відчувається як {wthr['current_condition'][0]['FeelsLikeC']}°C\n" \
                      f"🌡Швидкість вітру {wthr['current_condition'][0]['windspeedKmph']} км/г"
            bot.send_message(query['message']['chat']['id'],
                             text=message,
                             reply_markup=add_to_favorites_button())

            if not db.get_id(query['message']['chat_id']):
                db.insert_to_db(query['message']['chat']['id'], True)
Example #3
0
    def db_upgrade():
        log_d('Database connection failed')
        from PyQt5.QtGui import QIcon
        from PyQt5.QtWidgets import QMessageBox

        msg_box = QMessageBox()
        msg_box.setWindowIcon(QIcon(app_constants.APP_ICO_PATH))
        msg_box.setText('Incompatible database!')
        msg_box.setInformativeText(
            "Do you want to upgrade to newest version?" +
            " It shouldn't take more than a second. Don't start a new instance!"
        )
        msg_box.setIcon(QMessageBox.Critical)
        msg_box.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
        msg_box.setDefaultButton(QMessageBox.Yes)
        if msg_box.exec() == QMessageBox.Yes:
            utils.backup_database()
            import threading
            db_p = db_constants.DB_PATH
            db.add_db_revisions(db_p)
            conn = db.init_db()
            return start_main_window(conn)
        else:
            application.exit()
            log_d('Normal Exit App: OK')
            return 0
Example #4
0
	def db_upgrade():
		log_d('Database connection failed')
		from PyQt5.QtGui import QIcon
		from PyQt5.QtWidgets import QMessageBox

		msg_box = QMessageBox()
		msg_box.setWindowIcon(QIcon(gui_constants.APP_ICO_PATH))
		msg_box.setText('Incompatible database!')
		msg_box.setInformativeText("Do you want to upgrade to newest version?" +
							 " It shouldn't take more than a second. Don't start a new instance!")
		msg_box.setIcon(QMessageBox.Critical)
		msg_box.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
		msg_box.setDefaultButton(QMessageBox.Yes)
		if msg_box.exec() == QMessageBox.Yes:

			import threading
			db_p = db_constants.DB_PATH
			threading.Thread(target=db.add_db_revisions,
					args=(db_p,)).start()
			done = None
			while not done:
				done = db.ResultQueue.get()
			conn = db.init_db()
			start_main_window(conn)
		else:
			application.exit()
			log_d('Normal Exit App: OK')
			sys.exit()
Example #5
0
    def db_upgrade():
        log_d("Database connection failed")
        from PyQt5.QtGui import QIcon
        from PyQt5.QtWidgets import QMessageBox

        msg_box = QMessageBox()
        msg_box.setWindowIcon(QIcon(app_constants.APP_ICO_PATH))
        msg_box.setText("Incompatible database!")
        msg_box.setInformativeText(
            "Do you want to upgrade to newest version?"
            + " It shouldn't take more than a second. Don't start a new instance!"
        )
        msg_box.setIcon(QMessageBox.Critical)
        msg_box.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
        msg_box.setDefaultButton(QMessageBox.Yes)
        if msg_box.exec() == QMessageBox.Yes:
            utils.backup_database()
            import threading

            db_p = db_constants.DB_PATH
            db.add_db_revisions(db_p)
            conn = db.init_db()
            return start_main_window(conn)
        else:
            application.exit()
            log_d("Normal Exit App: OK")
            return 0
Example #6
0
 def __init__(self, debug):
     handlers = [(r"/", MainHandler),
                 (r'/download/(.*)',
                  tornado.web.StaticFileHandler,
                  {'path': stat_dir}),
                 ]
     settings = {'debug': debug,
                 'template_path': template_dir,
                 'static_path': stat_dir,
                 }
     tornado.web.Application.__init__(self, handlers, **settings)
     self.db = scoped_session(sessionmaker(bind=db.init_db(
                              config.DATABASE_LOCATION)))
Example #7
0
def start(test=False):

	if os.name == 'posix':
		main_path = os.path.dirname(os.path.realpath(__file__))
		log_path = os.path.join(main_path, 'happypanda.log')
		debug_log_path = os.path.join(main_path, 'happypanda_debug.log')
	else:
		log_path = 'happypanda.log'
		debug_log_path = 'happypanda_debug.log'

	parser = argparse.ArgumentParser(prog='Happypanda',
								  description='A manga/doujinshi manager with tagging support')
	parser.add_argument('-d', '--debug', action='store_true',
					 help='happypanda_debug_log.log will be created in main directory')
	parser.add_argument('-t', '--test', action='store_true',
					 help='Run happypanda in test mode. 5000 gallery will be preadded in DB.')
	parser.add_argument('-v', '--versi on', action='version',
					 version='Happypanda v{}'.format(gui_constants.vs))
	parser.add_argument('-e', '--exceptions', action='store_true',
					 help='Disable custom excepthook')

	args = parser.parse_args()
	if args.debug:
		print("happypanda_debug.log created at {}".format(os.getcwd()))
		# create log
		try:
			with open(debug_log_path, 'x') as f:
				pass
		except FileExistsError:
			pass

		logging.basicConfig(level=logging.DEBUG,
						format='%(asctime)-8s %(levelname)-6s %(name)-6s %(message)s',
						datefmt='%d-%m %H:%M',
						filename='happypanda_debug.log',
						filemode='w')
		gui_constants.DEBUG = True
	else:
		try:
			with open(log_path, 'x') as f:
				pass
		except FileExistsError: pass
		file_handler = logging.handlers.RotatingFileHandler(
			log_path, maxBytes=1000000*10, encoding='utf-8', backupCount=2)
		logging.basicConfig(level=logging.INFO,
						format='%(asctime)-8s %(levelname)-6s %(name)-6s %(message)s',
						datefmt='%d-%m %H:%M',
						handlers=(file_handler,))


	log = logging.getLogger(__name__)
	log_i = log.info
	log_d = log.debug
	log_w = log.warning
	log_e = log.error
	log_c = log.critical

	if not args.exceptions:
		def uncaught_exceptions(ex_type, ex, tb):
			log_c(''.join(traceback.format_tb(tb)))
			log_c('{}: {}'.format(ex_type, ex))
			traceback.print_exception(ex_type, ex, tb)

		sys.excepthook = uncaught_exceptions

	application = QApplication(sys.argv)
	application.setOrganizationName('Pewpews')
	application.setOrganizationDomain('https://github.com/Pewpews/happypanda')
	application.setApplicationName('Happypanda')
	application.setApplicationDisplayName('Happypanda')
	application.setApplicationVersion('v{}'.format(gui_constants.vs))
	log_i('Happypanda Version {}'.format(gui_constants.vs))
	log_i('OS: {} {}\n'.format(platform.system(), platform.release()))
	try:
		if args.test:
			conn = db.init_db(True)
		else:
			conn = db.init_db()
		log_d('Init DB Conn: OK')
		log_i("DB Version: {}".format(db_constants.REAL_DB_VERSION))
	except:
		log_c('Invalid database')
		log.exception('Database connection failed!')
		from PyQt5.QtGui import QIcon
		from PyQt5.QtWidgets import QMessageBox
		msg_box = QMessageBox()
		msg_box.setWindowIcon(QIcon(gui_constants.APP_ICO_PATH))
		msg_box.setText('Invalid database')
		msg_box.setInformativeText("Do you want to create a new database?")
		msg_box.setIcon(QMessageBox.Critical)
		msg_box.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
		msg_box.setDefaultButton(QMessageBox.Yes)
		if msg_box.exec() == QMessageBox.Yes:
			pass
		else:
			application.exit()
			log_d('Normal Exit App: OK')
			sys.exit()

	def start_main_window(conn):
		DB = db.DBThread(conn)
		#if args.test:
		#	import threading, time
		#	ser_list = []
		#	for x in range(5000):
		#		s = gallerydb.gallery()
		#		s.profile = gui_constants.NO_IMAGE_PATH
		#		s.title = 'Test {}'.format(x)
		#		s.artist = 'Author {}'.format(x)
		#		s.path = gui_constants.static_dir
		#		s.type = 'Test'
		#		s.chapters = {0:gui_constants.static_dir}
		#		s.language = 'English'
		#		s.info = 'I am number {}'.format(x)
		#		ser_list.append(s)

		#	done = False
		#	thread_list = []
		#	i = 0
		#	while not done:
		#		try:
		#			if threading.active_count() > 5000:
		#				thread_list = []
		#				done = True
		#			else:
		#				thread_list.append(
		#					threading.Thread(target=gallerydb.galleryDB.add_gallery,
		#					  args=(ser_list[i],)))
		#				thread_list[i].start()
		#				i += 1
		#				print(i)
		#				print('Threads running: {}'.format(threading.activeCount()))
		#		except IndexError:
		#			done = True

		WINDOW = app.AppWindow()

		# styling
		d_style = gui_constants.default_stylesheet_path
		u_style =  gui_constants.user_stylesheet_path

		if len(u_style) is not 0:
			try:
				style_file = QFile(u_style)
				log_i('Select userstyle: OK')
			except:
				style_file = QFile(d_style)
				log_i('Select defaultstyle: OK')
		else:
			style_file = QFile(d_style)
			log_i('Select defaultstyle: OK')

		style_file.open(QFile.ReadOnly)
		style = str(style_file.readAll(), 'utf-8')
		application.setStyleSheet(style)
		try:
			os.mkdir(gui_constants.temp_dir)
		except FileExistsError:
			try:
				for root, dirs, files in scandir.walk('temp', topdown=False):
					for name in files:
						os.remove(os.path.join(root, name))
					for name in dirs:
						os.rmdir(os.path.join(root, name))
			except:
				log_i('Empty temp: FAIL')
		log_d('Create temp: OK')

		if test:
			return application, WINDOW

		sys.exit(application.exec_())

	def db_upgrade():
		log_d('Database connection failed')
		from PyQt5.QtGui import QIcon
		from PyQt5.QtWidgets import QMessageBox

		msg_box = QMessageBox()
		msg_box.setWindowIcon(QIcon(gui_constants.APP_ICO_PATH))
		msg_box.setText('Incompatible database!')
		msg_box.setInformativeText("Do you want to upgrade to newest version?" +
							 " It shouldn't take more than a second. Don't start a new instance!")
		msg_box.setIcon(QMessageBox.Critical)
		msg_box.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
		msg_box.setDefaultButton(QMessageBox.Yes)
		if msg_box.exec() == QMessageBox.Yes:

			import threading
			db_p = db_constants.DB_PATH
			threading.Thread(target=db.add_db_revisions,
					args=(db_p,)).start()
			done = None
			while not done:
				done = db.ResultQueue.get()
			conn = db.init_db()
			start_main_window(conn)
		else:
			application.exit()
			log_d('Normal Exit App: OK')
			sys.exit()

	if conn:
		start_main_window(conn)
	else:
		db_upgrade()
Example #8
0
        u['name'] = username
        u['email'] = email
        u['password'] = password
        user = uSchema.load(u, session=db_session).data
        db_session.add(user)
        db_session.commit()
        session["email"] = email
        return render_template('pages/placeholder.home.html')
    else:
        form = RegisterForm(request.form)
        return render_template('forms/register.html', form=form)


# Error handlers.


@app.errorhandler(500)
def internal_error(error):
    db_session.rollback()
    return render_template('errors/500.html'), 500


@app.errorhandler(404)
def not_found_error(error):
    return render_template('errors/404.html'), 404

if __name__ == "__main__":
    init_db()
    app.debug = True
    app.run()
Example #9
0
# -*- coding: utf-8 -*-
"""
Module for testing database.db_functions.py
"""
import os
import sys

from sqlalchemy.orm import scoped_session, sessionmaker

sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
from database import db, db_functions

# TODO: Move to 'test setup'
engine = db.init_db('test_lite.db')
db_session = scoped_session(sessionmaker(autocommit=False,
                                         autoflush=False,
                                         bind=engine))


# TODO: Use fixtures instead for test data
tweets = [{u'profile': u'http://pbs.twimg.com/profile_images/'
           '378800000625405614/'
           '881510dae4b7d1a9525d205106c69118_normal.jpeg',
           u'language': u'en',
           u'author': u'BarootiShahabeh',
           u'url': u'https://twitter.com/BarootiShahabeh/status/'
           '403471725436076032',
           u'text': u"RT @AlMonitor: As the #Syria war continues,"
           " Assad'sopponents are incredibly becoming lawless"
           " criminals - http://t.co/eNuTnxnW2P",
           'tweet_country': 'syria',
            "Could not find the necessary keys in the request's body")
        return not_found_error("Required keys are not found in POST's body")

    user = db_repo.find_user_by_api_key(api_key_str)

    if user is None:
        app.logger.error("No user found with {0} api key".format(api_key_str))
        return not_found_error("No associated user with this api key")

    if user.available_units < size:
        app.logger.error(
            "User with api key {0} has no units to use the service".format(
                api_key_str))
        return not_found_error("Not enough unit to use the service")

    # Using the Sample Service
    random_str = super_service.generate_random_string(size)

    app.logger.info("Successful service usage by {0}".format(api_key_str))

    db_repo.update_after_service_usage(user, size)

    response_dict = {"service_result": random_str}

    return jsonify(response_dict)


if __name__ == '__main__':
    db.init_db()
    app.run(host='0.0.0.0', port=5000, debug=True, threaded=True)
def setup_db():
    init_db()
    Pictures.app_root = app.root_path
Example #12
0
def start(test=False):
    app_constants.APP_RESTART_CODE = -123456789

    if os.name == 'posix':
        main_path = os.path.dirname(os.path.realpath(__file__))
        log_path = os.path.join(main_path, 'happypanda.log')
        debug_log_path = os.path.join(main_path, 'happypanda_debug.log')
    else:
        log_path = 'happypanda.log'
        debug_log_path = 'happypanda_debug.log'

    parser = argparse.ArgumentParser(
        prog='Happypanda',
        description='A manga/doujinshi manager with tagging support')
    parser.add_argument(
        '-d',
        '--debug',
        action='store_true',
        help='happypanda_debug_log.log will be created in main directory')
    parser.add_argument(
        '-t',
        '--test',
        action='store_true',
        help='Run happypanda in test mode. 5000 gallery will be preadded in DB.'
    )
    parser.add_argument('-v',
                        '--version',
                        action='version',
                        version='Happypanda v{}'.format(app_constants.vs))
    parser.add_argument('-e',
                        '--exceptions',
                        action='store_true',
                        help='Disable custom excepthook')

    args = parser.parse_args()
    if args.debug:
        print("happypanda_debug.log created at {}".format(os.getcwd()))
        # create log
        try:
            with open(debug_log_path, 'x') as f:
                pass
        except FileExistsError:
            pass

        logging.basicConfig(
            level=logging.DEBUG,
            format='%(asctime)-8s %(levelname)-6s %(name)-6s %(message)s',
            datefmt='%d-%m %H:%M',
            filename='happypanda_debug.log',
            filemode='w')
        app_constants.DEBUG = True
    else:
        try:
            with open(log_path, 'x') as f:
                pass
        except FileExistsError:
            pass
        file_handler = logging.handlers.RotatingFileHandler(log_path,
                                                            maxBytes=1000000 *
                                                            10,
                                                            encoding='utf-8',
                                                            backupCount=2)
        logging.basicConfig(
            level=logging.INFO,
            format='%(asctime)-8s %(levelname)-6s %(name)-6s %(message)s',
            datefmt='%d-%m %H:%M',
            handlers=(file_handler, ))

    log = logging.getLogger(__name__)
    log_i = log.info
    log_d = log.debug
    log_w = log.warning
    log_e = log.error
    log_c = log.critical

    if not args.exceptions:

        def uncaught_exceptions(ex_type, ex, tb):
            log_c(''.join(traceback.format_tb(tb)))
            log_c('{}: {}'.format(ex_type, ex))
            traceback.print_exception(ex_type, ex, tb)

        sys.excepthook = uncaught_exceptions

    application = QApplication(sys.argv)
    application.setOrganizationName('Pewpews')
    application.setOrganizationDomain('https://github.com/Pewpews/happypanda')
    application.setApplicationName('Happypanda')
    application.setApplicationDisplayName('Happypanda')
    application.setApplicationVersion('v{}'.format(app_constants.vs))
    log_i('Happypanda Version {}'.format(app_constants.vs))
    log_i('OS: {} {}\n'.format(platform.system(), platform.release()))
    conn = None
    try:
        if args.test:
            conn = db.init_db(True)
        else:
            conn = db.init_db()
        log_d('Init DB Conn: OK')
        log_i("DB Version: {}".format(db_constants.REAL_DB_VERSION))
    except:
        log_c('Invalid database')
        log.exception('Database connection failed!')
        from PyQt5.QtGui import QIcon
        from PyQt5.QtWidgets import QMessageBox
        msg_box = QMessageBox()
        msg_box.setWindowIcon(QIcon(app_constants.APP_ICO_PATH))
        msg_box.setText('Invalid database')
        msg_box.setInformativeText("Do you want to create a new database?")
        msg_box.setIcon(QMessageBox.Critical)
        msg_box.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
        msg_box.setDefaultButton(QMessageBox.Yes)
        if msg_box.exec() == QMessageBox.Yes:
            pass
        else:
            application.exit()
            log_d('Normal Exit App: OK')
            sys.exit()

    def start_main_window(conn):
        db.DBBase._DB_CONN = conn
        #if args.test:
        #	import threading, time
        #	ser_list = []
        #	for x in range(5000):
        #		s = gallerydb.gallery()
        #		s.profile = app_constants.NO_IMAGE_PATH
        #		s.title = 'Test {}'.format(x)
        #		s.artist = 'Author {}'.format(x)
        #		s.path = app_constants.static_dir
        #		s.type = 'Test'
        #		s.language = 'English'
        #		s.info = 'I am number {}'.format(x)
        #		ser_list.append(s)

        #	done = False
        #	thread_list = []
        #	i = 0
        #	while not done:
        #		try:
        #			if threading.active_count() > 5000:
        #				thread_list = []
        #				done = True
        #			else:
        #				thread_list.append(
        #					threading.Thread(target=gallerydb.galleryDB.add_gallery,
        #					  args=(ser_list[i],)))
        #				thread_list[i].start()
        #				i += 1
        #				print(i)
        #				print('Threads running: {}'.format(threading.activeCount()))
        #		except IndexError:
        #			done = True

        WINDOW = app.AppWindow()

        # styling
        d_style = app_constants.default_stylesheet_path
        u_style = app_constants.user_stylesheet_path

        if len(u_style) is not 0:
            try:
                style_file = QFile(u_style)
                log_i('Select userstyle: OK')
            except:
                style_file = QFile(d_style)
                log_i('Select defaultstyle: OK')
        else:
            style_file = QFile(d_style)
            log_i('Select defaultstyle: OK')

        style_file.open(QFile.ReadOnly)
        style = str(style_file.readAll(), 'utf-8')
        application.setStyleSheet(style)
        try:
            os.mkdir(app_constants.temp_dir)
        except FileExistsError:
            try:
                for root, dirs, files in scandir.walk('temp', topdown=False):
                    for name in files:
                        os.remove(os.path.join(root, name))
                    for name in dirs:
                        os.rmdir(os.path.join(root, name))
            except:
                log.exception("Empty temp: FAIL")
        log_d('Create temp: OK')

        if test:
            return application, WINDOW

        return application.exec_()

    def db_upgrade():
        log_d('Database connection failed')
        from PyQt5.QtGui import QIcon
        from PyQt5.QtWidgets import QMessageBox

        msg_box = QMessageBox()
        msg_box.setWindowIcon(QIcon(app_constants.APP_ICO_PATH))
        msg_box.setText('Incompatible database!')
        msg_box.setInformativeText(
            "Do you want to upgrade to newest version?" +
            " It shouldn't take more than a second. Don't start a new instance!"
        )
        msg_box.setIcon(QMessageBox.Critical)
        msg_box.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
        msg_box.setDefaultButton(QMessageBox.Yes)
        if msg_box.exec() == QMessageBox.Yes:
            utils.backup_database()
            import threading
            db_p = db_constants.DB_PATH
            db.add_db_revisions(db_p)
            conn = db.init_db()
            return start_main_window(conn)
        else:
            application.exit()
            log_d('Normal Exit App: OK')
            return 0

    if conn:
        return start_main_window(conn)
    else:
        return db_upgrade()
Example #13
0
from flask import Flask
from flask_bcrypt import Bcrypt
from flask_jwt_extended import JWTManager
from flask_mongoengine import MongoEngineSessionInterface
from flask_restful import Api

from database.db import db
from database.db import init_db

from flask_Restfu_jwt.resource.routes import initialize_routes

app = Flask(__name__)
app.config[
    'JWT_SECRET_KEY'] = "44C176788A7EEF45F2F79F53D108F8FDC630B5A4B35D74097795CA35153C181C"
app.config['PROPAGATE_EXCEPTIONS'] = True
app.config['JWT_BLACKLIST_ENABLED'] = True
app.config['JWT_BLACKLIST_TOKEN_CHECKS'] = ['access', 'refresh']

#jwt config
jwt = JWTManager(app)
#Restful and hash password config
api = Api(app)
bcrypt = Bcrypt(app)

#mongodb address and create db product
app.config['MONGODB_SETTINGS'] = {'host': 'mongodb://localhost:27017/product'}

app.session_interface = MongoEngineSessionInterface(db)
init_db(app)
initialize_routes(api)
Example #14
0
def start(test=False):
    app_constants.APP_RESTART_CODE = -123456789

    if os.name == 'posix':
        main_path = os.path.dirname(os.path.realpath(__file__))
        log_path = os.path.join(main_path, 'happypanda.log')
        debug_log_path = os.path.join(main_path, 'happypanda_debug.log')
    else:
        log_path = 'happypanda.log'
        debug_log_path = 'happypanda_debug.log'
    if os.path.exists('cacert.pem'):
        os.environ["REQUESTS_CA_BUNDLE"] = os.path.join(os.getcwd(), "cacert.pem")

    parser = argparse.ArgumentParser(prog='Happypanda',
                                  description='A manga/doujinshi manager with tagging support')
    parser.add_argument('-d', '--debug', action='store_true',
                     help='happypanda_debug_log.log will be created in main directory')
    parser.add_argument('-v', '--version', action='version',
                     version='Happypanda v{}'.format(app_constants.vs))
    parser.add_argument('-e', '--exceptions', action='store_true',
                     help='Disable custom excepthook')
    parser.add_argument('-x', '--dev', action='store_true',
                     help='Development Switch')

    args = parser.parse_args()
    log_handlers = []
    log_level = logging.INFO
    if args.dev:
        log_handlers.append(logging.StreamHandler())
    if args.debug:
        print("happypanda_debug.log created at {}".format(os.getcwd()))
        # create log
        try:
            with open(debug_log_path, 'x') as f:
                pass
        except FileExistsError:
            pass

        log_handlers.append(logging.FileHandler(debug_log_path, 'w', 'utf-8'))
        log_level = logging.DEBUG
        app_constants.DEBUG = True
    else:
        try:
            with open(log_path, 'x') as f:
                pass
        except FileExistsError: pass
        log_handlers.append(logging.handlers.RotatingFileHandler(
            log_path, maxBytes=1000000*10, encoding='utf-8', backupCount=2))

    # Fix for logging not working
    # clear the handlers first before adding these custom handler
    # http://stackoverflow.com/a/15167862
    logging.getLogger('').handlers = []
    logging.basicConfig(level=log_level,
                    format='%(asctime)-8s %(levelname)-6s %(name)-6s %(message)s',
                    datefmt='%d-%m %H:%M',
                    handlers=tuple(log_handlers))

    log = logging.getLogger(__name__)
    log_i = log.info
    log_d = log.debug
    log_w = log.warning
    log_e = log.error
    log_c = log.critical

    if not args.exceptions:
        def uncaught_exceptions(ex_type, ex, tb):
            log_c(''.join(traceback.format_tb(tb)))
            log_c('{}: {}'.format(ex_type, ex))
            traceback.print_exception(ex_type, ex, tb)

        sys.excepthook = uncaught_exceptions

    if app_constants.FORCE_HIGH_DPI_SUPPORT:
        log_i("Enabling high DPI display support")
        os.environ.putenv("QT_DEVICE_PIXEL_RATIO", "auto")

    effects = [Qt.UI_AnimateCombo, Qt.UI_FadeMenu, Qt.UI_AnimateMenu,
            Qt.UI_AnimateTooltip, Qt.UI_FadeTooltip]
    for effect in effects:
        QApplication.setEffectEnabled(effect)

    application = QApplication(sys.argv)
    application.setOrganizationName('Pewpews')
    application.setOrganizationDomain('https://github.com/Pewpews/happypanda')
    application.setApplicationName('Happypanda')
    application.setApplicationDisplayName('Happypanda')
    application.setApplicationVersion('v{}'.format(app_constants.vs))
    application.setAttribute(Qt.AA_UseHighDpiPixmaps)
    application.font().setStyleStrategy(application.font().PreferAntialias)

    log_i('Starting Happypanda...'.format(app_constants.vs))
    if args.debug:
        log_i('Running in debug mode'.format(app_constants.vs))
        import pprint
        sys.displayhook = pprint.pprint
    app_constants.load_icons()
    log_i('Happypanda Version {}'.format(app_constants.vs))
    log_i('OS: {} {}\n'.format(platform.system(), platform.release()))
    conn = None
    try:
        conn = db.init_db()
        log_d('Init DB Conn: OK')
        log_i("DB Version: {}".format(db_constants.REAL_DB_VERSION))
    except:
        log_c('Invalid database')
        log.exception('Database connection failed!')
        from PyQt5.QtGui import QIcon
        from PyQt5.QtWidgets import QMessageBox
        msg_box = QMessageBox()
        msg_box.setWindowIcon(QIcon(app_constants.APP_ICO_PATH))
        msg_box.setText('Invalid database')
        msg_box.setInformativeText("Do you want to create a new database?")
        msg_box.setIcon(QMessageBox.Critical)
        msg_box.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
        msg_box.setDefaultButton(QMessageBox.Yes)
        if msg_box.exec() == QMessageBox.Yes:
            pass
        else:
            application.exit()
            log_d('Normal Exit App: OK')
            sys.exit()

    def start_main_window(conn):
        db.DBBase._DB_CONN = conn
        #if args.test:
        #	import threading, time
        #	ser_list = []
        #	for x in range(5000):
        #		s = gallerydb.gallery()
        #		s.profile = app_constants.NO_IMAGE_PATH
        #		s.title = 'Test {}'.format(x)
        #		s.artist = 'Author {}'.format(x)
        #		s.path = app_constants.static_dir
        #		s.type = 'Test'
        #		s.language = 'English'
        #		s.info = 'I am number {}'.format(x)
        #		ser_list.append(s)

        #	done = False
        #	thread_list = []
        #	i = 0
        #	while not done:
        #		try:
        #			if threading.active_count() > 5000:
            #				thread_list = []
        #				done = True
        #			else:
        #				thread_list.append(
        #					threading.Thread(target=gallerydb.galleryDB.add_gallery,
        #					  args=(ser_list[i],)))
        #				thread_list[i].start()
        #				i += 1
        #				print(i)
        #				print('Threads running: {}'.format(threading.activeCount()))
        #		except IndexError:
        #			done = True

        WINDOW = app.AppWindow(args.exceptions)

        # styling
        d_style = app_constants.default_stylesheet_path
        u_style =  app_constants.user_stylesheet_path

        if len(u_style) is not 0:
            try:
                style_file = QFile(u_style)
                log_i('Select userstyle: OK')
            except:
                style_file = QFile(d_style)
                log_i('Select defaultstyle: OK')
        else:
            style_file = QFile(d_style)
            log_i('Select defaultstyle: OK')

        style_file.open(QFile.ReadOnly)
        style = str(style_file.readAll(), 'utf-8')
        application.setStyleSheet(style)
        try:
            os.mkdir(app_constants.temp_dir)
        except FileExistsError:
            try:
                for root, dirs, files in scandir.walk('temp', topdown=False):
                    for name in files:
                        os.remove(os.path.join(root, name))
                    for name in dirs:
                        os.rmdir(os.path.join(root, name))
            except:
                log.exception("Empty temp: FAIL")
        log_d('Create temp: OK')

        if test:
            return application, WINDOW

        return application.exec_()

    def db_upgrade():
        log_d('Database connection failed')
        from PyQt5.QtGui import QIcon
        from PyQt5.QtWidgets import QMessageBox

        msg_box = QMessageBox()
        msg_box.setWindowIcon(QIcon(app_constants.APP_ICO_PATH))
        msg_box.setText('Incompatible database!')
        msg_box.setInformativeText("Do you want to upgrade to newest version?" +
                             " It shouldn't take more than a second. Don't start a new instance!")
        msg_box.setIcon(QMessageBox.Critical)
        msg_box.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
        msg_box.setDefaultButton(QMessageBox.Yes)
        if msg_box.exec() == QMessageBox.Yes:
            utils.backup_database()
            import threading
            db_p = db_constants.DB_PATH
            db.add_db_revisions(db_p)
            conn = db.init_db()
            return start_main_window(conn)
        else:
            application.exit()
            log_d('Normal Exit App: OK')
            return 0

    if conn:
        return start_main_window(conn)
    else:
        return db_upgrade()
Example #15
0
def main():
    """
    Main function in batch script, where eternal while loop is running.

    Script uses modules dataMining and database to retrieve and store data.
    """
    # Logger
    logHandler = TimedRotatingFileHandler(os.path.join(
                                          os.path.dirname(__file__),
                                          "logs", "batch.log"),
                                          when="midnight")
    logFormatter = logging.Formatter('%(asctime)s: %(levelname)s; %(message)s')
    logHandler.setFormatter(logFormatter)
    logger = logging.getLogger('batch logger')
    logger.addHandler(logHandler)
    logger.setLevel(logging.DEBUG)

    # Signals
    def signal_handler(sig, frame):
        """
        Method that handles received kill signals.
        """
        logger.warning('Caught signal: %s, killing process.', sig)
        sys.exit(0)
    signal.signal(signal.SIGINT, signal_handler)
    # Search setup
    countries = tweetSearch.getCountries()
    words = tweetSearch.getSearchWords()
    # Database setup
    create_tables = False
    if not os.path.isfile(config.DATABASE_LOCATION):
        create_tables = True
    engine = db.init_db(config.DATABASE_LOCATION)
    if create_tables:
        db.create_tables(engine)
    db_session = scoped_session(sessionmaker(autocommit=False,
                                             autoflush=False,
                                             bind=engine))
    logger.info('Started script (PID: %s).' % os.getpid())

    def get_and_store_data(search_terms, search_function, save_function):
        """
        Method used when getting and storing data. Method is made in a
        semi-generic manner, so that it the batch script can evolve into
        a dynamic script that can be tailored in the configuration file.
        """
        skipped_terms = []
        for terms in search_terms:
            results = None
            try:
                results = search_function(terms)
            except SearchEngineLimitError:
                logger.warning('Search engine limit exceeded,'
                               ' sleeping for %s seconds.' %
                               config.SLEEP_TIME)
                time.sleep(config.SLEEP_TIME)
                skipped_terms.append(terms)
            if results:
                # Save tweets
                logger.info('Found %s results when searching for %s.' %
                            (len(results), ' and '.join(terms)))
                save_function(db_session, results, terms)
        if skipped_terms:
            get_and_store_data(skipped_terms,
                               search_function,
                               save_function)
    # Starting datamining loop
    while 1:
        try:
            search_terms = [(a, b) for b in words for a in countries]
            search_function = tweetSearch.getTweets
            save_function = db_functions.saveTweets
            get_and_store_data(search_terms, search_function, save_function)
        except:
            logger.exception('Exception caught!')
            raise
Example #16
0
def setup_db():
    """Create a user to test with only run once"""
    init_db()
    userDatastore.find_or_create_role(name='admin')
    userDatastore.find_or_create_role(name='primary')

    if not fieldExists(dbSession, Company.company_name, "Admin"):
        newCompany = Company(company_name="Admin",
                             email="*****@*****.**")
        dbSession.add(newCompany)

    dbSession.commit()

    if not fieldExists(dbSession, User.id, 1):
        #primary
        newUser = User(id=1,
                       email='*****@*****.**',
                       username='******',
                       password='******',
                       company_name='Admin',
                       active=1)
        dbSession.add(newUser)
        userDatastore.add_role_to_user(newUser, 'admin')
        userDatastore.activate_user(newUser)
        dbSession.commit()

    if not fieldExists(dbSession, Status.status_name, "Not Reached"):
        newStatus = Status(status_name="Not Reached", status_number=1)
        dbSession.add(newStatus)
        dbSession.commit()

    if not fieldExists(dbSession, Status.status_name, "Paid"):
        newStatus = Status(status_name="Paid", status_number=2)
        dbSession.add(newStatus)
        dbSession.commit()

    if not fieldExists(dbSession, Status.status_name, "Appraisal Booked"):
        newStatus = Status(status_name="Appraisal Booked", status_number=3)
        dbSession.add(newStatus)
        dbSession.commit()

    if not fieldExists(dbSession, Status.status_name, "Waiting for Appraisal"):
        newStatus = Status(status_name="Waiting for Appraisal",
                           status_number=4)
        dbSession.add(newStatus)
        dbSession.commit()

    if not fieldExists(dbSession, Status.status_name, "Appraised"):
        newStatus = Status(status_name="Appraised", status_number=5)
        dbSession.add(newStatus)
        dbSession.commit()

    if not fieldExists(dbSession, Status.status_name, "Quote Sent"):
        newStatus = Status(status_name="Quote Sent", status_number=6)
        dbSession.add(newStatus)
        dbSession.commit()

    if not fieldExists(dbSession, Status.status_name,
                       "Waiting for Alberta1Call"):
        newStatus = Status(status_name="Waiting for Alberta1Call",
                           status_number=7)
        dbSession.add(newStatus)
        dbSession.commit()

    if not fieldExists(dbSession, Status.status_name, "Installation Pending"):
        newStatus = Status(status_name="Installation Pending", status_number=8)
        dbSession.add(newStatus)
        dbSession.commit()

    if not fieldExists(dbSession, Status.status_name, "Installing"):
        newStatus = Status(status_name="Installing", status_number=9)
        dbSession.add(newStatus)
        dbSession.commit()

    if not fieldExists(dbSession, Status.status_name, "No Longer Interested"):
        newStatus = Status(status_name="No Longer Interested",
                           status_number=10)
        dbSession.add(newStatus)
        dbSession.commit()
    Pictures.app_root = app.root_path
    Quotes.app_root = app.root_path
    Email.staticFolder = app.root_path + "/static/"
Example #17
0
def main():
    """
    Main function in batch script, where eternal while loop is running.

    Script uses modules dataMining and database to retrieve and store data.
    """
    # Logger
    logHandler = TimedRotatingFileHandler(os.path.join(
        os.path.dirname(__file__), "logs", "batch.log"),
                                          when="midnight")
    logFormatter = logging.Formatter('%(asctime)s: %(levelname)s; %(message)s')
    logHandler.setFormatter(logFormatter)
    logger = logging.getLogger('batch logger')
    logger.addHandler(logHandler)
    logger.setLevel(logging.DEBUG)

    # Signals
    def signal_handler(sig, frame):
        """
        Method that handles received kill signals.
        """
        logger.warning('Caught signal: %s, killing process.', sig)
        sys.exit(0)

    signal.signal(signal.SIGINT, signal_handler)
    # Search setup
    countries = tweetSearch.getCountries()
    words = tweetSearch.getSearchWords()
    # Database setup
    create_tables = False
    if not os.path.isfile(config.DATABASE_LOCATION):
        create_tables = True
    engine = db.init_db(config.DATABASE_LOCATION)
    if create_tables:
        db.create_tables(engine)
    db_session = scoped_session(
        sessionmaker(autocommit=False, autoflush=False, bind=engine))
    logger.info('Started script (PID: %s).' % os.getpid())

    def get_and_store_data(search_terms, search_function, save_function):
        """
        Method used when getting and storing data. Method is made in a
        semi-generic manner, so that it the batch script can evolve into
        a dynamic script that can be tailored in the configuration file.
        """
        skipped_terms = []
        for terms in search_terms:
            results = None
            try:
                results = search_function(terms)
            except SearchEngineLimitError:
                logger.warning('Search engine limit exceeded,'
                               ' sleeping for %s seconds.' % config.SLEEP_TIME)
                time.sleep(config.SLEEP_TIME)
                skipped_terms.append(terms)
            if results:
                # Save tweets
                logger.info('Found %s results when searching for %s.' %
                            (len(results), ' and '.join(terms)))
                save_function(db_session, results, terms)
        if skipped_terms:
            get_and_store_data(skipped_terms, search_function, save_function)

    # Starting datamining loop
    while 1:
        try:
            search_terms = [(a, b) for b in words for a in countries]
            search_function = tweetSearch.getTweets
            save_function = db_functions.saveTweets
            get_and_store_data(search_terms, search_function, save_function)
        except:
            logger.exception('Exception caught!')
            raise
Example #18
0
def start(test=False):
    app_constants.APP_RESTART_CODE = -123456789

    if os.name == "posix":
        main_path = os.path.dirname(os.path.realpath(__file__))
        log_path = os.path.join(main_path, "happypanda.log")
        debug_log_path = os.path.join(main_path, "happypanda_debug.log")
    else:
        log_path = "happypanda.log"
        debug_log_path = "happypanda_debug.log"

    parser = argparse.ArgumentParser(prog="Happypanda", description="A manga/doujinshi manager with tagging support")
    parser.add_argument(
        "-d", "--debug", action="store_true", help="happypanda_debug_log.log will be created in main directory"
    )
    parser.add_argument(
        "-t", "--test", action="store_true", help="Run happypanda in test mode. 5000 gallery will be preadded in DB."
    )
    parser.add_argument("-v", "--version", action="version", version="Happypanda v{}".format(app_constants.vs))
    parser.add_argument("-e", "--exceptions", action="store_true", help="Disable custom excepthook")

    args = parser.parse_args()
    if args.debug:
        print("happypanda_debug.log created at {}".format(os.getcwd()))
        # create log
        try:
            with open(debug_log_path, "x") as f:
                pass
        except FileExistsError:
            pass

        logging.basicConfig(
            level=logging.DEBUG,
            format="%(asctime)-8s %(levelname)-6s %(name)-6s %(message)s",
            datefmt="%d-%m %H:%M",
            filename="happypanda_debug.log",
            filemode="w",
        )
        app_constants.DEBUG = True
    else:
        try:
            with open(log_path, "x") as f:
                pass
        except FileExistsError:
            pass
        file_handler = logging.handlers.RotatingFileHandler(
            log_path, maxBytes=1000000 * 10, encoding="utf-8", backupCount=2
        )
        logging.basicConfig(
            level=logging.INFO,
            format="%(asctime)-8s %(levelname)-6s %(name)-6s %(message)s",
            datefmt="%d-%m %H:%M",
            handlers=(file_handler,),
        )

    log = logging.getLogger(__name__)
    log_i = log.info
    log_d = log.debug
    log_w = log.warning
    log_e = log.error
    log_c = log.critical

    if not args.exceptions:

        def uncaught_exceptions(ex_type, ex, tb):
            log_c("".join(traceback.format_tb(tb)))
            log_c("{}: {}".format(ex_type, ex))
            traceback.print_exception(ex_type, ex, tb)

        sys.excepthook = uncaught_exceptions

    application = QApplication(sys.argv)
    application.setOrganizationName("Pewpews")
    application.setOrganizationDomain("https://github.com/Pewpews/happypanda")
    application.setApplicationName("Happypanda")
    application.setApplicationDisplayName("Happypanda")
    application.setApplicationVersion("v{}".format(app_constants.vs))
    log_i("Happypanda Version {}".format(app_constants.vs))
    log_i("OS: {} {}\n".format(platform.system(), platform.release()))
    conn = None
    try:
        if args.test:
            conn = db.init_db(True)
        else:
            conn = db.init_db()
        log_d("Init DB Conn: OK")
        log_i("DB Version: {}".format(db_constants.REAL_DB_VERSION))
    except:
        log_c("Invalid database")
        log.exception("Database connection failed!")
        from PyQt5.QtGui import QIcon
        from PyQt5.QtWidgets import QMessageBox

        msg_box = QMessageBox()
        msg_box.setWindowIcon(QIcon(app_constants.APP_ICO_PATH))
        msg_box.setText("Invalid database")
        msg_box.setInformativeText("Do you want to create a new database?")
        msg_box.setIcon(QMessageBox.Critical)
        msg_box.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
        msg_box.setDefaultButton(QMessageBox.Yes)
        if msg_box.exec() == QMessageBox.Yes:
            pass
        else:
            application.exit()
            log_d("Normal Exit App: OK")
            sys.exit()

    def start_main_window(conn):
        db.DBBase._DB_CONN = conn
        # if args.test:
        # 	import threading, time
        # 	ser_list = []
        # 	for x in range(5000):
        # 		s = gallerydb.gallery()
        # 		s.profile = app_constants.NO_IMAGE_PATH
        # 		s.title = 'Test {}'.format(x)
        # 		s.artist = 'Author {}'.format(x)
        # 		s.path = app_constants.static_dir
        # 		s.type = 'Test'
        # 		s.language = 'English'
        # 		s.info = 'I am number {}'.format(x)
        # 		ser_list.append(s)

        # 	done = False
        # 	thread_list = []
        # 	i = 0
        # 	while not done:
        # 		try:
        # 			if threading.active_count() > 5000:
        # 				thread_list = []
        # 				done = True
        # 			else:
        # 				thread_list.append(
        # 					threading.Thread(target=gallerydb.galleryDB.add_gallery,
        # 					  args=(ser_list[i],)))
        # 				thread_list[i].start()
        # 				i += 1
        # 				print(i)
        # 				print('Threads running: {}'.format(threading.activeCount()))
        # 		except IndexError:
        # 			done = True

        WINDOW = app.AppWindow()

        # styling
        d_style = app_constants.default_stylesheet_path
        u_style = app_constants.user_stylesheet_path

        if len(u_style) is not 0:
            try:
                style_file = QFile(u_style)
                log_i("Select userstyle: OK")
            except:
                style_file = QFile(d_style)
                log_i("Select defaultstyle: OK")
        else:
            style_file = QFile(d_style)
            log_i("Select defaultstyle: OK")

        style_file.open(QFile.ReadOnly)
        style = str(style_file.readAll(), "utf-8")
        application.setStyleSheet(style)
        try:
            os.mkdir(app_constants.temp_dir)
        except FileExistsError:
            try:
                for root, dirs, files in scandir.walk("temp", topdown=False):
                    for name in files:
                        os.remove(os.path.join(root, name))
                    for name in dirs:
                        os.rmdir(os.path.join(root, name))
            except:
                log.exception("Empty temp: FAIL")
        log_d("Create temp: OK")

        if test:
            return application, WINDOW

        return application.exec_()

    def db_upgrade():
        log_d("Database connection failed")
        from PyQt5.QtGui import QIcon
        from PyQt5.QtWidgets import QMessageBox

        msg_box = QMessageBox()
        msg_box.setWindowIcon(QIcon(app_constants.APP_ICO_PATH))
        msg_box.setText("Incompatible database!")
        msg_box.setInformativeText(
            "Do you want to upgrade to newest version?"
            + " It shouldn't take more than a second. Don't start a new instance!"
        )
        msg_box.setIcon(QMessageBox.Critical)
        msg_box.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
        msg_box.setDefaultButton(QMessageBox.Yes)
        if msg_box.exec() == QMessageBox.Yes:
            utils.backup_database()
            import threading

            db_p = db_constants.DB_PATH
            db.add_db_revisions(db_p)
            conn = db.init_db()
            return start_main_window(conn)
        else:
            application.exit()
            log_d("Normal Exit App: OK")
            return 0

    if conn:
        return start_main_window(conn)
    else:
        return db_upgrade()