def py_stats(session): # FIXME: shares a lot of code with `py_search` - refactor them out! word_count, no_stories = 0, 0 date_iter = DateIterator(date_start = session.birthday) for i, day in date_iter: try: story = Story(session, day) if not story.get_path(): no_stories += 1 continue data = story.decrypt() word_count += simple_counter(data) date_iter.send_msg('[Total words: %s]' % word_count) except AssertionError: errors += 1 if errors > 10: print ERROR, "More than 10 files couldn't be decrypted! Terminating the search..." return None assert no_stories < (i + 1) return word_count
def py_stats( session ): # FIXME: shares a lot of code with `py_search` - refactor them out! word_count, no_stories = 0, 0 date_iter = DateIterator(date_start=session.birthday) for i, day in date_iter: try: story = Story(session, day) if not story.get_path(): no_stories += 1 continue data = story.decrypt() word_count += simple_counter(data) date_iter.send_msg('[Total words: %s]' % word_count) except AssertionError: errors += 1 if errors > 10: print ERROR, "More than 10 files couldn't be decrypted! Terminating the search..." return None assert no_stories < (i + 1) return word_count
def reconfigure(self): # FIXME: Could take arguments via command-line? '''Reset the diary's configuration''' try: self._reset() self.delete_config_file() clear_screen() print "\nLet's start configuring your diary...\n", \ '\nEnter the location for your diary...', \ "\n(Note that this will create a foler named 'Diary' if the path doesn't end with it)" self.location = os.path.expanduser(raw_input('\nPath: ')) while not write_access(self.location): self.location = os.path.expanduser(raw_input('\nPlease enter a valid path: ')) if not self.location.rstrip(os.sep).endswith('Diary'): # just put everything in a folder for Diary self.location = os.path.join(self.location, 'Diary') print '(Reminding you that this will make use of %r)' % self.location if not os.path.exists(self.location): os.mkdir(self.location) self.location = self.location.rstrip(os.sep) + os.sep while True: try: # 'birthday' of the diary is important because random stories and searching is based on that birth = raw_input('''\ \nWhen did you start writing this diary? (Press [Enter] for today)\ \nDate should be of the form YYYY-MM-DD (Mind you, with hyphen!) \nDate: ''') # FIXME: just ask the year and 'infer' from the location if not birth: self.birthday, life_time = datetime.now(), 'new' break else: self.birthday, life_time = datetime.strptime(birth, '%Y-%m-%d'), 'old' first_story_exists, date = self.find_stories() if first_story_exists: break elif date: print ERROR, "Your 'first' story doesn't exist!", \ date.strftime('However, a story exists on %B %d, %Y (%A)\n') if raw_input('Do you wanna begin from there (y), or go for another date (n)? ') == 'y': self.birthday = date break else: print ERROR, "A story doesn't exist (in the given path) on that day!" continue except ValueError: print ERROR, 'Oops! ERROR in input. Check the date format and try again...' while True: self.get_pass(life_time = life_time) first_story = Story(self, self.birthday) try: if first_story.get_path(): _data = first_story.decrypt() break elif (datetime.now() - self.birthday).days: # 'unreachable' because this should've already been handled raise Exception, 'Entered unreachable code!' else: # first-timer... break except AssertionError: print ERROR, "Couldn't decrypt your 'first' story with the given password! Try again..." self.write_to_config_file() self.loop = True print "\nIf you plan to reconfigure it manually, then it's located here (%s)" % self.config_location print "And, be careful with that, because invalid configuration files will be deleted during startup!" raw_input('\nPress [Enter] to continue...') except (KeyboardInterrupt, EOFError): sleep(CAPTURE_WAIT) if not all([self.location, self.key, self.birthday, self.loop]): print '\n', ERROR, "Failed to store the login credentials!" if os.path.exists(self.config_location): os.remove(self.config_location) self._reset()
def change_pass(session, is_arg=False): clear_screen() old_key, old_loc = session.key[:], session.location[:] try: assert write_access(session.location) print "\nLet's change your password..." temp_name = 'BIOGRAPHER_' + str(randgen())[2:] temp_loc = os.path.join( os.path.dirname(session.location.rstrip(os.sep)), temp_name) # If we're changing the password through command, then there's no reason for asking the existing one twice! key_hash = None if is_arg else hasher(sha256, old_key) session.get_pass(key_hash, check_against=old_key) new_key = session.key[:] while True: try: print WARNING, 'Copying your stories to a temporary working directory (%s)...' % temp_loc shutil.copytree(session.location, temp_loc) session.location = temp_loc break except (IOError, OSError): print ERROR, "Couldn't get write access to the path!" while True: working_dir = os.path.expanduser( raw_input( 'Enter a path to choose as working directory: ')) if old_loc.rstrip(os.sep) == os.path.dirname( working_dir.rstrip(os.sep)): print ERROR, "Working directory shouldn't share the location of your stories!" else: break temp_loc = os.path.join(working_dir, temp_name) for _i, day in DateIterator(date_start=session.birthday, progress_msg=' Processing files: %s'): session.key = old_key story_old = Story(session, day) session.key = new_key story_new = Story(session, day) try: if story_old.get_path(): story_old.decrypt( overwrite=True ) # well, both are working on the same file really! story_new.encrypt(echo=False) except AssertionError: print ERROR, "This file couldn't be decrypted! (filename hash: %s)" % story_old.get_hash(), \ "\nResolve it before changing the password again..." raise AssertionError except (AssertionError, KeyboardInterrupt, EOFError): session.key, session.location = old_key, old_loc sleep(CAPTURE_WAIT) if os.path.exists(temp_loc): shutil.rmtree(temp_loc) print ERROR, 'Interrupted! Failed to change the password!' return shutil.rmtree(old_loc) session.key, session.location = new_key, old_loc print "\n\nOverwriting the existing stories..." os.rename(temp_loc, old_loc) print 'Modifying the configuration file...' session.write_to_config_file() print SUCCESS, 'Password has been changed!'
def reconfigure(self): # FIXME: Could take arguments via command-line? '''Reset the diary's configuration''' try: self._reset() self.delete_config_file() clear_screen() print "\nLet's start configuring your diary...\n", \ '\nEnter the location for your diary...', \ "\n(Note that this will create a foler named 'Diary' if the path doesn't end with it)" self.location = os.path.expanduser(raw_input('\nPath: ')) while not write_access(self.location): self.location = os.path.expanduser( raw_input('\nPlease enter a valid path: ')) if not self.location.rstrip(os.sep).endswith( 'Diary'): # just put everything in a folder for Diary self.location = os.path.join(self.location, 'Diary') print '(Reminding you that this will make use of %r)' % self.location if not os.path.exists(self.location): os.mkdir(self.location) self.location = self.location.rstrip(os.sep) + os.sep while True: try: # 'birthday' of the diary is important because random stories and searching is based on that birth = raw_input('''\ \nWhen did you start writing this diary? (Press [Enter] for today)\ \nDate should be of the form YYYY-MM-DD (Mind you, with hyphen!) \nDate: ''') # FIXME: just ask the year and 'infer' from the location if not birth: self.birthday, life_time = datetime.now(), 'new' break else: self.birthday, life_time = datetime.strptime( birth, '%Y-%m-%d'), 'old' first_story_exists, date = self.find_stories() if first_story_exists: break elif date: print ERROR, "Your 'first' story doesn't exist!", \ date.strftime('However, a story exists on %B %d, %Y (%A)\n') if raw_input( 'Do you wanna begin from there (y), or go for another date (n)? ' ) == 'y': self.birthday = date break else: print ERROR, "A story doesn't exist (in the given path) on that day!" continue except ValueError: print ERROR, 'Oops! ERROR in input. Check the date format and try again...' while True: self.get_pass(life_time=life_time) first_story = Story(self, self.birthday) try: if first_story.get_path(): _data = first_story.decrypt() break elif (datetime.now() - self.birthday).days: # 'unreachable' because this should've already been handled raise Exception, 'Entered unreachable code!' else: # first-timer... break except AssertionError: print ERROR, "Couldn't decrypt your 'first' story with the given password! Try again..." self.write_to_config_file() self.loop = True print "\nIf you plan to reconfigure it manually, then it's located here (%s)" % self.config_location print "And, be careful with that, because invalid configuration files will be deleted during startup!" raw_input('\nPress [Enter] to continue...') except (KeyboardInterrupt, EOFError): sleep(CAPTURE_WAIT) if not all([self.location, self.key, self.birthday, self.loop]): print '\n', ERROR, "Failed to store the login credentials!" if os.path.exists(self.config_location): os.remove(self.config_location) self._reset()