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
test
Alea.random_string
Return string of `length` elements chosen from `alphabet`.
dddp/alea.py
def random_string(self, length, alphabet): """Return string of `length` elements chosen from `alphabet`.""" return ''.join( self.choice(alphabet) for n in range(length) )
def random_string(self, length, alphabet): """Return string of `length` elements chosen from `alphabet`.""" return ''.join( self.choice(alphabet) for n in range(length) )
[ "Return", "string", "of", "length", "elements", "chosen", "from", "alphabet", "." ]
jazzband/django-ddp
python
https://github.com/jazzband/django-ddp/blob/1e1954b06fe140346acea43582515991685e4e01/dddp/alea.py#L154-L158
[ "def", "random_string", "(", "self", ",", "length", ",", "alphabet", ")", ":", "return", "''", ".", "join", "(", "self", ".", "choice", "(", "alphabet", ")", "for", "n", "in", "range", "(", "length", ")", ")" ]
1e1954b06fe140346acea43582515991685e4e01
test
api_endpoint
Decorator to mark a method as an API endpoint for later registration. Args: path_or_func: either the function to be decorated or its API path. decorate (bool): Apply API_ENDPOINT_DECORATORS if True (default). Returns: Callable: Decorated function (with optionally applied decorators). Examples: >>> from dddp.api import APIMixin, api_endpoint >>> class Counter(APIMixin): ... value = 0 ... ... # default API path matches function name 'increment'. ... @api_endpoint ... def increment(self, amount): ... '''Increment counter value by `amount`.''' ... self.value += amount ... return self.value ... ... # excplicitly set API path to 'Decrement'. ... @api_endpoint('Decrement') ... def decrement(self, amount): ... '''Decrement counter value by `amount`.''' ... self.value -= amount ... return self.value
dddp/api.py
def api_endpoint(path_or_func=None, decorate=True): """ Decorator to mark a method as an API endpoint for later registration. Args: path_or_func: either the function to be decorated or its API path. decorate (bool): Apply API_ENDPOINT_DECORATORS if True (default). Returns: Callable: Decorated function (with optionally applied decorators). Examples: >>> from dddp.api import APIMixin, api_endpoint >>> class Counter(APIMixin): ... value = 0 ... ... # default API path matches function name 'increment'. ... @api_endpoint ... def increment(self, amount): ... '''Increment counter value by `amount`.''' ... self.value += amount ... return self.value ... ... # excplicitly set API path to 'Decrement'. ... @api_endpoint('Decrement') ... def decrement(self, amount): ... '''Decrement counter value by `amount`.''' ... self.value -= amount ... return self.value """ def maybe_decorated(func): """Apply API_ENDPOINT_DECORATORS to func.""" if decorate: for decorator in API_ENDPOINT_DECORATORS: func = decorator()(func) return func if callable(path_or_func): path_or_func.api_path = path_or_func.__name__ return maybe_decorated(path_or_func) else: def _api_endpoint(func): """Decorator inner.""" if path_or_func is None: func.api_path = func.__name__ else: func.api_path = path_or_func return maybe_decorated(func) return _api_endpoint
def api_endpoint(path_or_func=None, decorate=True): """ Decorator to mark a method as an API endpoint for later registration. Args: path_or_func: either the function to be decorated or its API path. decorate (bool): Apply API_ENDPOINT_DECORATORS if True (default). Returns: Callable: Decorated function (with optionally applied decorators). Examples: >>> from dddp.api import APIMixin, api_endpoint >>> class Counter(APIMixin): ... value = 0 ... ... # default API path matches function name 'increment'. ... @api_endpoint ... def increment(self, amount): ... '''Increment counter value by `amount`.''' ... self.value += amount ... return self.value ... ... # excplicitly set API path to 'Decrement'. ... @api_endpoint('Decrement') ... def decrement(self, amount): ... '''Decrement counter value by `amount`.''' ... self.value -= amount ... return self.value """ def maybe_decorated(func): """Apply API_ENDPOINT_DECORATORS to func.""" if decorate: for decorator in API_ENDPOINT_DECORATORS: func = decorator()(func) return func if callable(path_or_func): path_or_func.api_path = path_or_func.__name__ return maybe_decorated(path_or_func) else: def _api_endpoint(func): """Decorator inner.""" if path_or_func is None: func.api_path = func.__name__ else: func.api_path = path_or_func return maybe_decorated(func) return _api_endpoint
[ "Decorator", "to", "mark", "a", "method", "as", "an", "API", "endpoint", "for", "later", "registration", "." ]
jazzband/django-ddp
python
https://github.com/jazzband/django-ddp/blob/1e1954b06fe140346acea43582515991685e4e01/dddp/api.py#L54-L103
[ "def", "api_endpoint", "(", "path_or_func", "=", "None", ",", "decorate", "=", "True", ")", ":", "def", "maybe_decorated", "(", "func", ")", ":", "\"\"\"Apply API_ENDPOINT_DECORATORS to func.\"\"\"", "if", "decorate", ":", "for", "decorator", "in", "API_ENDPOINT_DECORATORS", ":", "func", "=", "decorator", "(", ")", "(", "func", ")", "return", "func", "if", "callable", "(", "path_or_func", ")", ":", "path_or_func", ".", "api_path", "=", "path_or_func", ".", "__name__", "return", "maybe_decorated", "(", "path_or_func", ")", "else", ":", "def", "_api_endpoint", "(", "func", ")", ":", "\"\"\"Decorator inner.\"\"\"", "if", "path_or_func", "is", "None", ":", "func", ".", "api_path", "=", "func", ".", "__name__", "else", ":", "func", ".", "api_path", "=", "path_or_func", "return", "maybe_decorated", "(", "func", ")", "return", "_api_endpoint" ]
1e1954b06fe140346acea43582515991685e4e01
test
api_endpoints
Iterator over all API endpoint names and callbacks.
dddp/api.py
def api_endpoints(obj): """Iterator over all API endpoint names and callbacks.""" for name in dir(obj): attr = getattr(obj, name) api_path = getattr(attr, 'api_path', None) if api_path: yield ( '%s%s' % (obj.api_path_prefix, api_path), attr, ) for api_provider in obj.api_providers: for api_path, attr in api_endpoints(api_provider): yield (api_path, attr)
def api_endpoints(obj): """Iterator over all API endpoint names and callbacks.""" for name in dir(obj): attr = getattr(obj, name) api_path = getattr(attr, 'api_path', None) if api_path: yield ( '%s%s' % (obj.api_path_prefix, api_path), attr, ) for api_provider in obj.api_providers: for api_path, attr in api_endpoints(api_provider): yield (api_path, attr)
[ "Iterator", "over", "all", "API", "endpoint", "names", "and", "callbacks", "." ]
jazzband/django-ddp
python
https://github.com/jazzband/django-ddp/blob/1e1954b06fe140346acea43582515991685e4e01/dddp/api.py#L106-L118
[ "def", "api_endpoints", "(", "obj", ")", ":", "for", "name", "in", "dir", "(", "obj", ")", ":", "attr", "=", "getattr", "(", "obj", ",", "name", ")", "api_path", "=", "getattr", "(", "attr", ",", "'api_path'", ",", "None", ")", "if", "api_path", ":", "yield", "(", "'%s%s'", "%", "(", "obj", ".", "api_path_prefix", ",", "api_path", ")", ",", "attr", ",", ")", "for", "api_provider", "in", "obj", ".", "api_providers", ":", "for", "api_path", ",", "attr", "in", "api_endpoints", "(", "api_provider", ")", ":", "yield", "(", "api_path", ",", "attr", ")" ]
1e1954b06fe140346acea43582515991685e4e01
test
APIMixin.api_path_map
Cached dict of api_path: func.
dddp/api.py
def api_path_map(self): """Cached dict of api_path: func.""" if self._api_path_cache is None: self._api_path_cache = { api_path: func for api_path, func in api_endpoints(self) } return self._api_path_cache
def api_path_map(self): """Cached dict of api_path: func.""" if self._api_path_cache is None: self._api_path_cache = { api_path: func for api_path, func in api_endpoints(self) } return self._api_path_cache
[ "Cached", "dict", "of", "api_path", ":", "func", "." ]
jazzband/django-ddp
python
https://github.com/jazzband/django-ddp/blob/1e1954b06fe140346acea43582515991685e4e01/dddp/api.py#L149-L157
[ "def", "api_path_map", "(", "self", ")", ":", "if", "self", ".", "_api_path_cache", "is", "None", ":", "self", ".", "_api_path_cache", "=", "{", "api_path", ":", "func", "for", "api_path", ",", "func", "in", "api_endpoints", "(", "self", ")", "}", "return", "self", ".", "_api_path_cache" ]
1e1954b06fe140346acea43582515991685e4e01
test
APIMixin.clear_api_path_map_cache
Clear out cache for api_path_map.
dddp/api.py
def clear_api_path_map_cache(self): """Clear out cache for api_path_map.""" self._api_path_cache = None for api_provider in self.api_providers: if six.get_method_self( api_provider.clear_api_path_map_cache, ) is not None: api_provider.clear_api_path_map_cache()
def clear_api_path_map_cache(self): """Clear out cache for api_path_map.""" self._api_path_cache = None for api_provider in self.api_providers: if six.get_method_self( api_provider.clear_api_path_map_cache, ) is not None: api_provider.clear_api_path_map_cache()
[ "Clear", "out", "cache", "for", "api_path_map", "." ]
jazzband/django-ddp
python
https://github.com/jazzband/django-ddp/blob/1e1954b06fe140346acea43582515991685e4e01/dddp/api.py#L159-L166
[ "def", "clear_api_path_map_cache", "(", "self", ")", ":", "self", ".", "_api_path_cache", "=", "None", "for", "api_provider", "in", "self", ".", "api_providers", ":", "if", "six", ".", "get_method_self", "(", "api_provider", ".", "clear_api_path_map_cache", ",", ")", "is", "not", "None", ":", "api_provider", ".", "clear_api_path_map_cache", "(", ")" ]
1e1954b06fe140346acea43582515991685e4e01
test
safe_call
Call `func(*args, **kwargs)` but NEVER raise an exception. Useful in situations such as inside exception handlers where calls to `logging.error` try to send email, but the SMTP server isn't always availalbe and you don't want your exception handler blowing up.
dddp/websocket.py
def safe_call(func, *args, **kwargs): """ Call `func(*args, **kwargs)` but NEVER raise an exception. Useful in situations such as inside exception handlers where calls to `logging.error` try to send email, but the SMTP server isn't always availalbe and you don't want your exception handler blowing up. """ try: return None, func(*args, **kwargs) except Exception: # pylint: disable=broad-except # something went wrong during the call, return a stack trace that can # be dealt with by the caller return traceback.format_exc(), None
def safe_call(func, *args, **kwargs): """ Call `func(*args, **kwargs)` but NEVER raise an exception. Useful in situations such as inside exception handlers where calls to `logging.error` try to send email, but the SMTP server isn't always availalbe and you don't want your exception handler blowing up. """ try: return None, func(*args, **kwargs) except Exception: # pylint: disable=broad-except # something went wrong during the call, return a stack trace that can # be dealt with by the caller return traceback.format_exc(), None
[ "Call", "func", "(", "*", "args", "**", "kwargs", ")", "but", "NEVER", "raise", "an", "exception", "." ]
jazzband/django-ddp
python
https://github.com/jazzband/django-ddp/blob/1e1954b06fe140346acea43582515991685e4e01/dddp/websocket.py#L26-L39
[ "def", "safe_call", "(", "func", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "try", ":", "return", "None", ",", "func", "(", "*", "args", ",", "*", "*", "kwargs", ")", "except", "Exception", ":", "# pylint: disable=broad-except", "# something went wrong during the call, return a stack trace that can", "# be dealt with by the caller", "return", "traceback", ".", "format_exc", "(", ")", ",", "None" ]
1e1954b06fe140346acea43582515991685e4e01
test
dprint
Debug print name and val.
dddp/websocket.py
def dprint(name, val): """Debug print name and val.""" from pprint import pformat print( '% 5s: %s' % ( name, '\n '.join( pformat( val, indent=4, width=75, ).split('\n') ), ), )
def dprint(name, val): """Debug print name and val.""" from pprint import pformat print( '% 5s: %s' % ( name, '\n '.join( pformat( val, indent=4, width=75, ).split('\n') ), ), )
[ "Debug", "print", "name", "and", "val", "." ]
jazzband/django-ddp
python
https://github.com/jazzband/django-ddp/blob/1e1954b06fe140346acea43582515991685e4e01/dddp/websocket.py#L42-L54
[ "def", "dprint", "(", "name", ",", "val", ")", ":", "from", "pprint", "import", "pformat", "print", "(", "'% 5s: %s'", "%", "(", "name", ",", "'\\n '", ".", "join", "(", "pformat", "(", "val", ",", "indent", "=", "4", ",", "width", "=", "75", ",", ")", ".", "split", "(", "'\\n'", ")", ")", ",", ")", ",", ")" ]
1e1954b06fe140346acea43582515991685e4e01
test
validate_kwargs
Validate arguments to be supplied to func.
dddp/websocket.py
def validate_kwargs(func, kwargs): """Validate arguments to be supplied to func.""" func_name = func.__name__ argspec = inspect.getargspec(func) all_args = argspec.args[:] defaults = list(argspec.defaults or []) # ignore implicit 'self' argument if inspect.ismethod(func) and all_args[:1] == ['self']: all_args[:1] = [] # don't require arguments that have defaults if defaults: required = all_args[:-len(defaults)] else: required = all_args[:] # translate 'foo_' to avoid reserved names like 'id' trans = { arg: arg.endswith('_') and arg[:-1] or arg for arg in all_args } for key in list(kwargs): key_adj = '%s_' % key if key_adj in all_args: kwargs[key_adj] = kwargs.pop(key) # figure out what we're missing supplied = sorted(kwargs) missing = [ trans.get(arg, arg) for arg in required if arg not in supplied ] if missing: raise MeteorError( 400, func.err, 'Missing required arguments to %s: %s' % ( func_name, ' '.join(missing), ), ) # figure out what is extra extra = [ arg for arg in supplied if arg not in all_args ] if extra: raise MeteorError( 400, func.err, 'Unknown arguments to %s: %s' % (func_name, ' '.join(extra)), )
def validate_kwargs(func, kwargs): """Validate arguments to be supplied to func.""" func_name = func.__name__ argspec = inspect.getargspec(func) all_args = argspec.args[:] defaults = list(argspec.defaults or []) # ignore implicit 'self' argument if inspect.ismethod(func) and all_args[:1] == ['self']: all_args[:1] = [] # don't require arguments that have defaults if defaults: required = all_args[:-len(defaults)] else: required = all_args[:] # translate 'foo_' to avoid reserved names like 'id' trans = { arg: arg.endswith('_') and arg[:-1] or arg for arg in all_args } for key in list(kwargs): key_adj = '%s_' % key if key_adj in all_args: kwargs[key_adj] = kwargs.pop(key) # figure out what we're missing supplied = sorted(kwargs) missing = [ trans.get(arg, arg) for arg in required if arg not in supplied ] if missing: raise MeteorError( 400, func.err, 'Missing required arguments to %s: %s' % ( func_name, ' '.join(missing), ), ) # figure out what is extra extra = [ arg for arg in supplied if arg not in all_args ] if extra: raise MeteorError( 400, func.err, 'Unknown arguments to %s: %s' % (func_name, ' '.join(extra)), )
[ "Validate", "arguments", "to", "be", "supplied", "to", "func", "." ]
jazzband/django-ddp
python
https://github.com/jazzband/django-ddp/blob/1e1954b06fe140346acea43582515991685e4e01/dddp/websocket.py#L57-L111
[ "def", "validate_kwargs", "(", "func", ",", "kwargs", ")", ":", "func_name", "=", "func", ".", "__name__", "argspec", "=", "inspect", ".", "getargspec", "(", "func", ")", "all_args", "=", "argspec", ".", "args", "[", ":", "]", "defaults", "=", "list", "(", "argspec", ".", "defaults", "or", "[", "]", ")", "# ignore implicit 'self' argument", "if", "inspect", ".", "ismethod", "(", "func", ")", "and", "all_args", "[", ":", "1", "]", "==", "[", "'self'", "]", ":", "all_args", "[", ":", "1", "]", "=", "[", "]", "# don't require arguments that have defaults", "if", "defaults", ":", "required", "=", "all_args", "[", ":", "-", "len", "(", "defaults", ")", "]", "else", ":", "required", "=", "all_args", "[", ":", "]", "# translate 'foo_' to avoid reserved names like 'id'", "trans", "=", "{", "arg", ":", "arg", ".", "endswith", "(", "'_'", ")", "and", "arg", "[", ":", "-", "1", "]", "or", "arg", "for", "arg", "in", "all_args", "}", "for", "key", "in", "list", "(", "kwargs", ")", ":", "key_adj", "=", "'%s_'", "%", "key", "if", "key_adj", "in", "all_args", ":", "kwargs", "[", "key_adj", "]", "=", "kwargs", ".", "pop", "(", "key", ")", "# figure out what we're missing", "supplied", "=", "sorted", "(", "kwargs", ")", "missing", "=", "[", "trans", ".", "get", "(", "arg", ",", "arg", ")", "for", "arg", "in", "required", "if", "arg", "not", "in", "supplied", "]", "if", "missing", ":", "raise", "MeteorError", "(", "400", ",", "func", ".", "err", ",", "'Missing required arguments to %s: %s'", "%", "(", "func_name", ",", "' '", ".", "join", "(", "missing", ")", ",", ")", ",", ")", "# figure out what is extra", "extra", "=", "[", "arg", "for", "arg", "in", "supplied", "if", "arg", "not", "in", "all_args", "]", "if", "extra", ":", "raise", "MeteorError", "(", "400", ",", "func", ".", "err", ",", "'Unknown arguments to %s: %s'", "%", "(", "func_name", ",", "' '", ".", "join", "(", "extra", ")", ")", ",", ")" ]
1e1954b06fe140346acea43582515991685e4e01
test
DDPWebSocketApplication.on_open
Handle new websocket connection.
dddp/websocket.py
def on_open(self): """Handle new websocket connection.""" this.request = WSGIRequest(self.ws.environ) this.ws = self this.send = self.send this.reply = self.reply self.logger = self.ws.logger self.remote_ids = collections.defaultdict(set) # `_tx_buffer` collects outgoing messages which must be sent in order self._tx_buffer = {} # track the head of the queue (buffer) and the next msg to be sent self._tx_buffer_id_gen = itertools.cycle(irange(sys.maxint)) self._tx_next_id_gen = itertools.cycle(irange(sys.maxint)) # start by waiting for the very first message self._tx_next_id = next(self._tx_next_id_gen) this.remote_addr = self.remote_addr = \ '{0[REMOTE_ADDR]}:{0[REMOTE_PORT]}'.format( self.ws.environ, ) this.subs = {} safe_call(self.logger.info, '+ %s OPEN', self) self.send('o') self.send('a["{\\"server_id\\":\\"0\\"}"]')
def on_open(self): """Handle new websocket connection.""" this.request = WSGIRequest(self.ws.environ) this.ws = self this.send = self.send this.reply = self.reply self.logger = self.ws.logger self.remote_ids = collections.defaultdict(set) # `_tx_buffer` collects outgoing messages which must be sent in order self._tx_buffer = {} # track the head of the queue (buffer) and the next msg to be sent self._tx_buffer_id_gen = itertools.cycle(irange(sys.maxint)) self._tx_next_id_gen = itertools.cycle(irange(sys.maxint)) # start by waiting for the very first message self._tx_next_id = next(self._tx_next_id_gen) this.remote_addr = self.remote_addr = \ '{0[REMOTE_ADDR]}:{0[REMOTE_PORT]}'.format( self.ws.environ, ) this.subs = {} safe_call(self.logger.info, '+ %s OPEN', self) self.send('o') self.send('a["{\\"server_id\\":\\"0\\"}"]')
[ "Handle", "new", "websocket", "connection", "." ]
jazzband/django-ddp
python
https://github.com/jazzband/django-ddp/blob/1e1954b06fe140346acea43582515991685e4e01/dddp/websocket.py#L143-L168
[ "def", "on_open", "(", "self", ")", ":", "this", ".", "request", "=", "WSGIRequest", "(", "self", ".", "ws", ".", "environ", ")", "this", ".", "ws", "=", "self", "this", ".", "send", "=", "self", ".", "send", "this", ".", "reply", "=", "self", ".", "reply", "self", ".", "logger", "=", "self", ".", "ws", ".", "logger", "self", ".", "remote_ids", "=", "collections", ".", "defaultdict", "(", "set", ")", "# `_tx_buffer` collects outgoing messages which must be sent in order", "self", ".", "_tx_buffer", "=", "{", "}", "# track the head of the queue (buffer) and the next msg to be sent", "self", ".", "_tx_buffer_id_gen", "=", "itertools", ".", "cycle", "(", "irange", "(", "sys", ".", "maxint", ")", ")", "self", ".", "_tx_next_id_gen", "=", "itertools", ".", "cycle", "(", "irange", "(", "sys", ".", "maxint", ")", ")", "# start by waiting for the very first message", "self", ".", "_tx_next_id", "=", "next", "(", "self", ".", "_tx_next_id_gen", ")", "this", ".", "remote_addr", "=", "self", ".", "remote_addr", "=", "'{0[REMOTE_ADDR]}:{0[REMOTE_PORT]}'", ".", "format", "(", "self", ".", "ws", ".", "environ", ",", ")", "this", ".", "subs", "=", "{", "}", "safe_call", "(", "self", ".", "logger", ".", "info", ",", "'+ %s OPEN'", ",", "self", ")", "self", ".", "send", "(", "'o'", ")", "self", ".", "send", "(", "'a[\"{\\\\\"server_id\\\\\":\\\\\"0\\\\\"}\"]'", ")" ]
1e1954b06fe140346acea43582515991685e4e01
test
DDPWebSocketApplication.on_close
Handle closing of websocket connection.
dddp/websocket.py
def on_close(self, *args, **kwargs): """Handle closing of websocket connection.""" if self.connection is not None: del self.pgworker.connections[self.connection.pk] self.connection.delete() self.connection = None signals.request_finished.send(sender=self.__class__) safe_call(self.logger.info, '- %s %s', self, args or 'CLOSE')
def on_close(self, *args, **kwargs): """Handle closing of websocket connection.""" if self.connection is not None: del self.pgworker.connections[self.connection.pk] self.connection.delete() self.connection = None signals.request_finished.send(sender=self.__class__) safe_call(self.logger.info, '- %s %s', self, args or 'CLOSE')
[ "Handle", "closing", "of", "websocket", "connection", "." ]
jazzband/django-ddp
python
https://github.com/jazzband/django-ddp/blob/1e1954b06fe140346acea43582515991685e4e01/dddp/websocket.py#L174-L181
[ "def", "on_close", "(", "self", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "if", "self", ".", "connection", "is", "not", "None", ":", "del", "self", ".", "pgworker", ".", "connections", "[", "self", ".", "connection", ".", "pk", "]", "self", ".", "connection", ".", "delete", "(", ")", "self", ".", "connection", "=", "None", "signals", ".", "request_finished", ".", "send", "(", "sender", "=", "self", ".", "__class__", ")", "safe_call", "(", "self", ".", "logger", ".", "info", ",", "'- %s %s'", ",", "self", ",", "args", "or", "'CLOSE'", ")" ]
1e1954b06fe140346acea43582515991685e4e01
test
DDPWebSocketApplication.on_message
Process a message received from remote.
dddp/websocket.py
def on_message(self, message): """Process a message received from remote.""" if self.ws.closed: return None try: safe_call(self.logger.debug, '< %s %r', self, message) # process individual messages for data in self.ddp_frames_from_message(message): self.process_ddp(data) # emit request_finished signal to close DB connections signals.request_finished.send(sender=self.__class__) except geventwebsocket.WebSocketError: self.ws.close()
def on_message(self, message): """Process a message received from remote.""" if self.ws.closed: return None try: safe_call(self.logger.debug, '< %s %r', self, message) # process individual messages for data in self.ddp_frames_from_message(message): self.process_ddp(data) # emit request_finished signal to close DB connections signals.request_finished.send(sender=self.__class__) except geventwebsocket.WebSocketError: self.ws.close()
[ "Process", "a", "message", "received", "from", "remote", "." ]
jazzband/django-ddp
python
https://github.com/jazzband/django-ddp/blob/1e1954b06fe140346acea43582515991685e4e01/dddp/websocket.py#L183-L196
[ "def", "on_message", "(", "self", ",", "message", ")", ":", "if", "self", ".", "ws", ".", "closed", ":", "return", "None", "try", ":", "safe_call", "(", "self", ".", "logger", ".", "debug", ",", "'< %s %r'", ",", "self", ",", "message", ")", "# process individual messages", "for", "data", "in", "self", ".", "ddp_frames_from_message", "(", "message", ")", ":", "self", ".", "process_ddp", "(", "data", ")", "# emit request_finished signal to close DB connections", "signals", ".", "request_finished", ".", "send", "(", "sender", "=", "self", ".", "__class__", ")", "except", "geventwebsocket", ".", "WebSocketError", ":", "self", ".", "ws", ".", "close", "(", ")" ]
1e1954b06fe140346acea43582515991685e4e01
test
DDPWebSocketApplication.ddp_frames_from_message
Yield DDP messages from a raw WebSocket message.
dddp/websocket.py
def ddp_frames_from_message(self, message): """Yield DDP messages from a raw WebSocket message.""" # parse message set try: msgs = ejson.loads(message) except ValueError: self.reply( 'error', error=400, reason='Data is not valid EJSON', ) raise StopIteration if not isinstance(msgs, list): self.reply( 'error', error=400, reason='Invalid EJSON messages', ) raise StopIteration # process individual messages while msgs: # pop raw message from the list raw = msgs.pop(0) # parse message payload try: data = ejson.loads(raw) except (TypeError, ValueError): data = None if not isinstance(data, dict): self.reply( 'error', error=400, reason='Invalid SockJS DDP payload', offendingMessage=raw, ) yield data if msgs: # yield to other greenlets before processing next msg gevent.sleep()
def ddp_frames_from_message(self, message): """Yield DDP messages from a raw WebSocket message.""" # parse message set try: msgs = ejson.loads(message) except ValueError: self.reply( 'error', error=400, reason='Data is not valid EJSON', ) raise StopIteration if not isinstance(msgs, list): self.reply( 'error', error=400, reason='Invalid EJSON messages', ) raise StopIteration # process individual messages while msgs: # pop raw message from the list raw = msgs.pop(0) # parse message payload try: data = ejson.loads(raw) except (TypeError, ValueError): data = None if not isinstance(data, dict): self.reply( 'error', error=400, reason='Invalid SockJS DDP payload', offendingMessage=raw, ) yield data if msgs: # yield to other greenlets before processing next msg gevent.sleep()
[ "Yield", "DDP", "messages", "from", "a", "raw", "WebSocket", "message", "." ]
jazzband/django-ddp
python
https://github.com/jazzband/django-ddp/blob/1e1954b06fe140346acea43582515991685e4e01/dddp/websocket.py#L198-L231
[ "def", "ddp_frames_from_message", "(", "self", ",", "message", ")", ":", "# parse message set", "try", ":", "msgs", "=", "ejson", ".", "loads", "(", "message", ")", "except", "ValueError", ":", "self", ".", "reply", "(", "'error'", ",", "error", "=", "400", ",", "reason", "=", "'Data is not valid EJSON'", ",", ")", "raise", "StopIteration", "if", "not", "isinstance", "(", "msgs", ",", "list", ")", ":", "self", ".", "reply", "(", "'error'", ",", "error", "=", "400", ",", "reason", "=", "'Invalid EJSON messages'", ",", ")", "raise", "StopIteration", "# process individual messages", "while", "msgs", ":", "# pop raw message from the list", "raw", "=", "msgs", ".", "pop", "(", "0", ")", "# parse message payload", "try", ":", "data", "=", "ejson", ".", "loads", "(", "raw", ")", "except", "(", "TypeError", ",", "ValueError", ")", ":", "data", "=", "None", "if", "not", "isinstance", "(", "data", ",", "dict", ")", ":", "self", ".", "reply", "(", "'error'", ",", "error", "=", "400", ",", "reason", "=", "'Invalid SockJS DDP payload'", ",", "offendingMessage", "=", "raw", ",", ")", "yield", "data", "if", "msgs", ":", "# yield to other greenlets before processing next msg", "gevent", ".", "sleep", "(", ")" ]
1e1954b06fe140346acea43582515991685e4e01
test
DDPWebSocketApplication.process_ddp
Process a single DDP message.
dddp/websocket.py
def process_ddp(self, data): """Process a single DDP message.""" msg_id = data.get('id', None) try: msg = data.pop('msg') except KeyError: self.reply( 'error', reason='Bad request', offendingMessage=data, ) return try: # dispatch message self.dispatch(msg, data) except Exception as err: # pylint: disable=broad-except # This should be the only protocol exception handler kwargs = { 'msg': {'method': 'result'}.get(msg, 'error'), } if msg_id is not None: kwargs['id'] = msg_id if isinstance(err, MeteorError): error = err.as_dict() else: error = { 'error': 500, 'reason': 'Internal server error', } if kwargs['msg'] == 'error': kwargs.update(error) else: kwargs['error'] = error if not isinstance(err, MeteorError): # not a client error, should always be logged. stack, _ = safe_call( self.logger.error, '%r %r', msg, data, exc_info=1, ) if stack is not None: # something went wrong while logging the error, revert to # writing a stack trace to stderr. traceback.print_exc(file=sys.stderr) sys.stderr.write( 'Additionally, while handling the above error the ' 'following error was encountered:\n' ) sys.stderr.write(stack) elif settings.DEBUG: print('ERROR: %s' % err) dprint('msg', msg) dprint('data', data) error.setdefault('details', traceback.format_exc()) # print stack trace for client errors when DEBUG is True. print(error['details']) self.reply(**kwargs) if msg_id and msg == 'method': self.reply('updated', methods=[msg_id])
def process_ddp(self, data): """Process a single DDP message.""" msg_id = data.get('id', None) try: msg = data.pop('msg') except KeyError: self.reply( 'error', reason='Bad request', offendingMessage=data, ) return try: # dispatch message self.dispatch(msg, data) except Exception as err: # pylint: disable=broad-except # This should be the only protocol exception handler kwargs = { 'msg': {'method': 'result'}.get(msg, 'error'), } if msg_id is not None: kwargs['id'] = msg_id if isinstance(err, MeteorError): error = err.as_dict() else: error = { 'error': 500, 'reason': 'Internal server error', } if kwargs['msg'] == 'error': kwargs.update(error) else: kwargs['error'] = error if not isinstance(err, MeteorError): # not a client error, should always be logged. stack, _ = safe_call( self.logger.error, '%r %r', msg, data, exc_info=1, ) if stack is not None: # something went wrong while logging the error, revert to # writing a stack trace to stderr. traceback.print_exc(file=sys.stderr) sys.stderr.write( 'Additionally, while handling the above error the ' 'following error was encountered:\n' ) sys.stderr.write(stack) elif settings.DEBUG: print('ERROR: %s' % err) dprint('msg', msg) dprint('data', data) error.setdefault('details', traceback.format_exc()) # print stack trace for client errors when DEBUG is True. print(error['details']) self.reply(**kwargs) if msg_id and msg == 'method': self.reply('updated', methods=[msg_id])
[ "Process", "a", "single", "DDP", "message", "." ]
jazzband/django-ddp
python
https://github.com/jazzband/django-ddp/blob/1e1954b06fe140346acea43582515991685e4e01/dddp/websocket.py#L233-L288
[ "def", "process_ddp", "(", "self", ",", "data", ")", ":", "msg_id", "=", "data", ".", "get", "(", "'id'", ",", "None", ")", "try", ":", "msg", "=", "data", ".", "pop", "(", "'msg'", ")", "except", "KeyError", ":", "self", ".", "reply", "(", "'error'", ",", "reason", "=", "'Bad request'", ",", "offendingMessage", "=", "data", ",", ")", "return", "try", ":", "# dispatch message", "self", ".", "dispatch", "(", "msg", ",", "data", ")", "except", "Exception", "as", "err", ":", "# pylint: disable=broad-except", "# This should be the only protocol exception handler", "kwargs", "=", "{", "'msg'", ":", "{", "'method'", ":", "'result'", "}", ".", "get", "(", "msg", ",", "'error'", ")", ",", "}", "if", "msg_id", "is", "not", "None", ":", "kwargs", "[", "'id'", "]", "=", "msg_id", "if", "isinstance", "(", "err", ",", "MeteorError", ")", ":", "error", "=", "err", ".", "as_dict", "(", ")", "else", ":", "error", "=", "{", "'error'", ":", "500", ",", "'reason'", ":", "'Internal server error'", ",", "}", "if", "kwargs", "[", "'msg'", "]", "==", "'error'", ":", "kwargs", ".", "update", "(", "error", ")", "else", ":", "kwargs", "[", "'error'", "]", "=", "error", "if", "not", "isinstance", "(", "err", ",", "MeteorError", ")", ":", "# not a client error, should always be logged.", "stack", ",", "_", "=", "safe_call", "(", "self", ".", "logger", ".", "error", ",", "'%r %r'", ",", "msg", ",", "data", ",", "exc_info", "=", "1", ",", ")", "if", "stack", "is", "not", "None", ":", "# something went wrong while logging the error, revert to", "# writing a stack trace to stderr.", "traceback", ".", "print_exc", "(", "file", "=", "sys", ".", "stderr", ")", "sys", ".", "stderr", ".", "write", "(", "'Additionally, while handling the above error the '", "'following error was encountered:\\n'", ")", "sys", ".", "stderr", ".", "write", "(", "stack", ")", "elif", "settings", ".", "DEBUG", ":", "print", "(", "'ERROR: %s'", "%", "err", ")", "dprint", "(", "'msg'", ",", "msg", ")", "dprint", "(", "'data'", ",", "data", ")", "error", ".", "setdefault", "(", "'details'", ",", "traceback", ".", "format_exc", "(", ")", ")", "# print stack trace for client errors when DEBUG is True.", "print", "(", "error", "[", "'details'", "]", ")", "self", ".", "reply", "(", "*", "*", "kwargs", ")", "if", "msg_id", "and", "msg", "==", "'method'", ":", "self", ".", "reply", "(", "'updated'", ",", "methods", "=", "[", "msg_id", "]", ")" ]
1e1954b06fe140346acea43582515991685e4e01
test
DDPWebSocketApplication.dispatch
Dispatch msg to appropriate recv_foo handler.
dddp/websocket.py
def dispatch(self, msg, kwargs): """Dispatch msg to appropriate recv_foo handler.""" # enforce calling 'connect' first if self.connection is None and msg != 'connect': self.reply('error', reason='Must connect first') return if msg == 'method': if ( 'method' not in kwargs ) or ( 'id' not in kwargs ): self.reply( 'error', error=400, reason='Malformed method invocation', ) return # lookup method handler try: handler = getattr(self, 'recv_%s' % msg) except (AttributeError, UnicodeEncodeError): raise MeteorError(404, 'Method not found') # validate handler arguments validate_kwargs(handler, kwargs) # dispatch to handler handler(**kwargs)
def dispatch(self, msg, kwargs): """Dispatch msg to appropriate recv_foo handler.""" # enforce calling 'connect' first if self.connection is None and msg != 'connect': self.reply('error', reason='Must connect first') return if msg == 'method': if ( 'method' not in kwargs ) or ( 'id' not in kwargs ): self.reply( 'error', error=400, reason='Malformed method invocation', ) return # lookup method handler try: handler = getattr(self, 'recv_%s' % msg) except (AttributeError, UnicodeEncodeError): raise MeteorError(404, 'Method not found') # validate handler arguments validate_kwargs(handler, kwargs) # dispatch to handler handler(**kwargs)
[ "Dispatch", "msg", "to", "appropriate", "recv_foo", "handler", "." ]
jazzband/django-ddp
python
https://github.com/jazzband/django-ddp/blob/1e1954b06fe140346acea43582515991685e4e01/dddp/websocket.py#L291-L318
[ "def", "dispatch", "(", "self", ",", "msg", ",", "kwargs", ")", ":", "# enforce calling 'connect' first", "if", "self", ".", "connection", "is", "None", "and", "msg", "!=", "'connect'", ":", "self", ".", "reply", "(", "'error'", ",", "reason", "=", "'Must connect first'", ")", "return", "if", "msg", "==", "'method'", ":", "if", "(", "'method'", "not", "in", "kwargs", ")", "or", "(", "'id'", "not", "in", "kwargs", ")", ":", "self", ".", "reply", "(", "'error'", ",", "error", "=", "400", ",", "reason", "=", "'Malformed method invocation'", ",", ")", "return", "# lookup method handler", "try", ":", "handler", "=", "getattr", "(", "self", ",", "'recv_%s'", "%", "msg", ")", "except", "(", "AttributeError", ",", "UnicodeEncodeError", ")", ":", "raise", "MeteorError", "(", "404", ",", "'Method not found'", ")", "# validate handler arguments", "validate_kwargs", "(", "handler", ",", "kwargs", ")", "# dispatch to handler", "handler", "(", "*", "*", "kwargs", ")" ]
1e1954b06fe140346acea43582515991685e4e01
test
DDPWebSocketApplication.send
Send `data` (raw string or EJSON payload) to WebSocket client.
dddp/websocket.py
def send(self, data, tx_id=None): """Send `data` (raw string or EJSON payload) to WebSocket client.""" # buffer data until we get pre-requisite data if tx_id is None: tx_id = self.get_tx_id() self._tx_buffer[tx_id] = data # de-queue messages from buffer while self._tx_next_id in self._tx_buffer: # pull next message from buffer data = self._tx_buffer.pop(self._tx_next_id) if self._tx_buffer: safe_call(self.logger.debug, 'TX found %d', self._tx_next_id) # advance next message ID self._tx_next_id = next(self._tx_next_id_gen) if not isinstance(data, basestring): # ejson payload msg = data.get('msg', None) if msg in (ADDED, CHANGED, REMOVED): ids = self.remote_ids[data['collection']] meteor_id = data['id'] if msg == ADDED: if meteor_id in ids: msg = data['msg'] = CHANGED else: ids.add(meteor_id) elif msg == CHANGED: if meteor_id not in ids: # object has become visible, treat as `added`. msg = data['msg'] = ADDED ids.add(meteor_id) elif msg == REMOVED: try: ids.remove(meteor_id) except KeyError: continue # client doesn't have this, don't send. data = 'a%s' % ejson.dumps([ejson.dumps(data)]) # send message safe_call(self.logger.debug, '> %s %r', self, data) try: self.ws.send(data) except geventwebsocket.WebSocketError: self.ws.close() self._tx_buffer.clear() break num_waiting = len(self._tx_buffer) if num_waiting > 10: safe_call( self.logger.warn, 'TX received %d, waiting for %d, have %d waiting: %r.', tx_id, self._tx_next_id, num_waiting, self._tx_buffer, )
def send(self, data, tx_id=None): """Send `data` (raw string or EJSON payload) to WebSocket client.""" # buffer data until we get pre-requisite data if tx_id is None: tx_id = self.get_tx_id() self._tx_buffer[tx_id] = data # de-queue messages from buffer while self._tx_next_id in self._tx_buffer: # pull next message from buffer data = self._tx_buffer.pop(self._tx_next_id) if self._tx_buffer: safe_call(self.logger.debug, 'TX found %d', self._tx_next_id) # advance next message ID self._tx_next_id = next(self._tx_next_id_gen) if not isinstance(data, basestring): # ejson payload msg = data.get('msg', None) if msg in (ADDED, CHANGED, REMOVED): ids = self.remote_ids[data['collection']] meteor_id = data['id'] if msg == ADDED: if meteor_id in ids: msg = data['msg'] = CHANGED else: ids.add(meteor_id) elif msg == CHANGED: if meteor_id not in ids: # object has become visible, treat as `added`. msg = data['msg'] = ADDED ids.add(meteor_id) elif msg == REMOVED: try: ids.remove(meteor_id) except KeyError: continue # client doesn't have this, don't send. data = 'a%s' % ejson.dumps([ejson.dumps(data)]) # send message safe_call(self.logger.debug, '> %s %r', self, data) try: self.ws.send(data) except geventwebsocket.WebSocketError: self.ws.close() self._tx_buffer.clear() break num_waiting = len(self._tx_buffer) if num_waiting > 10: safe_call( self.logger.warn, 'TX received %d, waiting for %d, have %d waiting: %r.', tx_id, self._tx_next_id, num_waiting, self._tx_buffer, )
[ "Send", "data", "(", "raw", "string", "or", "EJSON", "payload", ")", "to", "WebSocket", "client", "." ]
jazzband/django-ddp
python
https://github.com/jazzband/django-ddp/blob/1e1954b06fe140346acea43582515991685e4e01/dddp/websocket.py#L320-L371
[ "def", "send", "(", "self", ",", "data", ",", "tx_id", "=", "None", ")", ":", "# buffer data until we get pre-requisite data", "if", "tx_id", "is", "None", ":", "tx_id", "=", "self", ".", "get_tx_id", "(", ")", "self", ".", "_tx_buffer", "[", "tx_id", "]", "=", "data", "# de-queue messages from buffer", "while", "self", ".", "_tx_next_id", "in", "self", ".", "_tx_buffer", ":", "# pull next message from buffer", "data", "=", "self", ".", "_tx_buffer", ".", "pop", "(", "self", ".", "_tx_next_id", ")", "if", "self", ".", "_tx_buffer", ":", "safe_call", "(", "self", ".", "logger", ".", "debug", ",", "'TX found %d'", ",", "self", ".", "_tx_next_id", ")", "# advance next message ID", "self", ".", "_tx_next_id", "=", "next", "(", "self", ".", "_tx_next_id_gen", ")", "if", "not", "isinstance", "(", "data", ",", "basestring", ")", ":", "# ejson payload", "msg", "=", "data", ".", "get", "(", "'msg'", ",", "None", ")", "if", "msg", "in", "(", "ADDED", ",", "CHANGED", ",", "REMOVED", ")", ":", "ids", "=", "self", ".", "remote_ids", "[", "data", "[", "'collection'", "]", "]", "meteor_id", "=", "data", "[", "'id'", "]", "if", "msg", "==", "ADDED", ":", "if", "meteor_id", "in", "ids", ":", "msg", "=", "data", "[", "'msg'", "]", "=", "CHANGED", "else", ":", "ids", ".", "add", "(", "meteor_id", ")", "elif", "msg", "==", "CHANGED", ":", "if", "meteor_id", "not", "in", "ids", ":", "# object has become visible, treat as `added`.", "msg", "=", "data", "[", "'msg'", "]", "=", "ADDED", "ids", ".", "add", "(", "meteor_id", ")", "elif", "msg", "==", "REMOVED", ":", "try", ":", "ids", ".", "remove", "(", "meteor_id", ")", "except", "KeyError", ":", "continue", "# client doesn't have this, don't send.", "data", "=", "'a%s'", "%", "ejson", ".", "dumps", "(", "[", "ejson", ".", "dumps", "(", "data", ")", "]", ")", "# send message", "safe_call", "(", "self", ".", "logger", ".", "debug", ",", "'> %s %r'", ",", "self", ",", "data", ")", "try", ":", "self", ".", "ws", ".", "send", "(", "data", ")", "except", "geventwebsocket", ".", "WebSocketError", ":", "self", ".", "ws", ".", "close", "(", ")", "self", ".", "_tx_buffer", ".", "clear", "(", ")", "break", "num_waiting", "=", "len", "(", "self", ".", "_tx_buffer", ")", "if", "num_waiting", ">", "10", ":", "safe_call", "(", "self", ".", "logger", ".", "warn", ",", "'TX received %d, waiting for %d, have %d waiting: %r.'", ",", "tx_id", ",", "self", ".", "_tx_next_id", ",", "num_waiting", ",", "self", ".", "_tx_buffer", ",", ")" ]
1e1954b06fe140346acea43582515991685e4e01
test
DDPWebSocketApplication.recv_connect
DDP connect handler.
dddp/websocket.py
def recv_connect(self, version=None, support=None, session=None): """DDP connect handler.""" del session # Meteor doesn't even use this! if self.connection is not None: raise MeteorError( 400, 'Session already established.', self.connection.connection_id, ) elif None in (version, support) or version not in self.versions: self.reply('failed', version=self.versions[0]) elif version not in support: raise MeteorError(400, 'Client version/support mismatch.') else: from dddp.models import Connection cur = connection.cursor() cur.execute('SELECT pg_backend_pid()') (backend_pid,) = cur.fetchone() this.version = version this.support = support self.connection = Connection.objects.create( server_addr='%d:%s' % ( backend_pid, self.ws.handler.socket.getsockname(), ), remote_addr=self.remote_addr, version=version, ) self.pgworker.connections[self.connection.pk] = self atexit.register(self.on_close, 'Shutting down.') self.reply('connected', session=self.connection.connection_id)
def recv_connect(self, version=None, support=None, session=None): """DDP connect handler.""" del session # Meteor doesn't even use this! if self.connection is not None: raise MeteorError( 400, 'Session already established.', self.connection.connection_id, ) elif None in (version, support) or version not in self.versions: self.reply('failed', version=self.versions[0]) elif version not in support: raise MeteorError(400, 'Client version/support mismatch.') else: from dddp.models import Connection cur = connection.cursor() cur.execute('SELECT pg_backend_pid()') (backend_pid,) = cur.fetchone() this.version = version this.support = support self.connection = Connection.objects.create( server_addr='%d:%s' % ( backend_pid, self.ws.handler.socket.getsockname(), ), remote_addr=self.remote_addr, version=version, ) self.pgworker.connections[self.connection.pk] = self atexit.register(self.on_close, 'Shutting down.') self.reply('connected', session=self.connection.connection_id)
[ "DDP", "connect", "handler", "." ]
jazzband/django-ddp
python
https://github.com/jazzband/django-ddp/blob/1e1954b06fe140346acea43582515991685e4e01/dddp/websocket.py#L378-L407
[ "def", "recv_connect", "(", "self", ",", "version", "=", "None", ",", "support", "=", "None", ",", "session", "=", "None", ")", ":", "del", "session", "# Meteor doesn't even use this!", "if", "self", ".", "connection", "is", "not", "None", ":", "raise", "MeteorError", "(", "400", ",", "'Session already established.'", ",", "self", ".", "connection", ".", "connection_id", ",", ")", "elif", "None", "in", "(", "version", ",", "support", ")", "or", "version", "not", "in", "self", ".", "versions", ":", "self", ".", "reply", "(", "'failed'", ",", "version", "=", "self", ".", "versions", "[", "0", "]", ")", "elif", "version", "not", "in", "support", ":", "raise", "MeteorError", "(", "400", ",", "'Client version/support mismatch.'", ")", "else", ":", "from", "dddp", ".", "models", "import", "Connection", "cur", "=", "connection", ".", "cursor", "(", ")", "cur", ".", "execute", "(", "'SELECT pg_backend_pid()'", ")", "(", "backend_pid", ",", ")", "=", "cur", ".", "fetchone", "(", ")", "this", ".", "version", "=", "version", "this", ".", "support", "=", "support", "self", ".", "connection", "=", "Connection", ".", "objects", ".", "create", "(", "server_addr", "=", "'%d:%s'", "%", "(", "backend_pid", ",", "self", ".", "ws", ".", "handler", ".", "socket", ".", "getsockname", "(", ")", ",", ")", ",", "remote_addr", "=", "self", ".", "remote_addr", ",", "version", "=", "version", ",", ")", "self", ".", "pgworker", ".", "connections", "[", "self", ".", "connection", ".", "pk", "]", "=", "self", "atexit", ".", "register", "(", "self", ".", "on_close", ",", "'Shutting down.'", ")", "self", ".", "reply", "(", "'connected'", ",", "session", "=", "self", ".", "connection", ".", "connection_id", ")" ]
1e1954b06fe140346acea43582515991685e4e01
test
DDPWebSocketApplication.recv_ping
DDP ping handler.
dddp/websocket.py
def recv_ping(self, id_=None): """DDP ping handler.""" if id_ is None: self.reply('pong') else: self.reply('pong', id=id_)
def recv_ping(self, id_=None): """DDP ping handler.""" if id_ is None: self.reply('pong') else: self.reply('pong', id=id_)
[ "DDP", "ping", "handler", "." ]
jazzband/django-ddp
python
https://github.com/jazzband/django-ddp/blob/1e1954b06fe140346acea43582515991685e4e01/dddp/websocket.py#L410-L415
[ "def", "recv_ping", "(", "self", ",", "id_", "=", "None", ")", ":", "if", "id_", "is", "None", ":", "self", ".", "reply", "(", "'pong'", ")", "else", ":", "self", ".", "reply", "(", "'pong'", ",", "id", "=", "id_", ")" ]
1e1954b06fe140346acea43582515991685e4e01
test
DDPWebSocketApplication.recv_sub
DDP sub handler.
dddp/websocket.py
def recv_sub(self, id_, name, params): """DDP sub handler.""" self.api.sub(id_, name, *params)
def recv_sub(self, id_, name, params): """DDP sub handler.""" self.api.sub(id_, name, *params)
[ "DDP", "sub", "handler", "." ]
jazzband/django-ddp
python
https://github.com/jazzband/django-ddp/blob/1e1954b06fe140346acea43582515991685e4e01/dddp/websocket.py#L418-L420
[ "def", "recv_sub", "(", "self", ",", "id_", ",", "name", ",", "params", ")", ":", "self", ".", "api", ".", "sub", "(", "id_", ",", "name", ",", "*", "params", ")" ]
1e1954b06fe140346acea43582515991685e4e01
test
DDPWebSocketApplication.recv_unsub
DDP unsub handler.
dddp/websocket.py
def recv_unsub(self, id_=None): """DDP unsub handler.""" if id_: self.api.unsub(id_) else: self.reply('nosub')
def recv_unsub(self, id_=None): """DDP unsub handler.""" if id_: self.api.unsub(id_) else: self.reply('nosub')
[ "DDP", "unsub", "handler", "." ]
jazzband/django-ddp
python
https://github.com/jazzband/django-ddp/blob/1e1954b06fe140346acea43582515991685e4e01/dddp/websocket.py#L423-L428
[ "def", "recv_unsub", "(", "self", ",", "id_", "=", "None", ")", ":", "if", "id_", ":", "self", ".", "api", ".", "unsub", "(", "id_", ")", "else", ":", "self", ".", "reply", "(", "'nosub'", ")" ]
1e1954b06fe140346acea43582515991685e4e01
test
DDPWebSocketApplication.recv_method
DDP method handler.
dddp/websocket.py
def recv_method(self, method, params, id_, randomSeed=None): """DDP method handler.""" if randomSeed is not None: this.random_streams.random_seed = randomSeed this.alea_random = alea.Alea(randomSeed) self.api.method(method, params, id_) self.reply('updated', methods=[id_])
def recv_method(self, method, params, id_, randomSeed=None): """DDP method handler.""" if randomSeed is not None: this.random_streams.random_seed = randomSeed this.alea_random = alea.Alea(randomSeed) self.api.method(method, params, id_) self.reply('updated', methods=[id_])
[ "DDP", "method", "handler", "." ]
jazzband/django-ddp
python
https://github.com/jazzband/django-ddp/blob/1e1954b06fe140346acea43582515991685e4e01/dddp/websocket.py#L431-L437
[ "def", "recv_method", "(", "self", ",", "method", ",", "params", ",", "id_", ",", "randomSeed", "=", "None", ")", ":", "if", "randomSeed", "is", "not", "None", ":", "this", ".", "random_streams", ".", "random_seed", "=", "randomSeed", "this", ".", "alea_random", "=", "alea", ".", "Alea", "(", "randomSeed", ")", "self", ".", "api", ".", "method", "(", "method", ",", "params", ",", "id_", ")", "self", ".", "reply", "(", "'updated'", ",", "methods", "=", "[", "id_", "]", ")" ]
1e1954b06fe140346acea43582515991685e4e01
test
ddpp_sockjs_info
Inform client that WebSocket service is available.
dddp/main.py
def ddpp_sockjs_info(environ, start_response): """Inform client that WebSocket service is available.""" import random import ejson start_response( '200 OK', [ ('Content-Type', 'application/json; charset=UTF-8'), ] + common_headers(environ), ) yield ejson.dumps(collections.OrderedDict([ ('websocket', True), ('origins', [ '*:*', ]), ('cookie_needed', False), ('entropy', random.getrandbits(32)), ]))
def ddpp_sockjs_info(environ, start_response): """Inform client that WebSocket service is available.""" import random import ejson start_response( '200 OK', [ ('Content-Type', 'application/json; charset=UTF-8'), ] + common_headers(environ), ) yield ejson.dumps(collections.OrderedDict([ ('websocket', True), ('origins', [ '*:*', ]), ('cookie_needed', False), ('entropy', random.getrandbits(32)), ]))
[ "Inform", "client", "that", "WebSocket", "service", "is", "available", "." ]
jazzband/django-ddp
python
https://github.com/jazzband/django-ddp/blob/1e1954b06fe140346acea43582515991685e4e01/dddp/main.py#L48-L66
[ "def", "ddpp_sockjs_info", "(", "environ", ",", "start_response", ")", ":", "import", "random", "import", "ejson", "start_response", "(", "'200 OK'", ",", "[", "(", "'Content-Type'", ",", "'application/json; charset=UTF-8'", ")", ",", "]", "+", "common_headers", "(", "environ", ")", ",", ")", "yield", "ejson", ".", "dumps", "(", "collections", ".", "OrderedDict", "(", "[", "(", "'websocket'", ",", "True", ")", ",", "(", "'origins'", ",", "[", "'*:*'", ",", "]", ")", ",", "(", "'cookie_needed'", ",", "False", ")", ",", "(", "'entropy'", ",", "random", ".", "getrandbits", "(", "32", ")", ")", ",", "]", ")", ")" ]
1e1954b06fe140346acea43582515991685e4e01
test
addr
Convert a string of format host[:port] into Addr(host, port). >>> addr('0:80') Addr(host='0', port=80) >>> addr('127.0.0.1:80') Addr(host='127.0.0.1', port=80) >>> addr('0.0.0.0', default_port=8000) Addr(host='0.0.0.0', port=8000)
dddp/main.py
def addr(val, default_port=8000, defualt_host='localhost'): """ Convert a string of format host[:port] into Addr(host, port). >>> addr('0:80') Addr(host='0', port=80) >>> addr('127.0.0.1:80') Addr(host='127.0.0.1', port=80) >>> addr('0.0.0.0', default_port=8000) Addr(host='0.0.0.0', port=8000) """ import re import socket match = re.match(r'\A(?P<host>.*?)(:(?P<port>(\d+|\w+)))?\Z', val) if match is None: raise argparse.ArgumentTypeError( '%r is not a valid host[:port] address.' % val ) host, port = match.group('host', 'port') if not host: host = defualt_host if not port: port = default_port elif port.isdigit(): port = int(port) else: port = socket.getservbyname(port) return Addr(host, port)
def addr(val, default_port=8000, defualt_host='localhost'): """ Convert a string of format host[:port] into Addr(host, port). >>> addr('0:80') Addr(host='0', port=80) >>> addr('127.0.0.1:80') Addr(host='127.0.0.1', port=80) >>> addr('0.0.0.0', default_port=8000) Addr(host='0.0.0.0', port=8000) """ import re import socket match = re.match(r'\A(?P<host>.*?)(:(?P<port>(\d+|\w+)))?\Z', val) if match is None: raise argparse.ArgumentTypeError( '%r is not a valid host[:port] address.' % val ) host, port = match.group('host', 'port') if not host: host = defualt_host if not port: port = default_port elif port.isdigit(): port = int(port) else: port = socket.getservbyname(port) return Addr(host, port)
[ "Convert", "a", "string", "of", "format", "host", "[", ":", "port", "]", "into", "Addr", "(", "host", "port", ")", "." ]
jazzband/django-ddp
python
https://github.com/jazzband/django-ddp/blob/1e1954b06fe140346acea43582515991685e4e01/dddp/main.py#L241-L274
[ "def", "addr", "(", "val", ",", "default_port", "=", "8000", ",", "defualt_host", "=", "'localhost'", ")", ":", "import", "re", "import", "socket", "match", "=", "re", ".", "match", "(", "r'\\A(?P<host>.*?)(:(?P<port>(\\d+|\\w+)))?\\Z'", ",", "val", ")", "if", "match", "is", "None", ":", "raise", "argparse", ".", "ArgumentTypeError", "(", "'%r is not a valid host[:port] address.'", "%", "val", ")", "host", ",", "port", "=", "match", ".", "group", "(", "'host'", ",", "'port'", ")", "if", "not", "host", ":", "host", "=", "defualt_host", "if", "not", "port", ":", "port", "=", "default_port", "elif", "port", ".", "isdigit", "(", ")", ":", "port", "=", "int", "(", "port", ")", "else", ":", "port", "=", "socket", ".", "getservbyname", "(", "port", ")", "return", "Addr", "(", "host", ",", "port", ")" ]
1e1954b06fe140346acea43582515991685e4e01
test
serve
Spawn greenlets for handling websockets and PostgreSQL calls.
dddp/main.py
def serve(listen, verbosity=1, debug_port=0, **ssl_args): """Spawn greenlets for handling websockets and PostgreSQL calls.""" launcher = DDPLauncher(debug=verbosity == 3, verbosity=verbosity) if debug_port: launcher.servers.append( launcher.get_backdoor_server('localhost:%d' % debug_port) ) launcher.add_web_servers(listen, **ssl_args) # die gracefully with SIGINT or SIGQUIT sigmap = { val: name for name, val in vars(signal).items() if name.startswith('SIG') } def sighandler(signum=None, frame=None): """Signal handler""" launcher.logger.info( 'Received signal %s in frame %r', sigmap.get(signum, signum), frame, ) launcher.stop() for signum in [signal.SIGINT, signal.SIGQUIT]: gevent.signal(signum, sighandler) launcher.run()
def serve(listen, verbosity=1, debug_port=0, **ssl_args): """Spawn greenlets for handling websockets and PostgreSQL calls.""" launcher = DDPLauncher(debug=verbosity == 3, verbosity=verbosity) if debug_port: launcher.servers.append( launcher.get_backdoor_server('localhost:%d' % debug_port) ) launcher.add_web_servers(listen, **ssl_args) # die gracefully with SIGINT or SIGQUIT sigmap = { val: name for name, val in vars(signal).items() if name.startswith('SIG') } def sighandler(signum=None, frame=None): """Signal handler""" launcher.logger.info( 'Received signal %s in frame %r', sigmap.get(signum, signum), frame, ) launcher.stop() for signum in [signal.SIGINT, signal.SIGQUIT]: gevent.signal(signum, sighandler) launcher.run()
[ "Spawn", "greenlets", "for", "handling", "websockets", "and", "PostgreSQL", "calls", "." ]
jazzband/django-ddp
python
https://github.com/jazzband/django-ddp/blob/1e1954b06fe140346acea43582515991685e4e01/dddp/main.py#L277-L303
[ "def", "serve", "(", "listen", ",", "verbosity", "=", "1", ",", "debug_port", "=", "0", ",", "*", "*", "ssl_args", ")", ":", "launcher", "=", "DDPLauncher", "(", "debug", "=", "verbosity", "==", "3", ",", "verbosity", "=", "verbosity", ")", "if", "debug_port", ":", "launcher", ".", "servers", ".", "append", "(", "launcher", ".", "get_backdoor_server", "(", "'localhost:%d'", "%", "debug_port", ")", ")", "launcher", ".", "add_web_servers", "(", "listen", ",", "*", "*", "ssl_args", ")", "# die gracefully with SIGINT or SIGQUIT", "sigmap", "=", "{", "val", ":", "name", "for", "name", ",", "val", "in", "vars", "(", "signal", ")", ".", "items", "(", ")", "if", "name", ".", "startswith", "(", "'SIG'", ")", "}", "def", "sighandler", "(", "signum", "=", "None", ",", "frame", "=", "None", ")", ":", "\"\"\"Signal handler\"\"\"", "launcher", ".", "logger", ".", "info", "(", "'Received signal %s in frame %r'", ",", "sigmap", ".", "get", "(", "signum", ",", "signum", ")", ",", "frame", ",", ")", "launcher", ".", "stop", "(", ")", "for", "signum", "in", "[", "signal", ".", "SIGINT", ",", "signal", ".", "SIGQUIT", "]", ":", "gevent", ".", "signal", "(", "signum", ",", "sighandler", ")", "launcher", ".", "run", "(", ")" ]
1e1954b06fe140346acea43582515991685e4e01
test
main
Main entry point for `dddp` command.
dddp/main.py
def main(): """Main entry point for `dddp` command.""" parser = argparse.ArgumentParser(description=__doc__) django = parser.add_argument_group('Django Options') django.add_argument( '--verbosity', '-v', metavar='VERBOSITY', dest='verbosity', type=int, default=1, ) django.add_argument( '--debug-port', metavar='DEBUG_PORT', dest='debug_port', type=int, default=0, ) django.add_argument( '--settings', metavar='SETTINGS', dest='settings', help="The Python path to a settings module, e.g. " "\"myproject.settings.main\". If this isn't provided, the " "DJANGO_SETTINGS_MODULE environment variable will be used.", ) http = parser.add_argument_group('HTTP Options') http.add_argument( 'listen', metavar='address[:port]', nargs='*', type=addr, help='Listening address for HTTP(s) server.', ) ssl = parser.add_argument_group('SSL Options') ssl.add_argument('--ssl-version', metavar='SSL_VERSION', dest='ssl_version', help="SSL version to use (see stdlib ssl module's) [3]", choices=['1', '2', '3'], default='3') ssl.add_argument('--certfile', metavar='FILE', dest='certfile', help="SSL certificate file [None]") ssl.add_argument('--ciphers', metavar='CIPHERS', dest='ciphers', help="Ciphers to use (see stdlib ssl module's) [TLSv1]") ssl.add_argument('--ca-certs', metavar='FILE', dest='ca_certs', help="CA certificates file [None]") ssl.add_argument('--keyfile', metavar='FILE', dest='keyfile', help="SSL key file [None]") namespace = parser.parse_args() if namespace.settings: os.environ['DJANGO_SETTINGS_MODULE'] = namespace.settings serve( namespace.listen or [Addr('localhost', 8000)], debug_port=namespace.debug_port, keyfile=namespace.keyfile, certfile=namespace.certfile, verbosity=namespace.verbosity, )
def main(): """Main entry point for `dddp` command.""" parser = argparse.ArgumentParser(description=__doc__) django = parser.add_argument_group('Django Options') django.add_argument( '--verbosity', '-v', metavar='VERBOSITY', dest='verbosity', type=int, default=1, ) django.add_argument( '--debug-port', metavar='DEBUG_PORT', dest='debug_port', type=int, default=0, ) django.add_argument( '--settings', metavar='SETTINGS', dest='settings', help="The Python path to a settings module, e.g. " "\"myproject.settings.main\". If this isn't provided, the " "DJANGO_SETTINGS_MODULE environment variable will be used.", ) http = parser.add_argument_group('HTTP Options') http.add_argument( 'listen', metavar='address[:port]', nargs='*', type=addr, help='Listening address for HTTP(s) server.', ) ssl = parser.add_argument_group('SSL Options') ssl.add_argument('--ssl-version', metavar='SSL_VERSION', dest='ssl_version', help="SSL version to use (see stdlib ssl module's) [3]", choices=['1', '2', '3'], default='3') ssl.add_argument('--certfile', metavar='FILE', dest='certfile', help="SSL certificate file [None]") ssl.add_argument('--ciphers', metavar='CIPHERS', dest='ciphers', help="Ciphers to use (see stdlib ssl module's) [TLSv1]") ssl.add_argument('--ca-certs', metavar='FILE', dest='ca_certs', help="CA certificates file [None]") ssl.add_argument('--keyfile', metavar='FILE', dest='keyfile', help="SSL key file [None]") namespace = parser.parse_args() if namespace.settings: os.environ['DJANGO_SETTINGS_MODULE'] = namespace.settings serve( namespace.listen or [Addr('localhost', 8000)], debug_port=namespace.debug_port, keyfile=namespace.keyfile, certfile=namespace.certfile, verbosity=namespace.verbosity, )
[ "Main", "entry", "point", "for", "dddp", "command", "." ]
jazzband/django-ddp
python
https://github.com/jazzband/django-ddp/blob/1e1954b06fe140346acea43582515991685e4e01/dddp/main.py#L306-L350
[ "def", "main", "(", ")", ":", "parser", "=", "argparse", ".", "ArgumentParser", "(", "description", "=", "__doc__", ")", "django", "=", "parser", ".", "add_argument_group", "(", "'Django Options'", ")", "django", ".", "add_argument", "(", "'--verbosity'", ",", "'-v'", ",", "metavar", "=", "'VERBOSITY'", ",", "dest", "=", "'verbosity'", ",", "type", "=", "int", ",", "default", "=", "1", ",", ")", "django", ".", "add_argument", "(", "'--debug-port'", ",", "metavar", "=", "'DEBUG_PORT'", ",", "dest", "=", "'debug_port'", ",", "type", "=", "int", ",", "default", "=", "0", ",", ")", "django", ".", "add_argument", "(", "'--settings'", ",", "metavar", "=", "'SETTINGS'", ",", "dest", "=", "'settings'", ",", "help", "=", "\"The Python path to a settings module, e.g. \"", "\"\\\"myproject.settings.main\\\". If this isn't provided, the \"", "\"DJANGO_SETTINGS_MODULE environment variable will be used.\"", ",", ")", "http", "=", "parser", ".", "add_argument_group", "(", "'HTTP Options'", ")", "http", ".", "add_argument", "(", "'listen'", ",", "metavar", "=", "'address[:port]'", ",", "nargs", "=", "'*'", ",", "type", "=", "addr", ",", "help", "=", "'Listening address for HTTP(s) server.'", ",", ")", "ssl", "=", "parser", ".", "add_argument_group", "(", "'SSL Options'", ")", "ssl", ".", "add_argument", "(", "'--ssl-version'", ",", "metavar", "=", "'SSL_VERSION'", ",", "dest", "=", "'ssl_version'", ",", "help", "=", "\"SSL version to use (see stdlib ssl module's) [3]\"", ",", "choices", "=", "[", "'1'", ",", "'2'", ",", "'3'", "]", ",", "default", "=", "'3'", ")", "ssl", ".", "add_argument", "(", "'--certfile'", ",", "metavar", "=", "'FILE'", ",", "dest", "=", "'certfile'", ",", "help", "=", "\"SSL certificate file [None]\"", ")", "ssl", ".", "add_argument", "(", "'--ciphers'", ",", "metavar", "=", "'CIPHERS'", ",", "dest", "=", "'ciphers'", ",", "help", "=", "\"Ciphers to use (see stdlib ssl module's) [TLSv1]\"", ")", "ssl", ".", "add_argument", "(", "'--ca-certs'", ",", "metavar", "=", "'FILE'", ",", "dest", "=", "'ca_certs'", ",", "help", "=", "\"CA certificates file [None]\"", ")", "ssl", ".", "add_argument", "(", "'--keyfile'", ",", "metavar", "=", "'FILE'", ",", "dest", "=", "'keyfile'", ",", "help", "=", "\"SSL key file [None]\"", ")", "namespace", "=", "parser", ".", "parse_args", "(", ")", "if", "namespace", ".", "settings", ":", "os", ".", "environ", "[", "'DJANGO_SETTINGS_MODULE'", "]", "=", "namespace", ".", "settings", "serve", "(", "namespace", ".", "listen", "or", "[", "Addr", "(", "'localhost'", ",", "8000", ")", "]", ",", "debug_port", "=", "namespace", ".", "debug_port", ",", "keyfile", "=", "namespace", ".", "keyfile", ",", "certfile", "=", "namespace", ".", "certfile", ",", "verbosity", "=", "namespace", ".", "verbosity", ",", ")" ]
1e1954b06fe140346acea43582515991685e4e01
test
DDPLauncher.print
Print formatted msg if verbosity set at 1 or above.
dddp/main.py
def print(self, msg, *args, **kwargs): """Print formatted msg if verbosity set at 1 or above.""" if self.verbosity >= 1: print(msg, *args, **kwargs)
def print(self, msg, *args, **kwargs): """Print formatted msg if verbosity set at 1 or above.""" if self.verbosity >= 1: print(msg, *args, **kwargs)
[ "Print", "formatted", "msg", "if", "verbosity", "set", "at", "1", "or", "above", "." ]
jazzband/django-ddp
python
https://github.com/jazzband/django-ddp/blob/1e1954b06fe140346acea43582515991685e4e01/dddp/main.py#L142-L145
[ "def", "print", "(", "self", ",", "msg", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "if", "self", ".", "verbosity", ">=", "1", ":", "print", "(", "msg", ",", "*", "args", ",", "*", "*", "kwargs", ")" ]
1e1954b06fe140346acea43582515991685e4e01
test
DDPLauncher.add_web_servers
Add WebSocketServer for each (host, port) in listen_addrs.
dddp/main.py
def add_web_servers(self, listen_addrs, debug=False, **ssl_args): """Add WebSocketServer for each (host, port) in listen_addrs.""" self.servers.extend( self.get_web_server(listen_addr, debug=debug, **ssl_args) for listen_addr in listen_addrs )
def add_web_servers(self, listen_addrs, debug=False, **ssl_args): """Add WebSocketServer for each (host, port) in listen_addrs.""" self.servers.extend( self.get_web_server(listen_addr, debug=debug, **ssl_args) for listen_addr in listen_addrs )
[ "Add", "WebSocketServer", "for", "each", "(", "host", "port", ")", "in", "listen_addrs", "." ]
jazzband/django-ddp
python
https://github.com/jazzband/django-ddp/blob/1e1954b06fe140346acea43582515991685e4e01/dddp/main.py#L147-L152
[ "def", "add_web_servers", "(", "self", ",", "listen_addrs", ",", "debug", "=", "False", ",", "*", "*", "ssl_args", ")", ":", "self", ".", "servers", ".", "extend", "(", "self", ".", "get_web_server", "(", "listen_addr", ",", "debug", "=", "debug", ",", "*", "*", "ssl_args", ")", "for", "listen_addr", "in", "listen_addrs", ")" ]
1e1954b06fe140346acea43582515991685e4e01
test
DDPLauncher.get_web_server
Setup WebSocketServer on listen_addr (host, port).
dddp/main.py
def get_web_server(self, listen_addr, debug=False, **ssl_args): """Setup WebSocketServer on listen_addr (host, port).""" return geventwebsocket.WebSocketServer( listen_addr, self.resource, debug=debug, **{key: val for key, val in ssl_args.items() if val is not None} )
def get_web_server(self, listen_addr, debug=False, **ssl_args): """Setup WebSocketServer on listen_addr (host, port).""" return geventwebsocket.WebSocketServer( listen_addr, self.resource, debug=debug, **{key: val for key, val in ssl_args.items() if val is not None} )
[ "Setup", "WebSocketServer", "on", "listen_addr", "(", "host", "port", ")", "." ]
jazzband/django-ddp
python
https://github.com/jazzband/django-ddp/blob/1e1954b06fe140346acea43582515991685e4e01/dddp/main.py#L154-L161
[ "def", "get_web_server", "(", "self", ",", "listen_addr", ",", "debug", "=", "False", ",", "*", "*", "ssl_args", ")", ":", "return", "geventwebsocket", ".", "WebSocketServer", "(", "listen_addr", ",", "self", ".", "resource", ",", "debug", "=", "debug", ",", "*", "*", "{", "key", ":", "val", "for", "key", ",", "val", "in", "ssl_args", ".", "items", "(", ")", "if", "val", "is", "not", "None", "}", ")" ]
1e1954b06fe140346acea43582515991685e4e01
test
DDPLauncher.get_backdoor_server
Add a backdoor (debug) server.
dddp/main.py
def get_backdoor_server(self, listen_addr, **context): """Add a backdoor (debug) server.""" from django.conf import settings local_vars = { 'launcher': self, 'servers': self.servers, 'pgworker': self.pgworker, 'stop': self.stop, 'api': self.api, 'resource': self.resource, 'settings': settings, 'wsgi_app': self.wsgi_app, 'wsgi_name': self.wsgi_name, } local_vars.update(context) return BackdoorServer( listen_addr, banner='Django DDP', locals=local_vars, )
def get_backdoor_server(self, listen_addr, **context): """Add a backdoor (debug) server.""" from django.conf import settings local_vars = { 'launcher': self, 'servers': self.servers, 'pgworker': self.pgworker, 'stop': self.stop, 'api': self.api, 'resource': self.resource, 'settings': settings, 'wsgi_app': self.wsgi_app, 'wsgi_name': self.wsgi_name, } local_vars.update(context) return BackdoorServer( listen_addr, banner='Django DDP', locals=local_vars, )
[ "Add", "a", "backdoor", "(", "debug", ")", "server", "." ]
jazzband/django-ddp
python
https://github.com/jazzband/django-ddp/blob/1e1954b06fe140346acea43582515991685e4e01/dddp/main.py#L163-L180
[ "def", "get_backdoor_server", "(", "self", ",", "listen_addr", ",", "*", "*", "context", ")", ":", "from", "django", ".", "conf", "import", "settings", "local_vars", "=", "{", "'launcher'", ":", "self", ",", "'servers'", ":", "self", ".", "servers", ",", "'pgworker'", ":", "self", ".", "pgworker", ",", "'stop'", ":", "self", ".", "stop", ",", "'api'", ":", "self", ".", "api", ",", "'resource'", ":", "self", ".", "resource", ",", "'settings'", ":", "settings", ",", "'wsgi_app'", ":", "self", ".", "wsgi_app", ",", "'wsgi_name'", ":", "self", ".", "wsgi_name", ",", "}", "local_vars", ".", "update", "(", "context", ")", "return", "BackdoorServer", "(", "listen_addr", ",", "banner", "=", "'Django DDP'", ",", "locals", "=", "local_vars", ",", ")" ]
1e1954b06fe140346acea43582515991685e4e01
test
DDPLauncher.stop
Stop all green threads.
dddp/main.py
def stop(self): """Stop all green threads.""" self.logger.debug('PostgresGreenlet stop') self._stop_event.set() # ask all threads to stop. for server in self.servers + [DDPLauncher.pgworker]: self.logger.debug('Stopping %s', server) server.stop() # wait for all threads to stop. gevent.joinall(self.threads + [DDPLauncher.pgworker]) self.threads = []
def stop(self): """Stop all green threads.""" self.logger.debug('PostgresGreenlet stop') self._stop_event.set() # ask all threads to stop. for server in self.servers + [DDPLauncher.pgworker]: self.logger.debug('Stopping %s', server) server.stop() # wait for all threads to stop. gevent.joinall(self.threads + [DDPLauncher.pgworker]) self.threads = []
[ "Stop", "all", "green", "threads", "." ]
jazzband/django-ddp
python
https://github.com/jazzband/django-ddp/blob/1e1954b06fe140346acea43582515991685e4e01/dddp/main.py#L182-L192
[ "def", "stop", "(", "self", ")", ":", "self", ".", "logger", ".", "debug", "(", "'PostgresGreenlet stop'", ")", "self", ".", "_stop_event", ".", "set", "(", ")", "# ask all threads to stop.", "for", "server", "in", "self", ".", "servers", "+", "[", "DDPLauncher", ".", "pgworker", "]", ":", "self", ".", "logger", ".", "debug", "(", "'Stopping %s'", ",", "server", ")", "server", ".", "stop", "(", ")", "# wait for all threads to stop.", "gevent", ".", "joinall", "(", "self", ".", "threads", "+", "[", "DDPLauncher", ".", "pgworker", "]", ")", "self", ".", "threads", "=", "[", "]" ]
1e1954b06fe140346acea43582515991685e4e01
test
DDPLauncher.start
Run PostgresGreenlet and web/debug servers.
dddp/main.py
def start(self): """Run PostgresGreenlet and web/debug servers.""" self.logger.debug('PostgresGreenlet start') self._stop_event.clear() self.print('=> Discovering DDP endpoints...') if self.verbosity > 1: for api_path in sorted(self.api.api_path_map()): print(' %s' % api_path) # start greenlets self.pgworker.start() self.print('=> Started PostgresGreenlet.') for server in self.servers: thread = gevent.spawn(server.serve_forever) gevent.sleep() # yield to thread in case it can't start self.threads.append(thread) if thread.dead: # thread died, stop everything and re-raise the exception. self.stop() thread.get() if isinstance(server, geventwebsocket.WebSocketServer): self.print( '=> App running at: %s://%s:%d/' % ( 'https' if server.ssl_enabled else 'http', server.server_host, server.server_port, ), ) elif isinstance(server, gevent.backdoor.BackdoorServer): self.print( '=> Debug service running at: telnet://%s:%d/' % ( server.server_host, server.server_port, ), ) self.print('=> Started your app (%s).' % self.wsgi_name)
def start(self): """Run PostgresGreenlet and web/debug servers.""" self.logger.debug('PostgresGreenlet start') self._stop_event.clear() self.print('=> Discovering DDP endpoints...') if self.verbosity > 1: for api_path in sorted(self.api.api_path_map()): print(' %s' % api_path) # start greenlets self.pgworker.start() self.print('=> Started PostgresGreenlet.') for server in self.servers: thread = gevent.spawn(server.serve_forever) gevent.sleep() # yield to thread in case it can't start self.threads.append(thread) if thread.dead: # thread died, stop everything and re-raise the exception. self.stop() thread.get() if isinstance(server, geventwebsocket.WebSocketServer): self.print( '=> App running at: %s://%s:%d/' % ( 'https' if server.ssl_enabled else 'http', server.server_host, server.server_port, ), ) elif isinstance(server, gevent.backdoor.BackdoorServer): self.print( '=> Debug service running at: telnet://%s:%d/' % ( server.server_host, server.server_port, ), ) self.print('=> Started your app (%s).' % self.wsgi_name)
[ "Run", "PostgresGreenlet", "and", "web", "/", "debug", "servers", "." ]
jazzband/django-ddp
python
https://github.com/jazzband/django-ddp/blob/1e1954b06fe140346acea43582515991685e4e01/dddp/main.py#L194-L229
[ "def", "start", "(", "self", ")", ":", "self", ".", "logger", ".", "debug", "(", "'PostgresGreenlet start'", ")", "self", ".", "_stop_event", ".", "clear", "(", ")", "self", ".", "print", "(", "'=> Discovering DDP endpoints...'", ")", "if", "self", ".", "verbosity", ">", "1", ":", "for", "api_path", "in", "sorted", "(", "self", ".", "api", ".", "api_path_map", "(", ")", ")", ":", "print", "(", "' %s'", "%", "api_path", ")", "# start greenlets", "self", ".", "pgworker", ".", "start", "(", ")", "self", ".", "print", "(", "'=> Started PostgresGreenlet.'", ")", "for", "server", "in", "self", ".", "servers", ":", "thread", "=", "gevent", ".", "spawn", "(", "server", ".", "serve_forever", ")", "gevent", ".", "sleep", "(", ")", "# yield to thread in case it can't start", "self", ".", "threads", ".", "append", "(", "thread", ")", "if", "thread", ".", "dead", ":", "# thread died, stop everything and re-raise the exception.", "self", ".", "stop", "(", ")", "thread", ".", "get", "(", ")", "if", "isinstance", "(", "server", ",", "geventwebsocket", ".", "WebSocketServer", ")", ":", "self", ".", "print", "(", "'=> App running at: %s://%s:%d/'", "%", "(", "'https'", "if", "server", ".", "ssl_enabled", "else", "'http'", ",", "server", ".", "server_host", ",", "server", ".", "server_port", ",", ")", ",", ")", "elif", "isinstance", "(", "server", ",", "gevent", ".", "backdoor", ".", "BackdoorServer", ")", ":", "self", ".", "print", "(", "'=> Debug service running at: telnet://%s:%d/'", "%", "(", "server", ".", "server_host", ",", "server", ".", "server_port", ",", ")", ",", ")", "self", ".", "print", "(", "'=> Started your app (%s).'", "%", "self", ".", "wsgi_name", ")" ]
1e1954b06fe140346acea43582515991685e4e01
test
DDPLauncher.run
Run DDP greenlets.
dddp/main.py
def run(self): """Run DDP greenlets.""" self.logger.debug('PostgresGreenlet run') self.start() self._stop_event.wait() # wait for all threads to stop. gevent.joinall(self.threads + [DDPLauncher.pgworker]) self.threads = []
def run(self): """Run DDP greenlets.""" self.logger.debug('PostgresGreenlet run') self.start() self._stop_event.wait() # wait for all threads to stop. gevent.joinall(self.threads + [DDPLauncher.pgworker]) self.threads = []
[ "Run", "DDP", "greenlets", "." ]
jazzband/django-ddp
python
https://github.com/jazzband/django-ddp/blob/1e1954b06fe140346acea43582515991685e4e01/dddp/main.py#L231-L238
[ "def", "run", "(", "self", ")", ":", "self", ".", "logger", ".", "debug", "(", "'PostgresGreenlet run'", ")", "self", ".", "start", "(", ")", "self", ".", "_stop_event", ".", "wait", "(", ")", "# wait for all threads to stop.", "gevent", ".", "joinall", "(", "self", ".", "threads", "+", "[", "DDPLauncher", ".", "pgworker", "]", ")", "self", ".", "threads", "=", "[", "]" ]
1e1954b06fe140346acea43582515991685e4e01
test
DjangoDDPConfig.ready
Initialisation for django-ddp (setup lookups and signal handlers).
dddp/apps.py
def ready(self): """Initialisation for django-ddp (setup lookups and signal handlers).""" if not settings.DATABASES: raise ImproperlyConfigured('No databases configured.') for (alias, conf) in settings.DATABASES.items(): engine = conf['ENGINE'] if engine not in [ 'django.db.backends.postgresql', 'django.db.backends.postgresql_psycopg2', ]: warnings.warn( 'Database %r uses unsupported %r engine.' % ( alias, engine, ), UserWarning, ) self.api = autodiscover() self.api.ready()
def ready(self): """Initialisation for django-ddp (setup lookups and signal handlers).""" if not settings.DATABASES: raise ImproperlyConfigured('No databases configured.') for (alias, conf) in settings.DATABASES.items(): engine = conf['ENGINE'] if engine not in [ 'django.db.backends.postgresql', 'django.db.backends.postgresql_psycopg2', ]: warnings.warn( 'Database %r uses unsupported %r engine.' % ( alias, engine, ), UserWarning, ) self.api = autodiscover() self.api.ready()
[ "Initialisation", "for", "django", "-", "ddp", "(", "setup", "lookups", "and", "signal", "handlers", ")", "." ]
jazzband/django-ddp
python
https://github.com/jazzband/django-ddp/blob/1e1954b06fe140346acea43582515991685e4e01/dddp/apps.py#L18-L35
[ "def", "ready", "(", "self", ")", ":", "if", "not", "settings", ".", "DATABASES", ":", "raise", "ImproperlyConfigured", "(", "'No databases configured.'", ")", "for", "(", "alias", ",", "conf", ")", "in", "settings", ".", "DATABASES", ".", "items", "(", ")", ":", "engine", "=", "conf", "[", "'ENGINE'", "]", "if", "engine", "not", "in", "[", "'django.db.backends.postgresql'", ",", "'django.db.backends.postgresql_psycopg2'", ",", "]", ":", "warnings", ".", "warn", "(", "'Database %r uses unsupported %r engine.'", "%", "(", "alias", ",", "engine", ",", ")", ",", "UserWarning", ",", ")", "self", ".", "api", "=", "autodiscover", "(", ")", "self", ".", "api", ".", "ready", "(", ")" ]
1e1954b06fe140346acea43582515991685e4e01
test
PostgresGreenlet._run
Spawn sub tasks, wait for stop signal.
dddp/postgres.py
def _run(self): # pylint: disable=method-hidden """Spawn sub tasks, wait for stop signal.""" conn_params = self.connection.get_connection_params() # See http://initd.org/psycopg/docs/module.html#psycopg2.connect and # http://www.postgresql.org/docs/current/static/libpq-connect.html # section 31.1.2 (Parameter Key Words) for details on available params. conn_params.update( async=True, application_name='{} pid={} django-ddp'.format( socket.gethostname(), # hostname os.getpid(), # PID )[:64], # 64 characters for default PostgreSQL build config ) conn = None while conn is None: try: conn = psycopg2.connect(**conn_params) except psycopg2.OperationalError as err: # Some variants of the psycopg2 driver for Django add extra # params that aren't meant to be passed directly to # `psycopg2.connect()` -- issue a warning and try again. msg = ('%s' % err).strip() msg_prefix = 'invalid connection option "' if not msg.startswith(msg_prefix): # *waves hand* this is not the errror you are looking for. raise key = msg[len(msg_prefix):-1] self.logger.warning( 'Ignoring unknown settings.DATABASES[%r] option: %s=%r', self.connection.alias, key, conn_params.pop(key), ) self.poll(conn) # wait for conneciton to start import logging logging.getLogger('dddp').info('=> Started PostgresGreenlet.') cur = conn.cursor() cur.execute('LISTEN "ddp";') while not self._stop_event.is_set(): try: self.select_greenlet = gevent.spawn( gevent.select.select, [conn], [], [], timeout=None, ) self.select_greenlet.get() except gevent.GreenletExit: self._stop_event.set() finally: self.select_greenlet = None self.poll(conn) self.poll(conn) cur.close() self.poll(conn) conn.close()
def _run(self): # pylint: disable=method-hidden """Spawn sub tasks, wait for stop signal.""" conn_params = self.connection.get_connection_params() # See http://initd.org/psycopg/docs/module.html#psycopg2.connect and # http://www.postgresql.org/docs/current/static/libpq-connect.html # section 31.1.2 (Parameter Key Words) for details on available params. conn_params.update( async=True, application_name='{} pid={} django-ddp'.format( socket.gethostname(), # hostname os.getpid(), # PID )[:64], # 64 characters for default PostgreSQL build config ) conn = None while conn is None: try: conn = psycopg2.connect(**conn_params) except psycopg2.OperationalError as err: # Some variants of the psycopg2 driver for Django add extra # params that aren't meant to be passed directly to # `psycopg2.connect()` -- issue a warning and try again. msg = ('%s' % err).strip() msg_prefix = 'invalid connection option "' if not msg.startswith(msg_prefix): # *waves hand* this is not the errror you are looking for. raise key = msg[len(msg_prefix):-1] self.logger.warning( 'Ignoring unknown settings.DATABASES[%r] option: %s=%r', self.connection.alias, key, conn_params.pop(key), ) self.poll(conn) # wait for conneciton to start import logging logging.getLogger('dddp').info('=> Started PostgresGreenlet.') cur = conn.cursor() cur.execute('LISTEN "ddp";') while not self._stop_event.is_set(): try: self.select_greenlet = gevent.spawn( gevent.select.select, [conn], [], [], timeout=None, ) self.select_greenlet.get() except gevent.GreenletExit: self._stop_event.set() finally: self.select_greenlet = None self.poll(conn) self.poll(conn) cur.close() self.poll(conn) conn.close()
[ "Spawn", "sub", "tasks", "wait", "for", "stop", "signal", "." ]
jazzband/django-ddp
python
https://github.com/jazzband/django-ddp/blob/1e1954b06fe140346acea43582515991685e4e01/dddp/postgres.py#L35-L89
[ "def", "_run", "(", "self", ")", ":", "# pylint: disable=method-hidden", "conn_params", "=", "self", ".", "connection", ".", "get_connection_params", "(", ")", "# See http://initd.org/psycopg/docs/module.html#psycopg2.connect and", "# http://www.postgresql.org/docs/current/static/libpq-connect.html", "# section 31.1.2 (Parameter Key Words) for details on available params.", "conn_params", ".", "update", "(", "async", "=", "True", ",", "application_name", "=", "'{} pid={} django-ddp'", ".", "format", "(", "socket", ".", "gethostname", "(", ")", ",", "# hostname", "os", ".", "getpid", "(", ")", ",", "# PID", ")", "[", ":", "64", "]", ",", "# 64 characters for default PostgreSQL build config", ")", "conn", "=", "None", "while", "conn", "is", "None", ":", "try", ":", "conn", "=", "psycopg2", ".", "connect", "(", "*", "*", "conn_params", ")", "except", "psycopg2", ".", "OperationalError", "as", "err", ":", "# Some variants of the psycopg2 driver for Django add extra", "# params that aren't meant to be passed directly to", "# `psycopg2.connect()` -- issue a warning and try again.", "msg", "=", "(", "'%s'", "%", "err", ")", ".", "strip", "(", ")", "msg_prefix", "=", "'invalid connection option \"'", "if", "not", "msg", ".", "startswith", "(", "msg_prefix", ")", ":", "# *waves hand* this is not the errror you are looking for.", "raise", "key", "=", "msg", "[", "len", "(", "msg_prefix", ")", ":", "-", "1", "]", "self", ".", "logger", ".", "warning", "(", "'Ignoring unknown settings.DATABASES[%r] option: %s=%r'", ",", "self", ".", "connection", ".", "alias", ",", "key", ",", "conn_params", ".", "pop", "(", "key", ")", ",", ")", "self", ".", "poll", "(", "conn", ")", "# wait for conneciton to start", "import", "logging", "logging", ".", "getLogger", "(", "'dddp'", ")", ".", "info", "(", "'=> Started PostgresGreenlet.'", ")", "cur", "=", "conn", ".", "cursor", "(", ")", "cur", ".", "execute", "(", "'LISTEN \"ddp\";'", ")", "while", "not", "self", ".", "_stop_event", ".", "is_set", "(", ")", ":", "try", ":", "self", ".", "select_greenlet", "=", "gevent", ".", "spawn", "(", "gevent", ".", "select", ".", "select", ",", "[", "conn", "]", ",", "[", "]", ",", "[", "]", ",", "timeout", "=", "None", ",", ")", "self", ".", "select_greenlet", ".", "get", "(", ")", "except", "gevent", ".", "GreenletExit", ":", "self", ".", "_stop_event", ".", "set", "(", ")", "finally", ":", "self", ".", "select_greenlet", "=", "None", "self", ".", "poll", "(", "conn", ")", "self", ".", "poll", "(", "conn", ")", "cur", ".", "close", "(", ")", "self", ".", "poll", "(", "conn", ")", "conn", ".", "close", "(", ")" ]
1e1954b06fe140346acea43582515991685e4e01
test
PostgresGreenlet.stop
Stop subtasks and let run() finish.
dddp/postgres.py
def stop(self): """Stop subtasks and let run() finish.""" self._stop_event.set() if self.select_greenlet is not None: self.select_greenlet.kill() self.select_greenlet.get() gevent.sleep()
def stop(self): """Stop subtasks and let run() finish.""" self._stop_event.set() if self.select_greenlet is not None: self.select_greenlet.kill() self.select_greenlet.get() gevent.sleep()
[ "Stop", "subtasks", "and", "let", "run", "()", "finish", "." ]
jazzband/django-ddp
python
https://github.com/jazzband/django-ddp/blob/1e1954b06fe140346acea43582515991685e4e01/dddp/postgres.py#L91-L97
[ "def", "stop", "(", "self", ")", ":", "self", ".", "_stop_event", ".", "set", "(", ")", "if", "self", ".", "select_greenlet", "is", "not", "None", ":", "self", ".", "select_greenlet", ".", "kill", "(", ")", "self", ".", "select_greenlet", ".", "get", "(", ")", "gevent", ".", "sleep", "(", ")" ]
1e1954b06fe140346acea43582515991685e4e01
test
PostgresGreenlet.poll
Poll DB socket and process async tasks.
dddp/postgres.py
def poll(self, conn): """Poll DB socket and process async tasks.""" while 1: state = conn.poll() if state == psycopg2.extensions.POLL_OK: while conn.notifies: notify = conn.notifies.pop() self.logger.info( "Got NOTIFY (pid=%d, payload=%r)", notify.pid, notify.payload, ) # read the header and check seq/fin. hdr, chunk = notify.payload.split('|', 1) # print('RECEIVE: %s' % hdr) header = ejson.loads(hdr) uuid = header['uuid'] size, chunks = self.chunks.setdefault(uuid, [0, {}]) if header['fin']: size = self.chunks[uuid][0] = header['seq'] # stash the chunk chunks[header['seq']] = chunk if len(chunks) != size: # haven't got all the chunks yet continue # process next NOTIFY in loop # got the last chunk -> process it. data = ''.join( chunk for _, chunk in sorted(chunks.items()) ) del self.chunks[uuid] # don't forget to cleanup! data = ejson.loads(data) sender = data.pop('_sender', None) tx_id = data.pop('_tx_id', None) for connection_id in data.pop('_connection_ids'): try: websocket = self.connections[connection_id] except KeyError: continue # connection not in this process if connection_id == sender: websocket.send(data, tx_id=tx_id) else: websocket.send(data) break elif state == psycopg2.extensions.POLL_WRITE: gevent.select.select([], [conn.fileno()], []) elif state == psycopg2.extensions.POLL_READ: gevent.select.select([conn.fileno()], [], []) else: self.logger.warn('POLL_ERR: %s', state)
def poll(self, conn): """Poll DB socket and process async tasks.""" while 1: state = conn.poll() if state == psycopg2.extensions.POLL_OK: while conn.notifies: notify = conn.notifies.pop() self.logger.info( "Got NOTIFY (pid=%d, payload=%r)", notify.pid, notify.payload, ) # read the header and check seq/fin. hdr, chunk = notify.payload.split('|', 1) # print('RECEIVE: %s' % hdr) header = ejson.loads(hdr) uuid = header['uuid'] size, chunks = self.chunks.setdefault(uuid, [0, {}]) if header['fin']: size = self.chunks[uuid][0] = header['seq'] # stash the chunk chunks[header['seq']] = chunk if len(chunks) != size: # haven't got all the chunks yet continue # process next NOTIFY in loop # got the last chunk -> process it. data = ''.join( chunk for _, chunk in sorted(chunks.items()) ) del self.chunks[uuid] # don't forget to cleanup! data = ejson.loads(data) sender = data.pop('_sender', None) tx_id = data.pop('_tx_id', None) for connection_id in data.pop('_connection_ids'): try: websocket = self.connections[connection_id] except KeyError: continue # connection not in this process if connection_id == sender: websocket.send(data, tx_id=tx_id) else: websocket.send(data) break elif state == psycopg2.extensions.POLL_WRITE: gevent.select.select([], [conn.fileno()], []) elif state == psycopg2.extensions.POLL_READ: gevent.select.select([conn.fileno()], [], []) else: self.logger.warn('POLL_ERR: %s', state)
[ "Poll", "DB", "socket", "and", "process", "async", "tasks", "." ]
jazzband/django-ddp
python
https://github.com/jazzband/django-ddp/blob/1e1954b06fe140346acea43582515991685e4e01/dddp/postgres.py#L99-L150
[ "def", "poll", "(", "self", ",", "conn", ")", ":", "while", "1", ":", "state", "=", "conn", ".", "poll", "(", ")", "if", "state", "==", "psycopg2", ".", "extensions", ".", "POLL_OK", ":", "while", "conn", ".", "notifies", ":", "notify", "=", "conn", ".", "notifies", ".", "pop", "(", ")", "self", ".", "logger", ".", "info", "(", "\"Got NOTIFY (pid=%d, payload=%r)\"", ",", "notify", ".", "pid", ",", "notify", ".", "payload", ",", ")", "# read the header and check seq/fin.", "hdr", ",", "chunk", "=", "notify", ".", "payload", ".", "split", "(", "'|'", ",", "1", ")", "# print('RECEIVE: %s' % hdr)", "header", "=", "ejson", ".", "loads", "(", "hdr", ")", "uuid", "=", "header", "[", "'uuid'", "]", "size", ",", "chunks", "=", "self", ".", "chunks", ".", "setdefault", "(", "uuid", ",", "[", "0", ",", "{", "}", "]", ")", "if", "header", "[", "'fin'", "]", ":", "size", "=", "self", ".", "chunks", "[", "uuid", "]", "[", "0", "]", "=", "header", "[", "'seq'", "]", "# stash the chunk", "chunks", "[", "header", "[", "'seq'", "]", "]", "=", "chunk", "if", "len", "(", "chunks", ")", "!=", "size", ":", "# haven't got all the chunks yet", "continue", "# process next NOTIFY in loop", "# got the last chunk -> process it.", "data", "=", "''", ".", "join", "(", "chunk", "for", "_", ",", "chunk", "in", "sorted", "(", "chunks", ".", "items", "(", ")", ")", ")", "del", "self", ".", "chunks", "[", "uuid", "]", "# don't forget to cleanup!", "data", "=", "ejson", ".", "loads", "(", "data", ")", "sender", "=", "data", ".", "pop", "(", "'_sender'", ",", "None", ")", "tx_id", "=", "data", ".", "pop", "(", "'_tx_id'", ",", "None", ")", "for", "connection_id", "in", "data", ".", "pop", "(", "'_connection_ids'", ")", ":", "try", ":", "websocket", "=", "self", ".", "connections", "[", "connection_id", "]", "except", "KeyError", ":", "continue", "# connection not in this process", "if", "connection_id", "==", "sender", ":", "websocket", ".", "send", "(", "data", ",", "tx_id", "=", "tx_id", ")", "else", ":", "websocket", ".", "send", "(", "data", ")", "break", "elif", "state", "==", "psycopg2", ".", "extensions", ".", "POLL_WRITE", ":", "gevent", ".", "select", ".", "select", "(", "[", "]", ",", "[", "conn", ".", "fileno", "(", ")", "]", ",", "[", "]", ")", "elif", "state", "==", "psycopg2", ".", "extensions", ".", "POLL_READ", ":", "gevent", ".", "select", ".", "select", "(", "[", "conn", ".", "fileno", "(", ")", "]", ",", "[", "]", ",", "[", "]", ")", "else", ":", "self", ".", "logger", ".", "warn", "(", "'POLL_ERR: %s'", ",", "state", ")" ]
1e1954b06fe140346acea43582515991685e4e01
test
greenify
Patch threading and psycopg2 modules for green threads.
dddp/__init__.py
def greenify(): """Patch threading and psycopg2 modules for green threads.""" # don't greenify twice. if _GREEN: return _GREEN[True] = True from gevent.monkey import patch_all, saved if ('threading' in sys.modules) and ('threading' not in saved): import warnings warnings.warn('threading module loaded before patching!') patch_all() try: # Use psycopg2 by default import psycopg2 del psycopg2 except ImportError: # Fallback to psycopg2cffi if required (eg: pypy) from psycopg2cffi import compat compat.register() from psycogreen.gevent import patch_psycopg patch_psycopg()
def greenify(): """Patch threading and psycopg2 modules for green threads.""" # don't greenify twice. if _GREEN: return _GREEN[True] = True from gevent.monkey import patch_all, saved if ('threading' in sys.modules) and ('threading' not in saved): import warnings warnings.warn('threading module loaded before patching!') patch_all() try: # Use psycopg2 by default import psycopg2 del psycopg2 except ImportError: # Fallback to psycopg2cffi if required (eg: pypy) from psycopg2cffi import compat compat.register() from psycogreen.gevent import patch_psycopg patch_psycopg()
[ "Patch", "threading", "and", "psycopg2", "modules", "for", "green", "threads", "." ]
jazzband/django-ddp
python
https://github.com/jazzband/django-ddp/blob/1e1954b06fe140346acea43582515991685e4e01/dddp/__init__.py#L19-L42
[ "def", "greenify", "(", ")", ":", "# don't greenify twice.", "if", "_GREEN", ":", "return", "_GREEN", "[", "True", "]", "=", "True", "from", "gevent", ".", "monkey", "import", "patch_all", ",", "saved", "if", "(", "'threading'", "in", "sys", ".", "modules", ")", "and", "(", "'threading'", "not", "in", "saved", ")", ":", "import", "warnings", "warnings", ".", "warn", "(", "'threading module loaded before patching!'", ")", "patch_all", "(", ")", "try", ":", "# Use psycopg2 by default", "import", "psycopg2", "del", "psycopg2", "except", "ImportError", ":", "# Fallback to psycopg2cffi if required (eg: pypy)", "from", "psycopg2cffi", "import", "compat", "compat", ".", "register", "(", ")", "from", "psycogreen", ".", "gevent", "import", "patch_psycopg", "patch_psycopg", "(", ")" ]
1e1954b06fe140346acea43582515991685e4e01
test
meteor_random_id
Generate a new ID, optionally using namespace of given `name`.
dddp/__init__.py
def meteor_random_id(name=None, length=17): """Generate a new ID, optionally using namespace of given `name`.""" if name is None: stream = THREAD_LOCAL.alea_random else: stream = THREAD_LOCAL.random_streams[name] return stream.random_string(length, METEOR_ID_CHARS)
def meteor_random_id(name=None, length=17): """Generate a new ID, optionally using namespace of given `name`.""" if name is None: stream = THREAD_LOCAL.alea_random else: stream = THREAD_LOCAL.random_streams[name] return stream.random_string(length, METEOR_ID_CHARS)
[ "Generate", "a", "new", "ID", "optionally", "using", "namespace", "of", "given", "name", "." ]
jazzband/django-ddp
python
https://github.com/jazzband/django-ddp/blob/1e1954b06fe140346acea43582515991685e4e01/dddp/__init__.py#L171-L177
[ "def", "meteor_random_id", "(", "name", "=", "None", ",", "length", "=", "17", ")", ":", "if", "name", "is", "None", ":", "stream", "=", "THREAD_LOCAL", ".", "alea_random", "else", ":", "stream", "=", "THREAD_LOCAL", ".", "random_streams", "[", "name", "]", "return", "stream", ".", "random_string", "(", "length", ",", "METEOR_ID_CHARS", ")" ]
1e1954b06fe140346acea43582515991685e4e01
test
autodiscover
Import all `ddp` submodules from `settings.INSTALLED_APPS`.
dddp/__init__.py
def autodiscover(): """Import all `ddp` submodules from `settings.INSTALLED_APPS`.""" from django.utils.module_loading import autodiscover_modules from dddp.api import API autodiscover_modules('ddp', register_to=API) return API
def autodiscover(): """Import all `ddp` submodules from `settings.INSTALLED_APPS`.""" from django.utils.module_loading import autodiscover_modules from dddp.api import API autodiscover_modules('ddp', register_to=API) return API
[ "Import", "all", "ddp", "submodules", "from", "settings", ".", "INSTALLED_APPS", "." ]
jazzband/django-ddp
python
https://github.com/jazzband/django-ddp/blob/1e1954b06fe140346acea43582515991685e4e01/dddp/__init__.py#L180-L185
[ "def", "autodiscover", "(", ")", ":", "from", "django", ".", "utils", ".", "module_loading", "import", "autodiscover_modules", "from", "dddp", ".", "api", "import", "API", "autodiscover_modules", "(", "'ddp'", ",", "register_to", "=", "API", ")", "return", "API" ]
1e1954b06fe140346acea43582515991685e4e01
test
MeteorError.as_dict
Return an error dict for self.args and kwargs.
dddp/__init__.py
def as_dict(self, **kwargs): """Return an error dict for self.args and kwargs.""" error, reason, details, err_kwargs = self.args result = { key: val for key, val in { 'error': error, 'reason': reason, 'details': details, }.items() if val is not None } result.update(err_kwargs) result.update(kwargs) return result
def as_dict(self, **kwargs): """Return an error dict for self.args and kwargs.""" error, reason, details, err_kwargs = self.args result = { key: val for key, val in { 'error': error, 'reason': reason, 'details': details, }.items() if val is not None } result.update(err_kwargs) result.update(kwargs) return result
[ "Return", "an", "error", "dict", "for", "self", ".", "args", "and", "kwargs", "." ]
jazzband/django-ddp
python
https://github.com/jazzband/django-ddp/blob/1e1954b06fe140346acea43582515991685e4e01/dddp/__init__.py#L69-L81
[ "def", "as_dict", "(", "self", ",", "*", "*", "kwargs", ")", ":", "error", ",", "reason", ",", "details", ",", "err_kwargs", "=", "self", ".", "args", "result", "=", "{", "key", ":", "val", "for", "key", ",", "val", "in", "{", "'error'", ":", "error", ",", "'reason'", ":", "reason", ",", "'details'", ":", "details", ",", "}", ".", "items", "(", ")", "if", "val", "is", "not", "None", "}", "result", ".", "update", "(", "err_kwargs", ")", "result", ".", "update", "(", "kwargs", ")", "return", "result" ]
1e1954b06fe140346acea43582515991685e4e01
test
ThreadLocal.get
Get attribute, creating if required using specified factory.
dddp/__init__.py
def get(self, name, factory, *factory_args, **factory_kwargs): """Get attribute, creating if required using specified factory.""" update_thread_local = getattr(factory, 'update_thread_local', True) if (not update_thread_local) or (name not in self.__dict__): obj = factory(*factory_args, **factory_kwargs) if update_thread_local: setattr(self, name, obj) return obj return getattr(self, name)
def get(self, name, factory, *factory_args, **factory_kwargs): """Get attribute, creating if required using specified factory.""" update_thread_local = getattr(factory, 'update_thread_local', True) if (not update_thread_local) or (name not in self.__dict__): obj = factory(*factory_args, **factory_kwargs) if update_thread_local: setattr(self, name, obj) return obj return getattr(self, name)
[ "Get", "attribute", "creating", "if", "required", "using", "specified", "factory", "." ]
jazzband/django-ddp
python
https://github.com/jazzband/django-ddp/blob/1e1954b06fe140346acea43582515991685e4e01/dddp/__init__.py#L111-L119
[ "def", "get", "(", "self", ",", "name", ",", "factory", ",", "*", "factory_args", ",", "*", "*", "factory_kwargs", ")", ":", "update_thread_local", "=", "getattr", "(", "factory", ",", "'update_thread_local'", ",", "True", ")", "if", "(", "not", "update_thread_local", ")", "or", "(", "name", "not", "in", "self", ".", "__dict__", ")", ":", "obj", "=", "factory", "(", "*", "factory_args", ",", "*", "*", "factory_kwargs", ")", "if", "update_thread_local", ":", "setattr", "(", "self", ",", "name", ",", "obj", ")", "return", "obj", "return", "getattr", "(", "self", ",", "name", ")" ]
1e1954b06fe140346acea43582515991685e4e01
test
DDPHandler.emit
Emit a formatted log record via DDP.
dddp/logging.py
def emit(self, record): """Emit a formatted log record via DDP.""" if getattr(this, 'subs', {}).get(LOGS_NAME, False): self.format(record) this.send({ 'msg': ADDED, 'collection': LOGS_NAME, 'id': meteor_random_id('/collection/%s' % LOGS_NAME), 'fields': { attr: { # typecasting methods for specific attributes 'args': lambda args: [repr(arg) for arg in args], 'created': datetime.datetime.fromtimestamp, 'exc_info': stacklines_or_none, }.get( attr, lambda val: val # default typecasting method )(getattr(record, attr, None)) for attr in ( 'args', 'asctime', 'created', 'exc_info', 'filename', 'funcName', 'levelname', 'levelno', 'lineno', 'module', 'msecs', 'message', 'name', 'pathname', 'process', 'processName', 'relativeCreated', 'thread', 'threadName', ) }, })
def emit(self, record): """Emit a formatted log record via DDP.""" if getattr(this, 'subs', {}).get(LOGS_NAME, False): self.format(record) this.send({ 'msg': ADDED, 'collection': LOGS_NAME, 'id': meteor_random_id('/collection/%s' % LOGS_NAME), 'fields': { attr: { # typecasting methods for specific attributes 'args': lambda args: [repr(arg) for arg in args], 'created': datetime.datetime.fromtimestamp, 'exc_info': stacklines_or_none, }.get( attr, lambda val: val # default typecasting method )(getattr(record, attr, None)) for attr in ( 'args', 'asctime', 'created', 'exc_info', 'filename', 'funcName', 'levelname', 'levelno', 'lineno', 'module', 'msecs', 'message', 'name', 'pathname', 'process', 'processName', 'relativeCreated', 'thread', 'threadName', ) }, })
[ "Emit", "a", "formatted", "log", "record", "via", "DDP", "." ]
jazzband/django-ddp
python
https://github.com/jazzband/django-ddp/blob/1e1954b06fe140346acea43582515991685e4e01/dddp/logging.py#L27-L67
[ "def", "emit", "(", "self", ",", "record", ")", ":", "if", "getattr", "(", "this", ",", "'subs'", ",", "{", "}", ")", ".", "get", "(", "LOGS_NAME", ",", "False", ")", ":", "self", ".", "format", "(", "record", ")", "this", ".", "send", "(", "{", "'msg'", ":", "ADDED", ",", "'collection'", ":", "LOGS_NAME", ",", "'id'", ":", "meteor_random_id", "(", "'/collection/%s'", "%", "LOGS_NAME", ")", ",", "'fields'", ":", "{", "attr", ":", "{", "# typecasting methods for specific attributes", "'args'", ":", "lambda", "args", ":", "[", "repr", "(", "arg", ")", "for", "arg", "in", "args", "]", ",", "'created'", ":", "datetime", ".", "datetime", ".", "fromtimestamp", ",", "'exc_info'", ":", "stacklines_or_none", ",", "}", ".", "get", "(", "attr", ",", "lambda", "val", ":", "val", "# default typecasting method", ")", "(", "getattr", "(", "record", ",", "attr", ",", "None", ")", ")", "for", "attr", "in", "(", "'args'", ",", "'asctime'", ",", "'created'", ",", "'exc_info'", ",", "'filename'", ",", "'funcName'", ",", "'levelname'", ",", "'levelno'", ",", "'lineno'", ",", "'module'", ",", "'msecs'", ",", "'message'", ",", "'name'", ",", "'pathname'", ",", "'process'", ",", "'processName'", ",", "'relativeCreated'", ",", "'thread'", ",", "'threadName'", ",", ")", "}", ",", "}", ")" ]
1e1954b06fe140346acea43582515991685e4e01
test
select_renderer
Given a request, a list of renderers, and the ``force`` configuration option, return a two-tuple of: (media type, render callable). Uses mimeparse to find the best media type match from the ACCEPT header.
aiohttp_utils/negotiation.py
def select_renderer(request: web.Request, renderers: OrderedDict, force=True): """ Given a request, a list of renderers, and the ``force`` configuration option, return a two-tuple of: (media type, render callable). Uses mimeparse to find the best media type match from the ACCEPT header. """ header = request.headers.get('ACCEPT', '*/*') best_match = mimeparse.best_match(renderers.keys(), header) if not best_match or best_match not in renderers: if force: return tuple(renderers.items())[0] else: raise web.HTTPNotAcceptable return best_match, renderers[best_match]
def select_renderer(request: web.Request, renderers: OrderedDict, force=True): """ Given a request, a list of renderers, and the ``force`` configuration option, return a two-tuple of: (media type, render callable). Uses mimeparse to find the best media type match from the ACCEPT header. """ header = request.headers.get('ACCEPT', '*/*') best_match = mimeparse.best_match(renderers.keys(), header) if not best_match or best_match not in renderers: if force: return tuple(renderers.items())[0] else: raise web.HTTPNotAcceptable return best_match, renderers[best_match]
[ "Given", "a", "request", "a", "list", "of", "renderers", "and", "the", "force", "configuration", "option", "return", "a", "two", "-", "tuple", "of", ":", "(", "media", "type", "render", "callable", ")", ".", "Uses", "mimeparse", "to", "find", "the", "best", "media", "type", "match", "from", "the", "ACCEPT", "header", "." ]
sloria/aiohttp_utils
python
https://github.com/sloria/aiohttp_utils/blob/e5b41452f8077e7d749715606b1560f4b50e3d71/aiohttp_utils/negotiation.py#L131-L145
[ "def", "select_renderer", "(", "request", ":", "web", ".", "Request", ",", "renderers", ":", "OrderedDict", ",", "force", "=", "True", ")", ":", "header", "=", "request", ".", "headers", ".", "get", "(", "'ACCEPT'", ",", "'*/*'", ")", "best_match", "=", "mimeparse", ".", "best_match", "(", "renderers", ".", "keys", "(", ")", ",", "header", ")", "if", "not", "best_match", "or", "best_match", "not", "in", "renderers", ":", "if", "force", ":", "return", "tuple", "(", "renderers", ".", "items", "(", ")", ")", "[", "0", "]", "else", ":", "raise", "web", ".", "HTTPNotAcceptable", "return", "best_match", ",", "renderers", "[", "best_match", "]" ]
e5b41452f8077e7d749715606b1560f4b50e3d71
test
negotiation_middleware
Middleware which selects a renderer for a given request then renders a handler's data to a `aiohttp.web.Response`.
aiohttp_utils/negotiation.py
def negotiation_middleware( renderers=DEFAULTS['RENDERERS'], negotiator=DEFAULTS['NEGOTIATOR'], force_negotiation=DEFAULTS['FORCE_NEGOTIATION'] ): """Middleware which selects a renderer for a given request then renders a handler's data to a `aiohttp.web.Response`. """ @asyncio.coroutine def factory(app, handler): @asyncio.coroutine def middleware(request): content_type, renderer = negotiator( request, renderers, force_negotiation, ) request['selected_media_type'] = content_type response = yield from handler(request) if getattr(response, 'data', None): # Render data with the selected renderer if asyncio.iscoroutinefunction(renderer): render_result = yield from renderer(request, response.data) else: render_result = renderer(request, response.data) else: render_result = response if isinstance(render_result, web.Response): return render_result if getattr(response, 'data', None): response.body = render_result response.content_type = content_type return response return middleware return factory
def negotiation_middleware( renderers=DEFAULTS['RENDERERS'], negotiator=DEFAULTS['NEGOTIATOR'], force_negotiation=DEFAULTS['FORCE_NEGOTIATION'] ): """Middleware which selects a renderer for a given request then renders a handler's data to a `aiohttp.web.Response`. """ @asyncio.coroutine def factory(app, handler): @asyncio.coroutine def middleware(request): content_type, renderer = negotiator( request, renderers, force_negotiation, ) request['selected_media_type'] = content_type response = yield from handler(request) if getattr(response, 'data', None): # Render data with the selected renderer if asyncio.iscoroutinefunction(renderer): render_result = yield from renderer(request, response.data) else: render_result = renderer(request, response.data) else: render_result = response if isinstance(render_result, web.Response): return render_result if getattr(response, 'data', None): response.body = render_result response.content_type = content_type return response return middleware return factory
[ "Middleware", "which", "selects", "a", "renderer", "for", "a", "given", "request", "then", "renders", "a", "handler", "s", "data", "to", "a", "aiohttp", ".", "web", ".", "Response", "." ]
sloria/aiohttp_utils
python
https://github.com/sloria/aiohttp_utils/blob/e5b41452f8077e7d749715606b1560f4b50e3d71/aiohttp_utils/negotiation.py#L176-L215
[ "def", "negotiation_middleware", "(", "renderers", "=", "DEFAULTS", "[", "'RENDERERS'", "]", ",", "negotiator", "=", "DEFAULTS", "[", "'NEGOTIATOR'", "]", ",", "force_negotiation", "=", "DEFAULTS", "[", "'FORCE_NEGOTIATION'", "]", ")", ":", "@", "asyncio", ".", "coroutine", "def", "factory", "(", "app", ",", "handler", ")", ":", "@", "asyncio", ".", "coroutine", "def", "middleware", "(", "request", ")", ":", "content_type", ",", "renderer", "=", "negotiator", "(", "request", ",", "renderers", ",", "force_negotiation", ",", ")", "request", "[", "'selected_media_type'", "]", "=", "content_type", "response", "=", "yield", "from", "handler", "(", "request", ")", "if", "getattr", "(", "response", ",", "'data'", ",", "None", ")", ":", "# Render data with the selected renderer", "if", "asyncio", ".", "iscoroutinefunction", "(", "renderer", ")", ":", "render_result", "=", "yield", "from", "renderer", "(", "request", ",", "response", ".", "data", ")", "else", ":", "render_result", "=", "renderer", "(", "request", ",", "response", ".", "data", ")", "else", ":", "render_result", "=", "response", "if", "isinstance", "(", "render_result", ",", "web", ".", "Response", ")", ":", "return", "render_result", "if", "getattr", "(", "response", ",", "'data'", ",", "None", ")", ":", "response", ".", "body", "=", "render_result", "response", ".", "content_type", "=", "content_type", "return", "response", "return", "middleware", "return", "factory" ]
e5b41452f8077e7d749715606b1560f4b50e3d71
test
setup
Set up the negotiation middleware. Reads configuration from ``app['AIOHTTP_UTILS']``. :param app: Application to set up. :param negotiator: Function that selects a renderer given a request, a dict of renderers, and a ``force`` parameter (whether to return a renderer even if the client passes an unsupported media type). :param renderers: Mapping of mediatypes to callable renderers. :param force_negotiation: Whether to return a rennderer even if the client passes an unsupported media type).
aiohttp_utils/negotiation.py
def setup( app: web.Application, *, negotiator: callable=DEFAULTS['NEGOTIATOR'], renderers: OrderedDict=DEFAULTS['RENDERERS'], force_negotiation: bool=DEFAULTS['FORCE_NEGOTIATION'] ): """Set up the negotiation middleware. Reads configuration from ``app['AIOHTTP_UTILS']``. :param app: Application to set up. :param negotiator: Function that selects a renderer given a request, a dict of renderers, and a ``force`` parameter (whether to return a renderer even if the client passes an unsupported media type). :param renderers: Mapping of mediatypes to callable renderers. :param force_negotiation: Whether to return a rennderer even if the client passes an unsupported media type). """ config = app.get(CONFIG_KEY, {}) middleware = negotiation_middleware( renderers=config.get('RENDERERS', renderers), negotiator=config.get('NEGOTIATOR', negotiator), force_negotiation=config.get('FORCE_NEGOTIATION', force_negotiation) ) app.middlewares.append(middleware) return app
def setup( app: web.Application, *, negotiator: callable=DEFAULTS['NEGOTIATOR'], renderers: OrderedDict=DEFAULTS['RENDERERS'], force_negotiation: bool=DEFAULTS['FORCE_NEGOTIATION'] ): """Set up the negotiation middleware. Reads configuration from ``app['AIOHTTP_UTILS']``. :param app: Application to set up. :param negotiator: Function that selects a renderer given a request, a dict of renderers, and a ``force`` parameter (whether to return a renderer even if the client passes an unsupported media type). :param renderers: Mapping of mediatypes to callable renderers. :param force_negotiation: Whether to return a rennderer even if the client passes an unsupported media type). """ config = app.get(CONFIG_KEY, {}) middleware = negotiation_middleware( renderers=config.get('RENDERERS', renderers), negotiator=config.get('NEGOTIATOR', negotiator), force_negotiation=config.get('FORCE_NEGOTIATION', force_negotiation) ) app.middlewares.append(middleware) return app
[ "Set", "up", "the", "negotiation", "middleware", ".", "Reads", "configuration", "from", "app", "[", "AIOHTTP_UTILS", "]", "." ]
sloria/aiohttp_utils
python
https://github.com/sloria/aiohttp_utils/blob/e5b41452f8077e7d749715606b1560f4b50e3d71/aiohttp_utils/negotiation.py#L218-L241
[ "def", "setup", "(", "app", ":", "web", ".", "Application", ",", "*", ",", "negotiator", ":", "callable", "=", "DEFAULTS", "[", "'NEGOTIATOR'", "]", ",", "renderers", ":", "OrderedDict", "=", "DEFAULTS", "[", "'RENDERERS'", "]", ",", "force_negotiation", ":", "bool", "=", "DEFAULTS", "[", "'FORCE_NEGOTIATION'", "]", ")", ":", "config", "=", "app", ".", "get", "(", "CONFIG_KEY", ",", "{", "}", ")", "middleware", "=", "negotiation_middleware", "(", "renderers", "=", "config", ".", "get", "(", "'RENDERERS'", ",", "renderers", ")", ",", "negotiator", "=", "config", ".", "get", "(", "'NEGOTIATOR'", ",", "negotiator", ")", ",", "force_negotiation", "=", "config", ".", "get", "(", "'FORCE_NEGOTIATION'", ",", "force_negotiation", ")", ")", "app", ".", "middlewares", ".", "append", "(", "middleware", ")", "return", "app" ]
e5b41452f8077e7d749715606b1560f4b50e3d71
test
add_route_context
Context manager which yields a function for adding multiple routes from a given module. Example: .. code-block:: python # myapp/articles/views.py async def list_articles(request): return web.Response(b'article list...') async def create_article(request): return web.Response(b'created article...') .. code-block:: python # myapp/app.py from myapp.articles import views with add_route_context(app, url_prefix='/api/', name_prefix='articles') as route: route('GET', '/articles/', views.list_articles) route('POST', '/articles/', views.create_article) app.router['articles.list_articles'].url() # /api/articles/ If you prefer, you can also pass module and handler names as strings. .. code-block:: python with add_route_context(app, module='myapp.articles.views', url_prefix='/api/', name_prefix='articles') as route: route('GET', '/articles/', 'list_articles') route('POST', '/articles/', 'create_article') :param app: Application to add routes to. :param module: Import path to module (str) or module object which contains the handlers. :param url_prefix: Prefix to prepend to all route paths. :param name_prefix: Prefix to prepend to all route names.
aiohttp_utils/routing.py
def add_route_context( app: web.Application, module=None, url_prefix: str=None, name_prefix: str=None ): """Context manager which yields a function for adding multiple routes from a given module. Example: .. code-block:: python # myapp/articles/views.py async def list_articles(request): return web.Response(b'article list...') async def create_article(request): return web.Response(b'created article...') .. code-block:: python # myapp/app.py from myapp.articles import views with add_route_context(app, url_prefix='/api/', name_prefix='articles') as route: route('GET', '/articles/', views.list_articles) route('POST', '/articles/', views.create_article) app.router['articles.list_articles'].url() # /api/articles/ If you prefer, you can also pass module and handler names as strings. .. code-block:: python with add_route_context(app, module='myapp.articles.views', url_prefix='/api/', name_prefix='articles') as route: route('GET', '/articles/', 'list_articles') route('POST', '/articles/', 'create_article') :param app: Application to add routes to. :param module: Import path to module (str) or module object which contains the handlers. :param url_prefix: Prefix to prepend to all route paths. :param name_prefix: Prefix to prepend to all route names. """ if isinstance(module, (str, bytes)): module = importlib.import_module(module) def add_route(method, path, handler, name=None): """ :param str method: HTTP method. :param str path: Path for the route. :param handler: A handler function or a name of a handler function contained in `module`. :param str name: Name for the route. If `None`, defaults to the handler's function name. """ if isinstance(handler, (str, bytes)): if not module: raise ValueError( 'Must pass module to add_route_context if passing handler name strings.' ) name = name or handler handler = getattr(module, handler) else: name = name or handler.__name__ path = make_path(path, url_prefix) name = '.'.join((name_prefix, name)) if name_prefix else name return app.router.add_route(method, path, handler, name=name) yield add_route
def add_route_context( app: web.Application, module=None, url_prefix: str=None, name_prefix: str=None ): """Context manager which yields a function for adding multiple routes from a given module. Example: .. code-block:: python # myapp/articles/views.py async def list_articles(request): return web.Response(b'article list...') async def create_article(request): return web.Response(b'created article...') .. code-block:: python # myapp/app.py from myapp.articles import views with add_route_context(app, url_prefix='/api/', name_prefix='articles') as route: route('GET', '/articles/', views.list_articles) route('POST', '/articles/', views.create_article) app.router['articles.list_articles'].url() # /api/articles/ If you prefer, you can also pass module and handler names as strings. .. code-block:: python with add_route_context(app, module='myapp.articles.views', url_prefix='/api/', name_prefix='articles') as route: route('GET', '/articles/', 'list_articles') route('POST', '/articles/', 'create_article') :param app: Application to add routes to. :param module: Import path to module (str) or module object which contains the handlers. :param url_prefix: Prefix to prepend to all route paths. :param name_prefix: Prefix to prepend to all route names. """ if isinstance(module, (str, bytes)): module = importlib.import_module(module) def add_route(method, path, handler, name=None): """ :param str method: HTTP method. :param str path: Path for the route. :param handler: A handler function or a name of a handler function contained in `module`. :param str name: Name for the route. If `None`, defaults to the handler's function name. """ if isinstance(handler, (str, bytes)): if not module: raise ValueError( 'Must pass module to add_route_context if passing handler name strings.' ) name = name or handler handler = getattr(module, handler) else: name = name or handler.__name__ path = make_path(path, url_prefix) name = '.'.join((name_prefix, name)) if name_prefix else name return app.router.add_route(method, path, handler, name=name) yield add_route
[ "Context", "manager", "which", "yields", "a", "function", "for", "adding", "multiple", "routes", "from", "a", "given", "module", "." ]
sloria/aiohttp_utils
python
https://github.com/sloria/aiohttp_utils/blob/e5b41452f8077e7d749715606b1560f4b50e3d71/aiohttp_utils/routing.py#L85-L150
[ "def", "add_route_context", "(", "app", ":", "web", ".", "Application", ",", "module", "=", "None", ",", "url_prefix", ":", "str", "=", "None", ",", "name_prefix", ":", "str", "=", "None", ")", ":", "if", "isinstance", "(", "module", ",", "(", "str", ",", "bytes", ")", ")", ":", "module", "=", "importlib", ".", "import_module", "(", "module", ")", "def", "add_route", "(", "method", ",", "path", ",", "handler", ",", "name", "=", "None", ")", ":", "\"\"\"\n :param str method: HTTP method.\n :param str path: Path for the route.\n :param handler: A handler function or a name of a handler function contained\n in `module`.\n :param str name: Name for the route. If `None`, defaults to the handler's\n function name.\n \"\"\"", "if", "isinstance", "(", "handler", ",", "(", "str", ",", "bytes", ")", ")", ":", "if", "not", "module", ":", "raise", "ValueError", "(", "'Must pass module to add_route_context if passing handler name strings.'", ")", "name", "=", "name", "or", "handler", "handler", "=", "getattr", "(", "module", ",", "handler", ")", "else", ":", "name", "=", "name", "or", "handler", ".", "__name__", "path", "=", "make_path", "(", "path", ",", "url_prefix", ")", "name", "=", "'.'", ".", "join", "(", "(", "name_prefix", ",", "name", ")", ")", "if", "name_prefix", "else", "name", "return", "app", ".", "router", ".", "add_route", "(", "method", ",", "path", ",", "handler", ",", "name", "=", "name", ")", "yield", "add_route" ]
e5b41452f8077e7d749715606b1560f4b50e3d71
test
add_resource_context
Context manager which yields a function for adding multiple resources from a given module to an app using `ResourceRouter <aiohttp_utils.routing.ResourceRouter>`. Example: .. code-block:: python # myapp/articles/views.py class ArticleList: async def get(self, request): return web.Response(b'article list...') class ArticleDetail: async def get(self, request): return web.Response(b'article detail...') .. code-block:: python # myapp/app.py from myapp.articles import views with add_resource_context(app, url_prefix='/api/') as route: route('/articles/', views.ArticleList()) route('/articles/{pk}', views.ArticleDetail()) app.router['ArticleList:get'].url() # /api/articles/ app.router['ArticleDetail:get'].url(parts={'pk': 42}) # /api/articles/42 If you prefer, you can also pass module and class names as strings. :: with add_resource_context(app, module='myapp.articles.views', url_prefix='/api/') as route: route('/articles/', 'ArticleList') route('/articles/{pk}', 'ArticleDetail') .. note:: If passing class names, the resource classes will be instantiated with no arguments. You can change this behavior by overriding ``make_resource``. .. code-block:: python # myapp/authors/views.py class AuthorList: def __init__(self, db): self.db = db async def get(self, request): # Fetch authors from self.db... .. code-block:: python # myapp/app.py from myapp.database import db with add_resource_context(app, module='myapp.authors.views', url_prefix='/api/', make_resource=lambda cls: cls(db=db)) as route: route('/authors/', 'AuthorList') :param app: Application to add routes to. :param resource: Import path to module (str) or module object which contains the resource classes. :param url_prefix: Prefix to prepend to all route paths. :param name_prefix: Prefix to prepend to all route names. :param make_resource: Function which receives a resource class and returns a resource instance.
aiohttp_utils/routing.py
def add_resource_context( app: web.Application, module=None, url_prefix: str=None, name_prefix: str=None, make_resource=lambda cls: cls() ): """Context manager which yields a function for adding multiple resources from a given module to an app using `ResourceRouter <aiohttp_utils.routing.ResourceRouter>`. Example: .. code-block:: python # myapp/articles/views.py class ArticleList: async def get(self, request): return web.Response(b'article list...') class ArticleDetail: async def get(self, request): return web.Response(b'article detail...') .. code-block:: python # myapp/app.py from myapp.articles import views with add_resource_context(app, url_prefix='/api/') as route: route('/articles/', views.ArticleList()) route('/articles/{pk}', views.ArticleDetail()) app.router['ArticleList:get'].url() # /api/articles/ app.router['ArticleDetail:get'].url(parts={'pk': 42}) # /api/articles/42 If you prefer, you can also pass module and class names as strings. :: with add_resource_context(app, module='myapp.articles.views', url_prefix='/api/') as route: route('/articles/', 'ArticleList') route('/articles/{pk}', 'ArticleDetail') .. note:: If passing class names, the resource classes will be instantiated with no arguments. You can change this behavior by overriding ``make_resource``. .. code-block:: python # myapp/authors/views.py class AuthorList: def __init__(self, db): self.db = db async def get(self, request): # Fetch authors from self.db... .. code-block:: python # myapp/app.py from myapp.database import db with add_resource_context(app, module='myapp.authors.views', url_prefix='/api/', make_resource=lambda cls: cls(db=db)) as route: route('/authors/', 'AuthorList') :param app: Application to add routes to. :param resource: Import path to module (str) or module object which contains the resource classes. :param url_prefix: Prefix to prepend to all route paths. :param name_prefix: Prefix to prepend to all route names. :param make_resource: Function which receives a resource class and returns a resource instance. """ assert isinstance(app.router, ResourceRouter), 'app must be using ResourceRouter' if isinstance(module, (str, bytes)): module = importlib.import_module(module) def get_base_name(resource, method_name, names): return names.get(method_name, app.router.get_default_handler_name(resource, method_name)) default_make_resource = make_resource def add_route( path: str, resource, names: Mapping=None, make_resource=None ): make_resource = make_resource or default_make_resource names = names or {} if isinstance(resource, (str, bytes)): if not module: raise ValueError( 'Must pass module to add_route_context if passing resource name strings.' ) resource_cls = getattr(module, resource) resource = make_resource(resource_cls) path = make_path(path, url_prefix) if name_prefix: supported_method_names = get_supported_method_names(resource) names = { method_name: '.'.join( (name_prefix, get_base_name(resource, method_name, names=names)) ) for method_name in supported_method_names } return app.router.add_resource_object(path, resource, names=names) yield add_route
def add_resource_context( app: web.Application, module=None, url_prefix: str=None, name_prefix: str=None, make_resource=lambda cls: cls() ): """Context manager which yields a function for adding multiple resources from a given module to an app using `ResourceRouter <aiohttp_utils.routing.ResourceRouter>`. Example: .. code-block:: python # myapp/articles/views.py class ArticleList: async def get(self, request): return web.Response(b'article list...') class ArticleDetail: async def get(self, request): return web.Response(b'article detail...') .. code-block:: python # myapp/app.py from myapp.articles import views with add_resource_context(app, url_prefix='/api/') as route: route('/articles/', views.ArticleList()) route('/articles/{pk}', views.ArticleDetail()) app.router['ArticleList:get'].url() # /api/articles/ app.router['ArticleDetail:get'].url(parts={'pk': 42}) # /api/articles/42 If you prefer, you can also pass module and class names as strings. :: with add_resource_context(app, module='myapp.articles.views', url_prefix='/api/') as route: route('/articles/', 'ArticleList') route('/articles/{pk}', 'ArticleDetail') .. note:: If passing class names, the resource classes will be instantiated with no arguments. You can change this behavior by overriding ``make_resource``. .. code-block:: python # myapp/authors/views.py class AuthorList: def __init__(self, db): self.db = db async def get(self, request): # Fetch authors from self.db... .. code-block:: python # myapp/app.py from myapp.database import db with add_resource_context(app, module='myapp.authors.views', url_prefix='/api/', make_resource=lambda cls: cls(db=db)) as route: route('/authors/', 'AuthorList') :param app: Application to add routes to. :param resource: Import path to module (str) or module object which contains the resource classes. :param url_prefix: Prefix to prepend to all route paths. :param name_prefix: Prefix to prepend to all route names. :param make_resource: Function which receives a resource class and returns a resource instance. """ assert isinstance(app.router, ResourceRouter), 'app must be using ResourceRouter' if isinstance(module, (str, bytes)): module = importlib.import_module(module) def get_base_name(resource, method_name, names): return names.get(method_name, app.router.get_default_handler_name(resource, method_name)) default_make_resource = make_resource def add_route( path: str, resource, names: Mapping=None, make_resource=None ): make_resource = make_resource or default_make_resource names = names or {} if isinstance(resource, (str, bytes)): if not module: raise ValueError( 'Must pass module to add_route_context if passing resource name strings.' ) resource_cls = getattr(module, resource) resource = make_resource(resource_cls) path = make_path(path, url_prefix) if name_prefix: supported_method_names = get_supported_method_names(resource) names = { method_name: '.'.join( (name_prefix, get_base_name(resource, method_name, names=names)) ) for method_name in supported_method_names } return app.router.add_resource_object(path, resource, names=names) yield add_route
[ "Context", "manager", "which", "yields", "a", "function", "for", "adding", "multiple", "resources", "from", "a", "given", "module", "to", "an", "app", "using", "ResourceRouter", "<aiohttp_utils", ".", "routing", ".", "ResourceRouter", ">", "." ]
sloria/aiohttp_utils
python
https://github.com/sloria/aiohttp_utils/blob/e5b41452f8077e7d749715606b1560f4b50e3d71/aiohttp_utils/routing.py#L160-L264
[ "def", "add_resource_context", "(", "app", ":", "web", ".", "Application", ",", "module", "=", "None", ",", "url_prefix", ":", "str", "=", "None", ",", "name_prefix", ":", "str", "=", "None", ",", "make_resource", "=", "lambda", "cls", ":", "cls", "(", ")", ")", ":", "assert", "isinstance", "(", "app", ".", "router", ",", "ResourceRouter", ")", ",", "'app must be using ResourceRouter'", "if", "isinstance", "(", "module", ",", "(", "str", ",", "bytes", ")", ")", ":", "module", "=", "importlib", ".", "import_module", "(", "module", ")", "def", "get_base_name", "(", "resource", ",", "method_name", ",", "names", ")", ":", "return", "names", ".", "get", "(", "method_name", ",", "app", ".", "router", ".", "get_default_handler_name", "(", "resource", ",", "method_name", ")", ")", "default_make_resource", "=", "make_resource", "def", "add_route", "(", "path", ":", "str", ",", "resource", ",", "names", ":", "Mapping", "=", "None", ",", "make_resource", "=", "None", ")", ":", "make_resource", "=", "make_resource", "or", "default_make_resource", "names", "=", "names", "or", "{", "}", "if", "isinstance", "(", "resource", ",", "(", "str", ",", "bytes", ")", ")", ":", "if", "not", "module", ":", "raise", "ValueError", "(", "'Must pass module to add_route_context if passing resource name strings.'", ")", "resource_cls", "=", "getattr", "(", "module", ",", "resource", ")", "resource", "=", "make_resource", "(", "resource_cls", ")", "path", "=", "make_path", "(", "path", ",", "url_prefix", ")", "if", "name_prefix", ":", "supported_method_names", "=", "get_supported_method_names", "(", "resource", ")", "names", "=", "{", "method_name", ":", "'.'", ".", "join", "(", "(", "name_prefix", ",", "get_base_name", "(", "resource", ",", "method_name", ",", "names", "=", "names", ")", ")", ")", "for", "method_name", "in", "supported_method_names", "}", "return", "app", ".", "router", ".", "add_resource_object", "(", "path", ",", "resource", ",", "names", "=", "names", ")", "yield", "add_route" ]
e5b41452f8077e7d749715606b1560f4b50e3d71
test
ResourceRouter.add_resource_object
Add routes by an resource instance's methods. :param path: route path. Should be started with slash (``'/'``). :param resource: A "resource" instance. May be an instance of a plain object. :param methods: Methods (strings) to register. :param names: Dictionary of ``name`` overrides.
aiohttp_utils/routing.py
def add_resource_object(self, path: str, resource, methods: tuple=tuple(), names: Mapping=None): """Add routes by an resource instance's methods. :param path: route path. Should be started with slash (``'/'``). :param resource: A "resource" instance. May be an instance of a plain object. :param methods: Methods (strings) to register. :param names: Dictionary of ``name`` overrides. """ names = names or {} if methods: method_names = methods else: method_names = self.HTTP_METHOD_NAMES for method_name in method_names: handler = getattr(resource, method_name, None) if handler: name = names.get(method_name, self.get_default_handler_name(resource, method_name)) self.add_route(method_name.upper(), path, handler, name=name)
def add_resource_object(self, path: str, resource, methods: tuple=tuple(), names: Mapping=None): """Add routes by an resource instance's methods. :param path: route path. Should be started with slash (``'/'``). :param resource: A "resource" instance. May be an instance of a plain object. :param methods: Methods (strings) to register. :param names: Dictionary of ``name`` overrides. """ names = names or {} if methods: method_names = methods else: method_names = self.HTTP_METHOD_NAMES for method_name in method_names: handler = getattr(resource, method_name, None) if handler: name = names.get(method_name, self.get_default_handler_name(resource, method_name)) self.add_route(method_name.upper(), path, handler, name=name)
[ "Add", "routes", "by", "an", "resource", "instance", "s", "methods", "." ]
sloria/aiohttp_utils
python
https://github.com/sloria/aiohttp_utils/blob/e5b41452f8077e7d749715606b1560f4b50e3d71/aiohttp_utils/routing.py#L60-L77
[ "def", "add_resource_object", "(", "self", ",", "path", ":", "str", ",", "resource", ",", "methods", ":", "tuple", "=", "tuple", "(", ")", ",", "names", ":", "Mapping", "=", "None", ")", ":", "names", "=", "names", "or", "{", "}", "if", "methods", ":", "method_names", "=", "methods", "else", ":", "method_names", "=", "self", ".", "HTTP_METHOD_NAMES", "for", "method_name", "in", "method_names", ":", "handler", "=", "getattr", "(", "resource", ",", "method_name", ",", "None", ")", "if", "handler", ":", "name", "=", "names", ".", "get", "(", "method_name", ",", "self", ".", "get_default_handler_name", "(", "resource", ",", "method_name", ")", ")", "self", ".", "add_route", "(", "method_name", ".", "upper", "(", ")", ",", "path", ",", "handler", ",", "name", "=", "name", ")" ]
e5b41452f8077e7d749715606b1560f4b50e3d71
test
run
Run an `aiohttp.web.Application` using gunicorn. :param app: The app to run. :param str app_uri: Import path to `app`. Takes the form ``$(MODULE_NAME):$(VARIABLE_NAME)``. The module name can be a full dotted path. The variable name refers to the `aiohttp.web.Application` instance. This argument is required if ``reload=True``. :param str host: Hostname to listen on. :param int port: Port of the server. :param bool reload: Whether to reload the server on a code change. If not set, will take the same value as ``app.debug``. **EXPERIMENTAL**. :param \*\*kwargs: Extra configuration options to set on the ``GunicornApp's`` config object.
aiohttp_utils/runner.py
def run(app: web.Application, **kwargs): """Run an `aiohttp.web.Application` using gunicorn. :param app: The app to run. :param str app_uri: Import path to `app`. Takes the form ``$(MODULE_NAME):$(VARIABLE_NAME)``. The module name can be a full dotted path. The variable name refers to the `aiohttp.web.Application` instance. This argument is required if ``reload=True``. :param str host: Hostname to listen on. :param int port: Port of the server. :param bool reload: Whether to reload the server on a code change. If not set, will take the same value as ``app.debug``. **EXPERIMENTAL**. :param \*\*kwargs: Extra configuration options to set on the ``GunicornApp's`` config object. """ runner = Runner(app, **kwargs) runner.run()
def run(app: web.Application, **kwargs): """Run an `aiohttp.web.Application` using gunicorn. :param app: The app to run. :param str app_uri: Import path to `app`. Takes the form ``$(MODULE_NAME):$(VARIABLE_NAME)``. The module name can be a full dotted path. The variable name refers to the `aiohttp.web.Application` instance. This argument is required if ``reload=True``. :param str host: Hostname to listen on. :param int port: Port of the server. :param bool reload: Whether to reload the server on a code change. If not set, will take the same value as ``app.debug``. **EXPERIMENTAL**. :param \*\*kwargs: Extra configuration options to set on the ``GunicornApp's`` config object. """ runner = Runner(app, **kwargs) runner.run()
[ "Run", "an", "aiohttp", ".", "web", ".", "Application", "using", "gunicorn", "." ]
sloria/aiohttp_utils
python
https://github.com/sloria/aiohttp_utils/blob/e5b41452f8077e7d749715606b1560f4b50e3d71/aiohttp_utils/runner.py#L99-L117
[ "def", "run", "(", "app", ":", "web", ".", "Application", ",", "*", "*", "kwargs", ")", ":", "runner", "=", "Runner", "(", "app", ",", "*", "*", "kwargs", ")", "runner", ".", "run", "(", ")" ]
e5b41452f8077e7d749715606b1560f4b50e3d71
test
GCMDevice.send_message
Sends a push notification to this device via GCM
instapush/models/base.py
def send_message(self, message, **kwargs): """ Sends a push notification to this device via GCM """ from ..libs.gcm import gcm_send_message data = kwargs.pop("extra", {}) if message is not None: data["message"] = message return gcm_send_message(registration_id=self.registration_id, data=data, **kwargs)
def send_message(self, message, **kwargs): """ Sends a push notification to this device via GCM """ from ..libs.gcm import gcm_send_message data = kwargs.pop("extra", {}) if message is not None: data["message"] = message return gcm_send_message(registration_id=self.registration_id, data=data, **kwargs)
[ "Sends", "a", "push", "notification", "to", "this", "device", "via", "GCM" ]
amyth/django-instapush
python
https://github.com/amyth/django-instapush/blob/f8643a2e342fc73a16c95dff79c3daac8ce4b034/instapush/models/base.py#L63-L74
[ "def", "send_message", "(", "self", ",", "message", ",", "*", "*", "kwargs", ")", ":", "from", ".", ".", "libs", ".", "gcm", "import", "gcm_send_message", "data", "=", "kwargs", ".", "pop", "(", "\"extra\"", ",", "{", "}", ")", "if", "message", "is", "not", "None", ":", "data", "[", "\"message\"", "]", "=", "message", "return", "gcm_send_message", "(", "registration_id", "=", "self", ".", "registration_id", ",", "data", "=", "data", ",", "*", "*", "kwargs", ")" ]
f8643a2e342fc73a16c95dff79c3daac8ce4b034
test
apns_send_bulk_message
Sends an APNS notification to one or more registration_ids. The registration_ids argument needs to be a list. Note that if set alert should always be a string. If it is not set, it won't be included in the notification. You will need to pass None to this for silent notifications.
instapush/libs/apns.py
def apns_send_bulk_message(registration_ids, alert, **kwargs): """ Sends an APNS notification to one or more registration_ids. The registration_ids argument needs to be a list. Note that if set alert should always be a string. If it is not set, it won't be included in the notification. You will need to pass None to this for silent notifications. """ with closing(_apns_create_socket_to_push(**kwargs)) as socket: for identifier, registration_id in enumerate(registration_ids): _apns_send(registration_id, alert, identifier=identifier, socket=socket, **kwargs) _apns_check_errors(socket)
def apns_send_bulk_message(registration_ids, alert, **kwargs): """ Sends an APNS notification to one or more registration_ids. The registration_ids argument needs to be a list. Note that if set alert should always be a string. If it is not set, it won't be included in the notification. You will need to pass None to this for silent notifications. """ with closing(_apns_create_socket_to_push(**kwargs)) as socket: for identifier, registration_id in enumerate(registration_ids): _apns_send(registration_id, alert, identifier=identifier, socket=socket, **kwargs) _apns_check_errors(socket)
[ "Sends", "an", "APNS", "notification", "to", "one", "or", "more", "registration_ids", ".", "The", "registration_ids", "argument", "needs", "to", "be", "a", "list", "." ]
amyth/django-instapush
python
https://github.com/amyth/django-instapush/blob/f8643a2e342fc73a16c95dff79c3daac8ce4b034/instapush/libs/apns.py#L230-L242
[ "def", "apns_send_bulk_message", "(", "registration_ids", ",", "alert", ",", "*", "*", "kwargs", ")", ":", "with", "closing", "(", "_apns_create_socket_to_push", "(", "*", "*", "kwargs", ")", ")", "as", "socket", ":", "for", "identifier", ",", "registration_id", "in", "enumerate", "(", "registration_ids", ")", ":", "_apns_send", "(", "registration_id", ",", "alert", ",", "identifier", "=", "identifier", ",", "socket", "=", "socket", ",", "*", "*", "kwargs", ")", "_apns_check_errors", "(", "socket", ")" ]
f8643a2e342fc73a16c95dff79c3daac8ce4b034
test
apns_fetch_inactive_ids
Queries the APNS server for id's that are no longer active since the last fetch
instapush/libs/apns.py
def apns_fetch_inactive_ids(): """ Queries the APNS server for id's that are no longer active since the last fetch """ with closing(_apns_create_socket_to_feedback()) as socket: inactive_ids = [] for _, registration_id in _apns_receive_feedback(socket): inactive_ids.append(codecs.encode(registration_id, 'hex_codec')) return inactive_ids
def apns_fetch_inactive_ids(): """ Queries the APNS server for id's that are no longer active since the last fetch """ with closing(_apns_create_socket_to_feedback()) as socket: inactive_ids = [] for _, registration_id in _apns_receive_feedback(socket): inactive_ids.append(codecs.encode(registration_id, 'hex_codec')) return inactive_ids
[ "Queries", "the", "APNS", "server", "for", "id", "s", "that", "are", "no", "longer", "active", "since", "the", "last", "fetch" ]
amyth/django-instapush
python
https://github.com/amyth/django-instapush/blob/f8643a2e342fc73a16c95dff79c3daac8ce4b034/instapush/libs/apns.py#L245-L256
[ "def", "apns_fetch_inactive_ids", "(", ")", ":", "with", "closing", "(", "_apns_create_socket_to_feedback", "(", ")", ")", "as", "socket", ":", "inactive_ids", "=", "[", "]", "for", "_", ",", "registration_id", "in", "_apns_receive_feedback", "(", "socket", ")", ":", "inactive_ids", ".", "append", "(", "codecs", ".", "encode", "(", "registration_id", ",", "'hex_codec'", ")", ")", "return", "inactive_ids" ]
f8643a2e342fc73a16c95dff79c3daac8ce4b034
test
gcm_send_message
Standalone method to send a single gcm notification
instapush/libs/gcm.py
def gcm_send_message(registration_id, data, encoding='utf-8', **kwargs): """ Standalone method to send a single gcm notification """ messenger = GCMMessenger(registration_id, data, encoding=encoding, **kwargs) return messenger.send_plain()
def gcm_send_message(registration_id, data, encoding='utf-8', **kwargs): """ Standalone method to send a single gcm notification """ messenger = GCMMessenger(registration_id, data, encoding=encoding, **kwargs) return messenger.send_plain()
[ "Standalone", "method", "to", "send", "a", "single", "gcm", "notification" ]
amyth/django-instapush
python
https://github.com/amyth/django-instapush/blob/f8643a2e342fc73a16c95dff79c3daac8ce4b034/instapush/libs/gcm.py#L150-L156
[ "def", "gcm_send_message", "(", "registration_id", ",", "data", ",", "encoding", "=", "'utf-8'", ",", "*", "*", "kwargs", ")", ":", "messenger", "=", "GCMMessenger", "(", "registration_id", ",", "data", ",", "encoding", "=", "encoding", ",", "*", "*", "kwargs", ")", "return", "messenger", ".", "send_plain", "(", ")" ]
f8643a2e342fc73a16c95dff79c3daac8ce4b034
test
gcm_send_bulk_message
Standalone method to send bulk gcm notifications
instapush/libs/gcm.py
def gcm_send_bulk_message(registration_ids, data, encoding='utf-8', **kwargs): """ Standalone method to send bulk gcm notifications """ messenger = GCMMessenger(registration_ids, data, encoding=encoding, **kwargs) return messenger.send_bulk()
def gcm_send_bulk_message(registration_ids, data, encoding='utf-8', **kwargs): """ Standalone method to send bulk gcm notifications """ messenger = GCMMessenger(registration_ids, data, encoding=encoding, **kwargs) return messenger.send_bulk()
[ "Standalone", "method", "to", "send", "bulk", "gcm", "notifications" ]
amyth/django-instapush
python
https://github.com/amyth/django-instapush/blob/f8643a2e342fc73a16c95dff79c3daac8ce4b034/instapush/libs/gcm.py#L159-L165
[ "def", "gcm_send_bulk_message", "(", "registration_ids", ",", "data", ",", "encoding", "=", "'utf-8'", ",", "*", "*", "kwargs", ")", ":", "messenger", "=", "GCMMessenger", "(", "registration_ids", ",", "data", ",", "encoding", "=", "encoding", ",", "*", "*", "kwargs", ")", "return", "messenger", ".", "send_bulk", "(", ")" ]
f8643a2e342fc73a16c95dff79c3daac8ce4b034
test
GCMMessenger.send_plain
Sends a text/plain GCM message
instapush/libs/gcm.py
def send_plain(self): """ Sends a text/plain GCM message """ values = {"registration_id": self._registration_id} for key, val in self._data.items(): values["data.%s" % (key)] = val.encode(self.encoding) for key, val in self._kwargs.items(): if val and isinstance(val, bool): val = 1 values[key] = val data = urlencode(sorted(values.items())).encode(self.encoding) result = self._send(data, "application/x-www-form-urlencoded;charset=UTF-8") if result.startswith("Error="): if result in ("Error=NotRegistered", "Error=InvalidRegistration"): ## TODO: deactivate the unregistered device return result raise GCMPushError(result) return result
def send_plain(self): """ Sends a text/plain GCM message """ values = {"registration_id": self._registration_id} for key, val in self._data.items(): values["data.%s" % (key)] = val.encode(self.encoding) for key, val in self._kwargs.items(): if val and isinstance(val, bool): val = 1 values[key] = val data = urlencode(sorted(values.items())).encode(self.encoding) result = self._send(data, "application/x-www-form-urlencoded;charset=UTF-8") if result.startswith("Error="): if result in ("Error=NotRegistered", "Error=InvalidRegistration"): ## TODO: deactivate the unregistered device return result raise GCMPushError(result) return result
[ "Sends", "a", "text", "/", "plain", "GCM", "message" ]
amyth/django-instapush
python
https://github.com/amyth/django-instapush/blob/f8643a2e342fc73a16c95dff79c3daac8ce4b034/instapush/libs/gcm.py#L56-L81
[ "def", "send_plain", "(", "self", ")", ":", "values", "=", "{", "\"registration_id\"", ":", "self", ".", "_registration_id", "}", "for", "key", ",", "val", "in", "self", ".", "_data", ".", "items", "(", ")", ":", "values", "[", "\"data.%s\"", "%", "(", "key", ")", "]", "=", "val", ".", "encode", "(", "self", ".", "encoding", ")", "for", "key", ",", "val", "in", "self", ".", "_kwargs", ".", "items", "(", ")", ":", "if", "val", "and", "isinstance", "(", "val", ",", "bool", ")", ":", "val", "=", "1", "values", "[", "key", "]", "=", "val", "data", "=", "urlencode", "(", "sorted", "(", "values", ".", "items", "(", ")", ")", ")", ".", "encode", "(", "self", ".", "encoding", ")", "result", "=", "self", ".", "_send", "(", "data", ",", "\"application/x-www-form-urlencoded;charset=UTF-8\"", ")", "if", "result", ".", "startswith", "(", "\"Error=\"", ")", ":", "if", "result", "in", "(", "\"Error=NotRegistered\"", ",", "\"Error=InvalidRegistration\"", ")", ":", "## TODO: deactivate the unregistered device", "return", "result", "raise", "GCMPushError", "(", "result", ")", "return", "result" ]
f8643a2e342fc73a16c95dff79c3daac8ce4b034
test
GCMMessenger.send_json
Sends a json GCM message
instapush/libs/gcm.py
def send_json(self, ids=None): """ Sends a json GCM message """ items = ids or self._registration_id values = {"registration_ids": items} if self._data is not None: values["data"] = self._data for key, val in self._kwargs.items(): if val: values[key] = val data = json.dumps(values, separators=(",", ":"), sort_keys=True).encode( self.encoding) result = json.loads(self._send(data, "application/json")) if ("failure" in result) and (result["failure"]): unregistered = [] throw_error = False for index, error in enumerate(result.get("results", [])): error = error.get("error", "") if error in ("NotRegistered", "InvalidRegistration"): unregistered.append(items[index]) elif error != "": throw_error = True self.deactivate_unregistered_devices(unregistered) if throw_error: raise GCMPushError(result) return result
def send_json(self, ids=None): """ Sends a json GCM message """ items = ids or self._registration_id values = {"registration_ids": items} if self._data is not None: values["data"] = self._data for key, val in self._kwargs.items(): if val: values[key] = val data = json.dumps(values, separators=(",", ":"), sort_keys=True).encode( self.encoding) result = json.loads(self._send(data, "application/json")) if ("failure" in result) and (result["failure"]): unregistered = [] throw_error = False for index, error in enumerate(result.get("results", [])): error = error.get("error", "") if error in ("NotRegistered", "InvalidRegistration"): unregistered.append(items[index]) elif error != "": throw_error = True self.deactivate_unregistered_devices(unregistered) if throw_error: raise GCMPushError(result) return result
[ "Sends", "a", "json", "GCM", "message" ]
amyth/django-instapush
python
https://github.com/amyth/django-instapush/blob/f8643a2e342fc73a16c95dff79c3daac8ce4b034/instapush/libs/gcm.py#L83-L118
[ "def", "send_json", "(", "self", ",", "ids", "=", "None", ")", ":", "items", "=", "ids", "or", "self", ".", "_registration_id", "values", "=", "{", "\"registration_ids\"", ":", "items", "}", "if", "self", ".", "_data", "is", "not", "None", ":", "values", "[", "\"data\"", "]", "=", "self", ".", "_data", "for", "key", ",", "val", "in", "self", ".", "_kwargs", ".", "items", "(", ")", ":", "if", "val", ":", "values", "[", "key", "]", "=", "val", "data", "=", "json", ".", "dumps", "(", "values", ",", "separators", "=", "(", "\",\"", ",", "\":\"", ")", ",", "sort_keys", "=", "True", ")", ".", "encode", "(", "self", ".", "encoding", ")", "result", "=", "json", ".", "loads", "(", "self", ".", "_send", "(", "data", ",", "\"application/json\"", ")", ")", "if", "(", "\"failure\"", "in", "result", ")", "and", "(", "result", "[", "\"failure\"", "]", ")", ":", "unregistered", "=", "[", "]", "throw_error", "=", "False", "for", "index", ",", "error", "in", "enumerate", "(", "result", ".", "get", "(", "\"results\"", ",", "[", "]", ")", ")", ":", "error", "=", "error", ".", "get", "(", "\"error\"", ",", "\"\"", ")", "if", "error", "in", "(", "\"NotRegistered\"", ",", "\"InvalidRegistration\"", ")", ":", "unregistered", ".", "append", "(", "items", "[", "index", "]", ")", "elif", "error", "!=", "\"\"", ":", "throw_error", "=", "True", "self", ".", "deactivate_unregistered_devices", "(", "unregistered", ")", "if", "throw_error", ":", "raise", "GCMPushError", "(", "result", ")", "return", "result" ]
f8643a2e342fc73a16c95dff79c3daac8ce4b034
test
GCMMessenger._send
Sends a GCM message with the given content type
instapush/libs/gcm.py
def _send(self, data, content_type): """ Sends a GCM message with the given content type """ headers = { "Content-Type": content_type, "Authorization": "key=%s" % (self.api_key), "Content-Length": str(len(data)) } request = Request(self.api_url, data, headers) return urlopen(request).read().decode(self.encoding)
def _send(self, data, content_type): """ Sends a GCM message with the given content type """ headers = { "Content-Type": content_type, "Authorization": "key=%s" % (self.api_key), "Content-Length": str(len(data)) } request = Request(self.api_url, data, headers) return urlopen(request).read().decode(self.encoding)
[ "Sends", "a", "GCM", "message", "with", "the", "given", "content", "type" ]
amyth/django-instapush
python
https://github.com/amyth/django-instapush/blob/f8643a2e342fc73a16c95dff79c3daac8ce4b034/instapush/libs/gcm.py#L135-L147
[ "def", "_send", "(", "self", ",", "data", ",", "content_type", ")", ":", "headers", "=", "{", "\"Content-Type\"", ":", "content_type", ",", "\"Authorization\"", ":", "\"key=%s\"", "%", "(", "self", ".", "api_key", ")", ",", "\"Content-Length\"", ":", "str", "(", "len", "(", "data", ")", ")", "}", "request", "=", "Request", "(", "self", ".", "api_url", ",", "data", ",", "headers", ")", "return", "urlopen", "(", "request", ")", ".", "read", "(", ")", ".", "decode", "(", "self", ".", "encoding", ")" ]
f8643a2e342fc73a16c95dff79c3daac8ce4b034
test
get_model
Returns the instance of the given module location.
instapush/utils.py
def get_model(module_location): """ Returns the instance of the given module location. """ if not isinstance(module_location, (str, unicode)): raise ValueError("The value provided should either be a string or "\ "unicode instance. The value '%s' provided was %s "\ "rather." % (module_location, type(module_location))) try: name_split = module_location.split(".") class_name = name_split.pop(-1) if not len(name_split): raise ValueError("The value should provide the module location "\ "joined by '.' e.g. for model named 'test' in " "/app/module.py, The value should be 'app.module.test'") module_location = ".".join(name_split) module = importlib.import_module(module_location) cls = getattr(module, class_name) return cls except AttributeError: pass
def get_model(module_location): """ Returns the instance of the given module location. """ if not isinstance(module_location, (str, unicode)): raise ValueError("The value provided should either be a string or "\ "unicode instance. The value '%s' provided was %s "\ "rather." % (module_location, type(module_location))) try: name_split = module_location.split(".") class_name = name_split.pop(-1) if not len(name_split): raise ValueError("The value should provide the module location "\ "joined by '.' e.g. for model named 'test' in " "/app/module.py, The value should be 'app.module.test'") module_location = ".".join(name_split) module = importlib.import_module(module_location) cls = getattr(module, class_name) return cls except AttributeError: pass
[ "Returns", "the", "instance", "of", "the", "given", "module", "location", "." ]
amyth/django-instapush
python
https://github.com/amyth/django-instapush/blob/f8643a2e342fc73a16c95dff79c3daac8ce4b034/instapush/utils.py#L4-L30
[ "def", "get_model", "(", "module_location", ")", ":", "if", "not", "isinstance", "(", "module_location", ",", "(", "str", ",", "unicode", ")", ")", ":", "raise", "ValueError", "(", "\"The value provided should either be a string or \"", "\"unicode instance. The value '%s' provided was %s \"", "\"rather.\"", "%", "(", "module_location", ",", "type", "(", "module_location", ")", ")", ")", "try", ":", "name_split", "=", "module_location", ".", "split", "(", "\".\"", ")", "class_name", "=", "name_split", ".", "pop", "(", "-", "1", ")", "if", "not", "len", "(", "name_split", ")", ":", "raise", "ValueError", "(", "\"The value should provide the module location \"", "\"joined by '.' e.g. for model named 'test' in \"", "\"/app/module.py, The value should be 'app.module.test'\"", ")", "module_location", "=", "\".\"", ".", "join", "(", "name_split", ")", "module", "=", "importlib", ".", "import_module", "(", "module_location", ")", "cls", "=", "getattr", "(", "module", ",", "class_name", ")", "return", "cls", "except", "AttributeError", ":", "pass" ]
f8643a2e342fc73a16c95dff79c3daac8ce4b034
test
plot_line_power
obj: case or network
psst/plot/network.py
def plot_line_power(obj, results, hour, ax=None): ''' obj: case or network ''' if ax is None: fig, ax = plt.subplots(1, 1, figsize=(16, 10)) ax.axis('off') case, network = _return_case_network(obj) network.draw_buses(ax=ax) network.draw_loads(ax=ax) network.draw_generators(ax=ax) network.draw_connections('gen_to_bus', ax=ax) network.draw_connections('load_to_bus', ax=ax) edgelist, edge_color, edge_width, edge_labels = _generate_edges(results, case, hour) branches = network.draw_branches(ax=ax, edgelist=edgelist, edge_color=edge_color, width=edge_width, edge_labels=edge_labels) divider = make_axes_locatable(ax) cax = divider.append_axes('right', size='5%', pad=0.05) cb = plt.colorbar(branches, cax=cax, orientation='vertical') cax.yaxis.set_label_position('left') cax.yaxis.set_ticks_position('left') cb.set_label('Loading Factor') return ax
def plot_line_power(obj, results, hour, ax=None): ''' obj: case or network ''' if ax is None: fig, ax = plt.subplots(1, 1, figsize=(16, 10)) ax.axis('off') case, network = _return_case_network(obj) network.draw_buses(ax=ax) network.draw_loads(ax=ax) network.draw_generators(ax=ax) network.draw_connections('gen_to_bus', ax=ax) network.draw_connections('load_to_bus', ax=ax) edgelist, edge_color, edge_width, edge_labels = _generate_edges(results, case, hour) branches = network.draw_branches(ax=ax, edgelist=edgelist, edge_color=edge_color, width=edge_width, edge_labels=edge_labels) divider = make_axes_locatable(ax) cax = divider.append_axes('right', size='5%', pad=0.05) cb = plt.colorbar(branches, cax=cax, orientation='vertical') cax.yaxis.set_label_position('left') cax.yaxis.set_ticks_position('left') cb.set_label('Loading Factor') return ax
[ "obj", ":", "case", "or", "network" ]
power-system-simulation-toolbox/psst
python
https://github.com/power-system-simulation-toolbox/psst/blob/8c116fb7afd183881ecc605e017dffc87cdc49e6/psst/plot/network.py#L29-L56
[ "def", "plot_line_power", "(", "obj", ",", "results", ",", "hour", ",", "ax", "=", "None", ")", ":", "if", "ax", "is", "None", ":", "fig", ",", "ax", "=", "plt", ".", "subplots", "(", "1", ",", "1", ",", "figsize", "=", "(", "16", ",", "10", ")", ")", "ax", ".", "axis", "(", "'off'", ")", "case", ",", "network", "=", "_return_case_network", "(", "obj", ")", "network", ".", "draw_buses", "(", "ax", "=", "ax", ")", "network", ".", "draw_loads", "(", "ax", "=", "ax", ")", "network", ".", "draw_generators", "(", "ax", "=", "ax", ")", "network", ".", "draw_connections", "(", "'gen_to_bus'", ",", "ax", "=", "ax", ")", "network", ".", "draw_connections", "(", "'load_to_bus'", ",", "ax", "=", "ax", ")", "edgelist", ",", "edge_color", ",", "edge_width", ",", "edge_labels", "=", "_generate_edges", "(", "results", ",", "case", ",", "hour", ")", "branches", "=", "network", ".", "draw_branches", "(", "ax", "=", "ax", ",", "edgelist", "=", "edgelist", ",", "edge_color", "=", "edge_color", ",", "width", "=", "edge_width", ",", "edge_labels", "=", "edge_labels", ")", "divider", "=", "make_axes_locatable", "(", "ax", ")", "cax", "=", "divider", ".", "append_axes", "(", "'right'", ",", "size", "=", "'5%'", ",", "pad", "=", "0.05", ")", "cb", "=", "plt", ".", "colorbar", "(", "branches", ",", "cax", "=", "cax", ",", "orientation", "=", "'vertical'", ")", "cax", ".", "yaxis", ".", "set_label_position", "(", "'left'", ")", "cax", ".", "yaxis", ".", "set_ticks_position", "(", "'left'", ")", "cb", ".", "set_label", "(", "'Loading Factor'", ")", "return", "ax" ]
8c116fb7afd183881ecc605e017dffc87cdc49e6
test
fast_forward_selection
Fast forward selection algorithm Parameters ---------- scenarios : numpy.array Contain the input scenarios. The columns representing the individual scenarios The rows are the vector of values in each scenario number_of_reduced_scenarios : int final number of scenarios that the reduced scenarios contain. If number of scenarios is equal to or greater than the input scenarios, then the original input scenario set is returned as the reduced set probability : numpy.array (default=None) probability is a numpy.array with length equal to number of scenarios. if probability is not defined, all scenarios get equal probabilities Returns ------- reduced_scenarios : numpy.array reduced set of scenarios reduced_probability : numpy.array probability of reduced set of scenarios reduced_scenario_set : list scenario numbers of reduced set of scenarios Example ------- Scenario reduction can be performed as shown below:: >>> import numpy as np >>> import random >>> scenarios = np.array([[random.randint(500,1000) for i in range(0,24)], >>> [random.randint(500,1000) for i in range(0,24)], >>> [random.randint(500,1000) for i in range(0,24)], >>> [random.randint(500,1000) for i in range(0,24)], >>> [random.randint(500,1000) for i in range(0,24)], >>> [random.randint(500,1000) for i in range(0,24)], >>> [random.randint(500,1000) for i in range(0,24)], >>> [random.randint(500,1000) for i in range(0,24)], >>> [random.randint(500,1000) for i in range(0,24)], >>> [random.randint(500,1000) for i in range(0,24)]]) >>> import psst.scenario >>> reduced_scenarios, reduced_probability, reduced_scenario_numbers = psst.scenario.fast_forward_selection(scenarios, probability, 2)
psst/scenarios/reduction.py
def fast_forward_selection(scenarios, number_of_reduced_scenarios, probability=None): """Fast forward selection algorithm Parameters ---------- scenarios : numpy.array Contain the input scenarios. The columns representing the individual scenarios The rows are the vector of values in each scenario number_of_reduced_scenarios : int final number of scenarios that the reduced scenarios contain. If number of scenarios is equal to or greater than the input scenarios, then the original input scenario set is returned as the reduced set probability : numpy.array (default=None) probability is a numpy.array with length equal to number of scenarios. if probability is not defined, all scenarios get equal probabilities Returns ------- reduced_scenarios : numpy.array reduced set of scenarios reduced_probability : numpy.array probability of reduced set of scenarios reduced_scenario_set : list scenario numbers of reduced set of scenarios Example ------- Scenario reduction can be performed as shown below:: >>> import numpy as np >>> import random >>> scenarios = np.array([[random.randint(500,1000) for i in range(0,24)], >>> [random.randint(500,1000) for i in range(0,24)], >>> [random.randint(500,1000) for i in range(0,24)], >>> [random.randint(500,1000) for i in range(0,24)], >>> [random.randint(500,1000) for i in range(0,24)], >>> [random.randint(500,1000) for i in range(0,24)], >>> [random.randint(500,1000) for i in range(0,24)], >>> [random.randint(500,1000) for i in range(0,24)], >>> [random.randint(500,1000) for i in range(0,24)], >>> [random.randint(500,1000) for i in range(0,24)]]) >>> import psst.scenario >>> reduced_scenarios, reduced_probability, reduced_scenario_numbers = psst.scenario.fast_forward_selection(scenarios, probability, 2) """ print("Running fast forward selection algorithm") number_of_scenarios = scenarios.shape[1] logger.debug("Input number of scenarios = %d", number_of_scenarios) # if probability is not defined assign equal probability to all scenarios if probability is None: probability = np.array([1/number_of_scenarios for i in range(0, number_of_scenarios)]) # initialize z, c and J z = np.array([np.inf for i in range(0, number_of_scenarios)]) c = np.zeros((number_of_scenarios, number_of_scenarios)) J = range(0, number_of_scenarios) # no reduction necessary if number_of_reduced_scenarios >= number_of_scenarios: return(scenarios, probability, J) for scenario_k in range(0, number_of_scenarios): for scenario_u in range(0, number_of_scenarios): c[scenario_k, scenario_u] = distance(scenarios[:, scenario_k], scenarios[:, scenario_u]) for scenario_u in range(0, number_of_scenarios): summation = 0 for scenario_k in range(0, number_of_scenarios): if scenario_k != scenario_u: summation = summation + probability[scenario_k]*c[scenario_k, scenario_u] z[scenario_u] = summation U = [np.argmin(z)] for u in U: J.remove(u) for _ in range(0, number_of_scenarios - number_of_reduced_scenarios - 1): print("Running {}".format(_)) for scenario_u in J: for scenario_k in J: lowest_value = np.inf for scenario_number in U: lowest_value = min(c[scenario_k, scenario_u], c[scenario_k, scenario_number]) c[scenario_k, scenario_u] = lowest_value for scenario_u in J: summation = 0 for scenario_k in J: if scenario_k not in U: summation = summation + probability[scenario_k]*c[scenario_k, scenario_u] z[scenario_u] = summation u_i = np.argmin([item if i in J else np.inf for i, item in enumerate(z)]) J.remove(u_i) U.append(u_i) reduced_scenario_set = U reduced_probability = [] reduced_probability = copy.deepcopy(probability) for deleted_scenario_number in J: lowest_value = np.inf # find closest scenario_number for scenario_j in reduced_scenario_set: if c[deleted_scenario_number, scenario_j] < lowest_value: closest_scenario_number = scenario_j lowest_value = c[deleted_scenario_number, scenario_j] reduced_probability[closest_scenario_number] = reduced_probability[closest_scenario_number] + reduced_probability[deleted_scenario_number] reduced_scenarios = copy.deepcopy(scenarios[:, reduced_scenario_set]) reduced_probability = reduced_probability[reduced_scenario_set] return reduced_scenarios, reduced_probability, reduced_scenario_set
def fast_forward_selection(scenarios, number_of_reduced_scenarios, probability=None): """Fast forward selection algorithm Parameters ---------- scenarios : numpy.array Contain the input scenarios. The columns representing the individual scenarios The rows are the vector of values in each scenario number_of_reduced_scenarios : int final number of scenarios that the reduced scenarios contain. If number of scenarios is equal to or greater than the input scenarios, then the original input scenario set is returned as the reduced set probability : numpy.array (default=None) probability is a numpy.array with length equal to number of scenarios. if probability is not defined, all scenarios get equal probabilities Returns ------- reduced_scenarios : numpy.array reduced set of scenarios reduced_probability : numpy.array probability of reduced set of scenarios reduced_scenario_set : list scenario numbers of reduced set of scenarios Example ------- Scenario reduction can be performed as shown below:: >>> import numpy as np >>> import random >>> scenarios = np.array([[random.randint(500,1000) for i in range(0,24)], >>> [random.randint(500,1000) for i in range(0,24)], >>> [random.randint(500,1000) for i in range(0,24)], >>> [random.randint(500,1000) for i in range(0,24)], >>> [random.randint(500,1000) for i in range(0,24)], >>> [random.randint(500,1000) for i in range(0,24)], >>> [random.randint(500,1000) for i in range(0,24)], >>> [random.randint(500,1000) for i in range(0,24)], >>> [random.randint(500,1000) for i in range(0,24)], >>> [random.randint(500,1000) for i in range(0,24)]]) >>> import psst.scenario >>> reduced_scenarios, reduced_probability, reduced_scenario_numbers = psst.scenario.fast_forward_selection(scenarios, probability, 2) """ print("Running fast forward selection algorithm") number_of_scenarios = scenarios.shape[1] logger.debug("Input number of scenarios = %d", number_of_scenarios) # if probability is not defined assign equal probability to all scenarios if probability is None: probability = np.array([1/number_of_scenarios for i in range(0, number_of_scenarios)]) # initialize z, c and J z = np.array([np.inf for i in range(0, number_of_scenarios)]) c = np.zeros((number_of_scenarios, number_of_scenarios)) J = range(0, number_of_scenarios) # no reduction necessary if number_of_reduced_scenarios >= number_of_scenarios: return(scenarios, probability, J) for scenario_k in range(0, number_of_scenarios): for scenario_u in range(0, number_of_scenarios): c[scenario_k, scenario_u] = distance(scenarios[:, scenario_k], scenarios[:, scenario_u]) for scenario_u in range(0, number_of_scenarios): summation = 0 for scenario_k in range(0, number_of_scenarios): if scenario_k != scenario_u: summation = summation + probability[scenario_k]*c[scenario_k, scenario_u] z[scenario_u] = summation U = [np.argmin(z)] for u in U: J.remove(u) for _ in range(0, number_of_scenarios - number_of_reduced_scenarios - 1): print("Running {}".format(_)) for scenario_u in J: for scenario_k in J: lowest_value = np.inf for scenario_number in U: lowest_value = min(c[scenario_k, scenario_u], c[scenario_k, scenario_number]) c[scenario_k, scenario_u] = lowest_value for scenario_u in J: summation = 0 for scenario_k in J: if scenario_k not in U: summation = summation + probability[scenario_k]*c[scenario_k, scenario_u] z[scenario_u] = summation u_i = np.argmin([item if i in J else np.inf for i, item in enumerate(z)]) J.remove(u_i) U.append(u_i) reduced_scenario_set = U reduced_probability = [] reduced_probability = copy.deepcopy(probability) for deleted_scenario_number in J: lowest_value = np.inf # find closest scenario_number for scenario_j in reduced_scenario_set: if c[deleted_scenario_number, scenario_j] < lowest_value: closest_scenario_number = scenario_j lowest_value = c[deleted_scenario_number, scenario_j] reduced_probability[closest_scenario_number] = reduced_probability[closest_scenario_number] + reduced_probability[deleted_scenario_number] reduced_scenarios = copy.deepcopy(scenarios[:, reduced_scenario_set]) reduced_probability = reduced_probability[reduced_scenario_set] return reduced_scenarios, reduced_probability, reduced_scenario_set
[ "Fast", "forward", "selection", "algorithm" ]
power-system-simulation-toolbox/psst
python
https://github.com/power-system-simulation-toolbox/psst/blob/8c116fb7afd183881ecc605e017dffc87cdc49e6/psst/scenarios/reduction.py#L111-L237
[ "def", "fast_forward_selection", "(", "scenarios", ",", "number_of_reduced_scenarios", ",", "probability", "=", "None", ")", ":", "print", "(", "\"Running fast forward selection algorithm\"", ")", "number_of_scenarios", "=", "scenarios", ".", "shape", "[", "1", "]", "logger", ".", "debug", "(", "\"Input number of scenarios = %d\"", ",", "number_of_scenarios", ")", "# if probability is not defined assign equal probability to all scenarios", "if", "probability", "is", "None", ":", "probability", "=", "np", ".", "array", "(", "[", "1", "/", "number_of_scenarios", "for", "i", "in", "range", "(", "0", ",", "number_of_scenarios", ")", "]", ")", "# initialize z, c and J", "z", "=", "np", ".", "array", "(", "[", "np", ".", "inf", "for", "i", "in", "range", "(", "0", ",", "number_of_scenarios", ")", "]", ")", "c", "=", "np", ".", "zeros", "(", "(", "number_of_scenarios", ",", "number_of_scenarios", ")", ")", "J", "=", "range", "(", "0", ",", "number_of_scenarios", ")", "# no reduction necessary", "if", "number_of_reduced_scenarios", ">=", "number_of_scenarios", ":", "return", "(", "scenarios", ",", "probability", ",", "J", ")", "for", "scenario_k", "in", "range", "(", "0", ",", "number_of_scenarios", ")", ":", "for", "scenario_u", "in", "range", "(", "0", ",", "number_of_scenarios", ")", ":", "c", "[", "scenario_k", ",", "scenario_u", "]", "=", "distance", "(", "scenarios", "[", ":", ",", "scenario_k", "]", ",", "scenarios", "[", ":", ",", "scenario_u", "]", ")", "for", "scenario_u", "in", "range", "(", "0", ",", "number_of_scenarios", ")", ":", "summation", "=", "0", "for", "scenario_k", "in", "range", "(", "0", ",", "number_of_scenarios", ")", ":", "if", "scenario_k", "!=", "scenario_u", ":", "summation", "=", "summation", "+", "probability", "[", "scenario_k", "]", "*", "c", "[", "scenario_k", ",", "scenario_u", "]", "z", "[", "scenario_u", "]", "=", "summation", "U", "=", "[", "np", ".", "argmin", "(", "z", ")", "]", "for", "u", "in", "U", ":", "J", ".", "remove", "(", "u", ")", "for", "_", "in", "range", "(", "0", ",", "number_of_scenarios", "-", "number_of_reduced_scenarios", "-", "1", ")", ":", "print", "(", "\"Running {}\"", ".", "format", "(", "_", ")", ")", "for", "scenario_u", "in", "J", ":", "for", "scenario_k", "in", "J", ":", "lowest_value", "=", "np", ".", "inf", "for", "scenario_number", "in", "U", ":", "lowest_value", "=", "min", "(", "c", "[", "scenario_k", ",", "scenario_u", "]", ",", "c", "[", "scenario_k", ",", "scenario_number", "]", ")", "c", "[", "scenario_k", ",", "scenario_u", "]", "=", "lowest_value", "for", "scenario_u", "in", "J", ":", "summation", "=", "0", "for", "scenario_k", "in", "J", ":", "if", "scenario_k", "not", "in", "U", ":", "summation", "=", "summation", "+", "probability", "[", "scenario_k", "]", "*", "c", "[", "scenario_k", ",", "scenario_u", "]", "z", "[", "scenario_u", "]", "=", "summation", "u_i", "=", "np", ".", "argmin", "(", "[", "item", "if", "i", "in", "J", "else", "np", ".", "inf", "for", "i", ",", "item", "in", "enumerate", "(", "z", ")", "]", ")", "J", ".", "remove", "(", "u_i", ")", "U", ".", "append", "(", "u_i", ")", "reduced_scenario_set", "=", "U", "reduced_probability", "=", "[", "]", "reduced_probability", "=", "copy", ".", "deepcopy", "(", "probability", ")", "for", "deleted_scenario_number", "in", "J", ":", "lowest_value", "=", "np", ".", "inf", "# find closest scenario_number", "for", "scenario_j", "in", "reduced_scenario_set", ":", "if", "c", "[", "deleted_scenario_number", ",", "scenario_j", "]", "<", "lowest_value", ":", "closest_scenario_number", "=", "scenario_j", "lowest_value", "=", "c", "[", "deleted_scenario_number", ",", "scenario_j", "]", "reduced_probability", "[", "closest_scenario_number", "]", "=", "reduced_probability", "[", "closest_scenario_number", "]", "+", "reduced_probability", "[", "deleted_scenario_number", "]", "reduced_scenarios", "=", "copy", ".", "deepcopy", "(", "scenarios", "[", ":", ",", "reduced_scenario_set", "]", ")", "reduced_probability", "=", "reduced_probability", "[", "reduced_scenario_set", "]", "return", "reduced_scenarios", ",", "reduced_probability", ",", "reduced_scenario_set" ]
8c116fb7afd183881ecc605e017dffc87cdc49e6
test
simultaneous_backward_reduction
Simultaneous backward reduction algorithm Parameters ---------- scenarios : numpy.array Contain the input scenarios. The columns representing the individual scenarios The rows are the vector of values in each scenario number_of_reduced_scenarios : int final number of scenarios that the reduced scenarios contain. If number of scenarios is equal to or greater than the input scenarios, then the original input scenario set is returned as the reduced set probability : numpy.array (default=None) probability is a numpy.array with length equal to number of scenarios. if probability is not defined, all scenarios get equal probabilities Returns ------- reduced_scenarios : numpy.array reduced set of scenarios reduced_probability : numpy.array probability of reduced set of scenarios reduced_scenario_set : list scenario numbers of reduced set of scenarios Example ------- Scenario reduction can be performed as shown below:: >>> import numpy as np >>> import random >>> scenarios = np.array([[random.randint(500,1000) for i in range(0,24)], >>> [random.randint(500,1000) for i in range(0,24)], >>> [random.randint(500,1000) for i in range(0,24)], >>> [random.randint(500,1000) for i in range(0,24)], >>> [random.randint(500,1000) for i in range(0,24)], >>> [random.randint(500,1000) for i in range(0,24)], >>> [random.randint(500,1000) for i in range(0,24)], >>> [random.randint(500,1000) for i in range(0,24)], >>> [random.randint(500,1000) for i in range(0,24)], >>> [random.randint(500,1000) for i in range(0,24)]]) >>> import psst.scenario >>> reduced_scenarios, reduced_probability, reduced_scenario_numbers = psst.scenario.simultaneous_backward_reduction(scenarios, probability, 2)
psst/scenarios/reduction.py
def simultaneous_backward_reduction(scenarios, number_of_reduced_scenarios, probability=None): """Simultaneous backward reduction algorithm Parameters ---------- scenarios : numpy.array Contain the input scenarios. The columns representing the individual scenarios The rows are the vector of values in each scenario number_of_reduced_scenarios : int final number of scenarios that the reduced scenarios contain. If number of scenarios is equal to or greater than the input scenarios, then the original input scenario set is returned as the reduced set probability : numpy.array (default=None) probability is a numpy.array with length equal to number of scenarios. if probability is not defined, all scenarios get equal probabilities Returns ------- reduced_scenarios : numpy.array reduced set of scenarios reduced_probability : numpy.array probability of reduced set of scenarios reduced_scenario_set : list scenario numbers of reduced set of scenarios Example ------- Scenario reduction can be performed as shown below:: >>> import numpy as np >>> import random >>> scenarios = np.array([[random.randint(500,1000) for i in range(0,24)], >>> [random.randint(500,1000) for i in range(0,24)], >>> [random.randint(500,1000) for i in range(0,24)], >>> [random.randint(500,1000) for i in range(0,24)], >>> [random.randint(500,1000) for i in range(0,24)], >>> [random.randint(500,1000) for i in range(0,24)], >>> [random.randint(500,1000) for i in range(0,24)], >>> [random.randint(500,1000) for i in range(0,24)], >>> [random.randint(500,1000) for i in range(0,24)], >>> [random.randint(500,1000) for i in range(0,24)]]) >>> import psst.scenario >>> reduced_scenarios, reduced_probability, reduced_scenario_numbers = psst.scenario.simultaneous_backward_reduction(scenarios, probability, 2) """ print("Running simultaneous backward reduction algorithm") number_of_scenarios = scenarios.shape[1] logger.debug("Input number of scenarios = %d", number_of_scenarios) # if probability is not defined assign equal probability to all scenarios if probability is None: probability = np.array([1/number_of_scenarios for i in range(0, number_of_scenarios)]) # initialize z, c and J z = np.array([np.inf for i in range(0, number_of_scenarios)]) c = np.zeros((number_of_scenarios, number_of_scenarios)) J = [] # no reduction necessary if number_of_reduced_scenarios >= number_of_scenarios: return(scenarios, probability, J) """compute the distance of scenario pairs""" for scenario_k in range(0, number_of_scenarios): for scenario_j in range(0, number_of_scenarios): c[scenario_k, scenario_j] = distance(scenarios[:, scenario_k], scenarios[:, scenario_j]) for scenario_l in range(0, number_of_scenarios): lowest_value = np.inf for scenario_j in range(0, number_of_scenarios): if scenario_l == scenario_j: continue lowest_value = min(lowest_value, c[scenario_l, scenario_j]) c[scenario_l, scenario_l] = lowest_value z[scenario_l] = probability[scenario_l]*c[scenario_l, scenario_l] J.append(np.argmin(z)) for _ in range(0, number_of_scenarios - number_of_reduced_scenarios - 1): for scenario_l in range(0, number_of_scenarios): for scenario_k in range(0, number_of_scenarios): if scenario_k in J or scenario_k == scenario_l: if scenario_l not in J: lowest_value = np.inf for scenario_j in range(0, number_of_scenarios): if scenario_j not in J and scenario_j != scenario_l: lowest_value = min(lowest_value, c[scenario_k, scenario_j]) c[scenario_k, scenario_l] = lowest_value for scenario_l in range(0, number_of_scenarios): if scenario_l not in J: summation = 0 for scenario_k in range(0, number_of_scenarios): if scenario_k in J or scenario_k == scenario_l: summation = summation + probability[scenario_k]*c[scenario_k, scenario_l] z[scenario_l] = summation J.append(np.argmin([item if i not in J else np.inf for i, item in enumerate(z)])) reduced_scenario_set = [] for scenario_number in range(0, number_of_scenarios): if scenario_number not in J: reduced_scenario_set.append(scenario_number) reduced_probability = [] reduced_probability = copy.deepcopy(probability) for deleted_scenario_number in J: lowest_value = np.inf # find closest scenario_number for scenario_j in reduced_scenario_set: if c[deleted_scenario_number, scenario_j] < lowest_value: closest_scenario_number = scenario_j lowest_value = c[deleted_scenario_number, scenario_j] reduced_probability[closest_scenario_number] = reduced_probability[closest_scenario_number] + reduced_probability[deleted_scenario_number] reduced_scenarios = copy.deepcopy(scenarios[:, reduced_scenario_set]) reduced_probability = reduced_probability[reduced_scenario_set] return(reduced_scenarios, reduced_probability, reduced_scenario_set)
def simultaneous_backward_reduction(scenarios, number_of_reduced_scenarios, probability=None): """Simultaneous backward reduction algorithm Parameters ---------- scenarios : numpy.array Contain the input scenarios. The columns representing the individual scenarios The rows are the vector of values in each scenario number_of_reduced_scenarios : int final number of scenarios that the reduced scenarios contain. If number of scenarios is equal to or greater than the input scenarios, then the original input scenario set is returned as the reduced set probability : numpy.array (default=None) probability is a numpy.array with length equal to number of scenarios. if probability is not defined, all scenarios get equal probabilities Returns ------- reduced_scenarios : numpy.array reduced set of scenarios reduced_probability : numpy.array probability of reduced set of scenarios reduced_scenario_set : list scenario numbers of reduced set of scenarios Example ------- Scenario reduction can be performed as shown below:: >>> import numpy as np >>> import random >>> scenarios = np.array([[random.randint(500,1000) for i in range(0,24)], >>> [random.randint(500,1000) for i in range(0,24)], >>> [random.randint(500,1000) for i in range(0,24)], >>> [random.randint(500,1000) for i in range(0,24)], >>> [random.randint(500,1000) for i in range(0,24)], >>> [random.randint(500,1000) for i in range(0,24)], >>> [random.randint(500,1000) for i in range(0,24)], >>> [random.randint(500,1000) for i in range(0,24)], >>> [random.randint(500,1000) for i in range(0,24)], >>> [random.randint(500,1000) for i in range(0,24)]]) >>> import psst.scenario >>> reduced_scenarios, reduced_probability, reduced_scenario_numbers = psst.scenario.simultaneous_backward_reduction(scenarios, probability, 2) """ print("Running simultaneous backward reduction algorithm") number_of_scenarios = scenarios.shape[1] logger.debug("Input number of scenarios = %d", number_of_scenarios) # if probability is not defined assign equal probability to all scenarios if probability is None: probability = np.array([1/number_of_scenarios for i in range(0, number_of_scenarios)]) # initialize z, c and J z = np.array([np.inf for i in range(0, number_of_scenarios)]) c = np.zeros((number_of_scenarios, number_of_scenarios)) J = [] # no reduction necessary if number_of_reduced_scenarios >= number_of_scenarios: return(scenarios, probability, J) """compute the distance of scenario pairs""" for scenario_k in range(0, number_of_scenarios): for scenario_j in range(0, number_of_scenarios): c[scenario_k, scenario_j] = distance(scenarios[:, scenario_k], scenarios[:, scenario_j]) for scenario_l in range(0, number_of_scenarios): lowest_value = np.inf for scenario_j in range(0, number_of_scenarios): if scenario_l == scenario_j: continue lowest_value = min(lowest_value, c[scenario_l, scenario_j]) c[scenario_l, scenario_l] = lowest_value z[scenario_l] = probability[scenario_l]*c[scenario_l, scenario_l] J.append(np.argmin(z)) for _ in range(0, number_of_scenarios - number_of_reduced_scenarios - 1): for scenario_l in range(0, number_of_scenarios): for scenario_k in range(0, number_of_scenarios): if scenario_k in J or scenario_k == scenario_l: if scenario_l not in J: lowest_value = np.inf for scenario_j in range(0, number_of_scenarios): if scenario_j not in J and scenario_j != scenario_l: lowest_value = min(lowest_value, c[scenario_k, scenario_j]) c[scenario_k, scenario_l] = lowest_value for scenario_l in range(0, number_of_scenarios): if scenario_l not in J: summation = 0 for scenario_k in range(0, number_of_scenarios): if scenario_k in J or scenario_k == scenario_l: summation = summation + probability[scenario_k]*c[scenario_k, scenario_l] z[scenario_l] = summation J.append(np.argmin([item if i not in J else np.inf for i, item in enumerate(z)])) reduced_scenario_set = [] for scenario_number in range(0, number_of_scenarios): if scenario_number not in J: reduced_scenario_set.append(scenario_number) reduced_probability = [] reduced_probability = copy.deepcopy(probability) for deleted_scenario_number in J: lowest_value = np.inf # find closest scenario_number for scenario_j in reduced_scenario_set: if c[deleted_scenario_number, scenario_j] < lowest_value: closest_scenario_number = scenario_j lowest_value = c[deleted_scenario_number, scenario_j] reduced_probability[closest_scenario_number] = reduced_probability[closest_scenario_number] + reduced_probability[deleted_scenario_number] reduced_scenarios = copy.deepcopy(scenarios[:, reduced_scenario_set]) reduced_probability = reduced_probability[reduced_scenario_set] return(reduced_scenarios, reduced_probability, reduced_scenario_set)
[ "Simultaneous", "backward", "reduction", "algorithm" ]
power-system-simulation-toolbox/psst
python
https://github.com/power-system-simulation-toolbox/psst/blob/8c116fb7afd183881ecc605e017dffc87cdc49e6/psst/scenarios/reduction.py#L244-L373
[ "def", "simultaneous_backward_reduction", "(", "scenarios", ",", "number_of_reduced_scenarios", ",", "probability", "=", "None", ")", ":", "print", "(", "\"Running simultaneous backward reduction algorithm\"", ")", "number_of_scenarios", "=", "scenarios", ".", "shape", "[", "1", "]", "logger", ".", "debug", "(", "\"Input number of scenarios = %d\"", ",", "number_of_scenarios", ")", "# if probability is not defined assign equal probability to all scenarios", "if", "probability", "is", "None", ":", "probability", "=", "np", ".", "array", "(", "[", "1", "/", "number_of_scenarios", "for", "i", "in", "range", "(", "0", ",", "number_of_scenarios", ")", "]", ")", "# initialize z, c and J", "z", "=", "np", ".", "array", "(", "[", "np", ".", "inf", "for", "i", "in", "range", "(", "0", ",", "number_of_scenarios", ")", "]", ")", "c", "=", "np", ".", "zeros", "(", "(", "number_of_scenarios", ",", "number_of_scenarios", ")", ")", "J", "=", "[", "]", "# no reduction necessary", "if", "number_of_reduced_scenarios", ">=", "number_of_scenarios", ":", "return", "(", "scenarios", ",", "probability", ",", "J", ")", "\"\"\"compute the distance of scenario pairs\"\"\"", "for", "scenario_k", "in", "range", "(", "0", ",", "number_of_scenarios", ")", ":", "for", "scenario_j", "in", "range", "(", "0", ",", "number_of_scenarios", ")", ":", "c", "[", "scenario_k", ",", "scenario_j", "]", "=", "distance", "(", "scenarios", "[", ":", ",", "scenario_k", "]", ",", "scenarios", "[", ":", ",", "scenario_j", "]", ")", "for", "scenario_l", "in", "range", "(", "0", ",", "number_of_scenarios", ")", ":", "lowest_value", "=", "np", ".", "inf", "for", "scenario_j", "in", "range", "(", "0", ",", "number_of_scenarios", ")", ":", "if", "scenario_l", "==", "scenario_j", ":", "continue", "lowest_value", "=", "min", "(", "lowest_value", ",", "c", "[", "scenario_l", ",", "scenario_j", "]", ")", "c", "[", "scenario_l", ",", "scenario_l", "]", "=", "lowest_value", "z", "[", "scenario_l", "]", "=", "probability", "[", "scenario_l", "]", "*", "c", "[", "scenario_l", ",", "scenario_l", "]", "J", ".", "append", "(", "np", ".", "argmin", "(", "z", ")", ")", "for", "_", "in", "range", "(", "0", ",", "number_of_scenarios", "-", "number_of_reduced_scenarios", "-", "1", ")", ":", "for", "scenario_l", "in", "range", "(", "0", ",", "number_of_scenarios", ")", ":", "for", "scenario_k", "in", "range", "(", "0", ",", "number_of_scenarios", ")", ":", "if", "scenario_k", "in", "J", "or", "scenario_k", "==", "scenario_l", ":", "if", "scenario_l", "not", "in", "J", ":", "lowest_value", "=", "np", ".", "inf", "for", "scenario_j", "in", "range", "(", "0", ",", "number_of_scenarios", ")", ":", "if", "scenario_j", "not", "in", "J", "and", "scenario_j", "!=", "scenario_l", ":", "lowest_value", "=", "min", "(", "lowest_value", ",", "c", "[", "scenario_k", ",", "scenario_j", "]", ")", "c", "[", "scenario_k", ",", "scenario_l", "]", "=", "lowest_value", "for", "scenario_l", "in", "range", "(", "0", ",", "number_of_scenarios", ")", ":", "if", "scenario_l", "not", "in", "J", ":", "summation", "=", "0", "for", "scenario_k", "in", "range", "(", "0", ",", "number_of_scenarios", ")", ":", "if", "scenario_k", "in", "J", "or", "scenario_k", "==", "scenario_l", ":", "summation", "=", "summation", "+", "probability", "[", "scenario_k", "]", "*", "c", "[", "scenario_k", ",", "scenario_l", "]", "z", "[", "scenario_l", "]", "=", "summation", "J", ".", "append", "(", "np", ".", "argmin", "(", "[", "item", "if", "i", "not", "in", "J", "else", "np", ".", "inf", "for", "i", ",", "item", "in", "enumerate", "(", "z", ")", "]", ")", ")", "reduced_scenario_set", "=", "[", "]", "for", "scenario_number", "in", "range", "(", "0", ",", "number_of_scenarios", ")", ":", "if", "scenario_number", "not", "in", "J", ":", "reduced_scenario_set", ".", "append", "(", "scenario_number", ")", "reduced_probability", "=", "[", "]", "reduced_probability", "=", "copy", ".", "deepcopy", "(", "probability", ")", "for", "deleted_scenario_number", "in", "J", ":", "lowest_value", "=", "np", ".", "inf", "# find closest scenario_number", "for", "scenario_j", "in", "reduced_scenario_set", ":", "if", "c", "[", "deleted_scenario_number", ",", "scenario_j", "]", "<", "lowest_value", ":", "closest_scenario_number", "=", "scenario_j", "lowest_value", "=", "c", "[", "deleted_scenario_number", ",", "scenario_j", "]", "reduced_probability", "[", "closest_scenario_number", "]", "=", "reduced_probability", "[", "closest_scenario_number", "]", "+", "reduced_probability", "[", "deleted_scenario_number", "]", "reduced_scenarios", "=", "copy", ".", "deepcopy", "(", "scenarios", "[", ":", ",", "reduced_scenario_set", "]", ")", "reduced_probability", "=", "reduced_probability", "[", "reduced_scenario_set", "]", "return", "(", "reduced_scenarios", ",", "reduced_probability", ",", "reduced_scenario_set", ")" ]
8c116fb7afd183881ecc605e017dffc87cdc49e6
test
search
Shorthand for creating a Giphy api wrapper with the given api key and then calling the search method. Note that this will return a generator
giphypop.py
def search(term=None, phrase=None, limit=DEFAULT_SEARCH_LIMIT, api_key=GIPHY_PUBLIC_KEY, strict=False, rating=None): """ Shorthand for creating a Giphy api wrapper with the given api key and then calling the search method. Note that this will return a generator """ return Giphy(api_key=api_key, strict=strict).search( term=term, phrase=phrase, limit=limit, rating=rating)
def search(term=None, phrase=None, limit=DEFAULT_SEARCH_LIMIT, api_key=GIPHY_PUBLIC_KEY, strict=False, rating=None): """ Shorthand for creating a Giphy api wrapper with the given api key and then calling the search method. Note that this will return a generator """ return Giphy(api_key=api_key, strict=strict).search( term=term, phrase=phrase, limit=limit, rating=rating)
[ "Shorthand", "for", "creating", "a", "Giphy", "api", "wrapper", "with", "the", "given", "api", "key", "and", "then", "calling", "the", "search", "method", ".", "Note", "that", "this", "will", "return", "a", "generator" ]
shaunduncan/giphypop
python
https://github.com/shaunduncan/giphypop/blob/21e7f51c4f000ae24be3805b7eeec52bcce3d390/giphypop.py#L489-L496
[ "def", "search", "(", "term", "=", "None", ",", "phrase", "=", "None", ",", "limit", "=", "DEFAULT_SEARCH_LIMIT", ",", "api_key", "=", "GIPHY_PUBLIC_KEY", ",", "strict", "=", "False", ",", "rating", "=", "None", ")", ":", "return", "Giphy", "(", "api_key", "=", "api_key", ",", "strict", "=", "strict", ")", ".", "search", "(", "term", "=", "term", ",", "phrase", "=", "phrase", ",", "limit", "=", "limit", ",", "rating", "=", "rating", ")" ]
21e7f51c4f000ae24be3805b7eeec52bcce3d390
test
translate
Shorthand for creating a Giphy api wrapper with the given api key and then calling the translate method.
giphypop.py
def translate(term=None, phrase=None, api_key=GIPHY_PUBLIC_KEY, strict=False, rating=None): """ Shorthand for creating a Giphy api wrapper with the given api key and then calling the translate method. """ return Giphy(api_key=api_key, strict=strict).translate( term=term, phrase=phrase, rating=rating)
def translate(term=None, phrase=None, api_key=GIPHY_PUBLIC_KEY, strict=False, rating=None): """ Shorthand for creating a Giphy api wrapper with the given api key and then calling the translate method. """ return Giphy(api_key=api_key, strict=strict).translate( term=term, phrase=phrase, rating=rating)
[ "Shorthand", "for", "creating", "a", "Giphy", "api", "wrapper", "with", "the", "given", "api", "key", "and", "then", "calling", "the", "translate", "method", "." ]
shaunduncan/giphypop
python
https://github.com/shaunduncan/giphypop/blob/21e7f51c4f000ae24be3805b7eeec52bcce3d390/giphypop.py#L509-L516
[ "def", "translate", "(", "term", "=", "None", ",", "phrase", "=", "None", ",", "api_key", "=", "GIPHY_PUBLIC_KEY", ",", "strict", "=", "False", ",", "rating", "=", "None", ")", ":", "return", "Giphy", "(", "api_key", "=", "api_key", ",", "strict", "=", "strict", ")", ".", "translate", "(", "term", "=", "term", ",", "phrase", "=", "phrase", ",", "rating", "=", "rating", ")" ]
21e7f51c4f000ae24be3805b7eeec52bcce3d390
test
trending
Shorthand for creating a Giphy api wrapper with the given api key and then calling the trending method. Note that this will return a generator
giphypop.py
def trending(limit=DEFAULT_SEARCH_LIMIT, api_key=GIPHY_PUBLIC_KEY, strict=False, rating=None): """ Shorthand for creating a Giphy api wrapper with the given api key and then calling the trending method. Note that this will return a generator """ return Giphy(api_key=api_key, strict=strict).trending( limit=limit, rating=rating)
def trending(limit=DEFAULT_SEARCH_LIMIT, api_key=GIPHY_PUBLIC_KEY, strict=False, rating=None): """ Shorthand for creating a Giphy api wrapper with the given api key and then calling the trending method. Note that this will return a generator """ return Giphy(api_key=api_key, strict=strict).trending( limit=limit, rating=rating)
[ "Shorthand", "for", "creating", "a", "Giphy", "api", "wrapper", "with", "the", "given", "api", "key", "and", "then", "calling", "the", "trending", "method", ".", "Note", "that", "this", "will", "return", "a", "generator" ]
shaunduncan/giphypop
python
https://github.com/shaunduncan/giphypop/blob/21e7f51c4f000ae24be3805b7eeec52bcce3d390/giphypop.py#L519-L527
[ "def", "trending", "(", "limit", "=", "DEFAULT_SEARCH_LIMIT", ",", "api_key", "=", "GIPHY_PUBLIC_KEY", ",", "strict", "=", "False", ",", "rating", "=", "None", ")", ":", "return", "Giphy", "(", "api_key", "=", "api_key", ",", "strict", "=", "strict", ")", ".", "trending", "(", "limit", "=", "limit", ",", "rating", "=", "rating", ")" ]
21e7f51c4f000ae24be3805b7eeec52bcce3d390
test
gif
Shorthand for creating a Giphy api wrapper with the given api key and then calling the gif method.
giphypop.py
def gif(gif_id, api_key=GIPHY_PUBLIC_KEY, strict=False): """ Shorthand for creating a Giphy api wrapper with the given api key and then calling the gif method. """ return Giphy(api_key=api_key, strict=strict).gif(gif_id)
def gif(gif_id, api_key=GIPHY_PUBLIC_KEY, strict=False): """ Shorthand for creating a Giphy api wrapper with the given api key and then calling the gif method. """ return Giphy(api_key=api_key, strict=strict).gif(gif_id)
[ "Shorthand", "for", "creating", "a", "Giphy", "api", "wrapper", "with", "the", "given", "api", "key", "and", "then", "calling", "the", "gif", "method", "." ]
shaunduncan/giphypop
python
https://github.com/shaunduncan/giphypop/blob/21e7f51c4f000ae24be3805b7eeec52bcce3d390/giphypop.py#L540-L545
[ "def", "gif", "(", "gif_id", ",", "api_key", "=", "GIPHY_PUBLIC_KEY", ",", "strict", "=", "False", ")", ":", "return", "Giphy", "(", "api_key", "=", "api_key", ",", "strict", "=", "strict", ")", ".", "gif", "(", "gif_id", ")" ]
21e7f51c4f000ae24be3805b7eeec52bcce3d390
test
screensaver
Shorthand for creating a Giphy api wrapper with the given api key and then calling the screensaver method.
giphypop.py
def screensaver(tag=None, api_key=GIPHY_PUBLIC_KEY, strict=False): """ Shorthand for creating a Giphy api wrapper with the given api key and then calling the screensaver method. """ return Giphy(api_key=api_key, strict=strict).screensaver(tag=tag)
def screensaver(tag=None, api_key=GIPHY_PUBLIC_KEY, strict=False): """ Shorthand for creating a Giphy api wrapper with the given api key and then calling the screensaver method. """ return Giphy(api_key=api_key, strict=strict).screensaver(tag=tag)
[ "Shorthand", "for", "creating", "a", "Giphy", "api", "wrapper", "with", "the", "given", "api", "key", "and", "then", "calling", "the", "screensaver", "method", "." ]
shaunduncan/giphypop
python
https://github.com/shaunduncan/giphypop/blob/21e7f51c4f000ae24be3805b7eeec52bcce3d390/giphypop.py#L548-L553
[ "def", "screensaver", "(", "tag", "=", "None", ",", "api_key", "=", "GIPHY_PUBLIC_KEY", ",", "strict", "=", "False", ")", ":", "return", "Giphy", "(", "api_key", "=", "api_key", ",", "strict", "=", "strict", ")", ".", "screensaver", "(", "tag", "=", "tag", ")" ]
21e7f51c4f000ae24be3805b7eeec52bcce3d390
test
upload
Shorthand for creating a Giphy api wrapper with the given api key and then calling the upload method.
giphypop.py
def upload(tags, file_path, username=None, api_key=GIPHY_PUBLIC_KEY, strict=False): """ Shorthand for creating a Giphy api wrapper with the given api key and then calling the upload method. """ return Giphy(api_key=api_key, strict=strict).upload( tags, file_path, username)
def upload(tags, file_path, username=None, api_key=GIPHY_PUBLIC_KEY, strict=False): """ Shorthand for creating a Giphy api wrapper with the given api key and then calling the upload method. """ return Giphy(api_key=api_key, strict=strict).upload( tags, file_path, username)
[ "Shorthand", "for", "creating", "a", "Giphy", "api", "wrapper", "with", "the", "given", "api", "key", "and", "then", "calling", "the", "upload", "method", "." ]
shaunduncan/giphypop
python
https://github.com/shaunduncan/giphypop/blob/21e7f51c4f000ae24be3805b7eeec52bcce3d390/giphypop.py#L559-L566
[ "def", "upload", "(", "tags", ",", "file_path", ",", "username", "=", "None", ",", "api_key", "=", "GIPHY_PUBLIC_KEY", ",", "strict", "=", "False", ")", ":", "return", "Giphy", "(", "api_key", "=", "api_key", ",", "strict", "=", "strict", ")", ".", "upload", "(", "tags", ",", "file_path", ",", "username", ")" ]
21e7f51c4f000ae24be3805b7eeec52bcce3d390
test
GiphyImage._make_images
Takes an image dict from the giphy api and converts it to attributes. Any fields expected to be int (width, height, size, frames) will be attempted to be converted. Also, the keys of `data` serve as the attribute names, but with special action taken. Keys are split by the last underscore; anything prior becomes the attribute name, anything after becomes a sub-attribute. For example: fixed_width_downsampled will end up at `self.fixed_width.downsampled`
giphypop.py
def _make_images(self, images): """ Takes an image dict from the giphy api and converts it to attributes. Any fields expected to be int (width, height, size, frames) will be attempted to be converted. Also, the keys of `data` serve as the attribute names, but with special action taken. Keys are split by the last underscore; anything prior becomes the attribute name, anything after becomes a sub-attribute. For example: fixed_width_downsampled will end up at `self.fixed_width.downsampled` """ # Order matters :) process = ('original', 'fixed_width', 'fixed_height', 'fixed_width_downsampled', 'fixed_width_still', 'fixed_height_downsampled', 'fixed_height_still', 'downsized') for key in process: data = images.get(key) # Ignore empties if not data: continue parts = key.split('_') # attr/subattr style if len(parts) > 2: attr, subattr = '_'.join(parts[:-1]), parts[-1] else: attr, subattr = '_'.join(parts), None # Normalize data img = AttrDict(self._normalized(data)) if subattr is None: setattr(self, attr, img) else: setattr(getattr(self, attr), subattr, img)
def _make_images(self, images): """ Takes an image dict from the giphy api and converts it to attributes. Any fields expected to be int (width, height, size, frames) will be attempted to be converted. Also, the keys of `data` serve as the attribute names, but with special action taken. Keys are split by the last underscore; anything prior becomes the attribute name, anything after becomes a sub-attribute. For example: fixed_width_downsampled will end up at `self.fixed_width.downsampled` """ # Order matters :) process = ('original', 'fixed_width', 'fixed_height', 'fixed_width_downsampled', 'fixed_width_still', 'fixed_height_downsampled', 'fixed_height_still', 'downsized') for key in process: data = images.get(key) # Ignore empties if not data: continue parts = key.split('_') # attr/subattr style if len(parts) > 2: attr, subattr = '_'.join(parts[:-1]), parts[-1] else: attr, subattr = '_'.join(parts), None # Normalize data img = AttrDict(self._normalized(data)) if subattr is None: setattr(self, attr, img) else: setattr(getattr(self, attr), subattr, img)
[ "Takes", "an", "image", "dict", "from", "the", "giphy", "api", "and", "converts", "it", "to", "attributes", ".", "Any", "fields", "expected", "to", "be", "int", "(", "width", "height", "size", "frames", ")", "will", "be", "attempted", "to", "be", "converted", ".", "Also", "the", "keys", "of", "data", "serve", "as", "the", "attribute", "names", "but", "with", "special", "action", "taken", ".", "Keys", "are", "split", "by", "the", "last", "underscore", ";", "anything", "prior", "becomes", "the", "attribute", "name", "anything", "after", "becomes", "a", "sub", "-", "attribute", ".", "For", "example", ":", "fixed_width_downsampled", "will", "end", "up", "at", "self", ".", "fixed_width", ".", "downsampled" ]
shaunduncan/giphypop
python
https://github.com/shaunduncan/giphypop/blob/21e7f51c4f000ae24be3805b7eeec52bcce3d390/giphypop.py#L164-L204
[ "def", "_make_images", "(", "self", ",", "images", ")", ":", "# Order matters :)", "process", "=", "(", "'original'", ",", "'fixed_width'", ",", "'fixed_height'", ",", "'fixed_width_downsampled'", ",", "'fixed_width_still'", ",", "'fixed_height_downsampled'", ",", "'fixed_height_still'", ",", "'downsized'", ")", "for", "key", "in", "process", ":", "data", "=", "images", ".", "get", "(", "key", ")", "# Ignore empties", "if", "not", "data", ":", "continue", "parts", "=", "key", ".", "split", "(", "'_'", ")", "# attr/subattr style", "if", "len", "(", "parts", ")", ">", "2", ":", "attr", ",", "subattr", "=", "'_'", ".", "join", "(", "parts", "[", ":", "-", "1", "]", ")", ",", "parts", "[", "-", "1", "]", "else", ":", "attr", ",", "subattr", "=", "'_'", ".", "join", "(", "parts", ")", ",", "None", "# Normalize data", "img", "=", "AttrDict", "(", "self", ".", "_normalized", "(", "data", ")", ")", "if", "subattr", "is", "None", ":", "setattr", "(", "self", ",", "attr", ",", "img", ")", "else", ":", "setattr", "(", "getattr", "(", "self", ",", "attr", ")", ",", "subattr", ",", "img", ")" ]
21e7f51c4f000ae24be3805b7eeec52bcce3d390
test
GiphyImage._normalized
Does a normalization of sorts on image type data so that values that should be integers are converted from strings
giphypop.py
def _normalized(self, data): """ Does a normalization of sorts on image type data so that values that should be integers are converted from strings """ int_keys = ('frames', 'width', 'height', 'size') for key in int_keys: if key not in data: continue try: data[key] = int(data[key]) except ValueError: pass # Ignored return data
def _normalized(self, data): """ Does a normalization of sorts on image type data so that values that should be integers are converted from strings """ int_keys = ('frames', 'width', 'height', 'size') for key in int_keys: if key not in data: continue try: data[key] = int(data[key]) except ValueError: pass # Ignored return data
[ "Does", "a", "normalization", "of", "sorts", "on", "image", "type", "data", "so", "that", "values", "that", "should", "be", "integers", "are", "converted", "from", "strings" ]
shaunduncan/giphypop
python
https://github.com/shaunduncan/giphypop/blob/21e7f51c4f000ae24be3805b7eeec52bcce3d390/giphypop.py#L206-L222
[ "def", "_normalized", "(", "self", ",", "data", ")", ":", "int_keys", "=", "(", "'frames'", ",", "'width'", ",", "'height'", ",", "'size'", ")", "for", "key", "in", "int_keys", ":", "if", "key", "not", "in", "data", ":", "continue", "try", ":", "data", "[", "key", "]", "=", "int", "(", "data", "[", "key", "]", ")", "except", "ValueError", ":", "pass", "# Ignored", "return", "data" ]
21e7f51c4f000ae24be3805b7eeec52bcce3d390
test
Giphy._fetch
Wrapper for making an api request from giphy
giphypop.py
def _fetch(self, endpoint_name, **params): """ Wrapper for making an api request from giphy """ params['api_key'] = self.api_key resp = requests.get(self._endpoint(endpoint_name), params=params) resp.raise_for_status() data = resp.json() self._check_or_raise(data.get('meta', {})) return data
def _fetch(self, endpoint_name, **params): """ Wrapper for making an api request from giphy """ params['api_key'] = self.api_key resp = requests.get(self._endpoint(endpoint_name), params=params) resp.raise_for_status() data = resp.json() self._check_or_raise(data.get('meta', {})) return data
[ "Wrapper", "for", "making", "an", "api", "request", "from", "giphy" ]
shaunduncan/giphypop
python
https://github.com/shaunduncan/giphypop/blob/21e7f51c4f000ae24be3805b7eeec52bcce3d390/giphypop.py#L256-L268
[ "def", "_fetch", "(", "self", ",", "endpoint_name", ",", "*", "*", "params", ")", ":", "params", "[", "'api_key'", "]", "=", "self", ".", "api_key", "resp", "=", "requests", ".", "get", "(", "self", ".", "_endpoint", "(", "endpoint_name", ")", ",", "params", "=", "params", ")", "resp", ".", "raise_for_status", "(", ")", "data", "=", "resp", ".", "json", "(", ")", "self", ".", "_check_or_raise", "(", "data", ".", "get", "(", "'meta'", ",", "{", "}", ")", ")", "return", "data" ]
21e7f51c4f000ae24be3805b7eeec52bcce3d390
test
Giphy.search
Search for gifs with a given word or phrase. Punctuation is ignored. By default, this will perform a `term` search. If you want to search by phrase, use the `phrase` keyword argument. What's the difference between `term` and `phrase` searches? Simple: a term search will return results matching any words given, whereas a phrase search will match all words. Note that this method is a GiphyImage generator that automatically handles api paging. Optionally accepts a limit that will terminate the generation after a specified number of results have been yielded. This defaults to 25 results; a None implies no limit :param term: Search term or terms :type term: string :param phrase: Search phrase :type phrase: string :param limit: Maximum number of results to yield :type limit: int :param rating: limit results to those rated (y,g, pg, pg-13 or r). :type rating: string
giphypop.py
def search(self, term=None, phrase=None, limit=DEFAULT_SEARCH_LIMIT, rating=None): """ Search for gifs with a given word or phrase. Punctuation is ignored. By default, this will perform a `term` search. If you want to search by phrase, use the `phrase` keyword argument. What's the difference between `term` and `phrase` searches? Simple: a term search will return results matching any words given, whereas a phrase search will match all words. Note that this method is a GiphyImage generator that automatically handles api paging. Optionally accepts a limit that will terminate the generation after a specified number of results have been yielded. This defaults to 25 results; a None implies no limit :param term: Search term or terms :type term: string :param phrase: Search phrase :type phrase: string :param limit: Maximum number of results to yield :type limit: int :param rating: limit results to those rated (y,g, pg, pg-13 or r). :type rating: string """ assert any((term, phrase)), 'You must supply a term or phrase to search' # Phrases should have dashes and not spaces if phrase: phrase = phrase.replace(' ', '-') results_yielded = 0 # Count how many things we yield page, per_page = 0, 25 params = {'q': (term or phrase)} if rating: params.update({'rating': rating}) fetch = partial(self._fetch, 'search', **params) # Generate results until we 1) run out of pages 2) reach a limit while True: data = fetch(offset=page, limit=per_page) page += per_page # Guard for empty results if not data['data']: raise StopIteration for item in data['data']: results_yielded += 1 yield GiphyImage(item) if limit is not None and results_yielded >= limit: raise StopIteration # Check yieled limit and whether or not there are more items if (page >= data['pagination']['total_count'] or (limit is not None and results_yielded >= limit)): raise StopIteration
def search(self, term=None, phrase=None, limit=DEFAULT_SEARCH_LIMIT, rating=None): """ Search for gifs with a given word or phrase. Punctuation is ignored. By default, this will perform a `term` search. If you want to search by phrase, use the `phrase` keyword argument. What's the difference between `term` and `phrase` searches? Simple: a term search will return results matching any words given, whereas a phrase search will match all words. Note that this method is a GiphyImage generator that automatically handles api paging. Optionally accepts a limit that will terminate the generation after a specified number of results have been yielded. This defaults to 25 results; a None implies no limit :param term: Search term or terms :type term: string :param phrase: Search phrase :type phrase: string :param limit: Maximum number of results to yield :type limit: int :param rating: limit results to those rated (y,g, pg, pg-13 or r). :type rating: string """ assert any((term, phrase)), 'You must supply a term or phrase to search' # Phrases should have dashes and not spaces if phrase: phrase = phrase.replace(' ', '-') results_yielded = 0 # Count how many things we yield page, per_page = 0, 25 params = {'q': (term or phrase)} if rating: params.update({'rating': rating}) fetch = partial(self._fetch, 'search', **params) # Generate results until we 1) run out of pages 2) reach a limit while True: data = fetch(offset=page, limit=per_page) page += per_page # Guard for empty results if not data['data']: raise StopIteration for item in data['data']: results_yielded += 1 yield GiphyImage(item) if limit is not None and results_yielded >= limit: raise StopIteration # Check yieled limit and whether or not there are more items if (page >= data['pagination']['total_count'] or (limit is not None and results_yielded >= limit)): raise StopIteration
[ "Search", "for", "gifs", "with", "a", "given", "word", "or", "phrase", ".", "Punctuation", "is", "ignored", ".", "By", "default", "this", "will", "perform", "a", "term", "search", ".", "If", "you", "want", "to", "search", "by", "phrase", "use", "the", "phrase", "keyword", "argument", ".", "What", "s", "the", "difference", "between", "term", "and", "phrase", "searches?", "Simple", ":", "a", "term", "search", "will", "return", "results", "matching", "any", "words", "given", "whereas", "a", "phrase", "search", "will", "match", "all", "words", "." ]
shaunduncan/giphypop
python
https://github.com/shaunduncan/giphypop/blob/21e7f51c4f000ae24be3805b7eeec52bcce3d390/giphypop.py#L270-L326
[ "def", "search", "(", "self", ",", "term", "=", "None", ",", "phrase", "=", "None", ",", "limit", "=", "DEFAULT_SEARCH_LIMIT", ",", "rating", "=", "None", ")", ":", "assert", "any", "(", "(", "term", ",", "phrase", ")", ")", ",", "'You must supply a term or phrase to search'", "# Phrases should have dashes and not spaces", "if", "phrase", ":", "phrase", "=", "phrase", ".", "replace", "(", "' '", ",", "'-'", ")", "results_yielded", "=", "0", "# Count how many things we yield", "page", ",", "per_page", "=", "0", ",", "25", "params", "=", "{", "'q'", ":", "(", "term", "or", "phrase", ")", "}", "if", "rating", ":", "params", ".", "update", "(", "{", "'rating'", ":", "rating", "}", ")", "fetch", "=", "partial", "(", "self", ".", "_fetch", ",", "'search'", ",", "*", "*", "params", ")", "# Generate results until we 1) run out of pages 2) reach a limit", "while", "True", ":", "data", "=", "fetch", "(", "offset", "=", "page", ",", "limit", "=", "per_page", ")", "page", "+=", "per_page", "# Guard for empty results", "if", "not", "data", "[", "'data'", "]", ":", "raise", "StopIteration", "for", "item", "in", "data", "[", "'data'", "]", ":", "results_yielded", "+=", "1", "yield", "GiphyImage", "(", "item", ")", "if", "limit", "is", "not", "None", "and", "results_yielded", ">=", "limit", ":", "raise", "StopIteration", "# Check yieled limit and whether or not there are more items", "if", "(", "page", ">=", "data", "[", "'pagination'", "]", "[", "'total_count'", "]", "or", "(", "limit", "is", "not", "None", "and", "results_yielded", ">=", "limit", ")", ")", ":", "raise", "StopIteration" ]
21e7f51c4f000ae24be3805b7eeec52bcce3d390
test
Giphy.search_list
Suppose you expect the `search` method to just give you a list rather than a generator. This method will have that effect. Equivalent to:: >>> g = Giphy() >>> results = list(g.search('foo'))
giphypop.py
def search_list(self, term=None, phrase=None, limit=DEFAULT_SEARCH_LIMIT, rating=None): """ Suppose you expect the `search` method to just give you a list rather than a generator. This method will have that effect. Equivalent to:: >>> g = Giphy() >>> results = list(g.search('foo')) """ return list(self.search(term=term, phrase=phrase, limit=limit, rating=rating))
def search_list(self, term=None, phrase=None, limit=DEFAULT_SEARCH_LIMIT, rating=None): """ Suppose you expect the `search` method to just give you a list rather than a generator. This method will have that effect. Equivalent to:: >>> g = Giphy() >>> results = list(g.search('foo')) """ return list(self.search(term=term, phrase=phrase, limit=limit, rating=rating))
[ "Suppose", "you", "expect", "the", "search", "method", "to", "just", "give", "you", "a", "list", "rather", "than", "a", "generator", ".", "This", "method", "will", "have", "that", "effect", ".", "Equivalent", "to", "::" ]
shaunduncan/giphypop
python
https://github.com/shaunduncan/giphypop/blob/21e7f51c4f000ae24be3805b7eeec52bcce3d390/giphypop.py#L328-L338
[ "def", "search_list", "(", "self", ",", "term", "=", "None", ",", "phrase", "=", "None", ",", "limit", "=", "DEFAULT_SEARCH_LIMIT", ",", "rating", "=", "None", ")", ":", "return", "list", "(", "self", ".", "search", "(", "term", "=", "term", ",", "phrase", "=", "phrase", ",", "limit", "=", "limit", ",", "rating", "=", "rating", ")", ")" ]
21e7f51c4f000ae24be3805b7eeec52bcce3d390
test
Giphy.translate
Retrieve a single image that represents a transalation of a term or phrase into an animated gif. Punctuation is ignored. By default, this will perform a `term` translation. If you want to translate by phrase, use the `phrase` keyword argument. :param term: Search term or terms :type term: string :param phrase: Search phrase :type phrase: string :param strict: Whether an exception should be raised when no results :type strict: boolean :param rating: limit results to those rated (y,g, pg, pg-13 or r). :type rating: string
giphypop.py
def translate(self, term=None, phrase=None, strict=False, rating=None): """ Retrieve a single image that represents a transalation of a term or phrase into an animated gif. Punctuation is ignored. By default, this will perform a `term` translation. If you want to translate by phrase, use the `phrase` keyword argument. :param term: Search term or terms :type term: string :param phrase: Search phrase :type phrase: string :param strict: Whether an exception should be raised when no results :type strict: boolean :param rating: limit results to those rated (y,g, pg, pg-13 or r). :type rating: string """ assert any((term, phrase)), 'You must supply a term or phrase to search' # Phrases should have dashes and not spaces if phrase: phrase = phrase.replace(' ', '-') params = {'s': (term or phrase)} if rating: params.update({'rating': rating}) resp = self._fetch('translate', **params) if resp['data']: return GiphyImage(resp['data']) elif strict or self.strict: raise GiphyApiException( "Term/Phrase '%s' could not be translated into a GIF" % (term or phrase))
def translate(self, term=None, phrase=None, strict=False, rating=None): """ Retrieve a single image that represents a transalation of a term or phrase into an animated gif. Punctuation is ignored. By default, this will perform a `term` translation. If you want to translate by phrase, use the `phrase` keyword argument. :param term: Search term or terms :type term: string :param phrase: Search phrase :type phrase: string :param strict: Whether an exception should be raised when no results :type strict: boolean :param rating: limit results to those rated (y,g, pg, pg-13 or r). :type rating: string """ assert any((term, phrase)), 'You must supply a term or phrase to search' # Phrases should have dashes and not spaces if phrase: phrase = phrase.replace(' ', '-') params = {'s': (term or phrase)} if rating: params.update({'rating': rating}) resp = self._fetch('translate', **params) if resp['data']: return GiphyImage(resp['data']) elif strict or self.strict: raise GiphyApiException( "Term/Phrase '%s' could not be translated into a GIF" % (term or phrase))
[ "Retrieve", "a", "single", "image", "that", "represents", "a", "transalation", "of", "a", "term", "or", "phrase", "into", "an", "animated", "gif", ".", "Punctuation", "is", "ignored", ".", "By", "default", "this", "will", "perform", "a", "term", "translation", ".", "If", "you", "want", "to", "translate", "by", "phrase", "use", "the", "phrase", "keyword", "argument", "." ]
shaunduncan/giphypop
python
https://github.com/shaunduncan/giphypop/blob/21e7f51c4f000ae24be3805b7eeec52bcce3d390/giphypop.py#L340-L371
[ "def", "translate", "(", "self", ",", "term", "=", "None", ",", "phrase", "=", "None", ",", "strict", "=", "False", ",", "rating", "=", "None", ")", ":", "assert", "any", "(", "(", "term", ",", "phrase", ")", ")", ",", "'You must supply a term or phrase to search'", "# Phrases should have dashes and not spaces", "if", "phrase", ":", "phrase", "=", "phrase", ".", "replace", "(", "' '", ",", "'-'", ")", "params", "=", "{", "'s'", ":", "(", "term", "or", "phrase", ")", "}", "if", "rating", ":", "params", ".", "update", "(", "{", "'rating'", ":", "rating", "}", ")", "resp", "=", "self", ".", "_fetch", "(", "'translate'", ",", "*", "*", "params", ")", "if", "resp", "[", "'data'", "]", ":", "return", "GiphyImage", "(", "resp", "[", "'data'", "]", ")", "elif", "strict", "or", "self", ".", "strict", ":", "raise", "GiphyApiException", "(", "\"Term/Phrase '%s' could not be translated into a GIF\"", "%", "(", "term", "or", "phrase", ")", ")" ]
21e7f51c4f000ae24be3805b7eeec52bcce3d390
test
Giphy.trending
Retrieve GIFs currently trending online. The data returned mirrors that used to create The Hot 100 list of GIFs on Giphy. :param rating: limit results to those rated (y,g, pg, pg-13 or r). :type rating: string :param limit: Maximum number of results to yield :type limit: int
giphypop.py
def trending(self, rating=None, limit=DEFAULT_SEARCH_LIMIT): """ Retrieve GIFs currently trending online. The data returned mirrors that used to create The Hot 100 list of GIFs on Giphy. :param rating: limit results to those rated (y,g, pg, pg-13 or r). :type rating: string :param limit: Maximum number of results to yield :type limit: int """ results_yielded = 0 # Count how many things we yield page, per_page = 0, 25 params = {'rating': rating} if rating else {} fetch = partial(self._fetch, 'trending', **params) # Generate results until we 1) run out of pages 2) reach a limit while True: data = fetch(offset=page, limit=per_page) page += per_page # Guard for empty results if not data['data']: raise StopIteration for item in data['data']: results_yielded += 1 yield GiphyImage(item) if limit is not None and results_yielded >= limit: raise StopIteration # Check yieled limit and whether or not there are more items if (page >= data['pagination']['total_count'] or (limit is not None and results_yielded >= limit)): raise StopIteration
def trending(self, rating=None, limit=DEFAULT_SEARCH_LIMIT): """ Retrieve GIFs currently trending online. The data returned mirrors that used to create The Hot 100 list of GIFs on Giphy. :param rating: limit results to those rated (y,g, pg, pg-13 or r). :type rating: string :param limit: Maximum number of results to yield :type limit: int """ results_yielded = 0 # Count how many things we yield page, per_page = 0, 25 params = {'rating': rating} if rating else {} fetch = partial(self._fetch, 'trending', **params) # Generate results until we 1) run out of pages 2) reach a limit while True: data = fetch(offset=page, limit=per_page) page += per_page # Guard for empty results if not data['data']: raise StopIteration for item in data['data']: results_yielded += 1 yield GiphyImage(item) if limit is not None and results_yielded >= limit: raise StopIteration # Check yieled limit and whether or not there are more items if (page >= data['pagination']['total_count'] or (limit is not None and results_yielded >= limit)): raise StopIteration
[ "Retrieve", "GIFs", "currently", "trending", "online", ".", "The", "data", "returned", "mirrors", "that", "used", "to", "create", "The", "Hot", "100", "list", "of", "GIFs", "on", "Giphy", "." ]
shaunduncan/giphypop
python
https://github.com/shaunduncan/giphypop/blob/21e7f51c4f000ae24be3805b7eeec52bcce3d390/giphypop.py#L373-L408
[ "def", "trending", "(", "self", ",", "rating", "=", "None", ",", "limit", "=", "DEFAULT_SEARCH_LIMIT", ")", ":", "results_yielded", "=", "0", "# Count how many things we yield", "page", ",", "per_page", "=", "0", ",", "25", "params", "=", "{", "'rating'", ":", "rating", "}", "if", "rating", "else", "{", "}", "fetch", "=", "partial", "(", "self", ".", "_fetch", ",", "'trending'", ",", "*", "*", "params", ")", "# Generate results until we 1) run out of pages 2) reach a limit", "while", "True", ":", "data", "=", "fetch", "(", "offset", "=", "page", ",", "limit", "=", "per_page", ")", "page", "+=", "per_page", "# Guard for empty results", "if", "not", "data", "[", "'data'", "]", ":", "raise", "StopIteration", "for", "item", "in", "data", "[", "'data'", "]", ":", "results_yielded", "+=", "1", "yield", "GiphyImage", "(", "item", ")", "if", "limit", "is", "not", "None", "and", "results_yielded", ">=", "limit", ":", "raise", "StopIteration", "# Check yieled limit and whether or not there are more items", "if", "(", "page", ">=", "data", "[", "'pagination'", "]", "[", "'total_count'", "]", "or", "(", "limit", "is", "not", "None", "and", "results_yielded", ">=", "limit", ")", ")", ":", "raise", "StopIteration" ]
21e7f51c4f000ae24be3805b7eeec52bcce3d390
test
Giphy.trending_list
Suppose you expect the `trending` method to just give you a list rather than a generator. This method will have that effect. Equivalent to:: >>> g = Giphy() >>> results = list(g.trending())
giphypop.py
def trending_list(self, rating=None, limit=DEFAULT_SEARCH_LIMIT): """ Suppose you expect the `trending` method to just give you a list rather than a generator. This method will have that effect. Equivalent to:: >>> g = Giphy() >>> results = list(g.trending()) """ return list(self.trending(limit=limit, rating=rating))
def trending_list(self, rating=None, limit=DEFAULT_SEARCH_LIMIT): """ Suppose you expect the `trending` method to just give you a list rather than a generator. This method will have that effect. Equivalent to:: >>> g = Giphy() >>> results = list(g.trending()) """ return list(self.trending(limit=limit, rating=rating))
[ "Suppose", "you", "expect", "the", "trending", "method", "to", "just", "give", "you", "a", "list", "rather", "than", "a", "generator", ".", "This", "method", "will", "have", "that", "effect", ".", "Equivalent", "to", "::" ]
shaunduncan/giphypop
python
https://github.com/shaunduncan/giphypop/blob/21e7f51c4f000ae24be3805b7eeec52bcce3d390/giphypop.py#L410-L418
[ "def", "trending_list", "(", "self", ",", "rating", "=", "None", ",", "limit", "=", "DEFAULT_SEARCH_LIMIT", ")", ":", "return", "list", "(", "self", ".", "trending", "(", "limit", "=", "limit", ",", "rating", "=", "rating", ")", ")" ]
21e7f51c4f000ae24be3805b7eeec52bcce3d390
test
Giphy.gif
Retrieves a specifc gif from giphy based on unique id :param gif_id: Unique giphy gif ID :type gif_id: string :param strict: Whether an exception should be raised when no results :type strict: boolean
giphypop.py
def gif(self, gif_id, strict=False): """ Retrieves a specifc gif from giphy based on unique id :param gif_id: Unique giphy gif ID :type gif_id: string :param strict: Whether an exception should be raised when no results :type strict: boolean """ resp = self._fetch(gif_id) if resp['data']: return GiphyImage(resp['data']) elif strict or self.strict: raise GiphyApiException( "GIF with ID '%s' could not be found" % gif_id)
def gif(self, gif_id, strict=False): """ Retrieves a specifc gif from giphy based on unique id :param gif_id: Unique giphy gif ID :type gif_id: string :param strict: Whether an exception should be raised when no results :type strict: boolean """ resp = self._fetch(gif_id) if resp['data']: return GiphyImage(resp['data']) elif strict or self.strict: raise GiphyApiException( "GIF with ID '%s' could not be found" % gif_id)
[ "Retrieves", "a", "specifc", "gif", "from", "giphy", "based", "on", "unique", "id" ]
shaunduncan/giphypop
python
https://github.com/shaunduncan/giphypop/blob/21e7f51c4f000ae24be3805b7eeec52bcce3d390/giphypop.py#L420-L435
[ "def", "gif", "(", "self", ",", "gif_id", ",", "strict", "=", "False", ")", ":", "resp", "=", "self", ".", "_fetch", "(", "gif_id", ")", "if", "resp", "[", "'data'", "]", ":", "return", "GiphyImage", "(", "resp", "[", "'data'", "]", ")", "elif", "strict", "or", "self", ".", "strict", ":", "raise", "GiphyApiException", "(", "\"GIF with ID '%s' could not be found\"", "%", "gif_id", ")" ]
21e7f51c4f000ae24be3805b7eeec52bcce3d390
test
Giphy.screensaver
Returns a random giphy image, optionally based on a search of a given tag. Note that this method will both query for a screensaver image and fetch the full details of that image (2 request calls) :param tag: Tag to retrieve a screensaver image :type tag: string :param strict: Whether an exception should be raised when no results :type strict: boolean
giphypop.py
def screensaver(self, tag=None, strict=False): """ Returns a random giphy image, optionally based on a search of a given tag. Note that this method will both query for a screensaver image and fetch the full details of that image (2 request calls) :param tag: Tag to retrieve a screensaver image :type tag: string :param strict: Whether an exception should be raised when no results :type strict: boolean """ params = {'tag': tag} if tag else {} resp = self._fetch('screensaver', **params) if resp['data'] and resp['data']['id']: return self.gif(resp['data']['id']) elif strict or self.strict: raise GiphyApiException( "No screensaver GIF tagged '%s' found" % tag)
def screensaver(self, tag=None, strict=False): """ Returns a random giphy image, optionally based on a search of a given tag. Note that this method will both query for a screensaver image and fetch the full details of that image (2 request calls) :param tag: Tag to retrieve a screensaver image :type tag: string :param strict: Whether an exception should be raised when no results :type strict: boolean """ params = {'tag': tag} if tag else {} resp = self._fetch('screensaver', **params) if resp['data'] and resp['data']['id']: return self.gif(resp['data']['id']) elif strict or self.strict: raise GiphyApiException( "No screensaver GIF tagged '%s' found" % tag)
[ "Returns", "a", "random", "giphy", "image", "optionally", "based", "on", "a", "search", "of", "a", "given", "tag", ".", "Note", "that", "this", "method", "will", "both", "query", "for", "a", "screensaver", "image", "and", "fetch", "the", "full", "details", "of", "that", "image", "(", "2", "request", "calls", ")" ]
shaunduncan/giphypop
python
https://github.com/shaunduncan/giphypop/blob/21e7f51c4f000ae24be3805b7eeec52bcce3d390/giphypop.py#L437-L455
[ "def", "screensaver", "(", "self", ",", "tag", "=", "None", ",", "strict", "=", "False", ")", ":", "params", "=", "{", "'tag'", ":", "tag", "}", "if", "tag", "else", "{", "}", "resp", "=", "self", ".", "_fetch", "(", "'screensaver'", ",", "*", "*", "params", ")", "if", "resp", "[", "'data'", "]", "and", "resp", "[", "'data'", "]", "[", "'id'", "]", ":", "return", "self", ".", "gif", "(", "resp", "[", "'data'", "]", "[", "'id'", "]", ")", "elif", "strict", "or", "self", ".", "strict", ":", "raise", "GiphyApiException", "(", "\"No screensaver GIF tagged '%s' found\"", "%", "tag", ")" ]
21e7f51c4f000ae24be3805b7eeec52bcce3d390
test
Giphy.upload
Uploads a gif from the filesystem to Giphy. :param tags: Tags to apply to the uploaded image :type tags: list :param file_path: Path at which the image can be found :type file_path: string :param username: Your channel username if not using public API key
giphypop.py
def upload(self, tags, file_path, username=None): """ Uploads a gif from the filesystem to Giphy. :param tags: Tags to apply to the uploaded image :type tags: list :param file_path: Path at which the image can be found :type file_path: string :param username: Your channel username if not using public API key """ params = { 'api_key': self.api_key, 'tags': ','.join(tags) } if username is not None: params['username'] = username with open(file_path, 'rb') as f: resp = requests.post( GIPHY_UPLOAD_ENDPOINT, params=params, files={'file': f}) resp.raise_for_status() data = resp.json() self._check_or_raise(data.get('meta', {})) return self.gif(data['data']['id'])
def upload(self, tags, file_path, username=None): """ Uploads a gif from the filesystem to Giphy. :param tags: Tags to apply to the uploaded image :type tags: list :param file_path: Path at which the image can be found :type file_path: string :param username: Your channel username if not using public API key """ params = { 'api_key': self.api_key, 'tags': ','.join(tags) } if username is not None: params['username'] = username with open(file_path, 'rb') as f: resp = requests.post( GIPHY_UPLOAD_ENDPOINT, params=params, files={'file': f}) resp.raise_for_status() data = resp.json() self._check_or_raise(data.get('meta', {})) return self.gif(data['data']['id'])
[ "Uploads", "a", "gif", "from", "the", "filesystem", "to", "Giphy", "." ]
shaunduncan/giphypop
python
https://github.com/shaunduncan/giphypop/blob/21e7f51c4f000ae24be3805b7eeec52bcce3d390/giphypop.py#L460-L486
[ "def", "upload", "(", "self", ",", "tags", ",", "file_path", ",", "username", "=", "None", ")", ":", "params", "=", "{", "'api_key'", ":", "self", ".", "api_key", ",", "'tags'", ":", "','", ".", "join", "(", "tags", ")", "}", "if", "username", "is", "not", "None", ":", "params", "[", "'username'", "]", "=", "username", "with", "open", "(", "file_path", ",", "'rb'", ")", "as", "f", ":", "resp", "=", "requests", ".", "post", "(", "GIPHY_UPLOAD_ENDPOINT", ",", "params", "=", "params", ",", "files", "=", "{", "'file'", ":", "f", "}", ")", "resp", ".", "raise_for_status", "(", ")", "data", "=", "resp", ".", "json", "(", ")", "self", ".", "_check_or_raise", "(", "data", ".", "get", "(", "'meta'", ",", "{", "}", ")", ")", "return", "self", ".", "gif", "(", "data", "[", "'data'", "]", "[", "'id'", "]", ")" ]
21e7f51c4f000ae24be3805b7eeec52bcce3d390
test
OpenExcel._convert
convert '(1,1)' to 'B2' and 'B2' to '(1,1)' auto-recongnize
excel/xlrd_shortcuts.py
def _convert(self, args): '''convert '(1,1)' to 'B2' and 'B2' to '(1,1)' auto-recongnize''' if args.find(",") > -1: b, a = args.replace("(", "").replace(")", "").split(",") a = chr(int(a)+65)#chr(65) is "A" and ord("A") is 65 b = str(int(b)+1) return a+b else: a = str(int(args[1:2])-1) # D1-->(0,3) 1-->0 b = str(ord(args[0:1].upper())-65) # D1-->(0,3) D-->3 ord("D") is 68 return "("+a+","+b+")"
def _convert(self, args): '''convert '(1,1)' to 'B2' and 'B2' to '(1,1)' auto-recongnize''' if args.find(",") > -1: b, a = args.replace("(", "").replace(")", "").split(",") a = chr(int(a)+65)#chr(65) is "A" and ord("A") is 65 b = str(int(b)+1) return a+b else: a = str(int(args[1:2])-1) # D1-->(0,3) 1-->0 b = str(ord(args[0:1].upper())-65) # D1-->(0,3) D-->3 ord("D") is 68 return "("+a+","+b+")"
[ "convert", "(", "1", "1", ")", "to", "B2", "and", "B2", "to", "(", "1", "1", ")", "auto", "-", "recongnize" ]
twz915/excel
python
https://github.com/twz915/excel/blob/6a2c1d39c29d12f3b5724913902de6088cbc253f/excel/xlrd_shortcuts.py#L69-L79
[ "def", "_convert", "(", "self", ",", "args", ")", ":", "if", "args", ".", "find", "(", "\",\"", ")", ">", "-", "1", ":", "b", ",", "a", "=", "args", ".", "replace", "(", "\"(\"", ",", "\"\"", ")", ".", "replace", "(", "\")\"", ",", "\"\"", ")", ".", "split", "(", "\",\"", ")", "a", "=", "chr", "(", "int", "(", "a", ")", "+", "65", ")", "#chr(65) is \"A\" and ord(\"A\") is 65", "b", "=", "str", "(", "int", "(", "b", ")", "+", "1", ")", "return", "a", "+", "b", "else", ":", "a", "=", "str", "(", "int", "(", "args", "[", "1", ":", "2", "]", ")", "-", "1", ")", "# D1-->(0,3) 1-->0", "b", "=", "str", "(", "ord", "(", "args", "[", "0", ":", "1", "]", ".", "upper", "(", ")", ")", "-", "65", ")", "# D1-->(0,3) D-->3 ord(\"D\") is 68", "return", "\"(\"", "+", "a", "+", "\",\"", "+", "b", "+", "\")\"" ]
6a2c1d39c29d12f3b5724913902de6088cbc253f
test
Api._access_control
Prepares the extension element for access control Extension element is the optional parameter for the YouTubeVideoEntry We use extension element to modify access control settings Returns: tuple of extension elements
django_youtube/api.py
def _access_control(self, access_control, my_media_group=None): """ Prepares the extension element for access control Extension element is the optional parameter for the YouTubeVideoEntry We use extension element to modify access control settings Returns: tuple of extension elements """ # Access control extension = None if access_control is AccessControl.Private: # WARNING: this part of code is not tested # set video as private if my_media_group: my_media_group.private = gdata.media.Private() elif access_control is AccessControl.Unlisted: # set video as unlisted from gdata.media import YOUTUBE_NAMESPACE from atom import ExtensionElement kwargs = { "namespace": YOUTUBE_NAMESPACE, "attributes": {'action': 'list', 'permission': 'denied'}, } extension = ([ExtensionElement('accessControl', **kwargs)]) return extension
def _access_control(self, access_control, my_media_group=None): """ Prepares the extension element for access control Extension element is the optional parameter for the YouTubeVideoEntry We use extension element to modify access control settings Returns: tuple of extension elements """ # Access control extension = None if access_control is AccessControl.Private: # WARNING: this part of code is not tested # set video as private if my_media_group: my_media_group.private = gdata.media.Private() elif access_control is AccessControl.Unlisted: # set video as unlisted from gdata.media import YOUTUBE_NAMESPACE from atom import ExtensionElement kwargs = { "namespace": YOUTUBE_NAMESPACE, "attributes": {'action': 'list', 'permission': 'denied'}, } extension = ([ExtensionElement('accessControl', **kwargs)]) return extension
[ "Prepares", "the", "extension", "element", "for", "access", "control", "Extension", "element", "is", "the", "optional", "parameter", "for", "the", "YouTubeVideoEntry", "We", "use", "extension", "element", "to", "modify", "access", "control", "settings" ]
laplacesdemon/django-youtube
python
https://github.com/laplacesdemon/django-youtube/blob/8051ef372473eccb053f773c68e2e5e1b2cfb538/django_youtube/api.py#L63-L88
[ "def", "_access_control", "(", "self", ",", "access_control", ",", "my_media_group", "=", "None", ")", ":", "# Access control", "extension", "=", "None", "if", "access_control", "is", "AccessControl", ".", "Private", ":", "# WARNING: this part of code is not tested", "# set video as private", "if", "my_media_group", ":", "my_media_group", ".", "private", "=", "gdata", ".", "media", ".", "Private", "(", ")", "elif", "access_control", "is", "AccessControl", ".", "Unlisted", ":", "# set video as unlisted", "from", "gdata", ".", "media", "import", "YOUTUBE_NAMESPACE", "from", "atom", "import", "ExtensionElement", "kwargs", "=", "{", "\"namespace\"", ":", "YOUTUBE_NAMESPACE", ",", "\"attributes\"", ":", "{", "'action'", ":", "'list'", ",", "'permission'", ":", "'denied'", "}", ",", "}", "extension", "=", "(", "[", "ExtensionElement", "(", "'accessControl'", ",", "*", "*", "kwargs", ")", "]", ")", "return", "extension" ]
8051ef372473eccb053f773c68e2e5e1b2cfb538
test
Api.fetch_feed_by_username
Retrieve the video feed by username Returns: gdata.youtube.YouTubeVideoFeed object
django_youtube/api.py
def fetch_feed_by_username(self, username): """ Retrieve the video feed by username Returns: gdata.youtube.YouTubeVideoFeed object """ # Don't use trailing slash youtube_url = 'http://gdata.youtube.com/feeds/api' uri = os.sep.join([youtube_url, "users", username, "uploads"]) return Api.yt_service.GetYouTubeVideoFeed(uri)
def fetch_feed_by_username(self, username): """ Retrieve the video feed by username Returns: gdata.youtube.YouTubeVideoFeed object """ # Don't use trailing slash youtube_url = 'http://gdata.youtube.com/feeds/api' uri = os.sep.join([youtube_url, "users", username, "uploads"]) return Api.yt_service.GetYouTubeVideoFeed(uri)
[ "Retrieve", "the", "video", "feed", "by", "username", "Returns", ":", "gdata", ".", "youtube", ".", "YouTubeVideoFeed", "object" ]
laplacesdemon/django-youtube
python
https://github.com/laplacesdemon/django-youtube/blob/8051ef372473eccb053f773c68e2e5e1b2cfb538/django_youtube/api.py#L97-L106
[ "def", "fetch_feed_by_username", "(", "self", ",", "username", ")", ":", "# Don't use trailing slash", "youtube_url", "=", "'http://gdata.youtube.com/feeds/api'", "uri", "=", "os", ".", "sep", ".", "join", "(", "[", "youtube_url", ",", "\"users\"", ",", "username", ",", "\"uploads\"", "]", ")", "return", "Api", ".", "yt_service", ".", "GetYouTubeVideoFeed", "(", "uri", ")" ]
8051ef372473eccb053f773c68e2e5e1b2cfb538
test
Api.authenticate
Authenticates the user and sets the GData Auth token. All params are optional, if not set, we will use the ones on the settings, if no settings found, raises AttributeError params are email, password and source. Source is the app id Raises: gdata.service.exceptions.BadAuthentication
django_youtube/api.py
def authenticate(self, email=None, password=None, source=None): """ Authenticates the user and sets the GData Auth token. All params are optional, if not set, we will use the ones on the settings, if no settings found, raises AttributeError params are email, password and source. Source is the app id Raises: gdata.service.exceptions.BadAuthentication """ from gdata.service import BadAuthentication # Auth parameters Api.yt_service.email = email if email else settings.YOUTUBE_AUTH_EMAIL Api.yt_service.password = password if password else settings.YOUTUBE_AUTH_PASSWORD Api.yt_service.source = source if source else settings.YOUTUBE_CLIENT_ID try: Api.yt_service.ProgrammaticLogin() self.authenticated = True except BadAuthentication: raise ApiError(_("Incorrect username or password"))
def authenticate(self, email=None, password=None, source=None): """ Authenticates the user and sets the GData Auth token. All params are optional, if not set, we will use the ones on the settings, if no settings found, raises AttributeError params are email, password and source. Source is the app id Raises: gdata.service.exceptions.BadAuthentication """ from gdata.service import BadAuthentication # Auth parameters Api.yt_service.email = email if email else settings.YOUTUBE_AUTH_EMAIL Api.yt_service.password = password if password else settings.YOUTUBE_AUTH_PASSWORD Api.yt_service.source = source if source else settings.YOUTUBE_CLIENT_ID try: Api.yt_service.ProgrammaticLogin() self.authenticated = True except BadAuthentication: raise ApiError(_("Incorrect username or password"))
[ "Authenticates", "the", "user", "and", "sets", "the", "GData", "Auth", "token", ".", "All", "params", "are", "optional", "if", "not", "set", "we", "will", "use", "the", "ones", "on", "the", "settings", "if", "no", "settings", "found", "raises", "AttributeError", "params", "are", "email", "password", "and", "source", ".", "Source", "is", "the", "app", "id" ]
laplacesdemon/django-youtube
python
https://github.com/laplacesdemon/django-youtube/blob/8051ef372473eccb053f773c68e2e5e1b2cfb538/django_youtube/api.py#L108-L127
[ "def", "authenticate", "(", "self", ",", "email", "=", "None", ",", "password", "=", "None", ",", "source", "=", "None", ")", ":", "from", "gdata", ".", "service", "import", "BadAuthentication", "# Auth parameters", "Api", ".", "yt_service", ".", "email", "=", "email", "if", "email", "else", "settings", ".", "YOUTUBE_AUTH_EMAIL", "Api", ".", "yt_service", ".", "password", "=", "password", "if", "password", "else", "settings", ".", "YOUTUBE_AUTH_PASSWORD", "Api", ".", "yt_service", ".", "source", "=", "source", "if", "source", "else", "settings", ".", "YOUTUBE_CLIENT_ID", "try", ":", "Api", ".", "yt_service", ".", "ProgrammaticLogin", "(", ")", "self", ".", "authenticated", "=", "True", "except", "BadAuthentication", ":", "raise", "ApiError", "(", "_", "(", "\"Incorrect username or password\"", ")", ")" ]
8051ef372473eccb053f773c68e2e5e1b2cfb538
test
Api.upload_direct
Direct upload method: Uploads the video directly from your server to Youtube and creates a video Returns: gdata.youtube.YouTubeVideoEntry See: https://developers.google.com/youtube/1.0/developers_guide_python#UploadingVideos
django_youtube/api.py
def upload_direct(self, video_path, title, description="", keywords="", developer_tags=None, access_control=AccessControl.Public): """ Direct upload method: Uploads the video directly from your server to Youtube and creates a video Returns: gdata.youtube.YouTubeVideoEntry See: https://developers.google.com/youtube/1.0/developers_guide_python#UploadingVideos """ # prepare a media group object to hold our video's meta-data my_media_group = gdata.media.Group( title=gdata.media.Title(text=title), description=gdata.media.Description(description_type='plain', text=description), keywords=gdata.media.Keywords(text=keywords), category=[gdata.media.Category( text='Autos', scheme='http://gdata.youtube.com/schemas/2007/categories.cat', label='Autos')], #player = None ) # Access Control extension = self._access_control(access_control, my_media_group) # create the gdata.youtube.YouTubeVideoEntry to be uploaded video_entry = gdata.youtube.YouTubeVideoEntry(media=my_media_group, extension_elements=extension) # add developer tags if developer_tags: video_entry.AddDeveloperTags(developer_tags) # upload the video and create a new entry new_entry = Api.yt_service.InsertVideoEntry(video_entry, video_path) return new_entry
def upload_direct(self, video_path, title, description="", keywords="", developer_tags=None, access_control=AccessControl.Public): """ Direct upload method: Uploads the video directly from your server to Youtube and creates a video Returns: gdata.youtube.YouTubeVideoEntry See: https://developers.google.com/youtube/1.0/developers_guide_python#UploadingVideos """ # prepare a media group object to hold our video's meta-data my_media_group = gdata.media.Group( title=gdata.media.Title(text=title), description=gdata.media.Description(description_type='plain', text=description), keywords=gdata.media.Keywords(text=keywords), category=[gdata.media.Category( text='Autos', scheme='http://gdata.youtube.com/schemas/2007/categories.cat', label='Autos')], #player = None ) # Access Control extension = self._access_control(access_control, my_media_group) # create the gdata.youtube.YouTubeVideoEntry to be uploaded video_entry = gdata.youtube.YouTubeVideoEntry(media=my_media_group, extension_elements=extension) # add developer tags if developer_tags: video_entry.AddDeveloperTags(developer_tags) # upload the video and create a new entry new_entry = Api.yt_service.InsertVideoEntry(video_entry, video_path) return new_entry
[ "Direct", "upload", "method", ":", "Uploads", "the", "video", "directly", "from", "your", "server", "to", "Youtube", "and", "creates", "a", "video" ]
laplacesdemon/django-youtube
python
https://github.com/laplacesdemon/django-youtube/blob/8051ef372473eccb053f773c68e2e5e1b2cfb538/django_youtube/api.py#L129-L165
[ "def", "upload_direct", "(", "self", ",", "video_path", ",", "title", ",", "description", "=", "\"\"", ",", "keywords", "=", "\"\"", ",", "developer_tags", "=", "None", ",", "access_control", "=", "AccessControl", ".", "Public", ")", ":", "# prepare a media group object to hold our video's meta-data", "my_media_group", "=", "gdata", ".", "media", ".", "Group", "(", "title", "=", "gdata", ".", "media", ".", "Title", "(", "text", "=", "title", ")", ",", "description", "=", "gdata", ".", "media", ".", "Description", "(", "description_type", "=", "'plain'", ",", "text", "=", "description", ")", ",", "keywords", "=", "gdata", ".", "media", ".", "Keywords", "(", "text", "=", "keywords", ")", ",", "category", "=", "[", "gdata", ".", "media", ".", "Category", "(", "text", "=", "'Autos'", ",", "scheme", "=", "'http://gdata.youtube.com/schemas/2007/categories.cat'", ",", "label", "=", "'Autos'", ")", "]", ",", "#player = None", ")", "# Access Control", "extension", "=", "self", ".", "_access_control", "(", "access_control", ",", "my_media_group", ")", "# create the gdata.youtube.YouTubeVideoEntry to be uploaded", "video_entry", "=", "gdata", ".", "youtube", ".", "YouTubeVideoEntry", "(", "media", "=", "my_media_group", ",", "extension_elements", "=", "extension", ")", "# add developer tags", "if", "developer_tags", ":", "video_entry", ".", "AddDeveloperTags", "(", "developer_tags", ")", "# upload the video and create a new entry", "new_entry", "=", "Api", ".", "yt_service", ".", "InsertVideoEntry", "(", "video_entry", ",", "video_path", ")", "return", "new_entry" ]
8051ef372473eccb053f773c68e2e5e1b2cfb538
test
Api.upload
Browser based upload Creates the video entry and meta data to initiate a browser upload Authentication is needed Params: title: string description: string keywords: comma seperated string developer_tags: tuple Return: dict contains post_url and youtube_token. i.e { 'post_url': post_url, 'youtube_token': youtube_token } Raises: ApiError: on no authentication
django_youtube/api.py
def upload(self, title, description="", keywords="", developer_tags=None, access_control=AccessControl.Public): """ Browser based upload Creates the video entry and meta data to initiate a browser upload Authentication is needed Params: title: string description: string keywords: comma seperated string developer_tags: tuple Return: dict contains post_url and youtube_token. i.e { 'post_url': post_url, 'youtube_token': youtube_token } Raises: ApiError: on no authentication """ # Raise ApiError if not authenticated if not self.authenticated: raise ApiError(_("Authentication is required")) # create media group my_media_group = gdata.media.Group( title=gdata.media.Title(text=title), description=gdata.media.Description(description_type='plain', text=description), keywords=gdata.media.Keywords(text=keywords), category=[gdata.media.Category( text='Autos', scheme='http://gdata.youtube.com/schemas/2007/categories.cat', label='Autos')], #player = None ) # Access Control extension = self._access_control(access_control, my_media_group) # create video entry video_entry = gdata.youtube.YouTubeVideoEntry( media=my_media_group, extension_elements=extension) # add developer tags if developer_tags: video_entry.AddDeveloperTags(developer_tags) # upload meta data only response = Api.yt_service.GetFormUploadToken(video_entry) # parse response tuple and use the variables to build a form post_url = response[0] youtube_token = response[1] return {'post_url': post_url, 'youtube_token': youtube_token}
def upload(self, title, description="", keywords="", developer_tags=None, access_control=AccessControl.Public): """ Browser based upload Creates the video entry and meta data to initiate a browser upload Authentication is needed Params: title: string description: string keywords: comma seperated string developer_tags: tuple Return: dict contains post_url and youtube_token. i.e { 'post_url': post_url, 'youtube_token': youtube_token } Raises: ApiError: on no authentication """ # Raise ApiError if not authenticated if not self.authenticated: raise ApiError(_("Authentication is required")) # create media group my_media_group = gdata.media.Group( title=gdata.media.Title(text=title), description=gdata.media.Description(description_type='plain', text=description), keywords=gdata.media.Keywords(text=keywords), category=[gdata.media.Category( text='Autos', scheme='http://gdata.youtube.com/schemas/2007/categories.cat', label='Autos')], #player = None ) # Access Control extension = self._access_control(access_control, my_media_group) # create video entry video_entry = gdata.youtube.YouTubeVideoEntry( media=my_media_group, extension_elements=extension) # add developer tags if developer_tags: video_entry.AddDeveloperTags(developer_tags) # upload meta data only response = Api.yt_service.GetFormUploadToken(video_entry) # parse response tuple and use the variables to build a form post_url = response[0] youtube_token = response[1] return {'post_url': post_url, 'youtube_token': youtube_token}
[ "Browser", "based", "upload", "Creates", "the", "video", "entry", "and", "meta", "data", "to", "initiate", "a", "browser", "upload" ]
laplacesdemon/django-youtube
python
https://github.com/laplacesdemon/django-youtube/blob/8051ef372473eccb053f773c68e2e5e1b2cfb538/django_youtube/api.py#L167-L221
[ "def", "upload", "(", "self", ",", "title", ",", "description", "=", "\"\"", ",", "keywords", "=", "\"\"", ",", "developer_tags", "=", "None", ",", "access_control", "=", "AccessControl", ".", "Public", ")", ":", "# Raise ApiError if not authenticated", "if", "not", "self", ".", "authenticated", ":", "raise", "ApiError", "(", "_", "(", "\"Authentication is required\"", ")", ")", "# create media group", "my_media_group", "=", "gdata", ".", "media", ".", "Group", "(", "title", "=", "gdata", ".", "media", ".", "Title", "(", "text", "=", "title", ")", ",", "description", "=", "gdata", ".", "media", ".", "Description", "(", "description_type", "=", "'plain'", ",", "text", "=", "description", ")", ",", "keywords", "=", "gdata", ".", "media", ".", "Keywords", "(", "text", "=", "keywords", ")", ",", "category", "=", "[", "gdata", ".", "media", ".", "Category", "(", "text", "=", "'Autos'", ",", "scheme", "=", "'http://gdata.youtube.com/schemas/2007/categories.cat'", ",", "label", "=", "'Autos'", ")", "]", ",", "#player = None", ")", "# Access Control", "extension", "=", "self", ".", "_access_control", "(", "access_control", ",", "my_media_group", ")", "# create video entry", "video_entry", "=", "gdata", ".", "youtube", ".", "YouTubeVideoEntry", "(", "media", "=", "my_media_group", ",", "extension_elements", "=", "extension", ")", "# add developer tags", "if", "developer_tags", ":", "video_entry", ".", "AddDeveloperTags", "(", "developer_tags", ")", "# upload meta data only", "response", "=", "Api", ".", "yt_service", ".", "GetFormUploadToken", "(", "video_entry", ")", "# parse response tuple and use the variables to build a form", "post_url", "=", "response", "[", "0", "]", "youtube_token", "=", "response", "[", "1", "]", "return", "{", "'post_url'", ":", "post_url", ",", "'youtube_token'", ":", "youtube_token", "}" ]
8051ef372473eccb053f773c68e2e5e1b2cfb538
test
Api.check_upload_status
Checks the video upload status Newly uploaded videos may be in the processing state Authentication is required Returns: True if video is available otherwise a dict containes upload_state and detailed message i.e. {"upload_state": "processing", "detailed_message": ""}
django_youtube/api.py
def check_upload_status(self, video_id): """ Checks the video upload status Newly uploaded videos may be in the processing state Authentication is required Returns: True if video is available otherwise a dict containes upload_state and detailed message i.e. {"upload_state": "processing", "detailed_message": ""} """ # Raise ApiError if not authenticated if not self.authenticated: raise ApiError(_("Authentication is required")) entry = self.fetch_video(video_id) upload_status = Api.yt_service.CheckUploadStatus(entry) if upload_status is not None: video_upload_state = upload_status[0] detailed_message = upload_status[1] return {"upload_state": video_upload_state, "detailed_message": detailed_message} else: return True
def check_upload_status(self, video_id): """ Checks the video upload status Newly uploaded videos may be in the processing state Authentication is required Returns: True if video is available otherwise a dict containes upload_state and detailed message i.e. {"upload_state": "processing", "detailed_message": ""} """ # Raise ApiError if not authenticated if not self.authenticated: raise ApiError(_("Authentication is required")) entry = self.fetch_video(video_id) upload_status = Api.yt_service.CheckUploadStatus(entry) if upload_status is not None: video_upload_state = upload_status[0] detailed_message = upload_status[1] return {"upload_state": video_upload_state, "detailed_message": detailed_message} else: return True
[ "Checks", "the", "video", "upload", "status", "Newly", "uploaded", "videos", "may", "be", "in", "the", "processing", "state" ]
laplacesdemon/django-youtube
python
https://github.com/laplacesdemon/django-youtube/blob/8051ef372473eccb053f773c68e2e5e1b2cfb538/django_youtube/api.py#L223-L247
[ "def", "check_upload_status", "(", "self", ",", "video_id", ")", ":", "# Raise ApiError if not authenticated", "if", "not", "self", ".", "authenticated", ":", "raise", "ApiError", "(", "_", "(", "\"Authentication is required\"", ")", ")", "entry", "=", "self", ".", "fetch_video", "(", "video_id", ")", "upload_status", "=", "Api", ".", "yt_service", ".", "CheckUploadStatus", "(", "entry", ")", "if", "upload_status", "is", "not", "None", ":", "video_upload_state", "=", "upload_status", "[", "0", "]", "detailed_message", "=", "upload_status", "[", "1", "]", "return", "{", "\"upload_state\"", ":", "video_upload_state", ",", "\"detailed_message\"", ":", "detailed_message", "}", "else", ":", "return", "True" ]
8051ef372473eccb053f773c68e2e5e1b2cfb538
test
Api.update_video
Updates the video Authentication is required Params: entry: video entry fetch via 'fetch_video()' title: string description: string keywords: string Returns: a video entry on success None otherwise
django_youtube/api.py
def update_video(self, video_id, title="", description="", keywords="", access_control=AccessControl.Unlisted): """ Updates the video Authentication is required Params: entry: video entry fetch via 'fetch_video()' title: string description: string keywords: string Returns: a video entry on success None otherwise """ # Raise ApiError if not authenticated if not self.authenticated: raise ApiError(_("Authentication is required")) entry = self.fetch_video(video_id) # Set Access Control extension = self._access_control(access_control) if extension: entry.extension_elements = extension if title: entry.media.title.text = title if description: entry.media.description.text = description #if keywords: # entry.media.keywords.text = keywords success = Api.yt_service.UpdateVideoEntry(entry) return success
def update_video(self, video_id, title="", description="", keywords="", access_control=AccessControl.Unlisted): """ Updates the video Authentication is required Params: entry: video entry fetch via 'fetch_video()' title: string description: string keywords: string Returns: a video entry on success None otherwise """ # Raise ApiError if not authenticated if not self.authenticated: raise ApiError(_("Authentication is required")) entry = self.fetch_video(video_id) # Set Access Control extension = self._access_control(access_control) if extension: entry.extension_elements = extension if title: entry.media.title.text = title if description: entry.media.description.text = description #if keywords: # entry.media.keywords.text = keywords success = Api.yt_service.UpdateVideoEntry(entry) return success
[ "Updates", "the", "video" ]
laplacesdemon/django-youtube
python
https://github.com/laplacesdemon/django-youtube/blob/8051ef372473eccb053f773c68e2e5e1b2cfb538/django_youtube/api.py#L249-L287
[ "def", "update_video", "(", "self", ",", "video_id", ",", "title", "=", "\"\"", ",", "description", "=", "\"\"", ",", "keywords", "=", "\"\"", ",", "access_control", "=", "AccessControl", ".", "Unlisted", ")", ":", "# Raise ApiError if not authenticated", "if", "not", "self", ".", "authenticated", ":", "raise", "ApiError", "(", "_", "(", "\"Authentication is required\"", ")", ")", "entry", "=", "self", ".", "fetch_video", "(", "video_id", ")", "# Set Access Control", "extension", "=", "self", ".", "_access_control", "(", "access_control", ")", "if", "extension", ":", "entry", ".", "extension_elements", "=", "extension", "if", "title", ":", "entry", ".", "media", ".", "title", ".", "text", "=", "title", "if", "description", ":", "entry", ".", "media", ".", "description", ".", "text", "=", "description", "#if keywords:", "# entry.media.keywords.text = keywords", "success", "=", "Api", ".", "yt_service", ".", "UpdateVideoEntry", "(", "entry", ")", "return", "success" ]
8051ef372473eccb053f773c68e2e5e1b2cfb538
test
Api.delete_video
Deletes the video Authentication is required Params: entry: video entry fetch via 'fetch_video()' Return: True if successful Raise: OperationError: on unsuccessful deletion
django_youtube/api.py
def delete_video(self, video_id): """ Deletes the video Authentication is required Params: entry: video entry fetch via 'fetch_video()' Return: True if successful Raise: OperationError: on unsuccessful deletion """ # Raise ApiError if not authenticated if not self.authenticated: raise ApiError(_("Authentication is required")) entry = self.fetch_video(video_id) response = Api.yt_service.DeleteVideoEntry(entry) if not response: raise OperationError(_("Cannot be deleted from Youtube")) return True
def delete_video(self, video_id): """ Deletes the video Authentication is required Params: entry: video entry fetch via 'fetch_video()' Return: True if successful Raise: OperationError: on unsuccessful deletion """ # Raise ApiError if not authenticated if not self.authenticated: raise ApiError(_("Authentication is required")) entry = self.fetch_video(video_id) response = Api.yt_service.DeleteVideoEntry(entry) if not response: raise OperationError(_("Cannot be deleted from Youtube")) return True
[ "Deletes", "the", "video" ]
laplacesdemon/django-youtube
python
https://github.com/laplacesdemon/django-youtube/blob/8051ef372473eccb053f773c68e2e5e1b2cfb538/django_youtube/api.py#L291-L316
[ "def", "delete_video", "(", "self", ",", "video_id", ")", ":", "# Raise ApiError if not authenticated", "if", "not", "self", ".", "authenticated", ":", "raise", "ApiError", "(", "_", "(", "\"Authentication is required\"", ")", ")", "entry", "=", "self", ".", "fetch_video", "(", "video_id", ")", "response", "=", "Api", ".", "yt_service", ".", "DeleteVideoEntry", "(", "entry", ")", "if", "not", "response", ":", "raise", "OperationError", "(", "_", "(", "\"Cannot be deleted from Youtube\"", ")", ")", "return", "True" ]
8051ef372473eccb053f773c68e2e5e1b2cfb538
test
check_video_availability
Controls the availability of the video. Newly uploaded videos are in processing stage. And others might be rejected. Returns: json response
django_youtube/views.py
def check_video_availability(request, video_id): """ Controls the availability of the video. Newly uploaded videos are in processing stage. And others might be rejected. Returns: json response """ # Check video availability # Available states are: processing api = Api() api.authenticate() availability = api.check_upload_status(video_id) if availability is not True: data = {'success': False} else: data = {'success': True} return HttpResponse(json.dumps(data), content_type="application/json")
def check_video_availability(request, video_id): """ Controls the availability of the video. Newly uploaded videos are in processing stage. And others might be rejected. Returns: json response """ # Check video availability # Available states are: processing api = Api() api.authenticate() availability = api.check_upload_status(video_id) if availability is not True: data = {'success': False} else: data = {'success': True} return HttpResponse(json.dumps(data), content_type="application/json")
[ "Controls", "the", "availability", "of", "the", "video", ".", "Newly", "uploaded", "videos", "are", "in", "processing", "stage", ".", "And", "others", "might", "be", "rejected", "." ]
laplacesdemon/django-youtube
python
https://github.com/laplacesdemon/django-youtube/blob/8051ef372473eccb053f773c68e2e5e1b2cfb538/django_youtube/views.py#L29-L48
[ "def", "check_video_availability", "(", "request", ",", "video_id", ")", ":", "# Check video availability", "# Available states are: processing", "api", "=", "Api", "(", ")", "api", ".", "authenticate", "(", ")", "availability", "=", "api", ".", "check_upload_status", "(", "video_id", ")", "if", "availability", "is", "not", "True", ":", "data", "=", "{", "'success'", ":", "False", "}", "else", ":", "data", "=", "{", "'success'", ":", "True", "}", "return", "HttpResponse", "(", "json", ".", "dumps", "(", "data", ")", ",", "content_type", "=", "\"application/json\"", ")" ]
8051ef372473eccb053f773c68e2e5e1b2cfb538
test
video
Displays a video in an embed player
django_youtube/views.py
def video(request, video_id): """ Displays a video in an embed player """ # Check video availability # Available states are: processing api = Api() api.authenticate() availability = api.check_upload_status(video_id) if availability is not True: # Video is not available video = Video.objects.filter(video_id=video_id).get() state = availability["upload_state"] # Add additional states here. I'm not sure what states are available if state == "failed" or state == "rejected": return render_to_response( "django_youtube/video_failed.html", {"video": video, "video_id": video_id, "message": _("Invalid video."), "availability": availability}, context_instance=RequestContext(request) ) else: return render_to_response( "django_youtube/video_unavailable.html", {"video": video, "video_id": video_id, "message": _("This video is currently being processed"), "availability": availability}, context_instance=RequestContext(request) ) video_params = _video_params(request, video_id) return render_to_response( "django_youtube/video.html", video_params, context_instance=RequestContext(request) )
def video(request, video_id): """ Displays a video in an embed player """ # Check video availability # Available states are: processing api = Api() api.authenticate() availability = api.check_upload_status(video_id) if availability is not True: # Video is not available video = Video.objects.filter(video_id=video_id).get() state = availability["upload_state"] # Add additional states here. I'm not sure what states are available if state == "failed" or state == "rejected": return render_to_response( "django_youtube/video_failed.html", {"video": video, "video_id": video_id, "message": _("Invalid video."), "availability": availability}, context_instance=RequestContext(request) ) else: return render_to_response( "django_youtube/video_unavailable.html", {"video": video, "video_id": video_id, "message": _("This video is currently being processed"), "availability": availability}, context_instance=RequestContext(request) ) video_params = _video_params(request, video_id) return render_to_response( "django_youtube/video.html", video_params, context_instance=RequestContext(request) )
[ "Displays", "a", "video", "in", "an", "embed", "player" ]
laplacesdemon/django-youtube
python
https://github.com/laplacesdemon/django-youtube/blob/8051ef372473eccb053f773c68e2e5e1b2cfb538/django_youtube/views.py#L51-L90
[ "def", "video", "(", "request", ",", "video_id", ")", ":", "# Check video availability", "# Available states are: processing", "api", "=", "Api", "(", ")", "api", ".", "authenticate", "(", ")", "availability", "=", "api", ".", "check_upload_status", "(", "video_id", ")", "if", "availability", "is", "not", "True", ":", "# Video is not available", "video", "=", "Video", ".", "objects", ".", "filter", "(", "video_id", "=", "video_id", ")", ".", "get", "(", ")", "state", "=", "availability", "[", "\"upload_state\"", "]", "# Add additional states here. I'm not sure what states are available", "if", "state", "==", "\"failed\"", "or", "state", "==", "\"rejected\"", ":", "return", "render_to_response", "(", "\"django_youtube/video_failed.html\"", ",", "{", "\"video\"", ":", "video", ",", "\"video_id\"", ":", "video_id", ",", "\"message\"", ":", "_", "(", "\"Invalid video.\"", ")", ",", "\"availability\"", ":", "availability", "}", ",", "context_instance", "=", "RequestContext", "(", "request", ")", ")", "else", ":", "return", "render_to_response", "(", "\"django_youtube/video_unavailable.html\"", ",", "{", "\"video\"", ":", "video", ",", "\"video_id\"", ":", "video_id", ",", "\"message\"", ":", "_", "(", "\"This video is currently being processed\"", ")", ",", "\"availability\"", ":", "availability", "}", ",", "context_instance", "=", "RequestContext", "(", "request", ")", ")", "video_params", "=", "_video_params", "(", "request", ",", "video_id", ")", "return", "render_to_response", "(", "\"django_youtube/video.html\"", ",", "video_params", ",", "context_instance", "=", "RequestContext", "(", "request", ")", ")" ]
8051ef372473eccb053f773c68e2e5e1b2cfb538
test
video_list
list of videos of a user if username does not set, shows the currently logged in user
django_youtube/views.py
def video_list(request, username=None): """ list of videos of a user if username does not set, shows the currently logged in user """ # If user is not authenticated and username is None, raise an error if username is None and not request.user.is_authenticated(): from django.http import Http404 raise Http404 from django.contrib.auth.models import User user = User.objects.get(username=username) if username else request.user # loop through the videos of the user videos = Video.objects.filter(user=user).all() video_params = [] for video in videos: video_params.append(_video_params(request, video.video_id)) return render_to_response( "django_youtube/videos.html", {"video_params": video_params}, context_instance=RequestContext(request) )
def video_list(request, username=None): """ list of videos of a user if username does not set, shows the currently logged in user """ # If user is not authenticated and username is None, raise an error if username is None and not request.user.is_authenticated(): from django.http import Http404 raise Http404 from django.contrib.auth.models import User user = User.objects.get(username=username) if username else request.user # loop through the videos of the user videos = Video.objects.filter(user=user).all() video_params = [] for video in videos: video_params.append(_video_params(request, video.video_id)) return render_to_response( "django_youtube/videos.html", {"video_params": video_params}, context_instance=RequestContext(request) )
[ "list", "of", "videos", "of", "a", "user", "if", "username", "does", "not", "set", "shows", "the", "currently", "logged", "in", "user" ]
laplacesdemon/django-youtube
python
https://github.com/laplacesdemon/django-youtube/blob/8051ef372473eccb053f773c68e2e5e1b2cfb538/django_youtube/views.py#L93-L117
[ "def", "video_list", "(", "request", ",", "username", "=", "None", ")", ":", "# If user is not authenticated and username is None, raise an error", "if", "username", "is", "None", "and", "not", "request", ".", "user", ".", "is_authenticated", "(", ")", ":", "from", "django", ".", "http", "import", "Http404", "raise", "Http404", "from", "django", ".", "contrib", ".", "auth", ".", "models", "import", "User", "user", "=", "User", ".", "objects", ".", "get", "(", "username", "=", "username", ")", "if", "username", "else", "request", ".", "user", "# loop through the videos of the user", "videos", "=", "Video", ".", "objects", ".", "filter", "(", "user", "=", "user", ")", ".", "all", "(", ")", "video_params", "=", "[", "]", "for", "video", "in", "videos", ":", "video_params", ".", "append", "(", "_video_params", "(", "request", ",", "video", ".", "video_id", ")", ")", "return", "render_to_response", "(", "\"django_youtube/videos.html\"", ",", "{", "\"video_params\"", ":", "video_params", "}", ",", "context_instance", "=", "RequestContext", "(", "request", ")", ")" ]
8051ef372473eccb053f773c68e2e5e1b2cfb538
test
direct_upload
direct upload method starts with uploading video to our server then sends the video file to youtube param: (optional) `only_data`: if set, a json response is returns i.e. {'video_id':'124weg'} return: if `only_data` set, a json object. otherwise redirects to the video display page
django_youtube/views.py
def direct_upload(request): """ direct upload method starts with uploading video to our server then sends the video file to youtube param: (optional) `only_data`: if set, a json response is returns i.e. {'video_id':'124weg'} return: if `only_data` set, a json object. otherwise redirects to the video display page """ if request.method == "POST": try: form = YoutubeDirectUploadForm(request.POST, request.FILES) # upload the file to our server if form.is_valid(): uploaded_video = form.save() # send this file to youtube api = Api() api.authenticate() video_entry = api.upload_direct(uploaded_video.file_on_server.path, "Uploaded video from zuqqa") # get data from video entry swf_url = video_entry.GetSwfUrl() youtube_url = video_entry.id.text # getting video_id is tricky, I can only reach the url which # contains the video_id. # so the only option is to parse the id element # https://groups.google.com/forum/?fromgroups=#!topic/youtube-api-gdata/RRl_h4zuKDQ url_parts = youtube_url.split("/") url_parts.reverse() video_id = url_parts[0] # save video_id to video instance video = Video() video.user = request.user video.video_id = video_id video.title = 'tmp video' video.youtube_url = youtube_url video.swf_url = swf_url video.save() # send a signal video_created.send(sender=video, video=video) # delete the uploaded video instance uploaded_video.delete() # return the response return_only_data = request.GET.get('only_data') if return_only_data: return HttpResponse(json.dumps({"video_id": video_id}), content_type="application/json") else: # Redirect to the video page or the specified page try: next_url = settings.YOUTUBE_UPLOAD_REDIRECT_URL except AttributeError: next_url = reverse( "django_youtube.views.video", kwargs={"video_id": video_id}) return HttpResponseRedirect(next_url) except: import sys logger.error("Unexpected error: %s - %s" % (sys.exc_info()[ 0], sys.exc_info()[1])) # @todo: proper error management return HttpResponse("error happened") form = YoutubeDirectUploadForm() if return_only_data: return HttpResponse(json.dumps({"error": 500}), content_type="application/json") else: return render_to_response( "django_youtube/direct-upload.html", {"form": form}, context_instance=RequestContext(request) )
def direct_upload(request): """ direct upload method starts with uploading video to our server then sends the video file to youtube param: (optional) `only_data`: if set, a json response is returns i.e. {'video_id':'124weg'} return: if `only_data` set, a json object. otherwise redirects to the video display page """ if request.method == "POST": try: form = YoutubeDirectUploadForm(request.POST, request.FILES) # upload the file to our server if form.is_valid(): uploaded_video = form.save() # send this file to youtube api = Api() api.authenticate() video_entry = api.upload_direct(uploaded_video.file_on_server.path, "Uploaded video from zuqqa") # get data from video entry swf_url = video_entry.GetSwfUrl() youtube_url = video_entry.id.text # getting video_id is tricky, I can only reach the url which # contains the video_id. # so the only option is to parse the id element # https://groups.google.com/forum/?fromgroups=#!topic/youtube-api-gdata/RRl_h4zuKDQ url_parts = youtube_url.split("/") url_parts.reverse() video_id = url_parts[0] # save video_id to video instance video = Video() video.user = request.user video.video_id = video_id video.title = 'tmp video' video.youtube_url = youtube_url video.swf_url = swf_url video.save() # send a signal video_created.send(sender=video, video=video) # delete the uploaded video instance uploaded_video.delete() # return the response return_only_data = request.GET.get('only_data') if return_only_data: return HttpResponse(json.dumps({"video_id": video_id}), content_type="application/json") else: # Redirect to the video page or the specified page try: next_url = settings.YOUTUBE_UPLOAD_REDIRECT_URL except AttributeError: next_url = reverse( "django_youtube.views.video", kwargs={"video_id": video_id}) return HttpResponseRedirect(next_url) except: import sys logger.error("Unexpected error: %s - %s" % (sys.exc_info()[ 0], sys.exc_info()[1])) # @todo: proper error management return HttpResponse("error happened") form = YoutubeDirectUploadForm() if return_only_data: return HttpResponse(json.dumps({"error": 500}), content_type="application/json") else: return render_to_response( "django_youtube/direct-upload.html", {"form": form}, context_instance=RequestContext(request) )
[ "direct", "upload", "method", "starts", "with", "uploading", "video", "to", "our", "server", "then", "sends", "the", "video", "file", "to", "youtube" ]
laplacesdemon/django-youtube
python
https://github.com/laplacesdemon/django-youtube/blob/8051ef372473eccb053f773c68e2e5e1b2cfb538/django_youtube/views.py#L122-L203
[ "def", "direct_upload", "(", "request", ")", ":", "if", "request", ".", "method", "==", "\"POST\"", ":", "try", ":", "form", "=", "YoutubeDirectUploadForm", "(", "request", ".", "POST", ",", "request", ".", "FILES", ")", "# upload the file to our server", "if", "form", ".", "is_valid", "(", ")", ":", "uploaded_video", "=", "form", ".", "save", "(", ")", "# send this file to youtube", "api", "=", "Api", "(", ")", "api", ".", "authenticate", "(", ")", "video_entry", "=", "api", ".", "upload_direct", "(", "uploaded_video", ".", "file_on_server", ".", "path", ",", "\"Uploaded video from zuqqa\"", ")", "# get data from video entry", "swf_url", "=", "video_entry", ".", "GetSwfUrl", "(", ")", "youtube_url", "=", "video_entry", ".", "id", ".", "text", "# getting video_id is tricky, I can only reach the url which", "# contains the video_id.", "# so the only option is to parse the id element", "# https://groups.google.com/forum/?fromgroups=#!topic/youtube-api-gdata/RRl_h4zuKDQ", "url_parts", "=", "youtube_url", ".", "split", "(", "\"/\"", ")", "url_parts", ".", "reverse", "(", ")", "video_id", "=", "url_parts", "[", "0", "]", "# save video_id to video instance", "video", "=", "Video", "(", ")", "video", ".", "user", "=", "request", ".", "user", "video", ".", "video_id", "=", "video_id", "video", ".", "title", "=", "'tmp video'", "video", ".", "youtube_url", "=", "youtube_url", "video", ".", "swf_url", "=", "swf_url", "video", ".", "save", "(", ")", "# send a signal", "video_created", ".", "send", "(", "sender", "=", "video", ",", "video", "=", "video", ")", "# delete the uploaded video instance", "uploaded_video", ".", "delete", "(", ")", "# return the response", "return_only_data", "=", "request", ".", "GET", ".", "get", "(", "'only_data'", ")", "if", "return_only_data", ":", "return", "HttpResponse", "(", "json", ".", "dumps", "(", "{", "\"video_id\"", ":", "video_id", "}", ")", ",", "content_type", "=", "\"application/json\"", ")", "else", ":", "# Redirect to the video page or the specified page", "try", ":", "next_url", "=", "settings", ".", "YOUTUBE_UPLOAD_REDIRECT_URL", "except", "AttributeError", ":", "next_url", "=", "reverse", "(", "\"django_youtube.views.video\"", ",", "kwargs", "=", "{", "\"video_id\"", ":", "video_id", "}", ")", "return", "HttpResponseRedirect", "(", "next_url", ")", "except", ":", "import", "sys", "logger", ".", "error", "(", "\"Unexpected error: %s - %s\"", "%", "(", "sys", ".", "exc_info", "(", ")", "[", "0", "]", ",", "sys", ".", "exc_info", "(", ")", "[", "1", "]", ")", ")", "# @todo: proper error management", "return", "HttpResponse", "(", "\"error happened\"", ")", "form", "=", "YoutubeDirectUploadForm", "(", ")", "if", "return_only_data", ":", "return", "HttpResponse", "(", "json", ".", "dumps", "(", "{", "\"error\"", ":", "500", "}", ")", ",", "content_type", "=", "\"application/json\"", ")", "else", ":", "return", "render_to_response", "(", "\"django_youtube/direct-upload.html\"", ",", "{", "\"form\"", ":", "form", "}", ",", "context_instance", "=", "RequestContext", "(", "request", ")", ")" ]
8051ef372473eccb053f773c68e2e5e1b2cfb538
test
upload
Displays an upload form Creates upload url and token from youtube api and uses them on the form
django_youtube/views.py
def upload(request): """ Displays an upload form Creates upload url and token from youtube api and uses them on the form """ # Get the optional parameters title = request.GET.get("title", "%s's video on %s" % ( request.user.username, request.get_host())) description = request.GET.get("description", "") keywords = request.GET.get("keywords", "") # Try to create post_url and token to create an upload form try: api = Api() # upload method needs authentication api.authenticate() # Customize following line to your needs, you can add description, keywords or developer_keys # I prefer to update video information after upload finishes data = api.upload(title, description=description, keywords=keywords, access_control=AccessControl.Unlisted) except ApiError as e: # An api error happened, redirect to homepage messages.add_message(request, messages.ERROR, e.message) return HttpResponseRedirect("/") except: # An error happened, redirect to homepage messages.add_message(request, messages.ERROR, _( 'An error occurred during the upload, Please try again.')) return HttpResponseRedirect("/") # Create the form instance form = YoutubeUploadForm(initial={"token": data["youtube_token"]}) protocol = 'https' if request.is_secure() else 'http' next_url = '%s://%s%s/' % (protocol, request.get_host(), reverse("django_youtube.views.upload_return")) return render_to_response( "django_youtube/upload.html", {"form": form, "post_url": data["post_url"], "next_url": next_url}, context_instance=RequestContext(request) )
def upload(request): """ Displays an upload form Creates upload url and token from youtube api and uses them on the form """ # Get the optional parameters title = request.GET.get("title", "%s's video on %s" % ( request.user.username, request.get_host())) description = request.GET.get("description", "") keywords = request.GET.get("keywords", "") # Try to create post_url and token to create an upload form try: api = Api() # upload method needs authentication api.authenticate() # Customize following line to your needs, you can add description, keywords or developer_keys # I prefer to update video information after upload finishes data = api.upload(title, description=description, keywords=keywords, access_control=AccessControl.Unlisted) except ApiError as e: # An api error happened, redirect to homepage messages.add_message(request, messages.ERROR, e.message) return HttpResponseRedirect("/") except: # An error happened, redirect to homepage messages.add_message(request, messages.ERROR, _( 'An error occurred during the upload, Please try again.')) return HttpResponseRedirect("/") # Create the form instance form = YoutubeUploadForm(initial={"token": data["youtube_token"]}) protocol = 'https' if request.is_secure() else 'http' next_url = '%s://%s%s/' % (protocol, request.get_host(), reverse("django_youtube.views.upload_return")) return render_to_response( "django_youtube/upload.html", {"form": form, "post_url": data["post_url"], "next_url": next_url}, context_instance=RequestContext(request) )
[ "Displays", "an", "upload", "form", "Creates", "upload", "url", "and", "token", "from", "youtube", "api", "and", "uses", "them", "on", "the", "form" ]
laplacesdemon/django-youtube
python
https://github.com/laplacesdemon/django-youtube/blob/8051ef372473eccb053f773c68e2e5e1b2cfb538/django_youtube/views.py#L207-L248
[ "def", "upload", "(", "request", ")", ":", "# Get the optional parameters", "title", "=", "request", ".", "GET", ".", "get", "(", "\"title\"", ",", "\"%s's video on %s\"", "%", "(", "request", ".", "user", ".", "username", ",", "request", ".", "get_host", "(", ")", ")", ")", "description", "=", "request", ".", "GET", ".", "get", "(", "\"description\"", ",", "\"\"", ")", "keywords", "=", "request", ".", "GET", ".", "get", "(", "\"keywords\"", ",", "\"\"", ")", "# Try to create post_url and token to create an upload form", "try", ":", "api", "=", "Api", "(", ")", "# upload method needs authentication", "api", ".", "authenticate", "(", ")", "# Customize following line to your needs, you can add description, keywords or developer_keys", "# I prefer to update video information after upload finishes", "data", "=", "api", ".", "upload", "(", "title", ",", "description", "=", "description", ",", "keywords", "=", "keywords", ",", "access_control", "=", "AccessControl", ".", "Unlisted", ")", "except", "ApiError", "as", "e", ":", "# An api error happened, redirect to homepage", "messages", ".", "add_message", "(", "request", ",", "messages", ".", "ERROR", ",", "e", ".", "message", ")", "return", "HttpResponseRedirect", "(", "\"/\"", ")", "except", ":", "# An error happened, redirect to homepage", "messages", ".", "add_message", "(", "request", ",", "messages", ".", "ERROR", ",", "_", "(", "'An error occurred during the upload, Please try again.'", ")", ")", "return", "HttpResponseRedirect", "(", "\"/\"", ")", "# Create the form instance", "form", "=", "YoutubeUploadForm", "(", "initial", "=", "{", "\"token\"", ":", "data", "[", "\"youtube_token\"", "]", "}", ")", "protocol", "=", "'https'", "if", "request", ".", "is_secure", "(", ")", "else", "'http'", "next_url", "=", "'%s://%s%s/'", "%", "(", "protocol", ",", "request", ".", "get_host", "(", ")", ",", "reverse", "(", "\"django_youtube.views.upload_return\"", ")", ")", "return", "render_to_response", "(", "\"django_youtube/upload.html\"", ",", "{", "\"form\"", ":", "form", ",", "\"post_url\"", ":", "data", "[", "\"post_url\"", "]", ",", "\"next_url\"", ":", "next_url", "}", ",", "context_instance", "=", "RequestContext", "(", "request", ")", ")" ]
8051ef372473eccb053f773c68e2e5e1b2cfb538
test
upload_return
The upload result page Youtube will redirect to this page after upload is finished Saves the video data and redirects to the next page Params: status: status of the upload (200 for success) id: id number of the video
django_youtube/views.py
def upload_return(request): """ The upload result page Youtube will redirect to this page after upload is finished Saves the video data and redirects to the next page Params: status: status of the upload (200 for success) id: id number of the video """ status = request.GET.get("status") video_id = request.GET.get("id") if status == "200" and video_id: # upload is successful # save the video entry video = Video() video.user = request.user video.video_id = video_id video.save() # send a signal video_created.send(sender=video, video=video) # Redirect to the video page or the specified page try: next_url = settings.YOUTUBE_UPLOAD_REDIRECT_URL except AttributeError: next_url = reverse( "django_youtube.views.video", kwargs={"video_id": video_id}) return HttpResponseRedirect(next_url) else: # upload failed, redirect to upload page from django.contrib import messages messages.add_message( request, messages.ERROR, _('Upload failed, Please try again.')) return HttpResponseRedirect(reverse("django_youtube.views.upload"))
def upload_return(request): """ The upload result page Youtube will redirect to this page after upload is finished Saves the video data and redirects to the next page Params: status: status of the upload (200 for success) id: id number of the video """ status = request.GET.get("status") video_id = request.GET.get("id") if status == "200" and video_id: # upload is successful # save the video entry video = Video() video.user = request.user video.video_id = video_id video.save() # send a signal video_created.send(sender=video, video=video) # Redirect to the video page or the specified page try: next_url = settings.YOUTUBE_UPLOAD_REDIRECT_URL except AttributeError: next_url = reverse( "django_youtube.views.video", kwargs={"video_id": video_id}) return HttpResponseRedirect(next_url) else: # upload failed, redirect to upload page from django.contrib import messages messages.add_message( request, messages.ERROR, _('Upload failed, Please try again.')) return HttpResponseRedirect(reverse("django_youtube.views.upload"))
[ "The", "upload", "result", "page", "Youtube", "will", "redirect", "to", "this", "page", "after", "upload", "is", "finished", "Saves", "the", "video", "data", "and", "redirects", "to", "the", "next", "page" ]
laplacesdemon/django-youtube
python
https://github.com/laplacesdemon/django-youtube/blob/8051ef372473eccb053f773c68e2e5e1b2cfb538/django_youtube/views.py#L252-L290
[ "def", "upload_return", "(", "request", ")", ":", "status", "=", "request", ".", "GET", ".", "get", "(", "\"status\"", ")", "video_id", "=", "request", ".", "GET", ".", "get", "(", "\"id\"", ")", "if", "status", "==", "\"200\"", "and", "video_id", ":", "# upload is successful", "# save the video entry", "video", "=", "Video", "(", ")", "video", ".", "user", "=", "request", ".", "user", "video", ".", "video_id", "=", "video_id", "video", ".", "save", "(", ")", "# send a signal", "video_created", ".", "send", "(", "sender", "=", "video", ",", "video", "=", "video", ")", "# Redirect to the video page or the specified page", "try", ":", "next_url", "=", "settings", ".", "YOUTUBE_UPLOAD_REDIRECT_URL", "except", "AttributeError", ":", "next_url", "=", "reverse", "(", "\"django_youtube.views.video\"", ",", "kwargs", "=", "{", "\"video_id\"", ":", "video_id", "}", ")", "return", "HttpResponseRedirect", "(", "next_url", ")", "else", ":", "# upload failed, redirect to upload page", "from", "django", ".", "contrib", "import", "messages", "messages", ".", "add_message", "(", "request", ",", "messages", ".", "ERROR", ",", "_", "(", "'Upload failed, Please try again.'", ")", ")", "return", "HttpResponseRedirect", "(", "reverse", "(", "\"django_youtube.views.upload\"", ")", ")" ]
8051ef372473eccb053f773c68e2e5e1b2cfb538
test
remove
Removes the video from youtube and from db Requires POST
django_youtube/views.py
def remove(request, video_id): """ Removes the video from youtube and from db Requires POST """ # prepare redirection url try: next_url = settings.YOUTUBE_DELETE_REDIRECT_URL except AttributeError: next_url = reverse("django_youtube.views.upload") # Remove from db try: Video.objects.get(video_id=video_id).delete() except: from django.contrib import messages messages.add_message( request, messages.ERROR, _('Video could not be deleted.')) # Return to upload page or specified page return HttpResponseRedirect(next_url)
def remove(request, video_id): """ Removes the video from youtube and from db Requires POST """ # prepare redirection url try: next_url = settings.YOUTUBE_DELETE_REDIRECT_URL except AttributeError: next_url = reverse("django_youtube.views.upload") # Remove from db try: Video.objects.get(video_id=video_id).delete() except: from django.contrib import messages messages.add_message( request, messages.ERROR, _('Video could not be deleted.')) # Return to upload page or specified page return HttpResponseRedirect(next_url)
[ "Removes", "the", "video", "from", "youtube", "and", "from", "db", "Requires", "POST" ]
laplacesdemon/django-youtube
python
https://github.com/laplacesdemon/django-youtube/blob/8051ef372473eccb053f773c68e2e5e1b2cfb538/django_youtube/views.py#L295-L316
[ "def", "remove", "(", "request", ",", "video_id", ")", ":", "# prepare redirection url", "try", ":", "next_url", "=", "settings", ".", "YOUTUBE_DELETE_REDIRECT_URL", "except", "AttributeError", ":", "next_url", "=", "reverse", "(", "\"django_youtube.views.upload\"", ")", "# Remove from db", "try", ":", "Video", ".", "objects", ".", "get", "(", "video_id", "=", "video_id", ")", ".", "delete", "(", ")", "except", ":", "from", "django", ".", "contrib", "import", "messages", "messages", ".", "add_message", "(", "request", ",", "messages", ".", "ERROR", ",", "_", "(", "'Video could not be deleted.'", ")", ")", "# Return to upload page or specified page", "return", "HttpResponseRedirect", "(", "next_url", ")" ]
8051ef372473eccb053f773c68e2e5e1b2cfb538
test
Video.entry
Connects to Youtube Api and retrieves the video entry object Return: gdata.youtube.YouTubeVideoEntry
django_youtube/models.py
def entry(self): """ Connects to Youtube Api and retrieves the video entry object Return: gdata.youtube.YouTubeVideoEntry """ api = Api() api.authenticate() return api.fetch_video(self.video_id)
def entry(self): """ Connects to Youtube Api and retrieves the video entry object Return: gdata.youtube.YouTubeVideoEntry """ api = Api() api.authenticate() return api.fetch_video(self.video_id)
[ "Connects", "to", "Youtube", "Api", "and", "retrieves", "the", "video", "entry", "object" ]
laplacesdemon/django-youtube
python
https://github.com/laplacesdemon/django-youtube/blob/8051ef372473eccb053f773c68e2e5e1b2cfb538/django_youtube/models.py#L38-L47
[ "def", "entry", "(", "self", ")", ":", "api", "=", "Api", "(", ")", "api", ".", "authenticate", "(", ")", "return", "api", ".", "fetch_video", "(", "self", ".", "video_id", ")" ]
8051ef372473eccb053f773c68e2e5e1b2cfb538
test
Video.save
Syncronize the video information on db with the video on Youtube The reason that I didn't use signals is to avoid saving the video instance twice.
django_youtube/models.py
def save(self, *args, **kwargs): """ Syncronize the video information on db with the video on Youtube The reason that I didn't use signals is to avoid saving the video instance twice. """ # if this is a new instance add details from api if not self.id: # Connect to api and get the details entry = self.entry() # Set the details self.title = entry.media.title.text self.description = entry.media.description.text self.keywords = entry.media.keywords.text self.youtube_url = entry.media.player.url self.swf_url = entry.GetSwfUrl() if entry.media.private: self.access_control = AccessControl.Private else: self.access_control = AccessControl.Public # Save the instance super(Video, self).save(*args, **kwargs) # show thumbnails for thumbnail in entry.media.thumbnail: t = Thumbnail() t.url = thumbnail.url t.video = self t.save() else: # updating the video instance # Connect to API and update video on youtube api = Api() # update method needs authentication api.authenticate() # Update the info on youtube, raise error on failure api.update_video(self.video_id, self.title, self.description, self.keywords, self.access_control) # Save the model return super(Video, self).save(*args, **kwargs)
def save(self, *args, **kwargs): """ Syncronize the video information on db with the video on Youtube The reason that I didn't use signals is to avoid saving the video instance twice. """ # if this is a new instance add details from api if not self.id: # Connect to api and get the details entry = self.entry() # Set the details self.title = entry.media.title.text self.description = entry.media.description.text self.keywords = entry.media.keywords.text self.youtube_url = entry.media.player.url self.swf_url = entry.GetSwfUrl() if entry.media.private: self.access_control = AccessControl.Private else: self.access_control = AccessControl.Public # Save the instance super(Video, self).save(*args, **kwargs) # show thumbnails for thumbnail in entry.media.thumbnail: t = Thumbnail() t.url = thumbnail.url t.video = self t.save() else: # updating the video instance # Connect to API and update video on youtube api = Api() # update method needs authentication api.authenticate() # Update the info on youtube, raise error on failure api.update_video(self.video_id, self.title, self.description, self.keywords, self.access_control) # Save the model return super(Video, self).save(*args, **kwargs)
[ "Syncronize", "the", "video", "information", "on", "db", "with", "the", "video", "on", "Youtube", "The", "reason", "that", "I", "didn", "t", "use", "signals", "is", "to", "avoid", "saving", "the", "video", "instance", "twice", "." ]
laplacesdemon/django-youtube
python
https://github.com/laplacesdemon/django-youtube/blob/8051ef372473eccb053f773c68e2e5e1b2cfb538/django_youtube/models.py#L49-L93
[ "def", "save", "(", "self", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "# if this is a new instance add details from api", "if", "not", "self", ".", "id", ":", "# Connect to api and get the details", "entry", "=", "self", ".", "entry", "(", ")", "# Set the details", "self", ".", "title", "=", "entry", ".", "media", ".", "title", ".", "text", "self", ".", "description", "=", "entry", ".", "media", ".", "description", ".", "text", "self", ".", "keywords", "=", "entry", ".", "media", ".", "keywords", ".", "text", "self", ".", "youtube_url", "=", "entry", ".", "media", ".", "player", ".", "url", "self", ".", "swf_url", "=", "entry", ".", "GetSwfUrl", "(", ")", "if", "entry", ".", "media", ".", "private", ":", "self", ".", "access_control", "=", "AccessControl", ".", "Private", "else", ":", "self", ".", "access_control", "=", "AccessControl", ".", "Public", "# Save the instance", "super", "(", "Video", ",", "self", ")", ".", "save", "(", "*", "args", ",", "*", "*", "kwargs", ")", "# show thumbnails", "for", "thumbnail", "in", "entry", ".", "media", ".", "thumbnail", ":", "t", "=", "Thumbnail", "(", ")", "t", ".", "url", "=", "thumbnail", ".", "url", "t", ".", "video", "=", "self", "t", ".", "save", "(", ")", "else", ":", "# updating the video instance", "# Connect to API and update video on youtube", "api", "=", "Api", "(", ")", "# update method needs authentication", "api", ".", "authenticate", "(", ")", "# Update the info on youtube, raise error on failure", "api", ".", "update_video", "(", "self", ".", "video_id", ",", "self", ".", "title", ",", "self", ".", "description", ",", "self", ".", "keywords", ",", "self", ".", "access_control", ")", "# Save the model", "return", "super", "(", "Video", ",", "self", ")", ".", "save", "(", "*", "args", ",", "*", "*", "kwargs", ")" ]
8051ef372473eccb053f773c68e2e5e1b2cfb538
test
Video.delete
Deletes the video from youtube Raises: OperationError
django_youtube/models.py
def delete(self, *args, **kwargs): """ Deletes the video from youtube Raises: OperationError """ api = Api() # Authentication is required for deletion api.authenticate() # Send API request, raises OperationError on unsuccessful deletion api.delete_video(self.video_id) # Call the super method return super(Video, self).delete(*args, **kwargs)
def delete(self, *args, **kwargs): """ Deletes the video from youtube Raises: OperationError """ api = Api() # Authentication is required for deletion api.authenticate() # Send API request, raises OperationError on unsuccessful deletion api.delete_video(self.video_id) # Call the super method return super(Video, self).delete(*args, **kwargs)
[ "Deletes", "the", "video", "from", "youtube" ]
laplacesdemon/django-youtube
python
https://github.com/laplacesdemon/django-youtube/blob/8051ef372473eccb053f773c68e2e5e1b2cfb538/django_youtube/models.py#L95-L111
[ "def", "delete", "(", "self", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "api", "=", "Api", "(", ")", "# Authentication is required for deletion", "api", ".", "authenticate", "(", ")", "# Send API request, raises OperationError on unsuccessful deletion", "api", ".", "delete_video", "(", "self", ".", "video_id", ")", "# Call the super method", "return", "super", "(", "Video", ",", "self", ")", ".", "delete", "(", "*", "args", ",", "*", "*", "kwargs", ")" ]
8051ef372473eccb053f773c68e2e5e1b2cfb538
test
Key.regenerate
Method for `Regenerate Key <https://m2x.att.com/developer/documentation/v2/keys#Regenerate-Key>`_ endpoint. :raises: :class:`~requests.exceptions.HTTPError` if an error occurs when sending the HTTP request
m2x/v2/keys.py
def regenerate(self): """ Method for `Regenerate Key <https://m2x.att.com/developer/documentation/v2/keys#Regenerate-Key>`_ endpoint. :raises: :class:`~requests.exceptions.HTTPError` if an error occurs when sending the HTTP request """ self.data.update( self.api.post(self.item_path(self.key) + '/regenerate') )
def regenerate(self): """ Method for `Regenerate Key <https://m2x.att.com/developer/documentation/v2/keys#Regenerate-Key>`_ endpoint. :raises: :class:`~requests.exceptions.HTTPError` if an error occurs when sending the HTTP request """ self.data.update( self.api.post(self.item_path(self.key) + '/regenerate') )
[ "Method", "for", "Regenerate", "Key", "<https", ":", "//", "m2x", ".", "att", ".", "com", "/", "developer", "/", "documentation", "/", "v2", "/", "keys#Regenerate", "-", "Key", ">", "_", "endpoint", "." ]
attm2x/m2x-python
python
https://github.com/attm2x/m2x-python/blob/df83f590114692b1f96577148b7ba260065905bb/m2x/v2/keys.py#L11-L18
[ "def", "regenerate", "(", "self", ")", ":", "self", ".", "data", ".", "update", "(", "self", ".", "api", ".", "post", "(", "self", ".", "item_path", "(", "self", ".", "key", ")", "+", "'/regenerate'", ")", ")" ]
df83f590114692b1f96577148b7ba260065905bb
test
Distribution.devices
Method for `List Devices from an existing Distribution <https://m2x.att.com/developer/documentation/v2/distribution#List-Devices-from-an-existing-Distribution>`_ endpoint. :param params: Query parameters passed as keyword arguments. View M2X API Docs for listing of available parameters. :return: List of Devices associated with this Distribution as :class:`.DistributionDevice` objects :rtype: `list <https://docs.python.org/2/library/functions.html#list>`_ :raises: :class:`~requests.exceptions.HTTPError` if an error occurs when sending the HTTP request
m2x/v2/distributions.py
def devices(self, **params): """ Method for `List Devices from an existing Distribution <https://m2x.att.com/developer/documentation/v2/distribution#List-Devices-from-an-existing-Distribution>`_ endpoint. :param params: Query parameters passed as keyword arguments. View M2X API Docs for listing of available parameters. :return: List of Devices associated with this Distribution as :class:`.DistributionDevice` objects :rtype: `list <https://docs.python.org/2/library/functions.html#list>`_ :raises: :class:`~requests.exceptions.HTTPError` if an error occurs when sending the HTTP request """ return DistributionDevice.list(self.api, distribution_id=self.id, **params)
def devices(self, **params): """ Method for `List Devices from an existing Distribution <https://m2x.att.com/developer/documentation/v2/distribution#List-Devices-from-an-existing-Distribution>`_ endpoint. :param params: Query parameters passed as keyword arguments. View M2X API Docs for listing of available parameters. :return: List of Devices associated with this Distribution as :class:`.DistributionDevice` objects :rtype: `list <https://docs.python.org/2/library/functions.html#list>`_ :raises: :class:`~requests.exceptions.HTTPError` if an error occurs when sending the HTTP request """ return DistributionDevice.list(self.api, distribution_id=self.id, **params)
[ "Method", "for", "List", "Devices", "from", "an", "existing", "Distribution", "<https", ":", "//", "m2x", ".", "att", ".", "com", "/", "developer", "/", "documentation", "/", "v2", "/", "distribution#List", "-", "Devices", "-", "from", "-", "an", "-", "existing", "-", "Distribution", ">", "_", "endpoint", "." ]
attm2x/m2x-python
python
https://github.com/attm2x/m2x-python/blob/df83f590114692b1f96577148b7ba260065905bb/m2x/v2/distributions.py#L23-L33
[ "def", "devices", "(", "self", ",", "*", "*", "params", ")", ":", "return", "DistributionDevice", ".", "list", "(", "self", ".", "api", ",", "distribution_id", "=", "self", ".", "id", ",", "*", "*", "params", ")" ]
df83f590114692b1f96577148b7ba260065905bb
test
Distribution.add_device
Method for `Add Device to an Existing Distribution <https://m2x.att.com/developer/documentation/v2/distribution#Add-Device-to-an-existing-Distribution>`_ endpoint. :param params: Query parameters passed as keyword arguments. View M2X API Docs for listing of available parameters. :return: The newly created DistributionDevice :rtype: DistributionDevice :raises: :class:`~requests.exceptions.HTTPError` if an error occurs when sending the HTTP request
m2x/v2/distributions.py
def add_device(self, params): """ Method for `Add Device to an Existing Distribution <https://m2x.att.com/developer/documentation/v2/distribution#Add-Device-to-an-existing-Distribution>`_ endpoint. :param params: Query parameters passed as keyword arguments. View M2X API Docs for listing of available parameters. :return: The newly created DistributionDevice :rtype: DistributionDevice :raises: :class:`~requests.exceptions.HTTPError` if an error occurs when sending the HTTP request """ return DistributionDevice.create(self.api, distribution_id=self.id, **params)
def add_device(self, params): """ Method for `Add Device to an Existing Distribution <https://m2x.att.com/developer/documentation/v2/distribution#Add-Device-to-an-existing-Distribution>`_ endpoint. :param params: Query parameters passed as keyword arguments. View M2X API Docs for listing of available parameters. :return: The newly created DistributionDevice :rtype: DistributionDevice :raises: :class:`~requests.exceptions.HTTPError` if an error occurs when sending the HTTP request """ return DistributionDevice.create(self.api, distribution_id=self.id, **params)
[ "Method", "for", "Add", "Device", "to", "an", "Existing", "Distribution", "<https", ":", "//", "m2x", ".", "att", ".", "com", "/", "developer", "/", "documentation", "/", "v2", "/", "distribution#Add", "-", "Device", "-", "to", "-", "an", "-", "existing", "-", "Distribution", ">", "_", "endpoint", "." ]
attm2x/m2x-python
python
https://github.com/attm2x/m2x-python/blob/df83f590114692b1f96577148b7ba260065905bb/m2x/v2/distributions.py#L35-L45
[ "def", "add_device", "(", "self", ",", "params", ")", ":", "return", "DistributionDevice", ".", "create", "(", "self", ".", "api", ",", "distribution_id", "=", "self", ".", "id", ",", "*", "*", "params", ")" ]
df83f590114692b1f96577148b7ba260065905bb
test
Collection.devices
Method for `List Devices from an existing Collection <https://m2x.att.com/developer/documentation/v2/collections#List-Devices-from-an-existing-Collection>`_ endpoint. :param params: Query parameters passed as keyword arguments. View M2X API Docs for listing of available parameters. :return: List of :class:`.Device` objects :rtype: `list <https://docs.python.org/2/library/functions.html#list>`_ :raises: :class:`~requests.exceptions.HTTPError` if an error occurs when sending the HTTP request
m2x/v2/collections.py
def devices(self, **params): """ Method for `List Devices from an existing Collection <https://m2x.att.com/developer/documentation/v2/collections#List-Devices-from-an-existing-Collection>`_ endpoint. :param params: Query parameters passed as keyword arguments. View M2X API Docs for listing of available parameters. :return: List of :class:`.Device` objects :rtype: `list <https://docs.python.org/2/library/functions.html#list>`_ :raises: :class:`~requests.exceptions.HTTPError` if an error occurs when sending the HTTP request """ return CollectionDevice.list(self.api, collection_id=self.id, **params)
def devices(self, **params): """ Method for `List Devices from an existing Collection <https://m2x.att.com/developer/documentation/v2/collections#List-Devices-from-an-existing-Collection>`_ endpoint. :param params: Query parameters passed as keyword arguments. View M2X API Docs for listing of available parameters. :return: List of :class:`.Device` objects :rtype: `list <https://docs.python.org/2/library/functions.html#list>`_ :raises: :class:`~requests.exceptions.HTTPError` if an error occurs when sending the HTTP request """ return CollectionDevice.list(self.api, collection_id=self.id, **params)
[ "Method", "for", "List", "Devices", "from", "an", "existing", "Collection", "<https", ":", "//", "m2x", ".", "att", ".", "com", "/", "developer", "/", "documentation", "/", "v2", "/", "collections#List", "-", "Devices", "-", "from", "-", "an", "-", "existing", "-", "Collection", ">", "_", "endpoint", "." ]
attm2x/m2x-python
python
https://github.com/attm2x/m2x-python/blob/df83f590114692b1f96577148b7ba260065905bb/m2x/v2/collections.py#L16-L26
[ "def", "devices", "(", "self", ",", "*", "*", "params", ")", ":", "return", "CollectionDevice", ".", "list", "(", "self", ".", "api", ",", "collection_id", "=", "self", ".", "id", ",", "*", "*", "params", ")" ]
df83f590114692b1f96577148b7ba260065905bb