Last active
August 29, 2015 13:57
-
-
Save psykzz/9474469 to your computer and use it in GitHub Desktop.
Combining a set of files together ( unfinished )
This file contains hidden or 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
## | |
class FileCombiner: | |
""" File combiner class """ | |
# Mock functions | |
def _starts(self,a,b): | |
#print 'start',a,b,a.startswith(b) | |
return a.startswith(b) | |
def _ends(self,a,b): | |
#print 'end',a,b,a.endswith(b) | |
return a.endswith(b) | |
def _contains(self,a,b): | |
return True if b in a else False | |
def _surround(self,a,b): | |
return a._starts(b[0]) and a._ends(c[1]) | |
def _is(self,a,b): | |
return True if a == b else False | |
def _generate_ignore_lists(self, ignore_list): | |
func_list = [] | |
for pattern in ignore_list: | |
# Contains the following string pattern | |
if pattern.startswith('*') and pattern.endswith('*'): | |
func_list.append( lambda x, pattern=pattern: self._contains(x, pattern[1:-1]) ) | |
# Ends with the following | |
elif pattern.startswith('*'): | |
func_list.append( lambda x, pattern=pattern: self._ends(x, pattern[1:]) ) | |
# Starts with the following | |
elif pattern.endswith('*'): | |
func_list.append( lambda x, pattern=pattern: self._starts(x, pattern[:-1]) ) | |
# Surrounded by the following | |
elif pattern.count('*') is 1: | |
func_list.append( lambda x, pattern=pattern: self._surround(x, pattern.split('*')) ) | |
# Failover because we dont support this number of wildcards | |
elif pattern.count('*') > 1: | |
raise Exception("Multiple wildcards are only supported at the start and end of a string") | |
# Finally if we match nothing just do an exact match | |
else: | |
func_list.append(lambda x: self._is(x, pattern)) | |
# Return the results | |
return func_list | |
def __init__(self, output_filename=None, template_file=None, allow_overwrite=False, working_directory=None, | |
ignore_files=[], ignore_directories=[]): | |
# Init states | |
self._built = False | |
self._ignored_files = self._generate_ignore_lists(ignore_files) | |
self._ignored_directories = self._generate_ignore_lists(ignore_directories) | |
# Get working directory | |
self.__working_directory = working_directory | |
if not working_directory: | |
# Grab it from the output files absolute directory | |
if os.path.isabs(output_filename): | |
self.__working_directory = os.path.abspath(output_filename) | |
# Fall back to the current directory of the script | |
else: | |
self.__working_directory = os.path.dirname(os.path.realpath(__file__)) | |
if not self.__working_directory: | |
raise Exception("Invalid working directory") | |
# Template filename | |
if template_file and not os.path.isfile(template_file): | |
raise Exception("Template file doesn't exist! ({})".format(template_file)) | |
self.__template_filename = template_file | |
# Output filename | |
if not allow_overwrite and os.path.isfile(output_filename): | |
raise Exception("Output file already exists! ({})".format(output_filename)) | |
self.__output_filename = output_filename | |
def _get_directory(self, directory): | |
# Check if given a relative or abs path. | |
if os.path.isabs(directory): | |
search_dir = directory | |
else: | |
search_dir = os.path.join(self.__working_directory,directory) | |
# Check if our output is a directory itself | |
if not os.path.isdir(search_dir): | |
raise Exception("Invalid directory") | |
return search_dir | |
def _get_files(self, directory, recursive=False, max_recursion=3, ignore_ignores=False): | |
search_dir = self._get_directory(directory) | |
item_list = [os.path.join(search_dir, _dir) for _dir in os.listdir(search_dir)] | |
new_list = [] | |
for _item in item_list: | |
# filter out none files | |
if not os.path.isfile(_item): | |
continue | |
# WE might want to capture the directories here for the recursive function | |
# Rules matching | |
# Continue to loop through | |
no_error = True | |
if not ignore_ignores: | |
for fnc in self._ignored_files: | |
# Strip down the trailling slashes and get the base path to match against, this way we catch /.git/ along with other .* | |
stripped_down = os.path.basename(os.path.normpath(_item)) # http://stackoverflow.com/a/3925147/211081 | |
if fnc(stripped_down): | |
no_error = False | |
break | |
if no_error: | |
new_list.append(_item) | |
# If we are going to about about this recursively lets do a few things | |
# Check if we want to | |
# Get all the dirs we need to search in | |
# Search all those directories for files | |
# Return the full list combined | |
if recursive and max_recursion > 0: | |
_extra_directories = self._get_directories(directory, recursive, max_recursion) | |
for _directory in _extra_directories: | |
new_list += self._get_files(_directory, recursive, max_recursion-1) | |
return new_list | |
def _get_directories(self, directory, recursive=False, max_recursion=3, ignore_ignores=False): | |
search_dir = self._get_directory(directory) | |
item_list = [os.path.join(search_dir, _dir) for _dir in os.listdir(search_dir)] | |
new_list = [] | |
for _item in item_list: | |
# filter out none directories | |
if not os.path.isdir(_item): | |
continue | |
# Rules matching | |
# Loop through all the rules and see if any fail. | |
no_error = True | |
if not ignore_ignores: | |
for fnc in self._ignored_directories: | |
# Strip down the trailling slashes and get the base path to match against, this way we catch /.git/ along with other .* | |
stripped_down = os.path.basename(os.path.normpath(_item)) # http://stackoverflow.com/a/3925147/211081 | |
if fnc(stripped_down): | |
no_error = False | |
break | |
# If we didnt fail, lets add to the results and recursivly continue searching | |
if no_error: | |
new_list.append(_item) | |
if recursive and max_recursion > 0: | |
new_list += self._get_directories(_item, recursive, max_recursion-1) | |
# Return full paths found | |
return new_list | |
def combine(self): | |
return False | |
def get_ouput_filename(self, output_filename_only=True): | |
if not self._built: | |
raise Exception("File not yet built. Use combine() first.") | |
pass | |
## |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment