def get_text(self, title): text = filesystem.read_file(self.get_path(title)) # An Error occured if not text: text = _('This template file contains no text or has unreadable content.') return text
def _read_file(self, filename): content = filesystem.read_file(filename) # Delete comments and whitespace. lines = [delete_comment(line.strip()) for line in content.splitlines()] dictionary = {} for line in lines: if '=' not in line: continue pair = line.partition('=')[::2] key, value = [s.strip() for s in pair] # Skip obsolete keys to prevent rewriting them to disk. if key in self.obsolete_keys: continue try: value = int(value) except ValueError: pass dictionary[key] = value return dictionary
def _read_file(self, filename): content = filesystem.read_file(filename) # Delete comments and whitespace. lines = [delete_comment(line.strip()) for line in content.splitlines()] dictionary = {} for line in lines: if "=" not in line: continue pair = line.partition("=")[::2] key, value = [s.strip() for s in pair] # Skip obsolete keys to prevent rewriting them to disk. if key in self.obsolete_keys: continue try: value = int(value) except ValueError: pass dictionary[key] = value return dictionary
def get_days(self, dir): for file in self._get_files(dir): match = self.date_exp.match(file) if match: year = int(match.group(1)) month = int(match.group(2)) day = int(match.group(3)) import_day = ImportDay(year, month, day) path = os.path.join(dir, file) text = filesystem.read_file(path) import_day.text = text yield import_day
def _read_file(self, file): key_value_pairs = [] content = filesystem.read_file(file) if not content: return {} lines = content.split('\n') lines = map(str, lines) # delete comments key_value_pairs = map(lambda line: delete_comment(line), lines) #delete whitespace key_value_pairs = map(str.strip, key_value_pairs) #delete empty lines key_value_pairs = filter(lambda line: len(line) > 0, key_value_pairs) dictionary = {} #read keys and values for key_value_pair in key_value_pairs: if '=' in key_value_pair: try: # Delete whitespace around = pair = key_value_pair.split('=') key, value = map(str.strip, pair) # Do not add obsolete keys -> they will not be rewritten # to disk if key in self.obsolete_keys: continue try: #Save value as int if possible value_int = int(value) dictionary[key] = value_int except ValueError: dictionary[key] = value except Exception: logging.error('The line "' + key_value_pair + \ '" in the config file contains errors') return dictionary
def _read_file(self, file): key_value_pairs = [] content = filesystem.read_file(file) if not content: return {} lines = content.split('\n') # delete comments key_value_pairs = map(lambda line: delete_comment(line), lines) #delete whitespace key_value_pairs = map(unicode.strip, key_value_pairs) #delete empty lines key_value_pairs = filter(bool, key_value_pairs) dictionary = {} #read keys and values for key_value_pair in key_value_pairs: if '=' in key_value_pair: try: # Delete whitespace around = pair = key_value_pair.split('=') key, value = map(unicode.strip, pair) # Do not add obsolete keys -> they will not be rewritten # to disk if key in self.obsolete_keys: continue try: #Save value as int if possible dictionary[key] = int(value) except ValueError: dictionary[key] = value except Exception: msg = 'The line "%s" in the config file contains errors' logging.error(msg % key_value_pair) return dictionary
def get_text(self, title): filename = self.titles_to_files.get(title, None) if not filename: return '' text = filesystem.read_file(filename) # An Error occured if not text: text = ('This template contains no text or has unreadable content. To edit it, ' 'click the arrow right of "Template" ' 'and select the template under "Edit Template".') # convert every "$date$" to the current date config = self.main_window.journal.config format_string = config.read('dateTimeString', '%A, %x %X') date_string = dates.format_date(format_string) template_text = text.replace(u'$date$', date_string) return template_text
def _read_file(self, file): key_value_pairs = [] content = filesystem.read_file(file) if not content: return {} lines = content.split('\n') # Delete comments and whitespace. key_value_pairs = [delete_comment(line.strip()) for line in lines] #delete empty lines key_value_pairs = [line for line in lines if line] dictionary = {} #read keys and values for key_value_pair in key_value_pairs: if '=' in key_value_pair: try: # Delete whitespace around = pair = key_value_pair.partition('=')[::2] key, value = map(unicode.strip, pair) # Do not add obsolete keys -> they will not be rewritten # to disk if key in self.obsolete_keys: continue try: #Save value as int if possible dictionary[key] = int(value) except ValueError: dictionary[key] = value except Exception: msg = 'The line "%s" in the config file contains errors' logging.error(msg % key_value_pair) return dictionary
def get_text(self, title): filename = self.titles_to_files.get(title, None) if not filename: return '' text = filesystem.read_file(filename) # An Error occured if not text: text = ( 'This template contains no text or has unreadable content. To edit it, ' 'click the arrow right of "Template" ' 'and select the template under "Edit Template".') # convert every "$date$" to the current date config = self.main_window.journal.config format_string = config.read('dateTimeString', '%A, %x %X') date_string = dates.format_date(format_string) template_text = text.replace(u'$date$', date_string) return template_text
def get_text(self, title): filename = self.titles_to_files.get(title, None) if not filename: return '' text = filesystem.read_file(filename) # An Error occured if not text: text = 'This template contains no text or has unreadable content. To edit it, ' \ 'click the arrow right of "Template" ' \ 'and select the template under "Edit Template".' # convert every "$date$" to the current date default_date_string = '%A, %x %X' date_string = self.main_window.journal.config.read('dateTimeString', default_date_string) date = time.strftime(date_string) try: text = text.replace('$date$', date) except UnicodeDecodeError, err: # TODO: Figure out real source of the error, # maybe load config with filesystem.read_file logging.error('Error replacing $date$: "%s"' % err)