Exemplo n.º 1
0
def create_app(package_name, package_path, settings_override=None,
               register_security_blueprint=True, config_name="development"):
    """Returns a :class:`Flask` application instance configured with common
    functionality for the chatfirst platform.
    :param package_name: application package name
    :param package_path: application package path
    :param settings_override: a dictionary of settings to override
    :param register_security_blueprint: flag to specify if the Flask-Security
                                        Blueprint should be registered. Defaults
                                        to `True`.
    """
    app = Flask(package_name, instance_relative_config=True)

    app.config.from_object('webpanda.settings')
    app.config.from_object(config[config_name])
    app.config.from_pyfile('settings.cfg', silent=True)
    app.config.from_object(settings_override)
    if 'SQLALCHEMY_DATABASE_URI' in os.environ.keys():
        app.config['SQLALCHEMY_DATABASE_URI'] = os.environ['DATABASE_URL']

    db.init_app(app)
    #security.init_app(app, SQLAlchemyUserDatastore(db, User, Role),
    #                  register_blueprint=register_security_blueprint)

    register_blueprints(app, package_name, package_path)

    app.wsgi_app = HTTPMethodOverrideMiddleware(app.wsgi_app)

    app.log = NrckiLogger().getLogger(package_name)

    # Prepare auth
    lm.init_app(app)
    lm.login_view = 'auth.main_auth'

    lm.anonymous_user = AnonymousUser

    @lm.user_loader
    def load_user(id):
        if id == 0:
            return AnonymousUser()
        return users_.get(id=id)

    @app.before_request
    def before_request():
        g.user = current_user
        g.user.last_seen = datetime.utcnow()
        g.user.save()

        values = request.form.to_dict()
        app.log.info("incoming request: {method} {url}; Form: {form}; Data: {data}".format(method=request.method,
                                                                                       url=request.full_path,
                                                                                       form=str(values), data=str(
                request.get_json(silent=True))))

    return app
