Adding support for date and time data types
authorlbessard
Mon, 25 Jun 2007 18:57:14 +0200
changeset 24 364320323b4d
parent 23 cce8d5662738
child 25 8dc68e669d99
Adding support for date and time data types
PLCControler.py
examples/example.xml
plcopen/TC6_XML_V10_B.xsd
xmlclass/xmlclass.py
--- a/PLCControler.py	Thu Jun 21 10:23:26 2007 +0200
+++ b/PLCControler.py	Mon Jun 25 18:57:14 2007 +0200
@@ -25,12 +25,15 @@
 from minixsv import pyxsval
 import cPickle
 import os,sys,re
+from datetime import *
 
 from plcopen import plcopen
 from plcopen.structures import *
 from graphics.GraphicCommons import *
 from PLCGenerator import *
 
+duration_model = re.compile("(?:([0-9]{1,2})h)?(?:([0-9]{1,2})m)?(?:([0-9]{1,2})s)?(?:([0-9]{1,3})ms)?")
+
 [ITEM_UNEDITABLE, ITEM_PROJECT, ITEM_POU, ITEM_CLASS, ITEM_VARIABLE,
  ITEM_TRANSITION, ITEM_ACTION, ITEM_CONFIGURATION, ITEM_RESOURCE] = range(9)
 
@@ -1776,8 +1779,16 @@
             new_task.setName(task["Name"])
             if task["Single"] != "":
                 new_task.setSingle(task["Single"])
-            if task["Interval"] != "":
-                new_task.setInterval(task["Interval"])
+            result = duration_model.match(task["Interval"]).groups()
+            if reduce(lambda x, y: x or y != None, result):
+                values = []
+                for value in result:
+                    if value != None:
+                        values.append(int(value))
+                    else:
+                        values.append(0)
+                values[3] = values[3] * 1000
+                new_task.setInterval(time(*values))
             new_task.priority.setValue(int(task["Priority"]))
             if task["Name"] != "":
                 task_list[task["Name"]] = new_task
@@ -1807,7 +1818,16 @@
                 new_task["Single"] = ""
             interval = task.getInterval()
             if interval:
-                new_task["Interval"] = interval
+                text = ""
+                if interval.hour != 0:
+                    text += "%dh"%interval.hour
+                if interval.minute != 0:
+                    text += "%dm"%interval.minute
+                if interval.second != 0:
+                    text += "%ds"%interval.second
+                if interval.microsecond != 0:
+                    text += "%dms"%(interval.microsecond / 1000)
+                new_task["Interval"] = text
             else:
                 new_task["Interval"] = ""
             new_task["Priority"] = str(task.priority.getValue())
--- a/examples/example.xml	Thu Jun 21 10:23:26 2007 +0200
+++ b/examples/example.xml	Mon Jun 25 18:57:14 2007 +0200
@@ -868,7 +868,7 @@
     <configurations>
       <configuration name="ConfigTest">
         <resource name="ResourceTest">
-          <task interval="10ms" name="Toto" priority="6">
+          <task interval="01:00:00.100000" name="Toto" priority="6">
             <pouInstance type="SFCTest" name="Program1"/>
           </task>
           <globalVars>
--- a/plcopen/TC6_XML_V10_B.xsd	Thu Jun 21 10:23:26 2007 +0200
+++ b/plcopen/TC6_XML_V10_B.xsd	Mon Jun 25 18:57:14 2007 +0200
@@ -263,7 +263,7 @@
 																		</xsd:sequence>
 																		<xsd:attribute name="name" type="xsd:string" use="required"/>
 																		<xsd:attribute name="single" type="xsd:string" use="optional"/>
-																		<xsd:attribute name="interval" type="xsd:string" use="optional"/>
+																		<xsd:attribute name="interval" type="xsd:time" use="optional"/>
 																		<xsd:attribute name="priority" use="required">
 																			<xsd:simpleType>
 																				<xsd:restriction base="xsd:integer">
--- a/xmlclass/xmlclass.py	Thu Jun 21 10:23:26 2007 +0200
+++ b/xmlclass/xmlclass.py	Mon Jun 25 18:57:14 2007 +0200
@@ -37,9 +37,9 @@
 """
 Regular expression models for extracting dates and times from a string
 """
-time_model = re.compile('([0-9]{2}):([0-9]{2}):([0-9]{2})')
+time_model = re.compile('([0-9]{2}):([0-9]{2}):([0-9]{2}(?:.[0-9]*)?)')
 date_model = re.compile('([0-9]{4})-([0-9]{2})-([0-9]{2})')
-datetime_model = re.compile('([0-9]{4})-([0-9]{2})-([0-9]{2})[ T]([0-9]{2}):([0-9]{2}):([0-9]{2})')
+datetime_model = re.compile('([0-9]{4})-([0-9]{2})-([0-9]{2})[ T]([0-9]{2}):([0-9]{2}):([0-9]{2}(?:.[0-9]*)?)')
 
 """
 Dictionaries for stocking Classes and Types created from XML
@@ -254,11 +254,11 @@
         elif type_compute in ["string","anyURI","NMTOKEN"]:
             return StringType, "\"\""
         elif type_compute == "time":
-            return TimeType, "time(0,0,0)"
+            return TimeType, "time(0,0,0,0)"
         elif type_compute == "date":
             return DateType, "date(1,1,1)"
         elif type_compute == "dateTime":
-            return DateTimeType, "datetime(1,1,1,0,0,0)"
+            return DateTimeType, "datetime(1,1,1,0,0,0,0)"
         elif type_compute == "language":
             return StringType, "\"en-US\""
         else:
@@ -299,7 +299,10 @@
     elif type_compute == "time":
         result = time_model.match(value)
         if result:
-            time_values = [int(v) for v in result.groups()]
+            values = result.groups()
+            time_values = [int(v) for v in values[:2]]
+            seconds = float(values[2])
+            time_values.extend([int(seconds), int((seconds % 1) * 1000000)])
             return time(*time_values)
         else:
             raise ValueError, "\"%s\" is not a valid time!"%value
@@ -313,7 +316,10 @@
     elif type_compute == "dateTime":
         result = datetime_model.match(value)
         if result:
-            datetime_values = [int(v) for v in result.groups()]
+            values = result.groups()
+            datetime_values = [int(v) for v in values[:5]]
+            seconds = float(values[5])
+            datetime_values.extend([int(seconds), int((seconds % 1) * 1000000)])
             return datetime(*datetime_values)
         else:
             raise ValueError, "\"%s\" is not a valid datetime!"%value