Ejemplo n.º 1
0
def urlEncode(value):
	unreserved = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~"
	sb = StringBuilder()
	bytes = Encoding.UTF8.GetBytes(value)

	for b in bytes:
		if b < 0x80 and unreserved.IndexOf(Convert.ToChar(b)) != -1:
			sb.Append(Convert.ToChar(b))
		else:
			sb.Append('%' + String.Format("{0:X2}", Convert.ToInt32(b)))

	return sb.ToString()
Ejemplo n.º 2
0
def GetObservedTimeseries(dictObsSensors, startDate, endDate, outputPath, log):
    try:
        key = 0
        connection = SqlClient.SqlConnection(strObservedDB)
        connection.Open()

        for key, value in dictObsSensors.iteritems():
            strSQL = 'SELECT SAMPLES.DT, SAMPLES.SAMPLEVALUE, SAMPLES.POINT '
            strSQL = strSQL + '  FROM HydroTel.dbo.SAMPLES SAMPLES '
            strSQL = strSQL + ' WHERE (SAMPLES.DT>=CONVERT(DATETIME,\'' + startDate.strftime(
                pyDTFormat) + '\',102)) '
            strSQL = strSQL + '   AND (SAMPLES.DT<=CONVERT(DATETIME,\'' + endDate.strftime(
                pyDTFormat) + '\',102)) '
            strSQL = strSQL + '   AND (SAMPLES.POINT=' + key.ToString(
            ) + ') ORDER BY SAMPLES.DT'
            cmd = SqlClient.SqlCommand(strSQL, connection)
            reader = cmd.ExecuteReader()
            strData = StringBuilder()

            while (reader.Read()):
                theTime = reader[0]
                theValue = reader[1]
                strData.Append(theTime.ToString(netDTFormat) + ',')
                if theValue == iDeleteValue:
                    strData.Append('-1E-30\n')
                else:
                    strData.Append(str(theValue) + '\n')

            reader.Close()

            if strData.Length > 0:
                # Write to file
                f = open(outputPath + "\\Observed\\" + value + ".txt", 'w')
                f.write("Time," + value + "\n")
                f.write(strData.ToString())
                f.close()
                log.write("Data import success for sensor id: " +
                          str(int(key)) + " " + value + "\n")
            else:
                log.write("No data for sensor id: " + str(int(key)) + " " +
                          value + "!!!!\n")

        connection.Close()
    except:
        if key == 0:
            exceptionMessage = "Failed to connect to database. Connecton string: " + strObservedDB + "\n"
        else:
            exceptionMessage = "Data import failed for sensor id: " + str(
                int(key)) + " " + value + "!!!!\n"

        log.write(exceptionMessage)
        raise
Ejemplo n.º 3
0
 def showOutPut(self):
     if outPuts.Count != 0:
         sb = StringBuilder()
         for s in outPuts:
             sb.Append(s)
         TaskDialog.Show("Name Updated", sb.ToString())
     else:
         TaskDialog.Show("Name Updated", "nothing updated")
Ejemplo n.º 4
0
def CmdOutputDataHandler(process, outline):
    strOutput = StringBuilder()
    strOutput.Append(outline.Data)
    if not outline.Data:
        print(0, " ")
        wtr.WriteLine(" ")
    else:
        print(outline.Data.Length, outline.Data)
        wtr.WriteLine(outline.Data)
Ejemplo n.º 5
0
	def parseNumber(json, index, success):
		index = JsonDecoder.skipWhitespace(json, index)
		lastIndex = JsonDecoder.getLastIndexOfNumber(json, index)
		charLength = (lastIndex - index) + 1

		sb = StringBuilder()
						
		for i in range(charLength):
			sb.Append(json[index + i])

		success, number = Double.TryParse(sb.ToString(), NumberStyles.Any, CultureInfo.InvariantCulture)
		index = lastIndex + 1
		
		return number, index, success
Ejemplo n.º 6
0
def doc_to_text(filename):

    word_application = Word.ApplicationClass()
    word_application.visible = False

    document = word_application.Documents.Open(filename)

    result = StringBuilder()

    for p in document.Paragraphs:
        result.Append(clean_text(p.Range.Text))

    document.Close()
    document = None

    word_application.Quit()
    word_application = None

    return result.ToString()
