text
stringlengths
81
112k
Return a new value to mutate a field with. Do not modify the field directly in this function. Override the ``mutate()`` function if that is needed (the field is only passed into this function as a reference). :field: The pfp.fields.Field instance that will receive the new value. Passed in for r...
generate a new user in the database, still session based so we create a new identifier. def generate_subid(self, token=None, return_user=False): '''generate a new user in the database, still session based so we create a new identifier. ''' from expfactory.database.models import Participan...
print a relational database user def print_user(self, user): '''print a relational database user ''' status = "active" token = user.token if token in ['finished', 'revoked']: status = token if token is None: token = '' subid = "%s\t%s[%s]" %(user.id, token, status) ...
list users, each having a model in the database. A headless experiment will use protected tokens, and interactive will be based on auto- incremented ids. def list_users(self, user=None): '''list users, each having a model in the database. A headless experiment will use protected tokens, and in...
generate a new user in the database, still session based so we create a new identifier. This function is called from the users new entrypoint, and it assumes we want a user generated with a token. def generate_user(self): '''generate a new user in the database, still session based so we creat...
finish user will remove a user's token, making the user entry not accesible if running in headless model def finish_user(self, subid): '''finish user will remove a user's token, making the user entry not accesible if running in headless model''' p = self.revoke_token(subid) p.token =...
restart a user, which means revoking and issuing a new token. def restart_user(self, subid): '''restart a user, which means revoking and issuing a new token.''' p = self.revoke_token(subid) p = self.refresh_token(subid) return p
retrieve a subject based on a token. Valid means we return a participant invalid means we return None def validate_token(self, token): '''retrieve a subject based on a token. Valid means we return a participant invalid means we return None ''' from expfactory.database.models import Participan...
revoke a token by removing it. Is done at finish, and also available as a command line option def revoke_token(self, subid): '''revoke a token by removing it. Is done at finish, and also available as a command line option''' from expfactory.database.models import Participant p = Participant.query.f...
refresh or generate a new token for a user def refresh_token(self, subid): '''refresh or generate a new token for a user''' from expfactory.database.models import Participant p = Participant.query.filter(Participant.id == subid).first() if p is not None: p.token = str(uuid.uuid4()) self.ses...
save data will obtain the current subid from the session, and save it depending on the database type. Currently we just support flat files def save_data(self,session, exp_id, content): '''save data will obtain the current subid from the session, and save it depending on the database type. Currently w...
initialize the database, with the default database path or custom with a format corresponding to the database type: Examples: sqlite:////scif/data/expfactory.db def init_db(self): '''initialize the database, with the default database path or custom with a format corresponding to the d...
Super non-standard stuff here. Dynamically changing the base class using the scope and the lazy name when the class is instantiated. This works as long as the original base class is not directly inheriting from object (which we're not, since our original base class is fields.Field). def LazyField(looku...
Wrap the creation of the type so that we can provide a null-stream to initialize it def _wrap_type_instantiation(self, type_cls): """Wrap the creation of the type so that we can provide a null-stream to initialize it""" def wrapper(*args, **kwargs): # use args for struct arg...
Return the current scope level def level(self): """Return the current scope level """ res = len(self._scope_stack) if self._parent is not None: res += self._parent.level() return res
Create a new scope :returns: TODO def push(self, new_scope=None): """Create a new scope :returns: TODO """ if new_scope is None: new_scope = { "types": {}, "vars": {} } self._curr_scope = new_scope self._dl...
Return a new Scope object that has the curr_scope pinned at the current one :returns: A new scope object def clone(self): """Return a new Scope object that has the curr_scope pinned at the current one :returns: A new scope object """ self._dlog("cloning the stack...
Leave the current scope :returns: TODO def pop(self): """Leave the current scope :returns: TODO """ res = self._scope_stack.pop() self._dlog("popping scope, scope level = {}".format(self.level())) self._curr_scope = self._scope_stack[-1] return res
Add a var to the current scope (vars are fields that parse the input stream) :field_name: TODO :field: TODO :returns: TODO def add_var(self, field_name, field, root=False): """Add a var to the current scope (vars are fields that parse the input stream) :field_n...
Return the first var of name ``name`` in the current scope stack (remember, vars are the ones that parse the input stream) :name: The name of the id :recurse: Whether parent scopes should also be searched (defaults to True) :returns: TODO def get_var(self, name, recurse=True): ...
Add a local variable in the current scope :field_name: The field's name :field: The field :returns: None def add_local(self, field_name, field): """Add a local variable in the current scope :field_name: The field's name :field: The field :returns: None ...
Get the local field (search for it) from the scope stack. An alias for ``get_var`` :name: The name of the local field def get_local(self, name, recurse=True): """Get the local field (search for it) from the scope stack. An alias for ``get_var`` :name: The name of the local fie...
Store the node with the name. When it is instantiated, the node itself will be handled. :name: name of the typedefd struct/union :node: the union/struct node :interp: the 010 interpreter def add_type_struct_or_union(self, name, interp, node): """Store the node with the name. Wh...
Record the typedefd name for orig_names. Resolve orig_names to their core names and save those. :new_name: TODO :orig_names: TODO :returns: TODO def add_type(self, new_name, orig_names): """Record the typedefd name for orig_names. Resolve orig_names to their core names ...
Get the names for the typename (created by typedef) :name: The typedef'd name to resolve :returns: An array of resolved names associated with the typedef'd name def get_type(self, name, recurse=True): """Get the names for the typename (created by typedef) :name: The typedef'd name to ...
Get the first id matching ``name``. Will either be a local or a var. :name: TODO :returns: TODO def get_id(self, name, recurse=True): """Get the first id matching ``name``. Will either be a local or a var. :name: TODO :returns: TODO """ self._d...
TODO: Docstring for _resolve_names. :name: TODO :returns: TODO def _resolve_name(self, name): """TODO: Docstring for _resolve_names. :name: TODO :returns: TODO """ res = [name] while True: orig_names = self._search("types", name) ...
Search the scope stack for the name in the specified category (types/locals/vars). :category: the category to search in (locals/types/vars) :name: name to search for :returns: None if not found, the result of the found local/type/id def _search(self, category, name, recurse=True): ...
Add the native python function ``func`` into the pfp interpreter with the name ``name`` and return value ``ret`` so that it can be called from within a template script. .. note:: The :any:`@native <pfp.native.native>` decorator exists to simplify this. All native functions ...
Define the native functions for PFP def define_natives(cls): """Define the native functions for PFP """ if len(cls._natives) > 0: return glob_pattern = os.path.join(os.path.dirname(__file__), "native", "*.py") for filename in glob.glob(glob_pattern): bas...
log the message to the log def _dlog(self, msg, indent_increase=0): """log the message to the log""" self._log.debug("interp", msg, indent_increase, filename=self._orig_filename, coord=self._coord)
Parse the data stream using the template (e.g. parse the 010 template and interpret the template using the stream as the data source). :stream: The input data stream :template: The template to parse the stream with :keep_successful: Return whatever was successfully parsed before an erro...
Eval a single statement (something returnable) def eval(self, statement, ctxt=None): """Eval a single statement (something returnable) """ self._no_debug = True statement = statement.strip() if not statement.endswith(";"): statement += ";" ast = self._pars...
Set if the interpreter should break. :returns: TODO def set_break(self, break_type): """Set if the interpreter should break. :returns: TODO """ self._break_type = break_type self._break_level = self._scope.level()
Return the current line number in the template, as well as the surrounding source lines def get_curr_lines(self): """Return the current line number in the template, as well as the surrounding source lines """ start = max(0, self._coord.line - 5) end = min(len(self._templ...
Set if the bitfield input/output stream should be padded :val: True/False :returns: None def set_bitfield_padded(self, val): """Set if the bitfield input/output stream should be padded :val: True/False :returns: None """ self._padded_bitfield = val self...
Interpret the parsed 010 AST :returns: PfpDom def _run(self, keep_successfull): """Interpret the parsed 010 AST :returns: PfpDom """ # example self._ast.show(): # FileAST: # Decl: data, [], [], [] # TypeDecl: data, [] # S...
Recursively handle nodes in the 010 AST :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO def _handle_node(self, node, scope=None, ctxt=None, stream=None): """Recursively handle nodes in the 010 AST :node: TODO :scope: TODO :ctxt...
TODO: Docstring for _handle_file_ast. :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO def _handle_file_ast(self, node, scope, ctxt, stream): """TODO: Docstring for _handle_file_ast. :node: TODO :scope: TODO :ctxt: TODO ...
Handle cast nodes :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO def _handle_cast(self, node, scope, ctxt, stream): """Handle cast nodes :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO ...
TODO: Docstring for _handle_typename :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO def _handle_typename(self, node, scope, ctxt, stream): """TODO: Docstring for _handle_typename :node: TODO :scope: TODO :ctxt: TODO :s...
Get the name of the node - check for node.name and node.type.declname. Not sure why the second one occurs exactly - it happens with declaring a new struct field with parameters def _get_node_name(self, node): """Get the name of the node - check for node.name and node.type.declna...
TODO: Docstring for _handle_decl. :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO def _handle_decl(self, node, scope, ctxt, stream): """TODO: Docstring for _handle_decl. :node: TODO :scope: TODO :ctxt: TODO :stream: TOD...
Handle metadata for the node def _handle_metadata(self, node, scope, ctxt, stream): """Handle metadata for the node """ self._dlog("handling node metadata {}".format(node.metadata.keyvals)) keyvals = node.metadata.keyvals metadata_info = [] if "watch" in node.metadata...
Handle watch vars for fields def _handle_watch_metadata(self, node, scope, ctxt, stream): """Handle watch vars for fields """ keyvals = node.metadata.keyvals if "watch" not in keyvals: raise errors.PfpError("Packed fields require a packer function set") if "update" n...
Handle packed metadata def _handle_packed_metadata(self, node, scope, ctxt, stream): """Handle packed metadata """ keyvals = node.metadata.keyvals if "packer" not in keyvals and ("pack" not in keyvals or "unpack" not in keyvals): raise errors.PfpError("Packed fields require ...
TODO: Docstring for _handle_byref_decl. :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO def _handle_byref_decl(self, node, scope, ctxt, stream): """TODO: Docstring for _handle_byref_decl. :node: TODO :scope: TODO :ctxt: TODO ...
TODO: Docstring for _handle_type_decl. :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO def _handle_type_decl(self, node, scope, ctxt, stream): """TODO: Docstring for _handle_type_decl. :node: TODO :scope: TODO :ctxt: TODO ...
TODO: Docstring for _handle_struct_ref. :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO def _handle_struct_ref(self, node, scope, ctxt, stream): """TODO: Docstring for _handle_struct_ref. :node: TODO :scope: TODO :ctxt: TODO ...
TODO: Docstring for _handle_union. :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO def _handle_union(self, node, scope, ctxt, stream): """TODO: Docstring for _handle_union. :node: TODO :scope: TODO :ctxt: TODO :stream: ...
Handle InitList nodes (e.g. when initializing a struct) :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO def _handle_init_list(self, node, scope, ctxt, stream): """Handle InitList nodes (e.g. when initializing a struct) :node: TODO :sco...
TODO: Docstring for _handle_struct_call_type_decl. :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO def _handle_struct_call_type_decl(self, node, scope, ctxt, stream): """TODO: Docstring for _handle_struct_call_type_decl. :node: TODO :s...
TODO: Docstring for _handle_struct. :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO def _handle_struct(self, node, scope, ctxt, stream): """TODO: Docstring for _handle_struct. :node: TODO :scope: TODO :ctxt: TODO :strea...
TODO: Docstring for _handle_identifier_type. :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO def _handle_identifier_type(self, node, scope, ctxt, stream): """TODO: Docstring for _handle_identifier_type. :node: TODO :scope: TODO ...
TODO: Docstring for _handle_typedef. :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO def _handle_typedef(self, node, scope, ctxt, stream): """TODO: Docstring for _handle_typedef. :node: TODO :scope: TODO :ctxt: TODO :st...
Check for the hex def _str_to_int(self, string): """Check for the hex """ string = string.lower() if string.endswith("l"): string = string[:-1] if string.lower().startswith("0x"): # should always match match = re.match(r'0[xX]([a-fA-F0-9]+)', ...
TODO: Docstring for _handle_constant. :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO def _handle_constant(self, node, scope, ctxt, stream): """TODO: Docstring for _handle_constant. :node: TODO :scope: TODO :ctxt: TODO ...
TODO: Docstring for _handle_binary_op. :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO def _handle_binary_op(self, node, scope, ctxt, stream): """TODO: Docstring for _handle_binary_op. :node: TODO :scope: TODO :ctxt: TODO ...
TODO: Docstring for _handle_unary_op. :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO def _handle_unary_op(self, node, scope, ctxt, stream): """TODO: Docstring for _handle_unary_op. :node: TODO :scope: TODO :ctxt: TODO ...
Handle the parentof unary operator :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO def _handle_parentof(self, node, scope, ctxt, stream): """Handle the parentof unary operator :node: TODO :scope: TODO :ctxt: TODO :strea...
Handle the exists unary operator :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO def _handle_exists(self, node, scope, ctxt, stream): """Handle the exists unary operator :node: TODO :scope: TODO :ctxt: TODO :stream: TOD...
Handle the function_exists unary operator :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO def _handle_function_exists(self, node, scope, ctxt, stream): """Handle the function_exists unary operator :node: TODO :scope: TODO :ctxt...
Handle an ID node (return a field object for the ID) :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO def _handle_id(self, node, scope, ctxt, stream): """Handle an ID node (return a field object for the ID) :node: TODO :scope: TODO ...
Handle assignment nodes :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO def _handle_assignment(self, node, scope, ctxt, stream): """Handle assignment nodes :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :ret...
Handle FuncDef nodes :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO def _handle_func_def(self, node, scope, ctxt, stream): """Handle FuncDef nodes :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TO...
Handle ParamList nodes :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO def _handle_param_list(self, node, scope, ctxt, stream): """Handle ParamList nodes :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :retur...
Handle FuncDecl nodes :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO def _handle_func_decl(self, node, scope, ctxt, stream): """Handle FuncDecl nodes :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns:...
Handle FuncCall nodes :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO def _handle_func_call(self, node, scope, ctxt, stream): """Handle FuncCall nodes :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns:...
Handle ExprList nodes :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO def _handle_expr_list(self, node, scope, ctxt, stream): """Handle ExprList nodes :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns:...
Handle Compound nodes :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO def _handle_compound(self, node, scope, ctxt, stream): """Handle Compound nodes :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: ...
Handle Return nodes :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO def _handle_return(self, node, scope, ctxt, stream): """Handle Return nodes :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO ...
Handle enum nodes :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO def _handle_enum(self, node, scope, ctxt, stream): """Handle enum nodes :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO ...
Handle ArrayDecl nodes :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO def _handle_array_decl(self, node, scope, ctxt, stream): """Handle ArrayDecl nodes :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :retur...
Handle ArrayRef nodes :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO def _handle_array_ref(self, node, scope, ctxt, stream): """Handle ArrayRef nodes :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns:...
Handle If nodes :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO def _handle_if(self, node, scope, ctxt, stream): """Handle If nodes :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO """ ...
Handle For nodes :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO def _handle_for(self, node, scope, ctxt, stream): """Handle For nodes :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO "...
Handle break node :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO def _handle_while(self, node, scope, ctxt, stream): """Handle break node :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO ...
Handle break node :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO def _handle_switch(self, node, scope, ctxt, stream): """Handle break node :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO ...
Handle break node :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO def _handle_break(self, node, scope, ctxt, stream): """Handle break node :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO ...
Handle continue node :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO def _handle_continue(self, node, scope, ctxt, stream): """Handle continue node :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TO...
Handle For nodes :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO def _handle_decl_list(self, node, scope, ctxt, stream): """Handle For nodes :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO ...
TODO: Docstring for _create_scope. :returns: TODO def _create_scope(self): """TODO: Docstring for _create_scope. :returns: TODO """ res = Scope(self._log) for func_name,native_func in six.iteritems(self._natives): res.add_local(func_name, native_func) ...
Return the value of the node. It is expected to be either an AST.ID instance or a constant :node: TODO :returns: TODO def _get_value(self, node, scope, ctxt, stream): """Return the value of the node. It is expected to be either an AST.ID instance or a constant :node: T...
Resolve the names to a class in fields.py, resolving past typedefs, etc :names: TODO :scope: TODO :ctxt: TODO :returns: TODO def _resolve_to_field_class(self, names, scope): """Resolve the names to a class in fields.py, resolving past typedefs, etc :nam...
Convert the bit list into bytes. (Assumes bits is a list whose length is a multiple of 8) def bits_to_bytes(bits): """Convert the bit list into bytes. (Assumes bits is a list whose length is a multiple of 8) """ if len(bits) % 8 != 0: raise Exception("num bits must be multiple of 8") r...
Convert bytes to a list of bits def bytes_to_bits(bytes_): """Convert bytes to a list of bits """ res = [] for x in bytes_: if not isinstance(x, int): x = ord(x) res += byte_to_bits(x) return res
Return if the stream has reached EOF or not without discarding any unflushed bits :returns: True/False def is_eof(self): """Return if the stream has reached EOF or not without discarding any unflushed bits :returns: True/False """ pos = self._stream.tell() ...
Close the stream def close(self): """Close the stream """ self.closed = True self._flush_bits_to_stream() self._stream.close()
Read ``num`` number of bytes from the stream. Note that this will automatically resets/ends the current bit-reading if it does not end on an even byte AND ``self.padded`` is True. If ``self.padded`` is True, then the entire stream is treated as a bitstream. :num: number of bytes to read...
Read ``num`` number of bits from the stream :num: number of bits to read :returns: a list of ``num`` bits, or an empty list if EOF has been reached def read_bits(self, num): """Read ``num`` number of bits from the stream :num: number of bits to read :returns: a list of ``num``...
Write data to the stream :data: the data to write to the stream :returns: None def write(self, data): """Write data to the stream :data: the data to write to the stream :returns: None """ if self.padded: # flush out any remaining bits first ...
Write the bits to the stream. Add the bits to the existing unflushed bits and write complete bytes to the stream. def write_bits(self, bits): """Write the bits to the stream. Add the bits to the existing unflushed bits and write complete bytes to the stream. """ ...
Return the current position in the stream (ignoring bit position) :returns: int for the position in the stream def tell(self): """Return the current position in the stream (ignoring bit position) :returns: int for the position in the stream """ res = self._stre...
Seek to the specified position in the stream with seek_type. Unflushed bits will be discarded in the case of a seek. The stream will also keep track of which bytes have and have not been consumed so that the dom will capture all of the bytes in the stream. :pos: offset ...
Return the size of the stream, or -1 if it cannot be determined. def size(self): """Return the size of the stream, or -1 if it cannot be determined. """ pos = self._stream.tell() # seek to the end of the stream self._stream.seek(0,2) size = self._stream.t...
Return an IntervalTree of unconsumed ranges, of the format (start, end] with the end value not being included def unconsumed_ranges(self): """Return an IntervalTree of unconsumed ranges, of the format (start, end] with the end value not being included """ res = IntervalTree() ...
Update the ``self.consumed_ranges`` array with which byte ranges have been consecutively consumed. def _update_consumed_ranges(self, start_pos, end_pos): """Update the ``self.consumed_ranges`` array with which byte ranges have been consecutively consumed. """ self.range_set.add(...
Flush the bits to the stream. This is used when a few bits have been read and ``self._bits`` contains unconsumed/ flushed bits when data is to be written to the stream def _flush_bits_to_stream(self): """Flush the bits to the stream. This is used when a few bits have been read and ``sel...
ensure that fields are present in markdown file def _validate_markdown(self, expfile): '''ensure that fields are present in markdown file''' try: import yaml except: bot.error('Python yaml is required for testing yml/markdown files.') sys.exit(1) se...
return all checks for required variables before returning to desired view Parameters ========== template: the html template to render do_redirect: if True, perform a redirect and not render context: dictionary of context variables to pass to render_template next: a pre...