Exemplo n.º 2
0
    def create_flask_application(self):
        application = Flask(__name__)
        application.log = {}
        application.killurl = str(uuid.uuid4())
        application.jinja_env.add_extension('jinja2.ext.do')

        @application.template_filter(
            'remove_terms', )
        def remove_terms(content, repository_configuration):
            """
            remove the blacklisted terms from the content
            :param content: the content to anonymize
            :param repository_configuration: the configuration of the repository
            :return: the anonimized content
            """
            repo = repository_configuration['repository']
            if repo[-1] == '/':
                repo = repo[0:-1]
            content = re.compile("%s/blob/master" % repo, re.IGNORECASE).sub(
                "%s/repository/%s" %
                (self.public_url, repository_configuration["id"]), content)
            content = re.compile(repo, re.IGNORECASE).sub(
                "%s/repository/%s" %
                (self.public_url, repository_configuration["id"]), content)
            for term in repository_configuration['terms']:
                content = re.compile(term, re.IGNORECASE).sub("XXX", content)
            return content

        @application.template_filter(
            'file_render', )
        def file_render(file, repository_configuration):
            """
            produce the html representation of a file
            :param file: the file to display
            :param repository_configuration: the configuration of the repository
            :return: the html representation of the file
            """
            if type(file) == github.Commit.Commit:
                return Markup(
                    remove_terms(render_template('patch.html', patch=file),
                                 repository_configuration))
            if file.type == 'dir':
                return ""
            if file.size > 1000000:
                return Markup(
                    "The file %s is too big to be anonymized (beyond 1MB, Github limit)"
                    % (file.name))
            if ".md" in file.name or file.name == file.name.upper(
            ) or "changelog" == file.name.lower():
                return Markup(
                    "<div class='markdown-body'>%s</div>" % remove_terms(
                        self.github.render_markdown(
                            file.decoded_content.decode('utf-8')).decode(
                                'utf-8'), repository_configuration))
            if ".jpg" in file.name or ".png" in file.name or ".png" in file.name or ".gif" in file.name:
                return Markup("<img src='%s' alt='%s'>" %
                              (file.url, file.name))
            if ".txt" in file.name \
                    or ".rtf" in file.name \
                    or ".log" in file.name \
                    or ".csv" in file.name \
                    or ".xml" in file.name \
                    or ".json" in file.name \
                    or ".css" in file.name \
                    or ".html" in file.name \
                    or ".js" in file.name \
                    or ".tex" in file.name \
                    or ".java" in file.name \
                    or ".php" in file.name \
                    or ".c" in file.name \
                    or ".h" in file.name \
                    or ".lua" in file.name \
                    or ".py" in file.name \
                    or ".sh" in file.name \
                    or ".gitignore" in file.name \
                    or ".travis.yml" in file.name:
                return Markup("<pre><code>{}</code></pre>")\
                           .format(Markup.escape(remove_terms(file.decoded_content.decode("utf-8"), repository_configuration)))
            return Markup(
                "<b>%s has an unknown extension, we are unable to anonymize it (known extensions md/txt/json/java/...)</b>"
                % (file.name))

        @application.route('/' + application.killurl, methods=['POST'])
        def seriouslykill():
            func = request.environ.get('werkzeug.server.shutdown')
            func()
            return "Shutting down..."

        def get_element_from_path(g_repo, g_commit, path):
            """
            get a github element from its path
            :param g_repo: the github repository
            :param path: the path of the element
            :return: the element
            """
            if path == '':
                return g_repo.get_contents('/', g_commit.sha)
            current_element = os.path.basename(path)
            folder_content = g_repo.get_contents(quote(os.path.dirname(path)),
                                                 g_commit.sha)
            for file in folder_content:
                if file.name == current_element:
                    return file
            return None

        @application.route('/repository/<id>/commit/<sha>', methods=['GET'])
        def commit(id, sha):
            """
            display anonymously a commit from the repository
            :param id: the repository id
            :param sha: the commit id
            """
            config_path = self.config_dir + "/" + str(id) + "/config.json"
            if not os.path.exists(config_path):
                return render_template('404.html'), 404
            with open(config_path) as f:
                data = json.load(f)
                (username, repo,
                 branch) = clean_github_repository(data['repository'])
                g_repo = self.github.get_repo("%s/%s" % (username, repo))
                commit = g_repo.get_commit(sha)
                return render_template('repo.html',
                                       repository=data,
                                       current_repository=id,
                                       current_file=commit,
                                       files=[],
                                       path=[])

        def is_up_to_date(repository_config, g_commit):
            """
            check is the cache is up to date
            :param repository_config: the repository configuration
            :param g_commit: the Github commit
            :return: True if the cache is up to date
            """
            commit_date = datetime.strptime(g_commit.last_modified,
                                            "%a, %d %b %Y %H:%M:%S %Z")
            return 'pushed_at' in repository_config and commit_date.strftime(
                "%s") == repository_config["pushed_at"]

        def get_type_content(file_name, path, repository_configuration,
                             g_repo):
            """
            Get the content type of a file from its extension
            :param file_name: the filename
            :param path: the path of the file
            :param repository_configuration: the repository configuration
            :param g_repo: the Github repository
            :return: the content type
            """
            if is_website(path, repository_configuration, g_repo):
                content_type = 'text/plain; charset=utf-8'
                if ".html" in file_name:
                    content_type = 'text/html; charset=utf-8'
                if ".md" in file_name or file.name == file.name.upper():
                    content_type = 'text/html; charset=utf-8'
                if ".jpg" in file_name \
                        or ".png" in file_name \
                        or ".gif" in file_name:
                    content_type = 'image/jpeg'
                    if ".png" in file_name:
                        content_type = 'image/png'
                    elif ".gif" in file_name:
                        content_type = 'image/gif'
                if ".txt" in file_name \
                        or ".log" in file_name \
                        or ".csv" in file_name \
                        or ".xml" in file_name \
                        or ".json" in file_name \
                        or ".java" in file_name \
                        or ".py" in file_name \
                        or ".lua" in file_name \
                        or ".js" in file_name:
                    content_type = 'text/plain; charset=utf-8'
                    if ".xml" in file_name:
                        content_type = 'application/xml; charset=utf-8'
                    elif ".json" in file_name:
                        content_type = 'application/json; charset=utf-8'
                    elif ".js" in file_name:
                        content_type = 'application/javascript; charset=utf-8'
                if ".css" in file_name:
                    content_type = 'text/css; charset=utf-8'
                return content_type
            return 'text/html; charset=utf-8'

        def get_content(current_file, files, path, repository_config, g_repo):
            """
            get the content if the page
            :param current_file: the current file
            :param files: the list of file of the current directory
            :param path: the accessed path
            :param repository_config: the repository configuration
            :param g_repo: the Github repository
            :return: the content of the page
            """
            cache_path = os.path.join(self.config_dir, repository_config['id'],
                                      "cache")
            file_path = path
            if current_file is not None:
                if current_file.type == 'dir':
                    file_path = os.path.join(current_file.path, "index.html")
                else:
                    file_path = current_file.path
            cached_file_path = os.path.join(cache_path, file_path)
            if os.path.exists(cached_file_path):
                return send_from_directory(os.path.dirname(cached_file_path),
                                           os.path.basename(cached_file_path),
                                           mimetype=get_type_content(
                                               path, path, repository_config,
                                               g_repo).replace(
                                                   "; charset=utf-8", ""))
            content = ''
            if is_website(path, repository_config, g_repo):
                if current_file.size > 1000000:
                    blob = g_repo.get_git_blob(current_file.sha)
                    if blob.encoding == 'base64':
                        content = base64.b64decode(
                            blob.content).decode('utf-8')
                    else:
                        content = blob.content.decode('utf-8')
                else:
                    content = current_file.decoded_content.decode('utf-8')
                if ".html" in current_file.name \
                        or ".txt" in current_file.name \
                        or ".log" in current_file.name \
                        or ".java" in current_file.name \
                        or ".py" in current_file.name \
                        or ".xml" in current_file.name \
                        or ".json" in current_file.name \
                        or ".js" in current_file.name:
                    content = remove_terms(content, repository_config)
                if ".md" in current_file.name:
                    content = remove_terms(
                        self.github.render_markdown(content).decode('utf-8'),
                        repository_config)
            else:
                content = render_template(
                    'repo.html',
                    repository=repository_config,
                    current_repository=repository_config['id'],
                    current_file=current_file,
                    files=files.tree,
                    path_directory=path
                    if type(current_file) is not github.ContentFile.ContentFile
                    or current_file.type == 'dir' else os.path.dirname(
                        current_file.path),
                    path=path.split("/") if path != '' else [])
            content_cache_path = cached_file_path
            if not os.path.exists(os.path.dirname(content_cache_path)):
                os.makedirs(os.path.dirname(content_cache_path))
            with open(content_cache_path, 'w') as f:
                f.write(content.encode('utf8'))
            return content

        def is_website(path, repository_config, g_repo):
            """
            Check if the current request is a request to a GitHub pages
            :param path: the current path
            :param repository_config: the repository configuration
            :param g_repo: the Github repository
            :return: True if the current path is a website
            """
            return path[:4] == "docs"

        def is_default_file(f):
            default_name = ["readme", "index"]
            for name in default_name:
                try:
                    if type(f) is github.ContentFile.ContentFile:
                        f.name.lower().index(name)
                    elif type(f) is github.GitTreeElement.GitTreeElement:
                        f.path.lower().index(name)
                    return True
                except ValueError:
                    continue
            return False

        def get_current_folder_files(path, current_file, repository_config,
                                     g_repo, g_commit):
            """
            get the list of files of the current repository
            :param path: the path to the current file
            :param current_file: the current file
            :param repository_config: the repository configuration
            :param g_repo: the GitHub repository
            :return: the list of file of the current repository
            """
            files = []
            if current_file is None:
                return files, current_file
            if type(current_file) is not github.ContentFile.ContentFile:
                files = g_repo.get_git_tree(g_commit.sha)
                for f in current_file:
                    if is_default_file(f):
                        current_file = f
                        break
                if type(current_file) is not github.ContentFile.ContentFile:
                    current_file = current_file[0]
            elif current_file.type == 'file':
                if os.path.dirname(path) == '':
                    files = g_repo.get_git_tree(g_commit.sha)
                else:
                    files = g_repo.get_git_tree(
                        get_element_from_path(g_repo, g_commit,
                                              os.path.dirname(path)).sha)
            else:
                files = g_repo.get_git_tree(current_file.sha)
                for f in files.tree:
                    if is_default_file(f):
                        current_file = get_element_from_path(
                            g_repo, g_commit, os.path.join(path, f.path))
                        break
                if len(files.tree) == 1 and type(
                        files.tree[0]) is github.ContentFile.ContentFile:
                    current_file = get_element_from_path(
                        g_repo, g_commit, os.path.join(path,
                                                       files.tree[0].path))
            return files, current_file

        @application.route('/repository/<id>',
                           methods=['GET'],
                           defaults={'path': ''})
        @application.route('/repository/<id>/',
                           methods=['GET'],
                           defaults={'path': ''})
        @application.route('/repository/<id>/<path:path>', methods=['GET'])
        def repository(id, path):
            repo_path = self.config_dir + "/" + str(id)
            config_path = repo_path + "/config.json"
            if not os.path.exists(config_path):
                return render_template('404.html'), 404
            with open(config_path, 'r') as f:
                repository_configuration = json.load(f)
                (username, repo, branch) = clean_github_repository(
                    repository_configuration['repository'])
                g_repo = self.github.get_repo("%s/%s" % (username, repo))
                g_commit = g_repo.get_commit(branch)

                if not is_up_to_date(repository_configuration, g_commit):
                    if os.path.exists(os.path.join(repo_path, "cache")):
                        shutil.rmtree(os.path.join(repo_path, "cache"))
                    commit_date = datetime.strptime(
                        g_commit.last_modified, "%a, %d %b %Y %H:%M:%S %Z")
                    repository_configuration[
                        "pushed_at"] = commit_date.strftime("%s")
                    with open(config_path, 'w') as fa:
                        json.dump(repository_configuration, fa)

                cache_path = os.path.join(self.config_dir, id, "cache")
                if os.path.isfile(os.path.join(cache_path, path)):
                    return send_from_directory(
                        os.path.dirname(os.path.join(cache_path, path)),
                        os.path.basename(os.path.join(cache_path, path)),
                        mimetype=get_type_content(path, path,
                                                  repository_configuration,
                                                  g_repo).replace(
                                                      "; charset=utf-8", "")),
                elif os.path.exists(
                        os.path.join(cache_path, path, "index.html")):
                    return send_from_directory(os.path.join(cache_path, path),
                                               "index.html",
                                               mimetype='text/html')
                elif os.path.exists(os.path.join(cache_path, path,
                                                 "README.md")):
                    return send_from_directory(os.path.join(cache_path, path),
                                               "README.md",
                                               mimetype='text/html')

                clean_path = path
                if len(clean_path) > 0 and clean_path[-1] == '/':
                    clean_path = clean_path[0:-1]

                current_file = get_element_from_path(g_repo, g_commit,
                                                     clean_path)
                if current_file is None:
                    return render_template('404.html'), 404
                if type(
                        current_file
                ) == github.ContentFile.ContentFile and current_file.type == 'dir' and len(
                        path) > 0 and path[-1] != '/':
                    return redirect(
                        url_for('repository', id=id, path=path + '/'))

                files, current_file = get_current_folder_files(
                    clean_path, current_file, repository_configuration, g_repo,
                    g_commit)

                content = get_content(current_file, files, clean_path,
                                      repository_configuration, g_repo)
                content_type = get_type_content(current_file.name, clean_path,
                                                repository_configuration,
                                                g_repo)
                return content, {'Content-Type': content_type}

        @application.route('/', methods=['GET'])
        def index():
            id = request.args.get('id', None)
            repo_name = clean_github_repository(
                request.args.get('githubRepository', None))
            repo = None
            if id is not None:
                config_path = self.config_dir + "/" + id + "/config.json"
                if os.path.exists(config_path):
                    with open(config_path) as f:
                        data = json.load(f)
                        if repo_name == clean_github_repository(
                                data['repository']):
                            repo = data

            return render_template('index.html', repo=repo)

        @application.route('/', methods=['POST'])
        def add_repository():
            id = request.args.get('id', str(uuid.uuid4()))
            repo = request.form['githubRepository']
            terms = request.form['terms']

            config_path = self.config_dir + "/" + str(id)
            if not os.path.exists(config_path):
                os.mkdir(config_path)
            with open(config_path + "/config.json", 'w') as outfile:
                json.dump(
                    {
                        "id": id,
                        "repository": repo,
                        "terms": terms.splitlines()
                    }, outfile)
            return redirect(url_for('repository', id=id))

        return application
Exemplo n.º 3
0
from flask import Flask, Response, request
from flask.ext.httpauth import HTTPBasicAuth
from logbook import Logger
import json
import os.path

app = Flask(__name__)
auth = HTTPBasicAuth()
app.log = Logger(__name__)

user_db = json.load(
    open(os.path.expanduser("~/.doorbot_users"), 'r')
)
config_settings = json.load(
    open(os.path.expanduser("~/.doorbot_config"), 'r')
)

@auth.get_password
def get_pw(username):
    if username in user_db:
        return user_db.get(username)
    return None

import interfaces
app.doors = {
    door_config['doorid']:interfaces.pick(
        door_config['interface']
    )(**door_config) for door_config in config_settings['doors']
}

app.log.debug("Got Doors: {} from Config {}".format(app.doors, config_settings))
Exemplo n.º 4
0
    from .image_manager import ImageManager
    from .log_manager import LogManager
    from .permissions_manager import PermissionsManager
    from .stats_manager import StatsManager
    from .task_manager import TaskManager

    logger = None
    try:
        # Create the logging client+server
        logger = LogManager(
            __about__.__tag__.lower() + '_' + str(os.getpid()),
            app.config['DEBUG'],
            app.config['LOGGING_SERVER'],
            app.config['LOGGING_SERVER_PORT']
        )
        app.log = logger
        LogManager.run_server(
            app.config['LOGGING_SERVER'],
            app.config['LOGGING_SERVER_PORT'],
            __about__.__tag__.lower() + '.log',
            app.config['DEBUG']
        )
        # Capture Flask's internal logging
        app.logger.addHandler(logger.logging_handler)

        # Announce startup
        logger.info(__about__.__title__ + ' v' + __about__.__version__ + ' engine startup')
        logger.info('Using settings ' + app.config['_SETTINGS_IN_USE'])
        if app.config['DEBUG']:
            logger.info('*** Debug mode ENABLED ***')
        if app.config['BENCHMARKING']:
