partition
stringclasses
3 values
func_name
stringlengths
1
134
docstring
stringlengths
1
46.9k
path
stringlengths
4
223
original_string
stringlengths
75
104k
code
stringlengths
75
104k
docstring_tokens
listlengths
1
1.97k
repo
stringlengths
7
55
language
stringclasses
1 value
url
stringlengths
87
315
code_tokens
listlengths
19
28.4k
sha
stringlengths
40
40
valid
_nginx_stream_spec
This will output the nginx stream config string for specific port spec
dusty/compiler/nginx/__init__.py
def _nginx_stream_spec(port_spec, bridge_ip): """This will output the nginx stream config string for specific port spec """ server_string_spec = "\t server {\n" server_string_spec += "\t \t {}\n".format(_nginx_listen_string(port_spec)) server_string_spec += "\t \t {}\n".format(_nginx_proxy_string(port_spec, bridge_ip)) server_string_spec += "\t }\n" return server_string_spec
def _nginx_stream_spec(port_spec, bridge_ip): """This will output the nginx stream config string for specific port spec """ server_string_spec = "\t server {\n" server_string_spec += "\t \t {}\n".format(_nginx_listen_string(port_spec)) server_string_spec += "\t \t {}\n".format(_nginx_proxy_string(port_spec, bridge_ip)) server_string_spec += "\t }\n" return server_string_spec
[ "This", "will", "output", "the", "nginx", "stream", "config", "string", "for", "specific", "port", "spec" ]
gamechanger/dusty
python
https://github.com/gamechanger/dusty/blob/dc12de90bb6945023d6f43a8071e984313a1d984/dusty/compiler/nginx/__init__.py#L49-L55
[ "def", "_nginx_stream_spec", "(", "port_spec", ",", "bridge_ip", ")", ":", "server_string_spec", "=", "\"\\t server {\\n\"", "server_string_spec", "+=", "\"\\t \\t {}\\n\"", ".", "format", "(", "_nginx_listen_string", "(", "port_spec", ")", ")", "server_string_spec", "+=", "\"\\t \\t {}\\n\"", ".", "format", "(", "_nginx_proxy_string", "(", "port_spec", ",", "bridge_ip", ")", ")", "server_string_spec", "+=", "\"\\t }\\n\"", "return", "server_string_spec" ]
dc12de90bb6945023d6f43a8071e984313a1d984
valid
get_nginx_configuration_spec
This function will take in a port spec as specified by the port_spec compiler and will output an nginx web proxy config string. This string can then be written to a file and used running nginx
dusty/compiler/nginx/__init__.py
def get_nginx_configuration_spec(port_spec_dict, docker_bridge_ip): """This function will take in a port spec as specified by the port_spec compiler and will output an nginx web proxy config string. This string can then be written to a file and used running nginx """ nginx_http_config, nginx_stream_config = "", "" for port_spec in port_spec_dict['nginx']: if port_spec['type'] == 'http': nginx_http_config += _nginx_http_spec(port_spec, docker_bridge_ip) elif port_spec['type'] == 'stream': nginx_stream_config += _nginx_stream_spec(port_spec, docker_bridge_ip) return {'http': nginx_http_config, 'stream': nginx_stream_config}
def get_nginx_configuration_spec(port_spec_dict, docker_bridge_ip): """This function will take in a port spec as specified by the port_spec compiler and will output an nginx web proxy config string. This string can then be written to a file and used running nginx """ nginx_http_config, nginx_stream_config = "", "" for port_spec in port_spec_dict['nginx']: if port_spec['type'] == 'http': nginx_http_config += _nginx_http_spec(port_spec, docker_bridge_ip) elif port_spec['type'] == 'stream': nginx_stream_config += _nginx_stream_spec(port_spec, docker_bridge_ip) return {'http': nginx_http_config, 'stream': nginx_stream_config}
[ "This", "function", "will", "take", "in", "a", "port", "spec", "as", "specified", "by", "the", "port_spec", "compiler", "and", "will", "output", "an", "nginx", "web", "proxy", "config", "string", ".", "This", "string", "can", "then", "be", "written", "to", "a", "file", "and", "used", "running", "nginx" ]
gamechanger/dusty
python
https://github.com/gamechanger/dusty/blob/dc12de90bb6945023d6f43a8071e984313a1d984/dusty/compiler/nginx/__init__.py#L57-L67
[ "def", "get_nginx_configuration_spec", "(", "port_spec_dict", ",", "docker_bridge_ip", ")", ":", "nginx_http_config", ",", "nginx_stream_config", "=", "\"\"", ",", "\"\"", "for", "port_spec", "in", "port_spec_dict", "[", "'nginx'", "]", ":", "if", "port_spec", "[", "'type'", "]", "==", "'http'", ":", "nginx_http_config", "+=", "_nginx_http_spec", "(", "port_spec", ",", "docker_bridge_ip", ")", "elif", "port_spec", "[", "'type'", "]", "==", "'stream'", ":", "nginx_stream_config", "+=", "_nginx_stream_spec", "(", "port_spec", ",", "docker_bridge_ip", ")", "return", "{", "'http'", ":", "nginx_http_config", ",", "'stream'", ":", "nginx_stream_config", "}" ]
dc12de90bb6945023d6f43a8071e984313a1d984
valid
memoized
Decorator. Caches a function's return value each time it is called. If called later with the same arguments, the cached value is returned (not reevaluated). The cache lasts for the duration of each request.
dusty/memoize.py
def memoized(fn): """ Decorator. Caches a function's return value each time it is called. If called later with the same arguments, the cached value is returned (not reevaluated). The cache lasts for the duration of each request. """ @functools.wraps(fn) def memoizer(*args, **kwargs): key = function_key(fn) + pickle.dumps(args) + pickle.dumps(_hash_kwargs(kwargs)) if key not in cache: cache[key] = fn(*args, **kwargs) return cache[key] return memoizer
def memoized(fn): """ Decorator. Caches a function's return value each time it is called. If called later with the same arguments, the cached value is returned (not reevaluated). The cache lasts for the duration of each request. """ @functools.wraps(fn) def memoizer(*args, **kwargs): key = function_key(fn) + pickle.dumps(args) + pickle.dumps(_hash_kwargs(kwargs)) if key not in cache: cache[key] = fn(*args, **kwargs) return cache[key] return memoizer
[ "Decorator", ".", "Caches", "a", "function", "s", "return", "value", "each", "time", "it", "is", "called", ".", "If", "called", "later", "with", "the", "same", "arguments", "the", "cached", "value", "is", "returned", "(", "not", "reevaluated", ")", ".", "The", "cache", "lasts", "for", "the", "duration", "of", "each", "request", "." ]
gamechanger/dusty
python
https://github.com/gamechanger/dusty/blob/dc12de90bb6945023d6f43a8071e984313a1d984/dusty/memoize.py#L12-L24
[ "def", "memoized", "(", "fn", ")", ":", "@", "functools", ".", "wraps", "(", "fn", ")", "def", "memoizer", "(", "*", "args", ",", "*", "*", "kwargs", ")", ":", "key", "=", "function_key", "(", "fn", ")", "+", "pickle", ".", "dumps", "(", "args", ")", "+", "pickle", ".", "dumps", "(", "_hash_kwargs", "(", "kwargs", ")", ")", "if", "key", "not", "in", "cache", ":", "cache", "[", "key", "]", "=", "fn", "(", "*", "args", ",", "*", "*", "kwargs", ")", "return", "cache", "[", "key", "]", "return", "memoizer" ]
dc12de90bb6945023d6f43a8071e984313a1d984
valid
_load_ssh_auth_post_yosemite
Starting with Yosemite, launchd was rearchitected and now only one launchd process runs for all users. This allows us to much more easily impersonate a user through launchd and extract the environment variables from their running processes.
dusty/config.py
def _load_ssh_auth_post_yosemite(mac_username): """Starting with Yosemite, launchd was rearchitected and now only one launchd process runs for all users. This allows us to much more easily impersonate a user through launchd and extract the environment variables from their running processes.""" user_id = subprocess.check_output(['id', '-u', mac_username]) ssh_auth_sock = subprocess.check_output(['launchctl', 'asuser', user_id, 'launchctl', 'getenv', 'SSH_AUTH_SOCK']).rstrip() _set_ssh_auth_sock(ssh_auth_sock)
def _load_ssh_auth_post_yosemite(mac_username): """Starting with Yosemite, launchd was rearchitected and now only one launchd process runs for all users. This allows us to much more easily impersonate a user through launchd and extract the environment variables from their running processes.""" user_id = subprocess.check_output(['id', '-u', mac_username]) ssh_auth_sock = subprocess.check_output(['launchctl', 'asuser', user_id, 'launchctl', 'getenv', 'SSH_AUTH_SOCK']).rstrip() _set_ssh_auth_sock(ssh_auth_sock)
[ "Starting", "with", "Yosemite", "launchd", "was", "rearchitected", "and", "now", "only", "one", "launchd", "process", "runs", "for", "all", "users", ".", "This", "allows", "us", "to", "much", "more", "easily", "impersonate", "a", "user", "through", "launchd", "and", "extract", "the", "environment", "variables", "from", "their", "running", "processes", "." ]
gamechanger/dusty
python
https://github.com/gamechanger/dusty/blob/dc12de90bb6945023d6f43a8071e984313a1d984/dusty/config.py#L87-L94
[ "def", "_load_ssh_auth_post_yosemite", "(", "mac_username", ")", ":", "user_id", "=", "subprocess", ".", "check_output", "(", "[", "'id'", ",", "'-u'", ",", "mac_username", "]", ")", "ssh_auth_sock", "=", "subprocess", ".", "check_output", "(", "[", "'launchctl'", ",", "'asuser'", ",", "user_id", ",", "'launchctl'", ",", "'getenv'", ",", "'SSH_AUTH_SOCK'", "]", ")", ".", "rstrip", "(", ")", "_set_ssh_auth_sock", "(", "ssh_auth_sock", ")" ]
dc12de90bb6945023d6f43a8071e984313a1d984
valid
_load_ssh_auth_pre_yosemite
For OS X versions before Yosemite, many launchd processes run simultaneously under different users and different permission models. The simpler `asuser` trick we use in Yosemite doesn't work, since it gets routed to the wrong launchd. We instead need to find the running ssh-agent process and use its PID to navigate ourselves to the correct launchd.
dusty/config.py
def _load_ssh_auth_pre_yosemite(): """For OS X versions before Yosemite, many launchd processes run simultaneously under different users and different permission models. The simpler `asuser` trick we use in Yosemite doesn't work, since it gets routed to the wrong launchd. We instead need to find the running ssh-agent process and use its PID to navigate ourselves to the correct launchd.""" for process in psutil.process_iter(): if process.name() == 'ssh-agent': ssh_auth_sock = subprocess.check_output(['launchctl', 'bsexec', str(process.pid), 'launchctl', 'getenv', 'SSH_AUTH_SOCK']).rstrip() if ssh_auth_sock: _set_ssh_auth_sock(ssh_auth_sock) break else: daemon_warnings.warn('ssh', 'No running ssh-agent found linked to SSH_AUTH_SOCK')
def _load_ssh_auth_pre_yosemite(): """For OS X versions before Yosemite, many launchd processes run simultaneously under different users and different permission models. The simpler `asuser` trick we use in Yosemite doesn't work, since it gets routed to the wrong launchd. We instead need to find the running ssh-agent process and use its PID to navigate ourselves to the correct launchd.""" for process in psutil.process_iter(): if process.name() == 'ssh-agent': ssh_auth_sock = subprocess.check_output(['launchctl', 'bsexec', str(process.pid), 'launchctl', 'getenv', 'SSH_AUTH_SOCK']).rstrip() if ssh_auth_sock: _set_ssh_auth_sock(ssh_auth_sock) break else: daemon_warnings.warn('ssh', 'No running ssh-agent found linked to SSH_AUTH_SOCK')
[ "For", "OS", "X", "versions", "before", "Yosemite", "many", "launchd", "processes", "run", "simultaneously", "under", "different", "users", "and", "different", "permission", "models", ".", "The", "simpler", "asuser", "trick", "we", "use", "in", "Yosemite", "doesn", "t", "work", "since", "it", "gets", "routed", "to", "the", "wrong", "launchd", ".", "We", "instead", "need", "to", "find", "the", "running", "ssh", "-", "agent", "process", "and", "use", "its", "PID", "to", "navigate", "ourselves", "to", "the", "correct", "launchd", "." ]
gamechanger/dusty
python
https://github.com/gamechanger/dusty/blob/dc12de90bb6945023d6f43a8071e984313a1d984/dusty/config.py#L96-L109
[ "def", "_load_ssh_auth_pre_yosemite", "(", ")", ":", "for", "process", "in", "psutil", ".", "process_iter", "(", ")", ":", "if", "process", ".", "name", "(", ")", "==", "'ssh-agent'", ":", "ssh_auth_sock", "=", "subprocess", ".", "check_output", "(", "[", "'launchctl'", ",", "'bsexec'", ",", "str", "(", "process", ".", "pid", ")", ",", "'launchctl'", ",", "'getenv'", ",", "'SSH_AUTH_SOCK'", "]", ")", ".", "rstrip", "(", ")", "if", "ssh_auth_sock", ":", "_set_ssh_auth_sock", "(", "ssh_auth_sock", ")", "break", "else", ":", "daemon_warnings", ".", "warn", "(", "'ssh'", ",", "'No running ssh-agent found linked to SSH_AUTH_SOCK'", ")" ]
dc12de90bb6945023d6f43a8071e984313a1d984
valid
check_and_load_ssh_auth
Will check the mac_username config value; if it is present, will load that user's SSH_AUTH_SOCK environment variable to the current environment. This allows git clones to behave the same for the daemon as they do for the user
dusty/config.py
def check_and_load_ssh_auth(): """ Will check the mac_username config value; if it is present, will load that user's SSH_AUTH_SOCK environment variable to the current environment. This allows git clones to behave the same for the daemon as they do for the user """ mac_username = get_config_value(constants.CONFIG_MAC_USERNAME_KEY) if not mac_username: logging.info("Can't setup ssh authorization; no mac_username specified") return if not _running_on_mac(): # give our Linux unit tests a way to not freak out logging.info("Skipping SSH load, we are not running on Mac") return if _mac_version_is_post_yosemite(): _load_ssh_auth_post_yosemite(mac_username) else: _load_ssh_auth_pre_yosemite()
def check_and_load_ssh_auth(): """ Will check the mac_username config value; if it is present, will load that user's SSH_AUTH_SOCK environment variable to the current environment. This allows git clones to behave the same for the daemon as they do for the user """ mac_username = get_config_value(constants.CONFIG_MAC_USERNAME_KEY) if not mac_username: logging.info("Can't setup ssh authorization; no mac_username specified") return if not _running_on_mac(): # give our Linux unit tests a way to not freak out logging.info("Skipping SSH load, we are not running on Mac") return if _mac_version_is_post_yosemite(): _load_ssh_auth_post_yosemite(mac_username) else: _load_ssh_auth_pre_yosemite()
[ "Will", "check", "the", "mac_username", "config", "value", ";", "if", "it", "is", "present", "will", "load", "that", "user", "s", "SSH_AUTH_SOCK", "environment", "variable", "to", "the", "current", "environment", ".", "This", "allows", "git", "clones", "to", "behave", "the", "same", "for", "the", "daemon", "as", "they", "do", "for", "the", "user" ]
gamechanger/dusty
python
https://github.com/gamechanger/dusty/blob/dc12de90bb6945023d6f43a8071e984313a1d984/dusty/config.py#L120-L138
[ "def", "check_and_load_ssh_auth", "(", ")", ":", "mac_username", "=", "get_config_value", "(", "constants", ".", "CONFIG_MAC_USERNAME_KEY", ")", "if", "not", "mac_username", ":", "logging", ".", "info", "(", "\"Can't setup ssh authorization; no mac_username specified\"", ")", "return", "if", "not", "_running_on_mac", "(", ")", ":", "# give our Linux unit tests a way to not freak out", "logging", ".", "info", "(", "\"Skipping SSH load, we are not running on Mac\"", ")", "return", "if", "_mac_version_is_post_yosemite", "(", ")", ":", "_load_ssh_auth_post_yosemite", "(", "mac_username", ")", "else", ":", "_load_ssh_auth_pre_yosemite", "(", ")" ]
dc12de90bb6945023d6f43a8071e984313a1d984
valid
_cleanup_path
Recursively delete a path upon exiting this context manager. Supports targets that are files or directories.
dusty/commands/cp.py
def _cleanup_path(path): """Recursively delete a path upon exiting this context manager. Supports targets that are files or directories.""" try: yield finally: if os.path.exists(path): if os.path.isdir(path): shutil.rmtree(path) else: os.remove(path)
def _cleanup_path(path): """Recursively delete a path upon exiting this context manager. Supports targets that are files or directories.""" try: yield finally: if os.path.exists(path): if os.path.isdir(path): shutil.rmtree(path) else: os.remove(path)
[ "Recursively", "delete", "a", "path", "upon", "exiting", "this", "context", "manager", ".", "Supports", "targets", "that", "are", "files", "or", "directories", "." ]
gamechanger/dusty
python
https://github.com/gamechanger/dusty/blob/dc12de90bb6945023d6f43a8071e984313a1d984/dusty/commands/cp.py#L16-L26
[ "def", "_cleanup_path", "(", "path", ")", ":", "try", ":", "yield", "finally", ":", "if", "os", ".", "path", ".", "exists", "(", "path", ")", ":", "if", "os", ".", "path", ".", "isdir", "(", "path", ")", ":", "shutil", ".", "rmtree", "(", "path", ")", "else", ":", "os", ".", "remove", "(", "path", ")" ]
dc12de90bb6945023d6f43a8071e984313a1d984
valid
copy_between_containers
Copy a file from the source container to an intermediate staging area on the local filesystem, then from that staging area to the destination container. These moves take place without demotion for two reasons: 1. There should be no permissions vulnerabilities with copying between containers because it is assumed the non-privileged user has full access to all Dusty containers. 2. The temp dir created by mkdtemp is owned by the owner of the Dusty daemon process, so if we demoted our moves to/from that location they would encounter permission errors.
dusty/commands/cp.py
def copy_between_containers(source_name, source_path, dest_name, dest_path): """Copy a file from the source container to an intermediate staging area on the local filesystem, then from that staging area to the destination container. These moves take place without demotion for two reasons: 1. There should be no permissions vulnerabilities with copying between containers because it is assumed the non-privileged user has full access to all Dusty containers. 2. The temp dir created by mkdtemp is owned by the owner of the Dusty daemon process, so if we demoted our moves to/from that location they would encounter permission errors.""" if not container_path_exists(source_name, source_path): raise RuntimeError('ERROR: Path {} does not exist inside container {}.'.format(source_path, source_name)) temp_path = os.path.join(tempfile.mkdtemp(), str(uuid.uuid1())) with _cleanup_path(temp_path): copy_to_local(temp_path, source_name, source_path, demote=False) copy_from_local(temp_path, dest_name, dest_path, demote=False)
def copy_between_containers(source_name, source_path, dest_name, dest_path): """Copy a file from the source container to an intermediate staging area on the local filesystem, then from that staging area to the destination container. These moves take place without demotion for two reasons: 1. There should be no permissions vulnerabilities with copying between containers because it is assumed the non-privileged user has full access to all Dusty containers. 2. The temp dir created by mkdtemp is owned by the owner of the Dusty daemon process, so if we demoted our moves to/from that location they would encounter permission errors.""" if not container_path_exists(source_name, source_path): raise RuntimeError('ERROR: Path {} does not exist inside container {}.'.format(source_path, source_name)) temp_path = os.path.join(tempfile.mkdtemp(), str(uuid.uuid1())) with _cleanup_path(temp_path): copy_to_local(temp_path, source_name, source_path, demote=False) copy_from_local(temp_path, dest_name, dest_path, demote=False)
[ "Copy", "a", "file", "from", "the", "source", "container", "to", "an", "intermediate", "staging", "area", "on", "the", "local", "filesystem", "then", "from", "that", "staging", "area", "to", "the", "destination", "container", "." ]
gamechanger/dusty
python
https://github.com/gamechanger/dusty/blob/dc12de90bb6945023d6f43a8071e984313a1d984/dusty/commands/cp.py#L29-L46
[ "def", "copy_between_containers", "(", "source_name", ",", "source_path", ",", "dest_name", ",", "dest_path", ")", ":", "if", "not", "container_path_exists", "(", "source_name", ",", "source_path", ")", ":", "raise", "RuntimeError", "(", "'ERROR: Path {} does not exist inside container {}.'", ".", "format", "(", "source_path", ",", "source_name", ")", ")", "temp_path", "=", "os", ".", "path", ".", "join", "(", "tempfile", ".", "mkdtemp", "(", ")", ",", "str", "(", "uuid", ".", "uuid1", "(", ")", ")", ")", "with", "_cleanup_path", "(", "temp_path", ")", ":", "copy_to_local", "(", "temp_path", ",", "source_name", ",", "source_path", ",", "demote", "=", "False", ")", "copy_from_local", "(", "temp_path", ",", "dest_name", ",", "dest_path", ",", "demote", "=", "False", ")" ]
dc12de90bb6945023d6f43a8071e984313a1d984
valid
copy_from_local
Copy a path from the local filesystem to a path inside a Dusty container. The files on the local filesystem must be accessible by the user specified in mac_username.
dusty/commands/cp.py
def copy_from_local(local_path, remote_name, remote_path, demote=True): """Copy a path from the local filesystem to a path inside a Dusty container. The files on the local filesystem must be accessible by the user specified in mac_username.""" if not os.path.exists(local_path): raise RuntimeError('ERROR: Path {} does not exist'.format(local_path)) temp_identifier = str(uuid.uuid1()) if os.path.isdir(local_path): sync_local_path_to_vm(local_path, os.path.join(vm_cp_path(remote_name), temp_identifier), demote=demote) move_dir_inside_container(remote_name, os.path.join(constants.CONTAINER_CP_DIR, temp_identifier), remote_path) else: sync_local_path_to_vm(local_path, os.path.join(vm_cp_path(remote_name), temp_identifier), demote=demote) move_file_inside_container(remote_name, os.path.join(constants.CONTAINER_CP_DIR, temp_identifier), remote_path)
def copy_from_local(local_path, remote_name, remote_path, demote=True): """Copy a path from the local filesystem to a path inside a Dusty container. The files on the local filesystem must be accessible by the user specified in mac_username.""" if not os.path.exists(local_path): raise RuntimeError('ERROR: Path {} does not exist'.format(local_path)) temp_identifier = str(uuid.uuid1()) if os.path.isdir(local_path): sync_local_path_to_vm(local_path, os.path.join(vm_cp_path(remote_name), temp_identifier), demote=demote) move_dir_inside_container(remote_name, os.path.join(constants.CONTAINER_CP_DIR, temp_identifier), remote_path) else: sync_local_path_to_vm(local_path, os.path.join(vm_cp_path(remote_name), temp_identifier), demote=demote) move_file_inside_container(remote_name, os.path.join(constants.CONTAINER_CP_DIR, temp_identifier), remote_path)
[ "Copy", "a", "path", "from", "the", "local", "filesystem", "to", "a", "path", "inside", "a", "Dusty", "container", ".", "The", "files", "on", "the", "local", "filesystem", "must", "be", "accessible", "by", "the", "user", "specified", "in", "mac_username", "." ]
gamechanger/dusty
python
https://github.com/gamechanger/dusty/blob/dc12de90bb6945023d6f43a8071e984313a1d984/dusty/commands/cp.py#L49-L61
[ "def", "copy_from_local", "(", "local_path", ",", "remote_name", ",", "remote_path", ",", "demote", "=", "True", ")", ":", "if", "not", "os", ".", "path", ".", "exists", "(", "local_path", ")", ":", "raise", "RuntimeError", "(", "'ERROR: Path {} does not exist'", ".", "format", "(", "local_path", ")", ")", "temp_identifier", "=", "str", "(", "uuid", ".", "uuid1", "(", ")", ")", "if", "os", ".", "path", ".", "isdir", "(", "local_path", ")", ":", "sync_local_path_to_vm", "(", "local_path", ",", "os", ".", "path", ".", "join", "(", "vm_cp_path", "(", "remote_name", ")", ",", "temp_identifier", ")", ",", "demote", "=", "demote", ")", "move_dir_inside_container", "(", "remote_name", ",", "os", ".", "path", ".", "join", "(", "constants", ".", "CONTAINER_CP_DIR", ",", "temp_identifier", ")", ",", "remote_path", ")", "else", ":", "sync_local_path_to_vm", "(", "local_path", ",", "os", ".", "path", ".", "join", "(", "vm_cp_path", "(", "remote_name", ")", ",", "temp_identifier", ")", ",", "demote", "=", "demote", ")", "move_file_inside_container", "(", "remote_name", ",", "os", ".", "path", ".", "join", "(", "constants", ".", "CONTAINER_CP_DIR", ",", "temp_identifier", ")", ",", "remote_path", ")" ]
dc12de90bb6945023d6f43a8071e984313a1d984
valid
copy_to_local
Copy a path from inside a Dusty container to a path on the local filesystem. The path on the local filesystem must be wrist-accessible by the user specified in mac_username.
dusty/commands/cp.py
def copy_to_local(local_path, remote_name, remote_path, demote=True): """Copy a path from inside a Dusty container to a path on the local filesystem. The path on the local filesystem must be wrist-accessible by the user specified in mac_username.""" if not container_path_exists(remote_name, remote_path): raise RuntimeError('ERROR: Path {} does not exist inside container {}.'.format(remote_path, remote_name)) temp_identifier = str(uuid.uuid1()) copy_path_inside_container(remote_name, remote_path, os.path.join(constants.CONTAINER_CP_DIR, temp_identifier)) vm_path = os.path.join(vm_cp_path(remote_name), temp_identifier) is_dir = vm_path_is_directory(vm_path) sync_local_path_from_vm(local_path, vm_path, demote=demote, is_dir=is_dir)
def copy_to_local(local_path, remote_name, remote_path, demote=True): """Copy a path from inside a Dusty container to a path on the local filesystem. The path on the local filesystem must be wrist-accessible by the user specified in mac_username.""" if not container_path_exists(remote_name, remote_path): raise RuntimeError('ERROR: Path {} does not exist inside container {}.'.format(remote_path, remote_name)) temp_identifier = str(uuid.uuid1()) copy_path_inside_container(remote_name, remote_path, os.path.join(constants.CONTAINER_CP_DIR, temp_identifier)) vm_path = os.path.join(vm_cp_path(remote_name), temp_identifier) is_dir = vm_path_is_directory(vm_path) sync_local_path_from_vm(local_path, vm_path, demote=demote, is_dir=is_dir)
[ "Copy", "a", "path", "from", "inside", "a", "Dusty", "container", "to", "a", "path", "on", "the", "local", "filesystem", ".", "The", "path", "on", "the", "local", "filesystem", "must", "be", "wrist", "-", "accessible", "by", "the", "user", "specified", "in", "mac_username", "." ]
gamechanger/dusty
python
https://github.com/gamechanger/dusty/blob/dc12de90bb6945023d6f43a8071e984313a1d984/dusty/commands/cp.py#L64-L74
[ "def", "copy_to_local", "(", "local_path", ",", "remote_name", ",", "remote_path", ",", "demote", "=", "True", ")", ":", "if", "not", "container_path_exists", "(", "remote_name", ",", "remote_path", ")", ":", "raise", "RuntimeError", "(", "'ERROR: Path {} does not exist inside container {}.'", ".", "format", "(", "remote_path", ",", "remote_name", ")", ")", "temp_identifier", "=", "str", "(", "uuid", ".", "uuid1", "(", ")", ")", "copy_path_inside_container", "(", "remote_name", ",", "remote_path", ",", "os", ".", "path", ".", "join", "(", "constants", ".", "CONTAINER_CP_DIR", ",", "temp_identifier", ")", ")", "vm_path", "=", "os", ".", "path", ".", "join", "(", "vm_cp_path", "(", "remote_name", ")", ",", "temp_identifier", ")", "is_dir", "=", "vm_path_is_directory", "(", "vm_path", ")", "sync_local_path_from_vm", "(", "local_path", ",", "vm_path", ",", "demote", "=", "demote", ",", "is_dir", "=", "is_dir", ")" ]
dc12de90bb6945023d6f43a8071e984313a1d984
valid
_mount_repo
This function will create the VM directory where a repo will be mounted, if it doesn't exist. If wait_for_server is set, it will wait up to 10 seconds for the nfs server to start, by retrying mounts that fail with 'Connection Refused'. If wait_for_server is not set, it will attempt to run the mount command once
dusty/systems/nfs/client.py
def _mount_repo(repo, wait_for_server=False): """ This function will create the VM directory where a repo will be mounted, if it doesn't exist. If wait_for_server is set, it will wait up to 10 seconds for the nfs server to start, by retrying mounts that fail with 'Connection Refused'. If wait_for_server is not set, it will attempt to run the mount command once """ check_call_on_vm('sudo mkdir -p {}'.format(repo.vm_path)) if wait_for_server: for i in range(0,10): try: _run_mount_command(repo) return except CalledProcessError as e: if 'Connection refused' in e.output: logging.info('Failed to mount repo; waiting for nfsd to restart') time.sleep(1) else: logging.info(e.output) raise e log_to_client('Failed to mount repo {}'.format(repo.short_name)) raise RuntimeError('Unable to mount repo with NFS') else: _run_mount_command(repo)
def _mount_repo(repo, wait_for_server=False): """ This function will create the VM directory where a repo will be mounted, if it doesn't exist. If wait_for_server is set, it will wait up to 10 seconds for the nfs server to start, by retrying mounts that fail with 'Connection Refused'. If wait_for_server is not set, it will attempt to run the mount command once """ check_call_on_vm('sudo mkdir -p {}'.format(repo.vm_path)) if wait_for_server: for i in range(0,10): try: _run_mount_command(repo) return except CalledProcessError as e: if 'Connection refused' in e.output: logging.info('Failed to mount repo; waiting for nfsd to restart') time.sleep(1) else: logging.info(e.output) raise e log_to_client('Failed to mount repo {}'.format(repo.short_name)) raise RuntimeError('Unable to mount repo with NFS') else: _run_mount_command(repo)
[ "This", "function", "will", "create", "the", "VM", "directory", "where", "a", "repo", "will", "be", "mounted", "if", "it", "doesn", "t", "exist", ".", "If", "wait_for_server", "is", "set", "it", "will", "wait", "up", "to", "10", "seconds", "for", "the", "nfs", "server", "to", "start", "by", "retrying", "mounts", "that", "fail", "with", "Connection", "Refused", "." ]
gamechanger/dusty
python
https://github.com/gamechanger/dusty/blob/dc12de90bb6945023d6f43a8071e984313a1d984/dusty/systems/nfs/client.py#L41-L65
[ "def", "_mount_repo", "(", "repo", ",", "wait_for_server", "=", "False", ")", ":", "check_call_on_vm", "(", "'sudo mkdir -p {}'", ".", "format", "(", "repo", ".", "vm_path", ")", ")", "if", "wait_for_server", ":", "for", "i", "in", "range", "(", "0", ",", "10", ")", ":", "try", ":", "_run_mount_command", "(", "repo", ")", "return", "except", "CalledProcessError", "as", "e", ":", "if", "'Connection refused'", "in", "e", ".", "output", ":", "logging", ".", "info", "(", "'Failed to mount repo; waiting for nfsd to restart'", ")", "time", ".", "sleep", "(", "1", ")", "else", ":", "logging", ".", "info", "(", "e", ".", "output", ")", "raise", "e", "log_to_client", "(", "'Failed to mount repo {}'", ".", "format", "(", "repo", ".", "short_name", ")", ")", "raise", "RuntimeError", "(", "'Unable to mount repo with NFS'", ")", "else", ":", "_run_mount_command", "(", "repo", ")" ]
dc12de90bb6945023d6f43a8071e984313a1d984
valid
get_port_spec_document
Given a dictionary containing the expanded dusty DAG specs this function will return a dictionary containing the port mappings needed by downstream methods. Currently this includes docker_compose, virtualbox, nginx and hosts_file.
dusty/compiler/port_spec/__init__.py
def get_port_spec_document(expanded_active_specs, docker_vm_ip): """ Given a dictionary containing the expanded dusty DAG specs this function will return a dictionary containing the port mappings needed by downstream methods. Currently this includes docker_compose, virtualbox, nginx and hosts_file.""" forwarding_port = 65000 port_spec = {'docker_compose':{}, 'nginx':[], 'hosts_file':[]} host_full_addresses, host_names, stream_host_ports = set(), set(), set() # No matter the order of apps in expanded_active_specs, we want to produce a consistent # port_spec with respect to the apps and the ports they are outputted on for app_name in sorted(expanded_active_specs['apps'].keys()): app_spec = expanded_active_specs['apps'][app_name] if 'host_forwarding' not in app_spec: continue port_spec['docker_compose'][app_name] = [] for host_forwarding_spec in app_spec['host_forwarding']: # These functions are just used for validating the set of specs all works together _add_full_addresses(host_forwarding_spec, host_full_addresses) if host_forwarding_spec['type'] == 'stream': _add_stream_host_port(host_forwarding_spec, stream_host_ports) port_spec['docker_compose'][app_name].append(_docker_compose_port_spec(host_forwarding_spec, forwarding_port)) port_spec['nginx'].append(_nginx_port_spec(host_forwarding_spec, forwarding_port, docker_vm_ip)) _add_host_names(host_forwarding_spec, docker_vm_ip, port_spec, host_names) forwarding_port += 1 return port_spec
def get_port_spec_document(expanded_active_specs, docker_vm_ip): """ Given a dictionary containing the expanded dusty DAG specs this function will return a dictionary containing the port mappings needed by downstream methods. Currently this includes docker_compose, virtualbox, nginx and hosts_file.""" forwarding_port = 65000 port_spec = {'docker_compose':{}, 'nginx':[], 'hosts_file':[]} host_full_addresses, host_names, stream_host_ports = set(), set(), set() # No matter the order of apps in expanded_active_specs, we want to produce a consistent # port_spec with respect to the apps and the ports they are outputted on for app_name in sorted(expanded_active_specs['apps'].keys()): app_spec = expanded_active_specs['apps'][app_name] if 'host_forwarding' not in app_spec: continue port_spec['docker_compose'][app_name] = [] for host_forwarding_spec in app_spec['host_forwarding']: # These functions are just used for validating the set of specs all works together _add_full_addresses(host_forwarding_spec, host_full_addresses) if host_forwarding_spec['type'] == 'stream': _add_stream_host_port(host_forwarding_spec, stream_host_ports) port_spec['docker_compose'][app_name].append(_docker_compose_port_spec(host_forwarding_spec, forwarding_port)) port_spec['nginx'].append(_nginx_port_spec(host_forwarding_spec, forwarding_port, docker_vm_ip)) _add_host_names(host_forwarding_spec, docker_vm_ip, port_spec, host_names) forwarding_port += 1 return port_spec
[ "Given", "a", "dictionary", "containing", "the", "expanded", "dusty", "DAG", "specs", "this", "function", "will", "return", "a", "dictionary", "containing", "the", "port", "mappings", "needed", "by", "downstream", "methods", ".", "Currently", "this", "includes", "docker_compose", "virtualbox", "nginx", "and", "hosts_file", "." ]
gamechanger/dusty
python
https://github.com/gamechanger/dusty/blob/dc12de90bb6945023d6f43a8071e984313a1d984/dusty/compiler/port_spec/__init__.py#L39-L64
[ "def", "get_port_spec_document", "(", "expanded_active_specs", ",", "docker_vm_ip", ")", ":", "forwarding_port", "=", "65000", "port_spec", "=", "{", "'docker_compose'", ":", "{", "}", ",", "'nginx'", ":", "[", "]", ",", "'hosts_file'", ":", "[", "]", "}", "host_full_addresses", ",", "host_names", ",", "stream_host_ports", "=", "set", "(", ")", ",", "set", "(", ")", ",", "set", "(", ")", "# No matter the order of apps in expanded_active_specs, we want to produce a consistent", "# port_spec with respect to the apps and the ports they are outputted on", "for", "app_name", "in", "sorted", "(", "expanded_active_specs", "[", "'apps'", "]", ".", "keys", "(", ")", ")", ":", "app_spec", "=", "expanded_active_specs", "[", "'apps'", "]", "[", "app_name", "]", "if", "'host_forwarding'", "not", "in", "app_spec", ":", "continue", "port_spec", "[", "'docker_compose'", "]", "[", "app_name", "]", "=", "[", "]", "for", "host_forwarding_spec", "in", "app_spec", "[", "'host_forwarding'", "]", ":", "# These functions are just used for validating the set of specs all works together", "_add_full_addresses", "(", "host_forwarding_spec", ",", "host_full_addresses", ")", "if", "host_forwarding_spec", "[", "'type'", "]", "==", "'stream'", ":", "_add_stream_host_port", "(", "host_forwarding_spec", ",", "stream_host_ports", ")", "port_spec", "[", "'docker_compose'", "]", "[", "app_name", "]", ".", "append", "(", "_docker_compose_port_spec", "(", "host_forwarding_spec", ",", "forwarding_port", ")", ")", "port_spec", "[", "'nginx'", "]", ".", "append", "(", "_nginx_port_spec", "(", "host_forwarding_spec", ",", "forwarding_port", ",", "docker_vm_ip", ")", ")", "_add_host_names", "(", "host_forwarding_spec", ",", "docker_vm_ip", ",", "port_spec", ",", "host_names", ")", "forwarding_port", "+=", "1", "return", "port_spec" ]
dc12de90bb6945023d6f43a8071e984313a1d984
valid
init_yaml_constructor
This dark magic is used to make yaml.safe_load encode all strings as utf-8, where otherwise python unicode strings would be returned for non-ascii chars
dusty/payload.py
def init_yaml_constructor(): """ This dark magic is used to make yaml.safe_load encode all strings as utf-8, where otherwise python unicode strings would be returned for non-ascii chars """ def utf_encoding_string_constructor(loader, node): return loader.construct_scalar(node).encode('utf-8') yaml.SafeLoader.add_constructor(u'tag:yaml.org,2002:str', utf_encoding_string_constructor)
def init_yaml_constructor(): """ This dark magic is used to make yaml.safe_load encode all strings as utf-8, where otherwise python unicode strings would be returned for non-ascii chars """ def utf_encoding_string_constructor(loader, node): return loader.construct_scalar(node).encode('utf-8') yaml.SafeLoader.add_constructor(u'tag:yaml.org,2002:str', utf_encoding_string_constructor)
[ "This", "dark", "magic", "is", "used", "to", "make", "yaml", ".", "safe_load", "encode", "all", "strings", "as", "utf", "-", "8", "where", "otherwise", "python", "unicode", "strings", "would", "be", "returned", "for", "non", "-", "ascii", "chars" ]
gamechanger/dusty
python
https://github.com/gamechanger/dusty/blob/dc12de90bb6945023d6f43a8071e984313a1d984/dusty/payload.py#L52-L59
[ "def", "init_yaml_constructor", "(", ")", ":", "def", "utf_encoding_string_constructor", "(", "loader", ",", "node", ")", ":", "return", "loader", ".", "construct_scalar", "(", "node", ")", ".", "encode", "(", "'utf-8'", ")", "yaml", ".", "SafeLoader", ".", "add_constructor", "(", "u'tag:yaml.org,2002:str'", ",", "utf_encoding_string_constructor", ")" ]
dc12de90bb6945023d6f43a8071e984313a1d984
valid
registry_from_image
Returns the Docker registry host associated with a given image name.
dusty/systems/docker/config.py
def registry_from_image(image_name): """Returns the Docker registry host associated with a given image name.""" if '/' not in image_name: # official image return constants.PUBLIC_DOCKER_REGISTRY prefix = image_name.split('/')[0] if '.' not in prefix: # user image on official repository, e.g. thieman/clojure return constants.PUBLIC_DOCKER_REGISTRY return prefix
def registry_from_image(image_name): """Returns the Docker registry host associated with a given image name.""" if '/' not in image_name: # official image return constants.PUBLIC_DOCKER_REGISTRY prefix = image_name.split('/')[0] if '.' not in prefix: # user image on official repository, e.g. thieman/clojure return constants.PUBLIC_DOCKER_REGISTRY return prefix
[ "Returns", "the", "Docker", "registry", "host", "associated", "with", "a", "given", "image", "name", "." ]
gamechanger/dusty
python
https://github.com/gamechanger/dusty/blob/dc12de90bb6945023d6f43a8071e984313a1d984/dusty/systems/docker/config.py#L15-L23
[ "def", "registry_from_image", "(", "image_name", ")", ":", "if", "'/'", "not", "in", "image_name", ":", "# official image", "return", "constants", ".", "PUBLIC_DOCKER_REGISTRY", "prefix", "=", "image_name", ".", "split", "(", "'/'", ")", "[", "0", "]", "if", "'.'", "not", "in", "prefix", ":", "# user image on official repository, e.g. thieman/clojure", "return", "constants", ".", "PUBLIC_DOCKER_REGISTRY", "return", "prefix" ]
dc12de90bb6945023d6f43a8071e984313a1d984
valid
get_authed_registries
Reads the local Docker client config for the current user and returns all registries to which the user may be logged in. This is intended to be run client-side, not by the daemon.
dusty/systems/docker/config.py
def get_authed_registries(): """Reads the local Docker client config for the current user and returns all registries to which the user may be logged in. This is intended to be run client-side, not by the daemon.""" result = set() if not os.path.exists(constants.DOCKER_CONFIG_PATH): return result config = json.load(open(constants.DOCKER_CONFIG_PATH, 'r')) for registry in config.get('auths', {}).iterkeys(): try: parsed = urlparse(registry) except Exception: log_to_client('Error parsing registry {} from Docker config, will skip this registry').format(registry) # This logic assumes the auth is either of the form # gamechanger.io (no scheme, no path after host) or # of the form https://index.docker.io/v1/ (scheme, # netloc parses correctly, additional path does not matter). # These are the formats I saw in my personal config file, # not sure what other formats it might accept. result.add(parsed.netloc) if parsed.netloc else result.add(parsed.path) return result
def get_authed_registries(): """Reads the local Docker client config for the current user and returns all registries to which the user may be logged in. This is intended to be run client-side, not by the daemon.""" result = set() if not os.path.exists(constants.DOCKER_CONFIG_PATH): return result config = json.load(open(constants.DOCKER_CONFIG_PATH, 'r')) for registry in config.get('auths', {}).iterkeys(): try: parsed = urlparse(registry) except Exception: log_to_client('Error parsing registry {} from Docker config, will skip this registry').format(registry) # This logic assumes the auth is either of the form # gamechanger.io (no scheme, no path after host) or # of the form https://index.docker.io/v1/ (scheme, # netloc parses correctly, additional path does not matter). # These are the formats I saw in my personal config file, # not sure what other formats it might accept. result.add(parsed.netloc) if parsed.netloc else result.add(parsed.path) return result
[ "Reads", "the", "local", "Docker", "client", "config", "for", "the", "current", "user", "and", "returns", "all", "registries", "to", "which", "the", "user", "may", "be", "logged", "in", ".", "This", "is", "intended", "to", "be", "run", "client", "-", "side", "not", "by", "the", "daemon", "." ]
gamechanger/dusty
python
https://github.com/gamechanger/dusty/blob/dc12de90bb6945023d6f43a8071e984313a1d984/dusty/systems/docker/config.py#L26-L46
[ "def", "get_authed_registries", "(", ")", ":", "result", "=", "set", "(", ")", "if", "not", "os", ".", "path", ".", "exists", "(", "constants", ".", "DOCKER_CONFIG_PATH", ")", ":", "return", "result", "config", "=", "json", ".", "load", "(", "open", "(", "constants", ".", "DOCKER_CONFIG_PATH", ",", "'r'", ")", ")", "for", "registry", "in", "config", ".", "get", "(", "'auths'", ",", "{", "}", ")", ".", "iterkeys", "(", ")", ":", "try", ":", "parsed", "=", "urlparse", "(", "registry", ")", "except", "Exception", ":", "log_to_client", "(", "'Error parsing registry {} from Docker config, will skip this registry'", ")", ".", "format", "(", "registry", ")", "# This logic assumes the auth is either of the form", "# gamechanger.io (no scheme, no path after host) or", "# of the form https://index.docker.io/v1/ (scheme,", "# netloc parses correctly, additional path does not matter).", "# These are the formats I saw in my personal config file,", "# not sure what other formats it might accept.", "result", ".", "add", "(", "parsed", ".", "netloc", ")", "if", "parsed", ".", "netloc", "else", "result", ".", "add", "(", "parsed", ".", "path", ")", "return", "result" ]
dc12de90bb6945023d6f43a8071e984313a1d984
valid
streaming_to_client
Puts the client logger into streaming mode, which sends unbuffered input through to the socket one character at a time. We also disable propagation so the root logger does not receive many one-byte emissions. This context handler was originally created for streaming Compose up's terminal output through to the client and should only be used for similarly complex circumstances.
dusty/log.py
def streaming_to_client(): """Puts the client logger into streaming mode, which sends unbuffered input through to the socket one character at a time. We also disable propagation so the root logger does not receive many one-byte emissions. This context handler was originally created for streaming Compose up's terminal output through to the client and should only be used for similarly complex circumstances.""" for handler in client_logger.handlers: if hasattr(handler, 'append_newlines'): break else: handler = None old_propagate = client_logger.propagate client_logger.propagate = False if handler is not None: old_append = handler.append_newlines handler.append_newlines = False yield client_logger.propagate = old_propagate if handler is not None: handler.append_newlines = old_append
def streaming_to_client(): """Puts the client logger into streaming mode, which sends unbuffered input through to the socket one character at a time. We also disable propagation so the root logger does not receive many one-byte emissions. This context handler was originally created for streaming Compose up's terminal output through to the client and should only be used for similarly complex circumstances.""" for handler in client_logger.handlers: if hasattr(handler, 'append_newlines'): break else: handler = None old_propagate = client_logger.propagate client_logger.propagate = False if handler is not None: old_append = handler.append_newlines handler.append_newlines = False yield client_logger.propagate = old_propagate if handler is not None: handler.append_newlines = old_append
[ "Puts", "the", "client", "logger", "into", "streaming", "mode", "which", "sends", "unbuffered", "input", "through", "to", "the", "socket", "one", "character", "at", "a", "time", ".", "We", "also", "disable", "propagation", "so", "the", "root", "logger", "does", "not", "receive", "many", "one", "-", "byte", "emissions", ".", "This", "context", "handler", "was", "originally", "created", "for", "streaming", "Compose", "up", "s", "terminal", "output", "through", "to", "the", "client", "and", "should", "only", "be", "used", "for", "similarly", "complex", "circumstances", "." ]
gamechanger/dusty
python
https://github.com/gamechanger/dusty/blob/dc12de90bb6945023d6f43a8071e984313a1d984/dusty/log.py#L67-L88
[ "def", "streaming_to_client", "(", ")", ":", "for", "handler", "in", "client_logger", ".", "handlers", ":", "if", "hasattr", "(", "handler", ",", "'append_newlines'", ")", ":", "break", "else", ":", "handler", "=", "None", "old_propagate", "=", "client_logger", ".", "propagate", "client_logger", ".", "propagate", "=", "False", "if", "handler", "is", "not", "None", ":", "old_append", "=", "handler", ".", "append_newlines", "handler", ".", "append_newlines", "=", "False", "yield", "client_logger", ".", "propagate", "=", "old_propagate", "if", "handler", "is", "not", "None", ":", "handler", ".", "append_newlines", "=", "old_append" ]
dc12de90bb6945023d6f43a8071e984313a1d984
valid
pty_fork
Runs a subprocess with a PTY attached via fork and exec. The output from the PTY is streamed through log_to_client. This should not be necessary for most subprocesses, we built this to handle Compose up which only streams pull progress if it is attached to a TTY.
dusty/commands/utils.py
def pty_fork(*args): """Runs a subprocess with a PTY attached via fork and exec. The output from the PTY is streamed through log_to_client. This should not be necessary for most subprocesses, we built this to handle Compose up which only streams pull progress if it is attached to a TTY.""" updated_env = copy(os.environ) updated_env.update(get_docker_env()) args += (updated_env,) executable = args[0] demote_fn = demote_to_user(get_config_value(constants.CONFIG_MAC_USERNAME_KEY)) child_pid, pty_fd = pty.fork() if child_pid == 0: demote_fn() os.execle(_executable_path(executable), *args) else: child_process = psutil.Process(child_pid) terminal = os.fdopen(pty_fd, 'r', 0) with streaming_to_client(): while child_process.status() == 'running': output = terminal.read(1) log_to_client(output) _, exit_code = os.waitpid(child_pid, 0) if exit_code != 0: raise subprocess.CalledProcessError(exit_code, ' '.join(args[:-1]))
def pty_fork(*args): """Runs a subprocess with a PTY attached via fork and exec. The output from the PTY is streamed through log_to_client. This should not be necessary for most subprocesses, we built this to handle Compose up which only streams pull progress if it is attached to a TTY.""" updated_env = copy(os.environ) updated_env.update(get_docker_env()) args += (updated_env,) executable = args[0] demote_fn = demote_to_user(get_config_value(constants.CONFIG_MAC_USERNAME_KEY)) child_pid, pty_fd = pty.fork() if child_pid == 0: demote_fn() os.execle(_executable_path(executable), *args) else: child_process = psutil.Process(child_pid) terminal = os.fdopen(pty_fd, 'r', 0) with streaming_to_client(): while child_process.status() == 'running': output = terminal.read(1) log_to_client(output) _, exit_code = os.waitpid(child_pid, 0) if exit_code != 0: raise subprocess.CalledProcessError(exit_code, ' '.join(args[:-1]))
[ "Runs", "a", "subprocess", "with", "a", "PTY", "attached", "via", "fork", "and", "exec", ".", "The", "output", "from", "the", "PTY", "is", "streamed", "through", "log_to_client", ".", "This", "should", "not", "be", "necessary", "for", "most", "subprocesses", "we", "built", "this", "to", "handle", "Compose", "up", "which", "only", "streams", "pull", "progress", "if", "it", "is", "attached", "to", "a", "TTY", "." ]
gamechanger/dusty
python
https://github.com/gamechanger/dusty/blob/dc12de90bb6945023d6f43a8071e984313a1d984/dusty/commands/utils.py#L30-L56
[ "def", "pty_fork", "(", "*", "args", ")", ":", "updated_env", "=", "copy", "(", "os", ".", "environ", ")", "updated_env", ".", "update", "(", "get_docker_env", "(", ")", ")", "args", "+=", "(", "updated_env", ",", ")", "executable", "=", "args", "[", "0", "]", "demote_fn", "=", "demote_to_user", "(", "get_config_value", "(", "constants", ".", "CONFIG_MAC_USERNAME_KEY", ")", ")", "child_pid", ",", "pty_fd", "=", "pty", ".", "fork", "(", ")", "if", "child_pid", "==", "0", ":", "demote_fn", "(", ")", "os", ".", "execle", "(", "_executable_path", "(", "executable", ")", ",", "*", "args", ")", "else", ":", "child_process", "=", "psutil", ".", "Process", "(", "child_pid", ")", "terminal", "=", "os", ".", "fdopen", "(", "pty_fd", ",", "'r'", ",", "0", ")", "with", "streaming_to_client", "(", ")", ":", "while", "child_process", ".", "status", "(", ")", "==", "'running'", ":", "output", "=", "terminal", ".", "read", "(", "1", ")", "log_to_client", "(", "output", ")", "_", ",", "exit_code", "=", "os", ".", "waitpid", "(", "child_pid", ",", "0", ")", "if", "exit_code", "!=", "0", ":", "raise", "subprocess", ".", "CalledProcessError", "(", "exit_code", ",", "' '", ".", "join", "(", "args", "[", ":", "-", "1", "]", ")", ")" ]
dc12de90bb6945023d6f43a8071e984313a1d984
valid
_compile_docker_commands
This is used to compile the command that will be run when the docker container starts up. This command has to install any libs that the app uses, run the `always` command, and run the `once` command if the container is being launched for the first time
dusty/command_file.py
def _compile_docker_commands(app_name, assembled_specs, port_spec): """ This is used to compile the command that will be run when the docker container starts up. This command has to install any libs that the app uses, run the `always` command, and run the `once` command if the container is being launched for the first time """ app_spec = assembled_specs['apps'][app_name] commands = ['set -e'] commands += _lib_install_commands_for_app(app_name, assembled_specs) if app_spec['mount']: commands.append("cd {}".format(container_code_path(app_spec))) commands.append("export PATH=$PATH:{}".format(container_code_path(app_spec))) commands += _copy_assets_commands_for_app(app_spec, assembled_specs) commands += _get_once_commands(app_spec, port_spec) commands += _get_always_commands(app_spec) return commands
def _compile_docker_commands(app_name, assembled_specs, port_spec): """ This is used to compile the command that will be run when the docker container starts up. This command has to install any libs that the app uses, run the `always` command, and run the `once` command if the container is being launched for the first time """ app_spec = assembled_specs['apps'][app_name] commands = ['set -e'] commands += _lib_install_commands_for_app(app_name, assembled_specs) if app_spec['mount']: commands.append("cd {}".format(container_code_path(app_spec))) commands.append("export PATH=$PATH:{}".format(container_code_path(app_spec))) commands += _copy_assets_commands_for_app(app_spec, assembled_specs) commands += _get_once_commands(app_spec, port_spec) commands += _get_always_commands(app_spec) return commands
[ "This", "is", "used", "to", "compile", "the", "command", "that", "will", "be", "run", "when", "the", "docker", "container", "starts", "up", ".", "This", "command", "has", "to", "install", "any", "libs", "that", "the", "app", "uses", "run", "the", "always", "command", "and", "run", "the", "once", "command", "if", "the", "container", "is", "being", "launched", "for", "the", "first", "time" ]
gamechanger/dusty
python
https://github.com/gamechanger/dusty/blob/dc12de90bb6945023d6f43a8071e984313a1d984/dusty/command_file.py#L91-L104
[ "def", "_compile_docker_commands", "(", "app_name", ",", "assembled_specs", ",", "port_spec", ")", ":", "app_spec", "=", "assembled_specs", "[", "'apps'", "]", "[", "app_name", "]", "commands", "=", "[", "'set -e'", "]", "commands", "+=", "_lib_install_commands_for_app", "(", "app_name", ",", "assembled_specs", ")", "if", "app_spec", "[", "'mount'", "]", ":", "commands", ".", "append", "(", "\"cd {}\"", ".", "format", "(", "container_code_path", "(", "app_spec", ")", ")", ")", "commands", ".", "append", "(", "\"export PATH=$PATH:{}\"", ".", "format", "(", "container_code_path", "(", "app_spec", ")", ")", ")", "commands", "+=", "_copy_assets_commands_for_app", "(", "app_spec", ",", "assembled_specs", ")", "commands", "+=", "_get_once_commands", "(", "app_spec", ",", "port_spec", ")", "commands", "+=", "_get_always_commands", "(", "app_spec", ")", "return", "commands" ]
dc12de90bb6945023d6f43a8071e984313a1d984
valid
_increase_file_handle_limit
Raise the open file handles permitted by the Dusty daemon process and its child processes. The number we choose here needs to be within the OS X default kernel hard limit, which is 10240.
dusty/daemon.py
def _increase_file_handle_limit(): """Raise the open file handles permitted by the Dusty daemon process and its child processes. The number we choose here needs to be within the OS X default kernel hard limit, which is 10240.""" logging.info('Increasing file handle limit to {}'.format(constants.FILE_HANDLE_LIMIT)) resource.setrlimit(resource.RLIMIT_NOFILE, (constants.FILE_HANDLE_LIMIT, resource.RLIM_INFINITY))
def _increase_file_handle_limit(): """Raise the open file handles permitted by the Dusty daemon process and its child processes. The number we choose here needs to be within the OS X default kernel hard limit, which is 10240.""" logging.info('Increasing file handle limit to {}'.format(constants.FILE_HANDLE_LIMIT)) resource.setrlimit(resource.RLIMIT_NOFILE, (constants.FILE_HANDLE_LIMIT, resource.RLIM_INFINITY))
[ "Raise", "the", "open", "file", "handles", "permitted", "by", "the", "Dusty", "daemon", "process", "and", "its", "child", "processes", ".", "The", "number", "we", "choose", "here", "needs", "to", "be", "within", "the", "OS", "X", "default", "kernel", "hard", "limit", "which", "is", "10240", "." ]
gamechanger/dusty
python
https://github.com/gamechanger/dusty/blob/dc12de90bb6945023d6f43a8071e984313a1d984/dusty/daemon.py#L68-L74
[ "def", "_increase_file_handle_limit", "(", ")", ":", "logging", ".", "info", "(", "'Increasing file handle limit to {}'", ".", "format", "(", "constants", ".", "FILE_HANDLE_LIMIT", ")", ")", "resource", ".", "setrlimit", "(", "resource", ".", "RLIMIT_NOFILE", ",", "(", "constants", ".", "FILE_HANDLE_LIMIT", ",", "resource", ".", "RLIM_INFINITY", ")", ")" ]
dc12de90bb6945023d6f43a8071e984313a1d984
valid
_start_http_server
Start the daemon's HTTP server on a separate thread. This server is only used for servicing container status requests from Dusty's custom 502 page.
dusty/daemon.py
def _start_http_server(): """Start the daemon's HTTP server on a separate thread. This server is only used for servicing container status requests from Dusty's custom 502 page.""" logging.info('Starting HTTP server at {}:{}'.format(constants.DAEMON_HTTP_BIND_IP, constants.DAEMON_HTTP_BIND_PORT)) thread = threading.Thread(target=http_server.app.run, args=(constants.DAEMON_HTTP_BIND_IP, constants.DAEMON_HTTP_BIND_PORT)) thread.daemon = True thread.start()
def _start_http_server(): """Start the daemon's HTTP server on a separate thread. This server is only used for servicing container status requests from Dusty's custom 502 page.""" logging.info('Starting HTTP server at {}:{}'.format(constants.DAEMON_HTTP_BIND_IP, constants.DAEMON_HTTP_BIND_PORT)) thread = threading.Thread(target=http_server.app.run, args=(constants.DAEMON_HTTP_BIND_IP, constants.DAEMON_HTTP_BIND_PORT)) thread.daemon = True thread.start()
[ "Start", "the", "daemon", "s", "HTTP", "server", "on", "a", "separate", "thread", ".", "This", "server", "is", "only", "used", "for", "servicing", "container", "status", "requests", "from", "Dusty", "s", "custom", "502", "page", "." ]
gamechanger/dusty
python
https://github.com/gamechanger/dusty/blob/dc12de90bb6945023d6f43a8071e984313a1d984/dusty/daemon.py#L88-L97
[ "def", "_start_http_server", "(", ")", ":", "logging", ".", "info", "(", "'Starting HTTP server at {}:{}'", ".", "format", "(", "constants", ".", "DAEMON_HTTP_BIND_IP", ",", "constants", ".", "DAEMON_HTTP_BIND_PORT", ")", ")", "thread", "=", "threading", ".", "Thread", "(", "target", "=", "http_server", ".", "app", ".", "run", ",", "args", "=", "(", "constants", ".", "DAEMON_HTTP_BIND_IP", ",", "constants", ".", "DAEMON_HTTP_BIND_PORT", ")", ")", "thread", ".", "daemon", "=", "True", "thread", ".", "start", "(", ")" ]
dc12de90bb6945023d6f43a8071e984313a1d984
valid
get_dusty_images
Returns all images listed in dusty specs (apps + bundles), in the form repository:tag. Tag will be set to latest if no tag is specified in the specs
dusty/systems/docker/__init__.py
def get_dusty_images(): """Returns all images listed in dusty specs (apps + bundles), in the form repository:tag. Tag will be set to latest if no tag is specified in the specs""" specs = get_specs() dusty_image_names = [spec['image'] for spec in specs['apps'].values() + specs['services'].values() if 'image' in spec] dusty_images = set([name if ':' in name else "{}:latest".format(name) for name in dusty_image_names]) return dusty_images
def get_dusty_images(): """Returns all images listed in dusty specs (apps + bundles), in the form repository:tag. Tag will be set to latest if no tag is specified in the specs""" specs = get_specs() dusty_image_names = [spec['image'] for spec in specs['apps'].values() + specs['services'].values() if 'image' in spec] dusty_images = set([name if ':' in name else "{}:latest".format(name) for name in dusty_image_names]) return dusty_images
[ "Returns", "all", "images", "listed", "in", "dusty", "specs", "(", "apps", "+", "bundles", ")", "in", "the", "form", "repository", ":", "tag", ".", "Tag", "will", "be", "set", "to", "latest", "if", "no", "tag", "is", "specified", "in", "the", "specs" ]
gamechanger/dusty
python
https://github.com/gamechanger/dusty/blob/dc12de90bb6945023d6f43a8071e984313a1d984/dusty/systems/docker/__init__.py#L18-L24
[ "def", "get_dusty_images", "(", ")", ":", "specs", "=", "get_specs", "(", ")", "dusty_image_names", "=", "[", "spec", "[", "'image'", "]", "for", "spec", "in", "specs", "[", "'apps'", "]", ".", "values", "(", ")", "+", "specs", "[", "'services'", "]", ".", "values", "(", ")", "if", "'image'", "in", "spec", "]", "dusty_images", "=", "set", "(", "[", "name", "if", "':'", "in", "name", "else", "\"{}:latest\"", ".", "format", "(", "name", ")", "for", "name", "in", "dusty_image_names", "]", ")", "return", "dusty_images" ]
dc12de90bb6945023d6f43a8071e984313a1d984
valid
get_docker_client
Ripped off and slightly modified based on docker-py's kwargs_from_env utility function.
dusty/systems/docker/__init__.py
def get_docker_client(): """Ripped off and slightly modified based on docker-py's kwargs_from_env utility function.""" env = get_docker_env() host, cert_path, tls_verify = env['DOCKER_HOST'], env['DOCKER_CERT_PATH'], env['DOCKER_TLS_VERIFY'] params = {'base_url': host.replace('tcp://', 'https://'), 'timeout': None, 'version': 'auto'} if tls_verify and cert_path: params['tls'] = docker.tls.TLSConfig( client_cert=(os.path.join(cert_path, 'cert.pem'), os.path.join(cert_path, 'key.pem')), ca_cert=os.path.join(cert_path, 'ca.pem'), verify=True, ssl_version=None, assert_hostname=False) return docker.Client(**params)
def get_docker_client(): """Ripped off and slightly modified based on docker-py's kwargs_from_env utility function.""" env = get_docker_env() host, cert_path, tls_verify = env['DOCKER_HOST'], env['DOCKER_CERT_PATH'], env['DOCKER_TLS_VERIFY'] params = {'base_url': host.replace('tcp://', 'https://'), 'timeout': None, 'version': 'auto'} if tls_verify and cert_path: params['tls'] = docker.tls.TLSConfig( client_cert=(os.path.join(cert_path, 'cert.pem'), os.path.join(cert_path, 'key.pem')), ca_cert=os.path.join(cert_path, 'ca.pem'), verify=True, ssl_version=None, assert_hostname=False) return docker.Client(**params)
[ "Ripped", "off", "and", "slightly", "modified", "based", "on", "docker", "-", "py", "s", "kwargs_from_env", "utility", "function", "." ]
gamechanger/dusty
python
https://github.com/gamechanger/dusty/blob/dc12de90bb6945023d6f43a8071e984313a1d984/dusty/systems/docker/__init__.py#L42-L59
[ "def", "get_docker_client", "(", ")", ":", "env", "=", "get_docker_env", "(", ")", "host", ",", "cert_path", ",", "tls_verify", "=", "env", "[", "'DOCKER_HOST'", "]", ",", "env", "[", "'DOCKER_CERT_PATH'", "]", ",", "env", "[", "'DOCKER_TLS_VERIFY'", "]", "params", "=", "{", "'base_url'", ":", "host", ".", "replace", "(", "'tcp://'", ",", "'https://'", ")", ",", "'timeout'", ":", "None", ",", "'version'", ":", "'auto'", "}", "if", "tls_verify", "and", "cert_path", ":", "params", "[", "'tls'", "]", "=", "docker", ".", "tls", ".", "TLSConfig", "(", "client_cert", "=", "(", "os", ".", "path", ".", "join", "(", "cert_path", ",", "'cert.pem'", ")", ",", "os", ".", "path", ".", "join", "(", "cert_path", ",", "'key.pem'", ")", ")", ",", "ca_cert", "=", "os", ".", "path", ".", "join", "(", "cert_path", ",", "'ca.pem'", ")", ",", "verify", "=", "True", ",", "ssl_version", "=", "None", ",", "assert_hostname", "=", "False", ")", "return", "docker", ".", "Client", "(", "*", "*", "params", ")" ]
dc12de90bb6945023d6f43a8071e984313a1d984
valid
get_dusty_containers
Get a list of containers associated with the list of services. If no services are provided, attempts to return all containers associated with Dusty.
dusty/systems/docker/__init__.py
def get_dusty_containers(services, include_exited=False): """Get a list of containers associated with the list of services. If no services are provided, attempts to return all containers associated with Dusty.""" client = get_docker_client() if services: containers = [get_container_for_app_or_service(service, include_exited=include_exited) for service in services] return [container for container in containers if container] else: return [container for container in client.containers(all=include_exited) if any(name.startswith('/dusty') for name in container.get('Names', []))]
def get_dusty_containers(services, include_exited=False): """Get a list of containers associated with the list of services. If no services are provided, attempts to return all containers associated with Dusty.""" client = get_docker_client() if services: containers = [get_container_for_app_or_service(service, include_exited=include_exited) for service in services] return [container for container in containers if container] else: return [container for container in client.containers(all=include_exited) if any(name.startswith('/dusty') for name in container.get('Names', []))]
[ "Get", "a", "list", "of", "containers", "associated", "with", "the", "list", "of", "services", ".", "If", "no", "services", "are", "provided", "attempts", "to", "return", "all", "containers", "associated", "with", "Dusty", "." ]
gamechanger/dusty
python
https://github.com/gamechanger/dusty/blob/dc12de90bb6945023d6f43a8071e984313a1d984/dusty/systems/docker/__init__.py#L61-L72
[ "def", "get_dusty_containers", "(", "services", ",", "include_exited", "=", "False", ")", ":", "client", "=", "get_docker_client", "(", ")", "if", "services", ":", "containers", "=", "[", "get_container_for_app_or_service", "(", "service", ",", "include_exited", "=", "include_exited", ")", "for", "service", "in", "services", "]", "return", "[", "container", "for", "container", "in", "containers", "if", "container", "]", "else", ":", "return", "[", "container", "for", "container", "in", "client", ".", "containers", "(", "all", "=", "include_exited", ")", "if", "any", "(", "name", ".", "startswith", "(", "'/dusty'", ")", "for", "name", "in", "container", ".", "get", "(", "'Names'", ",", "[", "]", ")", ")", "]" ]
dc12de90bb6945023d6f43a8071e984313a1d984
valid
configure_nfs_server
This function is used with `dusty up`. It will check all active repos to see if they are exported. If any are missing, it will replace current dusty exports with exports that are needed for currently active repos, and restart the nfs server
dusty/systems/nfs/server.py
def configure_nfs_server(): """ This function is used with `dusty up`. It will check all active repos to see if they are exported. If any are missing, it will replace current dusty exports with exports that are needed for currently active repos, and restart the nfs server """ repos_for_export = get_all_repos(active_only=True, include_specs_repo=False) current_exports = _get_current_exports() needed_exports = _get_exports_for_repos(repos_for_export) _ensure_managed_repos_dir_exists() if not needed_exports.difference(current_exports): if not _server_is_running(): _restart_server() return _write_exports_config(needed_exports) _restart_server()
def configure_nfs_server(): """ This function is used with `dusty up`. It will check all active repos to see if they are exported. If any are missing, it will replace current dusty exports with exports that are needed for currently active repos, and restart the nfs server """ repos_for_export = get_all_repos(active_only=True, include_specs_repo=False) current_exports = _get_current_exports() needed_exports = _get_exports_for_repos(repos_for_export) _ensure_managed_repos_dir_exists() if not needed_exports.difference(current_exports): if not _server_is_running(): _restart_server() return _write_exports_config(needed_exports) _restart_server()
[ "This", "function", "is", "used", "with", "dusty", "up", ".", "It", "will", "check", "all", "active", "repos", "to", "see", "if", "they", "are", "exported", ".", "If", "any", "are", "missing", "it", "will", "replace", "current", "dusty", "exports", "with", "exports", "that", "are", "needed", "for", "currently", "active", "repos", "and", "restart", "the", "nfs", "server" ]
gamechanger/dusty
python
https://github.com/gamechanger/dusty/blob/dc12de90bb6945023d6f43a8071e984313a1d984/dusty/systems/nfs/server.py#L15-L35
[ "def", "configure_nfs_server", "(", ")", ":", "repos_for_export", "=", "get_all_repos", "(", "active_only", "=", "True", ",", "include_specs_repo", "=", "False", ")", "current_exports", "=", "_get_current_exports", "(", ")", "needed_exports", "=", "_get_exports_for_repos", "(", "repos_for_export", ")", "_ensure_managed_repos_dir_exists", "(", ")", "if", "not", "needed_exports", ".", "difference", "(", "current_exports", ")", ":", "if", "not", "_server_is_running", "(", ")", ":", "_restart_server", "(", ")", "return", "_write_exports_config", "(", "needed_exports", ")", "_restart_server", "(", ")" ]
dc12de90bb6945023d6f43a8071e984313a1d984
valid
add_exports_for_repos
This function will add needed entries to /etc/exports. It will not remove any entries from the file. It will then restart the server if necessary
dusty/systems/nfs/server.py
def add_exports_for_repos(repos): """ This function will add needed entries to /etc/exports. It will not remove any entries from the file. It will then restart the server if necessary """ current_exports = _get_current_exports() needed_exports = _get_exports_for_repos(repos) if not needed_exports.difference(current_exports): if not _server_is_running(): _restart_server() return _write_exports_config(current_exports.union(needed_exports)) _restart_server()
def add_exports_for_repos(repos): """ This function will add needed entries to /etc/exports. It will not remove any entries from the file. It will then restart the server if necessary """ current_exports = _get_current_exports() needed_exports = _get_exports_for_repos(repos) if not needed_exports.difference(current_exports): if not _server_is_running(): _restart_server() return _write_exports_config(current_exports.union(needed_exports)) _restart_server()
[ "This", "function", "will", "add", "needed", "entries", "to", "/", "etc", "/", "exports", ".", "It", "will", "not", "remove", "any", "entries", "from", "the", "file", ".", "It", "will", "then", "restart", "the", "server", "if", "necessary" ]
gamechanger/dusty
python
https://github.com/gamechanger/dusty/blob/dc12de90bb6945023d6f43a8071e984313a1d984/dusty/systems/nfs/server.py#L37-L51
[ "def", "add_exports_for_repos", "(", "repos", ")", ":", "current_exports", "=", "_get_current_exports", "(", ")", "needed_exports", "=", "_get_exports_for_repos", "(", "repos", ")", "if", "not", "needed_exports", ".", "difference", "(", "current_exports", ")", ":", "if", "not", "_server_is_running", "(", ")", ":", "_restart_server", "(", ")", "return", "_write_exports_config", "(", "current_exports", ".", "union", "(", "needed_exports", ")", ")", "_restart_server", "(", ")" ]
dc12de90bb6945023d6f43a8071e984313a1d984
valid
_ensure_managed_repos_dir_exists
Our exports file will be invalid if this folder doesn't exist, and the NFS server will not run correctly.
dusty/systems/nfs/server.py
def _ensure_managed_repos_dir_exists(): """ Our exports file will be invalid if this folder doesn't exist, and the NFS server will not run correctly. """ if not os.path.exists(constants.REPOS_DIR): os.makedirs(constants.REPOS_DIR)
def _ensure_managed_repos_dir_exists(): """ Our exports file will be invalid if this folder doesn't exist, and the NFS server will not run correctly. """ if not os.path.exists(constants.REPOS_DIR): os.makedirs(constants.REPOS_DIR)
[ "Our", "exports", "file", "will", "be", "invalid", "if", "this", "folder", "doesn", "t", "exist", "and", "the", "NFS", "server", "will", "not", "run", "correctly", "." ]
gamechanger/dusty
python
https://github.com/gamechanger/dusty/blob/dc12de90bb6945023d6f43a8071e984313a1d984/dusty/systems/nfs/server.py#L53-L59
[ "def", "_ensure_managed_repos_dir_exists", "(", ")", ":", "if", "not", "os", ".", "path", ".", "exists", "(", "constants", ".", "REPOS_DIR", ")", ":", "os", ".", "makedirs", "(", "constants", ".", "REPOS_DIR", ")" ]
dc12de90bb6945023d6f43a8071e984313a1d984
valid
register_consumer
Given a hostname and port attempting to be accessed, return a unique consumer ID for accessing logs from the referenced container.
dusty/http_server.py
def register_consumer(): """Given a hostname and port attempting to be accessed, return a unique consumer ID for accessing logs from the referenced container.""" global _consumers hostname, port = request.form['hostname'], request.form['port'] app_name = _app_name_from_forwarding_info(hostname, port) containers = get_dusty_containers([app_name], include_exited=True) if not containers: raise ValueError('No container exists for app {}'.format(app_name)) container = containers[0] new_id = uuid1() new_consumer = Consumer(container['Id'], datetime.utcnow()) _consumers[str(new_id)] = new_consumer response = jsonify({'app_name': app_name, 'consumer_id': new_id}) response.headers['Access-Control-Allow-Origin'] = '*' response.headers['Access-Control-Allow-Methods'] = 'GET, POST' return response
def register_consumer(): """Given a hostname and port attempting to be accessed, return a unique consumer ID for accessing logs from the referenced container.""" global _consumers hostname, port = request.form['hostname'], request.form['port'] app_name = _app_name_from_forwarding_info(hostname, port) containers = get_dusty_containers([app_name], include_exited=True) if not containers: raise ValueError('No container exists for app {}'.format(app_name)) container = containers[0] new_id = uuid1() new_consumer = Consumer(container['Id'], datetime.utcnow()) _consumers[str(new_id)] = new_consumer response = jsonify({'app_name': app_name, 'consumer_id': new_id}) response.headers['Access-Control-Allow-Origin'] = '*' response.headers['Access-Control-Allow-Methods'] = 'GET, POST' return response
[ "Given", "a", "hostname", "and", "port", "attempting", "to", "be", "accessed", "return", "a", "unique", "consumer", "ID", "for", "accessing", "logs", "from", "the", "referenced", "container", "." ]
gamechanger/dusty
python
https://github.com/gamechanger/dusty/blob/dc12de90bb6945023d6f43a8071e984313a1d984/dusty/http_server.py#L38-L58
[ "def", "register_consumer", "(", ")", ":", "global", "_consumers", "hostname", ",", "port", "=", "request", ".", "form", "[", "'hostname'", "]", ",", "request", ".", "form", "[", "'port'", "]", "app_name", "=", "_app_name_from_forwarding_info", "(", "hostname", ",", "port", ")", "containers", "=", "get_dusty_containers", "(", "[", "app_name", "]", ",", "include_exited", "=", "True", ")", "if", "not", "containers", ":", "raise", "ValueError", "(", "'No container exists for app {}'", ".", "format", "(", "app_name", ")", ")", "container", "=", "containers", "[", "0", "]", "new_id", "=", "uuid1", "(", ")", "new_consumer", "=", "Consumer", "(", "container", "[", "'Id'", "]", ",", "datetime", ".", "utcnow", "(", ")", ")", "_consumers", "[", "str", "(", "new_id", ")", "]", "=", "new_consumer", "response", "=", "jsonify", "(", "{", "'app_name'", ":", "app_name", ",", "'consumer_id'", ":", "new_id", "}", ")", "response", ".", "headers", "[", "'Access-Control-Allow-Origin'", "]", "=", "'*'", "response", ".", "headers", "[", "'Access-Control-Allow-Methods'", "]", "=", "'GET, POST'", "return", "response" ]
dc12de90bb6945023d6f43a8071e984313a1d984
valid
consume
Given an existing consumer ID, return any new lines from the log since the last time the consumer was consumed.
dusty/http_server.py
def consume(consumer_id): """Given an existing consumer ID, return any new lines from the log since the last time the consumer was consumed.""" global _consumers consumer = _consumers[consumer_id] client = get_docker_client() try: status = client.inspect_container(consumer.container_id)['State']['Status'] except Exception as e: status = 'unknown' new_logs = client.logs(consumer.container_id, stdout=True, stderr=True, stream=False, timestamps=False, since=calendar.timegm(consumer.offset.timetuple())) updated_consumer = Consumer(consumer.container_id, datetime.utcnow()) _consumers[str(consumer_id)] = updated_consumer response = jsonify({'logs': new_logs, 'status': status}) response.headers['Access-Control-Allow-Origin'] = '*' response.headers['Access-Control-Allow-Methods'] = 'GET, POST' return response
def consume(consumer_id): """Given an existing consumer ID, return any new lines from the log since the last time the consumer was consumed.""" global _consumers consumer = _consumers[consumer_id] client = get_docker_client() try: status = client.inspect_container(consumer.container_id)['State']['Status'] except Exception as e: status = 'unknown' new_logs = client.logs(consumer.container_id, stdout=True, stderr=True, stream=False, timestamps=False, since=calendar.timegm(consumer.offset.timetuple())) updated_consumer = Consumer(consumer.container_id, datetime.utcnow()) _consumers[str(consumer_id)] = updated_consumer response = jsonify({'logs': new_logs, 'status': status}) response.headers['Access-Control-Allow-Origin'] = '*' response.headers['Access-Control-Allow-Methods'] = 'GET, POST' return response
[ "Given", "an", "existing", "consumer", "ID", "return", "any", "new", "lines", "from", "the", "log", "since", "the", "last", "time", "the", "consumer", "was", "consumed", "." ]
gamechanger/dusty
python
https://github.com/gamechanger/dusty/blob/dc12de90bb6945023d6f43a8071e984313a1d984/dusty/http_server.py#L61-L85
[ "def", "consume", "(", "consumer_id", ")", ":", "global", "_consumers", "consumer", "=", "_consumers", "[", "consumer_id", "]", "client", "=", "get_docker_client", "(", ")", "try", ":", "status", "=", "client", ".", "inspect_container", "(", "consumer", ".", "container_id", ")", "[", "'State'", "]", "[", "'Status'", "]", "except", "Exception", "as", "e", ":", "status", "=", "'unknown'", "new_logs", "=", "client", ".", "logs", "(", "consumer", ".", "container_id", ",", "stdout", "=", "True", ",", "stderr", "=", "True", ",", "stream", "=", "False", ",", "timestamps", "=", "False", ",", "since", "=", "calendar", ".", "timegm", "(", "consumer", ".", "offset", ".", "timetuple", "(", ")", ")", ")", "updated_consumer", "=", "Consumer", "(", "consumer", ".", "container_id", ",", "datetime", ".", "utcnow", "(", ")", ")", "_consumers", "[", "str", "(", "consumer_id", ")", "]", "=", "updated_consumer", "response", "=", "jsonify", "(", "{", "'logs'", ":", "new_logs", ",", "'status'", ":", "status", "}", ")", "response", ".", "headers", "[", "'Access-Control-Allow-Origin'", "]", "=", "'*'", "response", ".", "headers", "[", "'Access-Control-Allow-Methods'", "]", "=", "'GET, POST'", "return", "response" ]
dc12de90bb6945023d6f43a8071e984313a1d984
valid
get_app_volume_mounts
This returns a list of formatted volume specs for an app. These mounts declared in the apps' spec and mounts declared in all lib specs the app depends on
dusty/compiler/compose/common.py
def get_app_volume_mounts(app_name, assembled_specs, test=False): """ This returns a list of formatted volume specs for an app. These mounts declared in the apps' spec and mounts declared in all lib specs the app depends on""" app_spec = assembled_specs['apps'][app_name] volumes = [get_command_files_volume_mount(app_name, test=test)] volumes.append(get_asset_volume_mount(app_name)) repo_mount = _get_app_repo_volume_mount(app_spec) if repo_mount: volumes.append(repo_mount) volumes += _get_app_libs_volume_mounts(app_name, assembled_specs) return volumes
def get_app_volume_mounts(app_name, assembled_specs, test=False): """ This returns a list of formatted volume specs for an app. These mounts declared in the apps' spec and mounts declared in all lib specs the app depends on""" app_spec = assembled_specs['apps'][app_name] volumes = [get_command_files_volume_mount(app_name, test=test)] volumes.append(get_asset_volume_mount(app_name)) repo_mount = _get_app_repo_volume_mount(app_spec) if repo_mount: volumes.append(repo_mount) volumes += _get_app_libs_volume_mounts(app_name, assembled_specs) return volumes
[ "This", "returns", "a", "list", "of", "formatted", "volume", "specs", "for", "an", "app", ".", "These", "mounts", "declared", "in", "the", "apps", "spec", "and", "mounts", "declared", "in", "all", "lib", "specs", "the", "app", "depends", "on" ]
gamechanger/dusty
python
https://github.com/gamechanger/dusty/blob/dc12de90bb6945023d6f43a8071e984313a1d984/dusty/compiler/compose/common.py#L18-L28
[ "def", "get_app_volume_mounts", "(", "app_name", ",", "assembled_specs", ",", "test", "=", "False", ")", ":", "app_spec", "=", "assembled_specs", "[", "'apps'", "]", "[", "app_name", "]", "volumes", "=", "[", "get_command_files_volume_mount", "(", "app_name", ",", "test", "=", "test", ")", "]", "volumes", ".", "append", "(", "get_asset_volume_mount", "(", "app_name", ")", ")", "repo_mount", "=", "_get_app_repo_volume_mount", "(", "app_spec", ")", "if", "repo_mount", ":", "volumes", ".", "append", "(", "repo_mount", ")", "volumes", "+=", "_get_app_libs_volume_mounts", "(", "app_name", ",", "assembled_specs", ")", "return", "volumes" ]
dc12de90bb6945023d6f43a8071e984313a1d984
valid
get_lib_volume_mounts
Returns a list of the formatted volume specs for a lib
dusty/compiler/compose/common.py
def get_lib_volume_mounts(base_lib_name, assembled_specs): """ Returns a list of the formatted volume specs for a lib""" volumes = [_get_lib_repo_volume_mount(assembled_specs['libs'][base_lib_name])] volumes.append(get_command_files_volume_mount(base_lib_name, test=True)) for lib_name in assembled_specs['libs'][base_lib_name]['depends']['libs']: lib_spec = assembled_specs['libs'][lib_name] volumes.append(_get_lib_repo_volume_mount(lib_spec)) return volumes
def get_lib_volume_mounts(base_lib_name, assembled_specs): """ Returns a list of the formatted volume specs for a lib""" volumes = [_get_lib_repo_volume_mount(assembled_specs['libs'][base_lib_name])] volumes.append(get_command_files_volume_mount(base_lib_name, test=True)) for lib_name in assembled_specs['libs'][base_lib_name]['depends']['libs']: lib_spec = assembled_specs['libs'][lib_name] volumes.append(_get_lib_repo_volume_mount(lib_spec)) return volumes
[ "Returns", "a", "list", "of", "the", "formatted", "volume", "specs", "for", "a", "lib" ]
gamechanger/dusty
python
https://github.com/gamechanger/dusty/blob/dc12de90bb6945023d6f43a8071e984313a1d984/dusty/compiler/compose/common.py#L30-L37
[ "def", "get_lib_volume_mounts", "(", "base_lib_name", ",", "assembled_specs", ")", ":", "volumes", "=", "[", "_get_lib_repo_volume_mount", "(", "assembled_specs", "[", "'libs'", "]", "[", "base_lib_name", "]", ")", "]", "volumes", ".", "append", "(", "get_command_files_volume_mount", "(", "base_lib_name", ",", "test", "=", "True", ")", ")", "for", "lib_name", "in", "assembled_specs", "[", "'libs'", "]", "[", "base_lib_name", "]", "[", "'depends'", "]", "[", "'libs'", "]", ":", "lib_spec", "=", "assembled_specs", "[", "'libs'", "]", "[", "lib_name", "]", "volumes", ".", "append", "(", "_get_lib_repo_volume_mount", "(", "lib_spec", ")", ")", "return", "volumes" ]
dc12de90bb6945023d6f43a8071e984313a1d984
valid
_get_app_libs_volume_mounts
Returns a list of the formatted volume mounts for all libs that an app uses
dusty/compiler/compose/common.py
def _get_app_libs_volume_mounts(app_name, assembled_specs): """ Returns a list of the formatted volume mounts for all libs that an app uses """ volumes = [] for lib_name in assembled_specs['apps'][app_name]['depends']['libs']: lib_spec = assembled_specs['libs'][lib_name] volumes.append("{}:{}".format(Repo(lib_spec['repo']).vm_path, container_code_path(lib_spec))) return volumes
def _get_app_libs_volume_mounts(app_name, assembled_specs): """ Returns a list of the formatted volume mounts for all libs that an app uses """ volumes = [] for lib_name in assembled_specs['apps'][app_name]['depends']['libs']: lib_spec = assembled_specs['libs'][lib_name] volumes.append("{}:{}".format(Repo(lib_spec['repo']).vm_path, container_code_path(lib_spec))) return volumes
[ "Returns", "a", "list", "of", "the", "formatted", "volume", "mounts", "for", "all", "libs", "that", "an", "app", "uses" ]
gamechanger/dusty
python
https://github.com/gamechanger/dusty/blob/dc12de90bb6945023d6f43a8071e984313a1d984/dusty/compiler/compose/common.py#L55-L61
[ "def", "_get_app_libs_volume_mounts", "(", "app_name", ",", "assembled_specs", ")", ":", "volumes", "=", "[", "]", "for", "lib_name", "in", "assembled_specs", "[", "'apps'", "]", "[", "app_name", "]", "[", "'depends'", "]", "[", "'libs'", "]", ":", "lib_spec", "=", "assembled_specs", "[", "'libs'", "]", "[", "lib_name", "]", "volumes", ".", "append", "(", "\"{}:{}\"", ".", "format", "(", "Repo", "(", "lib_spec", "[", "'repo'", "]", ")", ".", "vm_path", ",", "container_code_path", "(", "lib_spec", ")", ")", ")", "return", "volumes" ]
dc12de90bb6945023d6f43a8071e984313a1d984
valid
_dusty_vm_exists
We use VBox directly instead of Docker Machine because it shaves about 0.5 seconds off the runtime of this check.
dusty/systems/virtualbox/__init__.py
def _dusty_vm_exists(): """We use VBox directly instead of Docker Machine because it shaves about 0.5 seconds off the runtime of this check.""" existing_vms = check_output_demoted(['VBoxManage', 'list', 'vms']) for line in existing_vms.splitlines(): if '"{}"'.format(constants.VM_MACHINE_NAME) in line: return True return False
def _dusty_vm_exists(): """We use VBox directly instead of Docker Machine because it shaves about 0.5 seconds off the runtime of this check.""" existing_vms = check_output_demoted(['VBoxManage', 'list', 'vms']) for line in existing_vms.splitlines(): if '"{}"'.format(constants.VM_MACHINE_NAME) in line: return True return False
[ "We", "use", "VBox", "directly", "instead", "of", "Docker", "Machine", "because", "it", "shaves", "about", "0", ".", "5", "seconds", "off", "the", "runtime", "of", "this", "check", "." ]
gamechanger/dusty
python
https://github.com/gamechanger/dusty/blob/dc12de90bb6945023d6f43a8071e984313a1d984/dusty/systems/virtualbox/__init__.py#L64-L71
[ "def", "_dusty_vm_exists", "(", ")", ":", "existing_vms", "=", "check_output_demoted", "(", "[", "'VBoxManage'", ",", "'list'", ",", "'vms'", "]", ")", "for", "line", "in", "existing_vms", ".", "splitlines", "(", ")", ":", "if", "'\"{}\"'", ".", "format", "(", "constants", ".", "VM_MACHINE_NAME", ")", "in", "line", ":", "return", "True", "return", "False" ]
dc12de90bb6945023d6f43a8071e984313a1d984
valid
_init_docker_vm
Initialize the Dusty VM if it does not already exist.
dusty/systems/virtualbox/__init__.py
def _init_docker_vm(): """Initialize the Dusty VM if it does not already exist.""" if not _dusty_vm_exists(): log_to_client('Initializing new Dusty VM with Docker Machine') machine_options = ['--driver', 'virtualbox', '--virtualbox-cpu-count', '-1', '--virtualbox-boot2docker-url', constants.CONFIG_BOOT2DOCKER_URL, '--virtualbox-memory', str(get_config_value(constants.CONFIG_VM_MEM_SIZE)), '--virtualbox-hostonly-nictype', constants.VM_NIC_TYPE] check_call_demoted(['docker-machine', 'create'] + machine_options + [constants.VM_MACHINE_NAME], redirect_stderr=True)
def _init_docker_vm(): """Initialize the Dusty VM if it does not already exist.""" if not _dusty_vm_exists(): log_to_client('Initializing new Dusty VM with Docker Machine') machine_options = ['--driver', 'virtualbox', '--virtualbox-cpu-count', '-1', '--virtualbox-boot2docker-url', constants.CONFIG_BOOT2DOCKER_URL, '--virtualbox-memory', str(get_config_value(constants.CONFIG_VM_MEM_SIZE)), '--virtualbox-hostonly-nictype', constants.VM_NIC_TYPE] check_call_demoted(['docker-machine', 'create'] + machine_options + [constants.VM_MACHINE_NAME], redirect_stderr=True)
[ "Initialize", "the", "Dusty", "VM", "if", "it", "does", "not", "already", "exist", "." ]
gamechanger/dusty
python
https://github.com/gamechanger/dusty/blob/dc12de90bb6945023d6f43a8071e984313a1d984/dusty/systems/virtualbox/__init__.py#L93-L103
[ "def", "_init_docker_vm", "(", ")", ":", "if", "not", "_dusty_vm_exists", "(", ")", ":", "log_to_client", "(", "'Initializing new Dusty VM with Docker Machine'", ")", "machine_options", "=", "[", "'--driver'", ",", "'virtualbox'", ",", "'--virtualbox-cpu-count'", ",", "'-1'", ",", "'--virtualbox-boot2docker-url'", ",", "constants", ".", "CONFIG_BOOT2DOCKER_URL", ",", "'--virtualbox-memory'", ",", "str", "(", "get_config_value", "(", "constants", ".", "CONFIG_VM_MEM_SIZE", ")", ")", ",", "'--virtualbox-hostonly-nictype'", ",", "constants", ".", "VM_NIC_TYPE", "]", "check_call_demoted", "(", "[", "'docker-machine'", ",", "'create'", "]", "+", "machine_options", "+", "[", "constants", ".", "VM_MACHINE_NAME", "]", ",", "redirect_stderr", "=", "True", ")" ]
dc12de90bb6945023d6f43a8071e984313a1d984
valid
_start_docker_vm
Start the Dusty VM if it is not already running.
dusty/systems/virtualbox/__init__.py
def _start_docker_vm(): """Start the Dusty VM if it is not already running.""" is_running = docker_vm_is_running() if not is_running: log_to_client('Starting docker-machine VM {}'.format(constants.VM_MACHINE_NAME)) _apply_nat_dns_host_resolver() _apply_nat_net_less_greedy_subnet() check_and_log_output_and_error_demoted(['docker-machine', 'start', constants.VM_MACHINE_NAME], quiet_on_success=True) return is_running
def _start_docker_vm(): """Start the Dusty VM if it is not already running.""" is_running = docker_vm_is_running() if not is_running: log_to_client('Starting docker-machine VM {}'.format(constants.VM_MACHINE_NAME)) _apply_nat_dns_host_resolver() _apply_nat_net_less_greedy_subnet() check_and_log_output_and_error_demoted(['docker-machine', 'start', constants.VM_MACHINE_NAME], quiet_on_success=True) return is_running
[ "Start", "the", "Dusty", "VM", "if", "it", "is", "not", "already", "running", "." ]
gamechanger/dusty
python
https://github.com/gamechanger/dusty/blob/dc12de90bb6945023d6f43a8071e984313a1d984/dusty/systems/virtualbox/__init__.py#L105-L113
[ "def", "_start_docker_vm", "(", ")", ":", "is_running", "=", "docker_vm_is_running", "(", ")", "if", "not", "is_running", ":", "log_to_client", "(", "'Starting docker-machine VM {}'", ".", "format", "(", "constants", ".", "VM_MACHINE_NAME", ")", ")", "_apply_nat_dns_host_resolver", "(", ")", "_apply_nat_net_less_greedy_subnet", "(", ")", "check_and_log_output_and_error_demoted", "(", "[", "'docker-machine'", ",", "'start'", ",", "constants", ".", "VM_MACHINE_NAME", "]", ",", "quiet_on_success", "=", "True", ")", "return", "is_running" ]
dc12de90bb6945023d6f43a8071e984313a1d984
valid
docker_vm_is_running
Using VBoxManage is 0.5 seconds or so faster than Machine.
dusty/systems/virtualbox/__init__.py
def docker_vm_is_running(): """Using VBoxManage is 0.5 seconds or so faster than Machine.""" running_vms = check_output_demoted(['VBoxManage', 'list', 'runningvms']) for line in running_vms.splitlines(): if '"{}"'.format(constants.VM_MACHINE_NAME) in line: return True return False
def docker_vm_is_running(): """Using VBoxManage is 0.5 seconds or so faster than Machine.""" running_vms = check_output_demoted(['VBoxManage', 'list', 'runningvms']) for line in running_vms.splitlines(): if '"{}"'.format(constants.VM_MACHINE_NAME) in line: return True return False
[ "Using", "VBoxManage", "is", "0", ".", "5", "seconds", "or", "so", "faster", "than", "Machine", "." ]
gamechanger/dusty
python
https://github.com/gamechanger/dusty/blob/dc12de90bb6945023d6f43a8071e984313a1d984/dusty/systems/virtualbox/__init__.py#L123-L129
[ "def", "docker_vm_is_running", "(", ")", ":", "running_vms", "=", "check_output_demoted", "(", "[", "'VBoxManage'", ",", "'list'", ",", "'runningvms'", "]", ")", "for", "line", "in", "running_vms", ".", "splitlines", "(", ")", ":", "if", "'\"{}\"'", ".", "format", "(", "constants", ".", "VM_MACHINE_NAME", ")", "in", "line", ":", "return", "True", "return", "False" ]
dc12de90bb6945023d6f43a8071e984313a1d984
valid
_get_localhost_ssh_port
Something in the VM chain, either VirtualBox or Machine, helpfully sets up localhost-to-VM forwarding on port 22. We can inspect this rule to determine the port on localhost which gets forwarded to 22 in the VM.
dusty/systems/virtualbox/__init__.py
def _get_localhost_ssh_port(): """Something in the VM chain, either VirtualBox or Machine, helpfully sets up localhost-to-VM forwarding on port 22. We can inspect this rule to determine the port on localhost which gets forwarded to 22 in the VM.""" for line in _get_vm_config(): if line.startswith('Forwarding'): spec = line.split('=')[1].strip('"') name, protocol, host, host_port, target, target_port = spec.split(',') if name == 'ssh' and protocol == 'tcp' and target_port == '22': return host_port raise ValueError('Could not determine localhost port for SSH forwarding')
def _get_localhost_ssh_port(): """Something in the VM chain, either VirtualBox or Machine, helpfully sets up localhost-to-VM forwarding on port 22. We can inspect this rule to determine the port on localhost which gets forwarded to 22 in the VM.""" for line in _get_vm_config(): if line.startswith('Forwarding'): spec = line.split('=')[1].strip('"') name, protocol, host, host_port, target, target_port = spec.split(',') if name == 'ssh' and protocol == 'tcp' and target_port == '22': return host_port raise ValueError('Could not determine localhost port for SSH forwarding')
[ "Something", "in", "the", "VM", "chain", "either", "VirtualBox", "or", "Machine", "helpfully", "sets", "up", "localhost", "-", "to", "-", "VM", "forwarding", "on", "port", "22", ".", "We", "can", "inspect", "this", "rule", "to", "determine", "the", "port", "on", "localhost", "which", "gets", "forwarded", "to", "22", "in", "the", "VM", "." ]
gamechanger/dusty
python
https://github.com/gamechanger/dusty/blob/dc12de90bb6945023d6f43a8071e984313a1d984/dusty/systems/virtualbox/__init__.py#L194-L205
[ "def", "_get_localhost_ssh_port", "(", ")", ":", "for", "line", "in", "_get_vm_config", "(", ")", ":", "if", "line", ".", "startswith", "(", "'Forwarding'", ")", ":", "spec", "=", "line", ".", "split", "(", "'='", ")", "[", "1", "]", ".", "strip", "(", "'\"'", ")", "name", ",", "protocol", ",", "host", ",", "host_port", ",", "target", ",", "target_port", "=", "spec", ".", "split", "(", "','", ")", "if", "name", "==", "'ssh'", "and", "protocol", "==", "'tcp'", "and", "target_port", "==", "'22'", ":", "return", "host_port", "raise", "ValueError", "(", "'Could not determine localhost port for SSH forwarding'", ")" ]
dc12de90bb6945023d6f43a8071e984313a1d984
valid
_get_host_only_mac_address
Returns the MAC address assigned to the host-only adapter, using output from VBoxManage. Returned MAC address has no colons and is lower-cased.
dusty/systems/virtualbox/__init__.py
def _get_host_only_mac_address(): """Returns the MAC address assigned to the host-only adapter, using output from VBoxManage. Returned MAC address has no colons and is lower-cased.""" # Get the number of the host-only adapter vm_config = _get_vm_config() for line in vm_config: if line.startswith('hostonlyadapter'): adapter_number = int(line[15:16]) break else: raise ValueError('No host-only adapter is defined for the Dusty VM') for line in vm_config: if line.startswith('macaddress{}'.format(adapter_number)): return line.split('=')[1].strip('"').lower() raise ValueError('Could not find MAC address for adapter number {}'.format(adapter_number))
def _get_host_only_mac_address(): """Returns the MAC address assigned to the host-only adapter, using output from VBoxManage. Returned MAC address has no colons and is lower-cased.""" # Get the number of the host-only adapter vm_config = _get_vm_config() for line in vm_config: if line.startswith('hostonlyadapter'): adapter_number = int(line[15:16]) break else: raise ValueError('No host-only adapter is defined for the Dusty VM') for line in vm_config: if line.startswith('macaddress{}'.format(adapter_number)): return line.split('=')[1].strip('"').lower() raise ValueError('Could not find MAC address for adapter number {}'.format(adapter_number))
[ "Returns", "the", "MAC", "address", "assigned", "to", "the", "host", "-", "only", "adapter", "using", "output", "from", "VBoxManage", ".", "Returned", "MAC", "address", "has", "no", "colons", "and", "is", "lower", "-", "cased", "." ]
gamechanger/dusty
python
https://github.com/gamechanger/dusty/blob/dc12de90bb6945023d6f43a8071e984313a1d984/dusty/systems/virtualbox/__init__.py#L207-L223
[ "def", "_get_host_only_mac_address", "(", ")", ":", "# Get the number of the host-only adapter", "vm_config", "=", "_get_vm_config", "(", ")", "for", "line", "in", "vm_config", ":", "if", "line", ".", "startswith", "(", "'hostonlyadapter'", ")", ":", "adapter_number", "=", "int", "(", "line", "[", "15", ":", "16", "]", ")", "break", "else", ":", "raise", "ValueError", "(", "'No host-only adapter is defined for the Dusty VM'", ")", "for", "line", "in", "vm_config", ":", "if", "line", ".", "startswith", "(", "'macaddress{}'", ".", "format", "(", "adapter_number", ")", ")", ":", "return", "line", ".", "split", "(", "'='", ")", "[", "1", "]", ".", "strip", "(", "'\"'", ")", ".", "lower", "(", ")", "raise", "ValueError", "(", "'Could not find MAC address for adapter number {}'", ".", "format", "(", "adapter_number", ")", ")" ]
dc12de90bb6945023d6f43a8071e984313a1d984
valid
_ip_for_mac_from_ip_addr_show
Given the rather-complex output from an 'ip addr show' command on the VM, parse the output to determine the IP address assigned to the interface with the given MAC.
dusty/systems/virtualbox/__init__.py
def _ip_for_mac_from_ip_addr_show(ip_addr_show, target_mac): """Given the rather-complex output from an 'ip addr show' command on the VM, parse the output to determine the IP address assigned to the interface with the given MAC.""" return_next_ip = False for line in ip_addr_show.splitlines(): line = line.strip() if line.startswith('link/ether'): line_mac = line.split(' ')[1].replace(':', '') if line_mac == target_mac: return_next_ip = True elif return_next_ip and line.startswith('inet') and not line.startswith('inet6'): ip = line.split(' ')[1].split('/')[0] return ip
def _ip_for_mac_from_ip_addr_show(ip_addr_show, target_mac): """Given the rather-complex output from an 'ip addr show' command on the VM, parse the output to determine the IP address assigned to the interface with the given MAC.""" return_next_ip = False for line in ip_addr_show.splitlines(): line = line.strip() if line.startswith('link/ether'): line_mac = line.split(' ')[1].replace(':', '') if line_mac == target_mac: return_next_ip = True elif return_next_ip and line.startswith('inet') and not line.startswith('inet6'): ip = line.split(' ')[1].split('/')[0] return ip
[ "Given", "the", "rather", "-", "complex", "output", "from", "an", "ip", "addr", "show", "command", "on", "the", "VM", "parse", "the", "output", "to", "determine", "the", "IP", "address", "assigned", "to", "the", "interface", "with", "the", "given", "MAC", "." ]
gamechanger/dusty
python
https://github.com/gamechanger/dusty/blob/dc12de90bb6945023d6f43a8071e984313a1d984/dusty/systems/virtualbox/__init__.py#L225-L238
[ "def", "_ip_for_mac_from_ip_addr_show", "(", "ip_addr_show", ",", "target_mac", ")", ":", "return_next_ip", "=", "False", "for", "line", "in", "ip_addr_show", ".", "splitlines", "(", ")", ":", "line", "=", "line", ".", "strip", "(", ")", "if", "line", ".", "startswith", "(", "'link/ether'", ")", ":", "line_mac", "=", "line", ".", "split", "(", "' '", ")", "[", "1", "]", ".", "replace", "(", "':'", ",", "''", ")", "if", "line_mac", "==", "target_mac", ":", "return_next_ip", "=", "True", "elif", "return_next_ip", "and", "line", ".", "startswith", "(", "'inet'", ")", "and", "not", "line", ".", "startswith", "(", "'inet6'", ")", ":", "ip", "=", "line", ".", "split", "(", "' '", ")", "[", "1", "]", ".", "split", "(", "'/'", ")", "[", "0", "]", "return", "ip" ]
dc12de90bb6945023d6f43a8071e984313a1d984
valid
_get_host_only_ip
Determine the host-only IP of the Dusty VM through Virtualbox and SSH directly, bypassing Docker Machine. We do this because Docker Machine is much slower, taking about 600ms total. We are basically doing the same flow Docker Machine does in its own code.
dusty/systems/virtualbox/__init__.py
def _get_host_only_ip(): """Determine the host-only IP of the Dusty VM through Virtualbox and SSH directly, bypassing Docker Machine. We do this because Docker Machine is much slower, taking about 600ms total. We are basically doing the same flow Docker Machine does in its own code.""" mac = _get_host_only_mac_address() ip_addr_show = check_output_demoted(['ssh', '-o', 'StrictHostKeyChecking=no', '-o', 'UserKnownHostsFile=/dev/null', '-i', _vm_key_path(), '-p', _get_localhost_ssh_port(), 'docker@127.0.0.1', 'ip addr show']) return _ip_for_mac_from_ip_addr_show(ip_addr_show, mac)
def _get_host_only_ip(): """Determine the host-only IP of the Dusty VM through Virtualbox and SSH directly, bypassing Docker Machine. We do this because Docker Machine is much slower, taking about 600ms total. We are basically doing the same flow Docker Machine does in its own code.""" mac = _get_host_only_mac_address() ip_addr_show = check_output_demoted(['ssh', '-o', 'StrictHostKeyChecking=no', '-o', 'UserKnownHostsFile=/dev/null', '-i', _vm_key_path(), '-p', _get_localhost_ssh_port(), 'docker@127.0.0.1', 'ip addr show']) return _ip_for_mac_from_ip_addr_show(ip_addr_show, mac)
[ "Determine", "the", "host", "-", "only", "IP", "of", "the", "Dusty", "VM", "through", "Virtualbox", "and", "SSH", "directly", "bypassing", "Docker", "Machine", ".", "We", "do", "this", "because", "Docker", "Machine", "is", "much", "slower", "taking", "about", "600ms", "total", ".", "We", "are", "basically", "doing", "the", "same", "flow", "Docker", "Machine", "does", "in", "its", "own", "code", "." ]
gamechanger/dusty
python
https://github.com/gamechanger/dusty/blob/dc12de90bb6945023d6f43a8071e984313a1d984/dusty/systems/virtualbox/__init__.py#L240-L250
[ "def", "_get_host_only_ip", "(", ")", ":", "mac", "=", "_get_host_only_mac_address", "(", ")", "ip_addr_show", "=", "check_output_demoted", "(", "[", "'ssh'", ",", "'-o'", ",", "'StrictHostKeyChecking=no'", ",", "'-o'", ",", "'UserKnownHostsFile=/dev/null'", ",", "'-i'", ",", "_vm_key_path", "(", ")", ",", "'-p'", ",", "_get_localhost_ssh_port", "(", ")", ",", "'docker@127.0.0.1'", ",", "'ip addr show'", "]", ")", "return", "_ip_for_mac_from_ip_addr_show", "(", "ip_addr_show", ",", "mac", ")" ]
dc12de90bb6945023d6f43a8071e984313a1d984
valid
create_local_copy
Make a local copy of the sqlite cookie database and return the new filename. This is necessary in case this database is still being written to while the user browses to avoid sqlite locking errors.
__init__.py
def create_local_copy(cookie_file): """Make a local copy of the sqlite cookie database and return the new filename. This is necessary in case this database is still being written to while the user browses to avoid sqlite locking errors. """ # if type of cookie_file is a list, use the first element in the list if isinstance(cookie_file, list): cookie_file = cookie_file[0] # check if cookie file exists if os.path.exists(cookie_file): # copy to random name in tmp folder tmp_cookie_file = tempfile.NamedTemporaryFile(suffix='.sqlite').name open(tmp_cookie_file, 'wb').write(open(cookie_file, 'rb').read()) return tmp_cookie_file else: raise BrowserCookieError('Can not find cookie file at: ' + cookie_file)
def create_local_copy(cookie_file): """Make a local copy of the sqlite cookie database and return the new filename. This is necessary in case this database is still being written to while the user browses to avoid sqlite locking errors. """ # if type of cookie_file is a list, use the first element in the list if isinstance(cookie_file, list): cookie_file = cookie_file[0] # check if cookie file exists if os.path.exists(cookie_file): # copy to random name in tmp folder tmp_cookie_file = tempfile.NamedTemporaryFile(suffix='.sqlite').name open(tmp_cookie_file, 'wb').write(open(cookie_file, 'rb').read()) return tmp_cookie_file else: raise BrowserCookieError('Can not find cookie file at: ' + cookie_file)
[ "Make", "a", "local", "copy", "of", "the", "sqlite", "cookie", "database", "and", "return", "the", "new", "filename", ".", "This", "is", "necessary", "in", "case", "this", "database", "is", "still", "being", "written", "to", "while", "the", "user", "browses", "to", "avoid", "sqlite", "locking", "errors", "." ]
borisbabic/browser_cookie3
python
https://github.com/borisbabic/browser_cookie3/blob/e695777c54509c286991c5bb5ca65f043d748f55/__init__.py#L34-L50
[ "def", "create_local_copy", "(", "cookie_file", ")", ":", "# if type of cookie_file is a list, use the first element in the list", "if", "isinstance", "(", "cookie_file", ",", "list", ")", ":", "cookie_file", "=", "cookie_file", "[", "0", "]", "# check if cookie file exists", "if", "os", ".", "path", ".", "exists", "(", "cookie_file", ")", ":", "# copy to random name in tmp folder", "tmp_cookie_file", "=", "tempfile", ".", "NamedTemporaryFile", "(", "suffix", "=", "'.sqlite'", ")", ".", "name", "open", "(", "tmp_cookie_file", ",", "'wb'", ")", ".", "write", "(", "open", "(", "cookie_file", ",", "'rb'", ")", ".", "read", "(", ")", ")", "return", "tmp_cookie_file", "else", ":", "raise", "BrowserCookieError", "(", "'Can not find cookie file at: '", "+", "cookie_file", ")" ]
e695777c54509c286991c5bb5ca65f043d748f55
valid
create_cookie
Shortcut function to create a cookie
__init__.py
def create_cookie(host, path, secure, expires, name, value): """Shortcut function to create a cookie """ return http.cookiejar.Cookie(0, name, value, None, False, host, host.startswith('.'), host.startswith('.'), path, True, secure, expires, False, None, None, {})
def create_cookie(host, path, secure, expires, name, value): """Shortcut function to create a cookie """ return http.cookiejar.Cookie(0, name, value, None, False, host, host.startswith('.'), host.startswith('.'), path, True, secure, expires, False, None, None, {})
[ "Shortcut", "function", "to", "create", "a", "cookie" ]
borisbabic/browser_cookie3
python
https://github.com/borisbabic/browser_cookie3/blob/e695777c54509c286991c5bb5ca65f043d748f55/__init__.py#L274-L278
[ "def", "create_cookie", "(", "host", ",", "path", ",", "secure", ",", "expires", ",", "name", ",", "value", ")", ":", "return", "http", ".", "cookiejar", ".", "Cookie", "(", "0", ",", "name", ",", "value", ",", "None", ",", "False", ",", "host", ",", "host", ".", "startswith", "(", "'.'", ")", ",", "host", ".", "startswith", "(", "'.'", ")", ",", "path", ",", "True", ",", "secure", ",", "expires", ",", "False", ",", "None", ",", "None", ",", "{", "}", ")" ]
e695777c54509c286991c5bb5ca65f043d748f55
valid
load
Try to load cookies from all supported browsers and return combined cookiejar Optionally pass in a domain name to only load cookies from the specified domain
__init__.py
def load(domain_name=""): """Try to load cookies from all supported browsers and return combined cookiejar Optionally pass in a domain name to only load cookies from the specified domain """ cj = http.cookiejar.CookieJar() for cookie_fn in [chrome, firefox]: try: for cookie in cookie_fn(domain_name=domain_name): cj.set_cookie(cookie) except BrowserCookieError: pass return cj
def load(domain_name=""): """Try to load cookies from all supported browsers and return combined cookiejar Optionally pass in a domain name to only load cookies from the specified domain """ cj = http.cookiejar.CookieJar() for cookie_fn in [chrome, firefox]: try: for cookie in cookie_fn(domain_name=domain_name): cj.set_cookie(cookie) except BrowserCookieError: pass return cj
[ "Try", "to", "load", "cookies", "from", "all", "supported", "browsers", "and", "return", "combined", "cookiejar", "Optionally", "pass", "in", "a", "domain", "name", "to", "only", "load", "cookies", "from", "the", "specified", "domain" ]
borisbabic/browser_cookie3
python
https://github.com/borisbabic/browser_cookie3/blob/e695777c54509c286991c5bb5ca65f043d748f55/__init__.py#L295-L306
[ "def", "load", "(", "domain_name", "=", "\"\"", ")", ":", "cj", "=", "http", ".", "cookiejar", ".", "CookieJar", "(", ")", "for", "cookie_fn", "in", "[", "chrome", ",", "firefox", "]", ":", "try", ":", "for", "cookie", "in", "cookie_fn", "(", "domain_name", "=", "domain_name", ")", ":", "cj", ".", "set_cookie", "(", "cookie", ")", "except", "BrowserCookieError", ":", "pass", "return", "cj" ]
e695777c54509c286991c5bb5ca65f043d748f55
valid
Chrome.load
Load sqlite cookies into a cookiejar
__init__.py
def load(self): """Load sqlite cookies into a cookiejar """ con = sqlite3.connect(self.tmp_cookie_file) cur = con.cursor() try: # chrome <=55 cur.execute('SELECT host_key, path, secure, expires_utc, name, value, encrypted_value ' 'FROM cookies WHERE host_key like "%{}%";'.format(self.domain_name)) except sqlite3.OperationalError: # chrome >=56 cur.execute('SELECT host_key, path, is_secure, expires_utc, name, value, encrypted_value ' 'FROM cookies WHERE host_key like "%{}%";'.format(self.domain_name)) cj = http.cookiejar.CookieJar() for item in cur.fetchall(): host, path, secure, expires, name = item[:5] value = self._decrypt(item[5], item[6]) c = create_cookie(host, path, secure, expires, name, value) cj.set_cookie(c) con.close() return cj
def load(self): """Load sqlite cookies into a cookiejar """ con = sqlite3.connect(self.tmp_cookie_file) cur = con.cursor() try: # chrome <=55 cur.execute('SELECT host_key, path, secure, expires_utc, name, value, encrypted_value ' 'FROM cookies WHERE host_key like "%{}%";'.format(self.domain_name)) except sqlite3.OperationalError: # chrome >=56 cur.execute('SELECT host_key, path, is_secure, expires_utc, name, value, encrypted_value ' 'FROM cookies WHERE host_key like "%{}%";'.format(self.domain_name)) cj = http.cookiejar.CookieJar() for item in cur.fetchall(): host, path, secure, expires, name = item[:5] value = self._decrypt(item[5], item[6]) c = create_cookie(host, path, secure, expires, name, value) cj.set_cookie(c) con.close() return cj
[ "Load", "sqlite", "cookies", "into", "a", "cookiejar" ]
borisbabic/browser_cookie3
python
https://github.com/borisbabic/browser_cookie3/blob/e695777c54509c286991c5bb5ca65f043d748f55/__init__.py#L147-L168
[ "def", "load", "(", "self", ")", ":", "con", "=", "sqlite3", ".", "connect", "(", "self", ".", "tmp_cookie_file", ")", "cur", "=", "con", ".", "cursor", "(", ")", "try", ":", "# chrome <=55", "cur", ".", "execute", "(", "'SELECT host_key, path, secure, expires_utc, name, value, encrypted_value '", "'FROM cookies WHERE host_key like \"%{}%\";'", ".", "format", "(", "self", ".", "domain_name", ")", ")", "except", "sqlite3", ".", "OperationalError", ":", "# chrome >=56", "cur", ".", "execute", "(", "'SELECT host_key, path, is_secure, expires_utc, name, value, encrypted_value '", "'FROM cookies WHERE host_key like \"%{}%\";'", ".", "format", "(", "self", ".", "domain_name", ")", ")", "cj", "=", "http", ".", "cookiejar", ".", "CookieJar", "(", ")", "for", "item", "in", "cur", ".", "fetchall", "(", ")", ":", "host", ",", "path", ",", "secure", ",", "expires", ",", "name", "=", "item", "[", ":", "5", "]", "value", "=", "self", ".", "_decrypt", "(", "item", "[", "5", "]", ",", "item", "[", "6", "]", ")", "c", "=", "create_cookie", "(", "host", ",", "path", ",", "secure", ",", "expires", ",", "name", ",", "value", ")", "cj", ".", "set_cookie", "(", "c", ")", "con", ".", "close", "(", ")", "return", "cj" ]
e695777c54509c286991c5bb5ca65f043d748f55
valid
Chrome._decrypt
Decrypt encoded cookies
__init__.py
def _decrypt(self, value, encrypted_value): """Decrypt encoded cookies """ if sys.platform == 'win32': return self._decrypt_windows_chrome(value, encrypted_value) if value or (encrypted_value[:3] != b'v10'): return value # Encrypted cookies should be prefixed with 'v10' according to the # Chromium code. Strip it off. encrypted_value = encrypted_value[3:] encrypted_value_half_len = int(len(encrypted_value) / 2) cipher = pyaes.Decrypter(pyaes.AESModeOfOperationCBC(self.key, self.iv)) decrypted = cipher.feed(encrypted_value[:encrypted_value_half_len]) decrypted += cipher.feed(encrypted_value[encrypted_value_half_len:]) decrypted += cipher.feed() return decrypted.decode("utf-8")
def _decrypt(self, value, encrypted_value): """Decrypt encoded cookies """ if sys.platform == 'win32': return self._decrypt_windows_chrome(value, encrypted_value) if value or (encrypted_value[:3] != b'v10'): return value # Encrypted cookies should be prefixed with 'v10' according to the # Chromium code. Strip it off. encrypted_value = encrypted_value[3:] encrypted_value_half_len = int(len(encrypted_value) / 2) cipher = pyaes.Decrypter(pyaes.AESModeOfOperationCBC(self.key, self.iv)) decrypted = cipher.feed(encrypted_value[:encrypted_value_half_len]) decrypted += cipher.feed(encrypted_value[encrypted_value_half_len:]) decrypted += cipher.feed() return decrypted.decode("utf-8")
[ "Decrypt", "encoded", "cookies" ]
borisbabic/browser_cookie3
python
https://github.com/borisbabic/browser_cookie3/blob/e695777c54509c286991c5bb5ca65f043d748f55/__init__.py#L183-L202
[ "def", "_decrypt", "(", "self", ",", "value", ",", "encrypted_value", ")", ":", "if", "sys", ".", "platform", "==", "'win32'", ":", "return", "self", ".", "_decrypt_windows_chrome", "(", "value", ",", "encrypted_value", ")", "if", "value", "or", "(", "encrypted_value", "[", ":", "3", "]", "!=", "b'v10'", ")", ":", "return", "value", "# Encrypted cookies should be prefixed with 'v10' according to the", "# Chromium code. Strip it off.", "encrypted_value", "=", "encrypted_value", "[", "3", ":", "]", "encrypted_value_half_len", "=", "int", "(", "len", "(", "encrypted_value", ")", "/", "2", ")", "cipher", "=", "pyaes", ".", "Decrypter", "(", "pyaes", ".", "AESModeOfOperationCBC", "(", "self", ".", "key", ",", "self", ".", "iv", ")", ")", "decrypted", "=", "cipher", ".", "feed", "(", "encrypted_value", "[", ":", "encrypted_value_half_len", "]", ")", "decrypted", "+=", "cipher", ".", "feed", "(", "encrypted_value", "[", "encrypted_value_half_len", ":", "]", ")", "decrypted", "+=", "cipher", ".", "feed", "(", ")", "return", "decrypted", ".", "decode", "(", "\"utf-8\"", ")" ]
e695777c54509c286991c5bb5ca65f043d748f55
valid
_gen
docstring for _gen
exrex.py
def _gen(d, limit=20, count=False, grouprefs=None): """docstring for _gen""" if grouprefs is None: grouprefs = {} ret = [''] strings = 0 literal = False for i in d: if i[0] == sre_parse.IN: subs = _in(i[1]) if count: strings = (strings or 1) * len(subs) ret = comb(ret, subs) elif i[0] == sre_parse.LITERAL: literal = True ret = mappend(ret, unichr(i[1])) elif i[0] == sre_parse.CATEGORY: subs = CATEGORIES.get(i[1], ['']) if count: strings = (strings or 1) * len(subs) ret = comb(ret, subs) elif i[0] == sre_parse.ANY: subs = CATEGORIES['category_any'] if count: strings = (strings or 1) * len(subs) ret = comb(ret, subs) elif i[0] == sre_parse.MAX_REPEAT or i[0] == sre_parse.MIN_REPEAT: items = list(i[1][2]) if i[1][1] + 1 - i[1][0] >= limit: r1 = i[1][0] r2 = i[1][0] + limit else: r1 = i[1][0] r2 = i[1][1] + 1 ran = range(r1, r2) if count: branch_count = 0 for p in ran: branch_count += pow(_gen(items, limit, True, grouprefs), p) strings = (strings or 1) * branch_count ret = prods(ret, ran, items, limit, grouprefs) elif i[0] == sre_parse.BRANCH: if count: for x in i[1][1]: strings += _gen(x, limit, True, grouprefs) or 1 ret = concit(ret, i[1][1], limit, grouprefs) elif i[0] == sre_parse.SUBPATTERN or i[0] == sre_parse.ASSERT: subexpr = i[1][1] if IS_PY36_OR_GREATER and i[0] == sre_parse.SUBPATTERN: subexpr = i[1][3] if count: strings = ( strings or 1) * (sum(ggen([0], _gen, subexpr, limit=limit, count=True, grouprefs=grouprefs)) or 1) ret = ggen(ret, _gen, subexpr, limit=limit, count=False, grouprefs=grouprefs, groupref=i[1][0]) # ignore ^ and $ elif i[0] == sre_parse.AT: continue elif i[0] == sre_parse.NOT_LITERAL: subs = list(CATEGORIES['category_any']) if unichr(i[1]) in subs: subs.remove(unichr(i[1])) if count: strings = (strings or 1) * len(subs) ret = comb(ret, subs) elif i[0] == sre_parse.GROUPREF: ret = dappend(ret, grouprefs, i[1]) elif i[0] == sre_parse.ASSERT_NOT: pass else: print('[!] cannot handle expression ' + repr(i)) if count: if strings == 0 and literal: inc = True for i in d: if i[0] not in (sre_parse.AT, sre_parse.LITERAL): inc = False if inc: strings = 1 return strings return ret
def _gen(d, limit=20, count=False, grouprefs=None): """docstring for _gen""" if grouprefs is None: grouprefs = {} ret = [''] strings = 0 literal = False for i in d: if i[0] == sre_parse.IN: subs = _in(i[1]) if count: strings = (strings or 1) * len(subs) ret = comb(ret, subs) elif i[0] == sre_parse.LITERAL: literal = True ret = mappend(ret, unichr(i[1])) elif i[0] == sre_parse.CATEGORY: subs = CATEGORIES.get(i[1], ['']) if count: strings = (strings or 1) * len(subs) ret = comb(ret, subs) elif i[0] == sre_parse.ANY: subs = CATEGORIES['category_any'] if count: strings = (strings or 1) * len(subs) ret = comb(ret, subs) elif i[0] == sre_parse.MAX_REPEAT or i[0] == sre_parse.MIN_REPEAT: items = list(i[1][2]) if i[1][1] + 1 - i[1][0] >= limit: r1 = i[1][0] r2 = i[1][0] + limit else: r1 = i[1][0] r2 = i[1][1] + 1 ran = range(r1, r2) if count: branch_count = 0 for p in ran: branch_count += pow(_gen(items, limit, True, grouprefs), p) strings = (strings or 1) * branch_count ret = prods(ret, ran, items, limit, grouprefs) elif i[0] == sre_parse.BRANCH: if count: for x in i[1][1]: strings += _gen(x, limit, True, grouprefs) or 1 ret = concit(ret, i[1][1], limit, grouprefs) elif i[0] == sre_parse.SUBPATTERN or i[0] == sre_parse.ASSERT: subexpr = i[1][1] if IS_PY36_OR_GREATER and i[0] == sre_parse.SUBPATTERN: subexpr = i[1][3] if count: strings = ( strings or 1) * (sum(ggen([0], _gen, subexpr, limit=limit, count=True, grouprefs=grouprefs)) or 1) ret = ggen(ret, _gen, subexpr, limit=limit, count=False, grouprefs=grouprefs, groupref=i[1][0]) # ignore ^ and $ elif i[0] == sre_parse.AT: continue elif i[0] == sre_parse.NOT_LITERAL: subs = list(CATEGORIES['category_any']) if unichr(i[1]) in subs: subs.remove(unichr(i[1])) if count: strings = (strings or 1) * len(subs) ret = comb(ret, subs) elif i[0] == sre_parse.GROUPREF: ret = dappend(ret, grouprefs, i[1]) elif i[0] == sre_parse.ASSERT_NOT: pass else: print('[!] cannot handle expression ' + repr(i)) if count: if strings == 0 and literal: inc = True for i in d: if i[0] not in (sre_parse.AT, sre_parse.LITERAL): inc = False if inc: strings = 1 return strings return ret
[ "docstring", "for", "_gen" ]
asciimoo/exrex
python
https://github.com/asciimoo/exrex/blob/69733409042b526da584c675907a316ad708a8d4/exrex.py#L161-L243
[ "def", "_gen", "(", "d", ",", "limit", "=", "20", ",", "count", "=", "False", ",", "grouprefs", "=", "None", ")", ":", "if", "grouprefs", "is", "None", ":", "grouprefs", "=", "{", "}", "ret", "=", "[", "''", "]", "strings", "=", "0", "literal", "=", "False", "for", "i", "in", "d", ":", "if", "i", "[", "0", "]", "==", "sre_parse", ".", "IN", ":", "subs", "=", "_in", "(", "i", "[", "1", "]", ")", "if", "count", ":", "strings", "=", "(", "strings", "or", "1", ")", "*", "len", "(", "subs", ")", "ret", "=", "comb", "(", "ret", ",", "subs", ")", "elif", "i", "[", "0", "]", "==", "sre_parse", ".", "LITERAL", ":", "literal", "=", "True", "ret", "=", "mappend", "(", "ret", ",", "unichr", "(", "i", "[", "1", "]", ")", ")", "elif", "i", "[", "0", "]", "==", "sre_parse", ".", "CATEGORY", ":", "subs", "=", "CATEGORIES", ".", "get", "(", "i", "[", "1", "]", ",", "[", "''", "]", ")", "if", "count", ":", "strings", "=", "(", "strings", "or", "1", ")", "*", "len", "(", "subs", ")", "ret", "=", "comb", "(", "ret", ",", "subs", ")", "elif", "i", "[", "0", "]", "==", "sre_parse", ".", "ANY", ":", "subs", "=", "CATEGORIES", "[", "'category_any'", "]", "if", "count", ":", "strings", "=", "(", "strings", "or", "1", ")", "*", "len", "(", "subs", ")", "ret", "=", "comb", "(", "ret", ",", "subs", ")", "elif", "i", "[", "0", "]", "==", "sre_parse", ".", "MAX_REPEAT", "or", "i", "[", "0", "]", "==", "sre_parse", ".", "MIN_REPEAT", ":", "items", "=", "list", "(", "i", "[", "1", "]", "[", "2", "]", ")", "if", "i", "[", "1", "]", "[", "1", "]", "+", "1", "-", "i", "[", "1", "]", "[", "0", "]", ">=", "limit", ":", "r1", "=", "i", "[", "1", "]", "[", "0", "]", "r2", "=", "i", "[", "1", "]", "[", "0", "]", "+", "limit", "else", ":", "r1", "=", "i", "[", "1", "]", "[", "0", "]", "r2", "=", "i", "[", "1", "]", "[", "1", "]", "+", "1", "ran", "=", "range", "(", "r1", ",", "r2", ")", "if", "count", ":", "branch_count", "=", "0", "for", "p", "in", "ran", ":", "branch_count", "+=", "pow", "(", "_gen", "(", "items", ",", "limit", ",", "True", ",", "grouprefs", ")", ",", "p", ")", "strings", "=", "(", "strings", "or", "1", ")", "*", "branch_count", "ret", "=", "prods", "(", "ret", ",", "ran", ",", "items", ",", "limit", ",", "grouprefs", ")", "elif", "i", "[", "0", "]", "==", "sre_parse", ".", "BRANCH", ":", "if", "count", ":", "for", "x", "in", "i", "[", "1", "]", "[", "1", "]", ":", "strings", "+=", "_gen", "(", "x", ",", "limit", ",", "True", ",", "grouprefs", ")", "or", "1", "ret", "=", "concit", "(", "ret", ",", "i", "[", "1", "]", "[", "1", "]", ",", "limit", ",", "grouprefs", ")", "elif", "i", "[", "0", "]", "==", "sre_parse", ".", "SUBPATTERN", "or", "i", "[", "0", "]", "==", "sre_parse", ".", "ASSERT", ":", "subexpr", "=", "i", "[", "1", "]", "[", "1", "]", "if", "IS_PY36_OR_GREATER", "and", "i", "[", "0", "]", "==", "sre_parse", ".", "SUBPATTERN", ":", "subexpr", "=", "i", "[", "1", "]", "[", "3", "]", "if", "count", ":", "strings", "=", "(", "strings", "or", "1", ")", "*", "(", "sum", "(", "ggen", "(", "[", "0", "]", ",", "_gen", ",", "subexpr", ",", "limit", "=", "limit", ",", "count", "=", "True", ",", "grouprefs", "=", "grouprefs", ")", ")", "or", "1", ")", "ret", "=", "ggen", "(", "ret", ",", "_gen", ",", "subexpr", ",", "limit", "=", "limit", ",", "count", "=", "False", ",", "grouprefs", "=", "grouprefs", ",", "groupref", "=", "i", "[", "1", "]", "[", "0", "]", ")", "# ignore ^ and $", "elif", "i", "[", "0", "]", "==", "sre_parse", ".", "AT", ":", "continue", "elif", "i", "[", "0", "]", "==", "sre_parse", ".", "NOT_LITERAL", ":", "subs", "=", "list", "(", "CATEGORIES", "[", "'category_any'", "]", ")", "if", "unichr", "(", "i", "[", "1", "]", ")", "in", "subs", ":", "subs", ".", "remove", "(", "unichr", "(", "i", "[", "1", "]", ")", ")", "if", "count", ":", "strings", "=", "(", "strings", "or", "1", ")", "*", "len", "(", "subs", ")", "ret", "=", "comb", "(", "ret", ",", "subs", ")", "elif", "i", "[", "0", "]", "==", "sre_parse", ".", "GROUPREF", ":", "ret", "=", "dappend", "(", "ret", ",", "grouprefs", ",", "i", "[", "1", "]", ")", "elif", "i", "[", "0", "]", "==", "sre_parse", ".", "ASSERT_NOT", ":", "pass", "else", ":", "print", "(", "'[!] cannot handle expression '", "+", "repr", "(", "i", ")", ")", "if", "count", ":", "if", "strings", "==", "0", "and", "literal", ":", "inc", "=", "True", "for", "i", "in", "d", ":", "if", "i", "[", "0", "]", "not", "in", "(", "sre_parse", ".", "AT", ",", "sre_parse", ".", "LITERAL", ")", ":", "inc", "=", "False", "if", "inc", ":", "strings", "=", "1", "return", "strings", "return", "ret" ]
69733409042b526da584c675907a316ad708a8d4
valid
_randone
docstring for _randone
exrex.py
def _randone(d, limit=20, grouprefs=None): if grouprefs is None: grouprefs = {} """docstring for _randone""" ret = '' for i in d: if i[0] == sre_parse.IN: ret += choice(_in(i[1])) elif i[0] == sre_parse.LITERAL: ret += unichr(i[1]) elif i[0] == sre_parse.CATEGORY: ret += choice(CATEGORIES.get(i[1], [''])) elif i[0] == sre_parse.ANY: ret += choice(CATEGORIES['category_any']) elif i[0] == sre_parse.MAX_REPEAT or i[0] == sre_parse.MIN_REPEAT: if i[1][1] + 1 - i[1][0] >= limit: min, max = i[1][0], i[1][0] + limit - 1 else: min, max = i[1][0], i[1][1] for _ in range(randint(min, max)): ret += _randone(list(i[1][2]), limit, grouprefs) elif i[0] == sre_parse.BRANCH: ret += _randone(choice(i[1][1]), limit, grouprefs) elif i[0] == sre_parse.SUBPATTERN or i[0] == sre_parse.ASSERT: subexpr = i[1][1] if IS_PY36_OR_GREATER and i[0] == sre_parse.SUBPATTERN: subexpr = i[1][3] subp = _randone(subexpr, limit, grouprefs) if i[1][0]: grouprefs[i[1][0]] = subp ret += subp elif i[0] == sre_parse.AT: continue elif i[0] == sre_parse.NOT_LITERAL: c = list(CATEGORIES['category_any']) if unichr(i[1]) in c: c.remove(unichr(i[1])) ret += choice(c) elif i[0] == sre_parse.GROUPREF: ret += grouprefs[i[1]] elif i[0] == sre_parse.ASSERT_NOT: pass else: print('[!] cannot handle expression "%s"' % str(i)) return ret
def _randone(d, limit=20, grouprefs=None): if grouprefs is None: grouprefs = {} """docstring for _randone""" ret = '' for i in d: if i[0] == sre_parse.IN: ret += choice(_in(i[1])) elif i[0] == sre_parse.LITERAL: ret += unichr(i[1]) elif i[0] == sre_parse.CATEGORY: ret += choice(CATEGORIES.get(i[1], [''])) elif i[0] == sre_parse.ANY: ret += choice(CATEGORIES['category_any']) elif i[0] == sre_parse.MAX_REPEAT or i[0] == sre_parse.MIN_REPEAT: if i[1][1] + 1 - i[1][0] >= limit: min, max = i[1][0], i[1][0] + limit - 1 else: min, max = i[1][0], i[1][1] for _ in range(randint(min, max)): ret += _randone(list(i[1][2]), limit, grouprefs) elif i[0] == sre_parse.BRANCH: ret += _randone(choice(i[1][1]), limit, grouprefs) elif i[0] == sre_parse.SUBPATTERN or i[0] == sre_parse.ASSERT: subexpr = i[1][1] if IS_PY36_OR_GREATER and i[0] == sre_parse.SUBPATTERN: subexpr = i[1][3] subp = _randone(subexpr, limit, grouprefs) if i[1][0]: grouprefs[i[1][0]] = subp ret += subp elif i[0] == sre_parse.AT: continue elif i[0] == sre_parse.NOT_LITERAL: c = list(CATEGORIES['category_any']) if unichr(i[1]) in c: c.remove(unichr(i[1])) ret += choice(c) elif i[0] == sre_parse.GROUPREF: ret += grouprefs[i[1]] elif i[0] == sre_parse.ASSERT_NOT: pass else: print('[!] cannot handle expression "%s"' % str(i)) return ret
[ "docstring", "for", "_randone" ]
asciimoo/exrex
python
https://github.com/asciimoo/exrex/blob/69733409042b526da584c675907a316ad708a8d4/exrex.py#L246-L291
[ "def", "_randone", "(", "d", ",", "limit", "=", "20", ",", "grouprefs", "=", "None", ")", ":", "if", "grouprefs", "is", "None", ":", "grouprefs", "=", "{", "}", "ret", "=", "''", "for", "i", "in", "d", ":", "if", "i", "[", "0", "]", "==", "sre_parse", ".", "IN", ":", "ret", "+=", "choice", "(", "_in", "(", "i", "[", "1", "]", ")", ")", "elif", "i", "[", "0", "]", "==", "sre_parse", ".", "LITERAL", ":", "ret", "+=", "unichr", "(", "i", "[", "1", "]", ")", "elif", "i", "[", "0", "]", "==", "sre_parse", ".", "CATEGORY", ":", "ret", "+=", "choice", "(", "CATEGORIES", ".", "get", "(", "i", "[", "1", "]", ",", "[", "''", "]", ")", ")", "elif", "i", "[", "0", "]", "==", "sre_parse", ".", "ANY", ":", "ret", "+=", "choice", "(", "CATEGORIES", "[", "'category_any'", "]", ")", "elif", "i", "[", "0", "]", "==", "sre_parse", ".", "MAX_REPEAT", "or", "i", "[", "0", "]", "==", "sre_parse", ".", "MIN_REPEAT", ":", "if", "i", "[", "1", "]", "[", "1", "]", "+", "1", "-", "i", "[", "1", "]", "[", "0", "]", ">=", "limit", ":", "min", ",", "max", "=", "i", "[", "1", "]", "[", "0", "]", ",", "i", "[", "1", "]", "[", "0", "]", "+", "limit", "-", "1", "else", ":", "min", ",", "max", "=", "i", "[", "1", "]", "[", "0", "]", ",", "i", "[", "1", "]", "[", "1", "]", "for", "_", "in", "range", "(", "randint", "(", "min", ",", "max", ")", ")", ":", "ret", "+=", "_randone", "(", "list", "(", "i", "[", "1", "]", "[", "2", "]", ")", ",", "limit", ",", "grouprefs", ")", "elif", "i", "[", "0", "]", "==", "sre_parse", ".", "BRANCH", ":", "ret", "+=", "_randone", "(", "choice", "(", "i", "[", "1", "]", "[", "1", "]", ")", ",", "limit", ",", "grouprefs", ")", "elif", "i", "[", "0", "]", "==", "sre_parse", ".", "SUBPATTERN", "or", "i", "[", "0", "]", "==", "sre_parse", ".", "ASSERT", ":", "subexpr", "=", "i", "[", "1", "]", "[", "1", "]", "if", "IS_PY36_OR_GREATER", "and", "i", "[", "0", "]", "==", "sre_parse", ".", "SUBPATTERN", ":", "subexpr", "=", "i", "[", "1", "]", "[", "3", "]", "subp", "=", "_randone", "(", "subexpr", ",", "limit", ",", "grouprefs", ")", "if", "i", "[", "1", "]", "[", "0", "]", ":", "grouprefs", "[", "i", "[", "1", "]", "[", "0", "]", "]", "=", "subp", "ret", "+=", "subp", "elif", "i", "[", "0", "]", "==", "sre_parse", ".", "AT", ":", "continue", "elif", "i", "[", "0", "]", "==", "sre_parse", ".", "NOT_LITERAL", ":", "c", "=", "list", "(", "CATEGORIES", "[", "'category_any'", "]", ")", "if", "unichr", "(", "i", "[", "1", "]", ")", "in", "c", ":", "c", ".", "remove", "(", "unichr", "(", "i", "[", "1", "]", ")", ")", "ret", "+=", "choice", "(", "c", ")", "elif", "i", "[", "0", "]", "==", "sre_parse", ".", "GROUPREF", ":", "ret", "+=", "grouprefs", "[", "i", "[", "1", "]", "]", "elif", "i", "[", "0", "]", "==", "sre_parse", ".", "ASSERT_NOT", ":", "pass", "else", ":", "print", "(", "'[!] cannot handle expression \"%s\"'", "%", "str", "(", "i", ")", ")", "return", "ret" ]
69733409042b526da584c675907a316ad708a8d4
valid
sre_to_string
sre_parse object to string :param sre_obj: Output of sre_parse.parse() :type sre_obj: list :rtype: str
exrex.py
def sre_to_string(sre_obj, paren=True): """sre_parse object to string :param sre_obj: Output of sre_parse.parse() :type sre_obj: list :rtype: str """ ret = u'' for i in sre_obj: if i[0] == sre_parse.IN: prefix = '' if len(i[1]) and i[1][0][0] == sre_parse.NEGATE: prefix = '^' ret += u'[{0}{1}]'.format(prefix, sre_to_string(i[1], paren=paren)) elif i[0] == sre_parse.LITERAL: u = unichr(i[1]) ret += u if u not in sre_parse.SPECIAL_CHARS else '\\{0}'.format(u) elif i[0] == sre_parse.CATEGORY: ret += REVERSE_CATEGORIES[i[1]] elif i[0] == sre_parse.ANY: ret += '.' elif i[0] == sre_parse.BRANCH: # TODO simplifications here parts = [sre_to_string(x, paren=paren) for x in i[1][1]] if not any(parts): continue if i[1][0]: if len(parts) == 1: paren = False prefix = '' else: prefix = '?:' branch = '|'.join(parts) if paren: ret += '({0}{1})'.format(prefix, branch) else: ret += '{0}'.format(branch) elif i[0] == sre_parse.SUBPATTERN: subexpr = i[1][1] if IS_PY36_OR_GREATER and i[0] == sre_parse.SUBPATTERN: subexpr = i[1][3] if i[1][0]: ret += '({0})'.format(sre_to_string(subexpr, paren=False)) else: ret += '{0}'.format(sre_to_string(subexpr, paren=paren)) elif i[0] == sre_parse.NOT_LITERAL: ret += '[^{0}]'.format(unichr(i[1])) elif i[0] == sre_parse.MAX_REPEAT: if i[1][0] == i[1][1]: range_str = '{{{0}}}'.format(i[1][0]) else: if i[1][0] == 0 and i[1][1] - i[1][0] == sre_parse.MAXREPEAT: range_str = '*' elif i[1][0] == 1 and i[1][1] - i[1][0] == sre_parse.MAXREPEAT - 1: range_str = '+' else: range_str = '{{{0},{1}}}'.format(i[1][0], i[1][1]) ret += sre_to_string(i[1][2], paren=paren) + range_str elif i[0] == sre_parse.MIN_REPEAT: if i[1][0] == 0 and i[1][1] == sre_parse.MAXREPEAT: range_str = '*?' elif i[1][0] == 1 and i[1][1] == sre_parse.MAXREPEAT: range_str = '+?' elif i[1][1] == sre_parse.MAXREPEAT: range_str = '{{{0},}}?'.format(i[1][0]) else: range_str = '{{{0},{1}}}?'.format(i[1][0], i[1][1]) ret += sre_to_string(i[1][2], paren=paren) + range_str elif i[0] == sre_parse.GROUPREF: ret += '\\{0}'.format(i[1]) elif i[0] == sre_parse.AT: if i[1] == sre_parse.AT_BEGINNING: ret += '^' elif i[1] == sre_parse.AT_END: ret += '$' elif i[0] == sre_parse.NEGATE: pass elif i[0] == sre_parse.RANGE: ret += '{0}-{1}'.format(unichr(i[1][0]), unichr(i[1][1])) elif i[0] == sre_parse.ASSERT: if i[1][0]: ret += '(?={0})'.format(sre_to_string(i[1][1], paren=False)) else: ret += '{0}'.format(sre_to_string(i[1][1], paren=paren)) elif i[0] == sre_parse.ASSERT_NOT: pass else: print('[!] cannot handle expression "%s"' % str(i)) return ret
def sre_to_string(sre_obj, paren=True): """sre_parse object to string :param sre_obj: Output of sre_parse.parse() :type sre_obj: list :rtype: str """ ret = u'' for i in sre_obj: if i[0] == sre_parse.IN: prefix = '' if len(i[1]) and i[1][0][0] == sre_parse.NEGATE: prefix = '^' ret += u'[{0}{1}]'.format(prefix, sre_to_string(i[1], paren=paren)) elif i[0] == sre_parse.LITERAL: u = unichr(i[1]) ret += u if u not in sre_parse.SPECIAL_CHARS else '\\{0}'.format(u) elif i[0] == sre_parse.CATEGORY: ret += REVERSE_CATEGORIES[i[1]] elif i[0] == sre_parse.ANY: ret += '.' elif i[0] == sre_parse.BRANCH: # TODO simplifications here parts = [sre_to_string(x, paren=paren) for x in i[1][1]] if not any(parts): continue if i[1][0]: if len(parts) == 1: paren = False prefix = '' else: prefix = '?:' branch = '|'.join(parts) if paren: ret += '({0}{1})'.format(prefix, branch) else: ret += '{0}'.format(branch) elif i[0] == sre_parse.SUBPATTERN: subexpr = i[1][1] if IS_PY36_OR_GREATER and i[0] == sre_parse.SUBPATTERN: subexpr = i[1][3] if i[1][0]: ret += '({0})'.format(sre_to_string(subexpr, paren=False)) else: ret += '{0}'.format(sre_to_string(subexpr, paren=paren)) elif i[0] == sre_parse.NOT_LITERAL: ret += '[^{0}]'.format(unichr(i[1])) elif i[0] == sre_parse.MAX_REPEAT: if i[1][0] == i[1][1]: range_str = '{{{0}}}'.format(i[1][0]) else: if i[1][0] == 0 and i[1][1] - i[1][0] == sre_parse.MAXREPEAT: range_str = '*' elif i[1][0] == 1 and i[1][1] - i[1][0] == sre_parse.MAXREPEAT - 1: range_str = '+' else: range_str = '{{{0},{1}}}'.format(i[1][0], i[1][1]) ret += sre_to_string(i[1][2], paren=paren) + range_str elif i[0] == sre_parse.MIN_REPEAT: if i[1][0] == 0 and i[1][1] == sre_parse.MAXREPEAT: range_str = '*?' elif i[1][0] == 1 and i[1][1] == sre_parse.MAXREPEAT: range_str = '+?' elif i[1][1] == sre_parse.MAXREPEAT: range_str = '{{{0},}}?'.format(i[1][0]) else: range_str = '{{{0},{1}}}?'.format(i[1][0], i[1][1]) ret += sre_to_string(i[1][2], paren=paren) + range_str elif i[0] == sre_parse.GROUPREF: ret += '\\{0}'.format(i[1]) elif i[0] == sre_parse.AT: if i[1] == sre_parse.AT_BEGINNING: ret += '^' elif i[1] == sre_parse.AT_END: ret += '$' elif i[0] == sre_parse.NEGATE: pass elif i[0] == sre_parse.RANGE: ret += '{0}-{1}'.format(unichr(i[1][0]), unichr(i[1][1])) elif i[0] == sre_parse.ASSERT: if i[1][0]: ret += '(?={0})'.format(sre_to_string(i[1][1], paren=False)) else: ret += '{0}'.format(sre_to_string(i[1][1], paren=paren)) elif i[0] == sre_parse.ASSERT_NOT: pass else: print('[!] cannot handle expression "%s"' % str(i)) return ret
[ "sre_parse", "object", "to", "string" ]
asciimoo/exrex
python
https://github.com/asciimoo/exrex/blob/69733409042b526da584c675907a316ad708a8d4/exrex.py#L294-L382
[ "def", "sre_to_string", "(", "sre_obj", ",", "paren", "=", "True", ")", ":", "ret", "=", "u''", "for", "i", "in", "sre_obj", ":", "if", "i", "[", "0", "]", "==", "sre_parse", ".", "IN", ":", "prefix", "=", "''", "if", "len", "(", "i", "[", "1", "]", ")", "and", "i", "[", "1", "]", "[", "0", "]", "[", "0", "]", "==", "sre_parse", ".", "NEGATE", ":", "prefix", "=", "'^'", "ret", "+=", "u'[{0}{1}]'", ".", "format", "(", "prefix", ",", "sre_to_string", "(", "i", "[", "1", "]", ",", "paren", "=", "paren", ")", ")", "elif", "i", "[", "0", "]", "==", "sre_parse", ".", "LITERAL", ":", "u", "=", "unichr", "(", "i", "[", "1", "]", ")", "ret", "+=", "u", "if", "u", "not", "in", "sre_parse", ".", "SPECIAL_CHARS", "else", "'\\\\{0}'", ".", "format", "(", "u", ")", "elif", "i", "[", "0", "]", "==", "sre_parse", ".", "CATEGORY", ":", "ret", "+=", "REVERSE_CATEGORIES", "[", "i", "[", "1", "]", "]", "elif", "i", "[", "0", "]", "==", "sre_parse", ".", "ANY", ":", "ret", "+=", "'.'", "elif", "i", "[", "0", "]", "==", "sre_parse", ".", "BRANCH", ":", "# TODO simplifications here", "parts", "=", "[", "sre_to_string", "(", "x", ",", "paren", "=", "paren", ")", "for", "x", "in", "i", "[", "1", "]", "[", "1", "]", "]", "if", "not", "any", "(", "parts", ")", ":", "continue", "if", "i", "[", "1", "]", "[", "0", "]", ":", "if", "len", "(", "parts", ")", "==", "1", ":", "paren", "=", "False", "prefix", "=", "''", "else", ":", "prefix", "=", "'?:'", "branch", "=", "'|'", ".", "join", "(", "parts", ")", "if", "paren", ":", "ret", "+=", "'({0}{1})'", ".", "format", "(", "prefix", ",", "branch", ")", "else", ":", "ret", "+=", "'{0}'", ".", "format", "(", "branch", ")", "elif", "i", "[", "0", "]", "==", "sre_parse", ".", "SUBPATTERN", ":", "subexpr", "=", "i", "[", "1", "]", "[", "1", "]", "if", "IS_PY36_OR_GREATER", "and", "i", "[", "0", "]", "==", "sre_parse", ".", "SUBPATTERN", ":", "subexpr", "=", "i", "[", "1", "]", "[", "3", "]", "if", "i", "[", "1", "]", "[", "0", "]", ":", "ret", "+=", "'({0})'", ".", "format", "(", "sre_to_string", "(", "subexpr", ",", "paren", "=", "False", ")", ")", "else", ":", "ret", "+=", "'{0}'", ".", "format", "(", "sre_to_string", "(", "subexpr", ",", "paren", "=", "paren", ")", ")", "elif", "i", "[", "0", "]", "==", "sre_parse", ".", "NOT_LITERAL", ":", "ret", "+=", "'[^{0}]'", ".", "format", "(", "unichr", "(", "i", "[", "1", "]", ")", ")", "elif", "i", "[", "0", "]", "==", "sre_parse", ".", "MAX_REPEAT", ":", "if", "i", "[", "1", "]", "[", "0", "]", "==", "i", "[", "1", "]", "[", "1", "]", ":", "range_str", "=", "'{{{0}}}'", ".", "format", "(", "i", "[", "1", "]", "[", "0", "]", ")", "else", ":", "if", "i", "[", "1", "]", "[", "0", "]", "==", "0", "and", "i", "[", "1", "]", "[", "1", "]", "-", "i", "[", "1", "]", "[", "0", "]", "==", "sre_parse", ".", "MAXREPEAT", ":", "range_str", "=", "'*'", "elif", "i", "[", "1", "]", "[", "0", "]", "==", "1", "and", "i", "[", "1", "]", "[", "1", "]", "-", "i", "[", "1", "]", "[", "0", "]", "==", "sre_parse", ".", "MAXREPEAT", "-", "1", ":", "range_str", "=", "'+'", "else", ":", "range_str", "=", "'{{{0},{1}}}'", ".", "format", "(", "i", "[", "1", "]", "[", "0", "]", ",", "i", "[", "1", "]", "[", "1", "]", ")", "ret", "+=", "sre_to_string", "(", "i", "[", "1", "]", "[", "2", "]", ",", "paren", "=", "paren", ")", "+", "range_str", "elif", "i", "[", "0", "]", "==", "sre_parse", ".", "MIN_REPEAT", ":", "if", "i", "[", "1", "]", "[", "0", "]", "==", "0", "and", "i", "[", "1", "]", "[", "1", "]", "==", "sre_parse", ".", "MAXREPEAT", ":", "range_str", "=", "'*?'", "elif", "i", "[", "1", "]", "[", "0", "]", "==", "1", "and", "i", "[", "1", "]", "[", "1", "]", "==", "sre_parse", ".", "MAXREPEAT", ":", "range_str", "=", "'+?'", "elif", "i", "[", "1", "]", "[", "1", "]", "==", "sre_parse", ".", "MAXREPEAT", ":", "range_str", "=", "'{{{0},}}?'", ".", "format", "(", "i", "[", "1", "]", "[", "0", "]", ")", "else", ":", "range_str", "=", "'{{{0},{1}}}?'", ".", "format", "(", "i", "[", "1", "]", "[", "0", "]", ",", "i", "[", "1", "]", "[", "1", "]", ")", "ret", "+=", "sre_to_string", "(", "i", "[", "1", "]", "[", "2", "]", ",", "paren", "=", "paren", ")", "+", "range_str", "elif", "i", "[", "0", "]", "==", "sre_parse", ".", "GROUPREF", ":", "ret", "+=", "'\\\\{0}'", ".", "format", "(", "i", "[", "1", "]", ")", "elif", "i", "[", "0", "]", "==", "sre_parse", ".", "AT", ":", "if", "i", "[", "1", "]", "==", "sre_parse", ".", "AT_BEGINNING", ":", "ret", "+=", "'^'", "elif", "i", "[", "1", "]", "==", "sre_parse", ".", "AT_END", ":", "ret", "+=", "'$'", "elif", "i", "[", "0", "]", "==", "sre_parse", ".", "NEGATE", ":", "pass", "elif", "i", "[", "0", "]", "==", "sre_parse", ".", "RANGE", ":", "ret", "+=", "'{0}-{1}'", ".", "format", "(", "unichr", "(", "i", "[", "1", "]", "[", "0", "]", ")", ",", "unichr", "(", "i", "[", "1", "]", "[", "1", "]", ")", ")", "elif", "i", "[", "0", "]", "==", "sre_parse", ".", "ASSERT", ":", "if", "i", "[", "1", "]", "[", "0", "]", ":", "ret", "+=", "'(?={0})'", ".", "format", "(", "sre_to_string", "(", "i", "[", "1", "]", "[", "1", "]", ",", "paren", "=", "False", ")", ")", "else", ":", "ret", "+=", "'{0}'", ".", "format", "(", "sre_to_string", "(", "i", "[", "1", "]", "[", "1", "]", ",", "paren", "=", "paren", ")", ")", "elif", "i", "[", "0", "]", "==", "sre_parse", ".", "ASSERT_NOT", ":", "pass", "else", ":", "print", "(", "'[!] cannot handle expression \"%s\"'", "%", "str", "(", "i", ")", ")", "return", "ret" ]
69733409042b526da584c675907a316ad708a8d4
valid
parse
Regular expression parser :param s: Regular expression :type s: str :rtype: list
exrex.py
def parse(s): """Regular expression parser :param s: Regular expression :type s: str :rtype: list """ if IS_PY3: r = sre_parse.parse(s, flags=U) else: r = sre_parse.parse(s.decode('utf-8'), flags=U) return list(r)
def parse(s): """Regular expression parser :param s: Regular expression :type s: str :rtype: list """ if IS_PY3: r = sre_parse.parse(s, flags=U) else: r = sre_parse.parse(s.decode('utf-8'), flags=U) return list(r)
[ "Regular", "expression", "parser" ]
asciimoo/exrex
python
https://github.com/asciimoo/exrex/blob/69733409042b526da584c675907a316ad708a8d4/exrex.py#L396-L407
[ "def", "parse", "(", "s", ")", ":", "if", "IS_PY3", ":", "r", "=", "sre_parse", ".", "parse", "(", "s", ",", "flags", "=", "U", ")", "else", ":", "r", "=", "sre_parse", ".", "parse", "(", "s", ".", "decode", "(", "'utf-8'", ")", ",", "flags", "=", "U", ")", "return", "list", "(", "r", ")" ]
69733409042b526da584c675907a316ad708a8d4
valid
ib64_patched
Patch isBase64 to prevent Base64 encoding of JSON content
pyzotero/zotero.py
def ib64_patched(self, attrsD, contentparams): """ Patch isBase64 to prevent Base64 encoding of JSON content """ if attrsD.get("mode", "") == "base64": return 0 if self.contentparams["type"].startswith("text/"): return 0 if self.contentparams["type"].endswith("+xml"): return 0 if self.contentparams["type"].endswith("/xml"): return 0 if self.contentparams["type"].endswith("/json"): return 0 return 0
def ib64_patched(self, attrsD, contentparams): """ Patch isBase64 to prevent Base64 encoding of JSON content """ if attrsD.get("mode", "") == "base64": return 0 if self.contentparams["type"].startswith("text/"): return 0 if self.contentparams["type"].endswith("+xml"): return 0 if self.contentparams["type"].endswith("/xml"): return 0 if self.contentparams["type"].endswith("/json"): return 0 return 0
[ "Patch", "isBase64", "to", "prevent", "Base64", "encoding", "of", "JSON", "content" ]
urschrei/pyzotero
python
https://github.com/urschrei/pyzotero/blob/b378966b30146a952f7953c23202fb5a1ddf81d9/pyzotero/zotero.py#L78-L91
[ "def", "ib64_patched", "(", "self", ",", "attrsD", ",", "contentparams", ")", ":", "if", "attrsD", ".", "get", "(", "\"mode\"", ",", "\"\"", ")", "==", "\"base64\"", ":", "return", "0", "if", "self", ".", "contentparams", "[", "\"type\"", "]", ".", "startswith", "(", "\"text/\"", ")", ":", "return", "0", "if", "self", ".", "contentparams", "[", "\"type\"", "]", ".", "endswith", "(", "\"+xml\"", ")", ":", "return", "0", "if", "self", ".", "contentparams", "[", "\"type\"", "]", ".", "endswith", "(", "\"/xml\"", ")", ":", "return", "0", "if", "self", ".", "contentparams", "[", "\"type\"", "]", ".", "endswith", "(", "\"/json\"", ")", ":", "return", "0", "return", "0" ]
b378966b30146a952f7953c23202fb5a1ddf81d9
valid
cleanwrap
Wrapper for Zotero._cleanup
pyzotero/zotero.py
def cleanwrap(func): """ Wrapper for Zotero._cleanup """ def enc(self, *args, **kwargs): """ Send each item to _cleanup() """ return (func(self, item, **kwargs) for item in args) return enc
def cleanwrap(func): """ Wrapper for Zotero._cleanup """ def enc(self, *args, **kwargs): """ Send each item to _cleanup() """ return (func(self, item, **kwargs) for item in args) return enc
[ "Wrapper", "for", "Zotero", ".", "_cleanup" ]
urschrei/pyzotero
python
https://github.com/urschrei/pyzotero/blob/b378966b30146a952f7953c23202fb5a1ddf81d9/pyzotero/zotero.py#L104-L112
[ "def", "cleanwrap", "(", "func", ")", ":", "def", "enc", "(", "self", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "\"\"\" Send each item to _cleanup() \"\"\"", "return", "(", "func", "(", "self", ",", "item", ",", "*", "*", "kwargs", ")", "for", "item", "in", "args", ")", "return", "enc" ]
b378966b30146a952f7953c23202fb5a1ddf81d9
valid
retrieve
Decorator for Zotero read API methods; calls _retrieve_data() and passes the result to the correct processor, based on a lookup
pyzotero/zotero.py
def retrieve(func): """ Decorator for Zotero read API methods; calls _retrieve_data() and passes the result to the correct processor, based on a lookup """ def wrapped_f(self, *args, **kwargs): """ Returns result of _retrieve_data() func's return value is part of a URI, and it's this which is intercepted and passed to _retrieve_data: '/users/123/items?key=abc123' """ if kwargs: self.add_parameters(**kwargs) retrieved = self._retrieve_data(func(self, *args)) # we now always have links in the header response self.links = self._extract_links() # determine content and format, based on url params content = ( self.content.search(self.request.url) and self.content.search(self.request.url).group(0) or "bib" ) # JSON by default formats = { "application/atom+xml": "atom", "application/x-bibtex": "bibtex", "application/json": "json", "text/html": "snapshot", "text/plain": "plain", "application/pdf; charset=utf-8": "pdf", "application/pdf": "pdf", "application/msword": "doc", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": "xlsx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document": "docx", "application/zip": "zip", "application/epub+zip": "zip", "audio/mpeg": "mp3", "video/mp4": "mp4", "audio/x-wav": "wav", "video/x-msvideo": "avi", "application/octet-stream": "octet", "application/x-tex": "tex", "application/x-texinfo": "texinfo", "image/jpeg": "jpeg", "image/png": "png", "image/gif": "gif", "image/tiff": "tiff", "application/postscript": "postscript", "application/rtf": "rtf", } # select format, or assume JSON content_type_header = self.request.headers["Content-Type"].lower() + ";" re.compile("\s+") fmt = formats.get( # strip "; charset=..." segment content_type_header[0: content_type_header.index(";")], "json", ) # clear all query parameters self.url_params = None # check to see whether it's tag data if "tags" in self.request.url: self.tag_data = False return self._tags_data(retrieved.json()) if fmt == "atom": parsed = feedparser.parse(retrieved.text) # select the correct processor processor = self.processors.get(content) # process the content correctly with a custom rule return processor(parsed) if fmt == "snapshot": # we need to dump as a zip! self.snapshot = True if fmt == "bibtex": parser = bibtexparser.bparser.BibTexParser(common_strings=True) return parser.parse(retrieved.text) # it's binary, so return raw content elif fmt != "json": return retrieved.content # no need to do anything special, return JSON else: return retrieved.json() return wrapped_f
def retrieve(func): """ Decorator for Zotero read API methods; calls _retrieve_data() and passes the result to the correct processor, based on a lookup """ def wrapped_f(self, *args, **kwargs): """ Returns result of _retrieve_data() func's return value is part of a URI, and it's this which is intercepted and passed to _retrieve_data: '/users/123/items?key=abc123' """ if kwargs: self.add_parameters(**kwargs) retrieved = self._retrieve_data(func(self, *args)) # we now always have links in the header response self.links = self._extract_links() # determine content and format, based on url params content = ( self.content.search(self.request.url) and self.content.search(self.request.url).group(0) or "bib" ) # JSON by default formats = { "application/atom+xml": "atom", "application/x-bibtex": "bibtex", "application/json": "json", "text/html": "snapshot", "text/plain": "plain", "application/pdf; charset=utf-8": "pdf", "application/pdf": "pdf", "application/msword": "doc", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": "xlsx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document": "docx", "application/zip": "zip", "application/epub+zip": "zip", "audio/mpeg": "mp3", "video/mp4": "mp4", "audio/x-wav": "wav", "video/x-msvideo": "avi", "application/octet-stream": "octet", "application/x-tex": "tex", "application/x-texinfo": "texinfo", "image/jpeg": "jpeg", "image/png": "png", "image/gif": "gif", "image/tiff": "tiff", "application/postscript": "postscript", "application/rtf": "rtf", } # select format, or assume JSON content_type_header = self.request.headers["Content-Type"].lower() + ";" re.compile("\s+") fmt = formats.get( # strip "; charset=..." segment content_type_header[0: content_type_header.index(";")], "json", ) # clear all query parameters self.url_params = None # check to see whether it's tag data if "tags" in self.request.url: self.tag_data = False return self._tags_data(retrieved.json()) if fmt == "atom": parsed = feedparser.parse(retrieved.text) # select the correct processor processor = self.processors.get(content) # process the content correctly with a custom rule return processor(parsed) if fmt == "snapshot": # we need to dump as a zip! self.snapshot = True if fmt == "bibtex": parser = bibtexparser.bparser.BibTexParser(common_strings=True) return parser.parse(retrieved.text) # it's binary, so return raw content elif fmt != "json": return retrieved.content # no need to do anything special, return JSON else: return retrieved.json() return wrapped_f
[ "Decorator", "for", "Zotero", "read", "API", "methods", ";", "calls", "_retrieve_data", "()", "and", "passes", "the", "result", "to", "the", "correct", "processor", "based", "on", "a", "lookup" ]
urschrei/pyzotero
python
https://github.com/urschrei/pyzotero/blob/b378966b30146a952f7953c23202fb5a1ddf81d9/pyzotero/zotero.py#L121-L207
[ "def", "retrieve", "(", "func", ")", ":", "def", "wrapped_f", "(", "self", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "\"\"\"\n Returns result of _retrieve_data()\n\n func's return value is part of a URI, and it's this\n which is intercepted and passed to _retrieve_data:\n '/users/123/items?key=abc123'\n \"\"\"", "if", "kwargs", ":", "self", ".", "add_parameters", "(", "*", "*", "kwargs", ")", "retrieved", "=", "self", ".", "_retrieve_data", "(", "func", "(", "self", ",", "*", "args", ")", ")", "# we now always have links in the header response", "self", ".", "links", "=", "self", ".", "_extract_links", "(", ")", "# determine content and format, based on url params", "content", "=", "(", "self", ".", "content", ".", "search", "(", "self", ".", "request", ".", "url", ")", "and", "self", ".", "content", ".", "search", "(", "self", ".", "request", ".", "url", ")", ".", "group", "(", "0", ")", "or", "\"bib\"", ")", "# JSON by default", "formats", "=", "{", "\"application/atom+xml\"", ":", "\"atom\"", ",", "\"application/x-bibtex\"", ":", "\"bibtex\"", ",", "\"application/json\"", ":", "\"json\"", ",", "\"text/html\"", ":", "\"snapshot\"", ",", "\"text/plain\"", ":", "\"plain\"", ",", "\"application/pdf; charset=utf-8\"", ":", "\"pdf\"", ",", "\"application/pdf\"", ":", "\"pdf\"", ",", "\"application/msword\"", ":", "\"doc\"", ",", "\"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet\"", ":", "\"xlsx\"", ",", "\"application/vnd.openxmlformats-officedocument.wordprocessingml.document\"", ":", "\"docx\"", ",", "\"application/zip\"", ":", "\"zip\"", ",", "\"application/epub+zip\"", ":", "\"zip\"", ",", "\"audio/mpeg\"", ":", "\"mp3\"", ",", "\"video/mp4\"", ":", "\"mp4\"", ",", "\"audio/x-wav\"", ":", "\"wav\"", ",", "\"video/x-msvideo\"", ":", "\"avi\"", ",", "\"application/octet-stream\"", ":", "\"octet\"", ",", "\"application/x-tex\"", ":", "\"tex\"", ",", "\"application/x-texinfo\"", ":", "\"texinfo\"", ",", "\"image/jpeg\"", ":", "\"jpeg\"", ",", "\"image/png\"", ":", "\"png\"", ",", "\"image/gif\"", ":", "\"gif\"", ",", "\"image/tiff\"", ":", "\"tiff\"", ",", "\"application/postscript\"", ":", "\"postscript\"", ",", "\"application/rtf\"", ":", "\"rtf\"", ",", "}", "# select format, or assume JSON", "content_type_header", "=", "self", ".", "request", ".", "headers", "[", "\"Content-Type\"", "]", ".", "lower", "(", ")", "+", "\";\"", "re", ".", "compile", "(", "\"\\s+\"", ")", "fmt", "=", "formats", ".", "get", "(", "# strip \"; charset=...\" segment", "content_type_header", "[", "0", ":", "content_type_header", ".", "index", "(", "\";\"", ")", "]", ",", "\"json\"", ",", ")", "# clear all query parameters", "self", ".", "url_params", "=", "None", "# check to see whether it's tag data", "if", "\"tags\"", "in", "self", ".", "request", ".", "url", ":", "self", ".", "tag_data", "=", "False", "return", "self", ".", "_tags_data", "(", "retrieved", ".", "json", "(", ")", ")", "if", "fmt", "==", "\"atom\"", ":", "parsed", "=", "feedparser", ".", "parse", "(", "retrieved", ".", "text", ")", "# select the correct processor", "processor", "=", "self", ".", "processors", ".", "get", "(", "content", ")", "# process the content correctly with a custom rule", "return", "processor", "(", "parsed", ")", "if", "fmt", "==", "\"snapshot\"", ":", "# we need to dump as a zip!", "self", ".", "snapshot", "=", "True", "if", "fmt", "==", "\"bibtex\"", ":", "parser", "=", "bibtexparser", ".", "bparser", ".", "BibTexParser", "(", "common_strings", "=", "True", ")", "return", "parser", ".", "parse", "(", "retrieved", ".", "text", ")", "# it's binary, so return raw content", "elif", "fmt", "!=", "\"json\"", ":", "return", "retrieved", ".", "content", "# no need to do anything special, return JSON", "else", ":", "return", "retrieved", ".", "json", "(", ")", "return", "wrapped_f" ]
b378966b30146a952f7953c23202fb5a1ddf81d9
valid
ss_wrap
ensure that a SavedSearch object exists
pyzotero/zotero.py
def ss_wrap(func): """ ensure that a SavedSearch object exists """ def wrapper(self, *args, **kwargs): if not self.savedsearch: self.savedsearch = SavedSearch(self) return func(self, *args, **kwargs) return wrapper
def ss_wrap(func): """ ensure that a SavedSearch object exists """ def wrapper(self, *args, **kwargs): if not self.savedsearch: self.savedsearch = SavedSearch(self) return func(self, *args, **kwargs) return wrapper
[ "ensure", "that", "a", "SavedSearch", "object", "exists" ]
urschrei/pyzotero
python
https://github.com/urschrei/pyzotero/blob/b378966b30146a952f7953c23202fb5a1ddf81d9/pyzotero/zotero.py#L210-L218
[ "def", "ss_wrap", "(", "func", ")", ":", "def", "wrapper", "(", "self", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "if", "not", "self", ".", "savedsearch", ":", "self", ".", "savedsearch", "=", "SavedSearch", "(", "self", ")", "return", "func", "(", "self", ",", "*", "args", ",", "*", "*", "kwargs", ")", "return", "wrapper" ]
b378966b30146a952f7953c23202fb5a1ddf81d9
valid
error_handler
Error handler for HTTP requests
pyzotero/zotero.py
def error_handler(req): """ Error handler for HTTP requests """ error_codes = { 400: ze.UnsupportedParams, 401: ze.UserNotAuthorised, 403: ze.UserNotAuthorised, 404: ze.ResourceNotFound, 409: ze.Conflict, 412: ze.PreConditionFailed, 413: ze.RequestEntityTooLarge, 428: ze.PreConditionRequired, 429: ze.TooManyRequests, } def err_msg(req): """ Return a nicely-formatted error message """ return "\nCode: %s\nURL: %s\nMethod: %s\nResponse: %s" % ( req.status_code, # error.msg, req.url, req.request.method, req.text, ) if error_codes.get(req.status_code): # check to see whether its 429 if req.status_code == 429: # call our back-off function delay = backoff.delay if delay > 32: # we've waited a total of 62 seconds (2 + 4 … + 32), so give up backoff.reset() raise ze.TooManyRetries( "Continuing to receive HTTP 429 \ responses after 62 seconds. You are being rate-limited, try again later" ) time.sleep(delay) sess = requests.Session() new_req = sess.send(req.request) try: new_req.raise_for_status() except requests.exceptions.HTTPError: error_handler(new_req) else: raise error_codes.get(req.status_code)(err_msg(req)) else: raise ze.HTTPError(err_msg(req))
def error_handler(req): """ Error handler for HTTP requests """ error_codes = { 400: ze.UnsupportedParams, 401: ze.UserNotAuthorised, 403: ze.UserNotAuthorised, 404: ze.ResourceNotFound, 409: ze.Conflict, 412: ze.PreConditionFailed, 413: ze.RequestEntityTooLarge, 428: ze.PreConditionRequired, 429: ze.TooManyRequests, } def err_msg(req): """ Return a nicely-formatted error message """ return "\nCode: %s\nURL: %s\nMethod: %s\nResponse: %s" % ( req.status_code, # error.msg, req.url, req.request.method, req.text, ) if error_codes.get(req.status_code): # check to see whether its 429 if req.status_code == 429: # call our back-off function delay = backoff.delay if delay > 32: # we've waited a total of 62 seconds (2 + 4 … + 32), so give up backoff.reset() raise ze.TooManyRetries( "Continuing to receive HTTP 429 \ responses after 62 seconds. You are being rate-limited, try again later" ) time.sleep(delay) sess = requests.Session() new_req = sess.send(req.request) try: new_req.raise_for_status() except requests.exceptions.HTTPError: error_handler(new_req) else: raise error_codes.get(req.status_code)(err_msg(req)) else: raise ze.HTTPError(err_msg(req))
[ "Error", "handler", "for", "HTTP", "requests" ]
urschrei/pyzotero
python
https://github.com/urschrei/pyzotero/blob/b378966b30146a952f7953c23202fb5a1ddf81d9/pyzotero/zotero.py#L1541-L1589
[ "def", "error_handler", "(", "req", ")", ":", "error_codes", "=", "{", "400", ":", "ze", ".", "UnsupportedParams", ",", "401", ":", "ze", ".", "UserNotAuthorised", ",", "403", ":", "ze", ".", "UserNotAuthorised", ",", "404", ":", "ze", ".", "ResourceNotFound", ",", "409", ":", "ze", ".", "Conflict", ",", "412", ":", "ze", ".", "PreConditionFailed", ",", "413", ":", "ze", ".", "RequestEntityTooLarge", ",", "428", ":", "ze", ".", "PreConditionRequired", ",", "429", ":", "ze", ".", "TooManyRequests", ",", "}", "def", "err_msg", "(", "req", ")", ":", "\"\"\" Return a nicely-formatted error message\n \"\"\"", "return", "\"\\nCode: %s\\nURL: %s\\nMethod: %s\\nResponse: %s\"", "%", "(", "req", ".", "status_code", ",", "# error.msg,", "req", ".", "url", ",", "req", ".", "request", ".", "method", ",", "req", ".", "text", ",", ")", "if", "error_codes", ".", "get", "(", "req", ".", "status_code", ")", ":", "# check to see whether its 429", "if", "req", ".", "status_code", "==", "429", ":", "# call our back-off function", "delay", "=", "backoff", ".", "delay", "if", "delay", ">", "32", ":", "# we've waited a total of 62 seconds (2 + 4 … + 32), so give up", "backoff", ".", "reset", "(", ")", "raise", "ze", ".", "TooManyRetries", "(", "\"Continuing to receive HTTP 429 \\\nresponses after 62 seconds. You are being rate-limited, try again later\"", ")", "time", ".", "sleep", "(", "delay", ")", "sess", "=", "requests", ".", "Session", "(", ")", "new_req", "=", "sess", ".", "send", "(", "req", ".", "request", ")", "try", ":", "new_req", ".", "raise_for_status", "(", ")", "except", "requests", ".", "exceptions", ".", "HTTPError", ":", "error_handler", "(", "new_req", ")", "else", ":", "raise", "error_codes", ".", "get", "(", "req", ".", "status_code", ")", "(", "err_msg", "(", "req", ")", ")", "else", ":", "raise", "ze", ".", "HTTPError", "(", "err_msg", "(", "req", ")", ")" ]
b378966b30146a952f7953c23202fb5a1ddf81d9
valid
Zotero.default_headers
It's always OK to include these headers
pyzotero/zotero.py
def default_headers(self): """ It's always OK to include these headers """ _headers = { "User-Agent": "Pyzotero/%s" % __version__, "Zotero-API-Version": "%s" % __api_version__, } if self.api_key: _headers["Authorization"] = "Bearer %s" % self.api_key return _headers
def default_headers(self): """ It's always OK to include these headers """ _headers = { "User-Agent": "Pyzotero/%s" % __version__, "Zotero-API-Version": "%s" % __api_version__, } if self.api_key: _headers["Authorization"] = "Bearer %s" % self.api_key return _headers
[ "It", "s", "always", "OK", "to", "include", "these", "headers" ]
urschrei/pyzotero
python
https://github.com/urschrei/pyzotero/blob/b378966b30146a952f7953c23202fb5a1ddf81d9/pyzotero/zotero.py#L281-L291
[ "def", "default_headers", "(", "self", ")", ":", "_headers", "=", "{", "\"User-Agent\"", ":", "\"Pyzotero/%s\"", "%", "__version__", ",", "\"Zotero-API-Version\"", ":", "\"%s\"", "%", "__api_version__", ",", "}", "if", "self", ".", "api_key", ":", "_headers", "[", "\"Authorization\"", "]", "=", "\"Bearer %s\"", "%", "self", ".", "api_key", "return", "_headers" ]
b378966b30146a952f7953c23202fb5a1ddf81d9
valid
Zotero._cache
Add a retrieved template to the cache for 304 checking accepts a dict and key name, adds the retrieval time, and adds both to self.templates as a new dict using the specified key
pyzotero/zotero.py
def _cache(self, response, key): """ Add a retrieved template to the cache for 304 checking accepts a dict and key name, adds the retrieval time, and adds both to self.templates as a new dict using the specified key """ # cache template and retrieval time for subsequent calls thetime = datetime.datetime.utcnow().replace(tzinfo=pytz.timezone("GMT")) self.templates[key] = {"tmplt": response.json(), "updated": thetime} return copy.deepcopy(response.json())
def _cache(self, response, key): """ Add a retrieved template to the cache for 304 checking accepts a dict and key name, adds the retrieval time, and adds both to self.templates as a new dict using the specified key """ # cache template and retrieval time for subsequent calls thetime = datetime.datetime.utcnow().replace(tzinfo=pytz.timezone("GMT")) self.templates[key] = {"tmplt": response.json(), "updated": thetime} return copy.deepcopy(response.json())
[ "Add", "a", "retrieved", "template", "to", "the", "cache", "for", "304", "checking", "accepts", "a", "dict", "and", "key", "name", "adds", "the", "retrieval", "time", "and", "adds", "both", "to", "self", ".", "templates", "as", "a", "new", "dict", "using", "the", "specified", "key" ]
urschrei/pyzotero
python
https://github.com/urschrei/pyzotero/blob/b378966b30146a952f7953c23202fb5a1ddf81d9/pyzotero/zotero.py#L293-L302
[ "def", "_cache", "(", "self", ",", "response", ",", "key", ")", ":", "# cache template and retrieval time for subsequent calls", "thetime", "=", "datetime", ".", "datetime", ".", "utcnow", "(", ")", ".", "replace", "(", "tzinfo", "=", "pytz", ".", "timezone", "(", "\"GMT\"", ")", ")", "self", ".", "templates", "[", "key", "]", "=", "{", "\"tmplt\"", ":", "response", ".", "json", "(", ")", ",", "\"updated\"", ":", "thetime", "}", "return", "copy", ".", "deepcopy", "(", "response", ".", "json", "(", ")", ")" ]
b378966b30146a952f7953c23202fb5a1ddf81d9
valid
Zotero._cleanup
Remove keys we added for internal use
pyzotero/zotero.py
def _cleanup(self, to_clean, allow=()): """ Remove keys we added for internal use """ # this item's been retrieved from the API, we only need the 'data' # entry if to_clean.keys() == ["links", "library", "version", "meta", "key", "data"]: to_clean = to_clean["data"] return dict( [ [k, v] for k, v in list(to_clean.items()) if (k in allow or k not in self.temp_keys) ] )
def _cleanup(self, to_clean, allow=()): """ Remove keys we added for internal use """ # this item's been retrieved from the API, we only need the 'data' # entry if to_clean.keys() == ["links", "library", "version", "meta", "key", "data"]: to_clean = to_clean["data"] return dict( [ [k, v] for k, v in list(to_clean.items()) if (k in allow or k not in self.temp_keys) ] )
[ "Remove", "keys", "we", "added", "for", "internal", "use" ]
urschrei/pyzotero
python
https://github.com/urschrei/pyzotero/blob/b378966b30146a952f7953c23202fb5a1ddf81d9/pyzotero/zotero.py#L305-L318
[ "def", "_cleanup", "(", "self", ",", "to_clean", ",", "allow", "=", "(", ")", ")", ":", "# this item's been retrieved from the API, we only need the 'data'", "# entry", "if", "to_clean", ".", "keys", "(", ")", "==", "[", "\"links\"", ",", "\"library\"", ",", "\"version\"", ",", "\"meta\"", ",", "\"key\"", ",", "\"data\"", "]", ":", "to_clean", "=", "to_clean", "[", "\"data\"", "]", "return", "dict", "(", "[", "[", "k", ",", "v", "]", "for", "k", ",", "v", "in", "list", "(", "to_clean", ".", "items", "(", ")", ")", "if", "(", "k", "in", "allow", "or", "k", "not", "in", "self", ".", "temp_keys", ")", "]", ")" ]
b378966b30146a952f7953c23202fb5a1ddf81d9
valid
Zotero._retrieve_data
Retrieve Zotero items via the API Combine endpoint and request to access the specific resource Returns a JSON document
pyzotero/zotero.py
def _retrieve_data(self, request=None): """ Retrieve Zotero items via the API Combine endpoint and request to access the specific resource Returns a JSON document """ full_url = "%s%s" % (self.endpoint, request) # The API doesn't return this any more, so we have to cheat self.self_link = request self.request = requests.get(url=full_url, headers=self.default_headers()) self.request.encoding = "utf-8" try: self.request.raise_for_status() except requests.exceptions.HTTPError: error_handler(self.request) return self.request
def _retrieve_data(self, request=None): """ Retrieve Zotero items via the API Combine endpoint and request to access the specific resource Returns a JSON document """ full_url = "%s%s" % (self.endpoint, request) # The API doesn't return this any more, so we have to cheat self.self_link = request self.request = requests.get(url=full_url, headers=self.default_headers()) self.request.encoding = "utf-8" try: self.request.raise_for_status() except requests.exceptions.HTTPError: error_handler(self.request) return self.request
[ "Retrieve", "Zotero", "items", "via", "the", "API", "Combine", "endpoint", "and", "request", "to", "access", "the", "specific", "resource", "Returns", "a", "JSON", "document" ]
urschrei/pyzotero
python
https://github.com/urschrei/pyzotero/blob/b378966b30146a952f7953c23202fb5a1ddf81d9/pyzotero/zotero.py#L320-L335
[ "def", "_retrieve_data", "(", "self", ",", "request", "=", "None", ")", ":", "full_url", "=", "\"%s%s\"", "%", "(", "self", ".", "endpoint", ",", "request", ")", "# The API doesn't return this any more, so we have to cheat", "self", ".", "self_link", "=", "request", "self", ".", "request", "=", "requests", ".", "get", "(", "url", "=", "full_url", ",", "headers", "=", "self", ".", "default_headers", "(", ")", ")", "self", ".", "request", ".", "encoding", "=", "\"utf-8\"", "try", ":", "self", ".", "request", ".", "raise_for_status", "(", ")", "except", "requests", ".", "exceptions", ".", "HTTPError", ":", "error_handler", "(", "self", ".", "request", ")", "return", "self", ".", "request" ]
b378966b30146a952f7953c23202fb5a1ddf81d9
valid
Zotero._extract_links
Extract self, first, next, last links from a request response
pyzotero/zotero.py
def _extract_links(self): """ Extract self, first, next, last links from a request response """ extracted = dict() try: for key, value in self.request.links.items(): parsed = urlparse(value["url"]) fragment = "{path}?{query}".format(path=parsed[2], query=parsed[4]) extracted[key] = fragment # add a 'self' link parsed = list(urlparse(self.self_link)) # strip 'format' query parameter stripped = "&".join( [ "%s=%s" % (p[0], p[1]) for p in parse_qsl(parsed[4]) if p[0] != "format" ] ) # rebuild url fragment # this is a death march extracted["self"] = urlunparse( [parsed[0], parsed[1], parsed[2], parsed[3], stripped, parsed[5]] ) return extracted except KeyError: # No links present, because it's a single item return None
def _extract_links(self): """ Extract self, first, next, last links from a request response """ extracted = dict() try: for key, value in self.request.links.items(): parsed = urlparse(value["url"]) fragment = "{path}?{query}".format(path=parsed[2], query=parsed[4]) extracted[key] = fragment # add a 'self' link parsed = list(urlparse(self.self_link)) # strip 'format' query parameter stripped = "&".join( [ "%s=%s" % (p[0], p[1]) for p in parse_qsl(parsed[4]) if p[0] != "format" ] ) # rebuild url fragment # this is a death march extracted["self"] = urlunparse( [parsed[0], parsed[1], parsed[2], parsed[3], stripped, parsed[5]] ) return extracted except KeyError: # No links present, because it's a single item return None
[ "Extract", "self", "first", "next", "last", "links", "from", "a", "request", "response" ]
urschrei/pyzotero
python
https://github.com/urschrei/pyzotero/blob/b378966b30146a952f7953c23202fb5a1ddf81d9/pyzotero/zotero.py#L337-L365
[ "def", "_extract_links", "(", "self", ")", ":", "extracted", "=", "dict", "(", ")", "try", ":", "for", "key", ",", "value", "in", "self", ".", "request", ".", "links", ".", "items", "(", ")", ":", "parsed", "=", "urlparse", "(", "value", "[", "\"url\"", "]", ")", "fragment", "=", "\"{path}?{query}\"", ".", "format", "(", "path", "=", "parsed", "[", "2", "]", ",", "query", "=", "parsed", "[", "4", "]", ")", "extracted", "[", "key", "]", "=", "fragment", "# add a 'self' link", "parsed", "=", "list", "(", "urlparse", "(", "self", ".", "self_link", ")", ")", "# strip 'format' query parameter", "stripped", "=", "\"&\"", ".", "join", "(", "[", "\"%s=%s\"", "%", "(", "p", "[", "0", "]", ",", "p", "[", "1", "]", ")", "for", "p", "in", "parse_qsl", "(", "parsed", "[", "4", "]", ")", "if", "p", "[", "0", "]", "!=", "\"format\"", "]", ")", "# rebuild url fragment", "# this is a death march", "extracted", "[", "\"self\"", "]", "=", "urlunparse", "(", "[", "parsed", "[", "0", "]", ",", "parsed", "[", "1", "]", ",", "parsed", "[", "2", "]", ",", "parsed", "[", "3", "]", ",", "stripped", ",", "parsed", "[", "5", "]", "]", ")", "return", "extracted", "except", "KeyError", ":", "# No links present, because it's a single item", "return", "None" ]
b378966b30146a952f7953c23202fb5a1ddf81d9
valid
Zotero._updated
Generic call to see if a template request returns 304 accepts: - a string to combine with the API endpoint - a dict of format values, in case they're required by 'url' - a template name to check for As per the API docs, a template less than 1 hour old is assumed to be fresh, and will immediately return False if found
pyzotero/zotero.py
def _updated(self, url, payload, template=None): """ Generic call to see if a template request returns 304 accepts: - a string to combine with the API endpoint - a dict of format values, in case they're required by 'url' - a template name to check for As per the API docs, a template less than 1 hour old is assumed to be fresh, and will immediately return False if found """ # If the template is more than an hour old, try a 304 if ( abs( datetime.datetime.utcnow().replace(tzinfo=pytz.timezone("GMT")) - self.templates[template]["updated"] ).seconds > 3600 ): query = self.endpoint + url.format( u=self.library_id, t=self.library_type, **payload ) headers = { "If-Modified-Since": payload["updated"].strftime( "%a, %d %b %Y %H:%M:%S %Z" ) } headers.update(self.default_headers()) # perform the request, and check whether the response returns 304 req = requests.get(query, headers=headers) try: req.raise_for_status() except requests.exceptions.HTTPError: error_handler(req) return req.status_code == 304 # Still plenty of life left in't return False
def _updated(self, url, payload, template=None): """ Generic call to see if a template request returns 304 accepts: - a string to combine with the API endpoint - a dict of format values, in case they're required by 'url' - a template name to check for As per the API docs, a template less than 1 hour old is assumed to be fresh, and will immediately return False if found """ # If the template is more than an hour old, try a 304 if ( abs( datetime.datetime.utcnow().replace(tzinfo=pytz.timezone("GMT")) - self.templates[template]["updated"] ).seconds > 3600 ): query = self.endpoint + url.format( u=self.library_id, t=self.library_type, **payload ) headers = { "If-Modified-Since": payload["updated"].strftime( "%a, %d %b %Y %H:%M:%S %Z" ) } headers.update(self.default_headers()) # perform the request, and check whether the response returns 304 req = requests.get(query, headers=headers) try: req.raise_for_status() except requests.exceptions.HTTPError: error_handler(req) return req.status_code == 304 # Still plenty of life left in't return False
[ "Generic", "call", "to", "see", "if", "a", "template", "request", "returns", "304", "accepts", ":", "-", "a", "string", "to", "combine", "with", "the", "API", "endpoint", "-", "a", "dict", "of", "format", "values", "in", "case", "they", "re", "required", "by", "url", "-", "a", "template", "name", "to", "check", "for", "As", "per", "the", "API", "docs", "a", "template", "less", "than", "1", "hour", "old", "is", "assumed", "to", "be", "fresh", "and", "will", "immediately", "return", "False", "if", "found" ]
urschrei/pyzotero
python
https://github.com/urschrei/pyzotero/blob/b378966b30146a952f7953c23202fb5a1ddf81d9/pyzotero/zotero.py#L367-L402
[ "def", "_updated", "(", "self", ",", "url", ",", "payload", ",", "template", "=", "None", ")", ":", "# If the template is more than an hour old, try a 304", "if", "(", "abs", "(", "datetime", ".", "datetime", ".", "utcnow", "(", ")", ".", "replace", "(", "tzinfo", "=", "pytz", ".", "timezone", "(", "\"GMT\"", ")", ")", "-", "self", ".", "templates", "[", "template", "]", "[", "\"updated\"", "]", ")", ".", "seconds", ">", "3600", ")", ":", "query", "=", "self", ".", "endpoint", "+", "url", ".", "format", "(", "u", "=", "self", ".", "library_id", ",", "t", "=", "self", ".", "library_type", ",", "*", "*", "payload", ")", "headers", "=", "{", "\"If-Modified-Since\"", ":", "payload", "[", "\"updated\"", "]", ".", "strftime", "(", "\"%a, %d %b %Y %H:%M:%S %Z\"", ")", "}", "headers", ".", "update", "(", "self", ".", "default_headers", "(", ")", ")", "# perform the request, and check whether the response returns 304", "req", "=", "requests", ".", "get", "(", "query", ",", "headers", "=", "headers", ")", "try", ":", "req", ".", "raise_for_status", "(", ")", "except", "requests", ".", "exceptions", ".", "HTTPError", ":", "error_handler", "(", "req", ")", "return", "req", ".", "status_code", "==", "304", "# Still plenty of life left in't", "return", "False" ]
b378966b30146a952f7953c23202fb5a1ddf81d9
valid
Zotero.add_parameters
Add URL parameters Also ensure that only valid format/content combinations are requested
pyzotero/zotero.py
def add_parameters(self, **params): """ Add URL parameters Also ensure that only valid format/content combinations are requested """ self.url_params = None # we want JSON by default if not params.get("format"): params["format"] = "json" # non-standard content must be retrieved as Atom if params.get("content"): params["format"] = "atom" # TODO: rewrite format=atom, content=json request if "limit" not in params or params.get("limit") == 0: params["limit"] = 100 # Need ability to request arbitrary number of results for version # response # -1 value is hack that works with current version elif params["limit"] == -1 or params["limit"] is None: del params["limit"] # bib format can't have a limit if params.get("format") == "bib": del params["limit"] self.url_params = urlencode(params, doseq=True)
def add_parameters(self, **params): """ Add URL parameters Also ensure that only valid format/content combinations are requested """ self.url_params = None # we want JSON by default if not params.get("format"): params["format"] = "json" # non-standard content must be retrieved as Atom if params.get("content"): params["format"] = "atom" # TODO: rewrite format=atom, content=json request if "limit" not in params or params.get("limit") == 0: params["limit"] = 100 # Need ability to request arbitrary number of results for version # response # -1 value is hack that works with current version elif params["limit"] == -1 or params["limit"] is None: del params["limit"] # bib format can't have a limit if params.get("format") == "bib": del params["limit"] self.url_params = urlencode(params, doseq=True)
[ "Add", "URL", "parameters", "Also", "ensure", "that", "only", "valid", "format", "/", "content", "combinations", "are", "requested" ]
urschrei/pyzotero
python
https://github.com/urschrei/pyzotero/blob/b378966b30146a952f7953c23202fb5a1ddf81d9/pyzotero/zotero.py#L404-L427
[ "def", "add_parameters", "(", "self", ",", "*", "*", "params", ")", ":", "self", ".", "url_params", "=", "None", "# we want JSON by default", "if", "not", "params", ".", "get", "(", "\"format\"", ")", ":", "params", "[", "\"format\"", "]", "=", "\"json\"", "# non-standard content must be retrieved as Atom", "if", "params", ".", "get", "(", "\"content\"", ")", ":", "params", "[", "\"format\"", "]", "=", "\"atom\"", "# TODO: rewrite format=atom, content=json request", "if", "\"limit\"", "not", "in", "params", "or", "params", ".", "get", "(", "\"limit\"", ")", "==", "0", ":", "params", "[", "\"limit\"", "]", "=", "100", "# Need ability to request arbitrary number of results for version", "# response", "# -1 value is hack that works with current version", "elif", "params", "[", "\"limit\"", "]", "==", "-", "1", "or", "params", "[", "\"limit\"", "]", "is", "None", ":", "del", "params", "[", "\"limit\"", "]", "# bib format can't have a limit", "if", "params", ".", "get", "(", "\"format\"", ")", "==", "\"bib\"", ":", "del", "params", "[", "\"limit\"", "]", "self", ".", "url_params", "=", "urlencode", "(", "params", ",", "doseq", "=", "True", ")" ]
b378966b30146a952f7953c23202fb5a1ddf81d9
valid
Zotero._build_query
Set request parameters. Will always add the user ID if it hasn't been specifically set by an API method
pyzotero/zotero.py
def _build_query(self, query_string, no_params=False): """ Set request parameters. Will always add the user ID if it hasn't been specifically set by an API method """ try: query = quote(query_string.format(u=self.library_id, t=self.library_type)) except KeyError as err: raise ze.ParamNotPassed("There's a request parameter missing: %s" % err) # Add the URL parameters and the user key, if necessary if no_params is False: if not self.url_params: self.add_parameters() query = "%s?%s" % (query, self.url_params) return query
def _build_query(self, query_string, no_params=False): """ Set request parameters. Will always add the user ID if it hasn't been specifically set by an API method """ try: query = quote(query_string.format(u=self.library_id, t=self.library_type)) except KeyError as err: raise ze.ParamNotPassed("There's a request parameter missing: %s" % err) # Add the URL parameters and the user key, if necessary if no_params is False: if not self.url_params: self.add_parameters() query = "%s?%s" % (query, self.url_params) return query
[ "Set", "request", "parameters", ".", "Will", "always", "add", "the", "user", "ID", "if", "it", "hasn", "t", "been", "specifically", "set", "by", "an", "API", "method" ]
urschrei/pyzotero
python
https://github.com/urschrei/pyzotero/blob/b378966b30146a952f7953c23202fb5a1ddf81d9/pyzotero/zotero.py#L429-L443
[ "def", "_build_query", "(", "self", ",", "query_string", ",", "no_params", "=", "False", ")", ":", "try", ":", "query", "=", "quote", "(", "query_string", ".", "format", "(", "u", "=", "self", ".", "library_id", ",", "t", "=", "self", ".", "library_type", ")", ")", "except", "KeyError", "as", "err", ":", "raise", "ze", ".", "ParamNotPassed", "(", "\"There's a request parameter missing: %s\"", "%", "err", ")", "# Add the URL parameters and the user key, if necessary", "if", "no_params", "is", "False", ":", "if", "not", "self", ".", "url_params", ":", "self", ".", "add_parameters", "(", ")", "query", "=", "\"%s?%s\"", "%", "(", "query", ",", "self", ".", "url_params", ")", "return", "query" ]
b378966b30146a952f7953c23202fb5a1ddf81d9
valid
Zotero.publications
Return the contents of My Publications
pyzotero/zotero.py
def publications(self): """ Return the contents of My Publications """ if self.library_type != "users": raise ze.CallDoesNotExist( "This API call does not exist for group libraries" ) query_string = "/{t}/{u}/publications/items" return self._build_query(query_string)
def publications(self): """ Return the contents of My Publications """ if self.library_type != "users": raise ze.CallDoesNotExist( "This API call does not exist for group libraries" ) query_string = "/{t}/{u}/publications/items" return self._build_query(query_string)
[ "Return", "the", "contents", "of", "My", "Publications" ]
urschrei/pyzotero
python
https://github.com/urschrei/pyzotero/blob/b378966b30146a952f7953c23202fb5a1ddf81d9/pyzotero/zotero.py#L446-L454
[ "def", "publications", "(", "self", ")", ":", "if", "self", ".", "library_type", "!=", "\"users\"", ":", "raise", "ze", ".", "CallDoesNotExist", "(", "\"This API call does not exist for group libraries\"", ")", "query_string", "=", "\"/{t}/{u}/publications/items\"", "return", "self", ".", "_build_query", "(", "query_string", ")" ]
b378966b30146a952f7953c23202fb5a1ddf81d9
valid
Zotero.num_collectionitems
Return the total number of items in the specified collection
pyzotero/zotero.py
def num_collectionitems(self, collection): """ Return the total number of items in the specified collection """ query = "/{t}/{u}/collections/{c}/items".format( u=self.library_id, t=self.library_type, c=collection.upper() ) return self._totals(query)
def num_collectionitems(self, collection): """ Return the total number of items in the specified collection """ query = "/{t}/{u}/collections/{c}/items".format( u=self.library_id, t=self.library_type, c=collection.upper() ) return self._totals(query)
[ "Return", "the", "total", "number", "of", "items", "in", "the", "specified", "collection" ]
urschrei/pyzotero
python
https://github.com/urschrei/pyzotero/blob/b378966b30146a952f7953c23202fb5a1ddf81d9/pyzotero/zotero.py#L469-L475
[ "def", "num_collectionitems", "(", "self", ",", "collection", ")", ":", "query", "=", "\"/{t}/{u}/collections/{c}/items\"", ".", "format", "(", "u", "=", "self", ".", "library_id", ",", "t", "=", "self", ".", "library_type", ",", "c", "=", "collection", ".", "upper", "(", ")", ")", "return", "self", ".", "_totals", "(", "query", ")" ]
b378966b30146a952f7953c23202fb5a1ddf81d9
valid
Zotero.num_tagitems
Return the total number of items for the specified tag
pyzotero/zotero.py
def num_tagitems(self, tag): """ Return the total number of items for the specified tag """ query = "/{t}/{u}/tags/{ta}/items".format( u=self.library_id, t=self.library_type, ta=tag ) return self._totals(query)
def num_tagitems(self, tag): """ Return the total number of items for the specified tag """ query = "/{t}/{u}/tags/{ta}/items".format( u=self.library_id, t=self.library_type, ta=tag ) return self._totals(query)
[ "Return", "the", "total", "number", "of", "items", "for", "the", "specified", "tag" ]
urschrei/pyzotero
python
https://github.com/urschrei/pyzotero/blob/b378966b30146a952f7953c23202fb5a1ddf81d9/pyzotero/zotero.py#L477-L483
[ "def", "num_tagitems", "(", "self", ",", "tag", ")", ":", "query", "=", "\"/{t}/{u}/tags/{ta}/items\"", ".", "format", "(", "u", "=", "self", ".", "library_id", ",", "t", "=", "self", ".", "library_type", ",", "ta", "=", "tag", ")", "return", "self", ".", "_totals", "(", "query", ")" ]
b378966b30146a952f7953c23202fb5a1ddf81d9
valid
Zotero._totals
General method for returning total counts
pyzotero/zotero.py
def _totals(self, query): """ General method for returning total counts """ self.add_parameters(limit=1) query = self._build_query(query) self._retrieve_data(query) self.url_params = None # extract the 'total items' figure return int(self.request.headers["Total-Results"])
def _totals(self, query): """ General method for returning total counts """ self.add_parameters(limit=1) query = self._build_query(query) self._retrieve_data(query) self.url_params = None # extract the 'total items' figure return int(self.request.headers["Total-Results"])
[ "General", "method", "for", "returning", "total", "counts" ]
urschrei/pyzotero
python
https://github.com/urschrei/pyzotero/blob/b378966b30146a952f7953c23202fb5a1ddf81d9/pyzotero/zotero.py#L485-L493
[ "def", "_totals", "(", "self", ",", "query", ")", ":", "self", ".", "add_parameters", "(", "limit", "=", "1", ")", "query", "=", "self", ".", "_build_query", "(", "query", ")", "self", ".", "_retrieve_data", "(", "query", ")", "self", ".", "url_params", "=", "None", "# extract the 'total items' figure", "return", "int", "(", "self", ".", "request", ".", "headers", "[", "\"Total-Results\"", "]", ")" ]
b378966b30146a952f7953c23202fb5a1ddf81d9
valid
Zotero.key_info
Retrieve info about the permissions associated with the key associated to the given Zotero instance
pyzotero/zotero.py
def key_info(self, **kwargs): """ Retrieve info about the permissions associated with the key associated to the given Zotero instance """ query_string = "/keys/{k}".format(k=self.api_key) return self._build_query(query_string)
def key_info(self, **kwargs): """ Retrieve info about the permissions associated with the key associated to the given Zotero instance """ query_string = "/keys/{k}".format(k=self.api_key) return self._build_query(query_string)
[ "Retrieve", "info", "about", "the", "permissions", "associated", "with", "the", "key", "associated", "to", "the", "given", "Zotero", "instance" ]
urschrei/pyzotero
python
https://github.com/urschrei/pyzotero/blob/b378966b30146a952f7953c23202fb5a1ddf81d9/pyzotero/zotero.py#L496-L502
[ "def", "key_info", "(", "self", ",", "*", "*", "kwargs", ")", ":", "query_string", "=", "\"/keys/{k}\"", ".", "format", "(", "k", "=", "self", ".", "api_key", ")", "return", "self", ".", "_build_query", "(", "query_string", ")" ]
b378966b30146a952f7953c23202fb5a1ddf81d9
valid
Zotero.fulltext_item
Get full-text content for an item
pyzotero/zotero.py
def fulltext_item(self, itemkey, **kwargs): """ Get full-text content for an item""" query_string = "/{t}/{u}/items/{itemkey}/fulltext".format( t=self.library_type, u=self.library_id, itemkey=itemkey ) return self._build_query(query_string)
def fulltext_item(self, itemkey, **kwargs): """ Get full-text content for an item""" query_string = "/{t}/{u}/items/{itemkey}/fulltext".format( t=self.library_type, u=self.library_id, itemkey=itemkey ) return self._build_query(query_string)
[ "Get", "full", "-", "text", "content", "for", "an", "item" ]
urschrei/pyzotero
python
https://github.com/urschrei/pyzotero/blob/b378966b30146a952f7953c23202fb5a1ddf81d9/pyzotero/zotero.py#L512-L517
[ "def", "fulltext_item", "(", "self", ",", "itemkey", ",", "*", "*", "kwargs", ")", ":", "query_string", "=", "\"/{t}/{u}/items/{itemkey}/fulltext\"", ".", "format", "(", "t", "=", "self", ".", "library_type", ",", "u", "=", "self", ".", "library_id", ",", "itemkey", "=", "itemkey", ")", "return", "self", ".", "_build_query", "(", "query_string", ")" ]
b378966b30146a952f7953c23202fb5a1ddf81d9
valid
Zotero.set_fulltext
Set full-text data for an item <itemkey> should correspond to an existing attachment item. payload should be a dict containing three keys: 'content': the full-text content and either For text documents, 'indexedChars' and 'totalChars' OR For PDFs, 'indexedPages' and 'totalPages'.
pyzotero/zotero.py
def set_fulltext(self, itemkey, payload): """" Set full-text data for an item <itemkey> should correspond to an existing attachment item. payload should be a dict containing three keys: 'content': the full-text content and either For text documents, 'indexedChars' and 'totalChars' OR For PDFs, 'indexedPages' and 'totalPages'. """ headers = self.default_headers() headers.update({"Content-Type": "application/json"}) req = requests.put( url=self.endpoint + "/{t}/{u}/items/{k}/fulltext".format( t=self.library_type, u=self.library_id, k=itemkey ), headers=headers, data=json.dumps(payload), ) try: req.raise_for_status() except requests.exceptions.HTTPError: error_handler(req) return True
def set_fulltext(self, itemkey, payload): """" Set full-text data for an item <itemkey> should correspond to an existing attachment item. payload should be a dict containing three keys: 'content': the full-text content and either For text documents, 'indexedChars' and 'totalChars' OR For PDFs, 'indexedPages' and 'totalPages'. """ headers = self.default_headers() headers.update({"Content-Type": "application/json"}) req = requests.put( url=self.endpoint + "/{t}/{u}/items/{k}/fulltext".format( t=self.library_type, u=self.library_id, k=itemkey ), headers=headers, data=json.dumps(payload), ) try: req.raise_for_status() except requests.exceptions.HTTPError: error_handler(req) return True
[ "Set", "full", "-", "text", "data", "for", "an", "item", "<itemkey", ">", "should", "correspond", "to", "an", "existing", "attachment", "item", ".", "payload", "should", "be", "a", "dict", "containing", "three", "keys", ":", "content", ":", "the", "full", "-", "text", "content", "and", "either", "For", "text", "documents", "indexedChars", "and", "totalChars", "OR", "For", "PDFs", "indexedPages", "and", "totalPages", "." ]
urschrei/pyzotero
python
https://github.com/urschrei/pyzotero/blob/b378966b30146a952f7953c23202fb5a1ddf81d9/pyzotero/zotero.py#L519-L543
[ "def", "set_fulltext", "(", "self", ",", "itemkey", ",", "payload", ")", ":", "headers", "=", "self", ".", "default_headers", "(", ")", "headers", ".", "update", "(", "{", "\"Content-Type\"", ":", "\"application/json\"", "}", ")", "req", "=", "requests", ".", "put", "(", "url", "=", "self", ".", "endpoint", "+", "\"/{t}/{u}/items/{k}/fulltext\"", ".", "format", "(", "t", "=", "self", ".", "library_type", ",", "u", "=", "self", ".", "library_id", ",", "k", "=", "itemkey", ")", ",", "headers", "=", "headers", ",", "data", "=", "json", ".", "dumps", "(", "payload", ")", ",", ")", "try", ":", "req", ".", "raise_for_status", "(", ")", "except", "requests", ".", "exceptions", ".", "HTTPError", ":", "error_handler", "(", "req", ")", "return", "True" ]
b378966b30146a952f7953c23202fb5a1ddf81d9
valid
Zotero.new_fulltext
Retrieve list of full-text content items and versions which are newer than <version>
pyzotero/zotero.py
def new_fulltext(self, version): """ Retrieve list of full-text content items and versions which are newer than <version> """ query_string = "/{t}/{u}/fulltext".format( t=self.library_type, u=self.library_id ) headers = {"since": str(version)} headers.update(self.default_headers()) req = requests.get(self.endpoint + query_string, headers=headers) try: req.raise_for_status() except requests.exceptions.HTTPError: error_handler(req) return req.json()
def new_fulltext(self, version): """ Retrieve list of full-text content items and versions which are newer than <version> """ query_string = "/{t}/{u}/fulltext".format( t=self.library_type, u=self.library_id ) headers = {"since": str(version)} headers.update(self.default_headers()) req = requests.get(self.endpoint + query_string, headers=headers) try: req.raise_for_status() except requests.exceptions.HTTPError: error_handler(req) return req.json()
[ "Retrieve", "list", "of", "full", "-", "text", "content", "items", "and", "versions", "which", "are", "newer", "than", "<version", ">" ]
urschrei/pyzotero
python
https://github.com/urschrei/pyzotero/blob/b378966b30146a952f7953c23202fb5a1ddf81d9/pyzotero/zotero.py#L545-L560
[ "def", "new_fulltext", "(", "self", ",", "version", ")", ":", "query_string", "=", "\"/{t}/{u}/fulltext\"", ".", "format", "(", "t", "=", "self", ".", "library_type", ",", "u", "=", "self", ".", "library_id", ")", "headers", "=", "{", "\"since\"", ":", "str", "(", "version", ")", "}", "headers", ".", "update", "(", "self", ".", "default_headers", "(", ")", ")", "req", "=", "requests", ".", "get", "(", "self", ".", "endpoint", "+", "query_string", ",", "headers", "=", "headers", ")", "try", ":", "req", ".", "raise_for_status", "(", ")", "except", "requests", ".", "exceptions", ".", "HTTPError", ":", "error_handler", "(", "req", ")", "return", "req", ".", "json", "(", ")" ]
b378966b30146a952f7953c23202fb5a1ddf81d9
valid
Zotero.last_modified_version
Get the last modified version
pyzotero/zotero.py
def last_modified_version(self, **kwargs): """ Get the last modified version """ self.items(**kwargs) return int(self.request.headers.get("last-modified-version", 0))
def last_modified_version(self, **kwargs): """ Get the last modified version """ self.items(**kwargs) return int(self.request.headers.get("last-modified-version", 0))
[ "Get", "the", "last", "modified", "version" ]
urschrei/pyzotero
python
https://github.com/urschrei/pyzotero/blob/b378966b30146a952f7953c23202fb5a1ddf81d9/pyzotero/zotero.py#L582-L586
[ "def", "last_modified_version", "(", "self", ",", "*", "*", "kwargs", ")", ":", "self", ".", "items", "(", "*", "*", "kwargs", ")", "return", "int", "(", "self", ".", "request", ".", "headers", ".", "get", "(", "\"last-modified-version\"", ",", "0", ")", ")" ]
b378966b30146a952f7953c23202fb5a1ddf81d9
valid
Zotero.file
Get the file from an specific item
pyzotero/zotero.py
def file(self, item, **kwargs): """ Get the file from an specific item """ query_string = "/{t}/{u}/items/{i}/file".format( u=self.library_id, t=self.library_type, i=item.upper() ) return self._build_query(query_string, no_params=True)
def file(self, item, **kwargs): """ Get the file from an specific item """ query_string = "/{t}/{u}/items/{i}/file".format( u=self.library_id, t=self.library_type, i=item.upper() ) return self._build_query(query_string, no_params=True)
[ "Get", "the", "file", "from", "an", "specific", "item" ]
urschrei/pyzotero
python
https://github.com/urschrei/pyzotero/blob/b378966b30146a952f7953c23202fb5a1ddf81d9/pyzotero/zotero.py#L630-L636
[ "def", "file", "(", "self", ",", "item", ",", "*", "*", "kwargs", ")", ":", "query_string", "=", "\"/{t}/{u}/items/{i}/file\"", ".", "format", "(", "u", "=", "self", ".", "library_id", ",", "t", "=", "self", ".", "library_type", ",", "i", "=", "item", ".", "upper", "(", ")", ")", "return", "self", ".", "_build_query", "(", "query_string", ",", "no_params", "=", "True", ")" ]
b378966b30146a952f7953c23202fb5a1ddf81d9
valid
Zotero.dump
Dump a file attachment to disk, with optional filename and path
pyzotero/zotero.py
def dump(self, itemkey, filename=None, path=None): """ Dump a file attachment to disk, with optional filename and path """ if not filename: filename = self.item(itemkey)["data"]["filename"] if path: pth = os.path.join(path, filename) else: pth = filename file = self.file(itemkey) if self.snapshot: self.snapshot = False pth = pth + ".zip" with open(pth, "wb") as f: f.write(file)
def dump(self, itemkey, filename=None, path=None): """ Dump a file attachment to disk, with optional filename and path """ if not filename: filename = self.item(itemkey)["data"]["filename"] if path: pth = os.path.join(path, filename) else: pth = filename file = self.file(itemkey) if self.snapshot: self.snapshot = False pth = pth + ".zip" with open(pth, "wb") as f: f.write(file)
[ "Dump", "a", "file", "attachment", "to", "disk", "with", "optional", "filename", "and", "path" ]
urschrei/pyzotero
python
https://github.com/urschrei/pyzotero/blob/b378966b30146a952f7953c23202fb5a1ddf81d9/pyzotero/zotero.py#L638-L653
[ "def", "dump", "(", "self", ",", "itemkey", ",", "filename", "=", "None", ",", "path", "=", "None", ")", ":", "if", "not", "filename", ":", "filename", "=", "self", ".", "item", "(", "itemkey", ")", "[", "\"data\"", "]", "[", "\"filename\"", "]", "if", "path", ":", "pth", "=", "os", ".", "path", ".", "join", "(", "path", ",", "filename", ")", "else", ":", "pth", "=", "filename", "file", "=", "self", ".", "file", "(", "itemkey", ")", "if", "self", ".", "snapshot", ":", "self", ".", "snapshot", "=", "False", "pth", "=", "pth", "+", "\".zip\"", "with", "open", "(", "pth", ",", "\"wb\"", ")", "as", "f", ":", "f", ".", "write", "(", "file", ")" ]
b378966b30146a952f7953c23202fb5a1ddf81d9
valid
Zotero.all_collections
Retrieve all collections and subcollections. Works for top-level collections or for a specific collection. Works at all collection depths.
pyzotero/zotero.py
def all_collections(self, collid=None): """ Retrieve all collections and subcollections. Works for top-level collections or for a specific collection. Works at all collection depths. """ all_collections = [] def subcoll(clct): """ recursively add collections to a flat master list """ all_collections.append(clct) if clct["meta"].get("numCollections", 0) > 0: # add collection to master list & recur with all child # collections [ subcoll(c) for c in self.everything(self.collections_sub(clct["data"]["key"])) ] # select all top-level collections or a specific collection and # children if collid: toplevel = [self.collection(collid)] else: toplevel = self.everything(self.collections_top()) [subcoll(collection) for collection in toplevel] return all_collections
def all_collections(self, collid=None): """ Retrieve all collections and subcollections. Works for top-level collections or for a specific collection. Works at all collection depths. """ all_collections = [] def subcoll(clct): """ recursively add collections to a flat master list """ all_collections.append(clct) if clct["meta"].get("numCollections", 0) > 0: # add collection to master list & recur with all child # collections [ subcoll(c) for c in self.everything(self.collections_sub(clct["data"]["key"])) ] # select all top-level collections or a specific collection and # children if collid: toplevel = [self.collection(collid)] else: toplevel = self.everything(self.collections_top()) [subcoll(collection) for collection in toplevel] return all_collections
[ "Retrieve", "all", "collections", "and", "subcollections", ".", "Works", "for", "top", "-", "level", "collections", "or", "for", "a", "specific", "collection", ".", "Works", "at", "all", "collection", "depths", "." ]
urschrei/pyzotero
python
https://github.com/urschrei/pyzotero/blob/b378966b30146a952f7953c23202fb5a1ddf81d9/pyzotero/zotero.py#L707-L732
[ "def", "all_collections", "(", "self", ",", "collid", "=", "None", ")", ":", "all_collections", "=", "[", "]", "def", "subcoll", "(", "clct", ")", ":", "\"\"\" recursively add collections to a flat master list \"\"\"", "all_collections", ".", "append", "(", "clct", ")", "if", "clct", "[", "\"meta\"", "]", ".", "get", "(", "\"numCollections\"", ",", "0", ")", ">", "0", ":", "# add collection to master list & recur with all child", "# collections", "[", "subcoll", "(", "c", ")", "for", "c", "in", "self", ".", "everything", "(", "self", ".", "collections_sub", "(", "clct", "[", "\"data\"", "]", "[", "\"key\"", "]", ")", ")", "]", "# select all top-level collections or a specific collection and", "# children", "if", "collid", ":", "toplevel", "=", "[", "self", ".", "collection", "(", "collid", ")", "]", "else", ":", "toplevel", "=", "self", ".", "everything", "(", "self", ".", "collections_top", "(", ")", ")", "[", "subcoll", "(", "collection", ")", "for", "collection", "in", "toplevel", "]", "return", "all_collections" ]
b378966b30146a952f7953c23202fb5a1ddf81d9
valid
Zotero.collections_sub
Get subcollections for a specific collection
pyzotero/zotero.py
def collections_sub(self, collection, **kwargs): """ Get subcollections for a specific collection """ query_string = "/{t}/{u}/collections/{c}/collections".format( u=self.library_id, t=self.library_type, c=collection.upper() ) return self._build_query(query_string)
def collections_sub(self, collection, **kwargs): """ Get subcollections for a specific collection """ query_string = "/{t}/{u}/collections/{c}/collections".format( u=self.library_id, t=self.library_type, c=collection.upper() ) return self._build_query(query_string)
[ "Get", "subcollections", "for", "a", "specific", "collection" ]
urschrei/pyzotero
python
https://github.com/urschrei/pyzotero/blob/b378966b30146a952f7953c23202fb5a1ddf81d9/pyzotero/zotero.py#L742-L748
[ "def", "collections_sub", "(", "self", ",", "collection", ",", "*", "*", "kwargs", ")", ":", "query_string", "=", "\"/{t}/{u}/collections/{c}/collections\"", ".", "format", "(", "u", "=", "self", ".", "library_id", ",", "t", "=", "self", ".", "library_type", ",", "c", "=", "collection", ".", "upper", "(", ")", ")", "return", "self", ".", "_build_query", "(", "query_string", ")" ]
b378966b30146a952f7953c23202fb5a1ddf81d9
valid
Zotero.tags
Get tags
pyzotero/zotero.py
def tags(self, **kwargs): """ Get tags """ query_string = "/{t}/{u}/tags" self.tag_data = True return self._build_query(query_string)
def tags(self, **kwargs): """ Get tags """ query_string = "/{t}/{u}/tags" self.tag_data = True return self._build_query(query_string)
[ "Get", "tags" ]
urschrei/pyzotero
python
https://github.com/urschrei/pyzotero/blob/b378966b30146a952f7953c23202fb5a1ddf81d9/pyzotero/zotero.py#L758-L763
[ "def", "tags", "(", "self", ",", "*", "*", "kwargs", ")", ":", "query_string", "=", "\"/{t}/{u}/tags\"", "self", ".", "tag_data", "=", "True", "return", "self", ".", "_build_query", "(", "query_string", ")" ]
b378966b30146a952f7953c23202fb5a1ddf81d9
valid
Zotero.iterfollow
Generator for self.follow()
pyzotero/zotero.py
def iterfollow(self): """ Generator for self.follow() """ # use same criterion as self.follow() if self.links is None: return if self.links.get("next"): yield self.follow() else: raise StopIteration
def iterfollow(self): """ Generator for self.follow() """ # use same criterion as self.follow() if self.links is None: return if self.links.get("next"): yield self.follow() else: raise StopIteration
[ "Generator", "for", "self", ".", "follow", "()" ]
urschrei/pyzotero
python
https://github.com/urschrei/pyzotero/blob/b378966b30146a952f7953c23202fb5a1ddf81d9/pyzotero/zotero.py#L789-L798
[ "def", "iterfollow", "(", "self", ")", ":", "# use same criterion as self.follow()", "if", "self", ".", "links", "is", "None", ":", "return", "if", "self", ".", "links", ".", "get", "(", "\"next\"", ")", ":", "yield", "self", ".", "follow", "(", ")", "else", ":", "raise", "StopIteration" ]
b378966b30146a952f7953c23202fb5a1ddf81d9
valid
Zotero.everything
Retrieve all items in the library for a particular query This method will override the 'limit' parameter if it's been set
pyzotero/zotero.py
def everything(self, query): """ Retrieve all items in the library for a particular query This method will override the 'limit' parameter if it's been set """ try: items = [] items.extend(query) while self.links.get("next"): items.extend(self.follow()) except TypeError: # we have a bibliography object ughh items = copy.deepcopy(query) while self.links.get("next"): items.entries.extend(self.follow().entries) return items
def everything(self, query): """ Retrieve all items in the library for a particular query This method will override the 'limit' parameter if it's been set """ try: items = [] items.extend(query) while self.links.get("next"): items.extend(self.follow()) except TypeError: # we have a bibliography object ughh items = copy.deepcopy(query) while self.links.get("next"): items.entries.extend(self.follow().entries) return items
[ "Retrieve", "all", "items", "in", "the", "library", "for", "a", "particular", "query", "This", "method", "will", "override", "the", "limit", "parameter", "if", "it", "s", "been", "set" ]
urschrei/pyzotero
python
https://github.com/urschrei/pyzotero/blob/b378966b30146a952f7953c23202fb5a1ddf81d9/pyzotero/zotero.py#L807-L822
[ "def", "everything", "(", "self", ",", "query", ")", ":", "try", ":", "items", "=", "[", "]", "items", ".", "extend", "(", "query", ")", "while", "self", ".", "links", ".", "get", "(", "\"next\"", ")", ":", "items", ".", "extend", "(", "self", ".", "follow", "(", ")", ")", "except", "TypeError", ":", "# we have a bibliography object ughh", "items", "=", "copy", ".", "deepcopy", "(", "query", ")", "while", "self", ".", "links", ".", "get", "(", "\"next\"", ")", ":", "items", ".", "entries", ".", "extend", "(", "self", ".", "follow", "(", ")", ".", "entries", ")", "return", "items" ]
b378966b30146a952f7953c23202fb5a1ddf81d9
valid
Zotero.get_subset
Retrieve a subset of items Accepts a single argument: a list of item IDs
pyzotero/zotero.py
def get_subset(self, subset): """ Retrieve a subset of items Accepts a single argument: a list of item IDs """ if len(subset) > 50: raise ze.TooManyItems("You may only retrieve 50 items per call") # remember any url parameters that have been set params = self.url_params retr = [] for itm in subset: retr.extend(self.item(itm)) self.url_params = params # clean up URL params when we're finished self.url_params = None return retr
def get_subset(self, subset): """ Retrieve a subset of items Accepts a single argument: a list of item IDs """ if len(subset) > 50: raise ze.TooManyItems("You may only retrieve 50 items per call") # remember any url parameters that have been set params = self.url_params retr = [] for itm in subset: retr.extend(self.item(itm)) self.url_params = params # clean up URL params when we're finished self.url_params = None return retr
[ "Retrieve", "a", "subset", "of", "items", "Accepts", "a", "single", "argument", ":", "a", "list", "of", "item", "IDs" ]
urschrei/pyzotero
python
https://github.com/urschrei/pyzotero/blob/b378966b30146a952f7953c23202fb5a1ddf81d9/pyzotero/zotero.py#L824-L839
[ "def", "get_subset", "(", "self", ",", "subset", ")", ":", "if", "len", "(", "subset", ")", ">", "50", ":", "raise", "ze", ".", "TooManyItems", "(", "\"You may only retrieve 50 items per call\"", ")", "# remember any url parameters that have been set", "params", "=", "self", ".", "url_params", "retr", "=", "[", "]", "for", "itm", "in", "subset", ":", "retr", ".", "extend", "(", "self", ".", "item", "(", "itm", ")", ")", "self", ".", "url_params", "=", "params", "# clean up URL params when we're finished", "self", ".", "url_params", "=", "None", "return", "retr" ]
b378966b30146a952f7953c23202fb5a1ddf81d9
valid
Zotero._json_processor
Format and return data from API calls which return Items
pyzotero/zotero.py
def _json_processor(self, retrieved): """ Format and return data from API calls which return Items """ json_kwargs = {} if self.preserve_json_order: json_kwargs["object_pairs_hook"] = OrderedDict # send entries to _tags_data if there's no JSON try: items = [ json.loads(e["content"][0]["value"], **json_kwargs) for e in retrieved.entries ] except KeyError: return self._tags_data(retrieved) return items
def _json_processor(self, retrieved): """ Format and return data from API calls which return Items """ json_kwargs = {} if self.preserve_json_order: json_kwargs["object_pairs_hook"] = OrderedDict # send entries to _tags_data if there's no JSON try: items = [ json.loads(e["content"][0]["value"], **json_kwargs) for e in retrieved.entries ] except KeyError: return self._tags_data(retrieved) return items
[ "Format", "and", "return", "data", "from", "API", "calls", "which", "return", "Items" ]
urschrei/pyzotero
python
https://github.com/urschrei/pyzotero/blob/b378966b30146a952f7953c23202fb5a1ddf81d9/pyzotero/zotero.py#L842-L856
[ "def", "_json_processor", "(", "self", ",", "retrieved", ")", ":", "json_kwargs", "=", "{", "}", "if", "self", ".", "preserve_json_order", ":", "json_kwargs", "[", "\"object_pairs_hook\"", "]", "=", "OrderedDict", "# send entries to _tags_data if there's no JSON", "try", ":", "items", "=", "[", "json", ".", "loads", "(", "e", "[", "\"content\"", "]", "[", "0", "]", "[", "\"value\"", "]", ",", "*", "*", "json_kwargs", ")", "for", "e", "in", "retrieved", ".", "entries", "]", "except", "KeyError", ":", "return", "self", ".", "_tags_data", "(", "retrieved", ")", "return", "items" ]
b378966b30146a952f7953c23202fb5a1ddf81d9
valid
Zotero._csljson_processor
Return a list of dicts which are dumped CSL JSON
pyzotero/zotero.py
def _csljson_processor(self, retrieved): """ Return a list of dicts which are dumped CSL JSON """ items = [] json_kwargs = {} if self.preserve_json_order: json_kwargs["object_pairs_hook"] = OrderedDict for csl in retrieved.entries: items.append(json.loads(csl["content"][0]["value"], **json_kwargs)) self.url_params = None return items
def _csljson_processor(self, retrieved): """ Return a list of dicts which are dumped CSL JSON """ items = [] json_kwargs = {} if self.preserve_json_order: json_kwargs["object_pairs_hook"] = OrderedDict for csl in retrieved.entries: items.append(json.loads(csl["content"][0]["value"], **json_kwargs)) self.url_params = None return items
[ "Return", "a", "list", "of", "dicts", "which", "are", "dumped", "CSL", "JSON" ]
urschrei/pyzotero
python
https://github.com/urschrei/pyzotero/blob/b378966b30146a952f7953c23202fb5a1ddf81d9/pyzotero/zotero.py#L858-L868
[ "def", "_csljson_processor", "(", "self", ",", "retrieved", ")", ":", "items", "=", "[", "]", "json_kwargs", "=", "{", "}", "if", "self", ".", "preserve_json_order", ":", "json_kwargs", "[", "\"object_pairs_hook\"", "]", "=", "OrderedDict", "for", "csl", "in", "retrieved", ".", "entries", ":", "items", ".", "append", "(", "json", ".", "loads", "(", "csl", "[", "\"content\"", "]", "[", "0", "]", "[", "\"value\"", "]", ",", "*", "*", "json_kwargs", ")", ")", "self", ".", "url_params", "=", "None", "return", "items" ]
b378966b30146a952f7953c23202fb5a1ddf81d9
valid
Zotero._bib_processor
Return a list of strings formatted as HTML bibliography entries
pyzotero/zotero.py
def _bib_processor(self, retrieved): """ Return a list of strings formatted as HTML bibliography entries """ items = [] for bib in retrieved.entries: items.append(bib["content"][0]["value"]) self.url_params = None return items
def _bib_processor(self, retrieved): """ Return a list of strings formatted as HTML bibliography entries """ items = [] for bib in retrieved.entries: items.append(bib["content"][0]["value"]) self.url_params = None return items
[ "Return", "a", "list", "of", "strings", "formatted", "as", "HTML", "bibliography", "entries" ]
urschrei/pyzotero
python
https://github.com/urschrei/pyzotero/blob/b378966b30146a952f7953c23202fb5a1ddf81d9/pyzotero/zotero.py#L870-L877
[ "def", "_bib_processor", "(", "self", ",", "retrieved", ")", ":", "items", "=", "[", "]", "for", "bib", "in", "retrieved", ".", "entries", ":", "items", ".", "append", "(", "bib", "[", "\"content\"", "]", "[", "0", "]", "[", "\"value\"", "]", ")", "self", ".", "url_params", "=", "None", "return", "items" ]
b378966b30146a952f7953c23202fb5a1ddf81d9
valid
Zotero._citation_processor
Return a list of strings formatted as HTML citation entries
pyzotero/zotero.py
def _citation_processor(self, retrieved): """ Return a list of strings formatted as HTML citation entries """ items = [] for cit in retrieved.entries: items.append(cit["content"][0]["value"]) self.url_params = None return items
def _citation_processor(self, retrieved): """ Return a list of strings formatted as HTML citation entries """ items = [] for cit in retrieved.entries: items.append(cit["content"][0]["value"]) self.url_params = None return items
[ "Return", "a", "list", "of", "strings", "formatted", "as", "HTML", "citation", "entries" ]
urschrei/pyzotero
python
https://github.com/urschrei/pyzotero/blob/b378966b30146a952f7953c23202fb5a1ddf81d9/pyzotero/zotero.py#L879-L886
[ "def", "_citation_processor", "(", "self", ",", "retrieved", ")", ":", "items", "=", "[", "]", "for", "cit", "in", "retrieved", ".", "entries", ":", "items", ".", "append", "(", "cit", "[", "\"content\"", "]", "[", "0", "]", "[", "\"value\"", "]", ")", "self", ".", "url_params", "=", "None", "return", "items" ]
b378966b30146a952f7953c23202fb5a1ddf81d9
valid
Zotero.item_template
Get a template for a new item
pyzotero/zotero.py
def item_template(self, itemtype): """ Get a template for a new item """ # if we have a template and it hasn't been updated since we stored it template_name = "item_template_" + itemtype query_string = "/items/new?itemType={i}".format(i=itemtype) if self.templates.get(template_name) and not self._updated( query_string, self.templates[template_name], template_name ): return copy.deepcopy(self.templates[template_name]["tmplt"]) # otherwise perform a normal request and cache the response retrieved = self._retrieve_data(query_string) return self._cache(retrieved, template_name)
def item_template(self, itemtype): """ Get a template for a new item """ # if we have a template and it hasn't been updated since we stored it template_name = "item_template_" + itemtype query_string = "/items/new?itemType={i}".format(i=itemtype) if self.templates.get(template_name) and not self._updated( query_string, self.templates[template_name], template_name ): return copy.deepcopy(self.templates[template_name]["tmplt"]) # otherwise perform a normal request and cache the response retrieved = self._retrieve_data(query_string) return self._cache(retrieved, template_name)
[ "Get", "a", "template", "for", "a", "new", "item" ]
urschrei/pyzotero
python
https://github.com/urschrei/pyzotero/blob/b378966b30146a952f7953c23202fb5a1ddf81d9/pyzotero/zotero.py#L895-L907
[ "def", "item_template", "(", "self", ",", "itemtype", ")", ":", "# if we have a template and it hasn't been updated since we stored it", "template_name", "=", "\"item_template_\"", "+", "itemtype", "query_string", "=", "\"/items/new?itemType={i}\"", ".", "format", "(", "i", "=", "itemtype", ")", "if", "self", ".", "templates", ".", "get", "(", "template_name", ")", "and", "not", "self", ".", "_updated", "(", "query_string", ",", "self", ".", "templates", "[", "template_name", "]", ",", "template_name", ")", ":", "return", "copy", ".", "deepcopy", "(", "self", ".", "templates", "[", "template_name", "]", "[", "\"tmplt\"", "]", ")", "# otherwise perform a normal request and cache the response", "retrieved", "=", "self", ".", "_retrieve_data", "(", "query_string", ")", "return", "self", ".", "_cache", "(", "retrieved", ",", "template_name", ")" ]
b378966b30146a952f7953c23202fb5a1ddf81d9
valid
Zotero._attachment
Create attachments accepts a list of one or more attachment template dicts and an optional parent Item ID. If this is specified, attachments are created under this ID
pyzotero/zotero.py
def _attachment(self, payload, parentid=None): """ Create attachments accepts a list of one or more attachment template dicts and an optional parent Item ID. If this is specified, attachments are created under this ID """ attachment = Zupload(self, payload, parentid) res = attachment.upload() return res
def _attachment(self, payload, parentid=None): """ Create attachments accepts a list of one or more attachment template dicts and an optional parent Item ID. If this is specified, attachments are created under this ID """ attachment = Zupload(self, payload, parentid) res = attachment.upload() return res
[ "Create", "attachments", "accepts", "a", "list", "of", "one", "or", "more", "attachment", "template", "dicts", "and", "an", "optional", "parent", "Item", "ID", ".", "If", "this", "is", "specified", "attachments", "are", "created", "under", "this", "ID" ]
urschrei/pyzotero
python
https://github.com/urschrei/pyzotero/blob/b378966b30146a952f7953c23202fb5a1ddf81d9/pyzotero/zotero.py#L919-L928
[ "def", "_attachment", "(", "self", ",", "payload", ",", "parentid", "=", "None", ")", ":", "attachment", "=", "Zupload", "(", "self", ",", "payload", ",", "parentid", ")", "res", "=", "attachment", ".", "upload", "(", ")", "return", "res" ]
b378966b30146a952f7953c23202fb5a1ddf81d9
valid
Zotero.show_condition_operators
Show available operators for a given saved search condition
pyzotero/zotero.py
def show_condition_operators(self, condition): """ Show available operators for a given saved search condition """ # dict keys of allowed operators for the current condition permitted_operators = self.savedsearch.conditions_operators.get(condition) # transform these into values permitted_operators_list = set( [self.savedsearch.operators.get(op) for op in permitted_operators] ) return permitted_operators_list
def show_condition_operators(self, condition): """ Show available operators for a given saved search condition """ # dict keys of allowed operators for the current condition permitted_operators = self.savedsearch.conditions_operators.get(condition) # transform these into values permitted_operators_list = set( [self.savedsearch.operators.get(op) for op in permitted_operators] ) return permitted_operators_list
[ "Show", "available", "operators", "for", "a", "given", "saved", "search", "condition" ]
urschrei/pyzotero
python
https://github.com/urschrei/pyzotero/blob/b378966b30146a952f7953c23202fb5a1ddf81d9/pyzotero/zotero.py#L941-L949
[ "def", "show_condition_operators", "(", "self", ",", "condition", ")", ":", "# dict keys of allowed operators for the current condition", "permitted_operators", "=", "self", ".", "savedsearch", ".", "conditions_operators", ".", "get", "(", "condition", ")", "# transform these into values", "permitted_operators_list", "=", "set", "(", "[", "self", ".", "savedsearch", ".", "operators", ".", "get", "(", "op", ")", "for", "op", "in", "permitted_operators", "]", ")", "return", "permitted_operators_list" ]
b378966b30146a952f7953c23202fb5a1ddf81d9
valid
Zotero.saved_search
Create a saved search. conditions is a list of dicts containing search conditions, and must contain the following str keys: condition, operator, value
pyzotero/zotero.py
def saved_search(self, name, conditions): """ Create a saved search. conditions is a list of dicts containing search conditions, and must contain the following str keys: condition, operator, value """ self.savedsearch._validate(conditions) payload = [{"name": name, "conditions": conditions}] headers = {"Zotero-Write-Token": token()} headers.update(self.default_headers()) req = requests.post( url=self.endpoint + "/{t}/{u}/searches".format(t=self.library_type, u=self.library_id), headers=headers, data=json.dumps(payload), ) self.request = req try: req.raise_for_status() except requests.exceptions.HTTPError: error_handler(req) return req.json()
def saved_search(self, name, conditions): """ Create a saved search. conditions is a list of dicts containing search conditions, and must contain the following str keys: condition, operator, value """ self.savedsearch._validate(conditions) payload = [{"name": name, "conditions": conditions}] headers = {"Zotero-Write-Token": token()} headers.update(self.default_headers()) req = requests.post( url=self.endpoint + "/{t}/{u}/searches".format(t=self.library_type, u=self.library_id), headers=headers, data=json.dumps(payload), ) self.request = req try: req.raise_for_status() except requests.exceptions.HTTPError: error_handler(req) return req.json()
[ "Create", "a", "saved", "search", ".", "conditions", "is", "a", "list", "of", "dicts", "containing", "search", "conditions", "and", "must", "contain", "the", "following", "str", "keys", ":", "condition", "operator", "value" ]
urschrei/pyzotero
python
https://github.com/urschrei/pyzotero/blob/b378966b30146a952f7953c23202fb5a1ddf81d9/pyzotero/zotero.py#L952-L972
[ "def", "saved_search", "(", "self", ",", "name", ",", "conditions", ")", ":", "self", ".", "savedsearch", ".", "_validate", "(", "conditions", ")", "payload", "=", "[", "{", "\"name\"", ":", "name", ",", "\"conditions\"", ":", "conditions", "}", "]", "headers", "=", "{", "\"Zotero-Write-Token\"", ":", "token", "(", ")", "}", "headers", ".", "update", "(", "self", ".", "default_headers", "(", ")", ")", "req", "=", "requests", ".", "post", "(", "url", "=", "self", ".", "endpoint", "+", "\"/{t}/{u}/searches\"", ".", "format", "(", "t", "=", "self", ".", "library_type", ",", "u", "=", "self", ".", "library_id", ")", ",", "headers", "=", "headers", ",", "data", "=", "json", ".", "dumps", "(", "payload", ")", ",", ")", "self", ".", "request", "=", "req", "try", ":", "req", ".", "raise_for_status", "(", ")", "except", "requests", ".", "exceptions", ".", "HTTPError", ":", "error_handler", "(", "req", ")", "return", "req", ".", "json", "(", ")" ]
b378966b30146a952f7953c23202fb5a1ddf81d9
valid
Zotero.delete_saved_search
Delete one or more saved searches by passing a list of one or more unique search keys
pyzotero/zotero.py
def delete_saved_search(self, keys): """ Delete one or more saved searches by passing a list of one or more unique search keys """ headers = {"Zotero-Write-Token": token()} headers.update(self.default_headers()) req = requests.delete( url=self.endpoint + "/{t}/{u}/searches".format(t=self.library_type, u=self.library_id), headers=headers, params={"searchKey": ",".join(keys)}, ) self.request = req try: req.raise_for_status() except requests.exceptions.HTTPError: error_handler(req) return req.status_code
def delete_saved_search(self, keys): """ Delete one or more saved searches by passing a list of one or more unique search keys """ headers = {"Zotero-Write-Token": token()} headers.update(self.default_headers()) req = requests.delete( url=self.endpoint + "/{t}/{u}/searches".format(t=self.library_type, u=self.library_id), headers=headers, params={"searchKey": ",".join(keys)}, ) self.request = req try: req.raise_for_status() except requests.exceptions.HTTPError: error_handler(req) return req.status_code
[ "Delete", "one", "or", "more", "saved", "searches", "by", "passing", "a", "list", "of", "one", "or", "more", "unique", "search", "keys" ]
urschrei/pyzotero
python
https://github.com/urschrei/pyzotero/blob/b378966b30146a952f7953c23202fb5a1ddf81d9/pyzotero/zotero.py#L975-L992
[ "def", "delete_saved_search", "(", "self", ",", "keys", ")", ":", "headers", "=", "{", "\"Zotero-Write-Token\"", ":", "token", "(", ")", "}", "headers", ".", "update", "(", "self", ".", "default_headers", "(", ")", ")", "req", "=", "requests", ".", "delete", "(", "url", "=", "self", ".", "endpoint", "+", "\"/{t}/{u}/searches\"", ".", "format", "(", "t", "=", "self", ".", "library_type", ",", "u", "=", "self", ".", "library_id", ")", ",", "headers", "=", "headers", ",", "params", "=", "{", "\"searchKey\"", ":", "\",\"", ".", "join", "(", "keys", ")", "}", ",", ")", "self", ".", "request", "=", "req", "try", ":", "req", ".", "raise_for_status", "(", ")", "except", "requests", ".", "exceptions", ".", "HTTPError", ":", "error_handler", "(", "req", ")", "return", "req", ".", "status_code" ]
b378966b30146a952f7953c23202fb5a1ddf81d9
valid
Zotero.upload_attachments
Upload files to the already created (but never uploaded) attachments
pyzotero/zotero.py
def upload_attachments(self, attachments, parentid=None, basedir=None): """Upload files to the already created (but never uploaded) attachments""" return Zupload(self, attachments, parentid, basedir=basedir).upload()
def upload_attachments(self, attachments, parentid=None, basedir=None): """Upload files to the already created (but never uploaded) attachments""" return Zupload(self, attachments, parentid, basedir=basedir).upload()
[ "Upload", "files", "to", "the", "already", "created", "(", "but", "never", "uploaded", ")", "attachments" ]
urschrei/pyzotero
python
https://github.com/urschrei/pyzotero/blob/b378966b30146a952f7953c23202fb5a1ddf81d9/pyzotero/zotero.py#L994-L996
[ "def", "upload_attachments", "(", "self", ",", "attachments", ",", "parentid", "=", "None", ",", "basedir", "=", "None", ")", ":", "return", "Zupload", "(", "self", ",", "attachments", ",", "parentid", ",", "basedir", "=", "basedir", ")", ".", "upload", "(", ")" ]
b378966b30146a952f7953c23202fb5a1ddf81d9
valid
Zotero.add_tags
Add one or more tags to a retrieved item, then update it on the server Accepts a dict, and one or more tags to add to it Returns the updated item from the server
pyzotero/zotero.py
def add_tags(self, item, *tags): """ Add one or more tags to a retrieved item, then update it on the server Accepts a dict, and one or more tags to add to it Returns the updated item from the server """ # Make sure there's a tags field, or add one try: assert item["data"]["tags"] except AssertionError: item["data"]["tags"] = list() for tag in tags: item["data"]["tags"].append({"tag": "%s" % tag}) # make sure everything's OK assert self.check_items([item]) return self.update_item(item)
def add_tags(self, item, *tags): """ Add one or more tags to a retrieved item, then update it on the server Accepts a dict, and one or more tags to add to it Returns the updated item from the server """ # Make sure there's a tags field, or add one try: assert item["data"]["tags"] except AssertionError: item["data"]["tags"] = list() for tag in tags: item["data"]["tags"].append({"tag": "%s" % tag}) # make sure everything's OK assert self.check_items([item]) return self.update_item(item)
[ "Add", "one", "or", "more", "tags", "to", "a", "retrieved", "item", "then", "update", "it", "on", "the", "server", "Accepts", "a", "dict", "and", "one", "or", "more", "tags", "to", "add", "to", "it", "Returns", "the", "updated", "item", "from", "the", "server" ]
urschrei/pyzotero
python
https://github.com/urschrei/pyzotero/blob/b378966b30146a952f7953c23202fb5a1ddf81d9/pyzotero/zotero.py#L998-L1014
[ "def", "add_tags", "(", "self", ",", "item", ",", "*", "tags", ")", ":", "# Make sure there's a tags field, or add one", "try", ":", "assert", "item", "[", "\"data\"", "]", "[", "\"tags\"", "]", "except", "AssertionError", ":", "item", "[", "\"data\"", "]", "[", "\"tags\"", "]", "=", "list", "(", ")", "for", "tag", "in", "tags", ":", "item", "[", "\"data\"", "]", "[", "\"tags\"", "]", ".", "append", "(", "{", "\"tag\"", ":", "\"%s\"", "%", "tag", "}", ")", "# make sure everything's OK", "assert", "self", ".", "check_items", "(", "[", "item", "]", ")", "return", "self", ".", "update_item", "(", "item", ")" ]
b378966b30146a952f7953c23202fb5a1ddf81d9
valid
Zotero.check_items
Check that items to be created contain no invalid dict keys Accepts a single argument: a list of one or more dicts The retrieved fields are cached and re-used until a 304 call fails
pyzotero/zotero.py
def check_items(self, items): """ Check that items to be created contain no invalid dict keys Accepts a single argument: a list of one or more dicts The retrieved fields are cached and re-used until a 304 call fails """ # check for a valid cached version if self.templates.get("item_fields") and not self._updated( "/itemFields", self.templates["item_fields"], "item_fields" ): template = set(t["field"] for t in self.templates["item_fields"]["tmplt"]) else: template = set(t["field"] for t in self.item_fields()) # add fields we know to be OK template = template | set( [ "path", "tags", "notes", "itemType", "creators", "mimeType", "linkMode", "note", "charset", "dateAdded", "version", "collections", "dateModified", "relations", # attachment items "parentItem", "mtime", "contentType", "md5", "filename", ] ) template = template | set(self.temp_keys) for pos, item in enumerate(items): if set(item) == set(["links", "library", "version", "meta", "key", "data"]): # we have an item that was retrieved from the API item = item["data"] to_check = set(i for i in list(item.keys())) difference = to_check.difference(template) if difference: raise ze.InvalidItemFields( "Invalid keys present in item %s: %s" % (pos + 1, " ".join(i for i in difference)) ) return items
def check_items(self, items): """ Check that items to be created contain no invalid dict keys Accepts a single argument: a list of one or more dicts The retrieved fields are cached and re-used until a 304 call fails """ # check for a valid cached version if self.templates.get("item_fields") and not self._updated( "/itemFields", self.templates["item_fields"], "item_fields" ): template = set(t["field"] for t in self.templates["item_fields"]["tmplt"]) else: template = set(t["field"] for t in self.item_fields()) # add fields we know to be OK template = template | set( [ "path", "tags", "notes", "itemType", "creators", "mimeType", "linkMode", "note", "charset", "dateAdded", "version", "collections", "dateModified", "relations", # attachment items "parentItem", "mtime", "contentType", "md5", "filename", ] ) template = template | set(self.temp_keys) for pos, item in enumerate(items): if set(item) == set(["links", "library", "version", "meta", "key", "data"]): # we have an item that was retrieved from the API item = item["data"] to_check = set(i for i in list(item.keys())) difference = to_check.difference(template) if difference: raise ze.InvalidItemFields( "Invalid keys present in item %s: %s" % (pos + 1, " ".join(i for i in difference)) ) return items
[ "Check", "that", "items", "to", "be", "created", "contain", "no", "invalid", "dict", "keys", "Accepts", "a", "single", "argument", ":", "a", "list", "of", "one", "or", "more", "dicts", "The", "retrieved", "fields", "are", "cached", "and", "re", "-", "used", "until", "a", "304", "call", "fails" ]
urschrei/pyzotero
python
https://github.com/urschrei/pyzotero/blob/b378966b30146a952f7953c23202fb5a1ddf81d9/pyzotero/zotero.py#L1016-L1066
[ "def", "check_items", "(", "self", ",", "items", ")", ":", "# check for a valid cached version", "if", "self", ".", "templates", ".", "get", "(", "\"item_fields\"", ")", "and", "not", "self", ".", "_updated", "(", "\"/itemFields\"", ",", "self", ".", "templates", "[", "\"item_fields\"", "]", ",", "\"item_fields\"", ")", ":", "template", "=", "set", "(", "t", "[", "\"field\"", "]", "for", "t", "in", "self", ".", "templates", "[", "\"item_fields\"", "]", "[", "\"tmplt\"", "]", ")", "else", ":", "template", "=", "set", "(", "t", "[", "\"field\"", "]", "for", "t", "in", "self", ".", "item_fields", "(", ")", ")", "# add fields we know to be OK", "template", "=", "template", "|", "set", "(", "[", "\"path\"", ",", "\"tags\"", ",", "\"notes\"", ",", "\"itemType\"", ",", "\"creators\"", ",", "\"mimeType\"", ",", "\"linkMode\"", ",", "\"note\"", ",", "\"charset\"", ",", "\"dateAdded\"", ",", "\"version\"", ",", "\"collections\"", ",", "\"dateModified\"", ",", "\"relations\"", ",", "# attachment items", "\"parentItem\"", ",", "\"mtime\"", ",", "\"contentType\"", ",", "\"md5\"", ",", "\"filename\"", ",", "]", ")", "template", "=", "template", "|", "set", "(", "self", ".", "temp_keys", ")", "for", "pos", ",", "item", "in", "enumerate", "(", "items", ")", ":", "if", "set", "(", "item", ")", "==", "set", "(", "[", "\"links\"", ",", "\"library\"", ",", "\"version\"", ",", "\"meta\"", ",", "\"key\"", ",", "\"data\"", "]", ")", ":", "# we have an item that was retrieved from the API", "item", "=", "item", "[", "\"data\"", "]", "to_check", "=", "set", "(", "i", "for", "i", "in", "list", "(", "item", ".", "keys", "(", ")", ")", ")", "difference", "=", "to_check", ".", "difference", "(", "template", ")", "if", "difference", ":", "raise", "ze", ".", "InvalidItemFields", "(", "\"Invalid keys present in item %s: %s\"", "%", "(", "pos", "+", "1", ",", "\" \"", ".", "join", "(", "i", "for", "i", "in", "difference", ")", ")", ")", "return", "items" ]
b378966b30146a952f7953c23202fb5a1ddf81d9
valid
Zotero.fields_types
Retrieve item fields or creator types
pyzotero/zotero.py
def fields_types(self, tname, qstring, itemtype): """ Retrieve item fields or creator types """ # check for a valid cached version template_name = tname + itemtype query_string = qstring.format(i=itemtype) if self.templates.get(template_name) and not self._updated( query_string, self.templates[template_name], template_name ): return self.templates[template_name]["tmplt"] # otherwise perform a normal request and cache the response retrieved = self._retrieve_data(query_string) return self._cache(retrieved, template_name)
def fields_types(self, tname, qstring, itemtype): """ Retrieve item fields or creator types """ # check for a valid cached version template_name = tname + itemtype query_string = qstring.format(i=itemtype) if self.templates.get(template_name) and not self._updated( query_string, self.templates[template_name], template_name ): return self.templates[template_name]["tmplt"] # otherwise perform a normal request and cache the response retrieved = self._retrieve_data(query_string) return self._cache(retrieved, template_name)
[ "Retrieve", "item", "fields", "or", "creator", "types" ]
urschrei/pyzotero
python
https://github.com/urschrei/pyzotero/blob/b378966b30146a952f7953c23202fb5a1ddf81d9/pyzotero/zotero.py#L1094-L1106
[ "def", "fields_types", "(", "self", ",", "tname", ",", "qstring", ",", "itemtype", ")", ":", "# check for a valid cached version", "template_name", "=", "tname", "+", "itemtype", "query_string", "=", "qstring", ".", "format", "(", "i", "=", "itemtype", ")", "if", "self", ".", "templates", ".", "get", "(", "template_name", ")", "and", "not", "self", ".", "_updated", "(", "query_string", ",", "self", ".", "templates", "[", "template_name", "]", ",", "template_name", ")", ":", "return", "self", ".", "templates", "[", "template_name", "]", "[", "\"tmplt\"", "]", "# otherwise perform a normal request and cache the response", "retrieved", "=", "self", ".", "_retrieve_data", "(", "query_string", ")", "return", "self", ".", "_cache", "(", "retrieved", ",", "template_name", ")" ]
b378966b30146a952f7953c23202fb5a1ddf81d9
valid
Zotero.item_fields
Get all available item fields
pyzotero/zotero.py
def item_fields(self): """ Get all available item fields """ # Check for a valid cached version if self.templates.get("item_fields") and not self._updated( "/itemFields", self.templates["item_fields"], "item_fields" ): return self.templates["item_fields"]["tmplt"] query_string = "/itemFields" # otherwise perform a normal request and cache the response retrieved = self._retrieve_data(query_string) return self._cache(retrieved, "item_fields")
def item_fields(self): """ Get all available item fields """ # Check for a valid cached version if self.templates.get("item_fields") and not self._updated( "/itemFields", self.templates["item_fields"], "item_fields" ): return self.templates["item_fields"]["tmplt"] query_string = "/itemFields" # otherwise perform a normal request and cache the response retrieved = self._retrieve_data(query_string) return self._cache(retrieved, "item_fields")
[ "Get", "all", "available", "item", "fields" ]
urschrei/pyzotero
python
https://github.com/urschrei/pyzotero/blob/b378966b30146a952f7953c23202fb5a1ddf81d9/pyzotero/zotero.py#L1122-L1133
[ "def", "item_fields", "(", "self", ")", ":", "# Check for a valid cached version", "if", "self", ".", "templates", ".", "get", "(", "\"item_fields\"", ")", "and", "not", "self", ".", "_updated", "(", "\"/itemFields\"", ",", "self", ".", "templates", "[", "\"item_fields\"", "]", ",", "\"item_fields\"", ")", ":", "return", "self", ".", "templates", "[", "\"item_fields\"", "]", "[", "\"tmplt\"", "]", "query_string", "=", "\"/itemFields\"", "# otherwise perform a normal request and cache the response", "retrieved", "=", "self", ".", "_retrieve_data", "(", "query_string", ")", "return", "self", ".", "_cache", "(", "retrieved", ",", "\"item_fields\"", ")" ]
b378966b30146a952f7953c23202fb5a1ddf81d9
valid
Zotero.create_items
Create new Zotero items Accepts two arguments: a list containing one or more item dicts an optional parent item ID. Note that this can also be used to update existing items
pyzotero/zotero.py
def create_items(self, payload, parentid=None, last_modified=None): """ Create new Zotero items Accepts two arguments: a list containing one or more item dicts an optional parent item ID. Note that this can also be used to update existing items """ if len(payload) > 50: raise ze.TooManyItems("You may only create up to 50 items per call") # TODO: strip extra data if it's an existing item headers = {"Zotero-Write-Token": token(), "Content-Type": "application/json"} if last_modified is not None: headers["If-Unmodified-Since-Version"] = str(last_modified) to_send = json.dumps([i for i in self._cleanup(*payload, allow=("key"))]) headers.update(self.default_headers()) req = requests.post( url=self.endpoint + "/{t}/{u}/items".format(t=self.library_type, u=self.library_id), data=to_send, headers=dict(headers), ) self.request = req try: req.raise_for_status() except requests.exceptions.HTTPError: error_handler(req) resp = req.json() if parentid: # we need to create child items using PATCH # TODO: handle possibility of item creation + failed parent # attachment uheaders = { "If-Unmodified-Since-Version": req.headers["last-modified-version"] } uheaders.update(self.default_headers()) for value in resp["success"].values(): payload = json.dumps({"parentItem": parentid}) presp = requests.patch( url=self.endpoint + "/{t}/{u}/items/{v}".format( t=self.library_type, u=self.library_id, v=value ), data=payload, headers=dict(uheaders), ) self.request = presp try: presp.raise_for_status() except requests.exceptions.HTTPError: error_handler(presp) return resp
def create_items(self, payload, parentid=None, last_modified=None): """ Create new Zotero items Accepts two arguments: a list containing one or more item dicts an optional parent item ID. Note that this can also be used to update existing items """ if len(payload) > 50: raise ze.TooManyItems("You may only create up to 50 items per call") # TODO: strip extra data if it's an existing item headers = {"Zotero-Write-Token": token(), "Content-Type": "application/json"} if last_modified is not None: headers["If-Unmodified-Since-Version"] = str(last_modified) to_send = json.dumps([i for i in self._cleanup(*payload, allow=("key"))]) headers.update(self.default_headers()) req = requests.post( url=self.endpoint + "/{t}/{u}/items".format(t=self.library_type, u=self.library_id), data=to_send, headers=dict(headers), ) self.request = req try: req.raise_for_status() except requests.exceptions.HTTPError: error_handler(req) resp = req.json() if parentid: # we need to create child items using PATCH # TODO: handle possibility of item creation + failed parent # attachment uheaders = { "If-Unmodified-Since-Version": req.headers["last-modified-version"] } uheaders.update(self.default_headers()) for value in resp["success"].values(): payload = json.dumps({"parentItem": parentid}) presp = requests.patch( url=self.endpoint + "/{t}/{u}/items/{v}".format( t=self.library_type, u=self.library_id, v=value ), data=payload, headers=dict(uheaders), ) self.request = presp try: presp.raise_for_status() except requests.exceptions.HTTPError: error_handler(presp) return resp
[ "Create", "new", "Zotero", "items", "Accepts", "two", "arguments", ":", "a", "list", "containing", "one", "or", "more", "item", "dicts", "an", "optional", "parent", "item", "ID", ".", "Note", "that", "this", "can", "also", "be", "used", "to", "update", "existing", "items" ]
urschrei/pyzotero
python
https://github.com/urschrei/pyzotero/blob/b378966b30146a952f7953c23202fb5a1ddf81d9/pyzotero/zotero.py#L1135-L1186
[ "def", "create_items", "(", "self", ",", "payload", ",", "parentid", "=", "None", ",", "last_modified", "=", "None", ")", ":", "if", "len", "(", "payload", ")", ">", "50", ":", "raise", "ze", ".", "TooManyItems", "(", "\"You may only create up to 50 items per call\"", ")", "# TODO: strip extra data if it's an existing item", "headers", "=", "{", "\"Zotero-Write-Token\"", ":", "token", "(", ")", ",", "\"Content-Type\"", ":", "\"application/json\"", "}", "if", "last_modified", "is", "not", "None", ":", "headers", "[", "\"If-Unmodified-Since-Version\"", "]", "=", "str", "(", "last_modified", ")", "to_send", "=", "json", ".", "dumps", "(", "[", "i", "for", "i", "in", "self", ".", "_cleanup", "(", "*", "payload", ",", "allow", "=", "(", "\"key\"", ")", ")", "]", ")", "headers", ".", "update", "(", "self", ".", "default_headers", "(", ")", ")", "req", "=", "requests", ".", "post", "(", "url", "=", "self", ".", "endpoint", "+", "\"/{t}/{u}/items\"", ".", "format", "(", "t", "=", "self", ".", "library_type", ",", "u", "=", "self", ".", "library_id", ")", ",", "data", "=", "to_send", ",", "headers", "=", "dict", "(", "headers", ")", ",", ")", "self", ".", "request", "=", "req", "try", ":", "req", ".", "raise_for_status", "(", ")", "except", "requests", ".", "exceptions", ".", "HTTPError", ":", "error_handler", "(", "req", ")", "resp", "=", "req", ".", "json", "(", ")", "if", "parentid", ":", "# we need to create child items using PATCH", "# TODO: handle possibility of item creation + failed parent", "# attachment", "uheaders", "=", "{", "\"If-Unmodified-Since-Version\"", ":", "req", ".", "headers", "[", "\"last-modified-version\"", "]", "}", "uheaders", ".", "update", "(", "self", ".", "default_headers", "(", ")", ")", "for", "value", "in", "resp", "[", "\"success\"", "]", ".", "values", "(", ")", ":", "payload", "=", "json", ".", "dumps", "(", "{", "\"parentItem\"", ":", "parentid", "}", ")", "presp", "=", "requests", ".", "patch", "(", "url", "=", "self", ".", "endpoint", "+", "\"/{t}/{u}/items/{v}\"", ".", "format", "(", "t", "=", "self", ".", "library_type", ",", "u", "=", "self", ".", "library_id", ",", "v", "=", "value", ")", ",", "data", "=", "payload", ",", "headers", "=", "dict", "(", "uheaders", ")", ",", ")", "self", ".", "request", "=", "presp", "try", ":", "presp", ".", "raise_for_status", "(", ")", "except", "requests", ".", "exceptions", ".", "HTTPError", ":", "error_handler", "(", "presp", ")", "return", "resp" ]
b378966b30146a952f7953c23202fb5a1ddf81d9
valid
Zotero.create_collections
Create new Zotero collections Accepts one argument, a list of dicts containing the following keys: 'name': the name of the collection 'parentCollection': OPTIONAL, the parent collection to which you wish to add this
pyzotero/zotero.py
def create_collections(self, payload, last_modified=None): """ Create new Zotero collections Accepts one argument, a list of dicts containing the following keys: 'name': the name of the collection 'parentCollection': OPTIONAL, the parent collection to which you wish to add this """ # no point in proceeding if there's no 'name' key for item in payload: if "name" not in item: raise ze.ParamNotPassed("The dict you pass must include a 'name' key") # add a blank 'parentCollection' key if it hasn't been passed if "parentCollection" not in item: item["parentCollection"] = "" headers = {"Zotero-Write-Token": token()} if last_modified is not None: headers["If-Unmodified-Since-Version"] = str(last_modified) headers.update(self.default_headers()) req = requests.post( url=self.endpoint + "/{t}/{u}/collections".format(t=self.library_type, u=self.library_id), headers=headers, data=json.dumps(payload), ) self.request = req try: req.raise_for_status() except requests.exceptions.HTTPError: error_handler(req) return req.json()
def create_collections(self, payload, last_modified=None): """ Create new Zotero collections Accepts one argument, a list of dicts containing the following keys: 'name': the name of the collection 'parentCollection': OPTIONAL, the parent collection to which you wish to add this """ # no point in proceeding if there's no 'name' key for item in payload: if "name" not in item: raise ze.ParamNotPassed("The dict you pass must include a 'name' key") # add a blank 'parentCollection' key if it hasn't been passed if "parentCollection" not in item: item["parentCollection"] = "" headers = {"Zotero-Write-Token": token()} if last_modified is not None: headers["If-Unmodified-Since-Version"] = str(last_modified) headers.update(self.default_headers()) req = requests.post( url=self.endpoint + "/{t}/{u}/collections".format(t=self.library_type, u=self.library_id), headers=headers, data=json.dumps(payload), ) self.request = req try: req.raise_for_status() except requests.exceptions.HTTPError: error_handler(req) return req.json()
[ "Create", "new", "Zotero", "collections", "Accepts", "one", "argument", "a", "list", "of", "dicts", "containing", "the", "following", "keys", ":" ]
urschrei/pyzotero
python
https://github.com/urschrei/pyzotero/blob/b378966b30146a952f7953c23202fb5a1ddf81d9/pyzotero/zotero.py#L1192-L1222
[ "def", "create_collections", "(", "self", ",", "payload", ",", "last_modified", "=", "None", ")", ":", "# no point in proceeding if there's no 'name' key", "for", "item", "in", "payload", ":", "if", "\"name\"", "not", "in", "item", ":", "raise", "ze", ".", "ParamNotPassed", "(", "\"The dict you pass must include a 'name' key\"", ")", "# add a blank 'parentCollection' key if it hasn't been passed", "if", "\"parentCollection\"", "not", "in", "item", ":", "item", "[", "\"parentCollection\"", "]", "=", "\"\"", "headers", "=", "{", "\"Zotero-Write-Token\"", ":", "token", "(", ")", "}", "if", "last_modified", "is", "not", "None", ":", "headers", "[", "\"If-Unmodified-Since-Version\"", "]", "=", "str", "(", "last_modified", ")", "headers", ".", "update", "(", "self", ".", "default_headers", "(", ")", ")", "req", "=", "requests", ".", "post", "(", "url", "=", "self", ".", "endpoint", "+", "\"/{t}/{u}/collections\"", ".", "format", "(", "t", "=", "self", ".", "library_type", ",", "u", "=", "self", ".", "library_id", ")", ",", "headers", "=", "headers", ",", "data", "=", "json", ".", "dumps", "(", "payload", ")", ",", ")", "self", ".", "request", "=", "req", "try", ":", "req", ".", "raise_for_status", "(", ")", "except", "requests", ".", "exceptions", ".", "HTTPError", ":", "error_handler", "(", "req", ")", "return", "req", ".", "json", "(", ")" ]
b378966b30146a952f7953c23202fb5a1ddf81d9
valid
Zotero.update_collection
Update a Zotero collection property such as 'name' Accepts one argument, a dict containing collection data retrieved using e.g. 'collections()'
pyzotero/zotero.py
def update_collection(self, payload, last_modified=None): """ Update a Zotero collection property such as 'name' Accepts one argument, a dict containing collection data retrieved using e.g. 'collections()' """ modified = payload["version"] if last_modified is not None: modified = last_modified key = payload["key"] headers = {"If-Unmodified-Since-Version": str(modified)} headers.update(self.default_headers()) headers.update({"Content-Type": "application/json"}) req = requests.put( url=self.endpoint + "/{t}/{u}/collections/{c}".format( t=self.library_type, u=self.library_id, c=key ), headers=headers, data=json.dumps(payload), ) self.request = req try: req.raise_for_status() except requests.exceptions.HTTPError: error_handler(req) return True
def update_collection(self, payload, last_modified=None): """ Update a Zotero collection property such as 'name' Accepts one argument, a dict containing collection data retrieved using e.g. 'collections()' """ modified = payload["version"] if last_modified is not None: modified = last_modified key = payload["key"] headers = {"If-Unmodified-Since-Version": str(modified)} headers.update(self.default_headers()) headers.update({"Content-Type": "application/json"}) req = requests.put( url=self.endpoint + "/{t}/{u}/collections/{c}".format( t=self.library_type, u=self.library_id, c=key ), headers=headers, data=json.dumps(payload), ) self.request = req try: req.raise_for_status() except requests.exceptions.HTTPError: error_handler(req) return True
[ "Update", "a", "Zotero", "collection", "property", "such", "as", "name", "Accepts", "one", "argument", "a", "dict", "containing", "collection", "data", "retrieved", "using", "e", ".", "g", ".", "collections", "()" ]
urschrei/pyzotero
python
https://github.com/urschrei/pyzotero/blob/b378966b30146a952f7953c23202fb5a1ddf81d9/pyzotero/zotero.py#L1224-L1250
[ "def", "update_collection", "(", "self", ",", "payload", ",", "last_modified", "=", "None", ")", ":", "modified", "=", "payload", "[", "\"version\"", "]", "if", "last_modified", "is", "not", "None", ":", "modified", "=", "last_modified", "key", "=", "payload", "[", "\"key\"", "]", "headers", "=", "{", "\"If-Unmodified-Since-Version\"", ":", "str", "(", "modified", ")", "}", "headers", ".", "update", "(", "self", ".", "default_headers", "(", ")", ")", "headers", ".", "update", "(", "{", "\"Content-Type\"", ":", "\"application/json\"", "}", ")", "req", "=", "requests", ".", "put", "(", "url", "=", "self", ".", "endpoint", "+", "\"/{t}/{u}/collections/{c}\"", ".", "format", "(", "t", "=", "self", ".", "library_type", ",", "u", "=", "self", ".", "library_id", ",", "c", "=", "key", ")", ",", "headers", "=", "headers", ",", "data", "=", "json", ".", "dumps", "(", "payload", ")", ",", ")", "self", ".", "request", "=", "req", "try", ":", "req", ".", "raise_for_status", "(", ")", "except", "requests", ".", "exceptions", ".", "HTTPError", ":", "error_handler", "(", "req", ")", "return", "True" ]
b378966b30146a952f7953c23202fb5a1ddf81d9
valid
Zotero.attachment_simple
Add attachments using filenames as title Arguments: One or more file paths to add as attachments: An optional Item ID, which will create child attachments
pyzotero/zotero.py
def attachment_simple(self, files, parentid=None): """ Add attachments using filenames as title Arguments: One or more file paths to add as attachments: An optional Item ID, which will create child attachments """ orig = self._attachment_template("imported_file") to_add = [orig.copy() for fls in files] for idx, tmplt in enumerate(to_add): tmplt["title"] = os.path.basename(files[idx]) tmplt["filename"] = files[idx] if parentid: return self._attachment(to_add, parentid) else: return self._attachment(to_add)
def attachment_simple(self, files, parentid=None): """ Add attachments using filenames as title Arguments: One or more file paths to add as attachments: An optional Item ID, which will create child attachments """ orig = self._attachment_template("imported_file") to_add = [orig.copy() for fls in files] for idx, tmplt in enumerate(to_add): tmplt["title"] = os.path.basename(files[idx]) tmplt["filename"] = files[idx] if parentid: return self._attachment(to_add, parentid) else: return self._attachment(to_add)
[ "Add", "attachments", "using", "filenames", "as", "title", "Arguments", ":", "One", "or", "more", "file", "paths", "to", "add", "as", "attachments", ":", "An", "optional", "Item", "ID", "which", "will", "create", "child", "attachments" ]
urschrei/pyzotero
python
https://github.com/urschrei/pyzotero/blob/b378966b30146a952f7953c23202fb5a1ddf81d9/pyzotero/zotero.py#L1252-L1267
[ "def", "attachment_simple", "(", "self", ",", "files", ",", "parentid", "=", "None", ")", ":", "orig", "=", "self", ".", "_attachment_template", "(", "\"imported_file\"", ")", "to_add", "=", "[", "orig", ".", "copy", "(", ")", "for", "fls", "in", "files", "]", "for", "idx", ",", "tmplt", "in", "enumerate", "(", "to_add", ")", ":", "tmplt", "[", "\"title\"", "]", "=", "os", ".", "path", ".", "basename", "(", "files", "[", "idx", "]", ")", "tmplt", "[", "\"filename\"", "]", "=", "files", "[", "idx", "]", "if", "parentid", ":", "return", "self", ".", "_attachment", "(", "to_add", ",", "parentid", ")", "else", ":", "return", "self", ".", "_attachment", "(", "to_add", ")" ]
b378966b30146a952f7953c23202fb5a1ddf81d9
valid
Zotero.attachment_both
Add child attachments using title, filename Arguments: One or more lists or tuples containing title, file path An optional Item ID, which will create child attachments
pyzotero/zotero.py
def attachment_both(self, files, parentid=None): """ Add child attachments using title, filename Arguments: One or more lists or tuples containing title, file path An optional Item ID, which will create child attachments """ orig = self._attachment_template("imported_file") to_add = [orig.copy() for f in files] for idx, tmplt in enumerate(to_add): tmplt["title"] = files[idx][0] tmplt["filename"] = files[idx][1] if parentid: return self._attachment(to_add, parentid) else: return self._attachment(to_add)
def attachment_both(self, files, parentid=None): """ Add child attachments using title, filename Arguments: One or more lists or tuples containing title, file path An optional Item ID, which will create child attachments """ orig = self._attachment_template("imported_file") to_add = [orig.copy() for f in files] for idx, tmplt in enumerate(to_add): tmplt["title"] = files[idx][0] tmplt["filename"] = files[idx][1] if parentid: return self._attachment(to_add, parentid) else: return self._attachment(to_add)
[ "Add", "child", "attachments", "using", "title", "filename", "Arguments", ":", "One", "or", "more", "lists", "or", "tuples", "containing", "title", "file", "path", "An", "optional", "Item", "ID", "which", "will", "create", "child", "attachments" ]
urschrei/pyzotero
python
https://github.com/urschrei/pyzotero/blob/b378966b30146a952f7953c23202fb5a1ddf81d9/pyzotero/zotero.py#L1269-L1284
[ "def", "attachment_both", "(", "self", ",", "files", ",", "parentid", "=", "None", ")", ":", "orig", "=", "self", ".", "_attachment_template", "(", "\"imported_file\"", ")", "to_add", "=", "[", "orig", ".", "copy", "(", ")", "for", "f", "in", "files", "]", "for", "idx", ",", "tmplt", "in", "enumerate", "(", "to_add", ")", ":", "tmplt", "[", "\"title\"", "]", "=", "files", "[", "idx", "]", "[", "0", "]", "tmplt", "[", "\"filename\"", "]", "=", "files", "[", "idx", "]", "[", "1", "]", "if", "parentid", ":", "return", "self", ".", "_attachment", "(", "to_add", ",", "parentid", ")", "else", ":", "return", "self", ".", "_attachment", "(", "to_add", ")" ]
b378966b30146a952f7953c23202fb5a1ddf81d9
valid
Zotero.update_item
Update an existing item Accepts one argument, a dict containing Item data
pyzotero/zotero.py
def update_item(self, payload, last_modified=None): """ Update an existing item Accepts one argument, a dict containing Item data """ to_send = self.check_items([payload])[0] if last_modified is None: modified = payload["version"] else: modified = last_modified ident = payload["key"] headers = {"If-Unmodified-Since-Version": str(modified)} headers.update(self.default_headers()) req = requests.patch( url=self.endpoint + "/{t}/{u}/items/{id}".format( t=self.library_type, u=self.library_id, id=ident ), headers=headers, data=json.dumps(to_send), ) self.request = req try: req.raise_for_status() except requests.exceptions.HTTPError: error_handler(req) return True
def update_item(self, payload, last_modified=None): """ Update an existing item Accepts one argument, a dict containing Item data """ to_send = self.check_items([payload])[0] if last_modified is None: modified = payload["version"] else: modified = last_modified ident = payload["key"] headers = {"If-Unmodified-Since-Version": str(modified)} headers.update(self.default_headers()) req = requests.patch( url=self.endpoint + "/{t}/{u}/items/{id}".format( t=self.library_type, u=self.library_id, id=ident ), headers=headers, data=json.dumps(to_send), ) self.request = req try: req.raise_for_status() except requests.exceptions.HTTPError: error_handler(req) return True
[ "Update", "an", "existing", "item", "Accepts", "one", "argument", "a", "dict", "containing", "Item", "data" ]
urschrei/pyzotero
python
https://github.com/urschrei/pyzotero/blob/b378966b30146a952f7953c23202fb5a1ddf81d9/pyzotero/zotero.py#L1286-L1312
[ "def", "update_item", "(", "self", ",", "payload", ",", "last_modified", "=", "None", ")", ":", "to_send", "=", "self", ".", "check_items", "(", "[", "payload", "]", ")", "[", "0", "]", "if", "last_modified", "is", "None", ":", "modified", "=", "payload", "[", "\"version\"", "]", "else", ":", "modified", "=", "last_modified", "ident", "=", "payload", "[", "\"key\"", "]", "headers", "=", "{", "\"If-Unmodified-Since-Version\"", ":", "str", "(", "modified", ")", "}", "headers", ".", "update", "(", "self", ".", "default_headers", "(", ")", ")", "req", "=", "requests", ".", "patch", "(", "url", "=", "self", ".", "endpoint", "+", "\"/{t}/{u}/items/{id}\"", ".", "format", "(", "t", "=", "self", ".", "library_type", ",", "u", "=", "self", ".", "library_id", ",", "id", "=", "ident", ")", ",", "headers", "=", "headers", ",", "data", "=", "json", ".", "dumps", "(", "to_send", ")", ",", ")", "self", ".", "request", "=", "req", "try", ":", "req", ".", "raise_for_status", "(", ")", "except", "requests", ".", "exceptions", ".", "HTTPError", ":", "error_handler", "(", "req", ")", "return", "True" ]
b378966b30146a952f7953c23202fb5a1ddf81d9
valid
Zotero.update_items
Update existing items Accepts one argument, a list of dicts containing Item data
pyzotero/zotero.py
def update_items(self, payload): """ Update existing items Accepts one argument, a list of dicts containing Item data """ to_send = [self.check_items([p])[0] for p in payload] headers = {} headers.update(self.default_headers()) # the API only accepts 50 items at a time, so we have to split # anything longer for chunk in chunks(to_send, 50): req = requests.post( url=self.endpoint + "/{t}/{u}/items/".format(t=self.library_type, u=self.library_id), headers=headers, data=json.dumps(chunk), ) self.request = req try: req.raise_for_status() except requests.exceptions.HTTPError: error_handler(req) return True
def update_items(self, payload): """ Update existing items Accepts one argument, a list of dicts containing Item data """ to_send = [self.check_items([p])[0] for p in payload] headers = {} headers.update(self.default_headers()) # the API only accepts 50 items at a time, so we have to split # anything longer for chunk in chunks(to_send, 50): req = requests.post( url=self.endpoint + "/{t}/{u}/items/".format(t=self.library_type, u=self.library_id), headers=headers, data=json.dumps(chunk), ) self.request = req try: req.raise_for_status() except requests.exceptions.HTTPError: error_handler(req) return True
[ "Update", "existing", "items", "Accepts", "one", "argument", "a", "list", "of", "dicts", "containing", "Item", "data" ]
urschrei/pyzotero
python
https://github.com/urschrei/pyzotero/blob/b378966b30146a952f7953c23202fb5a1ddf81d9/pyzotero/zotero.py#L1314-L1336
[ "def", "update_items", "(", "self", ",", "payload", ")", ":", "to_send", "=", "[", "self", ".", "check_items", "(", "[", "p", "]", ")", "[", "0", "]", "for", "p", "in", "payload", "]", "headers", "=", "{", "}", "headers", ".", "update", "(", "self", ".", "default_headers", "(", ")", ")", "# the API only accepts 50 items at a time, so we have to split", "# anything longer", "for", "chunk", "in", "chunks", "(", "to_send", ",", "50", ")", ":", "req", "=", "requests", ".", "post", "(", "url", "=", "self", ".", "endpoint", "+", "\"/{t}/{u}/items/\"", ".", "format", "(", "t", "=", "self", ".", "library_type", ",", "u", "=", "self", ".", "library_id", ")", ",", "headers", "=", "headers", ",", "data", "=", "json", ".", "dumps", "(", "chunk", ")", ",", ")", "self", ".", "request", "=", "req", "try", ":", "req", ".", "raise_for_status", "(", ")", "except", "requests", ".", "exceptions", ".", "HTTPError", ":", "error_handler", "(", "req", ")", "return", "True" ]
b378966b30146a952f7953c23202fb5a1ddf81d9
valid
Zotero.addto_collection
Add one or more items to a collection Accepts two arguments: The collection ID, and an item dict
pyzotero/zotero.py
def addto_collection(self, collection, payload): """ Add one or more items to a collection Accepts two arguments: The collection ID, and an item dict """ ident = payload["key"] modified = payload["version"] # add the collection data from the item modified_collections = payload["data"]["collections"] + [collection] headers = {"If-Unmodified-Since-Version": str(modified)} headers.update(self.default_headers()) req = requests.patch( url=self.endpoint + "/{t}/{u}/items/{i}".format( t=self.library_type, u=self.library_id, i=ident ), data=json.dumps({"collections": modified_collections}), headers=headers, ) self.request = req try: req.raise_for_status() except requests.exceptions.HTTPError: error_handler(req) return True
def addto_collection(self, collection, payload): """ Add one or more items to a collection Accepts two arguments: The collection ID, and an item dict """ ident = payload["key"] modified = payload["version"] # add the collection data from the item modified_collections = payload["data"]["collections"] + [collection] headers = {"If-Unmodified-Since-Version": str(modified)} headers.update(self.default_headers()) req = requests.patch( url=self.endpoint + "/{t}/{u}/items/{i}".format( t=self.library_type, u=self.library_id, i=ident ), data=json.dumps({"collections": modified_collections}), headers=headers, ) self.request = req try: req.raise_for_status() except requests.exceptions.HTTPError: error_handler(req) return True
[ "Add", "one", "or", "more", "items", "to", "a", "collection", "Accepts", "two", "arguments", ":", "The", "collection", "ID", "and", "an", "item", "dict" ]
urschrei/pyzotero
python
https://github.com/urschrei/pyzotero/blob/b378966b30146a952f7953c23202fb5a1ddf81d9/pyzotero/zotero.py#L1364-L1389
[ "def", "addto_collection", "(", "self", ",", "collection", ",", "payload", ")", ":", "ident", "=", "payload", "[", "\"key\"", "]", "modified", "=", "payload", "[", "\"version\"", "]", "# add the collection data from the item", "modified_collections", "=", "payload", "[", "\"data\"", "]", "[", "\"collections\"", "]", "+", "[", "collection", "]", "headers", "=", "{", "\"If-Unmodified-Since-Version\"", ":", "str", "(", "modified", ")", "}", "headers", ".", "update", "(", "self", ".", "default_headers", "(", ")", ")", "req", "=", "requests", ".", "patch", "(", "url", "=", "self", ".", "endpoint", "+", "\"/{t}/{u}/items/{i}\"", ".", "format", "(", "t", "=", "self", ".", "library_type", ",", "u", "=", "self", ".", "library_id", ",", "i", "=", "ident", ")", ",", "data", "=", "json", ".", "dumps", "(", "{", "\"collections\"", ":", "modified_collections", "}", ")", ",", "headers", "=", "headers", ",", ")", "self", ".", "request", "=", "req", "try", ":", "req", ".", "raise_for_status", "(", ")", "except", "requests", ".", "exceptions", ".", "HTTPError", ":", "error_handler", "(", "req", ")", "return", "True" ]
b378966b30146a952f7953c23202fb5a1ddf81d9