def compile(file): log = logger.get('SCSS') p = path.splitext(file)[0] if not path.exists('web/css'): mkdir('web/css') css_file = path.join('web/css', p + '.css') scss_file = path.join('web/scss', p + '.scss') c = False if path.exists(css_file): if path.getmtime(css_file) < path.getmtime(scss_file): c = True else: c = True if c: log.info(f'Recompiling {css_file}') s = None try: s = compile_file(scss_file) except scss.errors.SassError as e: log.error(f'SCSS error on file {scss_file}:') log.error(e) s = e.to_css() + 'body > * {visibility: hidden}' if s: s = rcssmin.cssmin(s) with open(css_file, 'w') as css: # log.info(s) css.write(s) else: log.info(f'{css_file} already compiled')
def __init__(self): config = setup.get_config()['db'] self.l = logger.get('DB') self.connection = None try: self.connection = mysql.connect(user=config['user'], password=config['password'], database='coderbrothers', autocommit=True, host=HOST) self.l.info('Connection started') except mysql.Error as err: if err.errno == mysql.errorcode.ER_ACCESS_DENIED_ERROR: self.l.critical( "Something is wrong with your user name or password") elif err.errno == mysql.errorcode.ER_BAD_DB_ERROR: self.l.critical("Database does not exist") else: self.l.critical(err) exit(1) if self.connection is None: self.l.critical('Couldn\'t connect to the database') exit(1) atexit.register(self.close)
def utility_functions(): log = liblogger.get('TEMPLATE') def printconsole(message): log.info(str(message)) return dict(console=printconsole)
def setup(): global config global settingUp global hasSetUp global rsa_pub global rsa_priv if not settingUp: start_time = time.time() settingUp = True delete_users = False logger.setup() log = logger.get('Setup') log.info('setup called') if not os.path.lexists('web/post'): os.mkdir('web/post') # Config loading / writing exiting = False try: with open('config.json') as f: log.info('Loading config') try: fixed_json = json.load(f) if 'salt' in fixed_json: fixed_json['salt'] = b64decode(fixed_json['salt'].encode()) # Decode bytes object if len(fixed_json['salt']) == len(config['salt']): log.info('Saving salt') config['salt'] = fixed_json['salt'] else: log.warning('Changing salt, old passwords WON\'T WORK') delete_users = True if 'version' not in fixed_json: log.warning('No config version') raise OldConfigError() elif fixed_json['version'] != config['version']: log.warning('Old config') raise OldConfigError() # print('version' in fixed_json['version']) if 'db' not in fixed_json: log.critical('Wrong config') exiting = True elif 'version' not in fixed_json['db']: log.critical('No DB version') exiting = True elif fixed_json['db']['version'] != config['db']['version']: log.critical('Wrong DB version') exiting = True if not exiting: config = fixed_json except json.decoder.JSONDecodeError: raise OldConfigError() except FileNotFoundError: with open('config.json', 'x') as f: newConfig(f, log) except OldConfigError: with open('config.json', 'w') as f: newConfig(f, log) if exiting: with open('config.json', 'w') as f: newConfig(f, log) log.critical('Please re-setup the DB (cat setup.sql | mysql -uroot -p)') log.critical('Now end this process with ctrl-c') time.sleep(1000000) hasSetUp = True if delete_users: db.getDB().deleteUsers() log.info("Checking for RSA keys") Path("certificates").mkdir(exist_ok=True) try: with open('certificates/private.pem', 'rb') as f: log.info('Loading private key') b = f.read() rsa_priv = rsa.PrivateKey.load_pkcs1(b) # load certs with open('certificates/public.pem', 'rb') as f: log.info('Loading public key') b = f.read() rsa_pub = rsa.PublicKey.load_pkcs1(b) # load certs except FileNotFoundError: # both certs have to be generated at the same time log.info("RSA keys not found. Generating new ones") (pub_pem, priv_pem) = gen_keys() # gen keys automatically saves them to the global vars with open('certificates/private.pem', 'wb') as f: f.write(priv_pem) with open('certificates/public.pem', 'wb') as f: f.write(pub_pem) elapsed = round(time.time()-start_time,2) log.info(f'Setup done in {elapsed}s') logger.remove(log) else: while not hasSetUp: time.sleep(1) return config
import os import jinja2 from python import logger, utils from python.db import getDB l = logger.get('BLOG') def get_posts(): posts = [] for post in getDB().getPosts(): date = post['date'] post['date'] = utils.formatPostDate(date) posts.append(post) return posts def get_posts_paged(page, posts_per_page): posts = [] for post in getDB().getPostsPaged(page, posts_per_page): date = post['date'] post['date'] = utils.formatPostDate(date) posts.append(post) return posts def search_posts_paged(search, page, posts_per_page): posts = [] for post in getDB().searchPostsPaged(search, page, posts_per_page): date = post['date'] post['date'] = utils.formatPostDate(date)
import sys print('\n' * 10) print('#' * 30) print(f'{e.name} missing!!! (Dependency)') print('Please install dependencies using') print( f' python{sys.version_info[0]}.{sys.version_info[1]} -m pip install -r requirements.txt # See README.md for more options' ) print('#' * 30) exit(1) app = Flask('CoderBrothers') app.config['SESSION_COOKIE_SAMESITE'] = "Strict" # app.config['SESSION_COOKIE_SECURE'] = True logger = liblogger.get('server') if __name__ == "__main__": logger.warning( 'PLEASE use flask run and do not run this file (do not do python3 server.py)' ) logger.warning( 'Setting env to development, but not starting debugger (autoreload)') app.env = 'development' if app.env == 'development' and not app.debug: # For developing. Disable chaching for non html files app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0 if environ.get("WERKZEUG_RUN_MAIN") == "true": setup( ) # Run this here so that config errors can be seen (don't run in prod)