Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixed issues SunSpecOrangeButton#55 and started on SunSpecOrangeButton#51.  The code now runs entirely as a Vue.js application and the Python code prepares JSON.  There is no REST API anymore.
  • Loading branch information
joelebwf committed Dec 17, 2020
1 parent 1abfb9c commit 8e04faf
Show file tree
Hide file tree
Showing 20 changed files with 286 additions and 174 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ __pycache__
node_modules
.DS_Store
dist
venv
File renamed without changes.
65 changes: 65 additions & 0 deletions prep/generate_static_site.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import generator

import json
from oblib import taxonomy


tax = taxonomy.Taxonomy()


data = generator.entrypoints()
with open("../web/resources/entrypoints.json", "w") as outfile:
outfile.write(json.dumps(data))

entrypoints = data
with open("../web/resources/entrypoints-details.json", "w") as outfile:
data = {}
for entrypoint in entrypoints:
try:
data[entrypoint["entrypoint"]] = generator.entrypoint_detail(entrypoint["entrypoint"])
except:
pass
outfile.write(json.dumps(data))

with open("../web/resources/entrypoints-concepts.json", "w") as outfile:
data = {}
for entrypoint in entrypoints:
data[entrypoint["entrypoint"]] = []
for concept in tax.semantic.get_entrypoint_concepts(entrypoint["entrypoint"]):
data[entrypoint["entrypoint"]].append(concept.split(":")[1])
outfile.write(json.dumps(data))

data = generator.concepts("none")
with open("../web/resources/concepts.json", "w") as outfile:
outfile.write(json.dumps(data))

with open("../web/resources/concepts-details.json", "w") as outfile:
concepts = data
data = {}
for concept in concepts:
data[concept["name"]] = generator.concept_detail(concept["name"], concept["taxonomy"])
outfile.write(json.dumps(data))

data = generator.types()
with open("../web/resources/types.json", "w") as outfile:
outfile.write(json.dumps(data))

data = generator.units()
with open("../web/resources/units.json", "w") as outfile:
outfile.write(json.dumps(data))

data = generator.glossary()
with open("../web/resources/references.json", "w") as outfile:
outfile.write(json.dumps(data))
107 changes: 23 additions & 84 deletions viewer/viewer.py → prep/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,85 +11,27 @@
# limitations under the License.

"""
This program implements the Orange Button Viewer. The Orange Button Viewer provides full information on the Orange Button data structures
in an easy to use format that does not require XBRL experience.
Use the following comand to start the Viewer.
$ pip install Flask
$ FLASK_APP=viewer.py flask run
This allows OBTV to be generated for the Vue.js code. The data is generated from pyoblib and the output
is in Python lists/dictionaries that can be externalized to JSON.
"""

import reference
import relationships

import json
import re

import werkzeug
from oblib import taxonomy
from flask import Flask, make_response, jsonify
#from flask_cors import CORS
from flask import jsonify

RETURN_INDEX = "<h2><a href='/html'>Return to search page</a></h2>"
tax = None
tax = taxonomy.Taxonomy()

def convert(name):
s1 = re.sub('(.)([A-Z][a-z]+)', r'\1 \2', name)
return re.sub('([a-z0-9])([A-Z])', r'\1 \2', s1)


"""Main Code - Start Flask"""
tax = taxonomy.Taxonomy()
app = Flask(__name__, static_url_path='', static_folder='dist')
#CORS(app)
app.logger.setLevel(20)
app.logger.info("Initialization completed")
"""End of Main Code"""


@app.errorhandler(Exception)
def exception(e):
"""
Handle All Exceptions here - typically Exceptions are program logic issues so there is little that can
be returend other than an internal error has occured. It is possible that the issue is a bad key passsed
from the client side (KeyError) or some sort of HTTPException as well and these will be reflected.
"""

app.logger.error(e)