Exemplo n.º 5
0
app = Flask(__name__)
app.TZCO = 30
app.TZM = 100
app.TZCO0 = 30
app.FZM = 100
app.TPM = 100
app.TPCO = 30
app.TPCO0 = 30
app.FZCO = 150
app.value = 1
app.simthread = None
app.sendthread = None
app.value1 = 1
app.timeout = 1.5
app.time = 0
app.log = True
app.test = False
app.simbud = False
app.speed = True
app.mulbydif = 2000
app.i = 0
app.Tr = 20
print(['Tryb testowy', app.test])
print(['SimBud', app.simbud])
print(['speed', app.mulbydif])
import KPSSw0.views


def sim():
    while True:
        app.i = app.i + 1
Exemplo n.º 6
0
from flask_mwoauth import MWOAuth
from authlib.flask.client import OAuth
from loginpass import create_flask_blueprint, Gitlab, GitHub, ORCiD
from raven.contrib.flask import Sentry
import fatcat_openapi_client

from fatcat_web.web_config import Config


toolbar = DebugToolbarExtension()
app = Flask(__name__, static_url_path='/static')
app.config.from_object(Config)
toolbar = DebugToolbarExtension(app)
FlaskUUID(app)
app.csrf = CSRFProtect(app)
app.log = create_logger(app)

# This is the Markdown processor; setting default here
Misaka(app,
    autolink=True,
    no_intra_emphasis=True,
    strikethrough=True,
    escape=True,
)

login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = "/auth/login"
oauth = OAuth(app)

# Grabs sentry config from SENTRY_DSN environment variable
Exemplo n.º 7
0
logging.basicConfig(
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    level=logging.DEBUG)


def get_app_base_path():
    return os.path.dirname(os.path.realpath(__file__))


app = Flask(__name__,
            instance_path=get_app_base_path(),
            instance_relative_config=True)

app.config['JWT_SECRET_KEY'] = 'AB73WK2345BKKSWNPK1349'

app.log = logging


def exception_handler(func):
    def wrapper(*args, **kwargs):
        try:
            app.log.info(f'Try use {func.__name__}')
            return func(*args, **kwargs)
        except Exception as e:
            app.log.error(f'Error in function {func.__name__}, error: {e}')
            return make_response(jsonify({'error': f'Error, {e}'}), 500)

    wrapper.__name__ = func.__name__
    return wrapper

Exemplo n.º 8
0
from flask import jsonify, redirect, url_for, escape
from flask import request, session
from flask import g as Globals
from flask.ext.socketio import SocketIO, emit, join_room, leave_room
import logging

logging.basicConfig()

app = Flask(__name__)
# app.config['SECRET_KEY'] = 'secret!'
app.secret_key = "A0Zr98j/3yX R~XHH!jmN]LWX/,?RT"

socketio = SocketIO(app)

app.artist = 0
app.log = {}


@app.route("/<path:path>")
def static_proxy(path):
    return app.send_static_file(path)


@app.route("/draw/<room>")
def drawing_room(room):
    session["artist"] = str(app.artist)
    session["room"] = room
    app.artist += 1

    return app.send_static_file("draw.html")
Exemplo n.º 9
0
from flask import Flask
from flask_jwt import JWT
from flask_migrate import Migrate
from flask_sqlalchemy import SQLAlchemy
import logging
from app.config.config_handler import ConfigHandler

# get App instance
app = Flask(__name__)

# Set logger
app.log = logging.getLogger('ParkingApp')

# Load config into app object
ConfigHandler(app)

# Initialize DB instance
db = SQLAlchemy(app)

# Initialize Migrate instance for tracking model changes
Migrate(app, db)

from app.controllers import authentication

# importing the models to make sure they are known to Flask-Migrate
from app.models import user, parking_area, parking_slot, booking, role

# import apis
from app.apis import user_apis, parking_area_apis, parking_slot_apis, admin_apis
Exemplo n.º 10
0
		req_json = request.get_json()

		if req_json is None:

			return jsonify( error = 'this service require A JSON request' )

		else:
			if not ('text' in req_json):
				raise Exception('Missing mandatory paramater "text"')

			text = req_json['text']
			blob = TextBlob(text)
			sentiment = blob.sentiment

			return jsonify( polarity = sentiment.polarity, subjectivity = sentiment.subjectivity)

	except Exception as ex:
		app.log.error(type(ex))
		app.log.error(ex.args)
		app.log.error(ex)
		return jsonify(error = str(ex))

if __name__ == '__main__':

	LOG_FORMAT = "'%(asctime)s - %(name)s - %(levelname)s - %(message)s'"
  	logging.basicConfig(level=logging.DEBUG,format=LOG_FORMAT)
	app.log = logging.getLogger(__name__)

	port = os.environ['FLASK_PORT']
	app.run(host='0.0.0.0',port=int(port),debug=False)
Exemplo n.º 11
0
app = Flask('strike')
app.config['SECRET_KEY'] = 'Q3JlYXRlZCBieTogSHVudGVyIFBlYXJzb24='
app.debug = True
# Application configs
app.config['LOG_PATH'] = os.getcwd() + '\\strike\\log\\'
app.config['STORAGE'] = os.getcwd() + '\\strike\\storage\\'
app.config['CRED_STORAGE'] = os.getcwd() + '\\strike\\models\\'
# Database configs

# Dump in configurations
# Set flask logger to only show errors
log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)

# Setup for custom logger
app.log = logging.getLogger('Strike-from-the-skies')
logging.basicConfig(
    filename=app.config['LOG_PATH'] + datetime.now().strftime("%m.%d.%Y") +
    '.log',
    filemode='w',
    level=logging.INFO,
    format='%(asctime)s | %(levelname)-10s | %(name)s | %(message)s',
    datefmt="%m/%d/%Y %I:%M %p")

