def query(server, force_ipv6=False): """ Return current time from NTP server as a Unix timestamp >>> import time >>> res = query('us.pool.ntp.org') >>> type(res) <class 'int'> >>> res > time.time() - 5 True """ timeout = 3 ntp_port = 123 family = socket.AF_INET6 if force_ipv6 else 0 sock_type = socket.SOCK_DGRAM infos = socket.getaddrinfo(server, ntp_port, family, sock_type) log.debug(infos) family, socktype, proto, canonname, sockaddr = infos[0] log.info(lf('Requesting time from {sockaddr}')) client = socket.socket(family=family, type=sock_type, proto=proto) client.settimeout(timeout) data = b'\x1b' + 47 * b'\x00' client.sendto(data, sockaddr) data, address = client.recvfrom(1024) if not data: return log.info(lf('Response received from: {address}')) t = struct.unpack('!12I', data)[10] return t - TIME1970
def construct_payment_url(job): tmpl = 'RecaptureDocs conversion - {n_pages} pages' reason = tmpl.format(n_pages=len(job)) return_url = lf('/complete_payment/{job.id}') transaction_amount = str(float(job.cost)) # TODO: construct URL from details raise NotImplemented(locals())
def _restore(cls, data): id = data.pop('_id') result = jaraco.modb.decode(data) if not result.id == id: raise ValueError( lf("ID mutated on load: {id} became {result.id}")) return result
def authorize(self): sess = dropbox.get_session() request_token = sess.obtain_request_token() self.tokens[request_token.key] = request_token callback = cherrypy.url(lf('save_token')) url = sess.build_authorize_url(request_token, oauth_callback=callback) raise cherrypy.HTTPRedirect(url)
def process_page(self, job_id, page_number): tmpl = self.tl.load('retype page.xhtml') params = dict( assignment_id=lf('{job_id}-{page_number}'), submit_url='submit_text', ) return tmpl.generate(**params).render('xhtml')
def get_page_hits(page): search_rs = conn.search_hits(page_size=page_size, page_number=page) if not search_rs.status: raise Exception(lf( 'Error performing search, code:{search_rs.Code}, ' 'message:{search_rs.Message}' )) return search_rs
def error(self, why): tmpl = self.tl.load('simple.xhtml') message = lf( "An error has occurred ({why}). We apologize for the " "inconvenience. Our staff has " "been notified and should be responding. Please try again " "later, or for immediate assistance, contact our support team.") return tmpl.generate(content=message).render('xhtml')
def pay(self, job_id): """ Force payment for a given job. """ job = self.server._get_job_for_id(job_id) job.authorized = True job.register_hits() return lf( '<a href="/status/{job_id}">Payment simulated; click here for status.</a>')
def wake_on_lan(self, addr='00:00:00:00:00:00'): "Wake up a computer on the network" bytes = map(lambda bs: int(bs, 16), addr.split(':')) binary_addr = struct.pack('BBBBBB', *bytes) magic_packet = '\xff' * 6 + binary_addr * 16 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) s.sendto(magic_packet, ('<broadcast>', 9)) s.close() return lf('Woke up the computer at {addr}')
def upload(self, file, class_='MTurkConversionJob'): job_class = getattr(model, class_) server_url = self.construct_url('/process') content_type_map = dictlib.IdentityOverrideMap( { # some people have their browsers incorrectly configured with # mime types, so map these types. 'application/adobe': 'application/pdf', 'application/x-pdf': 'application/pdf', }, ) content_type = content_type_map[six.text_type(file.content_type)] if content_type != 'application/pdf': msg = "Got content other than PDF: {content_type}" cherrypy.log(msg.format(**vars(file)), severity=logging.WARNING) job = job_class(file.file, content_type, server_url, file.filename) job.save_if_new() self.send_notice(lf("A new document was uploaded ({job.id})")) raise cherrypy.HTTPRedirect(lf("status/{job.id}"))
def serve_until_auth(self): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((self.host, self.port)) s.listen(1) while True: self.conn, addr = s.accept() print(lf('Accepted connection from {addr}')) if not self.check_auth_response(self.conn) == 'retry': break
def is_symlink(path): """ Assuming path is a reparse point, determine if it's a symlink. """ path = _patch_path(path) try: return _is_symlink(next(find_files(path))) except WindowsError as orig_error: tmpl = "Error accessing {path}: {orig_error.message}" raise builtins.WindowsError(lf(tmpl))
def quick_brake(): name = dvd.infer_name() title = six.moves.input(lf("Movie title ({name})> ")) or name config.movies_root.isdir() or config.movies_root.makedirs() init_environment() dest = config.movies_root / title + '.mp4' cmd = get_handbrake_cmd() + [ '--main-feature', '-o', dest, ] with no_sleep(): subprocess.Popen(cmd).wait()
def complete_payment(self, job_id, **params): job = self._get_job_for_id(job_id) # TODO: using params, process the payment success = params['success'] == 'true' if success: tmpl = self.tl.load('declined.xhtml') params_mk = genshi.Markup(lf('<!-- {params} -->')) res = tmpl.generate(job=job, params=params_mk) self.send_notice(lf("Payment denied - {job.id}, {params}")) return res.render('xhtml') # TODO: perform any additional validation and save # any details on the job. try: job.register_hits() target = lf('/status/{job_id}') except errors.InsufficientFunds: self.send_notice( lf("insufficient funds registering hits for {job_id}")) target = '/error/our fault' job.save() raise cherrypy.HTTPRedirect(target)
def process( self, hitId, assignmentId, workerId=None, turkSubmitTo=None, **kwargs): """ Fulfill a request of a client who's been sent from AMT. This will be rendered in an iFrame, so don't use the template. """ preview = assignmentId == 'ASSIGNMENT_ID_NOT_AVAILABLE' submit_url = '{turkSubmitTo}/mturk/externalSubmit'.format(**vars()) params = dict( assignment_id=assignmentId, hit_id=hitId, worker_id=workerId, preview=preview, submit_url=submit_url, page_url=lf('/image/{hitId}') if not preview else '/static/Lorem ipsum.pdf', ) cherrypy.log(lf("params are {params}")) tmpl = self.tl.load('retype page.xhtml') return tmpl.generate(**params).render('xhtml')
def handle_command_line(): """ Query the NTP server for the current time. """ parser = argparse.ArgumentParser(usage=trim(handle_command_line.__doc__)) parser.add_argument( '-6', '--ipv6', help="Force IPv6", action="store_true", default=False) parser.add_argument('server', help="IP Address of server to query") jaraco.logging.add_arguments(parser) args = parser.parse_args() jaraco.logging.setup(args) logging.root.handlers[0].setFormatter(logging.Formatter("%(message)s")) val = query(args.server, args.ipv6) dt = datetime.datetime.fromtimestamp(val) # noqa log.info(lf('\tTime={dt}'))
def get_titles(root): title_durations() title_no = eval(six.moves.input('enter starting DVD title number> ')) visible_files = sorted(file for file in root.files() if not file.is_hidden()) if visible_files: last_file = visible_files[-1].basename() print('last file is', last_file) last_episode = int(re.match('\d+', last_file).group(0)) + 1 else: last_episode = 1 prompt = lf('enter starting episode [{last_episode}]> ') episode = eval(six.moves.input(prompt) or 'None') or last_episode ext = '.mp4' while True: title = six.moves.input('enter title> ') if not title: return yield TitleInfo(title_no, title, episode, root, ext) title_no += 1 episode += 1
def doQuery(self, query, name): headers = { "Content-type": "application/x-ofx", "Accept": "*/*, application/x-ofx", } resp = self.session.post( url=self.config["url"], data=query.encode('cp1252'), headers=headers, ) handle_response(resp) content_type = resp.headers['Content-type'] expected_types = 'application/x-ofx', 'application/qfx' if content_type not in expected_types: log.warning(lf('Unexpected content type {content_type}')) with open(name, "wb") as outfile: outfile.write(resp.content)
def _tag(tag, *contents): start_tag = lf('<{tag}>') end_tag = lf('</{tag}>') lines = itertools.chain([start_tag], contents, [end_tag]) return '\r\n'.join(lines)
def _get_password(site, username): password = keyring.get_password(site, username) if password is None: password = getpass.getpass(lf("Password for {site}:{username}: ")) keyring.set_password(site, username, password) return password
def _field(tag, value): return lf('<{tag}>{value}')
def install_service(install_root=install_root): sudo(lf('mkdir -p {install_root}')) files.upload_template("ubuntu/jaraco.site.service", "/etc/systemd/system", use_sudo=True, context=vars()) sudo('systemctl enable jaraco.site')
def _report(self): yield lf('hit {self.id} ({self.status})') for assignment in self.load_assignments(): yield indent(str(assignment))
def _report(self): yield lf('Job {self.id}') for hit in self.hits: for line in hit._report(): yield indent(line)
def install_upstart_conf(install_root=install_root): sudo(lf('mkdir -p "{install_root}"')) conf = gf("ubuntu/{proc_name}.service") files.upload_template(conf, "/etc/systemd/system", use_sudo=True, context=vars()) sudo(gf('systemctl enable {proc_name}'))