if isinstance(e, KeyError):
response = make_response()
response.data = json.dumps({
"code": 404,
"name": "404 Not Found",
"description": str(e)
})
response.content_type = "application/json"
return response
elif isinstance(e, werkzeug.exceptions.HTTPException):
response = e.get_response()
response.data = json.dumps({
"code": e.code,
"name": e.name,
"description": e.description,
})
response.content_type = "application/json"
return response
else:
response = make_response()
response.data = json.dumps({
"code": 500,
"name": "500 Internal Server Error",
"description": "An internal issue with the software occurred",
})
response.content_type = "application/json"
return response


@app.route('/concepts/<entrypoint>', methods=['GET'])
def concepts(entrypoint):
"""Flask Read Handler for concepts API Endpoint"""
"""Generates concepts data"""

if entrypoint == "none":
data = []
Expand Down Expand Up @@ -128,12 +70,11 @@ def concepts(entrypoint):
"period": details.period_type.value
})

return jsonify(data)
return data


@app.route('/units/', methods=['GET'])
def units():
"""Flask Read Handler for units API Endpoint"""
"""Generates units data"""

data = []
for unit in tax.units.get_all_units():
Expand All @@ -148,12 +89,11 @@ def units():
"definition": details.definition
})

return jsonify(data)
return data


@app.route('/types/', methods=['GET'])
def types():
"""Flask Read Handler for types API Endpoint"""
"""Generates types data"""

data = []

Expand Down Expand Up @@ -190,12 +130,11 @@ def types():
"definition": ""
})

return jsonify(sorted(data, key=lambda x: x["code"].lower()))
return sorted(data, key=lambda x: x["code"].lower())


@app.route('/entrypoints/', methods=['GET'])
def entrypoints():
"""Flask Read Handler for entrypoints API endpoint"""
"""Generates entrypoints data"""

# TODO: This code is a workaround for an issue in solar-taxonomy (and perhaps pyoblib) that
# the Monitoring Entrypoint has not metadata. Thus there is one extra entrypoint in the non-detailed
Expand All @@ -208,21 +147,23 @@ def entrypoints():
data.append({
"entrypoint": entrypoints_details[entrypoint].name,
"type": entrypoints_details[entrypoint].entrypoint_type.value,
"description": entrypoints_details[entrypoint].description
"description": reference.ENTRYPOINTS_DESCRIPTION[entrypoint]
})
else:
description = ""
if entrypoint in reference.ENTRYPOINTS_DESCRIPTION:
description = reference.ENTRYPOINTS_DESCRIPTION[entrypoint]
data.append({
"entrypoint": entrypoint,
"type": "Documents",
"description": "None"
"description": description
})

return jsonify(sorted(data, key=lambda x: x["entrypoint"]))
return sorted(data, key=lambda x: x["entrypoint"])


@app.route('/glossary/', methods=['GET'])
def glossary():
"""Flask Read Handler for glossary API Endpoint"""
"""Generates glossary data"""

data = []
for item in sorted(reference.ACRONYMS.items()):
Expand All @@ -239,11 +180,11 @@ def glossary():
"definition": item[0]
})

return jsonify(data)
return data


@app.route('/conceptdetail/<concept>/<taxonomy>', methods=['GET'])
def concept_detail(concept, taxonomy):
"""Generates concept_detail data"""

concept = taxonomy.lower() + ":" + concept

Expand Down Expand Up @@ -328,19 +269,17 @@ def concept_detail(concept, taxonomy):
"usages": usages
}

return jsonify(data)
return data


@app.route('/entrypointdetail/<entrypoint>/undefined', methods=['GET'])
def entrypoint_detail(entrypoint):
"""Generates entrypoint detail data"""

r = relationships.create_json(entrypoint)
if len(r) < 1:
raise KeyError('Entrypoint {} not found'.format(entrypoint))
data = relationships.create_json(entrypoint)

return jsonify(data)
return data


if __name__ == "__main__":
app.run(debug=True, port=5000)
Loading

0 comments on commit 8e04faf

Please sign in to comment.