Procházet zdrojové kódy

Epics is back
- own config file
- all handled by epics_widget.py

Matthias Martin před 6 roky
rodič
revize
84dc10d795
6 změnil soubory, kde provedl 133 přidání a 92 odebrání
  1. 9 1
      KCG/base/kcg.py
  2. 1 1
      KCG/config.py
  3. 0 18
      KCG/default_config.cfg
  4. 1 0
      KCG/kcg.pro
  5. 1 1
      KCG/widgets/__init__.py
  6. 121 71
      KCG/widgets/epics_widget.py

+ 9 - 1
KCG/base/kcg.py

@@ -40,7 +40,6 @@ def readconfig(parent):
     :param parent: parent for popup windows
     :return: -
     """
-
     nec_conf = ['acquireSettingsIcon', 'bunches_per_turn', 'default_log_entries', 'default_save_location', 'default_subdirectory_name',
                  'force_ask', 'guiIcon', 'language', 'logCommentIcon',
                  'logIcon', 'newPlotDataIcon', 'newPlotLiveIcon', 'save_header', 'show_advanced_control', 'singleReadIcon', 'startIcon',
@@ -222,14 +221,18 @@ class Gui(QtGui.QMainWindow):
         self.setStyleSheet(styleSheet)
 
 
+
+
         # evaluate config file regarding advanced_control
         self.showAdvancedControl(config.show_advanced_control)
         self.storage.advanced_control = config.show_advanced_control
 
         if not os.path.isdir(os.path.join(storage.storage.save_location, storage.storage.subdirname)):
             os.makedirs(     os.path.join(storage.storage.save_location, storage.storage.subdirname))
+        
         self.measurementLogger = log.MeasurementLogger()
         log.logger = self.measurementLogger
+
         logStrings = []
         functionAndParameter = []
         for par in self.measurementLogger.predefined_parameters:  # get strings and functions in seperate lists
@@ -245,6 +248,11 @@ class Gui(QtGui.QMainWindow):
         #    logging.error("Epics PVs could not be accessed. Check Internet connection and Epics PV provider. Logfiles will not contain"
         #                "information that is to be obtained via epics.")
 
+        # activate Epics if defined
+        if config.use_epics:
+            from ..widgets import epics_widget
+            epics_widget.setup()
+
 
     def doMenu(self):
         """

+ 1 - 1
KCG/config.py

@@ -142,7 +142,7 @@ class Configuration(object):
         defaultConfig.read(os.path.join(os.path.dirname(__file__), "default_config.cfg"))
 
         Machine_conf = ["bunches_per_turn", "save_header", "tRev"]