Ejemplo n.º 7
0
	def parseString(json, index, success):
		s = StringBuilder()
		index = JsonDecoder.skipWhitespace(json, index)
		c = json[index] # "
		index += 1
		complete = False
		
		while not complete:
			if index == json.Length:
				break

			c = json[index]
			index += 1

			if c == '"':
				complete = True
				break

			elif c == '\\':
				if index == json.Length:
					break

				c = json[index]
				index += 1

				if c == '"':
					s.Append('"')
				elif c == '\\':
					s.Append('\\')
				elif c == '/':
					s.Append('/')
				elif c == 'b':
					s.Append('\b')
				elif c == 'f':
					s.Append('\f')
				elif c == 'n':
					s.Append('\n')
				elif c == 'r':
					s.Append('\r')
				elif c == 't':
					s.Append('\t')
				elif c == 'u':
					remainingLength = json.Length - index

					if remainingLength >= 4:
						sb = StringBuilder()
						
						for i in range(4):
							sb.Append(json[index + i])

						success, codePoint = UInt32.TryParse(sb.ToString(), NumberStyles.HexNumber, CultureInfo.InvariantCulture)
						
						if not success:
							return String.Empty, index, success

						s.Append(Encoding.UTF32.GetString(BitConverter.GetBytes(codePoint)))
						index += 4

					else:
						break

			else:
				s.Append(c)

		if not complete:
			return None, index, False

		return s.ToString(), index, success
