def parse_args(): """Parse command-line arguments.""" parser = OptionParser() parser.add_option('-l', '--linelen', default=False, action="store_true", dest='line_lengths', help='Check line lengths') parser.add_option('-p', '--parser', default=None, dest='parser', help='path to Markdown parser') parser.add_option('-r', '--references', default=None, dest='reference_path', help='path to Markdown file of external references') parser.add_option('-s', '--source', default=os.curdir, dest='source_dir', help='source directory') parser.add_option('-w', '--whitespace', default=False, action="store_true", dest='trailing_whitespace', help='Check for trailing whitespace') args, extras = parser.parse_args() require(args.parser is not None, 'Path to Markdown parser not provided') require(not extras, 'Unexpected trailing command-line arguments "{0}"'.format(extras)) return args
def read_references(reporter, ref_path): """Read shared file of reference links, returning dictionary of valid references {symbolic_name : URL} """ result = {} urls_seen = set() if ref_path: with open(ref_path, 'r') as reader: for (num, line) in enumerate(reader): line_num = num + 1 m = P_INTERNAL_LINK_DEF.search(line) require(m, '{0}:{1} not valid reference:\n{2}'.format(ref_path, line_num, line.rstrip())) name = m.group(1) url = m.group(2) require(name, 'Empty reference at {0}:{1}'.format(ref_path, line_num)) reporter.check(name not in result, ref_path, 'Duplicate reference {0} at line {1}', name, line_num) reporter.check(url not in urls_seen, ref_path, 'Duplicate definition of URL {0} at line {1}', url, line_num) result[name] = url urls_seen.add(url) return result
def controlMessageHandler(self, socket, msg): if msg.type == "get-lists": for list in self.lists: if not list.entitled(socket.username): continue self.control.sendTo( socket, {"type": "list-added", "name": list.name, "id": list.id}) elif msg.type == "create": require(msg, "name") self.createList(msg.name, None, socket.username) self.dirty = True elif msg.type == "rename": require(msg, "id") require(msg, "name") self.byId[msg.id].name = msg.name self.control.broadcast({"type": "list-rename", "id": msg.id, "name": msg.name}) self.dirty = True elif msg.type == "delete": require(msg, "id") self.deleteList(msg.id) self.dirty = True
def let(*args): """style like (let ((a 1) (b a)) b) is not supported""" args = list(args) x = [_let] + args require(x, len(args)>1) bindings, body = args[0], args[1:] require(x, all(isa(b, list) and len(b)==2 and isa(b[0], Symbol) for b in bindings), "illegal binding list") vars, vals = zip(*bindings) return [[_lambda, list(vars)]+map(expand, body)] + map(expand, vals)
def get_labels(repo_url): """ Get actual labels from repository. """ m = P_REPO_URL.match(repo_url) require( m, 'repository URL {0} does not match expected pattern'.format(repo_url)) username = m.group(1) require(username, 'empty username in repository URL {0}'.format(repo_url)) project_name = m.group(2) require( username, 'empty project name in repository URL {0}'.format(repo_url)) url = F_API_URL.format(username, project_name) r = requests.get(url) require(r.status_code == 200, 'Request for {0} failed with {1}'.format(url, r.status_code)) result = {} for entry in r.json(): result[entry['name']] = entry['color'] return result
def check_reference_inclusion(self): """Check that links file has been included.""" if not self.args.reference_path: return for (i, last_line, line_len) in reversed(self.lines): if last_line: break require(last_line, 'No non-empty lines in {0}'.format(self.filename)) include_filename = os.path.split(self.args.reference_path)[-1] if include_filename not in last_line: self.reporter.add(self.filename, 'episode does not include "{0}"', include_filename)
def parse_args(): """ Parse command-line arguments. """ parser = ArgumentParser(description="""Check repository settings.""") parser.add_argument('-r', '--repo', default=None, dest='repo_url', help='repository URL') parser.add_argument('-s', '--source', default=os.curdir, dest='source_dir', help='source directory') args, extras = parser.parse_known_args() require(not extras, 'Unexpected trailing command-line arguments "{0}"'.format(extras)) return args
def onMessage(self, socket, msg): require(msg, "index") if msg.type == "insert": require(msg, "attrs") self.items.insert(msg.index, msg.attrs) self.channel.broadcast( {"type": "insert", "index": msg.index, "attrs": msg.attrs}) self.dirty = True elif msg.type == "delete": del self.items[msg.index] self.channel.broadcast( {"type": "delete", "index": msg.index}) self.dirty = True elif msg.type == "update": require(msg, "attrs") item = self.items[msg.index] item.update(msg.attrs) self.channel.broadcast( {"type": "update", "index": msg.index, "attrs": msg.attrs}) self.dirty = True
def get_repo_url(repo_url): """ Figure out which repository to query. """ # Explicitly specified. if repo_url is not None: return repo_url # Guess. cmd = 'git remote -v' p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, close_fds=True, universal_newlines=True) stdout_data, stderr_data = p.communicate() stdout_data = stdout_data.split('\n') matches = [P_GIT_REMOTE.match(line) for line in stdout_data] matches = [m for m in matches if m is not None] require(len(matches) == 1, 'Unexpected output from git remote command: "{0}"'.format(matches)) username = matches[0].group(1) require( username, 'empty username in git remote output {0}'.format(matches[0])) project_name = matches[0].group(2) require( username, 'empty project name in git remote output {0}'.format(matches[0])) url = F_REPO_URL.format(username, project_name) return url
def parse_args(): """Parse command-line arguments.""" parser = ArgumentParser(description="""Check episode files in a lesson.""") parser.add_argument('-l', '--linelen', default=False, action="store_true", dest='line_lengths', help='Check line lengths') parser.add_argument('-p', '--parser', default=None, dest='parser', help='path to Markdown parser') parser.add_argument('-r', '--references', default=None, dest='reference_path', help='path to Markdown file of external references') parser.add_argument('-s', '--source', default=os.curdir, dest='source_dir', help='source directory') parser.add_argument('-w', '--whitespace', default=False, action="store_true", dest='trailing_whitespace', help='Check for trailing whitespace') parser.add_argument('--permissive', default=False, action="store_true", dest='permissive', help='Do not raise an error even if issues are detected') args, extras = parser.parse_known_args() require(args.parser is not None, 'Path to Markdown parser not provided') require(not extras, 'Unexpected trailing command-line arguments "{0}"'.format(extras)) return args
def validate(self, request): self.grant_type = require( request.form, 'grant_type', TokenRequestError('invalid_request', 'grant_type parameter is missing')) if self.unsupported(self.grant_type): raise TokenRequestError('invalid_request', 'grant_type not supported') grant = handlers[self.grant_type] principal, client = grant(self.client_store).validate(request) return principal, client
def read_entry(): """ READ a unit of DATA. """ current = state.basic_state.bytecode.tell() state.basic_state.bytecode.seek(state.basic_state.data_pos) if util.peek(state.basic_state.bytecode) in tk.end_statement: # initialise - find first DATA util.skip_to(state.basic_state.bytecode, ('\x84', )) # DATA if state.basic_state.bytecode.read(1) not in ('\x84', ','): raise error.RunError(error.OUT_OF_DATA) vals, word, literal = '', '', False while True: # read next char; omit leading whitespace if not literal and vals == '': c = util.skip_white(state.basic_state.bytecode) else: c = util.peek(state.basic_state.bytecode) # parse char if c == '' or (not literal and c == ',') or ( c in tk.end_line or (not literal and c in tk.end_statement)): break elif c == '"': state.basic_state.bytecode.read(1) literal = not literal if not literal: util.require(state.basic_state.bytecode, tk.end_statement + (',', )) else: state.basic_state.bytecode.read(1) if literal: vals += c else: word += c # omit trailing whitespace if c not in tk.whitespace: vals += word word = '' state.basic_state.data_pos = state.basic_state.bytecode.tell() state.basic_state.bytecode.seek(current) return vals
def parse_args(): """ Parse command-line arguments. """ parser = OptionParser() parser.add_option('-r', '--repo', default=None, dest='repo_url', help='repository URL') parser.add_option('-s', '--source', default=os.curdir, dest='source_dir', help='source directory') args, extras = parser.parse_args() require(not extras, 'Unexpected trailing command-line arguments "{0}"'.format(extras)) return args
def read_entry(): """ READ a unit of DATA. """ current = state.basic_state.bytecode.tell() state.basic_state.bytecode.seek(state.basic_state.data_pos) if util.peek(state.basic_state.bytecode) in util.end_statement: # initialise - find first DATA util.skip_to(state.basic_state.bytecode, ('\x84',)) # DATA if state.basic_state.bytecode.read(1) not in ('\x84', ','): # out of DATA raise error.RunError(4) vals, word, literal = '', '', False while True: # read next char; omit leading whitespace if not literal and vals == '': c = util.skip_white(state.basic_state.bytecode) else: c = util.peek(state.basic_state.bytecode) # parse char if c == '' or (not literal and c == ',') or (c in util.end_line or (not literal and c in util.end_statement)): break elif c == '"': state.basic_state.bytecode.read(1) literal = not literal if not literal: util.require(state.basic_state.bytecode, util.end_statement+(',',)) else: state.basic_state.bytecode.read(1) if literal: vals += c else: word += c # omit trailing whitespace if c not in util.whitespace: vals += word word = '' state.basic_state.data_pos = state.basic_state.bytecode.tell() state.basic_state.bytecode.seek(current) return vals
def verify_refresh_token(self, client_store, request): refresh_token = require( request.form, 'refresh_token', GrantError('invalid_request', 'refresh_token is missing')) user_info = refresh_token_store.get(refresh_token) if not user_info: raise GrantError('invalid_grant', 'unknown refresh token') client_id = user_info['client_id'] client = client_store.get(client_id) if not client: raise GrantError('invalid_request', 'unknown client') self.verify_client_credentials(client, request) return user_info, client
def expand_quasiquote(x): """Expand `x => 'x; `,x => x; `(,@x y) => (append x y) quotes = {"'":_quote, "`":_quasiquote, ",":_unquote, ",@":_unquotesplicing} """ if not is_pair(x): return [_quote, x] require(x, x[0] is not _unquotesplicing, "can't splice here") if x[0] is _unquote: require(x, len(x)==2) return x[1] elif is_pair(x[0]) and x[0][0] is _unquotesplicing: require(x[0], len(x[0])==2) return [_append, x[0][1], expand_quasiquote(x[1:])] else: return [_cons, expand_quasiquote(x[0]), expand_quasiquote(x[1:])]
def get_repo_url(repo_url): """ Figure out which repository to query. """ # Explicitly specified. if repo_url is not None: return repo_url # Guess. cmd = 'git remote -v' p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, close_fds=True, universal_newlines=True, encoding='utf-8') stdout_data, stderr_data = p.communicate() stdout_data = stdout_data.split('\n') matches = [P_GIT_REMOTE.match(line) for line in stdout_data] matches = [m for m in matches if m is not None] require( len(matches) == 1, 'Unexpected output from git remote command: "{0}"'.format(matches)) username = matches[0].group(1) require(username, 'empty username in git remote output {0}'.format(matches[0])) project_name = matches[0].group(2) require(username, 'empty project name in git remote output {0}'.format(matches[0])) url = F_REPO_URL.format(username, project_name) return url
def expand(x, toplevel=False): "Walk tree of x, making optimizations/fixes, and signaling SyntaxError." require(x, x!=[]) # () => Error if not isa(x, list): # constant => unchanged return x elif x[0] is _quote: # (quote exp) require(x, len(x)==2) return x elif x[0] is _if: if len(x)==3: x = x + [None] # (if t c) => (if t c None) require(x, len(x)==4) return map(expand, x) elif x[0] is _set: require(x, len(x)==3); var = x[1] # (set! non-var exp) => Error require(x, isa(var, Symbol), "can set! only a symbol") return [_set, var, expand(x[2])] elif x[0] is _define or \ x[0] is _definemacro or \ x[0] is _defineactor: require(x, len(x)>=3) _def, v, body = x[0], x[1], x[2:] if isa(v, list) and v: # (define (f args) body) f, args = v[0], v[1:] # => (define f (lambda (args) body)) return expand([_def, f, [_lambda, args]+body]) else: require(x, len(x)==3) # (define non-var/list exp) => Error require(x, isa(v, Symbol), "can define only a symbol") exp = expand(x[2]) if _def is _definemacro: require(x, toplevel, "define-macro only allowed at top level") proc = eval(exp) require(x, callable(proc), "macro must be a procedure") macro_table[v] = proc # (define-macro v proc) return None # => None; add v:proc to macro_table return [_define, v, exp] elif x[0] is _begin: if len(x)==1: return None # (begin) => None else: return [expand(xi, toplevel) for xi in x] elif x[0] is _lambda: # (lambda (x) e1 e2) require(x, len(x)>=3) # => (lambda (x) (begin e1 e2)) vars, body = x[1], x[2:] require(x, (isa(vars, list) and all(isa(v, Symbol) for v in vars)) or isa(vars, Symbol), "illegal lambda argument list") exp = body[0] if len(body) == 1 else [_begin] + body return [_lambda, vars, expand(exp)] elif x[0] is _quasiquote: # `x => expand_quasiquote(x) require(x, len(x)==2) return expand_quasiquote(x[1]) elif isa(x[0], Symbol) and x[0] in macro_table: return expand(macro_table[x[0]](*x[1:]), toplevel) # (m arg...) elif x[0] is _spawn: require(x, len(x)>=2) return [_spawn] + map(expand, x[1:]) elif x[0] is _join: require(x, len(x)>=2) return [_join] + map(expand, x[1:]) elif x[0] is _value: require(x, len(x)==2) return [_value] + map(expand, x[1:]) elif x[0] is _spawnactor: require(x, len(x)>=2) return [_spawnactor] + map(expand, x[1:]) elif x[0] is _startactor: require(x, len(x)>=2) return [_startactor] + map(expand, x[1:]) elif x[0] is _joinactor: require(x, len(x)>=2) return [_joinactor] + map(expand, x[1:]) elif x[0] is _send: require(x, len(x)==3) return [_send] + map(expand, x[1:]) elif x[0] is _rcv: require(x, len(x)==1) return x elif x[0] is _makemsg: require(x, len(x)==2) return [_makemsg] + map(expand, x[1:]) elif x[0] is _getinfo: require(x, len(x)==2) return [_getinfo] + map(expand, x[1:]) elif x[0] is _getsender: require(x, len(x)==2) return [_getsender] + map(expand, x[1:]) else: # => macroexpand if m isa macro return map(expand, x) # (f arg...) => expand each