Exemple #1
0
 def _getLastKnown(fileStream, pair, attr):
     line1 = getLinesWith(fs, pair[0])[0]
     line2 = getLinesWith(fs, pair[1])[0]
     if "no value" not in line1.lower(
     ) and "no value" not in line2.lower():
         self.__dict__[attr] = (float(line1.split(pair[0])[1].strip()),
                                float(line2.split(pair[1])[1].strip()))
     else:
         self.__dict__[attr] = (0, 0)
Exemple #2
0
    def __getLocationCoords(self, fileStream, type_=""):
        """
        A function used by loadCrowdSourceInfo to parse for multiple location coordinates
        in the file based on type (everything, GPS, or Network)
        """
        if type_ not in ["", "GPS", "Network"]:
            raise ValueError(
                "Your type of Location values were not one of the options:" +
                str(["", "GPS", "Network"]))
        #Now we will look for the specified types of Location information
        latitudes = getLinesWith(fileStream, type_ + "Latitude:")
        longitudes = getLinesWith(fileStream, type_ + "Longitude:")
        distsMoved = getLinesWith(fileStream, type_ + "DistanceMoved:")

        pairs = []
        #Now we are going to go through our three arrays, grabbing each value
        # and making a 3-element array out of the latitude, longitude, and distance moved
        #We will only go along the array up to the shortest array so that we don't try to
        # index beyond the array
        for i in range(min([len(latitudes),
                            len(longitudes),
                            len(distsMoved)])):
            pairs.append([
                latitudes[i].split(type_ + "Latitude:")[1].strip(),
                longitudes[i].split(type_ + "Longitude:")[1].strip(),
                distsMoved[i].split(type_ + "DistanceMoved:")[1].strip()
            ])
        #END FOR
        #Going through each 'pair' (of three elements) and casting them to a float if they
        # are numeric. Otherwise, we assume that they are 0
        for pair in pairs:
            newPair = []
            for elem in pair:
                try:
                    elem = float(elem)
                    newPair.append(elem)
                except:
                    newPair.append(0)
            #END FOR
            pairs[pairs.index(pair)] = newPair
        #END FOR
        #Making all of our 3-element pairs into tuples
        pairs = [tuple(pair) for pair in pairs]
        return pairs
    def __getAllCoordinates(self, fileStream):
        """
        A function used by loadFieldTestInfo to parse all of the Location pairs
         (i.e. Latitude and Longitude), and save in self.AllCoordPairs
        """
        #Getting all lines with the Latitude info
        latitudes = getLinesWith(fileStream, "Latitude:")
        #Now that we have all of the latitudes, we need to go back to those locations
        # in the file and get the following lines, which contain the Longitudes
        longitudes = []
        oldLoc = fileStream.tell()
        for goodLat in latitudes:
            line = "__"
            while line:
                if goodLat in line:
                    line = fileStream.readline()
                    longitudes.append(line)
                    break
                line = fileStream.readline()
            #END WHILE
            if latitudes.index(goodLat) != (len(longitudes) - 1):
                longitudes.append("Longitude: 0.0")
        #END FOR
        fileStream.seek(oldLoc)

        pairs = []
        #Now we are going to go through our three arrays, grabbing each value
        # and making a 2-element array out of the latitude and longitude.
        #We will only go along the array up to the shortest array so that we don't try to
        # index beyond the array
        for i in range(len(latitudes)):
            pairs.append([
                latitudes[i].split("Latitude:")[1].strip(),
                longitudes[i].split("Longitude:")[1].strip()
            ])
        #END FOR
        #Going through each 'pair' (of three elements) and casting them to a float if they
        # are numeric. Otherwise, we assume that they are 0
        for pair in pairs:
            newPair = []
            for elem in pair:
                try:
                    elem = float(elem)
                    newPair.append(elem)
                except:
                    newPair.append(0)
            #END FOR
            pairs[pairs.index(pair)] = newPair
        #END FOR
        #Making all of our 3-element pairs into tuples
        pairs = [tuple(pair) for pair in pairs]
        return pairs
    def loadHeaderInfo(self):
        """Initializes the object by parsing the data in the given file path."""
        #This opens the file, and stores the file stream into the variabe fs
        with open(self.FilePath) as fs:
            #Reading in the Date and Time of the test
            datetime = getLinesWith(fs, "Testing started at")

            #If we were returned something, then we need to parse the date and time
            #
            # TODO : Use datetime objects
            #
            if datetime:
                datetime = datetime[0].split("Testing started at")[1]
                #Removing the day of the week from the remaining text
                datetime = datetime[4:-1].strip()

                #Determining the Month, Day, and Time from the first part of the text
                monthName = datetime[:3]
                month = str(monthAbbrToNum(monthName))
                datetime = datetime.split(monthName)[1].strip()
                day = str(datetime[:2])
                datetime = datetime[2:].strip()
                time = str(datetime[:8])
                datetime = datetime[8:].strip()

                #The year cannot be assumed to be in the same place, in the same format, so we
                # will split on " 20", and the next two characters must be the suffix
                year = "20" + datetime.split(" 20")[1][:2]
                self.Date = month + "/" + day + "/" + year
                self.Time = time
            else:
                #I can't use the getLinesWith function as I do not know what I'm looking for exactly,
                # so I'll have to do some regular expression searching
                #Reading in a chunk of text
                allText = fs.read(100)
                #Splitting based on newline characters
                topChunk = allText.split("\n")[:5]
                from re import search
                datetime = ""

                for line in topChunk:
                    #Searches for a line which contains any two characters at the start
                    # of a line (hopefully numbers), then a forward slash "/", then two more
                    # characters, then another "/", and then two more characters. Hopefully, the
                    # only line that has this kind of string is the one that contains
                    # the date and time
                    if search("^../../..", line):
                        datetime = line
                        break
                #END FOR
                self.Date = datetime.split(" ")[0].strip()
                self.Time = datetime.split(" ")[1].strip()
