Created
August 14, 2020 03:21
-
-
Save loonghao/b7da0647e1a9e94e1603ff85fa46e10c to your computer and use it in GitHub Desktop.
Provides function to parse dependencies files from clarisse working file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Provides function to Parse dependencies files from working file.""" | |
# Import built-in modules | |
from functools import partial | |
import re | |
class Parser(object): | |
"""Parser for parsing dependencies files from work file. | |
>>> parser = Parser("your/work/file.project") | |
>>> parser.run() | |
""" | |
def __init__(self, work_file): | |
self._work_file = work_file | |
self.files = [] | |
@staticmethod | |
def read_project_file(project_file): | |
files = [] | |
unread_files = [] | |
with open(project_file, 'r') as src: | |
_read = partial(src.read, 4096) | |
for chunk in iter(_read, b''): | |
match_files = re.findall(r'.+filename \"(.+)\"', chunk) | |
for file_ in match_files: | |
if file_.endswith('.project'): | |
unread_files.append(file_) | |
else: | |
files.append(file_) | |
return files, unread_files | |
def parsing_project_files(self): | |
files, projects = self.read_project_file(self._work_file) | |
for project in projects: | |
current_files, unread_files = self.read_project_file(project) | |
for unread_file in unread_files: | |
if unread_file in projects: | |
continue | |
projects.append(unread_file) | |
for current_file in current_files: | |
if current_file in files: | |
continue | |
files.append(current_file) | |
return files + projects | |
@staticmethod | |
def find_sequence_files(path): | |
"""Return a list containing all sequence files of `path`. | |
Args: | |
path (str): Absolute filepath or filepath pattern. | |
Returns: | |
:obj:`list` of :obj:`str`: Absolute paths of dependent files. | |
""" | |
base_name = os.path.basename(path) | |
# https://regex101.com/r/omA0q9/1 | |
regex = pixo_naming.get_key_pattern("counter") | |
re.sub(r"\.{}\.".format(regex), base_name, ".*.") | |
return glob.iglob(path) | |
@staticmethod | |
def lower_drive_letter(path): | |
"""Lower the windows drive letter. | |
Args: | |
path (str): The path to convert. | |
Returns: | |
str: The path using a lower drive letter if it contains one. | |
""" | |
match = re.match(r'^[A-Z]:', path) | |
if match: | |
drive_letter = match.group() | |
return path.replace(drive_letter, drive_letter.lower()) | |
return path | |
@staticmethod | |
def organize_files(files): | |
"""Unify file path and deduplicate files. | |
Args: | |
files (:obj:`list` of :obj:`str`): Absolute paths of depending | |
files. | |
Returns: | |
(:obj:`list` of :obj:`str`): Organized file paths. | |
""" | |
files_ = set() | |
for file_ in files: | |
file_ = self.lower_drive_letter(file_.replace("\\", "/")) | |
files_.add(file_) | |
return list(files_) | |
def run(self): | |
for file_ in self.organize_files(self.parsing_project_files()): | |
self.files.extend(self.find_sequence_files(file_)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment