def __init__(self, id=None, hash=None, url=None): if id: self.id = id self.url = self.read_from('url_by_id', self.id) elif hash: self.hash = hash self.id = self.read_from('id_by_hash', self.hash) self.url = self.read_from('url_by_id', self.id) elif url: self.url = url if urlparse(url).scheme else 'http://' + url try: self.id = self.read_from('id_by_hash', self.hash) except: while True: # Keep doing this until we find an unoccupied slot... self.id = make_identifier() if not os.path.exists(path_for('u', 'url_by_id', self.id + '.txt')): break else: raise Exception("Can't create an URL without an ID, a Hash or an URL")
def __init__(self, path): self.path = path self.components = [] last_component = None for comp in self.path.split(os.path.sep): if comp: last_component = Component(last_component, comp) self.components.append(last_component) self.title = last_component.title if last_component else 'Index' self.realpath = os.path.abspath(path_for('w', path)) if os.path.isdir(self.realpath): self.filepath = os.path.join(self.realpath, '_Index.md') self.load_subpages() else: self.filepath = self.realpath + '.md' self.exists = os.path.exists(self.filepath) if self.exists: self.load()
import config import util import camera from logzero import logger, logfile from sense_hat import SenseHat from datetime import datetime, timedelta from time import sleep sh = SenseHat() # Configure logging logfile(util.path_for(config.LOGFILE)) # Configure runtime start_time = datetime.now() end_time = start_time + timedelta(minutes=config.RUNTIME) logger.info(f'Starting logging at {start_time}') logger.info(f'Will log for {config.RUNTIME} minutes ({end_time})') # Configure CSV csvfile = util.path_for_data(1) logger.info(f'Logging to {csvfile}') # TODO write header while True: # Check for end time now = datetime.now() if now >= end_time: logger.info(f'Finished run at {now}')
def save(self): with open(path_for('u', 'id_by_hash', self.hash + '.txt'), 'w') as f: f.write(self.id) with open(path_for('u', 'url_by_id', self.id + '.txt'), 'w') as f: f.write(self.url)
def read_from(self, type_, key): path = path_for('u', type_, key + '.txt') with open(path) as f: return f.read()
def document(path): path = path_for('d', path) if not os.path.exists(path): abort(404) doc = Document(path) return render_template('d/document.html', document=doc)