Exemple #5
0
 def loadCrowdSourceInfo(self):
     """Initializes the object by parsing the data in the given file path from __init__."""
     #This opens the file, and stores the file stream into the variabe fs
     with open(self.FilePath, 'r') as fs:
         #We are going to first get the line that contains some of the basic
         # information, like the Client Type and the App Version
         temp = getLinesWith(fs, "Crowd Source")
         if temp:
             if "Phone" in temp[0]:
                 self.Devicetype = "Phone"
             elif "Desktop" in temp[0]:
                 self.Devicetype = "Desktop"
             else:
                 self.Devicetype = "UNKNOWN"
             #END IF/ELIF/ELSE
             version = temp[0].split("v")[1].strip()
             self.AppVersion = "v" + version
         else:
             raise RuntimeError(
                 "This file is not a Crowd Source CPUC file.")
         #END IF/ELSE
         self.Roaming = False
         #If the device was a phone, then we will parse accordingly
         if self.Devicetype == "Phone":
             self.__loadPhoneInfo(fs)
         elif self.Devicetype == "Desktop":
             self.__loadDesktopInfo(fs)
         elif self.Devicetype == "UNKNOWN":
             emptiesToSet = [
                 "Devicetype", "AppVersion", "OSName", "OSArchitecture",
                 "OSVersion", "JavaVersion", "JavaVendor", "Server", "Host",
                 "NetworkProvider", "NetworkOperator", "NetworkType",
                 "ConnectionType", "ConnectionName", "Environment",
                 "PhoneModel", "PhoneManufac", "PhoneAPIVer", "PhoneSDKVer",
                 "WiFiBSSID", "WiFiSSID", "LocationSource", "Latitude",
                 "Longitude", "DistanceMoved"
             ]
             self.setEmptysToDefault(attributes=emptiesToSet)
    def loadFieldTestInfo(self):
        """Parses data and info in given file (location is filePath) and stores it in the object's attributes"""
        #This opens the file, and stores the file stream into the variabe fs
        with open(self.FilePath, 'r') as fs:
            #Read in Operating System Header Information
            self.parseLineAndSetAttr(
                fileStream=fs,
                delimiter=["Name =", "Architecture =", ", Version ="],
                attribute=["OSName", "OSArchitecture", "OSVersion"],
                hasParts=True)
            #Read in Java Header Information
            self.parseLineAndSetAttr(fileStream=fs,
                                     delimiter=[": Version =", "Vendor ="],
                                     attribute=["JavaVersion", "JavaVendor"],
                                     hasParts=True)
            #Looping through pairs of delimiter and attribute pairs
            for pair in [("Server: ", "Server"), ("Host: ", "Host"),
                         ("NetworkProvider: ", "NetworkProvider"),
                         ("Network Provider: ", "NetworkProvider"),
                         ("NetworkOperator: ", "NetworkOperator"),
                         ("Device ID: ", "DeviceID"),
                         ("Host name: ", "DeviceID"),
                         ("ConnectionType: ", "ConnectionType"),
                         ("Location ID: ", "LocationID"),
                         ("Location: ", "LocationID")]:
                self.parseLineAndSetAttr(fileStream=fs,
                                         delimiter=pair[0],
                                         attribute=pair[1])
            #END FOR

            #Defining self.NetworkCarrier, based on the data in NetworkProvider and NetworkOperator
            if self.NetworkProvider in self.ConfirmedCarriers:
                self.NetworkCarrier = self.NetworkProvider
            elif self.NetworkOperator in self.ConfirmedCarriers:
                self.NetworkCarrier = self.NetworkOperator
            #These ELIFs are for the special cases when the Provider is 'sprint' or 'Verizon Wireless'
            elif self.NetworkProvider == "sprint":
                self.NetworkCarrier = "Sprint"
            elif self.NetworkOperator == "Verizon Wireless":
                self.NetworkCarrier = "Verizon"
            else:
                self.NetworkCarrier = "NA"
            #END IF/ELIF/ELSE

            #This is for the rare case when the Device ID was not recorded in the test for some reason
            if not self.DeviceID:
                self.DeviceID = "NA"

            #Determining which Tester used the device that conducted this test
            if self.DeviceID != "NA":
                self.Tester = "NA"
                #This converts the multiline string into a 2-D list, where the first
                # element is the device ID, and the second is the tester number
                dttable = [
                    elem.split(",") for elem in table.strip().split("\n")
                ]
                for pair in dttable:
                    if self.DeviceID in pair[0]:
                        self.Tester = pair[1].strip()
                        break
                #END FOR
            #END IF

            #Setting the Device Type based on the Date/Time line, and the file name
            if getLinesWith(
                    fs, "Testing started at") and "WBBD" not in self.Filename:
                self.DeviceType = "Phone"
            else:
                self.DeviceType = "Netbook"
            #END IF

            #Getting all of the Latitude and Longitude pairs from the file, and
            # then searching through them for the most accurate (ie. first pair,
            # starting from the end, to have non-zero values)
            self.AllCoordPairs = self.__getAllCoordinates(fileStream=fs)
            for pair in reversed(self.AllCoordPairs):
                if all([elem != 0 for elem in pair]):
                    self.Latitude = pair[0]
                    self.Longitude = pair[1]
                    break
            #END FOR
            if "Latitude" not in self.__dict__:
                self.Latitude = 0
                self.Longitude = 0
        #END WITH FILE

        #Creating an array of the variables that we want to set to "NA" if they are empty
        emptiesToSet = [
            "OSName", "OSArchitecture", "OSVersion", "JavaVersion",
            "JavaVendor", "Server", "Host", "NetworkProvider",
            "NetworkOperator", "NetworkCarrier", "ConnectionType"
        ]
        self.setEmptysToDefault(attributes=emptiesToSet)
    def parseLineAndSetAttr(self,
                            fileStream,
                            delimiter,
                            attribute,
                            hasParts=False):
        """
        Takes a file stream, and parses a specific line, gets the necessary values
        from the line, and sets them in the object. If the object does not have the specified
        variable (i.e. attribute is not specified in the class or __init__), then the function
        will create the variable.
        """
        #First, try to read from the file, to check if it is an actual file stream
        try:
            __ = fileStream.read(1)
            fileStream.seek(0)
        except:
            raise ValueError("You have not passed through an open file stream")
        #Now that we know we have an open file stream, we can perform the parsing function.
        #But first, we check that the delimiter is a string
        if not isinstance(delimiter, str) and not hasParts:
            raise ValueError(
                "The delimiter was {}, and must be a string.".format(
                    type(delimiter)))
        if not isinstance(delimiter, list) and hasParts:
            raise ValueError(
                "The delimiter was {}, and must be a list.".format(
                    type(delimiter)))

        #We check the variable hasParts, which is set to true if there are parts of the line that
        # hold separate values, like the OS and Java information in Phone versions of this testing
        if not hasParts:
            #If there is something in line, then we parse it out. Otherwise, the function is done, and
            # nothing is set that wasn't there
            if attribute not in self.__dict__:
                self.__dict__[attribute] = ""
            line = getLinesWith(fileStream, delimiter)
            if line:
                value = line[0].split(delimiter)[1].strip()
                if not value:
                    value = "NA"
                self.__dict__[attribute] = value
        else:
            #We need to check that the necessary argument types have been passed through
            # the function if hasParts was set to True
            if not isinstance(attribute, list):
                raise TypeError(
                    "You need to pass in a LIST of attributes to set")
            if len(delimiter) != len(attribute):
                raise ValueError(
                    "'delimiter' and 'attribute' must have the same number of values"
                )
            #END IFs
            #Now we are going to loop through each sub-delimiter, splitting the string on it. We also
            # keep track of what it's index is. The value parsed is then put into the variable
            # name from the tuple in subAttrs at the same index.
            for subDelimiter in delimiter:
                subDelimInd = delimiter.index(subDelimiter)
                #If there is something in line, then we parse it out. Otherwise, the function is done, and
                # nothing is set that wasn't there
                if attribute[subDelimInd] not in self.__dict__:
                    self.__dict__[attribute[subDelimInd]] = ""
                line = getLinesWith(fileStream, subDelimiter)
                if line:
                    value = line[0].split(subDelimiter)[1].strip().split(
                        ",")[0].strip()
                    if not value:
                        value = "NA"
                    self.__dict__[attribute[subDelimInd]] = value