Ejemplo n.º 8
0
def exportSignalsToStream(dc, dayToExport, deviceNameToExport, targetStream, subroutineName, \
	exportFileSystemSignals, exportWifiSignals):
	"""
	Exports signal data of activity log to a stream.

	Keyword arguments:
	dc -- DataContext that should be used to read activity log (TimeCockpit.Data.DataContext)
	dayToExport -- Day that should be exported (DateTime; time-part has to be 0)
	deviceNameToExport -- Name of the device that should be exported
	targetStream -- Target Stream (System.IO.StreamWriter)
	subroutineName -- Name of the generated routine (should be unique for each export)
	exportFileSystemSignals -- Indicates whether file system signals should be exported (bool); default is False
	exportWifiSignals -- Indicates whether WIFI signals should be exported (bool); default is False
	"""

	# Internal helper function for extracting type name
	def extractTypeName(x):
		helper = str(x)
		helper = helper.Substring(0, helper.IndexOf(' '))
		helper = helper.Substring(helper.LastIndexOf('.') + 1)
		return helper

	# Specify all signal types that should be exported
	signalTypes = [ "APP_CleansedChangeSetSignal", "APP_CleansedComputerActiveSignal", "APP_CleansedEmailSentSignal",
		"APP_CleansedIpConnectSignal", "APP_CleansedPhoneCallSignal", "APP_CleansedShortMessageSignal", 
		"APP_CleansedUserActiveSignal", "APP_CleansedUserNoteSignal", "APP_CleansedWindowActiveSignal", 
		"APP_CleansedWorkItemChangeSignal" ]
	if exportFileSystemSignals:
		# Only export file system signals if explicitly asked for
		signalTypes.append("APP_CleansedFileSystemSignal")
	if exportWifiSignals:
		# Only export WIFI signals if explicitly asked for
		signalTypes.append("APP_CleansedWifiAvailableSignal")

	try:
		chunks = dc.SelectWithParams({
			"Query": "From C In SYS_Chunk.Include('SYS_Entity').Include('SYS_Device') " \
				"Where ((:Year(C.BeginTime) = @FilterYear And :Month(C.BeginTime) = @FilterMonth And :Day(C.BeginTime) = @FilterDay) " \
				"	Or (:Year(C.EndTime) = @FilterYear And :Month(C.EndTime) = @FilterMonth And :Day(C.EndTime) = @FilterDay)) " \
				"	And C.Device.DeviceName = @DeviceName Select C",
			"@FilterYear": dayToExport.Year, "@FilterMonth": dayToExport.Month, "@FilterDay": dayToExport.Day,
			"@DeviceName": deviceNameToExport })
		if (len(chunks) == 0):
			raise Exception("No chunks found. Please review export filter criteria.")
		
		# Write documentation header
		targetStream.WriteLine("# -*- coding: utf-8 -*-")
		targetStream.WriteLine("# This is an auto-generated script")
		targetStream.WriteLine("#\tGeneration date: {0}", DateTime.Today)
		targetStream.WriteLine("#\tSource device name: {0}", deviceNameToExport)
		targetStream.WriteLine("#\tSource day: {0}", dayToExport)
		targetStream.WriteLine()

		# Write necessary imports		
		targetStream.WriteLine("from System.Collections.Generic import List")
		targetStream.WriteLine()
		
		# Generate method that regenerates activity log
		targetStream.WriteLine("def {0}(dc, targetDay):", subroutineName)

		targetStream.WriteLine("\t\"\"\"", subroutineName)
		targetStream.WriteLine("\tGenerates activity log in the current user account")
		targetStream.WriteLine("")
		targetStream.WriteLine("\tKeyword arguments:")
		targetStream.WriteLine("\tdc -- DataContext that should be used to write activity log")
		targetStream.WriteLine("\ttargetDay -- Day into which the activity log should be imported")
		targetStream.WriteLine("\t\"\"\"", subroutineName)

		targetStream.WriteLine("\tentities = dc.Select(\"From E In SYS_Entity Select E\")")
		targetStream.WriteLine("\tdevice = dc.SelectSingle(\"From D In SYS_Device Select D\")")
		targetStream.WriteLine("\tif device is None:")
		targetStream.WriteLine("\t\traise Exception(\"No device found\")")
		targetStream.WriteLine("\ttimeCorrection = targetDay - DateTime({0}, {1}, {2})", dayToExport.Year, dayToExport.Month, dayToExport.Day)
		
		targetStream.WriteLine("\tdc.DbClient.BeginTransaction()")
		targetStream.WriteLine("\ttry:")
		for chunk in chunks:
			if (chunk.Entity.EntityName in signalTypes):
				targetStream.WriteLine("\t\tchunk = dc.CreateChunk({{ \"BeginTime\": DateTime({0}, {1}, {2}, {3}, {4}, {5}) + timeCorrection,", chunk.BeginTime.Year, chunk.BeginTime.Month, chunk.BeginTime.Day, chunk.BeginTime.Hour, chunk.BeginTime.Minute, chunk.BeginTime.Second)
				targetStream.WriteLine("\t\t\t\"EndTime\": DateTime({0}, {1}, {2}, {3}, {4}, {5}) + timeCorrection,", chunk.EndTime.Year, chunk.EndTime.Month, chunk.EndTime.Day, chunk.EndTime.Hour, chunk.EndTime.Minute, chunk.EndTime.Second)
				targetStream.WriteLine("\t\t\t\"LogicalBeginTime\": DateTime({0}, {1}, {2}, {3}, {4}, {5}) + timeCorrection,", chunk.LogicalBeginTime.Year, chunk.LogicalBeginTime.Month, chunk.LogicalBeginTime.Day, chunk.LogicalBeginTime.Hour, chunk.LogicalBeginTime.Minute, chunk.LogicalBeginTime.Second)
				targetStream.WriteLine("\t\t\t\"LogicalEndTime\": DateTime({0}, {1}, {2}, {3}, {4}, {5}) + timeCorrection,", chunk.LogicalEndTime.Year, chunk.LogicalEndTime.Month, chunk.LogicalEndTime.Day, chunk.LogicalEndTime.Hour, chunk.LogicalEndTime.Minute, chunk.LogicalEndTime.Second)
				targetStream.WriteLine("\t\t\t\"Entity\": [e for e in entities if e.EntityName == \"{0}\"][0],", chunk.Entity.EntityName)
				targetStream.WriteLine("\t\t\t\"Device\": device })")
				
				targetStream.WriteLine("\t\tchunkContentList = List[EntityObject]()")
				for signal in chunk.Content:
					signalContent = StringBuilder()
					for prop in signal.Entity.Properties:
						typeName = extractTypeName(prop)
						if (typeName <> "CalculatedProperty"):
							if (signalContent.Length > 0):
								signalContent.Append(", ")
							signalContent.AppendFormat("\"{0}\": ", prop.Name)
							if (typeName == "TextProperty"):
								signalContent.Append('"')
								t = eval("signal.{0}".format(prop.Name)).Replace("\\", "\\\\").Replace("\"", "\\\"").Replace("\n", "\\t").Replace("\r", "\\r")
								signalContent.Append(t)
								signalContent.Append('"')
							elif (typeName == "DateTimeProperty"):
								signalContent.Append('DateTime(')
								d = eval("signal.{0}".format(prop.Name))
								signalContent.AppendFormat("{0}, {1}, {2}, {3}, {4}, {5}", d.Year, d.Month, d.Day, d.Hour, d.Minute, d.Second)
								signalContent.Append(')')
								if (prop.Name == "APP_BeginTime" or prop.Name == "APP_EndTime" or prop.Name == "APP_EventTime"):
									signalContent.Append(" + timeCorrection")
							elif (typeName == "BooleanProperty"):
								b = eval("signal.{0}".format(prop.Name))
								signalContent.Append("True" if b else "False")
							elif (typeName == "GuidProperty"):
								signalContent.Append('Guid("')
								signalContent.Append(eval("signal.{0}".format(prop.Name)))
								signalContent.Append('")')
							elif (typeName == "NumericProperty"):
								n = eval("signal.{0}".format(prop.Name))
								signalContent.Append(n)
							else:
								raise Exception("MISSING TYPE {0}".format(typeName))
					targetStream.WriteLine("\t\tchunkContentList.Add(dc.Create{0}({{ {1} }}))", chunk.Entity.EntityName, signalContent.ToString())
				targetStream.WriteLine("\t\tchunk.Content = chunkContentList")
				targetStream.WriteLine("\t\tdc.SaveObject(chunk)")
		targetStream.WriteLine("\texcept:")
		targetStream.WriteLine("\t\tif (dc.DbClient.TransactionCount > 0):")
		targetStream.WriteLine("\t\t\tdc.DbClient.RollbackTransaction()")
		targetStream.WriteLine("\t\traise")
		targetStream.WriteLine("\tfinally:")
		targetStream.WriteLine("\t\tif (dc.DbClient.TransactionCount > 0):")
		targetStream.WriteLine("\t\t\tdc.DbClient.CommitTransaction()")
		targetStream.WriteLine()

		targetStream.WriteLine("# Uncomment and adapt the following two lines to enable the import execution")
		targetStream.WriteLine("# dc = Context")
		targetStream.WriteLine("# {0}(dc, DateTime({1}, {2}, {3}))", subroutineName, dayToExport.Year, dayToExport.Month, dayToExport.Day)
		
	finally:
		targetStream.Close()