# Flask setup settings (Debug)
toolbar = DebugToolbarExtension(app)
app.config['DEBUG_TB_INTERCEPT_REDIRECTS'] = False
app.config['DEBUG_TB_PANELS'] = [
    'flask_debugtoolbar.panels.versions.VersionDebugPanel',
    'flask_debugtoolbar.panels.route_list.RouteListDebugPanel',
    'flask_debugtoolbar.panels.headers.HeaderDebugPanel',
Exemplo n.º 12
0
    def create_flask_application(self):
        application = Flask(__name__)
        gzip = Gzip(application)
        application.log = {}
        application.killurl = str(uuid.uuid4())
        application.jinja_env.add_extension('jinja2.ext.do')

        application.config.update(
            SESSION_TYPE='filesystem',
            PERMANENT_SESSION_LIFETIME=60*15, # 15 min
            SECRET_KEY=self.secret_key,
            GITHUB_CLIENT_ID=self.client_id,
            GITHUB_CLIENT_SECRET=self.client_secret,
            GITHUB_CLIENT_KWARGS = {
                'scope': 'repo'
            }
        )
        Session(application)
        oauth = OAuth(application)
        github_bp = create_flask_blueprint(GitHub, oauth, handle_authorize)
        application.register_blueprint(github_bp, url_prefix='/github')

        @application.template_filter('remove_terms', )
        def remove_terms(content, repository_configuration, word_boundaries=True, whole_urls=True):
            """
            remove the blacklisted terms from the content
            :param content: the content to anonymize
            :param repository_configuration: the configuration of the repository
            :return: the anonymized content
            """
            repo = repository_configuration['repository']
            if repo[-1] == '/':
                repo = repo[0:-1]
            content = re.compile("%s/blob/master" % repo, re.IGNORECASE).sub(
                "%s/repository/%s" % (self.public_url, repository_configuration["id"]), content)
            content = re.compile(repo, re.IGNORECASE).sub("%s/repository/%s" % (self.public_url, repository_configuration["id"]), content)
            for term in repository_configuration['terms']:
                if word_boundaries:
                    regex = re.compile(r'\b%s\b' % term, re.IGNORECASE)
                else:
                    regex = re.compile(term, re.IGNORECASE)

                if whole_urls:
                    def sub_url(m):
                        if regex.search(m.group(0)):
                            return 'XXX'
                        return m.group(0)

                    url_regex = re.compile('\\b((https?|ftp|file)://)[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]\\b')
                    content = url_regex.sub(sub_url, content)

                content = regex.sub("XXX", content)
            return content

        @application.template_filter('file_render', )
        def file_render(file, repository_configuration):
            """
            produce the html representation of a file
            :param file: the file to display
            :param repository_configuration: the configuration of the repository
            :return: the html representation of the file
            """
            if type(file) == github.Commit.Commit:
                return Markup(remove_terms(render_template('patch.html', patch=file), repository_configuration))
            if file.type == 'dir':
                return ""
            if file.size > 1000000:
                return Markup("The file %s is too big to be anonymized (beyond 1MB, Github limit)" % (file.name))
            if ".md" in file.name or file.name == file.name.upper() or "changelog" == file.name.lower():
                gh = self.github
                if 'token' in repository_configuration and repository_configuration['token'] is not None:
                    gh = github.Github(repository_configuration['token'])
                return Markup("<div class='markdown-body'>%s</div>" % remove_terms(
                    gh.render_markdown(file.decoded_content.decode('utf-8')),
                    repository_configuration))
            if ".jpg" in file.name or ".png" in file.name or ".png" in file.name or ".gif" in file.name:
                index = file.name.index('.')
                file_extension = file.name[index + 1:]
                return Markup("<img src='data:image/%s;base64, %s' alt='%s'>" % (file_extension, file.content, file.name))
            if istext(file.decoded_content):
                return Markup("<pre><code>{}</code></pre>")\
                           .format(Markup.escape(remove_terms(file.decoded_content.decode("utf-8"), repository_configuration)))
            return Markup("<b>%s has an unknown extension, we are unable to anonymize it (known extensions md/txt/json/java/...)</b>" % (file.name))

        @application.route('/' + application.killurl, methods=['POST'])
        def seriouslykill():
            func = request.environ.get('werkzeug.server.shutdown')
            func()
            return "Shutting down..."

        def get_element_from_path(g_repo, g_commit, path):
            """
            get a github element from its path
            :param g_repo: the github repository
            :param path: the path of the element
            :return: the element
            """
            if path == '':
                return g_repo.get_contents('', g_commit.sha), None
            current_element = os.path.basename(path)
            folder_content = g_repo.get_contents(quote(os.path.dirname(path)), g_commit.sha)
            for file in folder_content:
                if file.name == current_element:
                    return file, folder_content
            return None, folder_content

        @application.route('/myrepo', methods=['GET'])
        def myrepo():
            user = session.get('user', None)
            if user is None or 'token' not in user or user['token'] is None:
                return redirect('github/login')
            g = github.Github(user['token']['access_token'])
            repos = g.get_user().get_repos(sort="full_name")
            for repo in repos:
                repo.uuid = str(uuid.uuid4())
            return render_template('newrepo.html', repos=repos)
            

        @application.route('/repository/<id>/commit/<sha>', methods=['GET'])
        def commit(id, sha):
            """
            display anonymously a commit from the repository
            :param id: the repository id
            :param sha: the commit id
            """
            config_path = self.config_dir + "/" + str(id) + "/config.json"
            if not os.path.exists(config_path):
                return render_template('404.html'), 404
            with open(config_path) as f:
                data = json.load(f, object_hook=json_util.object_hook)
                (username, repo, branch) = clean_github_repository(data['repository'])
                gh = self.github
                if 'token' in data:
                    gh = github.Github(data['token'])
                g_repo = gh.get_repo("%s/%s" % (username, repo))
                commit = g_repo.get_commit(sha)
                return render_template('repo.html',
                                   repository=data,
                                   current_repository=id,
                                   current_file=commit,
                                   files=[],
                                   path=[])

        def is_up_to_date(repository_config, g_commit):
            """
            check is the cache is up to date
            :param repository_config: the repository configuration
            :param g_commit: the Github commit
            :return: True if the cache is up to date
            """
            commit_date = datetime.strptime(g_commit.last_modified, "%a, %d %b %Y %H:%M:%S %Z")
            return 'pushed_at' in repository_config and commit_date.strftime("%s") == repository_config["pushed_at"]

        def get_type_content(file_name, path, repository_configuration, g_repo, is_website):
            """
            Get the content type of a file from its extension
            :param file_name: the filename
            :param path: the path of the file
            :param repository_configuration: the repository configuration
            :param g_repo: the Github repository
            :return: the content type
            """
            if is_website:
                content_type = 'text/plain; charset=utf-8'
                if ".html" in file_name:
                    content_type = 'text/html; charset=utf-8'
                if ".md" in file_name or file_name == file_name.upper():
                    content_type = 'text/html; charset=utf-8'
                if ".jpg" in file_name \
                        or ".png" in file_name \
                        or ".gif" in file_name:
                    content_type = 'image/jpeg'
                    if ".png" in file_name:
                        content_type = 'image/png'
                    elif ".gif" in file_name:
                        content_type = 'image/gif'
                if ".txt" in file_name \
                        or ".log" in file_name \
                        or ".csv" in file_name \
                        or ".xml" in file_name \
                        or ".json" in file_name \
                        or ".java" in file_name \
                        or ".py" in file_name \
                        or ".lua" in file_name \
                        or ".js" in file_name:
                    content_type = 'text/plain; charset=utf-8'
                    if ".xml" in file_name:
                        content_type = 'application/xml; charset=utf-8'
                    elif ".json" in file_name:
                        content_type = 'application/json; charset=utf-8'
                    elif ".js" in file_name:
                        content_type = 'application/javascript; charset=utf-8'
                if ".css" in file_name:
                    content_type = 'text/css; charset=utf-8'
                return content_type
            return 'text/html; charset=utf-8'

        def get_content(current_file, files, path, repository_config, g_repo):
            """
            get the content if the page
            :param current_file: the current file
            :param files: the list of file of the current directory
            :param path: the accessed path
            :param repository_config: the repository configuration
            :param g_repo: the Github repository
            :return: the content of the page
            """
            cache_path = os.path.join(self.config_dir, repository_config['id'], "cache")
            file_path = path
            if current_file is not None:
                if current_file.type == 'dir':
                    file_path = os.path.join(current_file.path, "index.html")
                else:
                    file_path = current_file.path
            cached_file_path = os.path.join(cache_path, file_path)
            content_type = get_type_content(path, path, repository_config, g_repo, False).replace("; charset=utf-8", "")
            if os.path.exists(cached_file_path):
                return send_from_directory(os.path.dirname(cached_file_path), os.path.basename(cached_file_path),
                                           mimetype=content_type)
            content = ''
            if current_file.type != 'dir' and is_website(path, repository_config, g_repo):
                if current_file.size > 1000000:
                    blob = g_repo.get_git_blob(current_file.sha)
                    if blob.encoding == 'base64':
                        content = base64.b64decode(blob.content).decode('utf-8')
                    else:
                        content = blob.content.decode('utf-8')
                else:
                    content = current_file.decoded_content.decode('utf-8')
                if "text" in content_type:
                    content = remove_terms(content, repository_config)
                if ".md" in current_file.name:
                    gh = self.github
                    if 'token' in repository_config:
                        gh = github.Github(repository_config['token'])
                    content = remove_terms(gh.render_markdown(content), repository_config)
            else:
                tree = files 
                if type(tree) != list:
                    tree = files.tree
                content = render_template('repo.html',
                                       repository=repository_config,
                                       current_repository=repository_config['id'],
                                       current_file=current_file,
                                       files=tree,
                                       path_directory=path if type(
                                           current_file) is not github.ContentFile.ContentFile or current_file.type == 'dir' else os.path.dirname(
                                           current_file.path),
                                       path=path.split("/") if path != '' else [])
            content_cache_path = cached_file_path
            if not os.path.exists(os.path.dirname(content_cache_path)):
                os.makedirs(os.path.dirname(content_cache_path))
            with open(content_cache_path, 'w') as f:
                if type(content) == str:
                    f.write(content)
                else:
                    f.write(content.encode('utf8'))
            return content

        def is_website(path, repository_config, g_repo):
            """
            Check if the current request is a request to a GitHub pages
            :param path: the current path
            :param repository_config: the repository configuration
            :param g_repo: the Github repository
            :return: True if the current path is a website
            """
            return path[:4] == "docs"

        def is_default_file(f):
            default_name = ["readme", "index"]
            for name in default_name:
                try:
                    if type(f) is github.ContentFile.ContentFile:
                        f.name.lower().index(name)
                    elif type(f) is github.GitTreeElement.GitTreeElement:
                        f.path.lower().index(name)
                    return True
                except ValueError:
                    continue
            return False

        def get_current_folder_files(path, current_file, repository_config, g_repo, g_commit):
            """
            get the list of files of the current repository
            :param path: the path to the current file
            :param current_file: the current file
            :param repository_config: the repository configuration
            :param g_repo: the GitHub repository
            :return: the list of file of the current repository
            """
            files = []
            if current_file is None:
                return files, current_file
            if type(current_file) is not github.ContentFile.ContentFile:
                files = g_repo.get_git_tree(g_commit.sha)
                for f in current_file:
                    if is_default_file(f):
                        current_file = f
                        break
                if type(current_file) is not github.ContentFile.ContentFile:
                    current_file = current_file[0]
            elif current_file.type == 'file':
                if os.path.dirname(path) == '':
                    files = g_repo.get_git_tree(g_commit.sha)
                else:
                    f, folder = get_element_from_path(g_repo, g_commit, os.path.dirname(path))
                    if f is None:
                        files = folder
                    else:
                        files = g_repo.get_git_tree(f.sha)
            else:
                files = g_repo.get_git_tree(current_file.sha)
                for f in files.tree:
                    if is_default_file(f):
                        current_file, folder = get_element_from_path(g_repo, g_commit, os.path.join(path, f.path))
                        break
                if len(files.tree) == 1 and type(files.tree[0]) is github.ContentFile.ContentFile:
                    current_file, folder = get_element_from_path(g_repo, g_commit, os.path.join(path, files.tree[0].path))
            return files, current_file

        @application.route('/repository/<id>', methods=['GET'], defaults={'path': ''})
        @application.route('/repository/<id>/', methods=['GET'], defaults={'path': ''})
        @application.route('/repository/<id>/<path:path>', methods=['GET'])
        @application.route('/r/<id>', methods=['GET'], defaults={'path': ''})
        @application.route('/r/<id>/', methods=['GET'], defaults={'path': ''})
        @application.route('/r/<id>/<path:path>', methods=['GET'])
        def repository(id, path):
            repo_path = self.config_dir + "/" + str(id)
            config_path = repo_path + "/config.json"
            if not os.path.exists(config_path):
                return render_template('404.html'), 404
            with open(config_path, 'r') as f:
                repository_configuration = json.load(f, object_hook=json_util.object_hook)
                if 'expiration_date' in repository_configuration and repository_configuration['expiration_date'] is not None:
                    if repository_configuration['expiration_date'] <= datetime.now(repository_configuration['expiration_date'].tzinfo):
                        if repository_configuration['expiration'] == 'redirect':
                            return redirect(repository_configuration['repository'])
                        elif repository_configuration['expiration'] == 'remove':
                            return render_template('404.html'), 404
                (username, repo, branch) = clean_github_repository(repository_configuration['repository'])
                gh = self.github
                if 'token' in repository_configuration and repository_configuration['token'] is not None:
                    gh = github.Github(repository_configuration['token'])
                g_commit = None
                try:
                    g_repo = gh.get_repo("%s/%s" % (username, repo))
                    if branch is None:
                        branch = g_repo.default_branch
                    g_commit = g_repo.get_commit(branch)
                except:
                    return render_template('empty.html'), 404

                if not is_up_to_date(repository_configuration, g_commit):
                    if os.path.exists(os.path.join(repo_path, "cache")):
                        shutil.rmtree(os.path.join(repo_path, "cache"))
                    commit_date = datetime.strptime(g_commit.last_modified, "%a, %d %b %Y %H:%M:%S %Z")
                    repository_configuration["pushed_at"] = commit_date.strftime("%s")
                    with open(config_path, 'w') as fa:
                        json.dump(repository_configuration, fa, default=json_util.default)

                cache_path = os.path.join(self.config_dir, id, "cache")
                if os.path.isfile(os.path.join(cache_path, path)):
                    return send_from_directory(os.path.dirname(os.path.join(cache_path, path)),
                                               os.path.basename(os.path.join(cache_path, path)),
                                               mimetype=get_type_content(path, path, repository_configuration, g_repo, is_website(path, repository_configuration, g_repo)).replace("; charset=utf-8", ""))
                elif os.path.exists(os.path.join(cache_path, path, "index.html")):
                    return send_from_directory(os.path.join(cache_path, path), "index.html", mimetype='text/html')
                elif os.path.exists(os.path.join(cache_path, path, "README.md")):
                    return send_from_directory(os.path.join(cache_path, path), "README.md", mimetype='text/html')

                clean_path = path
                if len(clean_path) > 0 and clean_path[-1] == '/':
                    clean_path = clean_path[0:-1]
 
                current_file, files = get_element_from_path(g_repo, g_commit, clean_path)
                if current_file is None:
                    return render_template('404.html'), 404
                if type(current_file) == github.ContentFile.ContentFile and current_file.type == 'dir' and len(path) > 0 and path[-1] != '/':
                    return redirect(url_for('repository', id=id, path=path + '/'))

                files, current_file = get_current_folder_files(clean_path, current_file, repository_configuration, g_repo, g_commit)

                content = get_content(current_file, files, clean_path, repository_configuration, g_repo)
                content_type = get_type_content(current_file.name, clean_path, repository_configuration, g_repo, False)
                return content, {'Content-Type': content_type}

        @application.route('/', methods=['GET'])
        def index():
            id = request.args.get('id', None)
            repo_name = clean_github_repository(request.args.get('githubRepository', None))
            repo = None
            if id is not None:
                config_path = self.config_dir + "/" + id + "/config.json"
                if os.path.exists(config_path):
                    with open(config_path) as f:
                        data = json.load(f, object_hook=json_util.object_hook)
                        if repo_name == clean_github_repository(data['repository']):
                            repo = data
            return render_template('index.html', repo=repo)
        
        @application.route('/robots.txt')
        def robots():
            return application.send_static_file('robots.txt')

        @application.route('/', methods=['POST'])
        def add_repository():
            id = request.args.get('id', str(uuid.uuid4()))
            config_path = os.path.join(self.config_dir, str(id))
            
            repo = request.form['githubRepository']
            terms = request.form['terms']
            expiration_date = None
            expiration = None
            if 'expiration' in request.form:
                expiration = request.form['expiration']
            if 'expiration_date' in request.form and request.form['expiration_date'] != '':
                expiration_date = datetime.strptime(request.form['expiration_date'], '%Y-%m-%d')

            user = session.get('user', None)

            token = None
            if os.path.exists(config_path):
                with open(os.path.join(config_path, "config.json"), 'r') as r:
                    data = json.load(r)
                    if 'token' in data:
                       token = data['token']
            if token is None and user is not None and 'token' in user and user['token'] is not None:
                token = user['token']['access_token']
            
            if not os.path.exists(config_path):
                os.mkdir(config_path)
            with open(config_path + "/config.json", 'w') as outfile:
                json.dump({
                    "id": id,
                    "repository": repo,
                    "terms": terms.splitlines(),
                    "token": token,
                    "expiration_date": expiration_date,
                    "expiration": expiration
                }, outfile, default=json_util.default)
            return redirect(url_for('repository', id=id))

        return application
Exemplo n.º 13
0
    def create_flask_application(self):
        application = Flask(__name__)
        application.log = {}
        application.killurl = str(uuid.uuid4())
        application.jinja_env.add_extension('jinja2.ext.do')

        @application.template_filter(
            'file_render', )
        def file_render(file, terms):
            def removeTerms(content, terms):
                for term in terms:
                    content = re.compile(term,
                                         re.IGNORECASE).sub("XXX", content)
                return content

            if file.size > 1000000:
                return Markup(
                    "The file %s is too big please download it: <a href='%s'>Download %s</a>"
                    % (file.name, file.url, file.name))
            if ".md" in file.name:
                return Markup(
                    "<div class='markdown-body'>%s</div>" % removeTerms(
                        self.github.render_markdown(file.decoded_content),
                        terms))
            if ".jpg" in file.name or ".png" in file.name or ".png" in file.name or ".gif" in file.name:
                return Markup("<img src='%s' alt='%s'>" %
                              (file.url, file.name))
            if ".html" in file.name:
                return removeTerms(Markup(file.decoded_content), terms)
            if ".txt" in file.name or ".log" in file.name or ".xml" in file.name or ".json" in file.name or ".java" in file.name or ".py" in file.name:
                return removeTerms(
                    Markup("<pre>" + file.decoded_content + "</pre>"), terms)
            return Markup("<a href='%s'>Download %s</a>" %
                          (file.url, file.name))

        @application.route('/' + application.killurl, methods=['POST'])
        def seriouslykill():
            func = request.environ.get('werkzeug.server.shutdown')
            func()
            return "Shutting down..."

        @application.route('/repository/<id>',
                           methods=['GET'],
                           defaults={'path': ''})
        @application.route('/repository/<id>/',
                           methods=['GET'],
                           defaults={'path': ''})
        @application.route('/repository/<id>/<path:path>', methods=['GET'])
        def repository(id, path):
            config_path = self.config_dir + "/" + str(id) + "/config.json"
            if not os.path.exists(config_path):
                return render_template('404.html'), 404
            with open(config_path) as f:
                data = json.load(f)
                repo = clean_github_repository(data['repository'])
                g_repo = self.github.get_repo(repo)
                current_folder = g_repo.get_contents(urllib.quote(path))
                current_file = None
                if type(current_folder) is github.ContentFile.ContentFile:
                    current_file = current_folder
                    current_folder = g_repo.get_contents(
                        urllib.quote(os.path.dirname(path)))
                else:
                    for f in current_folder:
                        if f.name.lower() == "readme.md" or f.name.lower(
                        ) == "index.html":
                            current_file = f
                            break

                return render_template(
                    'repo.html',
                    terms=data["terms"],
                    current_repository=id,
                    current_file=current_file,
                    current_folder=current_folder,
                    path=path.split("/") if path != '' else [])

        @application.route('/', methods=['GET'])
        def index():
            id = request.args.get('id', None)
            repo_name = clean_github_repository(
                request.args.get('githubRepository', None))
            repo = None
            if id is not None:
                config_path = self.config_dir + "/" + id + "/config.json"
                if os.path.exists(config_path):
                    with open(config_path) as f:
                        data = json.load(f)
                        repo_data = clean_github_repository(data['repository'])
                        if repo_name == repo_data:
                            repo = data

            return render_template('index.html', repo=repo)

        @application.route('/', methods=['POST'])
        def add_repository():
            id = request.args.get('id', str(uuid.uuid4()))
            repo = request.form['githubRepository']
            terms = request.form['terms']

            config_path = self.config_dir + "/" + str(id)
            if not os.path.exists(config_path):
                os.mkdir(config_path)
            with open(config_path + "/config.json", 'w') as outfile:
                json.dump(
                    {
                        "id": id,
                        "repository": repo,
                        "terms": terms.splitlines()
                    }, outfile)
            return redirect(url_for('repository', id=id))

        return application
Exemplo n.º 14
0
def init(conf_dict):
    global confs_path

    conf=conf_dict['conf']
    logger=logging.getLogger('ServiceDraw-API')
    confs_path=conf.get('main','confs_path')

    #Create a Bottle object
    app=Flask('ServiceDraw-API',template_folder=conf.get('main','templates_path'))
    app.log=logger

    #Add some routes
    @app.route("/")
    def index():
        dict_vars={
                'mount_point': mount_point_href,
                'table_data': ''
                }
        t=dynamic_table.Table(dynamic_table.RenderHTML(table_attr="class=\"table\""))
        t.set_col_names(['Product/Service','Services', 'Services Groups'])
        #Define our table rows, which is basically our menu here
        dirlist=os.listdir(confs_path)
        if dirlist:
            dirlist.sort()
        for f in dirlist:
            if os.path.isfile("{}/{}".format(confs_path,f)):
                name, ext = os.path.splitext(f)
                if ext == '.conf':
                    pdt_conf=SafeConfigParser()
                    if pdt_conf.read(('{}/{}'.format(confs_path,f))) == []:
                        raise RuntimeError('Could not load Product configuration from file: {}/{}'.format(confs_path,f))
                    try:
                        product_name=pdt_conf.get('global','name')
                    except:
                        product_name=name
                    #Use servicedraw to get basic service and service group information
                    try:
                        sd=servicedraw.Draw(pdt_conf,logger=logging.getLogger('servicedraw.Draw({})'.format(pdt_conf)))
                        sg_cnt=len(sd.service_groups)
                        s_cnt=len(sd.services) - sg_cnt
                        del(sd)
                    except Exception as ex:
                        msg=ex.args[0]
                        sg_cnt='Error loading service info: {}'.format(msg)
                        s_cnt='Error loading service info: {}'.format(msg)
                    t.add_row([ '<a href="{href}/draw/{name}">{name}({pname})</a>'.format(href=mount_point_href,name=name,pname=product_name), s_cnt, sg_cnt ])
        dict_vars['table_data']=Markup(str(t))
        return render_template('index.html',**dict_vars)

    @app.route("/draw/<name>")
    @app.route("/draw/<name>/<sub_name>")
    def draw(name,sub_name=None):
        dict_vars={
            'mount_point': mount_point_href,
            'graph_name': name,
            'name': name,
            'svg_data': '',
            'jump_to_list': """<li><a href="{}/draw/{}">All</a></li>""".format(mount_point_href,name),
            'cur_view': 'All',
            'svc_info_table': '',
            'svc_deps_table': '',
            'svc_rdeps_table': '',
            'dl_formats_links': '',
            'xtra_dropdown1': ''
        }
        rev_deps=request.args.get('rev_deps','f')
        jl_append=''
        reverse_deps=False
        if sub_name:
            dict_vars['cur_view']=sub_name
            if rev_deps == 't':
                rev_dep_val="On"
                reverse_deps=True
                jl_append='?rev_deps=t'
            else:
                rev_dep_val="Off"
            dict_vars['xtra_dropdown1']="""
                <button class="dropdown-toggle btn btn-default" type="button" data-toggle="dropdown">Reverse Dependencies: {} <span class="caret"></span></button>
                <ul class="dropdown-menu">
                    <li><a href="?rev_deps=t">On</a></li>
                    <li><a href="?rev_deps=f">Off</a></li>
                </ul>
            """.format(rev_dep_val)
        path_to_conf="{}/{}.conf".format(confs_path,name)
        if os.path.isfile(path_to_conf):
            try:
                c=SafeConfigParser()
                c.read(path_to_conf)
                try:
                    data=gen_graph(name,from_obj=sub_name,reverse_deps=reverse_deps,tail_opts=jl_append)[1]
                    if hasattr(data, 'decode'):
                        data=data.decode()
                    dict_vars['svg_data']=data
                except:
                    app.log.error(traceback.format_exc())
                try:
                    dict_vars['graph_name'] = c.get('global','name')
                except:
                    pass
                try:
                    sd=servicedraw.Draw(c)
                    dl=dict_vars['dl_formats_links']
                    dl_append=''
                    dl_fmts=[ 'pydot' ] + sd.graph.formats
                    jl=dict_vars['jump_to_list']
                    svcs=list(sd.services.keys())
                    svcs.sort()
                    if sub_name:
                        dl_append="/{}".format(sub_name)
                        try:
                            svc_info=sd.services[sub_name]
                            t=dynamic_table.Table(dynamic_table.RenderHTML(table_attr="class=\"table\""))
                            t.set_col_names(['Service Attribute','Value'])
                            td=dynamic_table.Table(dynamic_table.RenderHTML(table_attr="class=\"table\""))
                            td.set_col_names(['Depends on Service','Port'])
                            trd=dynamic_table.Table(dynamic_table.RenderHTML(table_attr="class=\"table\""))
                            trd.set_col_names(['Depended on by Service','To Port'])
                            for k in svc_info:
                                if k == 'depends':
                                    if svc_info[k]:
                                        for d in svc_info[k]:
                                            if svc_info[k][d]['ports']:
                                                for p in svc_info[k][d]['ports']:
                                                    td.add_row([d,p])
                                            else:
                                                td.add_row([d,'N/A'])
                                    else:
                                        td.add_row(['None Defined',''])
                                elif k == 'reverse_depends':
                                    if svc_info[k]:
                                        for d in svc_info[k]:
                                            if svc_info[k][d]['ports']:
                                                for p in svc_info[k][d]['ports']:
                                                    trd.add_row([d,p])
                                            else:
                                                trd.add_row([d,'N/A'])
                                    else:
                                        trd.add_row(['None Defined',''])
                                else:
                                    t.add_row([k,svc_info[k]])
                            dict_vars['svc_info_table']=str(t)
                            dict_vars['svc_deps_table']=str(td)
                            dict_vars['svc_rdeps_table']=str(trd)
                        except:
                            pass
                    del(sd)
                    for svc in svcs:
                        jl+="""<li><a href="{href}/draw/{name}/{svc}{jl}">{svc} </a></li>""".format(href=mount_point_href,name=name,svc=svc,jl=jl_append)
                    dict_vars['jump_to_list']=jl
                    del(jl)
                    for fmt in dl_fmts:
                        f=fmt.upper()
                        dl+="""<li><a href="{}/drawgraph/{}{}.{}?download=t">{}</a></li>""".format(mount_point_href,name,dl_append,fmt,f)
                    dict_vars['dl_formats_links']=dl
                    del(dl)
                except:
                    dict_vars['graph_name'] = "Error loading info for Product/Service: {}".format(name)
                    dict_vars['jump_to_list'] = "<li>N/A</li>"
                    app.log.error(traceback.format_exc())
            except:
                app.log.error(traceback.format_exc())
                dict_vars['graph_name'] = "Error loading info for Product/Service: {}".format(name)
        for m in [ 'jump_to_list', 'svg_data', 'svc_info_table', 'svc_deps_table', 'svc_rdeps_table','dl_formats_links','xtra_dropdown1' ]:
            dict_vars[m] = Markup(dict_vars[m])
        return render_template('draw_graph.html',**dict_vars)

    @app.route("/drawgraph/<pname>.<ext>")
    @app.route("/drawgraph/<pname>/<sub_name>.<ext>")
    def drawgraph(pname,ext='svg',sub_name=None):
        fn=pname
        if sub_name:
            fn+="_{}".format(sub_name)
        fn+=".{}".format(ext)
        try:
            data=gen_graph(pname,ext,from_obj=sub_name)
        except:
            raise
        if ext in [ 'dot', 'pydot' ]:
            ext_type = 'text/plain'
        elif ext == 'svg':
            ext_type = 'text/html'
        else:
            ext_type='image/{}'.format(ext)
        if data[0]:
            ext_type='text/html'
        resp = Response(response=data[1],content_type=ext_type)
        dl=request.args.get('download','f')
        if dl == 't':
            resp.headers.set('Content-Disposition', 'attachment; filename="{}"'.format(fn))
        return resp

    #Return our Flask application object
    return app
Exemplo n.º 15
0
def create_app(mode=None):
    '''Create and configure an instance of the Flask application.'''
    if not mode:
        app = Flask(__name__, instance_relative_config=True)
        # store the database in the instance folder
        app.config['DATABASE'] = os.path.join(app.instance_path, 'OLMS.db')
    else:
        static_folder = os.path.join(sys._MEIPASS, 'static')
        template_folder = os.path.join(sys._MEIPASS, 'templates')
        instance_path = os.path.join(os.environ['LOCALAPPDATA'], 'webapp')
        app = localFlask('webapp',
                         static_folder=static_folder,
                         template_folder=template_folder,
                         instance_path=instance_path,
                         instance_relative_config=True)
        app.config['DATABASE'] = os.path.join(app.instance_path, 'database')
    # load config from config.py
    from OLMS import config
    app.config.from_object(config)
    # load custom config from instance/config.py
    app.config.from_pyfile('config.py', silent=True)

    # ensure jquery.js and bootstrap.css exists
    static_files = {
        'jquery.js':
        'http://code.jquery.com/jquery-3.4.1.min.js',
        'bootstrap.css':
        'http://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css',
        'bootstrap.min.css.map':
        'http://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css.map'
    }
    for i in static_files.keys():
        static_file_path = os.path.join(app.static_folder, i)
        if not os.path.isfile(static_file_path):
            try:
                static_file = urlopen(static_files[i], timeout=5)
                with open(static_file_path, 'wb') as f:
                    f.write(static_file.read())
            except:
                pass

    # ensure the instance folder exists
    try:
        os.makedirs(app.instance_path)
    except OSError:
        pass

    # Add custom logger
    from OLMS.log import Logger

    app.log = Logger(app)

    # register the database commands
    from OLMS import db

    db.init_app(app)

    # embed google reCAPTCHA protection
    from OLMS.recaptcha import reCAPTCHA

    @app.context_processor
    def embed_recaptcha():
        return dict(recaptcha=reCAPTCHA())

    # apply the blueprints to the app
    from OLMS import auth, dept, empl, export, record, stats

    app.register_blueprint(auth.bp)
    app.register_blueprint(dept.bp)
    app.register_blueprint(empl.bp)
    app.register_blueprint(export.bp)
    app.register_blueprint(record.bp)
    app.register_blueprint(stats.bp)

    # make url_for('index') == url_for('record.empl_index')
    app.add_url_rule('/', endpoint='index')

    return app
Exemplo n.º 16
0
def init_rest_api():
    class RestLoggerPrepender:
        def __init__(self, logger):
            self.logger = logger
            self.warn = self.warning
            self.fatal = self.critical

        def _ctx(self):
            ips = (
                request.environ.get('HTTP_X_FORWARDED_FOR', False),
                request.environ.get('HTTP_X_REAL_IP', False),
                request.environ['REMOTE_ADDR'],
            )
            for ip in ips:
                if ip:
                    break
            return "{}:{}".format(ip, request.environ['REMOTE_PORT'])

        def _edit_msg(self, msg):
            return "[{}] {}".format(self._ctx(), msg)

        def critical(self, msg, *args, **kwargs):
            self.logger.critical(self._edit_msg(msg), *args, **kwargs)

        def debug(self, msg, *args, **kwargs):
            self.logger.debug(self._edit_msg(msg), *args, **kwargs)

        def error(self, msg, *args, **kwargs):
            self.logger.error(self._edit_msg(msg), *args, **kwargs)

        def exception(self, msg, *args, **kwargs):
            self.logger.exception(self._edit_msg(msg), *args, **kwargs)

        def info(self, msg, *args, **kwargs):
            self.logger.info(self._edit_msg(msg), *args, **kwargs)

        def warning(self, msg, *args, **kwargs):
            self.logger.warning(self._edit_msg(msg), *args, **kwargs)

        def log(self, level, msg, *args, **kwargs):
            self.logger.log(level, self._edit_msg(msg), *args, **kwargs)

    #Initialize Flask based API
    rest_app = Flask("RestAPI", static_folder="{}/static".format(os.getcwd()))
    #Set Flask debug if needed
    if CONFIG['api_rest_debug']:
        rest_app.debug = True
    #Our special log wrapper to prepending context info
    rest_app.log = RestLoggerPrepender(rest_app.logger)

    @rest_app.route("/")
    def root():
        return rest_app.send_static_file('index.html')

    @rest_app.route("/status")
    def status():
        rest_app.log.debug("Gathering light states")
        js = {'result': 'Success'}
        get_state = request.args.get('state', 't').lower()
        get_intervals = request.args.get('intervals', 't').lower()
        get_color = request.args.get('color', '').lower()
        rest_app.log.info(
            "Recieved command: 'status', and data: {{color: {}, state: {}, intervals: {}}}"
            .format(get_color, get_state, get_intervals))
        if get_color:
            if get_color not in tclock.colors:
                js['msg'] = "Unknown color:{}".format(get_color)
                resp = jsonify(js)
                resp.status = 400
                return resp
        if get_state in ['t', 'true']:
            if get_color:
                js['state'] = tclock.state[get_color]
            else:
                js['state'] = tclock.state
        if get_intervals in ['t', 'true']:
            if get_color:
                js['intervals'] = tclock.timing[get_color]
            else:
                js['intervals'] = tclock.timing
        rest_app.log.debug("States gathered, responding with: {}".format(js))
        resp = rest_app.response_class(response=json.dumps(js,
                                                           indent=4,
                                                           default=jsonizer),
                                       status=200,
                                       mimetype='application/json')
        return resp

    @rest_app.route("/poll")
    def force_poll():
        rest_app.log.debug("Executing Poll")
        tclock.poll()
        js = {'msg': 'Completed', 'result': 'Success'}
        rest_app.log.debug("Poll completed, responding with: {}".format(js))
        return jsonify(js)

    @rest_app.route("/interval")
    def intervals():
        rest_app.log.debug("Gathering color change intervals")
        js = tclock.get_interval_ids()
        js['result'] = 'Success'
        rest_app.log.debug(
            "Color change intervals gathered, responding with: {}".format(js))
        resp = rest_app.response_class(response=json.dumps(js,
                                                           indent=4,
                                                           default=jsonizer),
                                       status=200,
                                       mimetype='application/json')
        return resp

    @rest_app.route("/interval/<color>")
    def interval(color):
        rest_app.log.debug(
            "Gathering color change intervals for color: {}".format(color))
        js = {}
        js['intervals'] = tclock.get_interval_ids(color)
        js['result'] = 'Success'
        rest_app.log.debug(
            "Color change intervals gathered for color: {}, responding with: {}"
            .format(color, js))
        resp = rest_app.response_class(response=json.dumps(js,
                                                           indent=4,
                                                           default=jsonizer),
                                       status=200,
                                       mimetype='application/json')
        return resp

    @rest_app.route("/interval/<color>", methods=["POST"])
    def create_interval(color):
        js = {'msg': '', 'result': ''}
        status_code = 200
        interval_settings = {'color': color}
        settings_reqd_keys = ['from_time', 'to_time', 'brightness']
        settings_opt_keys = ['brightness_changes']
        req_json = request.get_json()
        rest_app.log.debug(
            "Preparing to create a new interval for color: {}, details: {}".
            format(color, req_json))
        for rk in settings_reqd_keys:
            try:
                interval_settings[rk] = req_json[rk]
            except KeyError:
                rest_app.log.warning(
                    "Request for new interval is missing a required key: {}".
                    format(rk))
                js['msg'] = 'Missing required key "{}"'.format(rk)
                js['result'] = "Failed"
                resp = jsonify(js)
                resp.status_code = 400
                return resp
        for ok in settings_opt_keys:
            try:
                interval_settings[ok] = req_json[ok]
            except KeyError:
                pass
        try:
            rest_app.log.debug(
                "Attempting to create a new interval with settings: {}".format(
                    interval_settings))
            tclock.add_interval(**interval_settings)
            js['msg'] = "Created new interval"
            js['result'] = "Success"
            js['interval_id'] = len(tclock.timing[color]) - 1
            js['interval_settings'] = tclock.get_interval(
                color, js['interval_id'])
            js['interval_settings']['time_interval'] = jsonizer(
                js['interval_settings']['time_interval'])
            update_config()
        except:
            exc_type, exc_value, exc_traceback = sys.exc_info()
            js['msg'] = 'Failed: {}'.format(exc_value)
            js['result'] = 'Failed'
            rest_app.log.error(
                "Error trying to create a new interval: {}".format(exc_value),
                exc_info=True)
            status_code = 400
        rest_app.log.debug(
            "New interval processing done, responding with: {}".format(js))
        resp = jsonify(js)
        resp.status_code = status_code
        return resp

    @rest_app.route("/interval/<color>/<interval_id>", methods=["PUT"])
    def update_interval(color, interval_id):
        js = {'msg': '', 'result': ''}
        status_code = 200
        changes = False
        interval_settings = {
            'color': color,
        }
        settings_opt_keys = [
            'from_time', 'to_time', 'brightness', 'brightness_changes'
        ]
        req_json = request.get_json()
        rest_app.log.debug(
            "Preparing to update interval_id: {} for color: {} details: {}".
            format(interval_id, color, req_json))
        try:
            i_id = int(interval_id)
            interval_settings['interval_id'] = int(interval_id)
        except:
            rest_app.log.warning(
                "Interval ID MUST be an integer: {}".format(interval_id))
            js['msg'] = 'Interval id must be an integer'
            js['result'] = 'Failed'
            resp = jsonify(js)
            resp.status_code = 400
            return resp
        for ok in settings_opt_keys:
            try:
                changes = True
                interval_settings[ok] = req_json[ok]
            except KeyError:
                pass
        try:
            rest_app.log.debug(
                "Attempting to update interval_id: {} for color: {} with details: {}"
                .format(interval_id, color, interval_settings))
            tclock.update_interval(**interval_settings)
            js['msg'] = "Updated interval: {}:{}".format(color, interval_id)
            js['result'] = "Success"
            js['new_interval_settings'] = tclock.get_interval(
                color, int(interval_id))
            js['new_interval_settings']['time_interval'] = jsonizer(
                js['new_interval_settings']['time_interval'])
            update_config()
        except:
            exc_type, exc_value, exc_traceback = sys.exc_info()
            js['msg'] = 'Failed: {}'.format(exc_value)
            js['result'] = 'Failed'
            rest_app.log.error(
                "Error trying to update interval_id: {} for color: {}: {}".
                format(interval_id, color, exc_value),
                exc_info=True)
            status_code = 400
        rest_app.log.debug(
            "Update interval processing done, responding with: {}".format(js))
        resp = jsonify(js)
        resp.status_code = status_code
        return resp

    @rest_app.route("/interval/<color>/<interval_id>", methods=["DELETE"])
    def delete_interval(color, interval_id):
        js = {'msg': '', 'result': ''}
        status_code = 200
        rest_app.log.debug(
            "Preparing to delete interval_id: {} for color: {}".format(
                interval_id, color))
        try:
            i_id = int(interval_id)
        except:
            js['msg'] = 'Interval id must be an integer'
            js['result'] = 'Failed'
            resp = jsonify(js)
            resp.status_code = 400
            return resp
        try:
            rest_app.log.debug(
                "Attempting to delete interval_id: {} for color: {}".format(
                    interval_id, color))
            tclock.del_interval(color, i_id)
            js['msg'] = 'Deleted interval'
            js['result'] = 'Success'
            update_config()
        except:
            exc_type, exc_value, exc_traceback = sys.exc_info()
            js['msg'] = 'Failed: {}'.format(exc_value)
            js['result'] = 'Failed'
            rest_app.log.error(
                "Error trying to delete interval_id: {} for color: {}: {}".
                format(interval_id, color, exc_value),
                exc_info=True)
            status_code = 400
        rest_app.log.debug(
            "Delete interval processing done, responding with: {}".format(js))
        resp = jsonify(js)
        resp.status_code = status_code
        return resp

    @rest_app.route("/light/<color>", methods=["PUT"])
    def set_light(color):
        js = {'msg': '', 'result': ''}
        req_json = request.get_json()
        override_intervals = False
        rest_app.log.debug(
            "Preparing to change light state for color: {} details: {}".format(
                color, req_json))
        try:
            state = req_json['state'].lower()
        except KeyError:
            rest_app.log.warning("Missing state 'key'")
            js['msg'] = 'Missing "state" key'
            js['result'] = "Failed"
            resp = jsonify(js)
            resp.status_code = 400
            return resp
        if state not in ['on', 'off']:
            rest_app.log.warning(
                "State must be either 'on' or 'off': {}".format(state))
            js['msg'] = 'Invalid state: {}, must be one of :"on" or "off"'.format(
                state)
            js['result'] = 'Failed'
            resp = jsonify(js)
            resp.status_code = 400
            return resp
        try:
            override_intervals = req_json['override_intervals']
        except KeyError:
            pass
        if state == 'on':
            try:
                brightness = int(req_json['brightness'])
            except KeyError:
                brightness = 255
                pass
            rest_app.log.debug(
                "Attempting to change light state for color 'on': {}".format(
                    color))
            tclock.turn_on(color, brightness, override_intervals)
            js['msg'] = 'Turned color: {} ON'.format(color)
        if state == 'off':
            rest_app.log.debug(
                "Attempting to change light state for color 'off': {}".format(
                    color))
            tclock.turn_off(color, override_intervals)
            js['msg'] = 'Turned color: {} OFF'.format(color)
        js['result'] = 'Success'
        rest_app.log.debug(
            "Change light state processing done, responding with: {}".format(
                js))
        resp = jsonify(js)
        return resp

    return rest_app
Exemplo n.º 17
0
        if req_json is None:

            return jsonify(error='this service require A JSON request')

        else:
            if not ('text' in req_json):
                raise Exception('Missing mandatory paramater "text"')

            text = req_json['text']
            blob = TextBlob(text)
            sentiment = blob.sentiment

            return jsonify(polarity=sentiment.polarity,
                           subjectivity=sentiment.subjectivity)

    except Exception as ex:
        app.log.error(type(ex))
        app.log.error(ex.args)
        app.log.error(ex)
        return jsonify(error=str(ex))


if __name__ == '__main__':

    LOG_FORMAT = "'%(asctime)s - %(name)s - %(levelname)s - %(message)s'"
    logging.basicConfig(level=logging.DEBUG, format=LOG_FORMAT)
    app.log = logging.getLogger(__name__)

    port = os.environ['FLASK_PORT']
    app.run(host='0.0.0.0', port=int(port), debug=False)
Exemplo n.º 18
0
def create_app(config='config.ProductionDevelopmentConfig',
               apptype='profi',
               debug=None,
               testing=None):
    app = Flask(__name__, static_folder='./static')

    app.config.from_object(config)

    app.teardown_request(close_database)

    app.debug = app.config['DEBUG'] if 'DEBUG' in app.config else False
    app.testing = app.config['TESTING'] if 'TESTING' in app.config else False

    if debug is not None:
        app.debug = True if debug else False

    if testing is not None:
        app.testing = True if testing else False

    app.apptype = apptype
    app.log = logger(
        app, apptype not in ['profi', 'front', 'static', 'socket', 'file'])

    app.url_map.converters['translit'] = TransliterationConverter
    app.url_map.converters['short_uid'] = ShortUIDConverter
    app.url_map.converters['full_uid'] = FullUIDConverter
    # app.url_map.converters['uid'] = UIDConverter

    app.before_request(prepare_connections(app))
    app.before_request(lambda: load_user(apptype))
    # app.before_request(lambda: setup_logger(apptype, host='fluid.profi', port=24224))
    app.before_request(setup_authomatic(app))

    def add_map_headers_to_less_files(response):
        response.headers.add('Access-Control-Allow-Origin', '*')
        if request.path and re.search(r'\.css$', request.path):
            mapfile = re.sub(r'\.css$', r'.css.map', request.path)
            if os.path.isfile(
                    os.path.realpath(os.path.dirname(__file__)) + mapfile):
                response.headers.add('X-Sourcemap', mapfile)
        return response

    app.after_request(add_map_headers_to_less_files)

    @login_manager.user_loader
    def load_user_manager(user_id):
        return g.db.query(User).get(user_id)

    # if apptype in ['file', 'profi', 'static', 'front']:

    session_opts = {
        'session.type': 'ext:memcached',
        'session.cookie_domain': '.' + Config.MAIN_DOMAIN,
        'session.url': 'memcached.profi:11211'
    }

    class BeakerSessionInterface(SessionInterface):
        def open_session(self, app, request):
            return request.environ.get('beaker.session')

        def save_session(self, app, session, response):
            session.save()

    app.wsgi_app = SessionMiddleware(app.wsgi_app, session_opts)
    app.session_interface = BeakerSessionInterface()

    app.type = apptype
    login_manager.init_app(app)
    login_manager.session_protection = 'basic'

    if apptype == 'front':

        # relative paths
        def join_path(template, parent):
            return os.path.join(os.path.dirname(parent), template)

        app.jinja_env.join_path = join_path

        def load_portal():
            from profapp.models.portal import Portal
            portal = g.db.query(Portal).filter_by(host=request.host).first()
            g.portal = portal if portal else None
            g.portal_id = portal.id if portal else None
            g.portal_layout_path = portal.layout.path if portal else ''
            g.lang = g.portal.lang if g.portal else g.user_dict[
                'lang'] if portal else 'en'

        app.before_request(load_portal)
        from profapp.controllers.blueprints_register import register_front as register_blueprints_front
        register_blueprints_front(app)
        update_jinja_engine(app)

        @app.errorhandler(404)
        def page_not_found(e):
            from profapp.controllers.views_front import error_404
            return error_404()

    elif apptype == 'static':
        from profapp.controllers.blueprints_register import register_static as register_blueprints_static
        register_blueprints_static(app)
    elif apptype == 'file':
        from profapp.controllers.blueprints_register import register_file as register_blueprints_file
        register_blueprints_file(app)
    elif apptype == 'profi':
        from profapp.controllers.blueprints_register import register_profi as register_blueprints_profi
        register_blueprints_profi(app)
        update_jinja_engine(app)
    else:
        from profapp.controllers.blueprints_register import register_profi as register_blueprints_profi
        register_blueprints_profi(app)
        update_jinja_engine(app)

    if apptype in ['profi', 'front']:
        bootstrap.init_app(app)
        mail.init_app(app)

    return app