-        Ui_conf = ["language", "default_save_location", "default_subdirectory_name", "force_ask", "show_advanced_control", "integrate_single_read"]
+        Ui_conf = ["language", "default_save_location", "default_subdirectory_name", "force_ask", "show_advanced_control", "integrate_single_read", "use_epics"]
         Logging_conf = ["default_log_entries"]
         Misc_conf = ['newPlotLiveIcon', 'newPlotDataIcon', 'timingIcon', 'singleReadIcon',
                      'acquireSettingsIcon', 'startIcon', 'stopIcon', 'logIcon', 'logCommentIcon', 'guiIcon', 'style',

+ 0 - 18
KCG/default_config.cfg

@@ -51,24 +51,6 @@ show_advanced_control = False
 integrate_single_read = False
 
 [Logging]
-# These are PVs that will be possible to insert into log files
-# This variable is to be a list consisting of touples of two entries,
-# the first ist the Text that describes the value and the second is the EPICS PV that
-# holds that value
-epics_log_entry_pvs = [
-    ("Beam Energy (GeV)", "A:SR:BeamInfo:01:Energy"),
-    ("Beam Current (mA)", "A:SR:BeamInfo:01:Current"),
-    ("Beam Lifetime (s)", "A:SR:BeamInfo:01:Lifetime"),
-    ("Beam Lifetime (H:M:S)", "A:SR:BeamInfo:01:Lifetime:String"),
-    ("Injection Rate (mA/s)", "A:SR:BeamInfo:01:InjRate"),
-    ("Injection Rate (mA/5s)", "A:SR:BeamInfo:01:InjRate:5s"),
-    ("Fill Number", "A:SR:OperationStatus:01:FillNumber")
-    ]
-# This pv is used to determine if epics pvs are accessible
-epics_test_pv = "A:SR:BeamInfo:01:Current"
-# Path to your epics base installation
-epics_base_path = "/opt/epics/base/"
-
 # List of Entries that are default to save in Log
 # Possible Values are:
 # "Number of Orbits"

+ 1 - 0
KCG/kcg.pro

@@ -4,6 +4,7 @@ SOURCES += ./base/kcg.py \
 ./widgets/example_widget.py \
 ./widgets/singleread.py \
 ./widgets/acquiresettings.py \
+./widgets/epics_widget.py \
 ./kcg.py \
 ./base/groupedelements.py \
 ./base/multiWidget.py \

+ 1 - 1
KCG/widgets/__init__.py

@@ -1 +1 @@
-__all__ = ['acquiresettings', 'singleread', 'timingWidget'] #, 'example_widget']
+__all__ = ['acquiresettings', 'singleread', 'timingWidget', 'epics_widget'] #, 'example_widget']

+ 121 - 71
KCG/widgets/epics_widget.py

@@ -1,71 +1,121 @@
-from PyQt4 import QtGui
-
-
-from ..base import kcgwidget as kcgw
-from ..base.globals import glob as global_objects
-
-__widget_id__ = None
-
-epics_reachable = True
-try:  # try to import epics
-    import epics
-    no_epics = False
-
-    os.environ["EPICS_BASE"] = config.epics_base_path
-
-    try:  # if import was successful, try to find libca and test for connectivity with config.epics_test_pv
-        # sys.stderr = os.devnull  # epics.ca.find_libca() prints a useless None to stderr if libca is not found
-        epics.ca.find_libca()
-        # sys.stderr = sys.__stderr__
-        if epics.caget(config.epics_test_pv) == None:
-            no_epics = True
-            epics_reachable = False
-            logging.error("Epics is not accessible (possible Timeout)")
-
-    except epics.ca.ChannelAccessException as e:
-        if str(e) == "cannot find Epics CA DLL":
-            no_epics = True
-            logging.error("Epics CA DLL not found")
-
-
-except ImportError:
-    no_epics = True
-
-class EpicsWidget(kcgw.KCGWidgets):
-    def __init__(self, unique_id, parent):
-        super(EpicsWidget, self).__init__()
-
-        self.id = unique_id
-        self.par = parent
-
-        self.layout = QtGui.QHBoxLayout()
-        self.setLayout(self.layout)
-
-        self.button1 = self.createButton("Button 1", connect=self.pressed)
-        self.button2 = self.createButton("Button 2", connect=self.pressed)
-
-        self.layout.addWidget(self.button1)
-        self.layout.addWidget(self.button2)
-
-        self.setWindowTitle("Epics Widget")
-
-    def pressed(self):
-        print('Pressed')
-
-    def closeEvent(self, event):
-        global __widget_id__
-        __widget_id__ = None
-        del self.par.widgets[self.id]
-
-
-def addExampleWidget():
-    global __widget_id__
-    if __widget_id__:
-        global_objects.get_global('area').widgets[__widget_id__].setFocus()
-    else:
-        nid = kcgw.idg.genid()
-        __widget_id__ = nid
-        w = exampleWidget(nid, global_objects.get_global('area'))
-        global_objects.get_global('area').newWidget(w, "Epics Widget", nid, widget_type=4)
-
-kcgw.register_widget(QtGui.QIcon(config.icon_path("sproject.svg")), "Epics Widget", addExampleWidget, "Ctrl+e")
+from PyQt4 import QtGui
+
+
+from ..base import kcgwidget as kcgw
+from ..base.globals import glob as global_objects
+from ..base import log
+from .. import config
+import configparser
+import os
+
+__widget_id__ = None
+
+try:  # try to import epics
+    import epics
+    no_epics = False
+except ImportError:
+    no_epics = True
+    logging.error("Python Epics not found")
+
+
+def setup():
+
+    load_config()
+    
+    os.environ["EPICS_BASE"] = config.epics_base_path
+    try:  # if import was successful, try to find libca and test for connectivity with config.epics_test_pv
+        # sys.stderr = os.devnull  # epics.ca.find_libca() prints a useless None to stderr if libca is not found
+        epics.ca.find_libca()
+        # sys.stderr = sys.__stderr__
+        if epics.caget(config.epics_test_pv) == None:
+            no_epics = True
+            epics_reachable = False
+            logging.error("Epics is not accessible (possible Timeout)")
+
+    except epics.ca.ChannelAccessException as e:
+        if str(e) == "cannot find Epics CA DLL":
+            no_epics = True
+            logging.error("Epics CA DLL not found")
+
+    for entry in config.epics_log_entry_pvs:
+        log.logger.predefined_parameters.append([entry[0], [epics.caget, entry[1]]])
+        if entry[2] == "True":
+            log.logger.register_parameter(entry[0], epics.caget, entry[1])
+
+
+def load_config():
+
+    epics_key = ["epics_log_entry_pvs", "epics_test_pv", "epics_base_path"]
+    defaultstring = '[EPICS]\n# These are PVs that will be possible to insert into log files\n# This variable is to be a list consisting of touples of two entries,\n# the first ist the Text that describes the value and the second is the EPICS PV that\n# holds that value\n epics_log_entry_pvs = [\n    ("Beam Energy (GeV)", "A:SR:BeamInfo:01:Energy"),\n     ("Beam Current (mA)", "A:SR:BeamInfo:01:Current"),\n    ("Beam Lifetime (s)", "A:SR:BeamInfo:01:Lifetime"),\n    ("Beam Lifetime (H:M:S)", "A:SR:BeamInfo:01:Lifetime:String"),\n    ("Injection Rate (mA/s)", "A:SR:BeamInfo:01:InjRate"),\n    ("Injection Rate (mA/5s)", "A:SR:BeamInfo:01:InjRate:5s"),\n    ("Fill Number", "A:SR:OperationStatus:01:FillNumber")\n    ]\n# This pv is used to determine if epics pvs are accessible\n epics_test_pv = "A:SR:BeamInfo:01:Current"\n# Path to your epics base installation\n epics_base_path = "/opt/epics/base/"'
+
+    if not os.path.isfile(os.path.join(os.path.expanduser("~"),".kcg","epics.cfg")):
+        with open(os.path.join(os.path.expanduser("~"),'.kcg','epics.cfg', 'w+')) as f:
+            f.write(defaultstring)
+    
+    parser = configparser.ConfigParser()
+    parser.optionxform = str
+    parser.read(os.path.join(os.path.expanduser("~"),".kcg","epics.cfg"))
+
+    c_epics = config.ConfSection('EPICS', epics_key, parser)
+    error = c_epics.read()
+    
+
+
+
+
+
+
+
+
+
+# 8888888888        d8b                888       888d8b     888                888
+# 888               Y8P                888   o   888Y8P     888                888
+# 888                                  888  d8b  888        888                888
+# 8888888   88888b. 888 .d8888b.d8888b 888 d888b 888888 .d88888 .d88b.  .d88b. 888888
+# 888       888 "88b888d88P"   88K     888d88888b888888d88" 888d88P"88bd8P  Y8b888
+# 888       888  888888888     "Y8888b.88888P Y88888888888  888888  88888888888888
+# 888       888 d88P888Y88b.        X888888P   Y8888888Y88b 888Y88b 888Y8b.    Y88b.
+# 888888888888888P" 888 "Y8888P 88888P'888P     Y888888 "Y88888 "Y88888 "Y8888  "Y888
+#           888                                                     888
+#           888                                                Y8b d88P
+#           888                                                 "Y88P"
+#
+
+class EpicsWidget(kcgw.KCGWidgets):
+    def __init__(self, unique_id, parent):
+        super(EpicsWidget, self).__init__()
+
+        self.id = unique_id
+        self.par = parent
+
+        self.layout = QtGui.QHBoxLayout()
+        self.setLayout(self.layout)
+
+        self.button1 = self.createButton("Button 1", connect=self.pressed)
+        self.button2 = self.createButton("Button 2", connect=self.pressed)
+
+        self.layout.addWidget(self.button1)
+        self.layout.addWidget(self.button2)
+
+        self.setWindowTitle("Epics Widget")
+
+    def pressed(self):
+        print('Pressed')
+
+    def closeEvent(self, event):
+        global __widget_id__
+        __widget_id__ = None
+        del self.par.widgets[self.id]
+
+
+def addEpicsWidget():
+    global __widget_id__
+    if __widget_id__:
+        global_objects.get_global('area').widgets[__widget_id__].setFocus()
+    else:
+        nid = kcgw.idg.genid()
+        __widget_id__ = nid
+        w = EpicsWidget(nid, global_objects.get_global('area'))
+        global_objects.get_global('area').newWidget(w, "Epics Widget", nid, widget_type=4)
+
+kcgw.register_widget(QtGui.QIcon(config.icon_path("sproject.svg")), "Epics Widget", addEpicsWidget, "Ctrl+e")