INSTRUCTION
stringlengths
1
8.43k
RESPONSE
stringlengths
75
104k
Packs a sub field s array into another array using a mask
def pack(array, sub_field_array, mask, inplace=False): """ Packs a sub field's array into another array using a mask Parameters: ---------- array : numpy.ndarray The array in which the sub field array will be packed into array_in : numpy.ndarray sub field array to pack mask : ma...
Returns a list of the names of the dimensions that will be lost when converting from point_fmt_in to point_fmt_out
def lost_dimensions(point_fmt_in, point_fmt_out): """ Returns a list of the names of the dimensions that will be lost when converting from point_fmt_in to point_fmt_out """ unpacked_dims_in = PointFormat(point_fmt_in).dtype unpacked_dims_out = PointFormat(point_fmt_out).dtype out_dims = unpac...
Returns the numpy. dtype used to store the point records in a numpy array
def dtype(self): """ Returns the numpy.dtype used to store the point records in a numpy array .. note:: The dtype corresponds to the dtype with sub_fields *packed* into their composed fields """ dtype = self._access_dict(dims.ALL_POINT_FORMATS_DTYPE, self.id) ...
Returns the numpy. dtype used to store the point records in a numpy array
def unpacked_dtype(self): """ Returns the numpy.dtype used to store the point records in a numpy array .. note:: The dtype corresponds to the dtype with sub_fields *unpacked* """ dtype = self._access_dict(dims.UNPACKED_POINT_FORMATS_DTYPES, self.id) dtype = self._d...
Returns a dict of the sub fields for this point format
def sub_fields(self): """ Returns a dict of the sub fields for this point format Returns ------- Dict[str, Tuple[str, SubField]] maps a sub field name to its composed dimension with additional information """ sub_fields_dict = {} for composed_dim_nam...
Returns the number of extra bytes
def num_extra_bytes(self): """ Returns the number of extra bytes """ return sum(np.dtype(extra_dim[1]).itemsize for extra_dim in self.extra_dims)
Returns True if the point format has waveform packet dimensions
def has_waveform_packet(self): """ Returns True if the point format has waveform packet dimensions """ dimensions = set(self.dimension_names) return all(name in dimensions for name in dims.WAVEFORM_FIELDS_NAMES)
Console script for satel_integra.
def main(port, ip, command, loglevel): """Console script for satel_integra.""" numeric_level = getattr(logging, loglevel.upper(), None) if not isinstance(numeric_level, int): raise ValueError('Invalid log level: %s' % loglevel) logging.basicConfig(level=numeric_level) click.echo("D...
Function to calculate checksum as per Satel manual.
def checksum(command): """Function to calculate checksum as per Satel manual.""" crc = 0x147A for b in command: # rotate (crc 1 bit left) crc = ((crc << 1) & 0xFFFF) | (crc & 0x8000) >> 15 crc = crc ^ 0xFFFF crc = (crc + (crc >> 8) + b) & 0xFFFF return crc
Debugging method to print out frames in hex.
def print_hex(data): """Debugging method to print out frames in hex.""" hex_msg = "" for c in data: hex_msg += "\\x" + format(c, "02x") _LOGGER.debug(hex_msg)
Verify checksum and strip header and footer of received frame.
def verify_and_strip(resp): """Verify checksum and strip header and footer of received frame.""" if resp[0:2] != b'\xFE\xFE': _LOGGER.error("Houston, we got problem:") print_hex(resp) raise Exception("Wrong header - got %X%X" % (resp[0], resp[1])) if resp[-2:] != b'\xFE\x0D': ...
Return list of positions of bits set to one in given data.
def list_set_bits(r, expected_length): """Return list of positions of bits set to one in given data. This method is used to read e.g. violated zones. They are marked by ones on respective bit positions - as per Satel manual. """ set_bit_numbers = [] bit_index = 0x1 assert (len(r) == expecte...
Add header checksum and footer to command data.
def generate_query(command): """Add header, checksum and footer to command data.""" data = bytearray(command) c = checksum(data) data.append(c >> 8) data.append(c & 0xFF) data.replace(b'\xFE', b'\xFE\xF0') data = bytearray.fromhex("FEFE") + data + bytearray.fromhex("FE0D") return data
Basic demo of the monitoring capabilities.
def demo(host, port): """Basic demo of the monitoring capabilities.""" # logging.basicConfig(level=logging.DEBUG) loop = asyncio.get_event_loop() stl = AsyncSatel(host, port, loop, [1, 2, 3, 4, 5, 6, 7, 8, 12, 13, 14, 15, 16, 17, 18, 19, ...
Make a TCP connection to the alarm system.
async def connect(self): """Make a TCP connection to the alarm system.""" _LOGGER.debug("Connecting...") try: self._reader, self._writer = await asyncio.open_connection( self._host, self._port, loop=self._loop) _LOGGER.debug("sucess connecting...") ...
Start monitoring for interesting events.
async def start_monitoring(self): """Start monitoring for interesting events.""" data = generate_query( b'\x7F\x01\xDC\x99\x80\x00\x04\x00\x00\x00\x00\x00\x00') await self._send_data(data) resp = await self._read_data() if resp is None: _LOGGER.warning("...
0x17 outputs state 0x17 + 16/ 32 bytes
def _output_changed(self, msg): """0x17 outputs state 0x17 + 16/32 bytes""" status = {"outputs": {}} output_states = list_set_bits(msg, 32) self.violated_outputs = output_states _LOGGER.debug("Output states: %s, monitored outputs: %s", output_states, s...
Send arming command to the alarm. Modes allowed: from 0 till 3.
async def arm(self, code, partition_list, mode=0): """Send arming command to the alarm. Modes allowed: from 0 till 3.""" _LOGGER.debug("Sending arm command, mode: %s!", mode) while len(code) < 16: code += 'F' code_bytes = bytearray.fromhex(code) mode_command = 0x80 +...
Send command to disarm.
async def disarm(self, code, partition_list): """Send command to disarm.""" _LOGGER.info("Sending disarm command.") while len(code) < 16: code += 'F' code_bytes = bytearray.fromhex(code) data = generate_query(b'\x84' + code_bytes + part...
Send command to clear the alarm.
async def clear_alarm(self, code, partition_list): """Send command to clear the alarm.""" _LOGGER.info("Sending clear the alarm command.") while len(code) < 16: code += 'F' code_bytes = bytearray.fromhex(code) data = generate_query(b'\x85' + code_bytes ...
Send output turn on command to the alarm.
async def set_output(self, code, output_id, state): """Send output turn on command to the alarm.""" """0x88 outputs on + 8 bytes - user code + 16/32 bytes - output list If function is accepted, function result can be checked by observe the system...
A workaround for Satel Integra disconnecting after 25s.
async def keep_alive(self): """A workaround for Satel Integra disconnecting after 25s. Every interval it sends some random question to the device, ignoring answer - just to keep connection alive. """ while True: await asyncio.sleep(self._keep_alive_timeout) ...
Start monitoring of the alarm status.
async def monitor_status(self, alarm_status_callback=None, zone_changed_callback=None, output_changed_callback=None): """Start monitoring of the alarm status. Send command to satel integra to start sending updates. Read in a loop and cal...
Stop monitoring and close connection.
def close(self): """Stop monitoring and close connection.""" _LOGGER.debug("Closing...") self.closed = True if self.connected: self._writer.close()
Clear all matching our user_id.
def purge_db(self): """ Clear all matching our user_id. """ with self.engine.begin() as db: purge_user(db, self.user_id)
Guess the type of a file.
def guess_type(self, path, allow_directory=True): """ Guess the type of a file. If allow_directory is False, don't consider the possibility that the file is a directory. """ if path.endswith('.ipynb'): return 'notebook' elif allow_directory and self.d...
Get the id of a file in the database. This function is specific to this implementation of ContentsManager and is not in the base class.
def get_file_id(self, path): """ Get the id of a file in the database. This function is specific to this implementation of ContentsManager and is not in the base class. """ with self.engine.begin() as db: try: file_id = get_file_id(db, self.user_id, p...
Get a notebook from the database.
def _get_notebook(self, path, content, format): """ Get a notebook from the database. """ with self.engine.begin() as db: try: record = get_file( db, self.user_id, path, content, ...
Build a notebook model from database record.
def _notebook_model_from_db(self, record, content): """ Build a notebook model from database record. """ path = to_api_path(record['parent_name'] + record['name']) model = base_model(path) model['type'] = 'notebook' model['last_modified'] = model['created'] = reco...
Get a directory from the database.
def _get_directory(self, path, content, format): """ Get a directory from the database. """ with self.engine.begin() as db: try: record = get_directory( db, self.user_id, path, content ) except NoSuchDirectory: ...
Apply _notebook_model_from_db or _file_model_from_db to each entry in file_records depending on the result of guess_type.
def _convert_file_records(self, file_records): """ Apply _notebook_model_from_db or _file_model_from_db to each entry in file_records, depending on the result of `guess_type`. """ for record in file_records: type_ = self.guess_type(record['name'], allow_directory=Fals...
Build a directory model from database directory record.
def _directory_model_from_db(self, record, content): """ Build a directory model from database directory record. """ model = base_directory_model(to_api_path(record['name'])) if content: model['format'] = 'json' model['content'] = list( cha...
Build a file model from database record.
def _file_model_from_db(self, record, content, format): """ Build a file model from database record. """ # TODO: Most of this is shared with _notebook_model_from_db. path = to_api_path(record['parent_name'] + record['name']) model = base_model(path) model['type'] ...
Save a notebook.
def _save_notebook(self, db, model, path): """ Save a notebook. Returns a validation message. """ nb_contents = from_dict(model['content']) self.check_and_sign(nb_contents, path) save_file( db, self.user_id, path, w...
Save a non - notebook file.
def _save_file(self, db, model, path): """ Save a non-notebook file. """ save_file( db, self.user_id, path, to_b64(model['content'], model.get('format', None)), self.crypto.encrypt, self.max_file_size_bytes, ...
Rename object from old_path to path.
def rename_file(self, old_path, path): """ Rename object from old_path to path. NOTE: This method is unfortunately named on the base class. It actually moves a file or a directory. """ with self.engine.begin() as db: try: if self.file_exists(...
Delete object corresponding to path.
def delete_file(self, path): """ Delete object corresponding to path. """ if self.file_exists(path): self._delete_non_directory(path) elif self.dir_exists(path): self._delete_directory(path) else: self.no_such_entity(path)
Apply preprocessing steps to file/ notebook content that we re going to write to the database.
def preprocess_incoming_content(content, encrypt_func, max_size_bytes): """ Apply preprocessing steps to file/notebook content that we're going to write to the database. Applies ``encrypt_func`` to ``content`` and checks that the result is smaller than ``max_size_bytes``. """ encrypted = en...
Add a new user if they don t already exist.
def ensure_db_user(db, user_id): """ Add a new user if they don't already exist. """ with ignore_unique_violation(): db.execute( users.insert().values(id=user_id), )
Delete a user and all of their resources.
def purge_user(db, user_id): """ Delete a user and all of their resources. """ db.execute(files.delete().where( files.c.user_id == user_id )) db.execute(directories.delete().where( directories.c.user_id == user_id )) db.execute(users.delete().where( users.c.id == ...
Create a directory.
def create_directory(db, user_id, api_path): """ Create a directory. """ name = from_api_dirname(api_path) if name == '/': parent_name = null() parent_user_id = null() else: # Convert '/foo/bar/buzz/' -> '/foo/bar/' parent_name = name[:name.rindex('/', 0, -1) + 1]...
Return a WHERE clause that matches entries in a directory.
def _is_in_directory(table, user_id, db_dirname): """ Return a WHERE clause that matches entries in a directory. Parameterized on table because this clause is re-used between files and directories. """ return and_( table.c.parent_name == db_dirname, table.c.user_id == user_id, ...
Delete a directory.
def delete_directory(db, user_id, api_path): """ Delete a directory. """ db_dirname = from_api_dirname(api_path) try: result = db.execute( directories.delete().where( and_( directories.c.user_id == user_id, directories.c.nam...
Internal implementation of dir_exists.
def _dir_exists(db, user_id, db_dirname): """ Internal implementation of dir_exists. Expects a db-style path name. """ return db.execute( select( [func.count(directories.c.name)], ).where( and_( directories.c.user_id == user_id, ...
Return files in a directory.
def files_in_directory(db, user_id, db_dirname): """ Return files in a directory. """ fields = _file_default_fields() rows = db.execute( select( fields, ).where( _is_in_directory(files, user_id, db_dirname), ).order_by( files.c.user_id, ...
Return subdirectories of a directory.
def directories_in_directory(db, user_id, db_dirname): """ Return subdirectories of a directory. """ fields = _directory_default_fields() rows = db.execute( select( fields, ).where( _is_in_directory(directories, user_id, db_dirname), ) ) return...
Return the names of all files/ directories that are direct children of api_dirname.
def get_directory(db, user_id, api_dirname, content): """ Return the names of all files/directories that are direct children of api_dirname. If content is False, return a bare model containing just a database-style name. """ db_dirname = from_api_dirname(api_dirname) if not _dir_exists(...
Return a WHERE clause matching the given API path and user_id.
def _file_where(user_id, api_path): """ Return a WHERE clause matching the given API path and user_id. """ directory, name = split_api_filepath(api_path) return and_( files.c.name == name, files.c.user_id == user_id, files.c.parent_name == directory, )
Return a SELECT statement that returns the latest N versions of a file.
def _select_file(user_id, api_path, fields, limit): """ Return a SELECT statement that returns the latest N versions of a file. """ query = select(fields).where( _file_where(user_id, api_path), ).order_by( _file_creation_order(), ) if limit is not None: query = query....
Default fields returned by a file query.
def _file_default_fields(): """ Default fields returned by a file query. """ return [ files.c.name, files.c.created_at, files.c.parent_name, ]
Get file data for the given user_id path and query_fields. The query_fields parameter specifies which database fields should be included in the returned file data.
def _get_file(db, user_id, api_path, query_fields, decrypt_func): """ Get file data for the given user_id, path, and query_fields. The query_fields parameter specifies which database fields should be included in the returned file data. """ result = db.execute( _select_file(user_id, api_...
Get file data for the given user_id and path.
def get_file(db, user_id, api_path, include_content, decrypt_func): """ Get file data for the given user_id and path. Include content only if include_content=True. """ query_fields = _file_default_fields() if include_content: query_fields.append(files.c.content) return _get_file(db...
Get the value in the id column for the file with the given user_id and path.
def get_file_id(db, user_id, api_path): """ Get the value in the 'id' column for the file with the given user_id and path. """ return _get_file( db, user_id, api_path, [files.c.id], unused_decrypt_func, )['id']
Delete a file.
def delete_file(db, user_id, api_path): """ Delete a file. TODO: Consider making this a soft delete. """ result = db.execute( files.delete().where( _file_where(user_id, api_path) ) ) rowcount = result.rowcount if not rowcount: raise NoSuchFile(api_pa...
Check if a file exists.
def file_exists(db, user_id, path): """ Check if a file exists. """ try: get_file( db, user_id, path, include_content=False, decrypt_func=unused_decrypt_func, ) return True except NoSuchFile: return False
Rename a file.
def rename_file(db, user_id, old_api_path, new_api_path): """ Rename a file. """ # Overwriting existing files is disallowed. if file_exists(db, user_id, new_api_path): raise FileExists(new_api_path) old_dir, old_name = split_api_filepath(old_api_path) new_dir, new_name = split_api_...
Rename a directory.
def rename_directory(db, user_id, old_api_path, new_api_path): """ Rename a directory. """ old_db_path = from_api_dirname(old_api_path) new_db_path = from_api_dirname(new_api_path) if old_db_path == '/': raise RenameRoot('Renaming the root directory is not permitted.') # Overwritin...
Save a file.
def save_file(db, user_id, path, content, encrypt_func, max_size_bytes): """ Save a file. TODO: Update-then-insert is probably cheaper than insert-then-update. """ content = preprocess_incoming_content( content, encrypt_func, max_size_bytes, ) directory, name = split...
Create a generator of decrypted files.
def generate_files(engine, crypto_factory, min_dt=None, max_dt=None, logger=None): """ Create a generator of decrypted files. Files are yielded in ascending order of their timestamp. This function selects all current notebooks (optionally, falling within a datetime range), decry...
Delete all database records for the given user_id.
def purge_remote_checkpoints(db, user_id): """ Delete all database records for the given user_id. """ db.execute( remote_checkpoints.delete().where( remote_checkpoints.c.user_id == user_id, ) )
Create a generator of decrypted remote checkpoints.
def generate_checkpoints(engine, crypto_factory, min_dt=None, max_dt=None, logger=None): """ Create a generator of decrypted remote checkpoints. Checkpoints are yielded in ascending order of their timestamp. This function selects all notebook checkpoints (optionally, falling w...
See docstrings for generate_files and generate_checkpoints.
def _generate_notebooks(table, timestamp_column, engine, crypto_factory, min_dt, max_dt, logger): """ See docstrings for `generate_files` and `generate_checkpoints`. Parameters ---------- table : SQLAlchemy.Table Table to fetch notebooks from, `files` or `remote_chec...
Re - encrypt a row from table with id of row_id.
def reencrypt_row_content(db, table, row_id, decrypt_func, encrypt_func, logger): """ Re-encrypt a row from ``table`` with ``id`` of ``row_id``. """ q = (select([table.c.cont...
Get all file ids for a user.
def select_file_ids(db, user_id): """ Get all file ids for a user. """ return list( db.execute( select([files.c.id]) .where(files.c.user_id == user_id) ) )
Get all file ids for a user.
def select_remote_checkpoint_ids(db, user_id): """ Get all file ids for a user. """ return list( db.execute( select([remote_checkpoints.c.id]) .where(remote_checkpoints.c.user_id == user_id) ) )
Re - encrypt all of the files and checkpoints for a single user.
def reencrypt_user_content(engine, user_id, old_decrypt_func, new_encrypt_func, logger): """ Re-encrypt all of the files and checkpoints for a single user. """ logger.info("Begin re-encryption for...
Convert a secret key and a user ID into an encryption key to use with a cryptography. fernet. Fernet.
def derive_single_fernet_key(password, user_id): """ Convert a secret key and a user ID into an encryption key to use with a ``cryptography.fernet.Fernet``. Taken from https://cryptography.io/en/latest/fernet/#using-passwords-with-fernet Parameters ---------- password : unicode ...
Derive a list of per - user Fernet keys from a list of master keys and a username.
def derive_fallback_fernet_keys(passwords, user_id): """ Derive a list of per-user Fernet keys from a list of master keys and a username. If a None is encountered in ``passwords``, it is forwarded. Parameters ---------- passwords : list[unicode] List of ascii-encodable keys to deri...
Create and return a function suitable for passing as a crypto_factory to pgcontents. utils. sync. reencrypt_all_users
def single_password_crypto_factory(password): """ Create and return a function suitable for passing as a crypto_factory to ``pgcontents.utils.sync.reencrypt_all_users`` The factory here returns a ``FernetEncryption`` that uses a key derived from ``password`` and salted with the supplied user_id. ...
Decorator memoizing a single - argument function
def memoize_single_arg(f): """ Decorator memoizing a single-argument function """ memo = {} @wraps(f) def memoized_f(arg): try: return memo[arg] except KeyError: result = memo[arg] = f(arg) return result return memoized_f
Get the name from a column - like SQLAlchemy expression.
def _get_name(column_like): """ Get the name from a column-like SQLAlchemy expression. Works for Columns and Cast expressions. """ if isinstance(column_like, Column): return column_like.name elif isinstance(column_like, Cast): return column_like.clause.name
Convert a SQLAlchemy row that does not contain a content field to a dict.
def to_dict_no_content(fields, row): """ Convert a SQLAlchemy row that does not contain a 'content' field to a dict. If row is None, return None. Raises AssertionError if there is a field named 'content' in ``fields``. """ assert(len(fields) == len(row)) field_names = list(map(_get_name, ...
Convert a SQLAlchemy row that contains a content field to a dict.
def to_dict_with_content(fields, row, decrypt_func): """ Convert a SQLAlchemy row that contains a 'content' field to a dict. ``decrypt_func`` will be applied to the ``content`` field of the row. If row is None, return None. Raises AssertionError if there is no field named 'content' in ``fields``....
Create a checkpoint of the current state of a notebook
def create_notebook_checkpoint(self, nb, path): """Create a checkpoint of the current state of a notebook Returns a checkpoint_id for the new checkpoint. """ b64_content = writes_base64(nb) with self.engine.begin() as db: return save_remote_checkpoint( ...
Create a checkpoint of the current state of a file
def create_file_checkpoint(self, content, format, path): """Create a checkpoint of the current state of a file Returns a checkpoint_id for the new checkpoint. """ try: b64_content = to_b64(content, format) except ValueError as e: self.do_400(str(e)) ...
delete a checkpoint for a file
def delete_checkpoint(self, checkpoint_id, path): """delete a checkpoint for a file""" with self.engine.begin() as db: return delete_single_remote_checkpoint( db, self.user_id, path, checkpoint_id, )
Get the content of a checkpoint.
def get_checkpoint_content(self, checkpoint_id, path): """Get the content of a checkpoint.""" with self.engine.begin() as db: return get_remote_checkpoint( db, self.user_id, path, checkpoint_id, self.crypto.decry...
Return a list of checkpoints for a given file
def list_checkpoints(self, path): """Return a list of checkpoints for a given file""" with self.engine.begin() as db: return list_remote_checkpoints(db, self.user_id, path)
Rename all checkpoints for old_path to new_path.
def rename_all_checkpoints(self, old_path, new_path): """Rename all checkpoints for old_path to new_path.""" with self.engine.begin() as db: return move_remote_checkpoints( db, self.user_id, old_path, new_path, )
Delete all checkpoints for the given path.
def delete_all_checkpoints(self, path): """Delete all checkpoints for the given path.""" with self.engine.begin() as db: delete_remote_checkpoints(db, self.user_id, path)
Purge all database records for the current user.
def purge_db(self): """ Purge all database records for the current user. """ with self.engine.begin() as db: purge_remote_checkpoints(db, self.user_id)
Resolve a path based on a dictionary of manager prefixes.
def _resolve_path(path, manager_dict): """ Resolve a path based on a dictionary of manager prefixes. Returns a triple of (prefix, manager, manager_relative_path). """ path = normalize_api_path(path) parts = path.split('/') # Try to find a sub-manager for the first subdirectory. mgr = m...
Get an argument either from kwargs or from the first entry in args. Raises a TypeError if argname not in kwargs and len ( args ) == 0.
def _get_arg(argname, args, kwargs): """ Get an argument, either from kwargs or from the first entry in args. Raises a TypeError if argname not in kwargs and len(args) == 0. Mutates kwargs in place if the value is found in kwargs. """ try: return kwargs.pop(argname), args except Key...
Prefix all path entries in model with the given prefix.
def _apply_prefix(prefix, model): """ Prefix all path entries in model with the given prefix. """ if not isinstance(model, dict): raise TypeError("Expected dict for model, got %s" % type(model)) # We get unwanted leading/trailing slashes if prefix or model['path'] are # '', both of whic...
Decorator for methods that accept path as a first argument.
def path_dispatch1(mname, returns_model): """ Decorator for methods that accept path as a first argument. """ def _wrapper(self, *args, **kwargs): path, args = _get_arg('path', args, kwargs) prefix, mgr, mgr_path = _resolve_path(path, self.managers) result = getattr(mgr, mname)(m...
Parameterized decorator for methods that accept path as a second argument.
def path_dispatch_kwarg(mname, path_default, returns_model): """ Parameterized decorator for methods that accept path as a second argument. """ def _wrapper(self, path=path_default, **kwargs): prefix, mgr, mgr_path = _resolve_path(path, self.managers) result = getattr(mgr, mname)(pat...
Decorator for methods accepting old_path and new_path.
def path_dispatch_old_new(mname, returns_model): """ Decorator for methods accepting old_path and new_path. """ def _wrapper(self, old_path, new_path, *args, **kwargs): old_prefix, old_mgr, old_mgr_path = _resolve_path( old_path, self.managers ) new_prefix, new_mgr, n...
Strip slashes from directories before updating.
def _managers_changed(self, name, old, new): """ Strip slashes from directories before updating. """ for key in new: if '/' in key: raise ValueError( "Expected directory names w/o slashes. Got [%s]" % key ) self...
Special case handling for listing root dir.
def get(self, path, content=True, type=None, format=None): """ Special case handling for listing root dir. """ path = normalize_api_path(path) if path: return self.__get(path, content=content, type=type, format=format) if not content: return base_d...
Ensure that roots of our managers can t be deleted. This should be enforced by https:// github. com/ ipython/ ipython/ pull/ 8168 but rogue implementations might override this behavior.
def delete(self, path): """ Ensure that roots of our managers can't be deleted. This should be enforced by https://github.com/ipython/ipython/pull/8168, but rogue implementations might override this behavior. """ path = normalize_api_path(path) if path in self.ma...
Resolve paths with.. to normalized paths raising an error if the final result is outside root.
def normalize_api_path(api_path): """ Resolve paths with '..' to normalized paths, raising an error if the final result is outside root. """ normalized = posixpath.normpath(api_path.strip('/')) if normalized == '.': normalized = '' elif normalized.startswith('..'): raise Path...
Split an API file path into directory and name.
def split_api_filepath(path): """ Split an API file path into directory and name. """ parts = path.rsplit('/', 1) if len(parts) == 1: name = parts[0] dirname = '/' else: name = parts[1] dirname = parts[0] + '/' return from_api_dirname(dirname), name
Write a notebook as base64.
def writes_base64(nb, version=NBFORMAT_VERSION): """ Write a notebook as base64. """ return b64encode(writes(nb, version=version).encode('utf-8'))
Read a notebook from base64.
def reads_base64(nb, as_version=NBFORMAT_VERSION): """ Read a notebook from base64. """ try: return reads(b64decode(nb).decode('utf-8'), as_version=as_version) except Exception as e: raise CorruptedFile(e)
Decode base64 data of unknown format.
def _decode_unknown_from_base64(path, bcontent): """ Decode base64 data of unknown format. Attempts to interpret data as utf-8, falling back to ascii on failure. """ content = b64decode(bcontent) try: return (content.decode('utf-8'), 'text') except UnicodeError: pass ret...
Decode base64 content for a file.
def from_b64(path, bcontent, format): """ Decode base64 content for a file. format: If 'text', the contents will be decoded as UTF-8. If 'base64', do nothing. If not specified, try to decode as UTF-8, and fall back to base64 Returns a triple of decoded_content, format, and mimetype. ...
Return an iterable of all prefix directories of path descending from root.
def prefix_dirs(path): """ Return an iterable of all prefix directories of path, descending from root. """ _dirname = posixpath.dirname path = path.strip('/') out = [] while path != '': path = _dirname(path) out.append(path) return reversed(out)
Decorator for converting PathOutsideRoot errors to 404s.
def outside_root_to_404(fn): """ Decorator for converting PathOutsideRoot errors to 404s. """ @wraps(fn) def wrapped(*args, **kwargs): try: return fn(*args, **kwargs) except PathOutsideRoot as e: raise HTTPError(404, "Path outside root: [%s]" % e.args[0]) ...
Create a user.
def create_user(db_url, user): """ Create a user. """ PostgresCheckpoints( db_url=db_url, user_id=user, create_user_on_startup=True, )
Split an iterable of models into a list of file paths and a list of directory paths.
def _separate_dirs_files(models): """ Split an iterable of models into a list of file paths and a list of directory paths. """ dirs = [] files = [] for model in models: if model['type'] == 'directory': dirs.append(model['path']) else: files.append(mode...