def __init__(self) -> None: self.url = Config.get('URL_FETCHER', 'url') try: self.headers = Config.get('URL_FETCHER', 'headers', param_type="json") except JSONDecodeError: Logger.get(self.__class__.__name__).warn( "Problem decoding URL headers from JSON in config!") self.headers = None self.content: Optional[str] = None
def parse(self) -> None: contents = self.html[self.html.index(Config.get('PARSER', 'start_tag') ) + len(Config.get('PARSER', 'start_tag')):self.html. index(Config.get('PARSER', 'end_tag'))].strip() if Config.get('PARSER', 'empty_pattern') in contents: Logger.get(self.__class__.__name__).info("No new contents.") self.has_new_content = False self.links = {} try: os.remove("{}/{}".format(Config.get('CRON', 'project_root'), Config.get('PARSER', 'cache_file'))) except OSError as e: if e.errno != errno.ENOENT: raise return with open(Config.get('PARSER', 'cache_file'), 'a+') as f: f.seek(0) if f.read() == contents: Logger.get(self.__class__.__name__).info("No new contents.") self.has_new_content = False self.links = {} else: Logger.get( self.__class__.__name__).info("New content detected.") self.has_new_content = True self._extract_links(contents) f.seek(0) f.truncate() f.write(contents)
def create_database(database): db_config = Config.get('db') conn = MySQLdb.connect(user=db_config.get('user'), passwd=db_config.get('password'), host=db_config.get('host')) cursor = conn.cursor() # MySQL generates a warning if we try to create a database that already exists even # if we specify "IF NOT EXISTS" in the query. import warnings warnings.filterwarnings(action="ignore", category=MySQLdb.Warning, message="Can't create database '{}'; database exists".format(database)) cursor.execute('CREATE DATABASE IF NOT EXISTS {}'.format(database))
def __init__(self) -> None: if Mailer.__instance is not None: raise Exception("This class is a singleton!") Mailer.__instance = self self.init_ok: bool = False self.msg: EmailMessage = None self.smtp_host: str = Config.get('MAILER', 'smtp_host') self.smtp_port: str = Config.get('MAILER', 'smtp_port') self.smtp_user: str = Config.get('MAILER', 'smtp_user') self.smtp_password: str = Config.get('MAILER', 'smtp_password') _sender = Config.get('MAILER', 'sender') try: _receivers = Config.get('MAILER', 'receivers', param_type="json") except JSONDecodeError: Logger.get(self.__class__.__name__).warn('Problems decoding email receivers from JSON in config!') _receivers = None if not _sender or not _receivers or not self.smtp_host or not self.smtp_port: Logger.get(self.__class__.__name__).warn('Emails not being sent!') return self.msg = EmailMessage() self.msg.set_charset('utf-8') self.msg['From'] = _sender self.msg['To'] = _receivers self.init_ok = True
def send(cls, content: AbstractMailTemplate, subject: str = None) -> None: if Mailer.__instance is None: Mailer() if Mailer.__instance.init_ok: smtp = smtplib.SMTP_SSL("{}:{}".format(Mailer.__instance.smtp_host, Mailer.__instance.smtp_port)) try: smtp.login(Mailer.__instance.smtp_user, Mailer.__instance.smtp_password) except smtplib.SMTPAuthenticationError as e: Logger.get(cls.__name__).warn(str(e)) return Mailer.__instance.msg['Subject'] = subject if subject is not None else Config.get('MAILER', 'Subject') Mailer.__instance.msg.set_content(content.get()) try: smtp.send_message(Mailer.__instance.msg) except smtplib.SMTPDataError as e: Logger.get(cls.__name__).warn(str(e)) smtp.quit()
def __init__(self, name: str) -> None: if name in Logger.__instance: raise Exception("Logger '{}' already exists!".format(name)) log_handler = RotatingFileHandler(Config.get('LOGGER', 'log_file', '/dev/null'), mode='a', maxBytes=5 * 1024 * 1024, backupCount=2, encoding=None, delay=0) log_handler.setFormatter( logging.Formatter( '%(asctime)s <%(name)s> [%(levelname)s] %(message)s')) log_handler.setLevel(logging.INFO) Logger.__instance[name] = logging.getLogger(name) Logger.__instance[name].setLevel(logging.INFO) Logger.__instance[name].addHandler(log_handler)
from pyramid.config import Configurator from wsgiref.simple_server import make_server from app.api.core.routes import wire_routes from app.config import Config # Setup the api routes config = Configurator() wire_routes(config) config.scan() # Start the server api_config = Config.get('api') host = api_config.get('host') port = api_config.get('port') app = config.make_wsgi_app() server = make_server(host, port, app) print 'Started API on {}:{}'.format(host, port) server.serve_forever()
#!/usr/bin/env python # -*- coding: utf-8 -*- # vim:fileencoding=utf-8 import subprocess from app.config import Config commands = [ 'cd {}'.format(Config.get('CRON', 'project_root')), '. venv/bin/activate', './run.py' ] logging = '&>{}/{}'.format(Config.get('CRON', 'project_root'), Config.get('CRON', 'log_file')) cron_cmd = '(crontab -l 2>/dev/null ; echo "{} {} {}") | sort - | uniq - | crontab -'.format( Config.get('CRON', 'schedule'), " && ".join(commands), logging) subprocess.call(cron_cmd, shell=True)
from mysql import MySQL from app.config import Config # Create the database from the config MySQL.create_database(Config.get('db').get('database')) MySQL.run(''' CREATE TABLE IF NOT EXISTS user ( user_id INT(8) UNSIGNED, creation_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (user_id) ) ''') MySQL.run(''' CREATE TABLE IF NOT EXISTS user_device ( device_id INT(8) UNSIGNED, user_id INT(8) UNSIGNED, platform VARCHAR(255) CHARACTER SET utf8, system_version VARCHAR(255) CHARACTER SET utf8, app_version VARCHAR(255) CHARACTER SET utf8, push_token VARCHAR(255) CHARACTER SET utf8, creation_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP, last_seen TIMESTAMP, PRIMARY KEY (device_id) ) ''') MySQL.run('''
def get_connection(): if not MySQL._conn: db_config = Config.get('db') MySQL._conn = MySQLdb.connect(user=db_config.get('user'), passwd=db_config.get('password'), host=db_config.get('host'), db=db_config.get('database')) return MySQL._conn
def get_config_value(KEY): if not (Config.get(KEY)): value = input('%s: ' % KEY) Config.set(KEY, value) return Config.get(KEY)
from app.api.core.utils import api from app.config import Config from app.db.user_address import UserAddress from app.exception import MyBitsException blockcypher_config = Config.get('blockcypher') api_key = blockcypher_config.get('api_key') @api(enforce_user=True) def add(context, request, api_version, user, address=None): if not address: raise MyBitsException('Parameter `address` missing') # Store the user address in DB UserAddress.add(user.user_id, address, 'XPUB')