def dummyFile(self, filename, expected): f = self.tmp.write(filename, b'EOF') crawler = Crawler() ref = Sourcefile._make( ['', '', expected[0], expected[1], expected[2]]) res = crawler._parseFileName(f) self.assertEquals(ref[2], res[2]) self.assertEquals(ref[3], res[3]) self.assertEquals(ref[4], res[4])
def _parseFileName(self, f): """ Apply a RegEx on a filename to extract the sensor's orientation and session ID (aka patient ID); if an exercise ID is available, match that too. See the source code for available orientation parameters. :param str f: Path of the file :raises PermissionError: Indicating that no read access is provided :return: Parsed sourcfile information :rtype: :class:`Sourcefile <lib.shared.Sourcefile>` """ if not os.access(f, os.R_OK): try: raise PermissionError("No read access granted on '{}', " " skipping file") except Exception: return False attr = [''] * 5 # create list for file attributes attr[0], attr[1] = os.path.split(f) # 0: fullpath 1:filename # list of regexes we run the filename against ## orientation is either left or right ## exercise is prefixed by 'E' and sometimes followed by the orientation ## session is either prefixed by 'P', 'GA', 'GAstd' or 'Pat' orientation = re.compile("(left|right)", re.IGNORECASE|re.M) exercise = re.compile("E([A-Za-z0-9]+)(?:left|right)?", re.M) session = re.compile("P([0-9]+)E?|GA([0-9]+)|GAstd([0-9a-z]+)(?:left|right)|Pat([0-9a-z]+)(?:left|right)", re.IGNORECASE|re.M) orientationMatch = orientation.search(attr[1]) exerciseMatch = exercise.search(attr[1]) sessionMatch = session.search(attr[1]) # extract the matches if orientationMatch: m = orientationMatch.group(1).lower() if m == 'left': attr[4] = Orientation.left elif m == 'right': attr[4] = Orientation.right if exerciseMatch: attr[3] = exerciseMatch.group(1) if sessionMatch: for i in range(1, len(sessionMatch.groups()) + 1): if sessionMatch.group(i): attr[2] = sessionMatch.group(i) break return Sourcefile._make(attr)