def increase_timer(self, time=1): if (bpg.pause_secs + time) >= bpg.max_pause_secs: bpg.pause_secs = bpg.max_pause_secs else: bpg.pause_secs += time l.log("increasing pause_secs for autoplay: " + str(bpg.pause_secs)) self.timer.setInterval(bpg.pause_secs * 1000)
def reverse_sort(self): l.log("reverse sort") print(self._current_index) current_image_path = self._all_images[self._current_index] self._all_images.reverse() self._current_index = self.get_index_from_image_path( current_image_path)
def sort_by_date(self, reverse_sort: bool = False): l.log("sorting images") print(self._current_index) current_image_path = self._all_images[self._current_index] self.initialize_images(mode=file_walker.get_mode(), current_image_path=current_image_path, to_sort=True, reverse_sort=reverse_sort)
def play_image_next(self): if self.is_playing: l.log("play_image_next - auto_play: " + str(self.is_playing) + ", current_index: " + str(self._current_index)) self.image_next() else: l.log("stopping: auto_play: " + str(self.is_playing)) self.timer.stop()
def image_shuffle(self): l.log("shuffle") image_path = self._all_images[self._current_index] self._seed = time.time() random.seed(self._seed) random.shuffle(self._all_images) self._current_index = self.get_index_from_image_path(image_path) self._set_image(self._current_index) self._shuffle_start_index = self._current_index
def decrease_timer(self, time=1): if (bpg.pause_secs - time) < bpg.least_pause_secs: # l.log( # "ignoring decrease timer command as current value too low: " + str( # bpg.pause_secs)) bpg.pause_secs = bpg.least_pause_secs else: bpg.pause_secs -= time l.log("decreasing pause_secs for autoplay: " + str(bpg.pause_secs)) self.timer.setInterval(bpg.pause_secs * 1000)
def _set_image(self, index): if index > len( self._all_images) - 1 or index < -1 * len(self._all_images): l.log("error: resetting again") index = 0 self._current_index = index l.log("setting image") image_pix_map = QPixmap(self._all_images[self._current_index]) print("image: ", image_pix_map.width(), image_pix_map.height()) self._image_label.setPixmap(image_pix_map) self.set_title(self._all_images[self._current_index])
def _get_arguments(): l.log("parsing arguments") global _file_path global _ignore_keyword parser = argparse.ArgumentParser() parser.add_argument('--ignore', type=str, help='keywords to be ignored while parsing files') parser.add_argument('--path', type=str, help='address from where images will be pursed', required=True) args = parser.parse_args() _file_path = args.path if args.ignore: _ignore_keyword = args.ignore
def check_line(line: str, rules_packs: List): count_issues: int = 0 line = line.lower().strip() for rule_pack in rules_packs: rule_name = rule_pack[0] rules = rule_pack[1] l.log("rule_name: " + rule_name) for rule in rules: keyword = rule[0] l.log("keyword: " + keyword) regex = re.compile(r"\b{}\b".format(keyword)) # regex_period = re.compile(r"\b{}.\b".format(keyword)) if len(regex.findall(line)) != 0 : #or len(regex_period.findall(line)): print(""" issue: {}, issue type: {}, line: {} replace with: {}""".format(keyword, rule_name, line, rule[1:])) count_issues += 1 return count_issues
def process_file(filename_crashscope: str, filename_leak_log: str): count_yes: int = 0 count_no: int = 0 whole_leak_count: int = 0 log.log("starting program") print("filename: " + filename_crashscope) leak_set: Set[str] = set() # with open(filename) lines: List[str] = open(filename_crashscope).readlines() for line in lines: line = line.lower() if "leak-" in line: whole_leak_count += 1 log.log(line) leak_substr: str = line.split("/leak-")[1].split("(")[0].strip() log.log("leak_substr: " + leak_substr) leak_set.add(leak_substr) contents = open(filename_leak_log).read() for item in leak_set: str_to_check = "leak-" + item if str_to_check.lower() in contents.lower(): print(str_to_check + ": yes") count_yes += 1 else: count_no += 1 print(str_to_check + ": no") print("total leaks:", whole_leak_count, "unique leaks:", len(leak_set), "unique leaks in both logs:", count_yes, "anomalous leak count:", count_no)
def walk(mode=None, is_sorted: bool = False, reverse_sort: bool = False, dir_path: str = argh.get_path(), increasing_size_sort: bool = False) -> List: """ walks through a path to return list of absolute path of images with png or jpg extensions :param reverse_sort: allows sorting in reverse :param is_sorted: can sort based on time-date :param mode: can be landscape or portrait :param dir_path: to the directory where images will be searched for, defaults to argument :return: List of string path of images """ image_paths = [] # path: str = argh.get_path() if mode: dir_path += mode global current_mode current_mode = mode c_log.log(dir_path) ignore: str = argh.get_ignore_word() c_log.log("checking path integrity") if path.exists(dir_path): c_log.log([dir_path, "exists"]) else: c_log.log("path does not exist") exit() # l.disable() for folderName, sub_folders, filenames in os.walk(dir_path): for filename in filenames: if (filename.endswith('jpg') or filename.endswith('.png') or filename.endswith('.bmp') or filename.endswith('.jpeg')): file_path = os.path.join(folderName, filename) # l.log([file_path, filename]) if ignore and is_exclude_image(file_path, ignore): continue image_paths.append(file_path) if is_sorted: image_paths = sorted(image_paths, key=os.path.getmtime) if reverse_sort: image_paths = sorted(image_paths, key=os.path.getmtime, reverse=True) if increasing_size_sort: image_paths = sorted(image_paths, key=os.path.getsize, reverse=True) return image_paths
def is_exclude_image(image_path: str, ignore_word: str) -> str: import custom_log as l l.log(ignore_word) # exit() if ',' in ignore_word: list_ignores = ignore_word.split(',') for word in list_ignores: if image_path.lower().find(word) != -1: l.log(["found entry in list_ignore: ", image_path, word]) return True else: if image_path.lower().find(ignore_word) != -1: l.log(["found entry in list_ignore: ", image_path, ignore_word]) return True return False
def _get_arguments(): l.log("parsing arguments") global _file_path global _keyword global _ext global _print_line parser = argparse.ArgumentParser() parser.add_argument('--find', type=str, help='keyword to be searched for while parsing files', required=True) parser.add_argument('--path', type=str, help='address from where text files will be scanned', required=True) parser.add_argument( '--ext', type=str, help='extensions which will be looked at to search for word') parser.add_argument( '--printline', type=bool, help= 'configures the program to print the lines with keyword within the file' ) args = parser.parse_args() _file_path = args.path l.log("path: " + _file_path) _keyword = args.find l.log("keyword: " + _keyword) if args.ext: l.log("extension: " + args.ext) _ext = args.ext if args.printline: _print_line = args.printline
def walk(mode=None) -> List: """ walks through a path to return list of absolute path of images with png or jpg extensions :param mode: can be :param path: to the directory where images will be searched for :return: List of string path of images """ image_paths = [] path: str = argh.get_path() if mode: path += mode global current_mode current_mode = mode l.log(path) ignore: str = argh.get_ignore_word() l.log("checking path integrity") if Path.exists(path): l.log([path, "exists"]) else: l.log("path does not exist") exit() # l.disable() for folderName, subfolders, filenames in os.walk(path): for filename in filenames: if (filename.endswith('jpg') or filename.endswith('.png') or filename.endswith('.bmp') or filename.endswith('.jpeg')): file_path = os.path.join(folderName, filename) # l.log([file_path, filename]) if ignore and is_exclude_image(file_path, ignore): continue image_paths.append(file_path) return image_paths
def _get_arguments(): l.log("parsing arguments") global _dir_path global _file_path parser = argparse.ArgumentParser() parser.add_argument('--path', type=str, help='address from where tex files will be scanned') parser.add_argument('--file', type=str, help='address of tex file') args = parser.parse_args() if args.path: _dir_path = args.path l.log("dir path: " + _dir_path) if args.file: _file_path = args.file l.log("file path: " + _file_path)
def _get_arguments(): l.log("parsing arguments") global _force_run global _prefix parser = argparse.ArgumentParser() parser.add_argument('name', type=str, help='specifies the prefix to be used while renaming') parser.add_argument( '-f', action='store_true', help= 'force run, will output list of changes while making changes, without this will only output changes' ) args = parser.parse_args() _prefix = args.name # _file_path = args.path _force_run = args.f l.log(_prefix) l.log(_force_run)
def _build_rules(rule_names: List) -> List: HOME = str(Path.home()) path_rules = HOME + "/git/automating-boring-tasks-using-python/tex_writing_issues/" all_rules: List = [] for rule_name in rule_names: l.log("working with rule: " + rule_name) f_rule = path_rules + "words_" + rule_name l.log("rule file name: " + f_rule) current_rules: List = [] if not os.path.exists(f_rule): print("file_rule {} NOT FOUND. EXITING".format(f_rule)) exit() l.log("path to rule: " + f_rule) rules: List[str] = open(f_rule).readlines() for rule in rules: rule_key = rule.split(',')[:1][0].strip().lower() rule_value = _clean_rule(rule.split(',')[1:]) # double parenthesis, because we are adding a tuple in append function current_rules.append((rule_key, rule_value)) # again, double parenthesis, because we are adding a tuple in append function all_rules.append((rule_name, current_rules)) return all_rules
#!python3 import custom_log as l if __name__ == '__main__': import folder_walk as walk import argument_handler as argh from typing import List l.disable() keyword = argh.get_keyword() l.log("started parsing directories") file_paths = walk.walk() is_print_line: bool = argh.get_print_line() l.log("will start scanning files now.") for file_path in file_paths: l.log("at file: " + file_path) lines: List[str] = open(file_path).readlines() for line in lines: if line.find(keyword) != -1: print(line.strip())
def image_next(self, step: int = 1): l.log("next") self._current_index += step if self._current_index > len(self._all_images): self._current_index = 0 self._set_image(self._current_index)
def walk() -> List: """ walks through a path to return list of absolute path of images with png or jpg extensions :param path: to the directory where images will be searched for :return: List of string path of images """ file_paths = [] path: str = argh.get_path() l.log(path) keyword: str = argh.get_keyword() extension: str = argh.get_extension() l.log("checking path integrity") if Path.exists(path): l.log([path, "exists"]) else: l.log("path does not exist") exit() for folderName, subfolders, filenames in os.walk(path): for filename in filenames: if extension: l.log("checking for extension: " + extension) l.log("with filename: " + filename) if filename.endswith(extension): l.log("extension matched") file_path = os.path.join(folderName, filename) else: continue else: file_path = os.path.join(folderName, filename) l.log([file_path, filename]) file_paths.append(file_path) return file_paths
def pause(self): if not self.is_playing: l.log("pausing auto play: " + str(self.is_playing)) self.timer.stop()
def image_delete(self): l.log("delete: " + self._all_images[self._current_index]) send2trash(self._all_images[self._current_index]) self._all_images.remove(self._all_images[self._current_index]) self._set_image(self._current_index)
def set_title(self, image_path): new_title = self.default_title + image_path l.log("setting title: " + new_title) self.setWindowTitle(new_title)
print("at file: " + file_path) lines: List[str] = open(file_path).readlines() for line in lines: if will_i_process_line(line): line_issues = line_checker.check_line(line, all_rules) count_issues += line_issues print("done with: " + file_path) print("total issues found: " + str(count_issues)) if __name__ == '__main__': import folder_walk as walk import argument_handler as argh from typing import List l.disable() all_rules = rule_builder.get_rules() if argh.get_file_path() is not None: print("single file given : " + argh.get_file_path()) l.log("single file input") process_file(argh.get_file_path(), all_rules) elif argh.get_dir_path() is not None: l.log("directory is given. scanning files from directory") file_paths: List[str] = walk.walk() for file_path in file_paths: process_file(file_path, all_rules) print() else: print( "neither path nor file was declared. Run with -h switch for help")
def keyReleaseEvent(self, event: QKeyEvent): key = event.key() text = event.text() l.log("Key event at top_level_widget: " + str(key) + " " + QKeySequence(key).toString() + "text: " + text) if key == Qt.Key_S: l.log("Key S") self.image_shuffle() elif key == Qt.Key_Left or key == Qt.Key_Backspace: l.log("Key left or backspace") self.image_previous() elif key == Qt.Key_Right or key == Qt.Key_N or key == Qt.Key_Space: l.log("Key Right / N / Space") self.image_next() elif key == Qt.Key_Greater: l.log("> pressed") self.image_next(10) elif key == Qt.Key_Less: l.log("< pressed") self.image_previous(10) elif key == Qt.Key_Delete: l.log("Key Delete") self.image_delete() elif key == Qt.Key_F: l.log("Key F") if self.is_full_screen: self.showNormal() else: self.showFullScreen() # toggle self.is_full_screen = not self.is_full_screen elif key == Qt.Key_V: l.log("Key V") self._browse_event() elif key == Qt.Key_B: l.log("Key B") self._shuffle_start_index = self._current_index elif key == Qt.Key_1: l.log("Key 1 --> landscape mode") self.initialize_images("landscape/") elif key == Qt.Key_2: l.log("Key 2 --> Portrait mode") self.initialize_images("portrait/") elif key == Qt.Key_0: l.log("Key 0 --> go to index 0") if self._shuffle_start_index != 0: self._current_index = self._shuffle_start_index else: self._current_index = 0 self._set_image(self._current_index) elif key == Qt.Key_R: self.reverse_sort() elif key == Qt.Key_D: self.sort_by_date() elif key == Qt.Key_E: self.sort_by_date(reverse_sort=True) elif key == Qt.Key_3: l.log("Key 3 / Reset all") elif key == Qt.Key_4: l.log("Key 4 / Set image to 0th Index in whatever sort") self._set_image(index=0) elif key == Qt.Key_P: l.log("Key P / Play / Pause") self.toggle_play() # timer elif key == Qt.Key_Equal: l.log("equal pressed") self.increase_timer() elif key == Qt.Key_Plus: print("+ pressed") self.increase_timer(10) elif key == Qt.Key_Minus: print("dash pressed") self.decrease_timer() elif key == Qt.Key_Underscore: print("- pressed") self.decrease_timer(10) elif key == Qt.Key_Y: print("Y pressed") self.decrease_timer(bpg.max_pause_secs) elif key == Qt.Key_T: print("T pressed") self.increase_timer(bpg.max_pause_secs) self.pause() self.setFocus()
def toggle_play(self): l.log("toggle auto play is" + str(self.is_playing)) self.is_playing = not self.is_playing self.play()
def get_rules() -> List: all_rules = _build_rules( ["ambiguous", "complicated", "offensive", "strong"]) l.log("rules found are: " + str(all_rules)) return all_rules
# with open(filename) lines: List[str] = open(filename_crashscope).readlines() for line in lines: line = line.lower() if "leak-" in line: whole_leak_count += 1 log.log(line) leak_substr: str = line.split("leak-")[1].split(":")[0].strip() log.log("leak_substr: " + leak_substr) leak_set.add(leak_substr) contents = open(filename_leak_log).read() for item in leak_set: str_to_check = "leak-" + item if str_to_check.lower() in contents.lower(): print(str_to_check + ": yes") count_yes += 1 else: count_no += 1 print(str_to_check + ": no") print("total leaks:", whole_leak_count, "unique leaks:", len(leak_set), "unique leaks in both logs:", count_yes, "anomalous leak count:", count_no) if len(sys.argv) == 3: log.log("two arguments provided") process_file(sys.argv[1], sys.argv[2]) elif len(sys.argv) == 4: log.log("three arguments provided") process_file2(sys.argv[1], sys.argv[2])
def image_previous(self, step: int = 1): l.log("previous") self._current_index -= step if self._current_index <= -1 * len(self._all_images): self._current_index = 0 self._set_image(self._current_index)
def play(self): l.log("starting play timer") if self.is_playing: self.timer.start()