Ejemplo n.º 9
0
    def GetDescriptionSubRecord(self, rec):
        p = self.page

        # table has up to 5 columns
        ss = structure = rec.Structure
        if not structure or not structure.elements:
            with p.table(id='record-desc'):
                if ss:
                    with p.thead():
                        with p.tr():
                            p.td(ss.name, class_='headerlabel', width="33%")
                            p.td(ss.desc, colspan='4', class_='header')
                #with p.tfoot(): # write a blank footer to fix the HtmlRenderer Control
                #	with p.tr(class_='hidden'):
                #		p.td('',class_='header',width="33%").td('').td('').td('').td('')
                with p.tr():
                    p.td("String:", width="33%", class_='label')
                    p.td(rec.GetStrData(), class_='value', colspan='4')
                with p.tr():
                    p.td("Hex:", width="33%", class_='label')
                    p.td(rec.GetHexData(), class_='value', colspan='4')
            return

        try:
            plugin = rec.GetPlugin()
            pluginFile = plugin.Name
            elems = [
                elem for elem in rec.EnumerateElements(True)
                if elem.Structure != None and not elem.Structure.notininfo
            ]
            if not elems:
                return

            with p.table(id='record-desc'):
                with p.thead():
                    with p.tr():
                        p.td(ss.name, class_='headerlabel')
                        p.td(ss.desc, colspan='4', class_='header')
                #with p.tfoot(): # write a blank footer to fix the HtmlRenderer Control
                #	with p.tr(class_='hidden'):
                #		p.td('',class_='header',width="33%").td('').td('').td('').td('')
                with p.tbody():
                    for elem in elems:
                        sselem = elem.Structure
                        ssname = self.GetElementName(elem)
                        value = self.GetElementValue(elem)
                        strValue = str(value)

                        with p.tr():
                            p.td(ssname, width="33%", class_='label')

                            if sselem.type == ElementValueType.Blob:
                                p.td(TypeConverter.GetHexData(elem.Data),
                                     class_='value',
                                     colspan='4')
                            elif sselem.type == ElementValueType.Str4:
                                p.td(TypeConverter.GetString(elem.Data),
                                     class_='text',
                                     colspan='4')
                            elif sselem.type == ElementValueType.BString:
                                p.td(TypeConverter.GetBString(elem.Data),
                                     class_='text',
                                     colspan='4')
                            elif sselem.type == ElementValueType.IString:
                                p.td(TypeConverter.GetIString(elem.Data),
                                     class_='text',
                                     colspan='4')
                            elif sselem.type == ElementValueType.FormID:
                                if not value:
                                    p.td(strValue, class_='value', colspan='4')
                                else:
                                    formid = value.ToString("X8")
                                    record = plugin.GetRecordByID(value)
                                    if not record:  # lookup plugin name using the id
                                        prefName = plugin.GetRecordMaster(
                                            value)
                                        with p.td(class_='formid',
                                                  colspan='4'):
                                            p.a(formid,
                                                href=createLink(
                                                    pluginFile,
                                                    sselem.FormIDType, formid,
                                                    prefName))
                                    else:  # lookup actual record to know actual type
                                        pref = record.GetPlugin()
                                        with p.td(class_='formid',
                                                  width="15%"):
                                            p.a(formid,
                                                href=createLink(
                                                    pluginFile, record.Name,
                                                    record.FormID.ToString(
                                                        "X8"), pref.Name))

                                        if record.Name != sselem.FormIDType:
                                            p.td(record.DescriptiveName,
                                                 class_='text',
                                                 width='20%')
                                        else:
                                            p.td(getEditorID(record),
                                                 class_='text',
                                                 width='20%')

                                        id, fullStr = getFullNameWithID(record)
                                        if id == None:
                                            p.td(fullStr,
                                                 class_='text',
                                                 colspan=2)
                                        else:
                                            p.td(id,
                                                 class_='textid',
                                                 width="15%")
                                            p.td(fullStr, class_='text')

                            elif sselem.type == ElementValueType.LString:
                                if elem.Type == ElementValueType.String:
                                    p.td(value, class_='text', colspan=4)
                                elif TypeConverter.IsLikelyString(elem.Data):
                                    p.td(TypeConverter.GetString(elem.Data),
                                         class_='text',
                                         colspan=4)
                                else:
                                    id = TypeConverter.h2i(elem.Data)
                                    p.td(id.ToString("X8"), class_='text')
                                    p.td(plugin.LookupFormStrings(id),
                                         class_='text',
                                         colspan=3)

                            elif sselem.type in (ElementValueType.SByte,
                                                 ElementValueType.Int,
                                                 ElementValueType.Short,
                                                 ElementValueType.Byte,
                                                 ElementValueType.UInt,
                                                 ElementValueType.UShort):

                                if sselem.type in (ElementValueType.Byte,
                                                   ElementValueType.UInt,
                                                   ElementValueType.UShort):
                                    intVal = Convert.ToUInt32(value)
                                else:
                                    intVal = Convert.ToInt32(value)

                                hasOptions = sselem.options != None and sselem.options.Length > 0
                                hasFlags = sselem.flags != None and sselem.flags.Length > 1

                                if sselem.hexview or hasFlags:
                                    hexstr = value.ToString(
                                        "X" + str(elem.Data.Count * 2))
                                    if sselem.hexviewwithdec:
                                        p.td(hexstr,
                                             class_='text',
                                             width="15%")
                                        p.td(strValue,
                                             class_='text',
                                             width="15%")
                                    else:
                                        p.td(hexstr,
                                             class_='text',
                                             colspan=3,
                                             width="30%")
                                else:
                                    p.td(strValue,
                                         class_='text',
                                         colspan=3,
                                         width="30%")

                                strDesc = ''
                                if hasOptions:
                                    for k in xrange(0, sselem.options.Length,
                                                    2):
                                        ok, intValOption = int.TryParse(
                                            sselem.options[k + 1])
                                        if ok and intVal == intValOption:
                                            strDesc = sselem.options[k]
                                elif hasFlags:
                                    sb = StringBuilder()
                                    for k in xrange(0, sselem.flags.Length, 1):
                                        if ((intVal & (1 << k)) != 0):
                                            if (sb.Length > 0):
                                                sb.Append("<br/>")
                                            sb.Append(sselem.flags[k])
                                    strDesc = sb.ToString()
                                p.td(strDesc,
                                     class_='desc',
                                     colspan=3,
                                     width='50%')
                                pass

                            else:
                                #p.td(str(sselem.type), class_='text',width='auto' )
                                p.td(strValue, class_='text', colspan=4)
        except Exception, e:
            p.p("Warning: Subrecord doesn't seem to match the expected structure",
                class_='danger')
            p.p(str(e), class_='danger')