Skip to content

Commit

Permalink
Merge pull request #33 from KnowledgeSeed/feature/create_new_app
Browse files Browse the repository at this point in the history
Feature/create new app
  • Loading branch information
ote82 authored Oct 18, 2024
2 parents fb42885 + 2c77918 commit eba1f00
Show file tree
Hide file tree
Showing 102 changed files with 13,780 additions and 25 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ analogic/tests/apps/camsecure/app.json
analogic/tests/apps/default/app.json
analogic/tests/apps/logincam/app.json
long_running_tasks
analogic.properties
3 changes: 3 additions & 0 deletions analogic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
from .loader import ClassLoader
from .email import EmailManager
from .setting import SettingManager
from .setting import get_properties_file_value
from .setting import hash_password
from .multi_setting import MultiSettingManager
from .analogic_tm1_service import AnalogicTM1Service
from . import pivot
Expand All @@ -19,6 +21,7 @@
from .nologin import NoLogin
from .camsecure import CamSecure
from . import core_endpoints
from . import system_endpoints
from .task import scheduler
from .exceptions import AnalogicException
from .exceptions import AnalogicProxyException
Expand Down
40 changes: 37 additions & 3 deletions analogic/analogic.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import sys
from importlib import resources
from analogic.core_endpoints import core_endpoints
from analogic.system_endpoints import system_endpoints
from analogic.authentication_provider import AuthenticationProvider
from analogic.signal_receiver import SignalReceiver
from analogic.exceptions import AnalogicMaintenanceException
Expand All @@ -18,6 +19,9 @@
from analogic.task import scheduler
import atexit
from analogic.default_signal_receiver import DefaultSignalReceiver
import shutil
from time import time
from os import utime, stat

APPLICATIONS_DIR = 'apps'
APPLICATIONS_DIR_EXTRA = os.environ.get('APPLICATIONS_DIR_EXTRA', '')
Expand Down Expand Up @@ -49,6 +53,37 @@ def __init__(self, *args, **kwargs):
self.initialize_auth_providers = True
self.long_running_tasks = {}

def create_new_app(self, name, main_page):
if name in self.analogic_applications:
raise Exception('Application already exists')

target = os.path.join(self.instance_path, APPLICATIONS_DIR, name)

source = os.path.join(self.root_path, 'new_app_structure')

shutil.copytree(source, target)

self.replace_str_in_file(os.path.join(target, 'app.py'), 'default', name)
self.replace_str_in_file(os.path.join(target, 'app.json'), '$projectId', name)
self.replace_str_in_file(os.path.join(target, 'app.json'), '$mainPage', main_page)

self.trigger_change_monitor_for_restart()

def trigger_change_monitor_for_restart(self):
name = os.path.join(self.root_path, 'change_monitor.py')
st_info = stat(name)

utime(name, (st_info.st_atime, time()))

def replace_str_in_file(self, path, old_str, new_str):
with open(path, 'r') as file:
filedata = file.read()

new_content = filedata.replace(old_str, new_str)

with open(path, 'w') as file:
file.write(new_content)

def register_analogic_url_rules(self, instance):
for url_rule in self.endpoint_rules:
self.add_url_rule(instance + url_rule['rule'],
Expand Down Expand Up @@ -246,6 +281,8 @@ def create_app(instance_path, start_scheduler=True, initialize_auth_providers=Tr

atexit.register(app.on_exit)

app.register_blueprint(system_endpoints, url_prefix='/system_endpoints')

return app


Expand All @@ -258,9 +295,6 @@ def _load_logging(app):
log_config = json.load(file)
for h in log_config['handlers']:
if 'filename' in log_config['handlers'][h]:
# if h == 'scheduler_file_handler':
# file_name = f"logs/scheduler_{os.getpid()}.log"
# else:
file_name = log_config['handlers'][h]['filename']

log_config['handlers'][h]['filename'] = os.path.join(app.instance_path, file_name)
Expand Down
1 change: 1 addition & 0 deletions analogic/change_monitor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
t = '1'
4 changes: 1 addition & 3 deletions analogic/core_endpoints.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from analogic.endpoint import AnalogicEndpoint
from analogic.authentication_provider import get_authentication_provider, endpoint_login_required
from flask import request, redirect, render_template
import base64

core_endpoints = AnalogicEndpoint('core_endpoints', __name__)

Expand All @@ -28,7 +27,6 @@ def navigation_parameters():
navigation_parameters = authentication_provider.get_navigation_parameters()

if navigation_parameters is not None:

authentication_provider.clear_navigation_parameters()

return {'navigation_parameters': navigation_parameters}, 200, {'Content-type': 'application/json'}
Expand All @@ -46,6 +44,7 @@ def start_maintenance():
return "Maintenance mode started", 200
return render_template('start_maintenance.html', cnf=authentication_provider.get_setting().get_config()), 200


@core_endpoints.analogic_endpoint_route('/stop_maintenance', methods=['GET'])
def stop_maintenance():
authentication_provider = get_authentication_provider()
Expand All @@ -55,7 +54,6 @@ def stop_maintenance():
return "Maintenance mode stopped", 200



@core_endpoints.analogic_endpoint_route('/login', methods=['GET', 'POST'])
def login():
return get_authentication_provider().login()
Expand Down
3 changes: 3 additions & 0 deletions analogic/new_app_structure/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from . import app
from .server.Export import Export
from .server.Validation import Validation
7 changes: 7 additions & 0 deletions analogic/new_app_structure/app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"projectName": "$projectId",
"projectId": "$projectId",
"authenticationMode": "NoLogin",
"mainPage": "$mainPage",
"analogicApiAuthenticationMode": "unsecured"
}
3 changes: 3 additions & 0 deletions analogic/new_app_structure/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from flask import Blueprint

default = Blueprint('default', __name__, static_folder='static', static_url_path='/apps/default/static')
27 changes: 27 additions & 0 deletions analogic/new_app_structure/server/Export.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import io
import logging

import numpy as np
import pandas as pd
import xlsxwriter
from TM1py.Utils.Utils import build_pandas_dataframe_from_cellset

pd.set_option('display.float_format', lambda x: '%.3f' % x)


def getMDXJSONByRequest(request, setting):
mdx = setting.get_mdx(request.args['key'])
for k in request.args:
mdx = mdx.replace('$' + k, request.args[k].replace('"', '\\"'))

mdx = '{"MDX" :"' + mdx + '"}'
return mdx


class Export:

def __init__(self):
pass

def getLogger(self):
return logging.getLogger(__name__)
14 changes: 14 additions & 0 deletions analogic/new_app_structure/server/Validation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import os
import pandas as pd


def validateParameter(title, expected, received):
if expected != received:
return "expected " + title + ": " + expected + " but received: " + received + '<br/>'
return ''

class Validation:

def __init__(self):
pass

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/* global app */
'use strict';
EventMap = {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/* global app */
'use strict';
Repository = {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* global app */
'use strict';
WidgetConfig = {

};
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Loading

0 comments on commit eba1f00

Please sign in to comment.