Exemple #8
0
    def __loadPhoneInfo(self, fs):
        #Setting the values of our class variables
        #Read in Operating System Header Information
        self.parseLineAndSetAttr(
            fileStream=fs,
            delimiter=["Name =", "Architecture =", ", Version ="],
            attribute=["OSName", "OSArchitecture", "OSVersion"],
            hasParts=True)
        #Read in Java Header Information
        self.parseLineAndSetAttr(fileStream=fs,
                                 delimiter=[": Version =", "Vendor ="],
                                 attribute=["JavaVersion", "JavaVendor"],
                                 hasParts=True)
        #Read in many other values
        for pair in [("Server:", "Server"), ("Host:", "Host"),
                     ("NetworkProvider:", "NetworkProvider"),
                     ("NetworkOperator:", "NetworkOperator"),
                     ("NetworkType:", "NetworkType"),
                     ("ConnectionType:", "ConnectionType"),
                     ("This device was", "Environment"),
                     ("Phone Model:", "PhoneModel"),
                     ("Phone Manufacturer:", "PhoneManufac"),
                     ("API Version:", "PhoneAPIVer"),
                     ("SDK Version:", "PhoneSDKVer"),
                     ("WiFi BSSID:", "WiFiBSSID"), ("WiFi SSID:", "WiFiSSID")]:
            self.parseLineAndSetAttr(fileStream=fs,
                                     delimiter=pair[0],
                                     attribute=pair[1])

        #Determining if the device was Roaming or not.
        line = getLinesWith(fs, "Network is Roaming.")
        if line:
            self.Roaming = True
        line = getLinesWith(fs, "Network is Not Roaming.")
        if line:
            self.Roaming = False
        #Sometimes, there was a ":" in the output of the environment. This
        # little block below removes that
        self.Environment = self.Environment.replace(":", "").strip()

        #The Latitude and Longitude information between app versions is different, so
        # we need to have separate blocks for each. The main difference is that v1.0 has
        # just Latitude and Longitude, while v1.1 and later have GPS Lat, GPS Long,
        # Network Lat, and NetworkLong
        self.LocationSource = ""
        self.DistanceMoved = 0
        if self.AppVersion == "v1.0":
            #The Location source for v1.0 was always GPS, and Distance Moved does not apply
            self.LocationSource = "GPS"
            self.DistanceMoved = 0
            self.AllCoordPairs = self.__getLocationCoords(fs, "")
            for triplet in reversed(self.AllCoordPairs):
                if triplet[0] != 0 and triplet[1] != 0:
                    self.Latitude = triplet[0]
                    self.Longitude = triplet[1]
            #END FOR
            if "Latitude" not in self.__dict__:
                self.Latitude = 0
                self.Longitude = 0
        #If the App Version is v1.1 or greater, then we have GPS Lat/Long and
        # Network Lat/Long.
        else:
            self.__loadPhoneCoords(fs)
        #After all of this parsing, we just need to set anything that is empty to N/A
        emptiesToSet = ["Server", "Host", "WiFiBSSID", "WiFiSSID"]
        self.setEmptysToDefault(attributes=emptiesToSet)