def _init_file(self, filename, mkdirs=0): """ Set up optional file. Call this even if filename is None, so debug code can run. Don't output a history message naming the file -- but return one, so caller can do that later if it wants to. """ if filename: # user wants history saved; save it or print it. try: if mkdirs: # this is optional, for safety mkdirs_in_filename(filename) ff = open(filename, "a") except: print "bug warning: couldn't make history file %r; printing to sys.__stderr__ instead" % filename self.filename = "sys.__stderr__" self.file = sys.__stderr__ else: self.filename = filename self.file = ff elif debug_flags.atom_debug: # developer wants it saved, but program thinks user doesn't print "atom_debug: printing history to sys.__stderr__" self.filename = "sys.__stderr__" self.file = sys.__stderr__ else: pass # make sure print output says what's happening, one way or another if self.file: # even if it's __stderr__ self.file.write("# nanorex history file, format version 050104\n") self.file.flush() file_msg = "(saving history in file \"%s\")" % self.filename else: file_msg = "(history is not being saved in any file)" print file_msg #e remove this soon return file_msg
def _init_file(self, filename, mkdirs = 0): """ Set up optional file. Call this even if filename is None, so debug code can run. Don't output a history message naming the file -- but return one, so caller can do that later if it wants to. """ if filename: # user wants history saved; save it or print it. try: if mkdirs: # this is optional, for safety mkdirs_in_filename(filename) ff = open(filename, "a") except: print "bug warning: couldn't make history file %r; printing to sys.__stderr__ instead" % filename self.filename = "sys.__stderr__" self.file = sys.__stderr__ else: self.filename = filename self.file = ff elif debug_flags.atom_debug: # developer wants it saved, but program thinks user doesn't print "atom_debug: printing history to sys.__stderr__" self.filename = "sys.__stderr__" self.file = sys.__stderr__ else: pass # make sure print output says what's happening, one way or another if self.file: # even if it's __stderr__ self.file.write("# nanorex history file, format version 050104\n") self.file.flush() file_msg = "(saving history in file \"%s\")" % self.filename else: file_msg = "(history is not being saved in any file)" print file_msg #e remove this soon return file_msg
def _make_prefs_shelf(): """ [private function] call this once per session, to create or find the shelf (whose name depends only on the dbm format we'll use for it), and create the cache of its contents, and store a comment there about this process, and close the shelf again in case a concurrent process is sharing the same shelf with us. """ global _shelfname, _shelf, _cache, _defaults, _trackers nanorex = find_or_make_Nanorex_directory() global dbname _shelfname = os.path.join(nanorex, "Preferences", "%s-shelf" % dbname) # This name should differ when db format differs. # Note: the actual filename used might have an extension added # by the db module (in theory, it might even create two files # with different extensions from the given basename). # By experiment, on the Mac, with bsddb there is no extension added, # and without it there is '.db' added. [bruce 050105] mkdirs_in_filename(_shelfname) _shelf = shelve.open(_shelfname) _cache = {} _cache.update(_shelf) # will this work? was_just_made = (not _cache) #bruce 080505 if was_just_made: print "made prefs db, basename", _shelfname else: print "prefs db already existed, basename", _shelfname _defaults = {} _trackers = {} # zap obsolete contents obskeys = [] for key in _cache.keys(): if key.isdigit() or key in ['_session_counter']: obskeys.append(key) for key in obskeys: del _shelf[key] del _cache[key] ###@@@ following should be revised to handle junk contents gracefully, # and to notice the existing format version and handle older formats appropriately # or reject them gracefully. _store_while_open('_format_version', 'preferences.py/v050106') # storing this blindly is only ok since the only prior version is one # we can transparently convert to this one by the "zap obskeys" above. # store a comment about the last process to start using this shelf # (nothing yet looks at this comment) proc_info = "process: pid = %d, starttime = %r" % (os.getpid(), time.asctime()) _store_while_open('_fyi/last_proc', proc_info) # (nothing yet looks at this) _close() if was_just_made: # use DEFAULT_PREFS_BASENAME [bruce 080505 new feature]; # file format must correspond with that written by # packaging/Pref_Mod/pref_modifier.py default_prefs_values = {} # read the values from DEFAULT_PREFS_BASENAME # (while shelf is closed, in case this takes time) try: filename = os.path.join(nanorex, "Preferences", DEFAULT_PREFS_BASENAME) if not os.path.exists(filename): lines = [] print "didn't find", filename else: file = open(filename, "rU") lines = file.readlines() file.close() print "reading from", filename for line in lines: line0 = line try: # try/except so corrupted lines don't break good ones added later # assume line has the correct format: key = val\n while line[-1] in ('\r', '\n'): # 'while' is to handle Windows newlines # (probably not needed due to 'rU') line = line[:-1] key, val = line.split(" = ") # don't strip key or val -- they might end with spaces def decode(string1): words = string1.split(r'\\') for i in range(len(words)): word = words[i] word = word.replace(r'\=', '=') word = word.replace(r'\n', '\n') word = word.replace(r'\r', '\r') words[i] = word continue return '\\'.join(words) key = decode(key) val = decode(val) if val == 'True': val = True elif val == 'False': val = False default_prefs_values[key] = val # print "read key, val = (%r, %r)" % (key, val) pass except: print "ignoring exception in this line: %r" % (line0, ) pass continue pass except: print "ignoring exception reading from", DEFAULT_PREFS_BASENAME default_prefs_values = {} pass items = default_prefs_values.items() items.sort() # just to make the following console prints look nicer # now open, store the values, and close _shelf = shelve.open(_shelfname) for key, val in items: pkey = _PREFS_KEY_TO_SHELF_KEY(key) _store_while_open(pkey, val) print "stored key, val = (%r, %r)" % (key, val) _close() pass return
def _make_prefs_shelf(): """ [private function] call this once per session, to create or find the shelf (whose name depends only on the dbm format we'll use for it), and create the cache of its contents, and store a comment there about this process, and close the shelf again in case a concurrent process is sharing the same shelf with us. """ global _shelfname, _shelf, _cache, _defaults, _trackers nanorex = find_or_make_Nanorex_directory() global dbname _shelfname = str_or_unicode(os.path.join( nanorex, "Preferences", "%s-shelf" % dbname )) # This name should differ when db format differs. # Note: the actual filename used might have an extension added # by the db module (in theory, it might even create two files # with different extensions from the given basename). # By experiment, on the Mac, with bsddb there is no extension added, # and without it there is '.db' added. [bruce 050105] mkdirs_in_filename(_shelfname) _shelf = shelve.open(_shelfname.encode("utf_8")) _cache = {} _cache.update(_shelf) # will this work? was_just_made = (not _cache) #bruce 080505 if was_just_made: print u"made prefs db, basename", _shelfname.encode("utf_8") else: print u"prefs db already existed, basename", _shelfname.encode("utf_8") _defaults = {} _trackers = {} # zap obsolete contents obskeys = [] for key in _cache.keys(): if key.isdigit() or key in ['_session_counter']: obskeys.append(key) for key in obskeys: del _shelf[key] del _cache[key] ###@@@ following should be revised to handle junk contents gracefully, # and to notice the existing format version and handle older formats appropriately # or reject them gracefully. _store_while_open('_format_version', 'preferences.py/v050106') # storing this blindly is only ok since the only prior version is one # we can transparently convert to this one by the "zap obskeys" above. # store a comment about the last process to start using this shelf # (nothing yet looks at this comment) proc_info = "process: pid = %d, starttime = %r" % (os.getpid(), time.asctime()) _store_while_open( '_fyi/last_proc', proc_info ) # (nothing yet looks at this) _close() if was_just_made: # use DEFAULT_PREFS_BASENAME [bruce 080505 new feature]; # file format must correspond with that written by # packaging/Pref_Mod/pref_modifier.py default_prefs_values = {} # read the values from DEFAULT_PREFS_BASENAME # (while shelf is closed, in case this takes time) try: filename = os.path.join( nanorex, "Preferences", DEFAULT_PREFS_BASENAME ) if not os.path.exists(filename): lines = [] print u"didn't find", filename.encode("utf_8") else: file = open( filename, "rU") lines = file.readlines() file.close() print u"reading from", filename.encode("utf_8") for line in lines: line0 = line try: # try/except so corrupted lines don't break good ones added later # assume line has the correct format: key = val\n while line[-1] in ('\r', '\n'): # 'while' is to handle Windows newlines # (probably not needed due to 'rU') line = line[:-1] key, val = line.split(" = ") # don't strip key or val -- they might end with spaces def decode(string1): words = string1.split(r'\\') for i in range(len(words)): word = words[i] word = word.replace(r'\=', '=') word = word.replace(r'\n', '\n') word = word.replace(r'\r', '\r') words[i] = word continue return '\\'.join(words) key = decode(key) val = decode(val) if val == 'True': val = True elif val == 'False': val = False default_prefs_values[key] = val # print "read key, val = (%r, %r)" % (key, val) pass except: print "ignoring exception in this line: %r" % (line0,) pass continue pass except: print "ignoring exception reading from", DEFAULT_PREFS_BASENAME default_prefs_values = {} pass items = default_prefs_values.items() items.sort() # just to make the following console prints look nicer # now open, store the values, and close _shelf = shelve.open(_shelfname.encode("utf_8")) for key, val in items: pkey = _PREFS_KEY_TO_SHELF_KEY(key) _store_while_open( pkey, val) print "stored key, val = (%r, %r)" % (key, val) _close() pass return