Commit abbe7a78 authored by Daniel Brown's avatar Daniel Brown
Browse files

Merge branch 'master' of gitmaster.atlas.aei.uni-hannover.de:pykat/pykat

Conflicts:
	pykat/finesse.py
parents 99b161a1 a77ef735
import os
import re
"""
class SIfloat(value):
def __init__(self, value):
self.__value = value
"""
#staticmethod
def SIfloat(value):
value=str(value)
__prefix = {'y': 1e-24, # yocto
'z': 1e-21, # zepto
'a': 1e-18, # atto
'f': 1e-15, # femto
'p': 1e-12, # pico
'n': 1e-9, # nano
'u': 1e-6, # micro
'm': 1e-3, # mili
'c': 1e-2, # centi
'd': 1e-1, # deci
'k': 1e3, # kilo
'M': 1e6, # mega
'G': 1e9, # giga
'T': 1e12, # tera
'P': 1e15, # peta
'E': 1e18, # exa
'Z': 1e21, # zetta
'Y': 1e24, # yotta
}
for i, j in __prefix.iteritems():
value=value.replace(i, str(j))
return float(value)
......@@ -8,10 +8,12 @@ import exceptions
import pykat.exceptions as pkex
import pykat
from pykat.node_network import *
from pykat.exceptions import *
import pykat.gui.resources
import pykat.gui.graphics
from pykat.gui.graphics import *
from pykat.SIfloat import *
next_component_id = 1
......@@ -261,8 +263,8 @@ class space(Component):
self._requested_node_names.append(node1)
self._requested_node_names.append(node2)
self.__L = float(L)
self.__n = float(n)
self.__L = SIfloat(L)
self.__n = SIfloat(n)
self._QItem = None
@property
......@@ -358,7 +360,7 @@ class laser(Component):
elif len(values) == 4:
return laser(values[0],values[3],P=values[1],f_offset=values[2], phase=0)
else:
raise exceptions.RuntimeError("Laser Finesse code format incorrect '{0}'".format(text))
raise exceptions.FinesseParse("Laser Finesse code format incorrect '{0}'".format(text))
def getFinesseText(self):
nodes = self.getNodes()
......
import exceptions
class BasePyKatException(Exception):
def __init__(self, msg):
self.__msg = msg
......@@ -7,6 +8,10 @@ class BasePyKatException(Exception):
def __str__(self):
return self.__msg
class FinesseParse(BasePyKatException) :
def __init__(self, msg):
BasePyKatException.__init__(self, "Error parsing Finesse input\n{0}".format(msg))
class MissingFinesseEnvVar(BasePyKatException) :
def __init__(self):
BasePyKatException.__init__(self, "The environment variable FINESSE_DIR was not defined")
......
......@@ -32,6 +32,7 @@ import datetime
import pickle
import pykat
import warnings
import re
from pykat.exceptions import *
......@@ -126,17 +127,25 @@ class kat(object):
def noxaxis(self,value): self.__noxaxis = bool(value)
def loadKatFile(self, katfile):
with open(katfile) as f:
self.parseCommands(f.readlines())
commands=open(katfile).read()
self.parseCommands(commands)
def parseKatCode(self, code):
#commands = code.split("\n")
self.parseCommands(commands)
def parseCommands(self, commands):
blockComment = False
self.__currentTag= NO_BLOCK
if not (NO_BLOCK in self.__blocks):
self.__blocks[NO_BLOCK] = Block(NO_BLOCK)
commands=self.remove_comments(commands)
for line in commands.split("\n"):
#for line in commands:
if len(line.strip()) >= 2:
line = line.strip()
......@@ -536,3 +545,17 @@ class kat(object):
def __get_component(self, name):
return getattr(self, '__comp_' + name)
def remove_comments(self, string):
pattern = r"(\".*?\"|\'.*?\'|%{3}[^\r\n]*$)|(/\*.*?\*/|%[^\r\n]*$|#[^\r\n]*$|//[^\r\n]*$)"
# first group captures quoted strings (double or single)
# second group captures comments (//single-line or /* multi-line */)
regex = re.compile(pattern, re.MULTILINE|re.DOTALL)
def _replacer(match):
# if the 2nd group (capturing comments) is not None,
# it means we have captured a non-quoted (real) comment string.
if match.group(2) is not None:
return "" # so we will return empty to remove the comment
else: # otherwise, we will return the 1st group
return match.group(1) # captured quoted-string
return regex.sub(_replacer, string)
Supports Markdown
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