01234567890123456789012345678901234567890123456789012345678901234567890123456789
10191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059 |
<----SKIPPED LINES---->
file_contents = content_file.read()
except IOError:
if log_exception:
Log('Unable to read '+filename)
return ''
return file_contents
# because reading is ~25x more expensive than getmtime, we will only read & parse if
# the getmtime is more recent than last call for this file. So this dict stores the
# a tuple, the last time read & the resulting parsed return value
CACHED_FILES = {}
def ReadAndParseSettings(filename):
"""Reads given filename and then parses the resulting key-value pairs into a dict."""
global CACHED_FILES
(last_read_time, settings) = CACHED_FILES.get(filename, (0, {}))
if os.path.exists(filename):
last_modified = os.path.getmtime(filename)
if last_modified > last_read_time:
setting_str = ReadFile(filename)
settings = ParseSettings(setting_str)
CACHED_FILES[filename] = (last_modified, settings)
return settings
# File does not - or at least no longer - exists; so remove the cache
if filename in CACHED_FILES:
CACHED_FILES.pop(filename)
return {}
def BuildSettings(d):
"""Converts a dict to a string of form key1=value1;...;keyn=valuen; keys alpha sorted."""
kv_pairs = []
for key in sorted(list(d.keys())):
kv_pairs.append('%s=%s' % (key, d[key]))
s = ';'.join(kv_pairs)
if s: # add terminating semicolon
s += ';'
return s
<----SKIPPED LINES---->
|
01234567890123456789012345678901234567890123456789012345678901234567890123456789
10191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059 |
<----SKIPPED LINES---->
file_contents = content_file.read()
except IOError:
if log_exception:
Log('Unable to read '+filename)
return ''
return file_contents
# because reading is ~25x more expensive than getmtime, we will only read & parse if
# the getmtime is more recent than last call for this file. So this dict stores the
# a tuple, the last time read & the resulting parsed return value
CACHED_FILES = {}
def ReadAndParseSettings(filename):
"""Reads given filename and then parses the resulting key-value pairs into a dict."""
global CACHED_FILES
(last_read_time, settings) = CACHED_FILES.get(filename, (0, {}))
if os.path.exists(filename):
last_modified = os.path.getmtime(filename)
if last_modified > last_read_time:
setting_str = ReadFile(filename)
settings = ParseSettings(setting_str)
#CACHED_FILES[filename] = (last_modified, settings) # TODO - uncomment?
return settings
# File does not - or at least no longer - exists; so remove the cache
if filename in CACHED_FILES:
CACHED_FILES.pop(filename)
return {}
def BuildSettings(d):
"""Converts a dict to a string of form key1=value1;...;keyn=valuen; keys alpha sorted."""
kv_pairs = []
for key in sorted(list(d.keys())):
kv_pairs.append('%s=%s' % (key, d[key]))
s = ';'.join(kv_pairs)
if s: # add terminating semicolon
s += ';'
return s
<----SKIPPED LINES---->
|