def get_xy(self): """ Get XY file for the PCB. :returns: The XY file contents. """ cache_dir = get_cache_dir('bom', 'geda', 'pcbxy') ab = os.path.abspath(self.fname) # Generate cache filename h = hashlib.sha1() h.update(ab) cfname = os.path.join(cache_dir, h.hexdigest()) cache_good = False if os.path.exists(cfname): xy_t = os.path.getmtime(ab) cache_t = os.path.getmtime(cfname) if cache_t > xy_t: cache_good = True if not cache_good: self.__export_xy(cfname) else: print("Using cached PCB xy-data for %s" % os.path.basename(self.fname)) f = open(cfname, "r") xy = f.read() f.close() return xy
def __load_bom(self): cache_dir = get_cache_dir('bom', 'geda', 'bom') ab = os.path.abspath(self.fname) # Generate cache filename h = hashlib.sha1() h.update(ab.encode('UTF-8')) cfname = os.path.join(cache_dir, h.hexdigest()) cache_good = False if os.path.exists(cfname): # Discover if the cache is still valid schem_t = os.path.getmtime(ab) cache_t = os.path.getmtime(cfname) if cache_t > schem_t: # Cache is good cache_good = True if not cache_good: self.__export_bom(cfname) else: print("Using cached BOM for %s." % os.path.basename(self.fname)) self.__parse_bom_fname(cfname)
def get_image(self, res): """ Get an image of the PCB. :returns: The contents of the image. """ cache_dir = get_cache_dir('bom', 'geda', 'pcbimg') ab = os.path.abspath(self.fname) # Generate cache filename h = hashlib.sha1() h.update(ab + str(res)) cfname = os.path.join(cache_dir, h.hexdigest()) cache_good = False if os.path.exists(cfname): img_t = os.path.getmtime(ab) cache_t = os.path.getmtime(cfname) if cache_t > img_t: cache_good = True if not cache_good: self.__export_image(res, cfname) else: print('Using cached PCB image for %s.' % os.path.basename(self.fname)) with open(cfname) as file: img = file.read() return img
def grab_url_cached(url): """ Download a possibly cached URL. :returns: The contents of the page. """ cache_dir = get_cache_dir('urls') h = hashlib.sha1() h.update(url.encode('UTF-8')) F = os.path.join(cache_dir, h.hexdigest()) if os.path.exists(F) and (time.time() - os.path.getmtime(F)) < CACHE_LIFE: with open(F) as file: page = file.read() else: # try the remote supplier page cache try: base_url = "https://www.studentrobotics.org/~rspanton/supcache/{}" cached_url = base_url.format(h.hexdigest()) sc = urlopen(cached_url) page = sc.read() except HTTPError: page = urlopen(url).read() with open(F, 'wb') as file: file.write(page) return page
import email.utils import hashlib import re import os import subprocess import sys import six.moves.cPickle as pickle import yaml from sr.tools.inventory import assetcode from sr.tools.environment import get_cache_dir CACHE_DIR = get_cache_dir('inventory') RE_PART = re.compile("^(.+)-sr([%s]+)$" % "".join(assetcode.ALPHABET)) class NotAnInventoryError(OSError): """ Raised when an inventory object is created for a directory that is not an inventory. :param directory: The directory that is not an inventory. Also accessible as the ``directory`` attribute of this class. """ def __init__(self, directory): msg = "'{directory}' is not an inventory.".format(directory=directory) super(NotAnInventoryError, self).__init__(msg) self.directory = directory
import codecs import email.utils import hashlib import re import os import subprocess import sys import six.moves.cPickle as pickle import yaml from sr.tools.inventory import assetcode from sr.tools.environment import get_cache_dir CACHE_DIR = get_cache_dir('inventory') RE_PART = re.compile("^(.+)-sr([%s]+)$" % "".join(assetcode.ALPHABET)) class NotAnInventoryError(OSError): """ Raised when an inventory object is created for a directory that is not an inventory. :param directory: The directory that is not an inventory. Also accessible as the ``directory`` attribute of this class. """ def __init__(self, directory): msg = "'{directory}' is not an inventory.".format(directory=directory) super(NotAnInventoryError, self).__init__(msg) self.directory = directory