def setUp(self): self.project_dir = os.getcwd() self.printer = ConsolePrinter() self.coafile = os.path.join(tempfile.gettempdir(), '.coafile') self.writer = ConfWriter(self.coafile) self.arg_parser = _get_arg_parser() self.old_argv = deepcopy(sys.argv) del sys.argv[1:]
def delete_row(self, button, listboxrow): del self.sections_dict[self.section_stack_map[listboxrow].get_name()] self.section_stack_map[listboxrow].destroy() del self.section_stack_map[listboxrow] listboxrow.destroy() conf_writer = ConfWriter(self.src+'/.coafile') conf_writer.write_sections(self.sections_dict) conf_writer.close()
def setUp(self): self.file = os.path.join(tempfile.gettempdir(), 'ConfParserTestFile') with open(self.file, 'w', encoding='utf-8') as file: file.write(self.example_file) self.log_printer = LogPrinter() self.write_file_name = os.path.join(tempfile.gettempdir(), 'ConfWriterTestFile') self.uut = ConfWriter(self.write_file_name)
def setUp(self): self.file = os.path.join(tempfile.gettempdir(), "ConfParserTestFile") with open(self.file, "w", encoding='utf-8') as file: file.write(self.example_file) self.conf_parser = ConfParser() self.write_file_name = os.path.join(tempfile.gettempdir(), "ConfWriterTestFile") self.uut = ConfWriter(self.write_file_name)
def update_section_name(self, widget, arg, old_name, section_view): section_view.set_name(widget.get_name()) self.sections_dict[old_name].name = widget.get_name() self.sections_dict = update_ordered_dict_key(self.sections_dict, old_name, widget.get_name()) widget.connect("edited", self.update_section_name, widget.get_name()) conf_writer = ConfWriter(self.src+'/.coafile') conf_writer.write_sections(self.sections_dict) conf_writer.close()
def test_write_with_dir(self): self.uut_dir = ConfWriter(tempfile.gettempdir()) self.uut_dir.write_sections({'name': Section('name')}) self.uut_dir.close() with open(os.path.join(tempfile.gettempdir(), '.coafile'), 'r') as f: lines = f.readlines() self.assertEqual(['[name]\n'], lines) os.remove(os.path.join(tempfile.gettempdir(), '.coafile'))
def on_key_changed(self, widget, arg, sensitive_button, value_entry): setting = Setting(widget.get_name(), value_entry.get_text()) self.sections_dict[self.get_name()].append(setting) self.add_setting() sensitive_button.set_sensitive(True) sensitive_button.set_name(setting.key) widget.connect("edited", self.update_key, setting, sensitive_button) value_entry.connect("focus-out-event", self.update_value, setting) conf_writer = ConfWriter(self.src+'/.coafile') conf_writer.write_sections(self.sections_dict) conf_writer.close()
def _save_configuration(self): self.conf_writer = None try: if bool(self.conf_sections["default"]["save"]): self.conf_writer = ConfWriter( str(self.conf_sections["default"]["config"])) except ValueError: self.conf_writer = ConfWriter( str(self.conf_sections["default"]["save"])) if self.conf_writer is not None: self.conf_writer.write_sections(self.conf_sections)
class ConfWriterTestCase(unittest.TestCase): example_file = "to be ignored \n\ save=true\n\ a_default, another = val \n\ TEST = tobeignored # do you know that thats a comment \n\ test = push \n\ t = \n\ [MakeFiles] \n\ j , ANother = a \n\ multiline \n\ value \n\ ; just a omment \n\ ; just a omment \n" def setUp(self): self.file = os.path.join(tempfile.gettempdir(), "ConfParserTestFile") with open(self.file, "w", encoding='utf-8') as filehandler: filehandler.write(self.example_file) self.conf_parser = ConfParser() self.write_file_name = os.path.join(tempfile.gettempdir(), "ConfWriterTestFile") self.uut = ConfWriter(self.write_file_name) def tearDown(self): os.remove(self.file) os.remove(self.write_file_name) def test_exceptions(self): self.assertRaises(TypeError, self.uut.write_section, 5) def test_write(self): result_file = ["[Default]\n", "save = true\n", "a_default, another = val\n", "# do you know that thats a comment\n", "test = push\n", "t = \n", "\n", "[MakeFiles]\n", "j, ANother = a\n", "multiline\n", "value\n", "; just a omment\n", "; just a omment\n"] self.uut.write_sections(self.conf_parser.reparse(self.file)) del self.uut with open(self.write_file_name, "r") as f: lines = f.readlines() self.assertEqual(result_file, lines)
def _save_configuration(self): self.conf_writer = None default_section = self.sections["default"] try: if bool(default_section.get("save", "false")): self.conf_writer = ConfWriter(str( default_section.get("config", ".coafile"))) except ValueError: self.conf_writer = ConfWriter(str(default_section.get("save", ".coafile"))) if self.conf_writer is not None: self.conf_writer.write_sections(self.sections)
def _save_configuration(self): self.conf_writer = None default_section = self.sections["default"] try: if bool(default_section.get("save", "false")): self.conf_writer = ConfWriter( str(default_section.get("config", ".coafile"))) except ValueError: self.conf_writer = ConfWriter( str(default_section.get("save", ".coafile"))) if self.conf_writer is not None: self.conf_writer.write_sections(self.sections)
class SettingsTest(unittest.TestCase): def setUp(self): self.project_dir = os.getcwd() self.printer = ConsolePrinter() self.coafile = os.path.join(tempfile.gettempdir(), '.coafile') self.writer = ConfWriter(self.coafile) self.arg_parser = _get_arg_parser() self.old_argv = deepcopy(sys.argv) del sys.argv[1:] def tearDown(self): self.writer.close() os.remove(self.coafile) sys.argv = self.old_argv def test_write_info(self): result_date = date.today().strftime("%d %b %Y") result_comment = ('# Generated by coala-quickstart on ' '{date}.\n'.format(date=result_date)) write_info(self.writer) self.writer.close() with open(self.coafile, 'r') as f: line = f.readline() self.assertEqual(result_comment, line) def test_allow_complete_section_mode(self): project_dir = "/repo" project_files = ['/repo/hello.html'] ignore_globs = [] used_languages = list(get_used_languages(project_files)) relevant_bears = filter_relevant_bears( used_languages, self.printer, self.arg_parser, {}) res = generate_settings( project_dir, project_files, ignore_globs, relevant_bears, {}, True) bears_list = res["all.HTML"]["bears"].value.replace(" ", "").split(",") files_list = res["all.HTML"]["files"].value.replace(" ", "").split(",") self.assertEqual( ['HTMLLintBear', 'coalaBear', 'BootLintBear', 'LicenseCheckBear', 'SpaceConsistencyBear', 'KeywordBear', 'LineLengthBear', 'DuplicateFileBear'].sort(), bears_list.sort()) self.assertEqual(['**.html'], files_list)
def create_section_view(self, widget=None, arg=None, row_obejct=None): section_view = SectionView(self.sections_dict, self.src) section_view.set_visible(True) section_view.set_name(widget.get_name()) self.sections.add_named(section_view, widget.get_name()) self.sections.set_visible_child_name(widget.get_name()) if arg is not None: widget.connect("edited", self.update_section_name, widget.get_name(), section_view) self.sections_dict[widget.get_name()] = Section(widget.get_name()) section_view.add_setting() conf_writer = ConfWriter(self.src+'/.coafile') conf_writer.write_sections(self.sections_dict) conf_writer.close() self.section_stack_map[row_obejct] = section_view
def setUp(self): self.file = os.path.join(tempfile.gettempdir(), "ConfParserTestFile") with open(self.file, "w", encoding="utf-8") as file: file.write(self.example_file) self.conf_parser = ConfParser() self.write_file_name = os.path.join(tempfile.gettempdir(), "ConfWriterTestFile") self.uut = ConfWriter(self.write_file_name)
class SettingsTest(unittest.TestCase): def setUp(self): self.coafile = os.path.join(tempfile.gettempdir(), '.coafile') self.writer = ConfWriter(self.coafile) def tearDown(self): self.writer.close() os.remove(self.coafile) def test_write_info(self): result_date = date.today().strftime("%d %b %Y") result_comment = ('# Generated by coala-quickstart on ' '{date}.\n'.format(date=result_date)) write_info(self.writer) self.writer.close() with open(self.coafile, 'r') as f: line = f.readline() self.assertEqual(result_comment, line)
def delete_row(self, button, listboxrow): del self.sections_dict[self.section_stack_map[listboxrow].get_name()] self.section_stack_map[listboxrow].destroy() del self.section_stack_map[listboxrow] listboxrow.destroy() conf_writer = ConfWriter(self.src + '/.coafile') conf_writer.write_sections(self.sections_dict) conf_writer.close()
class ConfWriterTest(unittest.TestCase): example_file = ("to be ignored \n" " save=true\n" " a_default, another = val \n" " TEST = tobeignored # thats a comment \n" " test = push \n" " t = \n" " [Section] \n" " [MakeFiles] \n" " j , ANother = a \n" " multiline \n" " value \n" " ; just a omment \n" " ; just a omment \n" " key\\ space = value space\n" " key\\=equal = value=equal\n" " key\\\\backslash = value\\\\backslash\n" " key\\,comma = value,comma\n" " key\\#hash = value\\#hash\n" " key\\.dot = value.dot\n") def setUp(self): self.file = os.path.join(tempfile.gettempdir(), "ConfParserTestFile") with open(self.file, "w", encoding='utf-8') as file: file.write(self.example_file) self.conf_parser = ConfParser() self.write_file_name = os.path.join(tempfile.gettempdir(), "ConfWriterTestFile") self.uut = ConfWriter(self.write_file_name) def tearDown(self): self.uut.close() os.remove(self.file) os.remove(self.write_file_name) def test_exceptions(self): self.assertRaises(TypeError, self.uut.write_section, 5) def test_write(self): result_file = [ "[Default]\n", "save = true\n", "a_default, another = val\n", "# thats a comment\n", "test = push\n", "t = \n", "[Section]\n", "[MakeFiles]\n", "j, ANother = a\n", "multiline\n", "value\n", "; just a omment\n", "; just a omment\n", "key\\ space = value space\n", "key\\=equal = value=equal\n", "key\\\\backslash = value\\\\backslash\n", "key\\,comma = value,comma\n", "key\\#hash = value\\#hash\n", "key\\.dot = value.dot\n" ] self.uut.write_sections(self.conf_parser.parse(self.file)) self.uut.close() with open(self.write_file_name, "r") as f: lines = f.readlines() self.assertEqual(result_file, lines)
def update_section_name(self, widget, arg, old_name, section_view): section_view.set_name(widget.get_name()) self.sections_dict[old_name].name = widget.get_name() self.sections_dict = update_ordered_dict_key(self.sections_dict, old_name, widget.get_name()) widget.connect("edited", self.update_section_name, widget.get_name()) conf_writer = ConfWriter(self.src + '/.coafile') conf_writer.write_sections(self.sections_dict) conf_writer.close()
def save_sections(sections): """ Saves the given sections if they are to be saved. :param sections: A section dict. """ default_section = sections["default"] try: if bool(default_section.get("save", "false")): conf_writer = ConfWriter( str(default_section.get("config", Constants.default_coafile))) else: return except ValueError: conf_writer = ConfWriter(str(default_section.get("save", ".coafile"))) conf_writer.write_sections(sections) conf_writer.close()
def save_sections(sections): """ Saves the given sections if they are to be saved. :param sections: A section dict. """ default_section = sections['cli'] try: if bool(default_section.get('save', 'false')): conf_writer = ConfWriter( str(default_section.get('config', Constants.local_coafile))) else: return except ValueError: conf_writer = ConfWriter(str(default_section.get('save', '.coafile'))) conf_writer.write_sections(sections) conf_writer.close()
def on_key_changed(self, widget, arg, sensitive_button, value_entry): setting = Setting(widget.get_name(), value_entry.get_text()) self.sections_dict[self.get_name()].append(setting) self.add_setting() sensitive_button.set_sensitive(True) sensitive_button.set_name(setting.key) widget.connect("edited", self.update_key, setting, sensitive_button) value_entry.connect("focus-out-event", self.update_value, setting) conf_writer = ConfWriter(self.src + '/.coafile') conf_writer.write_sections(self.sections_dict) conf_writer.close()
def create_section_view(self, widget=None, arg=None, row_obejct=None): section_view = SectionView(self.sections_dict, self.src) section_view.set_visible(True) section_view.set_name(widget.get_name()) self.sections.add_named(section_view, widget.get_name()) self.sections.set_visible_child_name(widget.get_name()) if arg is not None: widget.connect("edited", self.update_section_name, widget.get_name(), section_view) self.sections_dict[widget.get_name()] = Section(widget.get_name()) section_view.add_setting() conf_writer = ConfWriter(self.src + '/.coafile') conf_writer.write_sections(self.sections_dict) conf_writer.close() self.section_stack_map[row_obejct] = section_view
def save_sections(sections): """ Saves the given sections if they are to be saved. :param sections: A section dict. """ default_section = sections["default"] try: if bool(default_section.get("save", "false")): conf_writer = ConfWriter(str(default_section.get("config", ".coafile"))) else: return except ValueError: conf_writer = ConfWriter(str(default_section.get("save", ".coafile"))) conf_writer.write_sections(sections) conf_writer.close()
def save_sections(sections): """ Saves the given sections if they are to be saved. :param sections: A section dict. """ default_section = sections['default'] try: if bool(default_section.get('save', 'false')): conf_writer = ConfWriter( str(default_section.get('config', Constants.default_coafile))) else: return except ValueError: conf_writer = ConfWriter(str(default_section.get('save', '.coafile'))) conf_writer.write_sections(sections) conf_writer.close()
def write_coafile(printer, project_dir, settings): """ Writes the coafile to disk. :param printer: A ``ConsolePrinter`` object used for console interactions. :param project_dir: Full path of the user's project directory. :param settings: A dict with section name as key and a ``Section`` object as value. """ coafile = os.path.join(project_dir, ".coafile") if os.path.isfile(coafile): printer.print("'" + coafile + "' already exists.\nThe settings will be" " written to '" + coafile + ".new'", color="yellow") coafile = coafile + ".new" writer = ConfWriter(coafile) writer.write_sections(settings) writer.close() printer.print("'" + coafile + "' successfully generated.", color="green")
def delete_setting(self, button, setting_row): self.sections_dict[self.get_name()].delete_setting(button.get_name()) setting_row.destroy() conf_writer = ConfWriter(self.src+'/.coafile') conf_writer.write_sections(self.sections_dict) conf_writer.close()
class SectionManager: """ The SectionManager does the following things: - Reading all settings in sections from - Default config - CLI - Configuration file - Collecting all the bears - Filling up all needed settings - Write back the new sections to the configuration file if needed - Give all information back to caller This is done when the run() method is invoked. Anything else is just helper stuff and initialization. """ def __init__(self): self.cli_sections = None self.default_sections = None self.user_sections = None self.coafile_sections = None self.sections = None self.cli_parser = CliParser() self.conf_parser = ConfParser() self.conf_writer = None self.local_bears = {} self.global_bears = {} self.targets = [] def run(self, arg_list=sys.argv[1:]): self._load_configuration(arg_list) self._fill_settings() self._save_configuration() self._warn_nonexistent_targets() return self.sections, self.local_bears, self.global_bears, self.targets def _load_configuration(self, arg_list): self.cli_sections = self.cli_parser.reparse(arg_list=arg_list) # We dont want to store targets argument back to file, thus remove it for item in list(self.cli_sections["default"].contents.pop( "targets", "")): self.targets.append(item.lower()) self.default_sections = self._load_config_file( StringConstants.system_coafile) self.user_sections = self._load_config_file( StringConstants.user_coafile, silent=True) default_config = str(self.default_sections["default"].get( "config", ".coafile")) user_config = str(self.user_sections["default"].get( "config", default_config)) config = os.path.abspath( str(self.cli_sections["default"].get("config", user_config))) self.coafile_sections = self._load_config_file(config) self.sections = self._merge_section_dicts(self.default_sections, self.user_sections) self.sections = self._merge_section_dicts(self.sections, self.coafile_sections) self.sections = self._merge_section_dicts(self.sections, self.cli_sections) for section in self.sections: if section != "default": self.sections[section].defaults = self.sections["default"] def _load_config_file(self, filename, silent=False): """ Loads sections from a config file. Prints an appropriate warning if it doesn't exist and returns a section dict containing an empty default section in that case. It assumes that the cli_sections are available. :param filename: The file to load settings from. :param silent: Whether or not to warn the user if the file doesn't exist. """ filename = os.path.abspath(filename) try: return self.conf_parser.reparse(filename) except self.conf_parser.FileNotFoundError: if not silent: self.cli_sections["default"].retrieve_logging_objects() self.cli_sections["default"].log_printer.warn( _("The requested coafile '{filename}' does not exist. " "Thus it will not be used.").format(filename=filename)) return {"default": Section("default")} def _fill_settings(self): for section_name in self.sections: section = self.sections[section_name] section.retrieve_logging_objects() bear_dirs = path_list(section.get("bear_dirs", "")) bear_dirs.append( os.path.join(StringConstants.coalib_bears_root, "**")) bears = list(section.get("bears", "")) local_bears = collect_bears(bear_dirs, bears, [BEAR_KIND.LOCAL]) global_bears = collect_bears(bear_dirs, bears, [BEAR_KIND.GLOBAL]) filler = SectionFiller(section) all_bears = copy.deepcopy(local_bears) all_bears.extend(global_bears) filler.fill_section(all_bears) self.local_bears[section_name] = local_bears self.global_bears[section_name] = global_bears def _save_configuration(self): self.conf_writer = None default_section = self.sections["default"] try: if bool(default_section.get("save", "false")): self.conf_writer = ConfWriter( str(default_section.get("config", ".coafile"))) except ValueError: self.conf_writer = ConfWriter( str(default_section.get("save", ".coafile"))) if self.conf_writer is not None: self.conf_writer.write_sections(self.sections) @staticmethod def _merge_section_dicts(lower, higher): """ Merges the section dictionaries. The values of higher will take precedence over the ones of lower. Lower will hold the modified dict in the end. """ for name in higher: if name in lower: lower[name].update(higher[name], ignore_defaults=True) else: # no deep copy needed lower[name] = higher[name] return lower def _warn_nonexistent_targets(self): for target in self.targets: if target not in self.sections: self.sections["default"].log_printer.warn( _("The requested section '{section}' is not existent. " "Thus it cannot be executed.").format(section=target))
def setUp(self): self.coafile = os.path.join(tempfile.gettempdir(), '.coafile') self.writer = ConfWriter(self.coafile)
def setUp(self): self.coafile = os.path.join(tempfile.gettempdir(), '.coafile') self.writer = ConfWriter(self.coafile) self.old_argv = deepcopy(sys.argv) del sys.argv[1:]
def generate_green_mode_sections(data, project_dir, project_files, ignore_globs, printer=None, suffix=''): """ Generates the section objects for the green_mode. :param data: This is the data structure generated from the method generate_data_struct_for_sections(). :param project_dir: The path of the project directory. :param project_files: List of paths to only the files inside the project directory. :param ignore_globs: The globs of files to ignore. :param printer: The ConsolePrinter object. :param suffix: A suffix that can be added to the `.coafile.green`. """ all_sections = { 'all': [], } lang_files = split_by_language(project_files) extset = get_extensions(project_files) ignored_files = generate_ignore_field(project_dir, lang_files.keys(), extset, ignore_globs) if ignored_files: section_all = Section('all') section_all['ignore'] = ignored_files all_sections['all'].append(section_all) for bear in data: num = 0 bear_sections = [] for setting_combs in data[bear]: if not setting_combs: continue for dict_ in setting_combs: num = num + 1 section = Section('all.' + bear.__name__ + str(num), None) for key_ in dict_: if key_ == 'filename': file_list_sec = dict_[key_] file_list_sec, ignore_list = aggregate_files( file_list_sec, project_dir) dict_[key_] = file_list_sec section['ignore'] = ', '.join( escape(x, '\\') for x in ignore_list) section['files'] = ', '.join( escape(x, '\\') for x in dict_[key_]) else: section[key_] = str(dict_[key_]) section['bears'] = bear.__name__ bear_sections.append(section) # Remove sections for the same bear who have common files i.e. more # than one setting was found to be green. file_list = [] new_bear_sections = [] for index, section in enumerate(bear_sections): if not section.contents['files'] in file_list: file_list.append(section.contents['files']) new_bear_sections.append(section) all_sections[bear.__name__] = new_bear_sections coafile = os.path.join(project_dir, '.coafile.green' + suffix) writer = ConfWriter(coafile) write_sections(writer, all_sections) writer.close() printer.print("'" + coafile + "' successfully generated.", color='green')
def generate_green_mode_sections(data, project_dir, project_files, ignore_globs, printer=None, suffix=''): """ Generates the section objects for the green_mode. :param data: This is the data structure generated from the method generate_data_struct_for_sections(). :param project_dir: The path of the project directory. :param project_files: List of paths to only the files inside the project directory. :param ignore_globs: The globs of files to ignore. :param printer: The ConsolePrinter object. :param suffix: A suffix that can be added to the `.coafile.green`. """ all_sections = {'all': [], } lang_files = split_by_language(project_files) extset = get_extensions(project_files) ignored_files = generate_ignore_field(project_dir, lang_files.keys(), extset, ignore_globs) if ignored_files: section_all = Section('all') section_all['ignore'] = ignored_files all_sections['all'].append(section_all) for bear in data: num = 0 bear_sections = [] for setting_combs in data[bear]: if not setting_combs: continue for dict_ in setting_combs: num = num + 1 section = Section('all.' + bear.__name__ + str(num), None) for key_ in dict_: if key_ == 'filename': file_list_sec = dict_[key_] file_list_sec, ignore_list = aggregate_files( file_list_sec, project_dir) dict_[key_] = file_list_sec section['ignore'] = ', '.join( escape(x, '\\') for x in ignore_list) section['files'] = ', '.join( escape(x, '\\') for x in dict_[key_]) else: section[key_] = str(dict_[key_]) section['bears'] = bear.__name__ bear_sections.append(section) # Remove sections for the same bear who have common files i.e. more # than one setting was found to be green. file_list = [] new_bear_sections = [] for index, section in enumerate(bear_sections): if not section.contents['files'] in file_list: file_list.append(section.contents['files']) new_bear_sections.append(section) all_sections[bear.__name__] = new_bear_sections coafile = os.path.join(project_dir, '.coafile.green' + suffix) writer = ConfWriter(coafile) write_sections(writer, all_sections) writer.close() printer.print("'" + coafile + "' successfully generated.", color='green')
class ConfWriterTest(unittest.TestCase): example_file = ('to be ignored \n' ' save=true\n' ' a_default, another = val \n' ' TEST = tobeignored # thats a comment \n' ' test = push \n' ' t = \n' ' [Section] \n' ' [MakeFiles] \n' ' j , ANother = a \n' ' multiline \n' ' value \n' ' ; just a omment \n' ' ; just a omment \n' ' key\\ space = value space\n' ' key\\=equal = value=equal\n' ' key\\\\backslash = value\\\\backslash\n' ' key\\,comma = value,comma\n' ' key\\#hash = value\\#hash\n' ' key\\.dot = value.dot\n' ' a_default = val, val2\n') append_example_file = ('[defaults]\n' 'a = 4\n' 'b = 4,5,6\n' 'c = 4,5\n' 'd = 4\n' '[defaults.new]\n' 'a = 4,5,6,7\n' 'b = 4,5,6,7\n' 'c = 4,5,6,7\n' 'd = 4,5,6,7\n') def setUp(self): self.file = os.path.join(tempfile.gettempdir(), 'ConfParserTestFile') with open(self.file, 'w', encoding='utf-8') as file: file.write(self.example_file) self.log_printer = LogPrinter() self.write_file_name = os.path.join(tempfile.gettempdir(), 'ConfWriterTestFile') self.uut = ConfWriter(self.write_file_name) def tearDown(self): self.uut.close() os.remove(self.file) os.remove(self.write_file_name) def test_exceptions(self): self.assertRaises(TypeError, self.uut.write_section, 5) def test_write(self): result_file = [ '[Section]\n', '[MakeFiles]\n', 'j, ANother = a\n', 'multiline\n', 'value\n', '; just a omment\n', '; just a omment\n', 'key\\ space = value space\n', 'key\\=equal = value=equal\n', 'key\\\\backslash = value\\\\backslash\n', 'key\\,comma = value,comma\n', 'key\\#hash = value\\#hash\n', 'key\\.dot = value.dot\n', 'a_default += val2\n', '[cli]\n', 'save = true\n', 'a_default, another = val\n', '# thats a comment\n', 'test = push\n', 't = \n' ] sections = load_configuration(['-c', escape(self.file, '\\')], self.log_printer)[0] del sections['cli'].contents['config'] self.uut.write_sections(sections) self.uut.close() with open(self.write_file_name, 'r') as f: lines = f.readlines() self.assertEqual(result_file, lines) def test_append(self): with open(self.file, 'w', encoding='utf-8') as file: file.write(self.append_example_file) result_file = [ '[defaults]\n', 'a = 4\n', 'b = 4,5,6\n', 'c = 4,5\n', 'd = 4\n', '[defaults.new]\n', 'b += 7\n', 'c += 6, 7\n', 'a, d += 5, 6, 7\n', '[cli]\n' ] sections = load_configuration(['-c', escape(self.file, '\\')], self.log_printer)[0] del sections['cli'].contents['config'] self.uut.write_sections(sections) self.uut.close() with open(self.write_file_name, 'r') as f: lines = f.readlines() self.assertEqual(result_file, lines) def test_write_with_dir(self): self.uut_dir = ConfWriter(tempfile.gettempdir()) self.uut_dir.write_sections({'name': Section('name')}) self.uut_dir.close() with open(os.path.join(tempfile.gettempdir(), '.coafile'), 'r') as f: lines = f.readlines() self.assertEqual(['[name]\n'], lines) os.remove(os.path.join(tempfile.gettempdir(), '.coafile'))
def update_key(self, widget, arg, setting, delete_button): setting.key = widget.get_name() delete_button.set_name(widget.get_name()) conf_writer = ConfWriter(self.src+'/.coafile') conf_writer.write_sections(self.sections_dict) conf_writer.close()
def update_value(self, widget, arg, setting): setting.value = widget.get_text() conf_writer = ConfWriter(self.src+'/.coafile') conf_writer.write_sections(self.sections_dict) conf_writer.close()
class ConfWriterTest(unittest.TestCase): example_file = ( "to be ignored \n" " save=true\n" " a_default, another = val \n" " TEST = tobeignored # thats a comment \n" " test = push \n" " t = \n" " [MakeFiles] \n" " j , ANother = a \n" " multiline \n" " value \n" " ; just a omment \n" " ; just a omment \n" " key\\ space = value space\n" " key\\=equal = value=equal\n" " key\\\\backslash = value\\\\backslash\n" " key\\,comma = value,comma\n" " key\\#hash = value\\#hash\n" " key\\.dot = value.dot\n" ) def setUp(self): self.file = os.path.join(tempfile.gettempdir(), "ConfParserTestFile") with open(self.file, "w", encoding="utf-8") as file: file.write(self.example_file) self.conf_parser = ConfParser() self.write_file_name = os.path.join(tempfile.gettempdir(), "ConfWriterTestFile") self.uut = ConfWriter(self.write_file_name) def tearDown(self): self.uut.close() os.remove(self.file) os.remove(self.write_file_name) def test_exceptions(self): self.assertRaises(TypeError, self.uut.write_section, 5) def test_write(self): result_file = [ "[Default]\n", "save = true\n", "a_default, another = val\n", "# thats a comment\n", "test = push\n", "t = \n", "\n", "[MakeFiles]\n", "j, ANother = a\n", "multiline\n", "value\n", "; just a omment\n", "; just a omment\n", "key\\ space = value space\n", "key\\=equal = value=equal\n", "key\\\\backslash = value\\\\backslash\n", "key\\,comma = value,comma\n", "key\\#hash = value\\#hash\n", "key\\.dot = value.dot\n", ] self.uut.write_sections(self.conf_parser.parse(self.file)) self.uut.close() with open(self.write_file_name, "r") as f: lines = f.readlines() self.assertEqual(result_file, lines)
class SettingsTest(unittest.TestCase): def path_leaf(self, path): """ :return: The file name from the given path. """ head, tail = os.path.split(path) return tail or os.path.basename(head) def setUp(self): self.project_dir = os.getcwd() self.printer = ConsolePrinter() self.coafile = os.path.join(tempfile.gettempdir(), '.coafile') self.writer = ConfWriter(self.coafile) self.arg_parser = _get_arg_parser() self.old_argv = deepcopy(sys.argv) del sys.argv[1:] def tearDown(self): self.writer.close() os.remove(self.coafile) sys.argv = self.old_argv def test_write_info(self): result_date = date.today().strftime("%d %b %Y") result_comment = ('# Generated by coala-quickstart on ' '{date}.\n'.format(date=result_date)) write_info(self.writer) self.writer.close() with open(self.coafile, 'r') as f: line = f.readline() self.assertEqual(result_comment, line) def test_allow_complete_section_mode_with_ignore_globs(self): project_dir = "/repo" project_files = ['/repo/hello.html'] ignore_globs = ["/repo/style.css"] used_languages = list(get_used_languages(project_files)) relevant_bears = filter_relevant_bears( used_languages, self.printer, self.arg_parser, {}) res = generate_settings( project_dir, project_files, ignore_globs, relevant_bears, {}, True) bears_list = res["all.HTML"]["bears"].value.replace(" ", "").split(",") files_list = res["all.HTML"]["files"].value.replace(" ", "").split(",") self.assertEqual( ['HTMLLintBear', 'coalaBear', 'BootLintBear', 'LicenseCheckBear', 'SpaceConsistencyBear', 'KeywordBear', 'LineLengthBear', 'DuplicateFileBear'].sort(), bears_list.sort()) ignore_file = str(os.path.normpath(self.path_leaf( str(res["all"]["ignore"])))) self.assertEqual(['**.html'], files_list) self.assertEqual('style.css', ignore_file) def test_allow_complete_section_mode(self): project_dir = "/repo" project_files = ['/repo/hello.html'] ignore_globs = [] used_languages = list(get_used_languages(project_files)) relevant_bears = filter_relevant_bears( used_languages, self.printer, self.arg_parser, {}) res = generate_settings( project_dir, project_files, ignore_globs, relevant_bears, {}, True) bears_list = res["all.HTML"]["bears"].value.replace(" ", "").split(",") files_list = res["all.HTML"]["files"].value.replace(" ", "").split(",") self.assertEqual( ['HTMLLintBear', 'coalaBear', 'BootLintBear', 'LicenseCheckBear', 'SpaceConsistencyBear', 'KeywordBear', 'LineLengthBear', 'DuplicateFileBear'].sort(), bears_list.sort()) self.assertEqual(['**.html'], files_list)
class ConfWriterTest(unittest.TestCase): example_file = ('to be ignored \n' ' save=true\n' ' a_default, another = val \n' ' TEST = tobeignored # thats a comment \n' ' test = push \n' ' t = \n' ' [Section] \n' ' [MakeFiles] \n' ' j , ANother = a \n' ' multiline \n' ' value \n' ' ; just a omment \n' ' ; just a omment \n' ' key\\ space = value space\n' ' key\\=equal = value=equal\n' ' key\\\\backslash = value\\\\backslash\n' ' key\\,comma = value,comma\n' ' key\\#hash = value\\#hash\n' ' key\\.dot = value.dot\n') def setUp(self): self.file = os.path.join(tempfile.gettempdir(), 'ConfParserTestFile') with open(self.file, 'w', encoding='utf-8') as file: file.write(self.example_file) self.conf_parser = ConfParser() self.write_file_name = os.path.join(tempfile.gettempdir(), 'ConfWriterTestFile') self.uut = ConfWriter(self.write_file_name) def tearDown(self): self.uut.close() os.remove(self.file) os.remove(self.write_file_name) def test_exceptions(self): self.assertRaises(TypeError, self.uut.write_section, 5) def test_write(self): result_file = ['[Default]\n', 'save = true\n', 'a_default, another = val\n', '# thats a comment\n', 'test = push\n', 't = \n', '[Section]\n', '[MakeFiles]\n', 'j, ANother = a\n', 'multiline\n', 'value\n', '; just a omment\n', '; just a omment\n', 'key\\ space = value space\n', 'key\\=equal = value=equal\n', 'key\\\\backslash = value\\\\backslash\n', 'key\\,comma = value,comma\n', 'key\\#hash = value\\#hash\n', 'key\\.dot = value.dot\n'] self.uut.write_sections(self.conf_parser.parse(self.file)) self.uut.close() with open(self.write_file_name, 'r') as f: lines = f.readlines() self.assertEqual(result_file, lines)
def update_value(self, widget, arg, setting): setting.value = widget.get_text() conf_writer = ConfWriter(self.src + '/.coafile') conf_writer.write_sections(self.sections_dict) conf_writer.close()
def update_key(self, widget, arg, setting, delete_button): setting.key = widget.get_name() delete_button.set_name(widget.get_name()) conf_writer = ConfWriter(self.src + '/.coafile') conf_writer.write_sections(self.sections_dict) conf_writer.close()
class SectionManager: """ The SectionManager does the following things: - Reading all settings in sections from - Default config - CLI - Configuration file - Collecting all the bears - Filling up all needed settings - Write back the new sections to the configuration file if needed - Give all information back to caller This is done when the run() method is invoked. Anything else is just helper stuff and initialization. """ def __init__(self): self.cli_sections = None self.default_sections = None self.user_sections = None self.coafile_sections = None self.sections = None self.cli_parser = CliParser() self.conf_parser = ConfParser() self.conf_writer = None self.local_bears = {} self.global_bears = {} self.targets = [] def run(self, arg_list=sys.argv[1:]): self._load_configuration(arg_list) self._fill_settings() self._save_configuration() self._warn_nonexistent_targets() return self.sections, self.local_bears, self.global_bears, self.targets def _load_configuration(self, arg_list): self.cli_sections = self.cli_parser.reparse(arg_list=arg_list) # We dont want to store targets argument back to file, thus remove it for item in list( self.cli_sections["default"].contents.pop("targets", "")): self.targets.append(item.lower()) self.default_sections = self._load_config_file( StringConstants.system_coafile) self.user_sections = self._load_config_file( StringConstants.user_coafile, silent=True) default_config = str( self.default_sections["default"].get("config", ".coafile")) user_config = str( self.user_sections["default"].get("config", default_config)) config = os.path.abspath(str( self.cli_sections["default"].get("config", user_config))) self.coafile_sections = self._load_config_file(config) self.sections = self._merge_section_dicts(self.default_sections, self.user_sections) self.sections = self._merge_section_dicts(self.sections, self.coafile_sections) self.sections = self._merge_section_dicts(self.sections, self.cli_sections) for section in self.sections: if section != "default": self.sections[section].defaults = self.sections["default"] def _load_config_file(self, filename, silent=False): """ Loads sections from a config file. Prints an appropriate warning if it doesn't exist and returns a section dict containing an empty default section in that case. It assumes that the cli_sections are available. :param filename: The file to load settings from. :param silent: Whether or not to warn the user if the file doesn't exist. """ filename = os.path.abspath(filename) try: return self.conf_parser.reparse(filename) except self.conf_parser.FileNotFoundError: if not silent: self.cli_sections["default"].retrieve_logging_objects() self.cli_sections["default"].log_printer.warn( _("The requested coafile '{filename}' does not exist. " "Thus it will not be used.").format(filename=filename)) return {"default": Section("default")} def _fill_settings(self): for section_name in self.sections: section = self.sections[section_name] section.retrieve_logging_objects() bear_dirs = path_list(section.get("bear_dirs", "")) bear_dirs.append(os.path.join(StringConstants.coalib_bears_root, "**")) bears = list(section.get("bears", "")) local_bears = collect_bears(bear_dirs, bears, [BEAR_KIND.LOCAL]) global_bears = collect_bears(bear_dirs, bears, [BEAR_KIND.GLOBAL]) filler = SectionFiller(section) all_bears = copy.deepcopy(local_bears) all_bears.extend(global_bears) filler.fill_section(all_bears) self.local_bears[section_name] = local_bears self.global_bears[section_name] = global_bears def _save_configuration(self): self.conf_writer = None default_section = self.sections["default"] try: if bool(default_section.get("save", "false")): self.conf_writer = ConfWriter(str( default_section.get("config", ".coafile"))) except ValueError: self.conf_writer = ConfWriter(str(default_section.get("save", ".coafile"))) if self.conf_writer is not None: self.conf_writer.write_sections(self.sections) @staticmethod def _merge_section_dicts(lower, higher): """ Merges the section dictionaries. The values of higher will take precedence over the ones of lower. Lower will hold the modified dict in the end. """ for name in higher: if name in lower: lower[name].update(higher[name], ignore_defaults=True) else: # no deep copy needed lower[name] = higher[name] return lower def _warn_nonexistent_targets(self): for target in self.targets: if target not in self.sections: self.sections["default"].log_printer.warn( _("The requested section '{section}' is not existent. " "Thus it cannot be executed.").format(section=target))
def delete_setting(self, button, setting_row): self.sections_dict[self.get_name()].delete_setting(button.get_name()) setting_row.destroy() conf_writer = ConfWriter(self.src + '/.coafile') conf_writer.write_sections(self.sections_dict) conf_writer.close()
class ConfWriterTest(unittest.TestCase): example_file = ('to be ignored \n' ' save=true\n' ' a_default, another = val \n' ' TEST = tobeignored # thats a comment \n' ' test = push \n' ' t = \n' ' [Section] \n' ' [MakeFiles] \n' ' j , ANother = a \n' ' multiline \n' ' value \n' ' ; just a omment \n' ' ; just a omment \n' ' key\\ space = value space\n' ' key\\=equal = value=equal\n' ' key\\\\backslash = value\\\\backslash\n' ' key\\,comma = value,comma\n' ' key\\#hash = value\\#hash\n' ' key\\.dot = value.dot\n' ' a_default = val, val2\n') append_example_file = ('[defaults]\n' 'a = 4\n' 'b = 4,5,6\n' 'c = 4,5\n' 'd = 4\n' '[defaults.new]\n' 'a = 4,5,6,7\n' 'b = 4,5,6,7\n' 'c = 4,5,6,7\n' 'd = 4,5,6,7\n') def setUp(self): self.file = os.path.join(tempfile.gettempdir(), 'ConfParserTestFile') with open(self.file, 'w', encoding='utf-8') as file: file.write(self.example_file) self.log_printer = LogPrinter() self.write_file_name = os.path.join(tempfile.gettempdir(), 'ConfWriterTestFile') self.uut = ConfWriter(self.write_file_name) def tearDown(self): self.uut.close() os.remove(self.file) os.remove(self.write_file_name) def test_exceptions(self): self.assertRaises(TypeError, self.uut.write_section, 5) def test_write(self): result_file = ['[Section]\n', '[MakeFiles]\n', 'j, ANother = a\n', 'multiline\n', 'value\n', '; just a omment\n', '; just a omment\n', 'key\\ space = value space\n', 'key\\=equal = value=equal\n', 'key\\\\backslash = value\\\\backslash\n', 'key\\,comma = value,comma\n', 'key\\#hash = value\\#hash\n', 'key\\.dot = value.dot\n', 'a_default += val2\n', '[cli]\n', 'save = true\n', 'a_default, another = val\n', '# thats a comment\n', 'test = push\n', 't = \n'] sections = load_configuration(['-c', escape(self.file, '\\')], self.log_printer)[0] del sections['cli'].contents['config'] self.uut.write_sections(sections) self.uut.close() with open(self.write_file_name, 'r') as f: lines = f.readlines() self.assertEqual(result_file, lines) def test_append(self): with open(self.file, 'w', encoding='utf-8') as file: file.write(self.append_example_file) result_file = ['[defaults]\n', 'a = 4\n', 'b = 4,5,6\n', 'c = 4,5\n', 'd = 4\n', '[defaults.new]\n', 'b += 7\n', 'c += 6, 7\n', 'a, d += 5, 6, 7\n', '[cli]\n'] sections = load_configuration(['-c', escape(self.file, '\\')], self.log_printer)[0] del sections['cli'].contents['config'] self.uut.write_sections(sections) self.uut.close() with open(self.write_file_name, 'r') as f: lines = f.readlines() self.assertEqual(result_file, lines) def test_write_with_dir(self): self.uut_dir = ConfWriter(tempfile.gettempdir()) self.uut_dir.write_sections({'name': Section('name')}) self.uut_dir.close() with open(os.path.join(tempfile.gettempdir(), '.coafile'), 'r') as f: lines = f.readlines() self.assertEqual(['[name]\n'], lines) os.remove(os.path.join(tempfile.gettempdir(), '.coafile'))
class SectionManager: """ The SectionManager does the following things: - Reading all settings in sections from - Default config - CLI - Configuration file - Collecting all the bears - Filling up all needed settings - Write back the new sections to the configuration file if needed - Give all information back to caller This is done when the run() method is invoked. Anything else is just helper stuff and initialization. """ def __init__(self): self.cli_sections = None self.default_section = None self.conf_sections = None self.cli_parser = CliParser() self.conf_parser = ConfParser() self.conf_writer = None self.local_bears = {} self.global_bears = {} def run(self, arg_list=sys.argv[1:]): self._load_configuration(arg_list) self._fill_settings() self._save_configuration() return self.conf_sections, self.local_bears, self.global_bears def _load_configuration(self, arg_list): self.cli_sections = self.cli_parser.reparse(arg_list=arg_list) try: self.default_section = self.conf_parser.reparse( os.path.abspath( os.path.join(StringConstants.coalib_root, "default_coafile")))["default"] except self.conf_parser.FileNotFoundError: self.cli_sections["default"].retrieve_logging_objects() self.cli_sections["default"].log_printer.err( _("The global default coafile for the settings was not found. " "It seems your installation is broken.") + " " + StringConstants.THIS_IS_A_BUG) raise SystemExit for section in self.cli_sections: self.cli_sections[section].defaults = self.default_section try: config = os.path.abspath( str(self.cli_sections["default"].get("config", "./coafile"))) self.conf_sections = self.conf_parser.reparse(config) # We'll get the default section as default section for every section in this dict with this # Furthermore we will have the CLI Values take precedence over the conf values. self._merge_section_dicts() except self.conf_parser.FileNotFoundError: self.conf_sections = self.cli_sections def _fill_settings(self): for section_name in self.conf_sections: section = self.conf_sections[section_name] section.retrieve_logging_objects() local_bears = BearCollector.from_section([BEAR_KIND.LOCAL], section).collect() global_bears = BearCollector.from_section([BEAR_KIND.GLOBAL], section).collect() filler = SectionFiller(section) all_bears = copy.deepcopy(local_bears) all_bears.extend(global_bears) filler.fill_section(all_bears) self.local_bears[section_name] = local_bears self.global_bears[section_name] = global_bears def _save_configuration(self): self.conf_writer = None try: if bool(self.conf_sections["default"]["save"]): self.conf_writer = ConfWriter( str(self.conf_sections["default"]["config"])) except ValueError: self.conf_writer = ConfWriter( str(self.conf_sections["default"]["save"])) if self.conf_writer is not None: self.conf_writer.write_sections(self.conf_sections) def _merge_section_dicts(self): for section_name in self.cli_sections: if section_name in self.conf_sections: self.conf_sections[section_name].update( self.cli_sections[section_name]) else: self.conf_sections[section_name] = self.cli_sections[ section_name] # no deep copy needed