Commit cfab7e51 authored by Spencer Kiy's avatar Spencer Kiy 🥑
Browse files

test acc with no python code

parent f8958198
graft acc/data
include acc/tests/xml_allowed_tags.ini
\ No newline at end of file
import pkg_resources, os
ACCDIR = pkg_resources.resource_filename('acc', 'data/')
import os, sys, configparser, json
from lxml import etree
parser = etree.XMLParser(remove_blank_text=True)
def xml_config():
"""
imports allowed xml tags, attribs, into a dictionariy.
"""
app_config = configparser.ConfigParser(allow_no_value=True)
app_config.read('xml_allowed_tags.ini')
xml_config = {}
xml_config['attribs'] = {}
xml_config['types'] = {}
for header in app_config.sections():
attribDict = {}
xml_config['attribs'][header] = json.loads(app_config.get(header,'allowed_attribs'))
if 'allowed_types' in app_config.options(header):
xml_config['types'][header] = json.loads(app_config.get(header,'allowed_types'))
# Additional imports like allowed types here
return xml_config
xml_config = xml_config()
#print(xml_config)
def check_path(facility,file):
"""
Checks given path for proper xml format.
"""
filename = os.path.join(facility,'path',file)
return_val = 0
try:
tree = etree.parse(filename,parser)
except Exception as e:
print("XML ERROR - Error in path "+file)
print("Error message: " + str(e))
return_val = 1
try:
tree.xinclude()
except Exception as e:
print("XML INCLUDE ERROR - Error in path "+file)
print("Error message: " + str(e))
return_val = 1
return return_val
def check_sequence(facility,file):
"""
Checks given sequence for proper xml format.
"""
filename = os.path.join(facility,'sequence',file)
return_val = 0
try:
tree = etree.parse(filename,parser)
except Exception as e:
print("XML ERROR - Error in sequence "+file)
print("Error message: " + str(e))
return_val = 1
for tag in xml_config['attribs']:
found_items = tree.findall('.//'+tag)
for item in found_items:
for attrib in item.attrib:
if attrib.lower() not in xml_config['attribs'][tag]:
print("XML ATTRIB ERROR - attribute "+attrib+ \
" not allowed for tag "+tag+" in file "+filename)
return_val = 1
for tag in xml_config['types']:
found_items = tree.findall('.//'+tag)
for item in found_items:
if 'type' not in item.attrib:
print("XML TYPE ERROR - must have attribue 'type' for "+ \
" tag "+tag+" in file "+filename)
return_val = 1
else:
attrib = item.attrib['type']
if attrib.lower() not in xml_config['types'][tag]:
print("XML TYPE ERROR - type "+attrib+ \
" not allowed for tag "+tag+" in file "+filename)
return_val = 1
return return_val
def check_tune(facility,file):
"""
Checks given tune for proper xml format.
"""
filename = os.path.join(facility,'tune',file)
return_val = 0
try:
tree = etree.parse(filename,parser)
except Exception as e:
print("XML ERROR - Error in tune "+file)
print("Error message: " + str(e))
return_val = 1
for tag in xml_config['attribs']:
found_items = tree.findall('.//'+tag)
for item in found_items:
for attrib in item.attrib:
if attrib.lower() not in xml_config['attribs'][tag]:
print("XML ATTRIB ERROR - attribute "+attrib+ \
" not allowed for tag "+tag+" in file "+filename)
return_val = 1
try:
tree.xinclude()
except Exception as e:
print("XML INCLUDE ERROR - Error in tune "+file)
print("Error message: " + str(e))
return_val = 1
return return_val
def check_tunesequence(facility,file):
"""
Checks given tunesequence for proper xml format.
"""
filename = os.path.join(facility,'tune','tunesequence',file)
return_val = 0
try:
tree = etree.parse(filename,parser)
except Exception as e:
print("XML ERROR - Error in tunesequence "+file)
print("Error message: " + str(e))
return_val = 1
for tag in xml_config['attribs']:
found_items = tree.findall('.//'+tag)
for item in found_items:
for attrib in item.attrib:
if attrib.lower() not in xml_config['attribs'][tag]:
print("XML ATTRIB ERROR - attribute "+attrib+ \
" not allowed for tag "+tag+" in file "+filename)
return_val = 1
return return_val
def parse_acc_xml():
######## Main Test Sequence ########
parser = etree.XMLParser(remove_blank_text=True)
error_found = 0
for facility in ['isac', 'ariel', 'elinac', 'cyclotron']:
print("*** Checking ", facility, " ***")
print("*** Checking ", facility, " paths ***")
for path in os.listdir( os.path.join(facility, 'path') ):
value = check_path(facility, path)
if value == 1:
error_found = 1
print("*** Checking ", facility, " sequences ***")
for sequence in os.listdir( os.path.join(facility, 'sequence') ):
value = check_sequence(facility, sequence)
if value == 1:
error_found = 1
print("*** Checking ", facility, " tunes ***")
for tune in os.listdir( os.path.join(facility, 'tune') ):
if os.path.isfile( os.path.join(facility, 'tune', tune) ):
value = check_tune(facility, tune)
if value == 1:
error_found = 1
if os.path.exists( os.path.join(facility, 'tune','tunesequence') ):
print("*** Checking ", facility, " tune sequences ***")
for item in os.listdir( os.path.join(facility, 'tune','tunesequence') ):
if os.path.isfile(os.path.join(facility, 'tune','tunesequence',item)):
value = check_tunesequence(facility, item)
if value == 1:
error_found = 1
else:
for sub_tune in os.listdir(os.path.join(facility, 'tune','tunesequence',item)):
if os.path.isfile( os.path.join(facility, 'tune','tunesequence',item, sub_tune) ):
value = check_tunesequence(facility, os.path.join(item,sub_tune) )
if value == 1:
error_found = 1
return error_found
def test_acc_xml():
assert parse_acc_xml() == 0
\ No newline at end of file
[tune]
allowed_attribs = ["chargestate","energypernucleon","energy","id","mass"]
[set]
allowed_attribs = ["pv","value","tuneable"]
[seq]
allowed_attribs = []
[element]
allowed_attribs = ["id","type","s","l"]
allowed_types = ["bias","eq","eb","ee","el","acc","pdt", "deflx", "defly","Bfield","abcoil","mq","mcb","mb","sol","ymb","ecb","amp","rf","rfq","phase","common","strp","static_sol","static_acc","slit","fc","rpm","marker","yeb","emit","iv","fortline","harp","diag","acccolumn","sol","rf","rfq","rt","wienfy","wienf","extbz","lpm","pm","cem","col","sid","ffc","ftm","bb","scd","psid","att","nim","mcp","drift","emp","fit","ebis","yeb", "hecell","kick"]
[layout]
allowed_attribs = ["x","y","z"]
[epics]
allowed_attribs = []
[setpoint]
allowed_attribs = ["pv","pol","unit","min","max","type","rw","pvon"]
[readback]
allowed_attribs = ["pv","pol","unit","type"]
[optr]
allowed_attribs = ["theta","ip","cee","eltrdv","aperr","btip","entredge","exitedge","edgecurv","fieldindex","k1","k2","fullgap","eltrgap","freq","phase","scale","scalevmap","scalevmap1","scalevmap2","scalebz","mapfile","nlines","disabled","v11","phi11","freq11","v23","phi23","freq23","v35","freq35","phi35","intgdl","r","s11","s22","s33","s44","s55","s66","r12","r34","r56","r15","r25","r16","r26","bunchcharge","beamcurrent","start","end","varname","varmin","varmax","varval","varunit","aperrx", "aperry", "effl", "skaperr", "skbalance"]
import setuptools, shutil, os
# SK hack to keep facilities at top level of acc repo
# Easiest to move them to within acc module before building package
rootdir = os.path.abspath(os.path.dirname(__file__))
DATA_DIR = os.path.join( rootdir, 'acc', 'data')
if not os.path.exists( DATA_DIR ):
os.mkdir( DATA_DIR )
for facility in ['isac','ariel','elinac','cyclotron']:
src = os.path.join( rootdir,facility)
dst = os.path.join(DATA_DIR,facility)
if os.path.exists(dst):
shutil.rmtree(dst)
shutil.copytree(src,dst)
with open("README.md", "r") as fh:
long_description = fh.read()
# info on including datafiles and referencing their location after install.
# referenced as acc.ACCDIR
# https://stackoverflow.com/a/5601839
# https://python-packaging.readthedocs.io/en/latest/non-code-files.html
# https://stackoverflow.com/a/14159430
# MANIFEST.in info:
# https://docs.python.org/2/distutils/sourcedist.html#manifest-template
setuptools.setup(
name="acc",
version="0.0.2",
author="Spencer Kiy",
author_email="spencerkiy@triumf.ca",
description="Collection of xml files describing TRIUMF's beamlines.",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://gitlab.triumf.ca/hla/acc",
packages=setuptools.find_packages(),
package_dir={'acc': 'acc'},
package_data={'acc': ['data/*']},
include_package_data=True,
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
python_requires='>=3.4',
test_suite='nose.collector',
tests_require=['nose','lxml==4.1.1'],
)
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment