def get_default_cache():
"""
Return the ``PYTHON_EGG_CACHE`` environment variable
or a platform-relevant user cache dir for an app
named "Python-Eggs".
"""
return (
os.environ.get('PYTHON_EGG_CACHE')
or appdirs.user_cache_dir(appname='Python-Eggs')
)
python类user_cache_dir()的实例源码
def run(self, cache=True):
"""Run application."""
self._query()
# configure `requests` cache
if cache:
cache_dir = appdirs.user_cache_dir('craigslist')
os.makedirs(cache_dir, exist_ok=True)
requests_cache.install_cache(
cache_name=os.path.join(cache_dir, 'craigslist'),
expire_after=timedelta(hours=0.5))
print('Running query...\n')
# record the start time
start = time.time()
self.prices = self._getprices()
# determine elapsed time of queries
self.duration = time.time() - start
# remove expired cache entries
if cache:
requests_cache.core.remove_expired_responses()
# print statistics (if any price data exists)
if self.prices:
self._print()
else:
print('Nothing found for that search.')
def get_default_cache():
"""
Return the ``PYTHON_EGG_CACHE`` environment variable
or a platform-relevant user cache dir for an app
named "Python-Eggs".
"""
return (
os.environ.get('PYTHON_EGG_CACHE')
or appdirs.user_cache_dir(appname='Python-Eggs')
)
def get_default_cache():
"""
Return the ``PYTHON_EGG_CACHE`` environment variable
or a platform-relevant user cache dir for an app
named "Python-Eggs".
"""
return (
os.environ.get('PYTHON_EGG_CACHE')
or appdirs.user_cache_dir(appname='Python-Eggs')
)
def main():
generators = {"grid": grid_drawer.TracksDrawer(),
"calendar": calendar_drawer.TracksDrawer(),
"heatmap": heatmap_drawer.TracksDrawer(),
"circular": circular_drawer.TracksDrawer()}
args_parser = argparse.ArgumentParser()
args_parser.add_argument('--gpx-dir', dest='gpx_dir', metavar='DIR', type=str, default='.',
help='Directory containing GPX files (default: current directory).')
args_parser.add_argument('--output', metavar='FILE', type=str, default='poster.svg',
help='Name of generated SVG image file (default: "poster.svg").')
args_parser.add_argument('--year', metavar='YEAR', type=str, default='all',
help='Filter tracks by year; "NUM", "NUM-NUM", "all" (default: all years)')
args_parser.add_argument('--title', metavar='TITLE', type=str, default="My Tracks",
help='Title to display (default: "My Tracks").')
args_parser.add_argument('--athlete', metavar='NAME', type=str, default="John Doe",
help='Athlete name to display (default: "John Doe").')
args_parser.add_argument('--special', metavar='FILE', action='append', default=[],
help='Mark track file from the GPX directory as special; use multiple times to mark multiple tracks.')
args_parser.add_argument('--type', metavar='TYPE', default='grid', choices=generators.keys(),
help='Type of poster to create (default: "grid", available: "{}").'.format('", "'.join(generators.keys())))
args_parser.add_argument('--background-color', dest='background_color', metavar='COLOR', type=str,
default='#222222', help='Background color of poster (default: "#222222").')
args_parser.add_argument('--track-color', dest='track_color', metavar='COLOR', type=str, default='#4DD2FF',
help='Color of tracks (default: "#4DD2FF").')
args_parser.add_argument('--text-color', dest='text_color', metavar='COLOR', type=str, default='#FFFFFF',
help='Color of text (default: "#FFFFFF").')
args_parser.add_argument('--special-color', dest='special_color', metavar='COLOR', default='#FFFF00',
help='Special track color (default: "#FFFF00").')
args_parser.add_argument('--units', dest='units', metavar='UNITS', type=str, choices=['metric', 'imperial'],
default='metric', help='Distance units; "metric", "imperial" (default: "metric").')
args_parser.add_argument('--clear-cache', dest='clear_cache', action='store_true', help='Clear the track cache.')
args = args_parser.parse_args()
loader = track_loader.TrackLoader()
loader.cache_dir = os.path.join(appdirs.user_cache_dir(__app_name__, __app_author__), "tracks")
if not loader.year_range.parse(args.year):
raise Exception('Bad year range: {}.'.format(args.year))
loader.special_file_names = args.special
if args.clear_cache:
loader.clear_cache()
tracks = loader.load_tracks(args.gpx_dir)
if not tracks:
raise Exception('No tracks found.')
print("Creating poster of type '{}' and storing it in file '{}'...".format(args.type, args.output))
p = poster.Poster(generators[args.type])
p.athlete = args.athlete
p.title = args.title
p.colors = {'background': args.background_color,
'track': args.track_color,
'special': args.special_color,
'text': args.text_color}
p.units = args.units
p.tracks = tracks
p.draw(args.output)
def setup_sync_dir(self):
self.cache_dir = appdirs.user_cache_dir("studip", "fknorr")
self.create_path(self.cache_dir)
history_file_name = os.path.join(appdirs.user_cache_dir("studip", "fknorr"), "history")
history = []
try:
with open(history_file_name, "r", encoding="utf-8") as file:
history = list(filter(None, file.read().splitlines()))
except Exception:
pass
skipped_history = 0
if "sync_dir" in self.command_line:
sync_dir = self.command_line["sync_dir"]
else:
if history and os.path.isdir(history[0]):
sync_dir = history[0]
print("Using last sync directory {} ...".format(sync_dir))
else:
skipped_history = 1
default_dir = "~/StudIP"
for entry in history[1:]:
skipped_history += 1
if os.path.isdir(entry):
default_dir = entry
sync_dir = input("Sync directory [{}]: ".format(default_dir))
if not sync_dir:
sync_dir = default_dir
sync_dir = os.path.abspath(os.path.expanduser(sync_dir))
history = history[skipped_history:]
while sync_dir in history:
history.remove(sync_dir)
history.insert(0, sync_dir)
self.sync_dir = sync_dir
try:
with open(history_file_name, "w", encoding="utf-8") as file:
file.write("\n".join(history) + "\n")
except Exception as e:
self.print_io_error("Unable to write to", history_file_name, e)
raise ApplicationExit()
self.dot_dir = os.path.join(self.sync_dir, ".studip")
self.create_path(self.dot_dir)
self.config_file_name = os.path.join(self.dot_dir, "studip.conf")
self.db_file_name = os.path.join(self.dot_dir, "cache.sqlite")