def generate(self, dir_pattern, file_pattern, action_ch='<STR_LIT:g>', recursively=False, force=False):
<EOL>directories = [os.path.abspath(directory) for directory in glob.glob(os.path.join(self.__root_directory, dir_pattern)) if os.path.isdir(directory)]<EOL>extensions = self.__extensions.keys()<EOL>for directory in directories:<EOL><INDENT>for b, f in find_files(directory, file_pattern, recursively=recursively):<EOL><INDENT>file_basename, file_ext = os.path.splitext(f)<EOL>if file_ext in extensions:<EOL><INDENT>rel_path = os.path.relpath(os.path.join(b,f), self.__root_directory)<EOL>rel_basename, rel_filename = os.path.split(rel_path)<EOL>rel_filename_without_ext, rel_ext = os.path.splitext(rel_filename)<EOL>in_file_name = os.path.join(b, f)<EOL>generator_action_container = self.__actions.retrieve_element_or_default(rel_basename, None)<EOL>action = None<EOL>if generator_action_container is not None:<EOL><INDENT>action = generator_action_container.get_compatible_generator_action(f)<EOL><DEDENT>if action is None:<EOL><INDENT>action = self.__default_action<EOL><DEDENT>if action:<EOL><INDENT>if action_ch == '<STR_LIT:d>':<EOL><INDENT>print("<STR_LIT>" % (rel_path, action.action_function_name()))<EOL><DEDENT>for filename_end, context in action.run():<EOL><INDENT>out_file_name = os.path.join(b, rel_filename_without_ext + filename_end + self.__extensions[file_ext])<EOL>if action_ch == '<STR_LIT:g>':<EOL><INDENT>self.__generate_file(template_filename=in_file_name, context=context, generated_filename=out_file_name, force=force)<EOL><DEDENT>elif action_ch == '<STR_LIT:c>':<EOL><INDENT>try:<EOL><INDENT>os.remove(out_file_name)<EOL>self.log_info("<STR_LIT>" % out_file_name)<EOL><DEDENT>except OSError:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>elif action_ch == '<STR_LIT:d>':<EOL><INDENT>print("<STR_LIT>" % os.path.join(rel_basename, rel_filename_without_ext + filename_end + self.__extensions[file_ext]))<EOL><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT>
Main method to generate (source code) files from templates.

See documentation about the directory and file patterns and their possible combinations.

Args:
    dir_pattern: ``glob`` pattern taken from the root directory. **Only** used for directories.
    file_pattern: ``fnmatch`` pattern taken from all matching directories. **Only** used for files.
    action (char): Denote action to be taken. Can be:
        - g: Generate all files that match both directory and file patterns. This is the default behavior.
        - d: Same as `g` but with doing anything, i.e. dry run.
        - c: Same as `g` but erasing the generated files instead, i.e. clean.
    recursively: Do we do the actions in the sub-directories? Note that in this case **only** the file pattern applies as **all**
        the subdirectories are visited.
    force (boolean): Do we force the generation or not?