id
int32
0
252k
repo
stringlengths
7
55
path
stringlengths
4
127
func_name
stringlengths
1
88
original_string
stringlengths
75
19.8k
language
stringclasses
1 value
code
stringlengths
51
19.8k
code_tokens
list
docstring
stringlengths
3
17.3k
docstring_tokens
list
sha
stringlengths
40
40
url
stringlengths
87
242
14,900
materialsproject/pymatgen-db
matgendb/builders/examples/copy_builder.py
CopyBuilder.get_items
def get_items(self, source=None, target=None, crit=None): """Copy records from source to target collection. :param source: Input collection :type source: QueryEngine :param target: Output collection :type target: QueryEngine :param crit: Filter criteria, e.g. "{ 'flag': True }". :type crit: dict """ self._target_coll = target.collection if not crit: # reduce any False-y crit value to None crit = None cur = source.query(criteria=crit) _log.info("source.collection={} crit={} source_records={:d}" .format(source.collection, crit, len(cur))) return cur
python
def get_items(self, source=None, target=None, crit=None): self._target_coll = target.collection if not crit: # reduce any False-y crit value to None crit = None cur = source.query(criteria=crit) _log.info("source.collection={} crit={} source_records={:d}" .format(source.collection, crit, len(cur))) return cur
[ "def", "get_items", "(", "self", ",", "source", "=", "None", ",", "target", "=", "None", ",", "crit", "=", "None", ")", ":", "self", ".", "_target_coll", "=", "target", ".", "collection", "if", "not", "crit", ":", "# reduce any False-y crit value to None", ...
Copy records from source to target collection. :param source: Input collection :type source: QueryEngine :param target: Output collection :type target: QueryEngine :param crit: Filter criteria, e.g. "{ 'flag': True }". :type crit: dict
[ "Copy", "records", "from", "source", "to", "target", "collection", "." ]
02e4351c2cea431407644f49193e8bf43ed39b9a
https://github.com/materialsproject/pymatgen-db/blob/02e4351c2cea431407644f49193e8bf43ed39b9a/matgendb/builders/examples/copy_builder.py#L28-L44
14,901
materialsproject/pymatgen-db
matgendb/builders/core.py
parse_fn_docstring
def parse_fn_docstring(fn): """Get parameter and return types from function's docstring. Docstrings must use this format:: :param foo: What is foo :type foo: int :return: What is returned :rtype: double :return: A map of names, each with keys 'type' and 'desc'. :rtype: tuple(dict) """ doc = fn.__doc__ params, return_ = {}, {} param_order = [] for line in doc.split("\n"): line = line.strip() if line.startswith(":param"): _, name, desc = line.split(":", 2) name = name[6:].strip() # skip 'param ' params[name] = {'desc': desc.strip()} param_order.append(name) elif line.startswith(":type"): _, name, desc = line.split(":", 2) name = name[5:].strip() # skip 'type ' if not name in params: raise ValueError("'type' without 'param' for {}".format(name)) params[name]['type'] = desc.strip() elif line.startswith(":return"): _1, _2, desc = line.split(":", 2) return_['desc'] = desc elif line.startswith(":rtype"): _1, _2, desc = line.split(":", 2) return_['type'] = desc.strip() return params
python
def parse_fn_docstring(fn): doc = fn.__doc__ params, return_ = {}, {} param_order = [] for line in doc.split("\n"): line = line.strip() if line.startswith(":param"): _, name, desc = line.split(":", 2) name = name[6:].strip() # skip 'param ' params[name] = {'desc': desc.strip()} param_order.append(name) elif line.startswith(":type"): _, name, desc = line.split(":", 2) name = name[5:].strip() # skip 'type ' if not name in params: raise ValueError("'type' without 'param' for {}".format(name)) params[name]['type'] = desc.strip() elif line.startswith(":return"): _1, _2, desc = line.split(":", 2) return_['desc'] = desc elif line.startswith(":rtype"): _1, _2, desc = line.split(":", 2) return_['type'] = desc.strip() return params
[ "def", "parse_fn_docstring", "(", "fn", ")", ":", "doc", "=", "fn", ".", "__doc__", "params", ",", "return_", "=", "{", "}", ",", "{", "}", "param_order", "=", "[", "]", "for", "line", "in", "doc", ".", "split", "(", "\"\\n\"", ")", ":", "line", ...
Get parameter and return types from function's docstring. Docstrings must use this format:: :param foo: What is foo :type foo: int :return: What is returned :rtype: double :return: A map of names, each with keys 'type' and 'desc'. :rtype: tuple(dict)
[ "Get", "parameter", "and", "return", "types", "from", "function", "s", "docstring", "." ]
02e4351c2cea431407644f49193e8bf43ed39b9a
https://github.com/materialsproject/pymatgen-db/blob/02e4351c2cea431407644f49193e8bf43ed39b9a/matgendb/builders/core.py#L45-L80
14,902
materialsproject/pymatgen-db
matgendb/builders/core.py
merge_tasks
def merge_tasks(core_collections, sandbox_collections, id_prefix, new_tasks, batch_size=100, wipe=False): """Merge core and sandbox collections into a temporary collection in the sandbox. :param core_collections: Core collection info :type core_collections: Collections :param sandbox_collections: Sandbox collection info :type sandbox_collections: Collections """ merged = copy.copy(sandbox_collections) # create/clear target collection target = merged.database[new_tasks] if wipe: _log.debug("merge_tasks.wipe.begin") target.remove() merged.database['counter'].remove() _log.debug("merge_tasks.wipe.end") # perform the merge batch = [] for doc in core_collections.tasks.find(): batch.append(doc) if len(batch) == batch_size: target.insert(batch) batch = [] if batch: target.insert(batch) batch = [] for doc in sandbox_collections.tasks.find(): doc['task_id'] = id_prefix + '-' + str(doc['task_id']) batch.append(doc) if len(batch) == batch_size: target.insert(batch) batch = [] if batch: target.insert(batch)
python
def merge_tasks(core_collections, sandbox_collections, id_prefix, new_tasks, batch_size=100, wipe=False): merged = copy.copy(sandbox_collections) # create/clear target collection target = merged.database[new_tasks] if wipe: _log.debug("merge_tasks.wipe.begin") target.remove() merged.database['counter'].remove() _log.debug("merge_tasks.wipe.end") # perform the merge batch = [] for doc in core_collections.tasks.find(): batch.append(doc) if len(batch) == batch_size: target.insert(batch) batch = [] if batch: target.insert(batch) batch = [] for doc in sandbox_collections.tasks.find(): doc['task_id'] = id_prefix + '-' + str(doc['task_id']) batch.append(doc) if len(batch) == batch_size: target.insert(batch) batch = [] if batch: target.insert(batch)
[ "def", "merge_tasks", "(", "core_collections", ",", "sandbox_collections", ",", "id_prefix", ",", "new_tasks", ",", "batch_size", "=", "100", ",", "wipe", "=", "False", ")", ":", "merged", "=", "copy", ".", "copy", "(", "sandbox_collections", ")", "# create/cl...
Merge core and sandbox collections into a temporary collection in the sandbox. :param core_collections: Core collection info :type core_collections: Collections :param sandbox_collections: Sandbox collection info :type sandbox_collections: Collections
[ "Merge", "core", "and", "sandbox", "collections", "into", "a", "temporary", "collection", "in", "the", "sandbox", "." ]
02e4351c2cea431407644f49193e8bf43ed39b9a
https://github.com/materialsproject/pymatgen-db/blob/02e4351c2cea431407644f49193e8bf43ed39b9a/matgendb/builders/core.py#L153-L186
14,903
materialsproject/pymatgen-db
matgendb/builders/core.py
alphadump
def alphadump(d, indent=2, depth=0): """Dump a dict to a str, with keys in alphabetical order. """ sep = '\n' + ' ' * depth * indent return ''.join( ("{}: {}{}".format( k, alphadump(d[k], depth=depth+1) if isinstance(d[k], dict) else str(d[k]), sep) for k in sorted(d.keys())) )
python
def alphadump(d, indent=2, depth=0): sep = '\n' + ' ' * depth * indent return ''.join( ("{}: {}{}".format( k, alphadump(d[k], depth=depth+1) if isinstance(d[k], dict) else str(d[k]), sep) for k in sorted(d.keys())) )
[ "def", "alphadump", "(", "d", ",", "indent", "=", "2", ",", "depth", "=", "0", ")", ":", "sep", "=", "'\\n'", "+", "' '", "*", "depth", "*", "indent", "return", "''", ".", "join", "(", "(", "\"{}: {}{}\"", ".", "format", "(", "k", ",", "alphadump...
Dump a dict to a str, with keys in alphabetical order.
[ "Dump", "a", "dict", "to", "a", "str", "with", "keys", "in", "alphabetical", "order", "." ]
02e4351c2cea431407644f49193e8bf43ed39b9a
https://github.com/materialsproject/pymatgen-db/blob/02e4351c2cea431407644f49193e8bf43ed39b9a/matgendb/builders/core.py#L521-L533
14,904
materialsproject/pymatgen-db
matgendb/builders/core.py
HasExamples.validate_examples
def validate_examples(self, fail_fn): """Check the examples against the schema. :param fail_fn: Pass failure messages to this function :type fail_fn: function(str) """ for collection, doc in self.examples(): _log.debug("validating example in collection {}".format(collection)) sch = schema.get_schema(collection) # with more err. checking result = sch.validate(doc) _log.debug("validation result: {}".format("OK" if result is None else result)) if result is not None: fail_fn("Failed to validate sample document: {}".format(result))
python
def validate_examples(self, fail_fn): for collection, doc in self.examples(): _log.debug("validating example in collection {}".format(collection)) sch = schema.get_schema(collection) # with more err. checking result = sch.validate(doc) _log.debug("validation result: {}".format("OK" if result is None else result)) if result is not None: fail_fn("Failed to validate sample document: {}".format(result))
[ "def", "validate_examples", "(", "self", ",", "fail_fn", ")", ":", "for", "collection", ",", "doc", "in", "self", ".", "examples", "(", ")", ":", "_log", ".", "debug", "(", "\"validating example in collection {}\"", ".", "format", "(", "collection", ")", ")"...
Check the examples against the schema. :param fail_fn: Pass failure messages to this function :type fail_fn: function(str)
[ "Check", "the", "examples", "against", "the", "schema", "." ]
02e4351c2cea431407644f49193e8bf43ed39b9a
https://github.com/materialsproject/pymatgen-db/blob/02e4351c2cea431407644f49193e8bf43ed39b9a/matgendb/builders/core.py#L212-L224
14,905
materialsproject/pymatgen-db
matgendb/builders/core.py
Builder.run
def run(self, user_kw=None, build_kw=None): """Run the builder. :param user_kw: keywords from user :type user_kw: dict :param build_kw: internal settings :type build_kw: dict :return: Number of items processed :rtype: int """ user_kw = {} if user_kw is None else user_kw build_kw = {} if build_kw is None else build_kw n = self._build(self.get_items(**user_kw), **build_kw) finalized = self.finalize(self._status.has_failures()) if not finalized: _log.error("Finalization failed") return n
python
def run(self, user_kw=None, build_kw=None): user_kw = {} if user_kw is None else user_kw build_kw = {} if build_kw is None else build_kw n = self._build(self.get_items(**user_kw), **build_kw) finalized = self.finalize(self._status.has_failures()) if not finalized: _log.error("Finalization failed") return n
[ "def", "run", "(", "self", ",", "user_kw", "=", "None", ",", "build_kw", "=", "None", ")", ":", "user_kw", "=", "{", "}", "if", "user_kw", "is", "None", "else", "user_kw", "build_kw", "=", "{", "}", "if", "build_kw", "is", "None", "else", "build_kw",...
Run the builder. :param user_kw: keywords from user :type user_kw: dict :param build_kw: internal settings :type build_kw: dict :return: Number of items processed :rtype: int
[ "Run", "the", "builder", "." ]
02e4351c2cea431407644f49193e8bf43ed39b9a
https://github.com/materialsproject/pymatgen-db/blob/02e4351c2cea431407644f49193e8bf43ed39b9a/matgendb/builders/core.py#L351-L367
14,906
materialsproject/pymatgen-db
matgendb/builders/core.py
Builder.connect
def connect(self, config): """Connect to database with given configuration, which may be a dict or a path to a pymatgen-db configuration. """ if isinstance(config, str): conn = dbutil.get_database(config_file=config) elif isinstance(config, dict): conn = dbutil.get_database(settings=config) else: raise ValueError("Configuration, '{}', must be a path to " "a configuration file or dict".format(config)) return conn
python
def connect(self, config): if isinstance(config, str): conn = dbutil.get_database(config_file=config) elif isinstance(config, dict): conn = dbutil.get_database(settings=config) else: raise ValueError("Configuration, '{}', must be a path to " "a configuration file or dict".format(config)) return conn
[ "def", "connect", "(", "self", ",", "config", ")", ":", "if", "isinstance", "(", "config", ",", "str", ")", ":", "conn", "=", "dbutil", ".", "get_database", "(", "config_file", "=", "config", ")", "elif", "isinstance", "(", "config", ",", "dict", ")", ...
Connect to database with given configuration, which may be a dict or a path to a pymatgen-db configuration.
[ "Connect", "to", "database", "with", "given", "configuration", "which", "may", "be", "a", "dict", "or", "a", "path", "to", "a", "pymatgen", "-", "db", "configuration", "." ]
02e4351c2cea431407644f49193e8bf43ed39b9a
https://github.com/materialsproject/pymatgen-db/blob/02e4351c2cea431407644f49193e8bf43ed39b9a/matgendb/builders/core.py#L369-L380
14,907
materialsproject/pymatgen-db
matgendb/builders/core.py
Builder._build
def _build(self, items, chunk_size=10000): """Build the output, in chunks. :return: Number of items processed :rtype: int """ _log.debug("_build, chunk_size={:d}".format(chunk_size)) n, i = 0, 0 for i, item in enumerate(items): if i == 0: _log.debug("_build, first item") if 0 == (i + 1) % chunk_size: if self._seq: self._run(0) else: self._run_parallel_fn() # process the chunk if self._status.has_failures(): break n = i + 1 self._queue.put(item) # process final chunk if self._seq: self._run(0) else: self._run_parallel_fn() if not self._status.has_failures(): n = i + 1 return n
python
def _build(self, items, chunk_size=10000): _log.debug("_build, chunk_size={:d}".format(chunk_size)) n, i = 0, 0 for i, item in enumerate(items): if i == 0: _log.debug("_build, first item") if 0 == (i + 1) % chunk_size: if self._seq: self._run(0) else: self._run_parallel_fn() # process the chunk if self._status.has_failures(): break n = i + 1 self._queue.put(item) # process final chunk if self._seq: self._run(0) else: self._run_parallel_fn() if not self._status.has_failures(): n = i + 1 return n
[ "def", "_build", "(", "self", ",", "items", ",", "chunk_size", "=", "10000", ")", ":", "_log", ".", "debug", "(", "\"_build, chunk_size={:d}\"", ".", "format", "(", "chunk_size", ")", ")", "n", ",", "i", "=", "0", ",", "0", "for", "i", ",", "item", ...
Build the output, in chunks. :return: Number of items processed :rtype: int
[ "Build", "the", "output", "in", "chunks", "." ]
02e4351c2cea431407644f49193e8bf43ed39b9a
https://github.com/materialsproject/pymatgen-db/blob/02e4351c2cea431407644f49193e8bf43ed39b9a/matgendb/builders/core.py#L384-L411
14,908
materialsproject/pymatgen-db
matgendb/builders/core.py
Builder._run_parallel_multiprocess
def _run_parallel_multiprocess(self): """Run processes from queue """ _log.debug("run.parallel.multiprocess.start") processes = [] ProcRunner.instance = self for i in range(self._ncores): self._status.running(i) proc = multiprocessing.Process(target=ProcRunner.run, args=(i,)) proc.start() processes.append(proc) for i in range(self._ncores): processes[i].join() code = processes[i].exitcode self._status.success(i) if 0 == code else self._status.fail(i) _log.debug("run.parallel.multiprocess.end states={}".format(self._status))
python
def _run_parallel_multiprocess(self): _log.debug("run.parallel.multiprocess.start") processes = [] ProcRunner.instance = self for i in range(self._ncores): self._status.running(i) proc = multiprocessing.Process(target=ProcRunner.run, args=(i,)) proc.start() processes.append(proc) for i in range(self._ncores): processes[i].join() code = processes[i].exitcode self._status.success(i) if 0 == code else self._status.fail(i) _log.debug("run.parallel.multiprocess.end states={}".format(self._status))
[ "def", "_run_parallel_multiprocess", "(", "self", ")", ":", "_log", ".", "debug", "(", "\"run.parallel.multiprocess.start\"", ")", "processes", "=", "[", "]", "ProcRunner", ".", "instance", "=", "self", "for", "i", "in", "range", "(", "self", ".", "_ncores", ...
Run processes from queue
[ "Run", "processes", "from", "queue" ]
02e4351c2cea431407644f49193e8bf43ed39b9a
https://github.com/materialsproject/pymatgen-db/blob/02e4351c2cea431407644f49193e8bf43ed39b9a/matgendb/builders/core.py#L413-L428
14,909
materialsproject/pymatgen-db
matgendb/builders/core.py
Builder._run
def _run(self, index): """Run method for one thread or process Just pull an item off the queue and process it, until the queue is empty. :param index: Sequential index of this process or thread :type index: int """ while 1: try: item = self._queue.get(timeout=2) self.process_item(item) except Queue.Empty: break except Exception as err: _log.error("In _run(): {}".format(err)) if _log.isEnabledFor(logging.DEBUG): _log.error(traceback.format_exc()) self._status.fail(index) raise self._status.success(index)
python
def _run(self, index): while 1: try: item = self._queue.get(timeout=2) self.process_item(item) except Queue.Empty: break except Exception as err: _log.error("In _run(): {}".format(err)) if _log.isEnabledFor(logging.DEBUG): _log.error(traceback.format_exc()) self._status.fail(index) raise self._status.success(index)
[ "def", "_run", "(", "self", ",", "index", ")", ":", "while", "1", ":", "try", ":", "item", "=", "self", ".", "_queue", ".", "get", "(", "timeout", "=", "2", ")", "self", ".", "process_item", "(", "item", ")", "except", "Queue", ".", "Empty", ":",...
Run method for one thread or process Just pull an item off the queue and process it, until the queue is empty. :param index: Sequential index of this process or thread :type index: int
[ "Run", "method", "for", "one", "thread", "or", "process", "Just", "pull", "an", "item", "off", "the", "queue", "and", "process", "it", "until", "the", "queue", "is", "empty", "." ]
02e4351c2cea431407644f49193e8bf43ed39b9a
https://github.com/materialsproject/pymatgen-db/blob/02e4351c2cea431407644f49193e8bf43ed39b9a/matgendb/builders/core.py#L430-L450
14,910
materialsproject/pymatgen-db
matgendb/util.py
csv_dict
def csv_dict(d): """Format dict to a string with comma-separated values. """ if len(d) == 0: return "{}" return "{" + ', '.join(["'{}': {}".format(k, quotable(v)) for k, v in d.items()]) + "}"
python
def csv_dict(d): if len(d) == 0: return "{}" return "{" + ', '.join(["'{}': {}".format(k, quotable(v)) for k, v in d.items()]) + "}"
[ "def", "csv_dict", "(", "d", ")", ":", "if", "len", "(", "d", ")", "==", "0", ":", "return", "\"{}\"", "return", "\"{\"", "+", "', '", ".", "join", "(", "[", "\"'{}': {}\"", ".", "format", "(", "k", ",", "quotable", "(", "v", ")", ")", "for", "...
Format dict to a string with comma-separated values.
[ "Format", "dict", "to", "a", "string", "with", "comma", "-", "separated", "values", "." ]
02e4351c2cea431407644f49193e8bf43ed39b9a
https://github.com/materialsproject/pymatgen-db/blob/02e4351c2cea431407644f49193e8bf43ed39b9a/matgendb/util.py#L102-L108
14,911
materialsproject/pymatgen-db
matgendb/util.py
kvp_dict
def kvp_dict(d): """Format dict to key=value pairs. """ return ', '.join( ["{}={}".format(k, quotable(v)) for k, v in d.items()])
python
def kvp_dict(d): return ', '.join( ["{}={}".format(k, quotable(v)) for k, v in d.items()])
[ "def", "kvp_dict", "(", "d", ")", ":", "return", "', '", ".", "join", "(", "[", "\"{}={}\"", ".", "format", "(", "k", ",", "quotable", "(", "v", ")", ")", "for", "k", ",", "v", "in", "d", ".", "items", "(", ")", "]", ")" ]
Format dict to key=value pairs.
[ "Format", "dict", "to", "key", "=", "value", "pairs", "." ]
02e4351c2cea431407644f49193e8bf43ed39b9a
https://github.com/materialsproject/pymatgen-db/blob/02e4351c2cea431407644f49193e8bf43ed39b9a/matgendb/util.py#L110-L114
14,912
materialsproject/pymatgen-db
matgendb/builders/examples/maxvalue_builder.py
MaxValueBuilder.get_items
def get_items(self, source=None, target=None): """Get all records from source collection to add to target. :param source: Input collection :type source: QueryEngine :param target: Output collection :type target: QueryEngine """ self._groups = self.shared_dict() self._target_coll = target.collection self._src = source return source.query()
python
def get_items(self, source=None, target=None): self._groups = self.shared_dict() self._target_coll = target.collection self._src = source return source.query()
[ "def", "get_items", "(", "self", ",", "source", "=", "None", ",", "target", "=", "None", ")", ":", "self", ".", "_groups", "=", "self", ".", "shared_dict", "(", ")", "self", ".", "_target_coll", "=", "target", ".", "collection", "self", ".", "_src", ...
Get all records from source collection to add to target. :param source: Input collection :type source: QueryEngine :param target: Output collection :type target: QueryEngine
[ "Get", "all", "records", "from", "source", "collection", "to", "add", "to", "target", "." ]
02e4351c2cea431407644f49193e8bf43ed39b9a
https://github.com/materialsproject/pymatgen-db/blob/02e4351c2cea431407644f49193e8bf43ed39b9a/matgendb/builders/examples/maxvalue_builder.py#L19-L30
14,913
materialsproject/pymatgen-db
matgendb/builders/examples/maxvalue_builder.py
MaxValueBuilder.process_item
def process_item(self, item): """Calculate new maximum value for each group, for "new" items only. """ group, value = item['group'], item['value'] if group in self._groups: cur_val = self._groups[group] self._groups[group] = max(cur_val, value) else: # New group. Could fetch old max. from target collection, # but for the sake of illustration recalculate it from # the source collection. self._src.tracking = False # examine entire collection new_max = value for rec in self._src.query(criteria={'group': group}, properties=['value']): new_max = max(new_max, rec['value']) self._src.tracking = True # back to incremental mode # calculate new max self._groups[group] = new_max
python
def process_item(self, item): group, value = item['group'], item['value'] if group in self._groups: cur_val = self._groups[group] self._groups[group] = max(cur_val, value) else: # New group. Could fetch old max. from target collection, # but for the sake of illustration recalculate it from # the source collection. self._src.tracking = False # examine entire collection new_max = value for rec in self._src.query(criteria={'group': group}, properties=['value']): new_max = max(new_max, rec['value']) self._src.tracking = True # back to incremental mode # calculate new max self._groups[group] = new_max
[ "def", "process_item", "(", "self", ",", "item", ")", ":", "group", ",", "value", "=", "item", "[", "'group'", "]", ",", "item", "[", "'value'", "]", "if", "group", "in", "self", ".", "_groups", ":", "cur_val", "=", "self", ".", "_groups", "[", "gr...
Calculate new maximum value for each group, for "new" items only.
[ "Calculate", "new", "maximum", "value", "for", "each", "group", "for", "new", "items", "only", "." ]
02e4351c2cea431407644f49193e8bf43ed39b9a
https://github.com/materialsproject/pymatgen-db/blob/02e4351c2cea431407644f49193e8bf43ed39b9a/matgendb/builders/examples/maxvalue_builder.py#L32-L51
14,914
materialsproject/pymatgen-db
matgendb/builders/examples/maxvalue_builder.py
MaxValueBuilder.finalize
def finalize(self, errs): """Update target collection with calculated maximum values. """ for group, value in self._groups.items(): doc = {'group': group, 'value': value} self._target_coll.update({'group': group}, doc, upsert=True) return True
python
def finalize(self, errs): for group, value in self._groups.items(): doc = {'group': group, 'value': value} self._target_coll.update({'group': group}, doc, upsert=True) return True
[ "def", "finalize", "(", "self", ",", "errs", ")", ":", "for", "group", ",", "value", "in", "self", ".", "_groups", ".", "items", "(", ")", ":", "doc", "=", "{", "'group'", ":", "group", ",", "'value'", ":", "value", "}", "self", ".", "_target_coll"...
Update target collection with calculated maximum values.
[ "Update", "target", "collection", "with", "calculated", "maximum", "values", "." ]
02e4351c2cea431407644f49193e8bf43ed39b9a
https://github.com/materialsproject/pymatgen-db/blob/02e4351c2cea431407644f49193e8bf43ed39b9a/matgendb/builders/examples/maxvalue_builder.py#L53-L59
14,915
materialsproject/pymatgen-db
matgendb/vv/validate.py
Projection.add
def add(self, field, op=None, val=None): """Update report fields to include new one, if it doesn't already. :param field: The field to include :type field: Field :param op: Operation :type op: ConstraintOperator :return: None """ if field.has_subfield(): self._fields[field.full_name] = 1 else: self._fields[field.name] = 1 if op and op.is_size() and not op.is_variable(): # get minimal part of array with slicing, # but cannot use slice with variables self._slices[field.name] = val + 1 if op and op.is_variable(): # add the variable too self._fields[val] = 1
python
def add(self, field, op=None, val=None): if field.has_subfield(): self._fields[field.full_name] = 1 else: self._fields[field.name] = 1 if op and op.is_size() and not op.is_variable(): # get minimal part of array with slicing, # but cannot use slice with variables self._slices[field.name] = val + 1 if op and op.is_variable(): # add the variable too self._fields[val] = 1
[ "def", "add", "(", "self", ",", "field", ",", "op", "=", "None", ",", "val", "=", "None", ")", ":", "if", "field", ".", "has_subfield", "(", ")", ":", "self", ".", "_fields", "[", "field", ".", "full_name", "]", "=", "1", "else", ":", "self", "...
Update report fields to include new one, if it doesn't already. :param field: The field to include :type field: Field :param op: Operation :type op: ConstraintOperator :return: None
[ "Update", "report", "fields", "to", "include", "new", "one", "if", "it", "doesn", "t", "already", "." ]
02e4351c2cea431407644f49193e8bf43ed39b9a
https://github.com/materialsproject/pymatgen-db/blob/02e4351c2cea431407644f49193e8bf43ed39b9a/matgendb/vv/validate.py#L101-L120
14,916
materialsproject/pymatgen-db
matgendb/vv/validate.py
Projection.to_mongo
def to_mongo(self): """Translate projection to MongoDB query form. :return: Dictionary to put into a MongoDB JSON query :rtype: dict """ d = copy.copy(self._fields) for k, v in self._slices.items(): d[k] = {'$slice': v} return d
python
def to_mongo(self): d = copy.copy(self._fields) for k, v in self._slices.items(): d[k] = {'$slice': v} return d
[ "def", "to_mongo", "(", "self", ")", ":", "d", "=", "copy", ".", "copy", "(", "self", ".", "_fields", ")", "for", "k", ",", "v", "in", "self", ".", "_slices", ".", "items", "(", ")", ":", "d", "[", "k", "]", "=", "{", "'$slice'", ":", "v", ...
Translate projection to MongoDB query form. :return: Dictionary to put into a MongoDB JSON query :rtype: dict
[ "Translate", "projection", "to", "MongoDB", "query", "form", "." ]
02e4351c2cea431407644f49193e8bf43ed39b9a
https://github.com/materialsproject/pymatgen-db/blob/02e4351c2cea431407644f49193e8bf43ed39b9a/matgendb/vv/validate.py#L122-L131
14,917
materialsproject/pymatgen-db
matgendb/vv/validate.py
ConstraintViolationGroup.add_violations
def add_violations(self, violations, record=None): """Add constraint violations and associated record. :param violations: List of violations :type violations: list(ConstraintViolation) :param record: Associated record :type record: dict :rtype: None """ rec = {} if record is None else record for v in violations: self._viol.append((v, rec))
python
def add_violations(self, violations, record=None): rec = {} if record is None else record for v in violations: self._viol.append((v, rec))
[ "def", "add_violations", "(", "self", ",", "violations", ",", "record", "=", "None", ")", ":", "rec", "=", "{", "}", "if", "record", "is", "None", "else", "record", "for", "v", "in", "violations", ":", "self", ".", "_viol", ".", "append", "(", "(", ...
Add constraint violations and associated record. :param violations: List of violations :type violations: list(ConstraintViolation) :param record: Associated record :type record: dict :rtype: None
[ "Add", "constraint", "violations", "and", "associated", "record", "." ]
02e4351c2cea431407644f49193e8bf43ed39b9a
https://github.com/materialsproject/pymatgen-db/blob/02e4351c2cea431407644f49193e8bf43ed39b9a/matgendb/vv/validate.py#L187-L198
14,918
materialsproject/pymatgen-db
matgendb/vv/validate.py
ConstraintSpec._add_complex_section
def _add_complex_section(self, item): """Add a section that has a filter and set of constraints :raise: ValueError if filter or constraints is missing """ # extract filter and constraints try: fltr = item[self.FILTER_SECT] except KeyError: raise ValueError("configuration requires '{}'".format(self.FILTER_SECT)) sample = item.get(self.SAMPLE_SECT, None) constraints = item.get(self.CONSTRAINT_SECT, None) section = ConstraintSpecSection(fltr, constraints, sample) key = section.get_key() if key in self._sections: self._sections[key].append(section) else: self._sections[key] = [section]
python
def _add_complex_section(self, item): # extract filter and constraints try: fltr = item[self.FILTER_SECT] except KeyError: raise ValueError("configuration requires '{}'".format(self.FILTER_SECT)) sample = item.get(self.SAMPLE_SECT, None) constraints = item.get(self.CONSTRAINT_SECT, None) section = ConstraintSpecSection(fltr, constraints, sample) key = section.get_key() if key in self._sections: self._sections[key].append(section) else: self._sections[key] = [section]
[ "def", "_add_complex_section", "(", "self", ",", "item", ")", ":", "# extract filter and constraints", "try", ":", "fltr", "=", "item", "[", "self", ".", "FILTER_SECT", "]", "except", "KeyError", ":", "raise", "ValueError", "(", "\"configuration requires '{}'\"", ...
Add a section that has a filter and set of constraints :raise: ValueError if filter or constraints is missing
[ "Add", "a", "section", "that", "has", "a", "filter", "and", "set", "of", "constraints" ]
02e4351c2cea431407644f49193e8bf43ed39b9a
https://github.com/materialsproject/pymatgen-db/blob/02e4351c2cea431407644f49193e8bf43ed39b9a/matgendb/vv/validate.py#L270-L288
14,919
materialsproject/pymatgen-db
matgendb/vv/validate.py
Validator.validate
def validate(self, coll, constraint_spec, subject='collection'): """Validation of a collection. This is a generator that yields ConstraintViolationGroups. :param coll: Mongo collection :type coll: pymongo.Collection :param constraint_spec: Constraint specification :type constraint_spec: ConstraintSpec :param subject: Name of the thing being validated :type subject: str :return: Sets of constraint violation, one for each constraint_section :rtype: ConstraintViolationGroup :raises: ValidatorSyntaxError """ self._spec = constraint_spec self._progress.set_subject(subject) self._build(constraint_spec) for sect_parts in self._sections: cvg = self._validate_section(subject, coll, sect_parts) if cvg is not None: yield cvg
python
def validate(self, coll, constraint_spec, subject='collection'): self._spec = constraint_spec self._progress.set_subject(subject) self._build(constraint_spec) for sect_parts in self._sections: cvg = self._validate_section(subject, coll, sect_parts) if cvg is not None: yield cvg
[ "def", "validate", "(", "self", ",", "coll", ",", "constraint_spec", ",", "subject", "=", "'collection'", ")", ":", "self", ".", "_spec", "=", "constraint_spec", "self", ".", "_progress", ".", "set_subject", "(", "subject", ")", "self", ".", "_build", "(",...
Validation of a collection. This is a generator that yields ConstraintViolationGroups. :param coll: Mongo collection :type coll: pymongo.Collection :param constraint_spec: Constraint specification :type constraint_spec: ConstraintSpec :param subject: Name of the thing being validated :type subject: str :return: Sets of constraint violation, one for each constraint_section :rtype: ConstraintViolationGroup :raises: ValidatorSyntaxError
[ "Validation", "of", "a", "collection", ".", "This", "is", "a", "generator", "that", "yields", "ConstraintViolationGroups", "." ]
02e4351c2cea431407644f49193e8bf43ed39b9a
https://github.com/materialsproject/pymatgen-db/blob/02e4351c2cea431407644f49193e8bf43ed39b9a/matgendb/vv/validate.py#L380-L400
14,920
materialsproject/pymatgen-db
matgendb/vv/validate.py
Validator._validate_section
def _validate_section(self, subject, coll, parts): """Validate one section of a spec. :param subject: Name of subject :type subject: str :param coll: The collection to validate :type coll: pymongo.Collection :param parts: Section parts :type parts: Validator.SectionParts :return: Group of constraint violations, if any, otherwise None :rtype: ConstraintViolationGroup or None """ cvgroup = ConstraintViolationGroup() cvgroup.subject = subject # If the constraint is an 'import' of code, treat it differently here # if self._is_python(parts): # num_found = self._run_python(cvgroup, coll, parts) # return None if num_found == 0 else cvgroup query = parts.cond.to_mongo(disjunction=False) query.update(parts.body.to_mongo()) cvgroup.condition = parts.cond.to_mongo(disjunction=False) self._log.debug('Query spec: {}'.format(query)) self._log.debug('Query fields: {}'.format(parts.report_fields)) # Find records that violate 1 or more constraints cursor = coll.find(query, parts.report_fields, **self._find_kw) if parts.sampler is not None: cursor = parts.sampler.sample(cursor) nbytes, num_dberr, num_rec = 0, 0, 0 while 1: try: record = next(cursor) nbytes += total_size(record) num_rec += 1 except StopIteration: self._log.info("collection {}: {:d} records, {:d} bytes, {:d} db-errors" .format(subject, num_rec, nbytes, num_dberr)) break except pymongo.errors.PyMongoError as err: num_dberr += 1 if num_dberr > self._max_dberr > 0: raise DBError("Too many errors") self._log.warn("DB.{:d}: {}".format(num_dberr, err)) continue # report progress if self._progress: self._progress.update(num_dberr, nbytes) # get reasons for badness violations = self._get_violations(parts.body, record) cvgroup.add_violations(violations, record) return None if nbytes == 0 else cvgroup
python
def _validate_section(self, subject, coll, parts): cvgroup = ConstraintViolationGroup() cvgroup.subject = subject # If the constraint is an 'import' of code, treat it differently here # if self._is_python(parts): # num_found = self._run_python(cvgroup, coll, parts) # return None if num_found == 0 else cvgroup query = parts.cond.to_mongo(disjunction=False) query.update(parts.body.to_mongo()) cvgroup.condition = parts.cond.to_mongo(disjunction=False) self._log.debug('Query spec: {}'.format(query)) self._log.debug('Query fields: {}'.format(parts.report_fields)) # Find records that violate 1 or more constraints cursor = coll.find(query, parts.report_fields, **self._find_kw) if parts.sampler is not None: cursor = parts.sampler.sample(cursor) nbytes, num_dberr, num_rec = 0, 0, 0 while 1: try: record = next(cursor) nbytes += total_size(record) num_rec += 1 except StopIteration: self._log.info("collection {}: {:d} records, {:d} bytes, {:d} db-errors" .format(subject, num_rec, nbytes, num_dberr)) break except pymongo.errors.PyMongoError as err: num_dberr += 1 if num_dberr > self._max_dberr > 0: raise DBError("Too many errors") self._log.warn("DB.{:d}: {}".format(num_dberr, err)) continue # report progress if self._progress: self._progress.update(num_dberr, nbytes) # get reasons for badness violations = self._get_violations(parts.body, record) cvgroup.add_violations(violations, record) return None if nbytes == 0 else cvgroup
[ "def", "_validate_section", "(", "self", ",", "subject", ",", "coll", ",", "parts", ")", ":", "cvgroup", "=", "ConstraintViolationGroup", "(", ")", "cvgroup", ".", "subject", "=", "subject", "# If the constraint is an 'import' of code, treat it differently here", "# if ...
Validate one section of a spec. :param subject: Name of subject :type subject: str :param coll: The collection to validate :type coll: pymongo.Collection :param parts: Section parts :type parts: Validator.SectionParts :return: Group of constraint violations, if any, otherwise None :rtype: ConstraintViolationGroup or None
[ "Validate", "one", "section", "of", "a", "spec", "." ]
02e4351c2cea431407644f49193e8bf43ed39b9a
https://github.com/materialsproject/pymatgen-db/blob/02e4351c2cea431407644f49193e8bf43ed39b9a/matgendb/vv/validate.py#L402-L454
14,921
materialsproject/pymatgen-db
matgendb/vv/validate.py
Validator._get_violations
def _get_violations(self, query, record): """Reverse-engineer the query to figure out why a record was selected. :param query: MongoDB query :type query: MongQuery :param record: Record in question :type record: dict :return: Reasons why bad :rtype: list(ConstraintViolation) """ # special case, when no constraints are given if len(query.all_clauses) == 0: return [NullConstraintViolation()] # normal case, check all the constraints reasons = [] for clause in query.all_clauses: var_name = None key = clause.constraint.field.name op = clause.constraint.op fval = mongo_get(record, key) if fval is None: expected = clause.constraint.value reasons.append(ConstraintViolation(clause.constraint, 'missing', expected)) continue if op.is_variable(): # retrieve value for variable var_name = clause.constraint.value value = mongo_get(record, var_name, default=None) if value is None: reasons.append(ConstraintViolation(clause.constraint, 'missing', var_name)) continue clause.constraint.value = value # swap out value, temporarily # take length for size if op.is_size(): if isinstance(fval, str) or not hasattr(fval, '__len__'): reasons.append(ConstraintViolation(clause.constraint, type(fval), 'sequence')) if op.is_variable(): clause.constraint.value = var_name # put original value back continue fval = len(fval) ok, expected = clause.constraint.passes(fval) if not ok: reasons.append(ConstraintViolation(clause.constraint, fval, expected)) if op.is_variable(): clause.constraint.value = var_name # put original value back return reasons
python
def _get_violations(self, query, record): # special case, when no constraints are given if len(query.all_clauses) == 0: return [NullConstraintViolation()] # normal case, check all the constraints reasons = [] for clause in query.all_clauses: var_name = None key = clause.constraint.field.name op = clause.constraint.op fval = mongo_get(record, key) if fval is None: expected = clause.constraint.value reasons.append(ConstraintViolation(clause.constraint, 'missing', expected)) continue if op.is_variable(): # retrieve value for variable var_name = clause.constraint.value value = mongo_get(record, var_name, default=None) if value is None: reasons.append(ConstraintViolation(clause.constraint, 'missing', var_name)) continue clause.constraint.value = value # swap out value, temporarily # take length for size if op.is_size(): if isinstance(fval, str) or not hasattr(fval, '__len__'): reasons.append(ConstraintViolation(clause.constraint, type(fval), 'sequence')) if op.is_variable(): clause.constraint.value = var_name # put original value back continue fval = len(fval) ok, expected = clause.constraint.passes(fval) if not ok: reasons.append(ConstraintViolation(clause.constraint, fval, expected)) if op.is_variable(): clause.constraint.value = var_name # put original value back return reasons
[ "def", "_get_violations", "(", "self", ",", "query", ",", "record", ")", ":", "# special case, when no constraints are given", "if", "len", "(", "query", ".", "all_clauses", ")", "==", "0", ":", "return", "[", "NullConstraintViolation", "(", ")", "]", "# normal ...
Reverse-engineer the query to figure out why a record was selected. :param query: MongoDB query :type query: MongQuery :param record: Record in question :type record: dict :return: Reasons why bad :rtype: list(ConstraintViolation)
[ "Reverse", "-", "engineer", "the", "query", "to", "figure", "out", "why", "a", "record", "was", "selected", "." ]
02e4351c2cea431407644f49193e8bf43ed39b9a
https://github.com/materialsproject/pymatgen-db/blob/02e4351c2cea431407644f49193e8bf43ed39b9a/matgendb/vv/validate.py#L456-L501
14,922
materialsproject/pymatgen-db
matgendb/vv/validate.py
Validator._build
def _build(self, constraint_spec): """Generate queries to execute. Sets instance variables so that Mongo query strings, etc. can now be extracted from the object. :param constraint_spec: Constraint specification :type constraint_spec: ConstraintSpec """ self._sections = [] # For each condition in the spec for sval in constraint_spec: rpt_fld = self._base_report_fields.copy() #print("@@ CONDS = {}".format(sval.filters)) #print("@@ MAIN = {}".format(sval.constraints)) # Constraints # If the constraint is an external call to Python code if self._is_python(sval.constraints): query, proj = self._process_python(sval.constraints) rpt_fld.update(proj.to_mongo()) # All other constraints, e.g. 'foo > 12' else: query = MongoQuery() if sval.constraints is not None: groups = self._process_constraint_expressions(sval.constraints) projection = Projection() for cg in groups.values(): for c in cg: projection.add(c.field, c.op, c.value) query.add_clause(MongoClause(c)) if self._add_exists: for c in cg.existence_constraints: query.add_clause(MongoClause(c, exists_main=True)) rpt_fld.update(projection.to_mongo()) # Filters cond_query = MongoQuery() if sval.filters is not None: cond_groups = self._process_constraint_expressions(sval.filters, rev=False) for cg in cond_groups.values(): for c in cg: cond_query.add_clause(MongoClause(c, rev=False)) # Done. Add a new 'SectionPart' for the filter and constraint result = self.SectionParts(cond_query, query, sval.sampler, rpt_fld) self._sections.append(result)
python
def _build(self, constraint_spec): self._sections = [] # For each condition in the spec for sval in constraint_spec: rpt_fld = self._base_report_fields.copy() #print("@@ CONDS = {}".format(sval.filters)) #print("@@ MAIN = {}".format(sval.constraints)) # Constraints # If the constraint is an external call to Python code if self._is_python(sval.constraints): query, proj = self._process_python(sval.constraints) rpt_fld.update(proj.to_mongo()) # All other constraints, e.g. 'foo > 12' else: query = MongoQuery() if sval.constraints is not None: groups = self._process_constraint_expressions(sval.constraints) projection = Projection() for cg in groups.values(): for c in cg: projection.add(c.field, c.op, c.value) query.add_clause(MongoClause(c)) if self._add_exists: for c in cg.existence_constraints: query.add_clause(MongoClause(c, exists_main=True)) rpt_fld.update(projection.to_mongo()) # Filters cond_query = MongoQuery() if sval.filters is not None: cond_groups = self._process_constraint_expressions(sval.filters, rev=False) for cg in cond_groups.values(): for c in cg: cond_query.add_clause(MongoClause(c, rev=False)) # Done. Add a new 'SectionPart' for the filter and constraint result = self.SectionParts(cond_query, query, sval.sampler, rpt_fld) self._sections.append(result)
[ "def", "_build", "(", "self", ",", "constraint_spec", ")", ":", "self", ".", "_sections", "=", "[", "]", "# For each condition in the spec", "for", "sval", "in", "constraint_spec", ":", "rpt_fld", "=", "self", ".", "_base_report_fields", ".", "copy", "(", ")",...
Generate queries to execute. Sets instance variables so that Mongo query strings, etc. can now be extracted from the object. :param constraint_spec: Constraint specification :type constraint_spec: ConstraintSpec
[ "Generate", "queries", "to", "execute", "." ]
02e4351c2cea431407644f49193e8bf43ed39b9a
https://github.com/materialsproject/pymatgen-db/blob/02e4351c2cea431407644f49193e8bf43ed39b9a/matgendb/vv/validate.py#L503-L555
14,923
materialsproject/pymatgen-db
matgendb/vv/validate.py
Validator._process_constraint_expressions
def _process_constraint_expressions(self, expr_list, conflict_check=True, rev=True): """Create and return constraints from expressions in expr_list. :param expr_list: The expressions :conflict_check: If True, check for conflicting expressions within each field :return: Constraints grouped by field (the key is the field name) :rtype: dict """ # process expressions, grouping by field groups = {} for expr in expr_list: field, raw_op, val = parse_expr(expr) op = ConstraintOperator(raw_op) if field not in groups: groups[field] = ConstraintGroup(Field(field, self._aliases)) groups[field].add_constraint(op, val) # add existence constraints for cgroup in groups.values(): cgroup.add_existence(rev) # optionally check for conflicts if conflict_check: # check for conflicts in each group for field_name, group in groups.items(): conflicts = group.get_conflicts() if conflicts: raise ValueError('Conflicts for field {}: {}'.format(field_name, conflicts)) return groups
python
def _process_constraint_expressions(self, expr_list, conflict_check=True, rev=True): # process expressions, grouping by field groups = {} for expr in expr_list: field, raw_op, val = parse_expr(expr) op = ConstraintOperator(raw_op) if field not in groups: groups[field] = ConstraintGroup(Field(field, self._aliases)) groups[field].add_constraint(op, val) # add existence constraints for cgroup in groups.values(): cgroup.add_existence(rev) # optionally check for conflicts if conflict_check: # check for conflicts in each group for field_name, group in groups.items(): conflicts = group.get_conflicts() if conflicts: raise ValueError('Conflicts for field {}: {}'.format(field_name, conflicts)) return groups
[ "def", "_process_constraint_expressions", "(", "self", ",", "expr_list", ",", "conflict_check", "=", "True", ",", "rev", "=", "True", ")", ":", "# process expressions, grouping by field", "groups", "=", "{", "}", "for", "expr", "in", "expr_list", ":", "field", "...
Create and return constraints from expressions in expr_list. :param expr_list: The expressions :conflict_check: If True, check for conflicting expressions within each field :return: Constraints grouped by field (the key is the field name) :rtype: dict
[ "Create", "and", "return", "constraints", "from", "expressions", "in", "expr_list", "." ]
02e4351c2cea431407644f49193e8bf43ed39b9a
https://github.com/materialsproject/pymatgen-db/blob/02e4351c2cea431407644f49193e8bf43ed39b9a/matgendb/vv/validate.py#L557-L585
14,924
materialsproject/pymatgen-db
matgendb/vv/validate.py
Validator._is_python
def _is_python(self, constraint_list): """Check whether constraint is an import of Python code. :param constraint_list: List of raw constraints from YAML file :type constraint_list: list(str) :return: True if this refers to an import of code, False otherwise :raises: ValidatorSyntaxError """ if len(constraint_list) == 1 and \ PythonMethod.constraint_is_method(constraint_list[0]): return True if len(constraint_list) > 1 and \ any(filter(PythonMethod.constraint_is_method, constraint_list)): condensed_list = '/'.join(constraint_list) err = PythonMethod.CANNOT_COMBINE_ERR raise ValidatorSyntaxError(condensed_list, err) return False
python
def _is_python(self, constraint_list): if len(constraint_list) == 1 and \ PythonMethod.constraint_is_method(constraint_list[0]): return True if len(constraint_list) > 1 and \ any(filter(PythonMethod.constraint_is_method, constraint_list)): condensed_list = '/'.join(constraint_list) err = PythonMethod.CANNOT_COMBINE_ERR raise ValidatorSyntaxError(condensed_list, err) return False
[ "def", "_is_python", "(", "self", ",", "constraint_list", ")", ":", "if", "len", "(", "constraint_list", ")", "==", "1", "and", "PythonMethod", ".", "constraint_is_method", "(", "constraint_list", "[", "0", "]", ")", ":", "return", "True", "if", "len", "("...
Check whether constraint is an import of Python code. :param constraint_list: List of raw constraints from YAML file :type constraint_list: list(str) :return: True if this refers to an import of code, False otherwise :raises: ValidatorSyntaxError
[ "Check", "whether", "constraint", "is", "an", "import", "of", "Python", "code", "." ]
02e4351c2cea431407644f49193e8bf43ed39b9a
https://github.com/materialsproject/pymatgen-db/blob/02e4351c2cea431407644f49193e8bf43ed39b9a/matgendb/vv/validate.py#L587-L603
14,925
materialsproject/pymatgen-db
matgendb/vv/validate.py
Validator.set_aliases
def set_aliases(self, new_value): "Set aliases and wrap errors in ValueError" try: self.aliases = new_value except Exception as err: raise ValueError("invalid value: {}".format(err))
python
def set_aliases(self, new_value): "Set aliases and wrap errors in ValueError" try: self.aliases = new_value except Exception as err: raise ValueError("invalid value: {}".format(err))
[ "def", "set_aliases", "(", "self", ",", "new_value", ")", ":", "try", ":", "self", ".", "aliases", "=", "new_value", "except", "Exception", "as", "err", ":", "raise", "ValueError", "(", "\"invalid value: {}\"", ".", "format", "(", "err", ")", ")" ]
Set aliases and wrap errors in ValueError
[ "Set", "aliases", "and", "wrap", "errors", "in", "ValueError" ]
02e4351c2cea431407644f49193e8bf43ed39b9a
https://github.com/materialsproject/pymatgen-db/blob/02e4351c2cea431407644f49193e8bf43ed39b9a/matgendb/vv/validate.py#L614-L619
14,926
materialsproject/pymatgen-db
matgendb/vv/validate.py
Sampler.sample
def sample(self, cursor): """Extract records randomly from the database. Continue until the target proportion of the items have been extracted, or until `min_items` if this is larger. If `max_items` is non-negative, do not extract more than these. This function is a generator, yielding items incrementally. :param cursor: Cursor to sample :type cursor: pymongo.cursor.Cursor :return: yields each item :rtype: dict :raise: ValueError, if max_items is valid and less than `min_items` or if target collection is empty """ count = cursor.count() # special case: empty collection if count == 0: self._empty = True raise ValueError("Empty collection") # special case: entire collection if self.p >= 1 and self.max_items <= 0: for item in cursor: yield item return # calculate target number of items to select if self.max_items <= 0: n_target = max(self.min_items, self.p * count) else: if self.p <= 0: n_target = max(self.min_items, self.max_items) else: n_target = max(self.min_items, min(self.max_items, self.p * count)) if n_target == 0: raise ValueError("No items requested") # select first `n_target` items that pop up with # probability self.p # This is actually biased to items at the beginning # of the file if n_target is smaller than (p * count), n = 0 while n < n_target: try: item = next(cursor) except StopIteration: # need to keep looping through data until # we get all our items! cursor.rewind() item = next(cursor) if self._keep(): yield item n += 1
python
def sample(self, cursor): count = cursor.count() # special case: empty collection if count == 0: self._empty = True raise ValueError("Empty collection") # special case: entire collection if self.p >= 1 and self.max_items <= 0: for item in cursor: yield item return # calculate target number of items to select if self.max_items <= 0: n_target = max(self.min_items, self.p * count) else: if self.p <= 0: n_target = max(self.min_items, self.max_items) else: n_target = max(self.min_items, min(self.max_items, self.p * count)) if n_target == 0: raise ValueError("No items requested") # select first `n_target` items that pop up with # probability self.p # This is actually biased to items at the beginning # of the file if n_target is smaller than (p * count), n = 0 while n < n_target: try: item = next(cursor) except StopIteration: # need to keep looping through data until # we get all our items! cursor.rewind() item = next(cursor) if self._keep(): yield item n += 1
[ "def", "sample", "(", "self", ",", "cursor", ")", ":", "count", "=", "cursor", ".", "count", "(", ")", "# special case: empty collection", "if", "count", "==", "0", ":", "self", ".", "_empty", "=", "True", "raise", "ValueError", "(", "\"Empty collection\"", ...
Extract records randomly from the database. Continue until the target proportion of the items have been extracted, or until `min_items` if this is larger. If `max_items` is non-negative, do not extract more than these. This function is a generator, yielding items incrementally. :param cursor: Cursor to sample :type cursor: pymongo.cursor.Cursor :return: yields each item :rtype: dict :raise: ValueError, if max_items is valid and less than `min_items` or if target collection is empty
[ "Extract", "records", "randomly", "from", "the", "database", ".", "Continue", "until", "the", "target", "proportion", "of", "the", "items", "have", "been", "extracted", "or", "until", "min_items", "if", "this", "is", "larger", ".", "If", "max_items", "is", "...
02e4351c2cea431407644f49193e8bf43ed39b9a
https://github.com/materialsproject/pymatgen-db/blob/02e4351c2cea431407644f49193e8bf43ed39b9a/matgendb/vv/validate.py#L669-L723
14,927
indico/indico-plugins
livesync/indico_livesync/cli.py
available_backends
def available_backends(): """Lists the currently available backend types""" print 'The following LiveSync agents are available:' for name, backend in current_plugin.backend_classes.iteritems(): print cformat(' - %{white!}{}%{reset}: {} ({})').format(name, backend.title, backend.description)
python
def available_backends(): print 'The following LiveSync agents are available:' for name, backend in current_plugin.backend_classes.iteritems(): print cformat(' - %{white!}{}%{reset}: {} ({})').format(name, backend.title, backend.description)
[ "def", "available_backends", "(", ")", ":", "print", "'The following LiveSync agents are available:'", "for", "name", ",", "backend", "in", "current_plugin", ".", "backend_classes", ".", "iteritems", "(", ")", ":", "print", "cformat", "(", "' - %{white!}{}%{reset}: {} ...
Lists the currently available backend types
[ "Lists", "the", "currently", "available", "backend", "types" ]
fe50085cc63be9b8161b09539e662e7b04e4b38e
https://github.com/indico/indico-plugins/blob/fe50085cc63be9b8161b09539e662e7b04e4b38e/livesync/indico_livesync/cli.py#L37-L41
14,928
indico/indico-plugins
livesync/indico_livesync/cli.py
agents
def agents(): """Lists the currently active agents""" print 'The following LiveSync agents are active:' agent_list = LiveSyncAgent.find().order_by(LiveSyncAgent.backend_name, db.func.lower(LiveSyncAgent.name)).all() table_data = [['ID', 'Name', 'Backend', 'Initial Export', 'Queue']] for agent in agent_list: initial = (cformat('%{green!}done%{reset}') if agent.initial_data_exported else cformat('%{yellow!}pending%{reset}')) if agent.backend is None: backend_title = cformat('%{red!}invalid backend ({})%{reset}').format(agent.backend_name) else: backend_title = agent.backend.title table_data.append([unicode(agent.id), agent.name, backend_title, initial, unicode(agent.queue.filter_by(processed=False).count())]) table = AsciiTable(table_data) table.justify_columns[4] = 'right' print table.table if not all(a.initial_data_exported for a in agent_list): print print "You need to perform the initial data export for some agents." print cformat("To do so, run " "%{yellow!}indico livesync initial_export %{reset}%{yellow}<agent_id>%{reset} for those agents.")
python
def agents(): print 'The following LiveSync agents are active:' agent_list = LiveSyncAgent.find().order_by(LiveSyncAgent.backend_name, db.func.lower(LiveSyncAgent.name)).all() table_data = [['ID', 'Name', 'Backend', 'Initial Export', 'Queue']] for agent in agent_list: initial = (cformat('%{green!}done%{reset}') if agent.initial_data_exported else cformat('%{yellow!}pending%{reset}')) if agent.backend is None: backend_title = cformat('%{red!}invalid backend ({})%{reset}').format(agent.backend_name) else: backend_title = agent.backend.title table_data.append([unicode(agent.id), agent.name, backend_title, initial, unicode(agent.queue.filter_by(processed=False).count())]) table = AsciiTable(table_data) table.justify_columns[4] = 'right' print table.table if not all(a.initial_data_exported for a in agent_list): print print "You need to perform the initial data export for some agents." print cformat("To do so, run " "%{yellow!}indico livesync initial_export %{reset}%{yellow}<agent_id>%{reset} for those agents.")
[ "def", "agents", "(", ")", ":", "print", "'The following LiveSync agents are active:'", "agent_list", "=", "LiveSyncAgent", ".", "find", "(", ")", ".", "order_by", "(", "LiveSyncAgent", ".", "backend_name", ",", "db", ".", "func", ".", "lower", "(", "LiveSyncAge...
Lists the currently active agents
[ "Lists", "the", "currently", "active", "agents" ]
fe50085cc63be9b8161b09539e662e7b04e4b38e
https://github.com/indico/indico-plugins/blob/fe50085cc63be9b8161b09539e662e7b04e4b38e/livesync/indico_livesync/cli.py#L45-L66
14,929
indico/indico-plugins
livesync/indico_livesync/cli.py
initial_export
def initial_export(agent_id, force): """Performs the initial data export for an agent""" agent = LiveSyncAgent.find_first(id=agent_id) if agent is None: print 'No such agent' return if agent.backend is None: print cformat('Cannot run agent %{red!}{}%{reset} (backend not found)').format(agent.name) return print cformat('Selected agent: %{white!}{}%{reset} ({})').format(agent.name, agent.backend.title) if agent.initial_data_exported and not force: print 'The initial export has already been performed for this agent.' print cformat('To re-run it, use %{yellow!}--force%{reset}') return agent.create_backend().run_initial_export(Event.find(is_deleted=False)) agent.initial_data_exported = True db.session.commit()
python
def initial_export(agent_id, force): agent = LiveSyncAgent.find_first(id=agent_id) if agent is None: print 'No such agent' return if agent.backend is None: print cformat('Cannot run agent %{red!}{}%{reset} (backend not found)').format(agent.name) return print cformat('Selected agent: %{white!}{}%{reset} ({})').format(agent.name, agent.backend.title) if agent.initial_data_exported and not force: print 'The initial export has already been performed for this agent.' print cformat('To re-run it, use %{yellow!}--force%{reset}') return agent.create_backend().run_initial_export(Event.find(is_deleted=False)) agent.initial_data_exported = True db.session.commit()
[ "def", "initial_export", "(", "agent_id", ",", "force", ")", ":", "agent", "=", "LiveSyncAgent", ".", "find_first", "(", "id", "=", "agent_id", ")", "if", "agent", "is", "None", ":", "print", "'No such agent'", "return", "if", "agent", ".", "backend", "is"...
Performs the initial data export for an agent
[ "Performs", "the", "initial", "data", "export", "for", "an", "agent" ]
fe50085cc63be9b8161b09539e662e7b04e4b38e
https://github.com/indico/indico-plugins/blob/fe50085cc63be9b8161b09539e662e7b04e4b38e/livesync/indico_livesync/cli.py#L72-L89
14,930
indico/indico-plugins
livesync/indico_livesync/cli.py
run
def run(agent_id, force=False): """Runs the livesync agent""" if agent_id is None: agent_list = LiveSyncAgent.find_all() else: agent = LiveSyncAgent.find_first(id=agent_id) if agent is None: print 'No such agent' return agent_list = [agent] for agent in agent_list: if agent.backend is None: print cformat('Skipping agent: %{red!}{}%{reset} (backend not found)').format(agent.name) continue if not agent.initial_data_exported and not force: print cformat('Skipping agent: %{red!}{}%{reset} (initial export not performed)').format(agent.name) continue print cformat('Running agent: %{white!}{}%{reset}').format(agent.name) try: agent.create_backend().run() db.session.commit() except: db.session.rollback() raise
python
def run(agent_id, force=False): if agent_id is None: agent_list = LiveSyncAgent.find_all() else: agent = LiveSyncAgent.find_first(id=agent_id) if agent is None: print 'No such agent' return agent_list = [agent] for agent in agent_list: if agent.backend is None: print cformat('Skipping agent: %{red!}{}%{reset} (backend not found)').format(agent.name) continue if not agent.initial_data_exported and not force: print cformat('Skipping agent: %{red!}{}%{reset} (initial export not performed)').format(agent.name) continue print cformat('Running agent: %{white!}{}%{reset}').format(agent.name) try: agent.create_backend().run() db.session.commit() except: db.session.rollback() raise
[ "def", "run", "(", "agent_id", ",", "force", "=", "False", ")", ":", "if", "agent_id", "is", "None", ":", "agent_list", "=", "LiveSyncAgent", ".", "find_all", "(", ")", "else", ":", "agent", "=", "LiveSyncAgent", ".", "find_first", "(", "id", "=", "age...
Runs the livesync agent
[ "Runs", "the", "livesync", "agent" ]
fe50085cc63be9b8161b09539e662e7b04e4b38e
https://github.com/indico/indico-plugins/blob/fe50085cc63be9b8161b09539e662e7b04e4b38e/livesync/indico_livesync/cli.py#L95-L119
14,931
sbg/sevenbridges-python
sevenbridges/models/task.py
Task._serialize_inputs
def _serialize_inputs(inputs): """Serialize task input dictionary""" serialized_inputs = {} for input_id, input_value in inputs.items(): if isinstance(input_value, list): serialized_list = Task._serialize_input_list(input_value) serialized_inputs[input_id] = serialized_list else: if isinstance(input_value, File): input_value = Task._to_api_file_format(input_value) serialized_inputs[input_id] = input_value return serialized_inputs
python
def _serialize_inputs(inputs): serialized_inputs = {} for input_id, input_value in inputs.items(): if isinstance(input_value, list): serialized_list = Task._serialize_input_list(input_value) serialized_inputs[input_id] = serialized_list else: if isinstance(input_value, File): input_value = Task._to_api_file_format(input_value) serialized_inputs[input_id] = input_value return serialized_inputs
[ "def", "_serialize_inputs", "(", "inputs", ")", ":", "serialized_inputs", "=", "{", "}", "for", "input_id", ",", "input_value", "in", "inputs", ".", "items", "(", ")", ":", "if", "isinstance", "(", "input_value", ",", "list", ")", ":", "serialized_list", "...
Serialize task input dictionary
[ "Serialize", "task", "input", "dictionary" ]
f62640d1018d959f0b686f2dbe5e183085336607
https://github.com/sbg/sevenbridges-python/blob/f62640d1018d959f0b686f2dbe5e183085336607/sevenbridges/models/task.py#L334-L345
14,932
sbg/sevenbridges-python
sevenbridges/models/task.py
Task._serialize_input_list
def _serialize_input_list(input_value): """Recursively serialize task input list""" input_list = [] for item in input_value: if isinstance(item, list): input_list.append(Task._serialize_input_list(item)) else: if isinstance(item, File): item = Task._to_api_file_format(item) input_list.append(item) return input_list
python
def _serialize_input_list(input_value): input_list = [] for item in input_value: if isinstance(item, list): input_list.append(Task._serialize_input_list(item)) else: if isinstance(item, File): item = Task._to_api_file_format(item) input_list.append(item) return input_list
[ "def", "_serialize_input_list", "(", "input_value", ")", ":", "input_list", "=", "[", "]", "for", "item", "in", "input_value", ":", "if", "isinstance", "(", "item", ",", "list", ")", ":", "input_list", ".", "append", "(", "Task", ".", "_serialize_input_list"...
Recursively serialize task input list
[ "Recursively", "serialize", "task", "input", "list" ]
f62640d1018d959f0b686f2dbe5e183085336607
https://github.com/sbg/sevenbridges-python/blob/f62640d1018d959f0b686f2dbe5e183085336607/sevenbridges/models/task.py#L348-L358
14,933
indico/indico-plugins
piwik/indico_piwik/queries/graphs.py
PiwikQueryReportEventGraphBase.get_result
def get_result(self): """Perform the call and return the graph data :return: Encoded PNG graph data string to be inserted in a `src` atribute of a HTML img tag. """ png = self.call() if png is None: return if png.startswith('GD extension must be loaded'): current_plugin.logger.warning('Piwik server answered on ImageGraph.get: %s', png) return return 'data:image/png;base64,{}'.format(b64encode(png))
python
def get_result(self): png = self.call() if png is None: return if png.startswith('GD extension must be loaded'): current_plugin.logger.warning('Piwik server answered on ImageGraph.get: %s', png) return return 'data:image/png;base64,{}'.format(b64encode(png))
[ "def", "get_result", "(", "self", ")", ":", "png", "=", "self", ".", "call", "(", ")", "if", "png", "is", "None", ":", "return", "if", "png", ".", "startswith", "(", "'GD extension must be loaded'", ")", ":", "current_plugin", ".", "logger", ".", "warnin...
Perform the call and return the graph data :return: Encoded PNG graph data string to be inserted in a `src` atribute of a HTML img tag.
[ "Perform", "the", "call", "and", "return", "the", "graph", "data" ]
fe50085cc63be9b8161b09539e662e7b04e4b38e
https://github.com/indico/indico-plugins/blob/fe50085cc63be9b8161b09539e662e7b04e4b38e/piwik/indico_piwik/queries/graphs.py#L36-L48
14,934
indico/indico-plugins
chat/indico_chat/models/chatrooms.py
Chatroom.server
def server(self): """The server name of the chatroom. Usually the default one unless a custom one is set. """ from indico_chat.plugin import ChatPlugin return self.custom_server or ChatPlugin.settings.get('muc_server')
python
def server(self): from indico_chat.plugin import ChatPlugin return self.custom_server or ChatPlugin.settings.get('muc_server')
[ "def", "server", "(", "self", ")", ":", "from", "indico_chat", ".", "plugin", "import", "ChatPlugin", "return", "self", ".", "custom_server", "or", "ChatPlugin", ".", "settings", ".", "get", "(", "'muc_server'", ")" ]
The server name of the chatroom. Usually the default one unless a custom one is set.
[ "The", "server", "name", "of", "the", "chatroom", "." ]
fe50085cc63be9b8161b09539e662e7b04e4b38e
https://github.com/indico/indico-plugins/blob/fe50085cc63be9b8161b09539e662e7b04e4b38e/chat/indico_chat/models/chatrooms.py#L97-L104
14,935
indico/indico-plugins
chat/indico_chat/models/chatrooms.py
ChatroomEventAssociation.find_for_event
def find_for_event(cls, event, include_hidden=False, **kwargs): """Returns a Query that retrieves the chatrooms for an event :param event: an indico event (with a numeric ID) :param include_hidden: if hidden chatrooms should be included, too :param kwargs: extra kwargs to pass to ``find()`` """ query = cls.find(event_id=event.id, **kwargs) if not include_hidden: query = query.filter(~cls.hidden) return query
python
def find_for_event(cls, event, include_hidden=False, **kwargs): query = cls.find(event_id=event.id, **kwargs) if not include_hidden: query = query.filter(~cls.hidden) return query
[ "def", "find_for_event", "(", "cls", ",", "event", ",", "include_hidden", "=", "False", ",", "*", "*", "kwargs", ")", ":", "query", "=", "cls", ".", "find", "(", "event_id", "=", "event", ".", "id", ",", "*", "*", "kwargs", ")", "if", "not", "inclu...
Returns a Query that retrieves the chatrooms for an event :param event: an indico event (with a numeric ID) :param include_hidden: if hidden chatrooms should be included, too :param kwargs: extra kwargs to pass to ``find()``
[ "Returns", "a", "Query", "that", "retrieves", "the", "chatrooms", "for", "an", "event" ]
fe50085cc63be9b8161b09539e662e7b04e4b38e
https://github.com/indico/indico-plugins/blob/fe50085cc63be9b8161b09539e662e7b04e4b38e/chat/indico_chat/models/chatrooms.py#L175-L185
14,936
indico/indico-plugins
chat/indico_chat/models/chatrooms.py
ChatroomEventAssociation.delete
def delete(self, reason=''): """Deletes the event chatroom and if necessary the chatroom, too. :param reason: reason for the deletion :return: True if the associated chatroom was also deleted, otherwise False """ db.session.delete(self) db.session.flush() if not self.chatroom.events: db.session.delete(self.chatroom) db.session.flush() delete_room(self.chatroom, reason) return True return False
python
def delete(self, reason=''): db.session.delete(self) db.session.flush() if not self.chatroom.events: db.session.delete(self.chatroom) db.session.flush() delete_room(self.chatroom, reason) return True return False
[ "def", "delete", "(", "self", ",", "reason", "=", "''", ")", ":", "db", ".", "session", ".", "delete", "(", "self", ")", "db", ".", "session", ".", "flush", "(", ")", "if", "not", "self", ".", "chatroom", ".", "events", ":", "db", ".", "session",...
Deletes the event chatroom and if necessary the chatroom, too. :param reason: reason for the deletion :return: True if the associated chatroom was also deleted, otherwise False
[ "Deletes", "the", "event", "chatroom", "and", "if", "necessary", "the", "chatroom", "too", "." ]
fe50085cc63be9b8161b09539e662e7b04e4b38e
https://github.com/indico/indico-plugins/blob/fe50085cc63be9b8161b09539e662e7b04e4b38e/chat/indico_chat/models/chatrooms.py#L187-L201
14,937
indico/indico-plugins
livesync/indico_livesync/util.py
obj_deref
def obj_deref(ref): """Returns the object identified by `ref`""" from indico_livesync.models.queue import EntryType if ref['type'] == EntryType.category: return Category.get_one(ref['category_id']) elif ref['type'] == EntryType.event: return Event.get_one(ref['event_id']) elif ref['type'] == EntryType.session: return Session.get_one(ref['session_id']) elif ref['type'] == EntryType.contribution: return Contribution.get_one(ref['contrib_id']) elif ref['type'] == EntryType.subcontribution: return SubContribution.get_one(ref['subcontrib_id']) else: raise ValueError('Unexpected object type: {}'.format(ref['type']))
python
def obj_deref(ref): from indico_livesync.models.queue import EntryType if ref['type'] == EntryType.category: return Category.get_one(ref['category_id']) elif ref['type'] == EntryType.event: return Event.get_one(ref['event_id']) elif ref['type'] == EntryType.session: return Session.get_one(ref['session_id']) elif ref['type'] == EntryType.contribution: return Contribution.get_one(ref['contrib_id']) elif ref['type'] == EntryType.subcontribution: return SubContribution.get_one(ref['subcontrib_id']) else: raise ValueError('Unexpected object type: {}'.format(ref['type']))
[ "def", "obj_deref", "(", "ref", ")", ":", "from", "indico_livesync", ".", "models", ".", "queue", "import", "EntryType", "if", "ref", "[", "'type'", "]", "==", "EntryType", ".", "category", ":", "return", "Category", ".", "get_one", "(", "ref", "[", "'ca...
Returns the object identified by `ref`
[ "Returns", "the", "object", "identified", "by", "ref" ]
fe50085cc63be9b8161b09539e662e7b04e4b38e
https://github.com/indico/indico-plugins/blob/fe50085cc63be9b8161b09539e662e7b04e4b38e/livesync/indico_livesync/util.py#L51-L65
14,938
indico/indico-plugins
livesync/indico_livesync/util.py
clean_old_entries
def clean_old_entries(): """Deletes obsolete entries from the queues""" from indico_livesync.plugin import LiveSyncPlugin from indico_livesync.models.queue import LiveSyncQueueEntry queue_entry_ttl = LiveSyncPlugin.settings.get('queue_entry_ttl') if not queue_entry_ttl: return expire_threshold = now_utc() - timedelta(days=queue_entry_ttl) LiveSyncQueueEntry.find(LiveSyncQueueEntry.processed, LiveSyncQueueEntry.timestamp < expire_threshold).delete(synchronize_session='fetch')
python
def clean_old_entries(): from indico_livesync.plugin import LiveSyncPlugin from indico_livesync.models.queue import LiveSyncQueueEntry queue_entry_ttl = LiveSyncPlugin.settings.get('queue_entry_ttl') if not queue_entry_ttl: return expire_threshold = now_utc() - timedelta(days=queue_entry_ttl) LiveSyncQueueEntry.find(LiveSyncQueueEntry.processed, LiveSyncQueueEntry.timestamp < expire_threshold).delete(synchronize_session='fetch')
[ "def", "clean_old_entries", "(", ")", ":", "from", "indico_livesync", ".", "plugin", "import", "LiveSyncPlugin", "from", "indico_livesync", ".", "models", ".", "queue", "import", "LiveSyncQueueEntry", "queue_entry_ttl", "=", "LiveSyncPlugin", ".", "settings", ".", "...
Deletes obsolete entries from the queues
[ "Deletes", "obsolete", "entries", "from", "the", "queues" ]
fe50085cc63be9b8161b09539e662e7b04e4b38e
https://github.com/indico/indico-plugins/blob/fe50085cc63be9b8161b09539e662e7b04e4b38e/livesync/indico_livesync/util.py#L68-L78
14,939
indico/indico-plugins
livesync/indico_livesync/util.py
get_excluded_categories
def get_excluded_categories(): """Get excluded category IDs.""" from indico_livesync.plugin import LiveSyncPlugin return {int(x['id']) for x in LiveSyncPlugin.settings.get('excluded_categories')}
python
def get_excluded_categories(): from indico_livesync.plugin import LiveSyncPlugin return {int(x['id']) for x in LiveSyncPlugin.settings.get('excluded_categories')}
[ "def", "get_excluded_categories", "(", ")", ":", "from", "indico_livesync", ".", "plugin", "import", "LiveSyncPlugin", "return", "{", "int", "(", "x", "[", "'id'", "]", ")", "for", "x", "in", "LiveSyncPlugin", ".", "settings", ".", "get", "(", "'excluded_cat...
Get excluded category IDs.
[ "Get", "excluded", "category", "IDs", "." ]
fe50085cc63be9b8161b09539e662e7b04e4b38e
https://github.com/indico/indico-plugins/blob/fe50085cc63be9b8161b09539e662e7b04e4b38e/livesync/indico_livesync/util.py#L82-L85
14,940
indico/indico-plugins
livesync/indico_livesync/util.py
compound_id
def compound_id(obj): """Generate a hierarchical compound ID, separated by dots.""" if isinstance(obj, (Category, Session)): raise TypeError('Compound IDs are not supported for this entry type') elif isinstance(obj, Event): return unicode(obj.id) elif isinstance(obj, Contribution): return '{}.{}'.format(obj.event_id, obj.id) elif isinstance(obj, SubContribution): return '{}.{}.{}'.format(obj.contribution.event_id, obj.contribution_id, obj.id)
python
def compound_id(obj): if isinstance(obj, (Category, Session)): raise TypeError('Compound IDs are not supported for this entry type') elif isinstance(obj, Event): return unicode(obj.id) elif isinstance(obj, Contribution): return '{}.{}'.format(obj.event_id, obj.id) elif isinstance(obj, SubContribution): return '{}.{}.{}'.format(obj.contribution.event_id, obj.contribution_id, obj.id)
[ "def", "compound_id", "(", "obj", ")", ":", "if", "isinstance", "(", "obj", ",", "(", "Category", ",", "Session", ")", ")", ":", "raise", "TypeError", "(", "'Compound IDs are not supported for this entry type'", ")", "elif", "isinstance", "(", "obj", ",", "Eve...
Generate a hierarchical compound ID, separated by dots.
[ "Generate", "a", "hierarchical", "compound", "ID", "separated", "by", "dots", "." ]
fe50085cc63be9b8161b09539e662e7b04e4b38e
https://github.com/indico/indico-plugins/blob/fe50085cc63be9b8161b09539e662e7b04e4b38e/livesync/indico_livesync/util.py#L88-L97
14,941
indico/indico-plugins
piwik/indico_piwik/queries/tracking.py
track_download_request
def track_download_request(download_url, download_title): """Track a download in Piwik""" from indico_piwik.plugin import PiwikPlugin if not download_url: raise ValueError("download_url can't be empty") if not download_title: raise ValueError("download_title can't be empty") request = PiwikRequest(server_url=PiwikPlugin.settings.get('server_api_url'), site_id=PiwikPlugin.settings.get('site_id_events'), api_token=PiwikPlugin.settings.get('server_token'), query_script=PiwikPlugin.track_script) action_url = quote(download_url) dt = datetime.now() request.call(idsite=request.site_id, rec=1, action_name=quote(download_title.encode('utf-8')), url=action_url, download=action_url, h=dt.hour, m=dt.minute, s=dt.second)
python
def track_download_request(download_url, download_title): from indico_piwik.plugin import PiwikPlugin if not download_url: raise ValueError("download_url can't be empty") if not download_title: raise ValueError("download_title can't be empty") request = PiwikRequest(server_url=PiwikPlugin.settings.get('server_api_url'), site_id=PiwikPlugin.settings.get('site_id_events'), api_token=PiwikPlugin.settings.get('server_token'), query_script=PiwikPlugin.track_script) action_url = quote(download_url) dt = datetime.now() request.call(idsite=request.site_id, rec=1, action_name=quote(download_title.encode('utf-8')), url=action_url, download=action_url, h=dt.hour, m=dt.minute, s=dt.second)
[ "def", "track_download_request", "(", "download_url", ",", "download_title", ")", ":", "from", "indico_piwik", ".", "plugin", "import", "PiwikPlugin", "if", "not", "download_url", ":", "raise", "ValueError", "(", "\"download_url can't be empty\"", ")", "if", "not", ...
Track a download in Piwik
[ "Track", "a", "download", "in", "Piwik" ]
fe50085cc63be9b8161b09539e662e7b04e4b38e
https://github.com/indico/indico-plugins/blob/fe50085cc63be9b8161b09539e662e7b04e4b38e/piwik/indico_piwik/queries/tracking.py#L26-L47
14,942
indico/indico-plugins
livesync/indico_livesync/models/agents.py
LiveSyncAgent.backend
def backend(self): """Returns the backend class""" from indico_livesync.plugin import LiveSyncPlugin return LiveSyncPlugin.instance.backend_classes.get(self.backend_name)
python
def backend(self): from indico_livesync.plugin import LiveSyncPlugin return LiveSyncPlugin.instance.backend_classes.get(self.backend_name)
[ "def", "backend", "(", "self", ")", ":", "from", "indico_livesync", ".", "plugin", "import", "LiveSyncPlugin", "return", "LiveSyncPlugin", ".", "instance", ".", "backend_classes", ".", "get", "(", "self", ".", "backend_name", ")" ]
Returns the backend class
[ "Returns", "the", "backend", "class" ]
fe50085cc63be9b8161b09539e662e7b04e4b38e
https://github.com/indico/indico-plugins/blob/fe50085cc63be9b8161b09539e662e7b04e4b38e/livesync/indico_livesync/models/agents.py#L74-L77
14,943
indico/indico-plugins
chat/indico_chat/notifications.py
notify_created
def notify_created(room, event, user): """Notifies about the creation of a chatroom. :param room: the chatroom :param event: the event :param user: the user performing the action """ tpl = get_plugin_template_module('emails/created.txt', chatroom=room, event=event, user=user) _send(event, tpl)
python
def notify_created(room, event, user): tpl = get_plugin_template_module('emails/created.txt', chatroom=room, event=event, user=user) _send(event, tpl)
[ "def", "notify_created", "(", "room", ",", "event", ",", "user", ")", ":", "tpl", "=", "get_plugin_template_module", "(", "'emails/created.txt'", ",", "chatroom", "=", "room", ",", "event", "=", "event", ",", "user", "=", "user", ")", "_send", "(", "event"...
Notifies about the creation of a chatroom. :param room: the chatroom :param event: the event :param user: the user performing the action
[ "Notifies", "about", "the", "creation", "of", "a", "chatroom", "." ]
fe50085cc63be9b8161b09539e662e7b04e4b38e
https://github.com/indico/indico-plugins/blob/fe50085cc63be9b8161b09539e662e7b04e4b38e/chat/indico_chat/notifications.py#L23-L31
14,944
indico/indico-plugins
chat/indico_chat/notifications.py
notify_attached
def notify_attached(room, event, user): """Notifies about an existing chatroom being attached to an event. :param room: the chatroom :param event: the event :param user: the user performing the action """ tpl = get_plugin_template_module('emails/attached.txt', chatroom=room, event=event, user=user) _send(event, tpl)
python
def notify_attached(room, event, user): tpl = get_plugin_template_module('emails/attached.txt', chatroom=room, event=event, user=user) _send(event, tpl)
[ "def", "notify_attached", "(", "room", ",", "event", ",", "user", ")", ":", "tpl", "=", "get_plugin_template_module", "(", "'emails/attached.txt'", ",", "chatroom", "=", "room", ",", "event", "=", "event", ",", "user", "=", "user", ")", "_send", "(", "even...
Notifies about an existing chatroom being attached to an event. :param room: the chatroom :param event: the event :param user: the user performing the action
[ "Notifies", "about", "an", "existing", "chatroom", "being", "attached", "to", "an", "event", "." ]
fe50085cc63be9b8161b09539e662e7b04e4b38e
https://github.com/indico/indico-plugins/blob/fe50085cc63be9b8161b09539e662e7b04e4b38e/chat/indico_chat/notifications.py#L34-L42
14,945
indico/indico-plugins
chat/indico_chat/notifications.py
notify_modified
def notify_modified(room, event, user): """Notifies about the modification of a chatroom. :param room: the chatroom :param event: the event :param user: the user performing the action """ tpl = get_plugin_template_module('emails/modified.txt', chatroom=room, event=event, user=user) _send(event, tpl)
python
def notify_modified(room, event, user): tpl = get_plugin_template_module('emails/modified.txt', chatroom=room, event=event, user=user) _send(event, tpl)
[ "def", "notify_modified", "(", "room", ",", "event", ",", "user", ")", ":", "tpl", "=", "get_plugin_template_module", "(", "'emails/modified.txt'", ",", "chatroom", "=", "room", ",", "event", "=", "event", ",", "user", "=", "user", ")", "_send", "(", "even...
Notifies about the modification of a chatroom. :param room: the chatroom :param event: the event :param user: the user performing the action
[ "Notifies", "about", "the", "modification", "of", "a", "chatroom", "." ]
fe50085cc63be9b8161b09539e662e7b04e4b38e
https://github.com/indico/indico-plugins/blob/fe50085cc63be9b8161b09539e662e7b04e4b38e/chat/indico_chat/notifications.py#L45-L53
14,946
indico/indico-plugins
chat/indico_chat/notifications.py
notify_deleted
def notify_deleted(room, event, user, room_deleted): """Notifies about the deletion of a chatroom. :param room: the chatroom :param event: the event :param user: the user performing the action; `None` if due to event deletion :param room_deleted: if the room has been deleted from the jabber server """ tpl = get_plugin_template_module('emails/deleted.txt', chatroom=room, event=event, user=user, room_deleted=room_deleted) _send(event, tpl)
python
def notify_deleted(room, event, user, room_deleted): tpl = get_plugin_template_module('emails/deleted.txt', chatroom=room, event=event, user=user, room_deleted=room_deleted) _send(event, tpl)
[ "def", "notify_deleted", "(", "room", ",", "event", ",", "user", ",", "room_deleted", ")", ":", "tpl", "=", "get_plugin_template_module", "(", "'emails/deleted.txt'", ",", "chatroom", "=", "room", ",", "event", "=", "event", ",", "user", "=", "user", ",", ...
Notifies about the deletion of a chatroom. :param room: the chatroom :param event: the event :param user: the user performing the action; `None` if due to event deletion :param room_deleted: if the room has been deleted from the jabber server
[ "Notifies", "about", "the", "deletion", "of", "a", "chatroom", "." ]
fe50085cc63be9b8161b09539e662e7b04e4b38e
https://github.com/indico/indico-plugins/blob/fe50085cc63be9b8161b09539e662e7b04e4b38e/chat/indico_chat/notifications.py#L56-L66
14,947
sbg/sevenbridges-python
sevenbridges/transfer/upload.py
UPartedFile.get_parts
def get_parts(self): """ Partitions the file and saves the parts to be uploaded in memory. """ parts = [] start_byte = 0 for i in range(1, self.total + 1): end_byte = start_byte + self.part_size if end_byte >= self.file_size - 1: end_byte = self.file_size parts.append({ 'part': i, 'offset': start_byte, 'limit': end_byte }) start_byte = end_byte return parts
python
def get_parts(self): parts = [] start_byte = 0 for i in range(1, self.total + 1): end_byte = start_byte + self.part_size if end_byte >= self.file_size - 1: end_byte = self.file_size parts.append({ 'part': i, 'offset': start_byte, 'limit': end_byte }) start_byte = end_byte return parts
[ "def", "get_parts", "(", "self", ")", ":", "parts", "=", "[", "]", "start_byte", "=", "0", "for", "i", "in", "range", "(", "1", ",", "self", ".", "total", "+", "1", ")", ":", "end_byte", "=", "start_byte", "+", "self", ".", "part_size", "if", "en...
Partitions the file and saves the parts to be uploaded in memory.
[ "Partitions", "the", "file", "and", "saves", "the", "parts", "to", "be", "uploaded", "in", "memory", "." ]
f62640d1018d959f0b686f2dbe5e183085336607
https://github.com/sbg/sevenbridges-python/blob/f62640d1018d959f0b686f2dbe5e183085336607/sevenbridges/transfer/upload.py#L184-L201
14,948
sbg/sevenbridges-python
sevenbridges/transfer/upload.py
Upload._verify_part_number
def _verify_part_number(self): """ Verifies that the total number of parts is smaller then 10^5 which is the maximum number of parts. """ total = int(math.ceil(self._file_size / self._part_size)) if total > PartSize.MAXIMUM_TOTAL_PARTS: self._status = TransferState.FAILED raise SbgError( 'Total parts = {}. Maximum number of parts is {}'.format( total, PartSize.MAXIMUM_TOTAL_PARTS) )
python
def _verify_part_number(self): total = int(math.ceil(self._file_size / self._part_size)) if total > PartSize.MAXIMUM_TOTAL_PARTS: self._status = TransferState.FAILED raise SbgError( 'Total parts = {}. Maximum number of parts is {}'.format( total, PartSize.MAXIMUM_TOTAL_PARTS) )
[ "def", "_verify_part_number", "(", "self", ")", ":", "total", "=", "int", "(", "math", ".", "ceil", "(", "self", ".", "_file_size", "/", "self", ".", "_part_size", ")", ")", "if", "total", ">", "PartSize", ".", "MAXIMUM_TOTAL_PARTS", ":", "self", ".", ...
Verifies that the total number of parts is smaller then 10^5 which is the maximum number of parts.
[ "Verifies", "that", "the", "total", "number", "of", "parts", "is", "smaller", "then", "10^5", "which", "is", "the", "maximum", "number", "of", "parts", "." ]
f62640d1018d959f0b686f2dbe5e183085336607
https://github.com/sbg/sevenbridges-python/blob/f62640d1018d959f0b686f2dbe5e183085336607/sevenbridges/transfer/upload.py#L294-L305
14,949
sbg/sevenbridges-python
sevenbridges/transfer/upload.py
Upload._verify_part_size
def _verify_part_size(self): """ Verifies that the part size is smaller then the maximum part size which is 5GB. """ if self._part_size > PartSize.MAXIMUM_UPLOAD_SIZE: self._status = TransferState.FAILED raise SbgError('Part size = {}b. Maximum part size is {}b'.format( self._part_size, PartSize.MAXIMUM_UPLOAD_SIZE) )
python
def _verify_part_size(self): if self._part_size > PartSize.MAXIMUM_UPLOAD_SIZE: self._status = TransferState.FAILED raise SbgError('Part size = {}b. Maximum part size is {}b'.format( self._part_size, PartSize.MAXIMUM_UPLOAD_SIZE) )
[ "def", "_verify_part_size", "(", "self", ")", ":", "if", "self", ".", "_part_size", ">", "PartSize", ".", "MAXIMUM_UPLOAD_SIZE", ":", "self", ".", "_status", "=", "TransferState", ".", "FAILED", "raise", "SbgError", "(", "'Part size = {}b. Maximum part size is {}b'"...
Verifies that the part size is smaller then the maximum part size which is 5GB.
[ "Verifies", "that", "the", "part", "size", "is", "smaller", "then", "the", "maximum", "part", "size", "which", "is", "5GB", "." ]
f62640d1018d959f0b686f2dbe5e183085336607
https://github.com/sbg/sevenbridges-python/blob/f62640d1018d959f0b686f2dbe5e183085336607/sevenbridges/transfer/upload.py#L307-L316
14,950
sbg/sevenbridges-python
sevenbridges/transfer/upload.py
Upload._verify_file_size
def _verify_file_size(self): """ Verifies that the file is smaller then 5TB which is the maximum that is allowed for upload. """ if self._file_size > PartSize.MAXIMUM_OBJECT_SIZE: self._status = TransferState.FAILED raise SbgError('File size = {}b. Maximum file size is {}b'.format( self._file_size, PartSize.MAXIMUM_OBJECT_SIZE) )
python
def _verify_file_size(self): if self._file_size > PartSize.MAXIMUM_OBJECT_SIZE: self._status = TransferState.FAILED raise SbgError('File size = {}b. Maximum file size is {}b'.format( self._file_size, PartSize.MAXIMUM_OBJECT_SIZE) )
[ "def", "_verify_file_size", "(", "self", ")", ":", "if", "self", ".", "_file_size", ">", "PartSize", ".", "MAXIMUM_OBJECT_SIZE", ":", "self", ".", "_status", "=", "TransferState", ".", "FAILED", "raise", "SbgError", "(", "'File size = {}b. Maximum file size is {}b'"...
Verifies that the file is smaller then 5TB which is the maximum that is allowed for upload.
[ "Verifies", "that", "the", "file", "is", "smaller", "then", "5TB", "which", "is", "the", "maximum", "that", "is", "allowed", "for", "upload", "." ]
f62640d1018d959f0b686f2dbe5e183085336607
https://github.com/sbg/sevenbridges-python/blob/f62640d1018d959f0b686f2dbe5e183085336607/sevenbridges/transfer/upload.py#L318-L327
14,951
sbg/sevenbridges-python
sevenbridges/transfer/upload.py
Upload._initialize_upload
def _initialize_upload(self): """ Initialized the upload on the API server by submitting the information about the project, the file name, file size and the part size that is going to be used during multipart upload. """ init_data = { 'name': self._file_name, 'part_size': self._part_size, 'size': self._file_size } if self._project: init_data['project'] = self._project elif self._parent: init_data['parent'] = self._parent init_params = {} if self._overwrite: init_params['overwrite'] = self._overwrite try: response = self._api.post( self._URL['upload_init'], data=init_data, params=init_params ) self._upload_id = response.json()['upload_id'] except SbgError as e: self._status = TransferState.FAILED raise SbgError( 'Unable to initialize upload! Failed to get upload id! ' 'Reason: {}'.format(e.message) )
python
def _initialize_upload(self): init_data = { 'name': self._file_name, 'part_size': self._part_size, 'size': self._file_size } if self._project: init_data['project'] = self._project elif self._parent: init_data['parent'] = self._parent init_params = {} if self._overwrite: init_params['overwrite'] = self._overwrite try: response = self._api.post( self._URL['upload_init'], data=init_data, params=init_params ) self._upload_id = response.json()['upload_id'] except SbgError as e: self._status = TransferState.FAILED raise SbgError( 'Unable to initialize upload! Failed to get upload id! ' 'Reason: {}'.format(e.message) )
[ "def", "_initialize_upload", "(", "self", ")", ":", "init_data", "=", "{", "'name'", ":", "self", ".", "_file_name", ",", "'part_size'", ":", "self", ".", "_part_size", ",", "'size'", ":", "self", ".", "_file_size", "}", "if", "self", ".", "_project", ":...
Initialized the upload on the API server by submitting the information about the project, the file name, file size and the part size that is going to be used during multipart upload.
[ "Initialized", "the", "upload", "on", "the", "API", "server", "by", "submitting", "the", "information", "about", "the", "project", "the", "file", "name", "file", "size", "and", "the", "part", "size", "that", "is", "going", "to", "be", "used", "during", "mu...
f62640d1018d959f0b686f2dbe5e183085336607
https://github.com/sbg/sevenbridges-python/blob/f62640d1018d959f0b686f2dbe5e183085336607/sevenbridges/transfer/upload.py#L329-L361
14,952
sbg/sevenbridges-python
sevenbridges/transfer/upload.py
Upload._finalize_upload
def _finalize_upload(self): """ Finalizes the upload on the API server. """ from sevenbridges.models.file import File try: response = self._api.post( self._URL['upload_complete'].format(upload_id=self._upload_id) ).json() self._result = File(api=self._api, **response) self._status = TransferState.COMPLETED except SbgError as e: self._status = TransferState.FAILED raise SbgError( 'Failed to complete upload! Reason: {}'.format(e.message) )
python
def _finalize_upload(self): from sevenbridges.models.file import File try: response = self._api.post( self._URL['upload_complete'].format(upload_id=self._upload_id) ).json() self._result = File(api=self._api, **response) self._status = TransferState.COMPLETED except SbgError as e: self._status = TransferState.FAILED raise SbgError( 'Failed to complete upload! Reason: {}'.format(e.message) )
[ "def", "_finalize_upload", "(", "self", ")", ":", "from", "sevenbridges", ".", "models", ".", "file", "import", "File", "try", ":", "response", "=", "self", ".", "_api", ".", "post", "(", "self", ".", "_URL", "[", "'upload_complete'", "]", ".", "format",...
Finalizes the upload on the API server.
[ "Finalizes", "the", "upload", "on", "the", "API", "server", "." ]
f62640d1018d959f0b686f2dbe5e183085336607
https://github.com/sbg/sevenbridges-python/blob/f62640d1018d959f0b686f2dbe5e183085336607/sevenbridges/transfer/upload.py#L363-L379
14,953
sbg/sevenbridges-python
sevenbridges/transfer/upload.py
Upload._abort_upload
def _abort_upload(self): """ Aborts the upload on the API server. """ try: self._api.delete( self._URL['upload_info'].format(upload_id=self._upload_id) ) except SbgError as e: self._status = TransferState.FAILED raise SbgError( 'Failed to abort upload! Reason: {}'.format(e.message) )
python
def _abort_upload(self): try: self._api.delete( self._URL['upload_info'].format(upload_id=self._upload_id) ) except SbgError as e: self._status = TransferState.FAILED raise SbgError( 'Failed to abort upload! Reason: {}'.format(e.message) )
[ "def", "_abort_upload", "(", "self", ")", ":", "try", ":", "self", ".", "_api", ".", "delete", "(", "self", ".", "_URL", "[", "'upload_info'", "]", ".", "format", "(", "upload_id", "=", "self", ".", "_upload_id", ")", ")", "except", "SbgError", "as", ...
Aborts the upload on the API server.
[ "Aborts", "the", "upload", "on", "the", "API", "server", "." ]
f62640d1018d959f0b686f2dbe5e183085336607
https://github.com/sbg/sevenbridges-python/blob/f62640d1018d959f0b686f2dbe5e183085336607/sevenbridges/transfer/upload.py#L381-L393
14,954
sbg/sevenbridges-python
sevenbridges/transfer/upload.py
Upload.add_callback
def add_callback(self, callback=None, errorback=None): """ Adds a callback that will be called when the upload finishes successfully or when error is raised. """ self._callback = callback self._errorback = errorback
python
def add_callback(self, callback=None, errorback=None): self._callback = callback self._errorback = errorback
[ "def", "add_callback", "(", "self", ",", "callback", "=", "None", ",", "errorback", "=", "None", ")", ":", "self", ".", "_callback", "=", "callback", "self", ".", "_errorback", "=", "errorback" ]
Adds a callback that will be called when the upload finishes successfully or when error is raised.
[ "Adds", "a", "callback", "that", "will", "be", "called", "when", "the", "upload", "finishes", "successfully", "or", "when", "error", "is", "raised", "." ]
f62640d1018d959f0b686f2dbe5e183085336607
https://github.com/sbg/sevenbridges-python/blob/f62640d1018d959f0b686f2dbe5e183085336607/sevenbridges/transfer/upload.py#L415-L421
14,955
indico/indico-plugins
livesync/indico_livesync/simplify.py
process_records
def process_records(records): """Converts queue entries into object changes. :param records: an iterable containing `LiveSyncQueueEntry` objects :return: a dict mapping object references to `SimpleChange` bitsets """ changes = defaultdict(int) cascaded_update_records = set() cascaded_delete_records = set() for record in records: if record.change != ChangeType.deleted and record.object is None: # Skip entries which are not deletions but have no corresponding objects. # Probably they are updates for objects that got deleted afterwards. continue if record.change == ChangeType.created: assert record.type != EntryType.category changes[record.object] |= SimpleChange.created elif record.change == ChangeType.deleted: assert record.type != EntryType.category cascaded_delete_records.add(record) elif record.change in {ChangeType.moved, ChangeType.protection_changed}: cascaded_update_records.add(record) elif record.change == ChangeType.data_changed: assert record.type != EntryType.category changes[record.object] |= SimpleChange.updated for obj in _process_cascaded_category_contents(cascaded_update_records): changes[obj] |= SimpleChange.updated for obj in _process_cascaded_event_contents(cascaded_delete_records): changes[obj] |= SimpleChange.deleted return changes
python
def process_records(records): changes = defaultdict(int) cascaded_update_records = set() cascaded_delete_records = set() for record in records: if record.change != ChangeType.deleted and record.object is None: # Skip entries which are not deletions but have no corresponding objects. # Probably they are updates for objects that got deleted afterwards. continue if record.change == ChangeType.created: assert record.type != EntryType.category changes[record.object] |= SimpleChange.created elif record.change == ChangeType.deleted: assert record.type != EntryType.category cascaded_delete_records.add(record) elif record.change in {ChangeType.moved, ChangeType.protection_changed}: cascaded_update_records.add(record) elif record.change == ChangeType.data_changed: assert record.type != EntryType.category changes[record.object] |= SimpleChange.updated for obj in _process_cascaded_category_contents(cascaded_update_records): changes[obj] |= SimpleChange.updated for obj in _process_cascaded_event_contents(cascaded_delete_records): changes[obj] |= SimpleChange.deleted return changes
[ "def", "process_records", "(", "records", ")", ":", "changes", "=", "defaultdict", "(", "int", ")", "cascaded_update_records", "=", "set", "(", ")", "cascaded_delete_records", "=", "set", "(", ")", "for", "record", "in", "records", ":", "if", "record", ".", ...
Converts queue entries into object changes. :param records: an iterable containing `LiveSyncQueueEntry` objects :return: a dict mapping object references to `SimpleChange` bitsets
[ "Converts", "queue", "entries", "into", "object", "changes", "." ]
fe50085cc63be9b8161b09539e662e7b04e4b38e
https://github.com/indico/indico-plugins/blob/fe50085cc63be9b8161b09539e662e7b04e4b38e/livesync/indico_livesync/simplify.py#L40-L73
14,956
indico/indico-plugins
livesync/indico_livesync/simplify.py
_process_cascaded_category_contents
def _process_cascaded_category_contents(records): """ Travel from categories to subcontributions, flattening the whole event structure. Yields everything that it finds (except for elements whose protection has changed but are not inheriting their protection settings from anywhere). :param records: queue records to process """ category_prot_records = {rec.category_id for rec in records if rec.type == EntryType.category and rec.change == ChangeType.protection_changed} category_move_records = {rec.category_id for rec in records if rec.type == EntryType.category and rec.change == ChangeType.moved} changed_events = set() category_prot_records -= category_move_records # A move already implies sending the whole record # Protection changes are handled differently, as there may not be the need to re-generate the record if category_prot_records: for categ in Category.find(Category.id.in_(category_prot_records)): cte = categ.get_protection_parent_cte() # Update only children that inherit inheriting_categ_children = (Event.query .join(cte, db.and_((Event.category_id == cte.c.id), (cte.c.protection_parent == categ.id)))) inheriting_direct_children = Event.find((Event.category_id == categ.id) & Event.is_inheriting) changed_events.update(itertools.chain(inheriting_direct_children, inheriting_categ_children)) # Add move operations and explicitly-passed event records if category_move_records: changed_events.update(Event.find(Event.category_chain_overlaps(category_move_records))) for elem in _process_cascaded_event_contents(records, additional_events=changed_events): yield elem
python
def _process_cascaded_category_contents(records): category_prot_records = {rec.category_id for rec in records if rec.type == EntryType.category and rec.change == ChangeType.protection_changed} category_move_records = {rec.category_id for rec in records if rec.type == EntryType.category and rec.change == ChangeType.moved} changed_events = set() category_prot_records -= category_move_records # A move already implies sending the whole record # Protection changes are handled differently, as there may not be the need to re-generate the record if category_prot_records: for categ in Category.find(Category.id.in_(category_prot_records)): cte = categ.get_protection_parent_cte() # Update only children that inherit inheriting_categ_children = (Event.query .join(cte, db.and_((Event.category_id == cte.c.id), (cte.c.protection_parent == categ.id)))) inheriting_direct_children = Event.find((Event.category_id == categ.id) & Event.is_inheriting) changed_events.update(itertools.chain(inheriting_direct_children, inheriting_categ_children)) # Add move operations and explicitly-passed event records if category_move_records: changed_events.update(Event.find(Event.category_chain_overlaps(category_move_records))) for elem in _process_cascaded_event_contents(records, additional_events=changed_events): yield elem
[ "def", "_process_cascaded_category_contents", "(", "records", ")", ":", "category_prot_records", "=", "{", "rec", ".", "category_id", "for", "rec", "in", "records", "if", "rec", ".", "type", "==", "EntryType", ".", "category", "and", "rec", ".", "change", "=="...
Travel from categories to subcontributions, flattening the whole event structure. Yields everything that it finds (except for elements whose protection has changed but are not inheriting their protection settings from anywhere). :param records: queue records to process
[ "Travel", "from", "categories", "to", "subcontributions", "flattening", "the", "whole", "event", "structure", "." ]
fe50085cc63be9b8161b09539e662e7b04e4b38e
https://github.com/indico/indico-plugins/blob/fe50085cc63be9b8161b09539e662e7b04e4b38e/livesync/indico_livesync/simplify.py#L76-L111
14,957
indico/indico-plugins
vc_vidyo/indico_vc_vidyo/plugin.py
VidyoPlugin.create_room
def create_room(self, vc_room, event): """Create a new Vidyo room for an event, given a VC room. In order to create the Vidyo room, the function will try to do so with all the available identities of the user based on the authenticators defined in Vidyo plugin's settings, in that order. :param vc_room: VCRoom -- The VC room from which to create the Vidyo room :param event: Event -- The event to the Vidyo room will be attached """ client = AdminClient(self.settings) owner = retrieve_principal(vc_room.data['owner']) login_gen = iter_user_identities(owner) login = next(login_gen, None) if login is None: raise VCRoomError(_("No valid Vidyo account found for this user"), field='owner_user') extension_gen = iter_extensions(self.settings.get('indico_room_prefix'), event.id) extension = next(extension_gen) while True: room_mode = { 'isLocked': False, 'hasPIN': bool(vc_room.data['room_pin']), 'hasModeratorPIN': bool(vc_room.data['moderation_pin']) } if room_mode['hasPIN']: room_mode['roomPIN'] = vc_room.data['room_pin'] if room_mode['hasModeratorPIN']: room_mode['moderatorPIN'] = vc_room.data['moderation_pin'] room_obj = client.create_room_object( name=vc_room.name, RoomType='Public', ownerName=login, extension=extension, groupName=self.settings.get('room_group_name'), description=vc_room.data['description'], RoomMode=room_mode) if room_obj.RoomMode.hasPIN: room_obj.RoomMode.roomPIN = vc_room.data['room_pin'] if room_obj.RoomMode.hasModeratorPIN: room_obj.RoomMode.moderatorPIN = vc_room.data['moderation_pin'] try: client.add_room(room_obj) except APIException as err: err_msg = err.message if err_msg.startswith('Room exist for name'): raise VCRoomError(_("Room name already in use"), field='name') elif err_msg.startswith('Member not found for ownerName'): login = next(login_gen, None) if login is None: raise VCRoomError(_("No valid Vidyo account found for this user"), field='owner_user') elif err_msg.startswith('Room exist for extension'): extension = next(extension_gen) else: raise else: # get room back, in order to fetch Vidyo-set parameters created_room = client.find_room(extension) if not created_room: raise VCRoomNotFoundError(_("Could not find newly created room in Vidyo")) vc_room.data.update({ 'vidyo_id': unicode(created_room.roomID), 'url': created_room.RoomMode.roomURL, 'owner_identity': created_room.ownerName }) flag_modified(vc_room, 'data') vc_room.vidyo_extension = VidyoExtension(vc_room_id=vc_room.id, extension=int(created_room.extension), owned_by_user=owner) client.set_automute(created_room.roomID, vc_room.data['auto_mute']) break
python
def create_room(self, vc_room, event): client = AdminClient(self.settings) owner = retrieve_principal(vc_room.data['owner']) login_gen = iter_user_identities(owner) login = next(login_gen, None) if login is None: raise VCRoomError(_("No valid Vidyo account found for this user"), field='owner_user') extension_gen = iter_extensions(self.settings.get('indico_room_prefix'), event.id) extension = next(extension_gen) while True: room_mode = { 'isLocked': False, 'hasPIN': bool(vc_room.data['room_pin']), 'hasModeratorPIN': bool(vc_room.data['moderation_pin']) } if room_mode['hasPIN']: room_mode['roomPIN'] = vc_room.data['room_pin'] if room_mode['hasModeratorPIN']: room_mode['moderatorPIN'] = vc_room.data['moderation_pin'] room_obj = client.create_room_object( name=vc_room.name, RoomType='Public', ownerName=login, extension=extension, groupName=self.settings.get('room_group_name'), description=vc_room.data['description'], RoomMode=room_mode) if room_obj.RoomMode.hasPIN: room_obj.RoomMode.roomPIN = vc_room.data['room_pin'] if room_obj.RoomMode.hasModeratorPIN: room_obj.RoomMode.moderatorPIN = vc_room.data['moderation_pin'] try: client.add_room(room_obj) except APIException as err: err_msg = err.message if err_msg.startswith('Room exist for name'): raise VCRoomError(_("Room name already in use"), field='name') elif err_msg.startswith('Member not found for ownerName'): login = next(login_gen, None) if login is None: raise VCRoomError(_("No valid Vidyo account found for this user"), field='owner_user') elif err_msg.startswith('Room exist for extension'): extension = next(extension_gen) else: raise else: # get room back, in order to fetch Vidyo-set parameters created_room = client.find_room(extension) if not created_room: raise VCRoomNotFoundError(_("Could not find newly created room in Vidyo")) vc_room.data.update({ 'vidyo_id': unicode(created_room.roomID), 'url': created_room.RoomMode.roomURL, 'owner_identity': created_room.ownerName }) flag_modified(vc_room, 'data') vc_room.vidyo_extension = VidyoExtension(vc_room_id=vc_room.id, extension=int(created_room.extension), owned_by_user=owner) client.set_automute(created_room.roomID, vc_room.data['auto_mute']) break
[ "def", "create_room", "(", "self", ",", "vc_room", ",", "event", ")", ":", "client", "=", "AdminClient", "(", "self", ".", "settings", ")", "owner", "=", "retrieve_principal", "(", "vc_room", ".", "data", "[", "'owner'", "]", ")", "login_gen", "=", "iter...
Create a new Vidyo room for an event, given a VC room. In order to create the Vidyo room, the function will try to do so with all the available identities of the user based on the authenticators defined in Vidyo plugin's settings, in that order. :param vc_room: VCRoom -- The VC room from which to create the Vidyo room :param event: Event -- The event to the Vidyo room will be attached
[ "Create", "a", "new", "Vidyo", "room", "for", "an", "event", "given", "a", "VC", "room", "." ]
fe50085cc63be9b8161b09539e662e7b04e4b38e
https://github.com/indico/indico-plugins/blob/fe50085cc63be9b8161b09539e662e7b04e4b38e/vc_vidyo/indico_vc_vidyo/plugin.py#L146-L224
14,958
indico/indico-plugins
livesync/indico_livesync/models/queue.py
LiveSyncQueueEntry.object
def object(self): """Return the changed object.""" if self.type == EntryType.category: return self.category elif self.type == EntryType.event: return self.event elif self.type == EntryType.session: return self.session elif self.type == EntryType.contribution: return self.contribution elif self.type == EntryType.subcontribution: return self.subcontribution
python
def object(self): if self.type == EntryType.category: return self.category elif self.type == EntryType.event: return self.event elif self.type == EntryType.session: return self.session elif self.type == EntryType.contribution: return self.contribution elif self.type == EntryType.subcontribution: return self.subcontribution
[ "def", "object", "(", "self", ")", ":", "if", "self", ".", "type", "==", "EntryType", ".", "category", ":", "return", "self", ".", "category", "elif", "self", ".", "type", "==", "EntryType", ".", "event", ":", "return", "self", ".", "event", "elif", ...
Return the changed object.
[ "Return", "the", "changed", "object", "." ]
fe50085cc63be9b8161b09539e662e7b04e4b38e
https://github.com/indico/indico-plugins/blob/fe50085cc63be9b8161b09539e662e7b04e4b38e/livesync/indico_livesync/models/queue.py#L213-L224
14,959
indico/indico-plugins
livesync/indico_livesync/models/queue.py
LiveSyncQueueEntry.object_ref
def object_ref(self): """Return the reference of the changed object.""" return ImmutableDict(type=self.type, category_id=self.category_id, event_id=self.event_id, session_id=self.session_id, contrib_id=self.contrib_id, subcontrib_id=self.subcontrib_id)
python
def object_ref(self): return ImmutableDict(type=self.type, category_id=self.category_id, event_id=self.event_id, session_id=self.session_id, contrib_id=self.contrib_id, subcontrib_id=self.subcontrib_id)
[ "def", "object_ref", "(", "self", ")", ":", "return", "ImmutableDict", "(", "type", "=", "self", ".", "type", ",", "category_id", "=", "self", ".", "category_id", ",", "event_id", "=", "self", ".", "event_id", ",", "session_id", "=", "self", ".", "sessio...
Return the reference of the changed object.
[ "Return", "the", "reference", "of", "the", "changed", "object", "." ]
fe50085cc63be9b8161b09539e662e7b04e4b38e
https://github.com/indico/indico-plugins/blob/fe50085cc63be9b8161b09539e662e7b04e4b38e/livesync/indico_livesync/models/queue.py#L227-L230
14,960
indico/indico-plugins
livesync/indico_livesync/models/queue.py
LiveSyncQueueEntry.create
def create(cls, changes, ref, excluded_categories=set()): """Create a new change in all queues. :param changes: the change types, an iterable containing :class:`ChangeType` :param ref: the object reference (returned by `obj_ref`) of the changed object :param excluded_categories: set of categories (IDs) whose items will not be tracked """ ref = dict(ref) obj = obj_deref(ref) if isinstance(obj, Category): if any(c.id in excluded_categories for c in obj.chain_query): return else: event = obj if isinstance(obj, Event) else obj.event if event.category not in g.setdefault('livesync_excluded_categories_checked', {}): g.livesync_excluded_categories_checked[event.category] = excluded_categories & set(event.category_chain) if g.livesync_excluded_categories_checked[event.category]: return try: agents = g.livesync_agents except AttributeError: agents = g.livesync_agents = LiveSyncAgent.query.all() for change in changes: for agent in agents: entry = cls(agent=agent, change=change, **ref) db.session.add(entry) db.session.flush()
python
def create(cls, changes, ref, excluded_categories=set()): ref = dict(ref) obj = obj_deref(ref) if isinstance(obj, Category): if any(c.id in excluded_categories for c in obj.chain_query): return else: event = obj if isinstance(obj, Event) else obj.event if event.category not in g.setdefault('livesync_excluded_categories_checked', {}): g.livesync_excluded_categories_checked[event.category] = excluded_categories & set(event.category_chain) if g.livesync_excluded_categories_checked[event.category]: return try: agents = g.livesync_agents except AttributeError: agents = g.livesync_agents = LiveSyncAgent.query.all() for change in changes: for agent in agents: entry = cls(agent=agent, change=change, **ref) db.session.add(entry) db.session.flush()
[ "def", "create", "(", "cls", ",", "changes", ",", "ref", ",", "excluded_categories", "=", "set", "(", ")", ")", ":", "ref", "=", "dict", "(", "ref", ")", "obj", "=", "obj_deref", "(", "ref", ")", "if", "isinstance", "(", "obj", ",", "Category", ")"...
Create a new change in all queues. :param changes: the change types, an iterable containing :class:`ChangeType` :param ref: the object reference (returned by `obj_ref`) of the changed object :param excluded_categories: set of categories (IDs) whose items will not be tracked
[ "Create", "a", "new", "change", "in", "all", "queues", "." ]
fe50085cc63be9b8161b09539e662e7b04e4b38e
https://github.com/indico/indico-plugins/blob/fe50085cc63be9b8161b09539e662e7b04e4b38e/livesync/indico_livesync/models/queue.py#L238-L271
14,961
indico/indico-plugins
importer_invenio/indico_importer_invenio/connector.py
decompose_code
def decompose_code(code): """ Decomposes a MARC "code" into tag, ind1, ind2, subcode """ code = "%-6s" % code ind1 = code[3:4] if ind1 == " ": ind1 = "_" ind2 = code[4:5] if ind2 == " ": ind2 = "_" subcode = code[5:6] if subcode == " ": subcode = None return (code[0:3], ind1, ind2, subcode)
python
def decompose_code(code): code = "%-6s" % code ind1 = code[3:4] if ind1 == " ": ind1 = "_" ind2 = code[4:5] if ind2 == " ": ind2 = "_" subcode = code[5:6] if subcode == " ": subcode = None return (code[0:3], ind1, ind2, subcode)
[ "def", "decompose_code", "(", "code", ")", ":", "code", "=", "\"%-6s\"", "%", "code", "ind1", "=", "code", "[", "3", ":", "4", "]", "if", "ind1", "==", "\" \"", ":", "ind1", "=", "\"_\"", "ind2", "=", "code", "[", "4", ":", "5", "]", "if", "ind...
Decomposes a MARC "code" into tag, ind1, ind2, subcode
[ "Decomposes", "a", "MARC", "code", "into", "tag", "ind1", "ind2", "subcode" ]
fe50085cc63be9b8161b09539e662e7b04e4b38e
https://github.com/indico/indico-plugins/blob/fe50085cc63be9b8161b09539e662e7b04e4b38e/importer_invenio/indico_importer_invenio/connector.py#L642-L653
14,962
indico/indico-plugins
importer_invenio/indico_importer_invenio/connector.py
InvenioConnector._init_browser
def _init_browser(self): """ Ovveride this method with the appropriate way to prepare a logged in browser. """ self.browser = mechanize.Browser() self.browser.set_handle_robots(False) self.browser.open(self.server_url + "/youraccount/login") self.browser.select_form(nr=0) try: self.browser['nickname'] = self.user self.browser['password'] = self.password except: self.browser['p_un'] = self.user self.browser['p_pw'] = self.password # Set login_method to be writable self.browser.form.find_control('login_method').readonly = False self.browser['login_method'] = self.login_method self.browser.submit()
python
def _init_browser(self): self.browser = mechanize.Browser() self.browser.set_handle_robots(False) self.browser.open(self.server_url + "/youraccount/login") self.browser.select_form(nr=0) try: self.browser['nickname'] = self.user self.browser['password'] = self.password except: self.browser['p_un'] = self.user self.browser['p_pw'] = self.password # Set login_method to be writable self.browser.form.find_control('login_method').readonly = False self.browser['login_method'] = self.login_method self.browser.submit()
[ "def", "_init_browser", "(", "self", ")", ":", "self", ".", "browser", "=", "mechanize", ".", "Browser", "(", ")", "self", ".", "browser", ".", "set_handle_robots", "(", "False", ")", "self", ".", "browser", ".", "open", "(", "self", ".", "server_url", ...
Ovveride this method with the appropriate way to prepare a logged in browser.
[ "Ovveride", "this", "method", "with", "the", "appropriate", "way", "to", "prepare", "a", "logged", "in", "browser", "." ]
fe50085cc63be9b8161b09539e662e7b04e4b38e
https://github.com/indico/indico-plugins/blob/fe50085cc63be9b8161b09539e662e7b04e4b38e/importer_invenio/indico_importer_invenio/connector.py#L193-L211
14,963
indico/indico-plugins
importer_invenio/indico_importer_invenio/connector.py
InvenioConnector.get_record
def get_record(self, recid, read_cache=True): """ Returns the record with given recid """ if recid in self.cached_records or not read_cache: return self.cached_records[recid] else: return self.search(p="recid:" + str(recid))
python
def get_record(self, recid, read_cache=True): if recid in self.cached_records or not read_cache: return self.cached_records[recid] else: return self.search(p="recid:" + str(recid))
[ "def", "get_record", "(", "self", ",", "recid", ",", "read_cache", "=", "True", ")", ":", "if", "recid", "in", "self", ".", "cached_records", "or", "not", "read_cache", ":", "return", "self", ".", "cached_records", "[", "recid", "]", "else", ":", "return...
Returns the record with given recid
[ "Returns", "the", "record", "with", "given", "recid" ]
fe50085cc63be9b8161b09539e662e7b04e4b38e
https://github.com/indico/indico-plugins/blob/fe50085cc63be9b8161b09539e662e7b04e4b38e/importer_invenio/indico_importer_invenio/connector.py#L360-L367
14,964
indico/indico-plugins
importer_invenio/indico_importer_invenio/connector.py
InvenioConnector.upload_marcxml
def upload_marcxml(self, marcxml, mode): """ Uploads a record to the server Parameters: marcxml - *str* the XML to upload. mode - *str* the mode to use for the upload. "-i" insert new records "-r" replace existing records "-c" correct fields of records "-a" append fields to records "-ir" insert record or replace if it exists """ if mode not in ["-i", "-r", "-c", "-a", "-ir"]: raise NameError, "Incorrect mode " + str(mode) # Are we running locally? If so, submit directly if self.local: (code, marcxml_filepath) = tempfile.mkstemp(prefix="upload_%s" % \ time.strftime("%Y%m%d_%H%M%S_", time.localtime())) marcxml_file_d = os.fdopen(code, "w") marcxml_file_d.write(marcxml) marcxml_file_d.close() return task_low_level_submission("bibupload", "", mode, marcxml_filepath) else: params = urllib.urlencode({'file': marcxml, 'mode': mode}) ## We don't use self.browser as batchuploader is protected by IP opener = urllib2.build_opener() opener.addheaders = [('User-Agent', CFG_USER_AGENT)] return opener.open(self.server_url + "/batchuploader/robotupload", params,)
python
def upload_marcxml(self, marcxml, mode): if mode not in ["-i", "-r", "-c", "-a", "-ir"]: raise NameError, "Incorrect mode " + str(mode) # Are we running locally? If so, submit directly if self.local: (code, marcxml_filepath) = tempfile.mkstemp(prefix="upload_%s" % \ time.strftime("%Y%m%d_%H%M%S_", time.localtime())) marcxml_file_d = os.fdopen(code, "w") marcxml_file_d.write(marcxml) marcxml_file_d.close() return task_low_level_submission("bibupload", "", mode, marcxml_filepath) else: params = urllib.urlencode({'file': marcxml, 'mode': mode}) ## We don't use self.browser as batchuploader is protected by IP opener = urllib2.build_opener() opener.addheaders = [('User-Agent', CFG_USER_AGENT)] return opener.open(self.server_url + "/batchuploader/robotupload", params,)
[ "def", "upload_marcxml", "(", "self", ",", "marcxml", ",", "mode", ")", ":", "if", "mode", "not", "in", "[", "\"-i\"", ",", "\"-r\"", ",", "\"-c\"", ",", "\"-a\"", ",", "\"-ir\"", "]", ":", "raise", "NameError", ",", "\"Incorrect mode \"", "+", "str", ...
Uploads a record to the server Parameters: marcxml - *str* the XML to upload. mode - *str* the mode to use for the upload. "-i" insert new records "-r" replace existing records "-c" correct fields of records "-a" append fields to records "-ir" insert record or replace if it exists
[ "Uploads", "a", "record", "to", "the", "server" ]
fe50085cc63be9b8161b09539e662e7b04e4b38e
https://github.com/indico/indico-plugins/blob/fe50085cc63be9b8161b09539e662e7b04e4b38e/importer_invenio/indico_importer_invenio/connector.py#L369-L400
14,965
indico/indico-plugins
importer_invenio/indico_importer_invenio/connector.py
InvenioConnector._validate_server_url
def _validate_server_url(self): """Validates self.server_url""" try: request = requests.head(self.server_url) if request.status_code >= 400: raise InvenioConnectorServerError( "Unexpected status code '%d' accessing URL: %s" % (request.status_code, self.server_url)) except (InvalidSchema, MissingSchema) as err: raise InvenioConnectorServerError( "Bad schema, expecting http:// or https://:\n %s" % (err,)) except ConnectionError as err: raise InvenioConnectorServerError( "Couldn't establish connection to '%s':\n %s" % (self.server_url, err)) except InvalidURL as err: raise InvenioConnectorServerError( "Invalid URL '%s':\n %s" % (self.server_url, err)) except RequestException as err: raise InvenioConnectorServerError( "Unknown error connecting to '%s':\n %s" % (self.server_url, err))
python
def _validate_server_url(self): try: request = requests.head(self.server_url) if request.status_code >= 400: raise InvenioConnectorServerError( "Unexpected status code '%d' accessing URL: %s" % (request.status_code, self.server_url)) except (InvalidSchema, MissingSchema) as err: raise InvenioConnectorServerError( "Bad schema, expecting http:// or https://:\n %s" % (err,)) except ConnectionError as err: raise InvenioConnectorServerError( "Couldn't establish connection to '%s':\n %s" % (self.server_url, err)) except InvalidURL as err: raise InvenioConnectorServerError( "Invalid URL '%s':\n %s" % (self.server_url, err)) except RequestException as err: raise InvenioConnectorServerError( "Unknown error connecting to '%s':\n %s" % (self.server_url, err))
[ "def", "_validate_server_url", "(", "self", ")", ":", "try", ":", "request", "=", "requests", ".", "head", "(", "self", ".", "server_url", ")", "if", "request", ".", "status_code", ">=", "400", ":", "raise", "InvenioConnectorServerError", "(", "\"Unexpected st...
Validates self.server_url
[ "Validates", "self", ".", "server_url" ]
fe50085cc63be9b8161b09539e662e7b04e4b38e
https://github.com/indico/indico-plugins/blob/fe50085cc63be9b8161b09539e662e7b04e4b38e/importer_invenio/indico_importer_invenio/connector.py#L416-L438
14,966
indico/indico-plugins
piwik/indico_piwik/queries/metrics.py
PiwikQueryReportEventMetricReferrers.get_result
def get_result(self): """Perform the call and return a list of referrers""" result = get_json_from_remote_server(self.call) referrers = list(result) for referrer in referrers: referrer['sum_visit_length'] = stringify_seconds(referrer['sum_visit_length']) return sorted(referrers, key=itemgetter('nb_visits'), reverse=True)[0:10]
python
def get_result(self): result = get_json_from_remote_server(self.call) referrers = list(result) for referrer in referrers: referrer['sum_visit_length'] = stringify_seconds(referrer['sum_visit_length']) return sorted(referrers, key=itemgetter('nb_visits'), reverse=True)[0:10]
[ "def", "get_result", "(", "self", ")", ":", "result", "=", "get_json_from_remote_server", "(", "self", ".", "call", ")", "referrers", "=", "list", "(", "result", ")", "for", "referrer", "in", "referrers", ":", "referrer", "[", "'sum_visit_length'", "]", "=",...
Perform the call and return a list of referrers
[ "Perform", "the", "call", "and", "return", "a", "list", "of", "referrers" ]
fe50085cc63be9b8161b09539e662e7b04e4b38e
https://github.com/indico/indico-plugins/blob/fe50085cc63be9b8161b09539e662e7b04e4b38e/piwik/indico_piwik/queries/metrics.py#L89-L95
14,967
indico/indico-plugins
piwik/indico_piwik/queries/metrics.py
PiwikQueryReportEventMetricPeakDateAndVisitors.get_result
def get_result(self): """Perform the call and return the peak date and how many users""" result = get_json_from_remote_server(self.call) if result: date, value = max(result.iteritems(), key=itemgetter(1)) return {'date': date, 'users': value} else: return {'date': "No Data", 'users': 0}
python
def get_result(self): result = get_json_from_remote_server(self.call) if result: date, value = max(result.iteritems(), key=itemgetter(1)) return {'date': date, 'users': value} else: return {'date': "No Data", 'users': 0}
[ "def", "get_result", "(", "self", ")", ":", "result", "=", "get_json_from_remote_server", "(", "self", ".", "call", ")", "if", "result", ":", "date", ",", "value", "=", "max", "(", "result", ".", "iteritems", "(", ")", ",", "key", "=", "itemgetter", "(...
Perform the call and return the peak date and how many users
[ "Perform", "the", "call", "and", "return", "the", "peak", "date", "and", "how", "many", "users" ]
fe50085cc63be9b8161b09539e662e7b04e4b38e
https://github.com/indico/indico-plugins/blob/fe50085cc63be9b8161b09539e662e7b04e4b38e/piwik/indico_piwik/queries/metrics.py#L135-L142
14,968
indico/indico-plugins
piwik/indico_piwik/piwik.py
PiwikRequest.call
def call(self, default_response=None, **query_params): """Perform a query to the Piwik server and return the response. :param default_response: Return value in case the query fails :param query_params: Dictionary with the parameters of the query """ query_url = self.get_query_url(**query_params) return self._perform_call(query_url, default_response)
python
def call(self, default_response=None, **query_params): query_url = self.get_query_url(**query_params) return self._perform_call(query_url, default_response)
[ "def", "call", "(", "self", ",", "default_response", "=", "None", ",", "*", "*", "query_params", ")", ":", "query_url", "=", "self", ".", "get_query_url", "(", "*", "*", "query_params", ")", "return", "self", ".", "_perform_call", "(", "query_url", ",", ...
Perform a query to the Piwik server and return the response. :param default_response: Return value in case the query fails :param query_params: Dictionary with the parameters of the query
[ "Perform", "a", "query", "to", "the", "Piwik", "server", "and", "return", "the", "response", "." ]
fe50085cc63be9b8161b09539e662e7b04e4b38e
https://github.com/indico/indico-plugins/blob/fe50085cc63be9b8161b09539e662e7b04e4b38e/piwik/indico_piwik/piwik.py#L45-L52
14,969
indico/indico-plugins
piwik/indico_piwik/piwik.py
PiwikRequest.get_query
def get_query(self, query_params=None): """Return a query string""" if query_params is None: query_params = {} query = '' query_params['idSite'] = self.site_id if self.api_token is not None: query_params['token_auth'] = self.api_token for key, value in query_params.iteritems(): if isinstance(value, list): value = ','.join(value) query += '{}={}&'.format(str(key), str(value)) return query[:-1]
python
def get_query(self, query_params=None): if query_params is None: query_params = {} query = '' query_params['idSite'] = self.site_id if self.api_token is not None: query_params['token_auth'] = self.api_token for key, value in query_params.iteritems(): if isinstance(value, list): value = ','.join(value) query += '{}={}&'.format(str(key), str(value)) return query[:-1]
[ "def", "get_query", "(", "self", ",", "query_params", "=", "None", ")", ":", "if", "query_params", "is", "None", ":", "query_params", "=", "{", "}", "query", "=", "''", "query_params", "[", "'idSite'", "]", "=", "self", ".", "site_id", "if", "self", "....
Return a query string
[ "Return", "a", "query", "string" ]
fe50085cc63be9b8161b09539e662e7b04e4b38e
https://github.com/indico/indico-plugins/blob/fe50085cc63be9b8161b09539e662e7b04e4b38e/piwik/indico_piwik/piwik.py#L54-L66
14,970
indico/indico-plugins
piwik/indico_piwik/piwik.py
PiwikRequest._perform_call
def _perform_call(self, query_url, default_response=None, timeout=10): """Returns the raw results from the API""" try: response = requests.get(query_url, timeout=timeout) except socket.timeout: current_plugin.logger.warning("Timeout contacting Piwik server") return default_response except Exception: current_plugin.logger.exception("Unable to connect") return default_response return response.content
python
def _perform_call(self, query_url, default_response=None, timeout=10): try: response = requests.get(query_url, timeout=timeout) except socket.timeout: current_plugin.logger.warning("Timeout contacting Piwik server") return default_response except Exception: current_plugin.logger.exception("Unable to connect") return default_response return response.content
[ "def", "_perform_call", "(", "self", ",", "query_url", ",", "default_response", "=", "None", ",", "timeout", "=", "10", ")", ":", "try", ":", "response", "=", "requests", ".", "get", "(", "query_url", ",", "timeout", "=", "timeout", ")", "except", "socke...
Returns the raw results from the API
[ "Returns", "the", "raw", "results", "from", "the", "API" ]
fe50085cc63be9b8161b09539e662e7b04e4b38e
https://github.com/indico/indico-plugins/blob/fe50085cc63be9b8161b09539e662e7b04e4b38e/piwik/indico_piwik/piwik.py#L72-L82
14,971
indico/indico-plugins
livesync/indico_livesync/uploader.py
Uploader.run
def run(self, records): """Runs the batch upload :param records: an iterable containing queue entries """ self_name = type(self).__name__ for i, batch in enumerate(grouper(records, self.BATCH_SIZE, skip_missing=True), 1): self.logger.info('%s processing batch %d', self_name, i) try: for j, proc_batch in enumerate(grouper( process_records(batch).iteritems(), self.BATCH_SIZE, skip_missing=True), 1): self.logger.info('%s uploading chunk #%d (batch %d)', self_name, j, i) self.upload_records({k: v for k, v in proc_batch}, from_queue=True) except Exception: self.logger.exception('%s could not upload batch', self_name) return self.logger.info('%s finished batch %d', self_name, i) self.processed_records(batch) self.logger.info('%s finished', self_name)
python
def run(self, records): self_name = type(self).__name__ for i, batch in enumerate(grouper(records, self.BATCH_SIZE, skip_missing=True), 1): self.logger.info('%s processing batch %d', self_name, i) try: for j, proc_batch in enumerate(grouper( process_records(batch).iteritems(), self.BATCH_SIZE, skip_missing=True), 1): self.logger.info('%s uploading chunk #%d (batch %d)', self_name, j, i) self.upload_records({k: v for k, v in proc_batch}, from_queue=True) except Exception: self.logger.exception('%s could not upload batch', self_name) return self.logger.info('%s finished batch %d', self_name, i) self.processed_records(batch) self.logger.info('%s finished', self_name)
[ "def", "run", "(", "self", ",", "records", ")", ":", "self_name", "=", "type", "(", "self", ")", ".", "__name__", "for", "i", ",", "batch", "in", "enumerate", "(", "grouper", "(", "records", ",", "self", ".", "BATCH_SIZE", ",", "skip_missing", "=", "...
Runs the batch upload :param records: an iterable containing queue entries
[ "Runs", "the", "batch", "upload" ]
fe50085cc63be9b8161b09539e662e7b04e4b38e
https://github.com/indico/indico-plugins/blob/fe50085cc63be9b8161b09539e662e7b04e4b38e/livesync/indico_livesync/uploader.py#L37-L55
14,972
indico/indico-plugins
livesync/indico_livesync/uploader.py
Uploader.run_initial
def run_initial(self, events): """Runs the initial batch upload :param events: an iterable containing events """ self_name = type(self).__name__ for i, batch in enumerate(grouper(events, self.INITIAL_BATCH_SIZE, skip_missing=True), 1): self.logger.debug('%s processing initial batch %d', self_name, i) for j, processed_batch in enumerate(grouper( batch, self.BATCH_SIZE, skip_missing=True), 1): self.logger.info('%s uploading initial chunk #%d (batch %d)', self_name, j, i) self.upload_records(processed_batch, from_queue=False)
python
def run_initial(self, events): self_name = type(self).__name__ for i, batch in enumerate(grouper(events, self.INITIAL_BATCH_SIZE, skip_missing=True), 1): self.logger.debug('%s processing initial batch %d', self_name, i) for j, processed_batch in enumerate(grouper( batch, self.BATCH_SIZE, skip_missing=True), 1): self.logger.info('%s uploading initial chunk #%d (batch %d)', self_name, j, i) self.upload_records(processed_batch, from_queue=False)
[ "def", "run_initial", "(", "self", ",", "events", ")", ":", "self_name", "=", "type", "(", "self", ")", ".", "__name__", "for", "i", ",", "batch", "in", "enumerate", "(", "grouper", "(", "events", ",", "self", ".", "INITIAL_BATCH_SIZE", ",", "skip_missin...
Runs the initial batch upload :param events: an iterable containing events
[ "Runs", "the", "initial", "batch", "upload" ]
fe50085cc63be9b8161b09539e662e7b04e4b38e
https://github.com/indico/indico-plugins/blob/fe50085cc63be9b8161b09539e662e7b04e4b38e/livesync/indico_livesync/uploader.py#L57-L69
14,973
indico/indico-plugins
livesync/indico_livesync/uploader.py
Uploader.processed_records
def processed_records(self, records): """Executed after successfully uploading a batch of records from the queue. :param records: a list of queue entries """ for record in records: self.logger.debug('Marking as processed: %s', record) record.processed = True db.session.commit()
python
def processed_records(self, records): for record in records: self.logger.debug('Marking as processed: %s', record) record.processed = True db.session.commit()
[ "def", "processed_records", "(", "self", ",", "records", ")", ":", "for", "record", "in", "records", ":", "self", ".", "logger", ".", "debug", "(", "'Marking as processed: %s'", ",", "record", ")", "record", ".", "processed", "=", "True", "db", ".", "sessi...
Executed after successfully uploading a batch of records from the queue. :param records: a list of queue entries
[ "Executed", "after", "successfully", "uploading", "a", "batch", "of", "records", "from", "the", "queue", "." ]
fe50085cc63be9b8161b09539e662e7b04e4b38e
https://github.com/indico/indico-plugins/blob/fe50085cc63be9b8161b09539e662e7b04e4b38e/livesync/indico_livesync/uploader.py#L80-L88
14,974
indico/indico-plugins
storage_s3/indico_storage_s3/migrate.py
cli
def cli(): """Migrate data to S3. Use the `copy` subcommand to copy data to S3. This can be done safely while Indico is running. At the end it will show you what you need to add to your `indico.conf`. Once you updated your config with the new storage backends, you can use the `apply` subcommand to update your database so files will actually be loaded using the new S3 storage backends. In case you ever need to switch back to your previous storage, you can use `revert` to undo the database changes. """ if config.DB_LOG: click.secho('Warning: The database logger is currently enabled (DB_LOG = True).\n' 'This will slow down the migration. Unless you database is very small, please disable it.', fg='yellow') click.confirm('Continue anyway?', abort=True)
python
def cli(): if config.DB_LOG: click.secho('Warning: The database logger is currently enabled (DB_LOG = True).\n' 'This will slow down the migration. Unless you database is very small, please disable it.', fg='yellow') click.confirm('Continue anyway?', abort=True)
[ "def", "cli", "(", ")", ":", "if", "config", ".", "DB_LOG", ":", "click", ".", "secho", "(", "'Warning: The database logger is currently enabled (DB_LOG = True).\\n'", "'This will slow down the migration. Unless you database is very small, please disable it.'", ",", "fg", "=", ...
Migrate data to S3. Use the `copy` subcommand to copy data to S3. This can be done safely while Indico is running. At the end it will show you what you need to add to your `indico.conf`. Once you updated your config with the new storage backends, you can use the `apply` subcommand to update your database so files will actually be loaded using the new S3 storage backends. In case you ever need to switch back to your previous storage, you can use `revert` to undo the database changes.
[ "Migrate", "data", "to", "S3", "." ]
fe50085cc63be9b8161b09539e662e7b04e4b38e
https://github.com/indico/indico-plugins/blob/fe50085cc63be9b8161b09539e662e7b04e4b38e/storage_s3/indico_storage_s3/migrate.py#L385-L403
14,975
Kane610/deconz
pydeconz/__init__.py
DeconzSession.start
def start(self) -> None: """Connect websocket to deCONZ.""" if self.config: self.websocket = self.ws_client( self.loop, self.session, self.host, self.config.websocketport, self.async_session_handler) self.websocket.start() else: _LOGGER.error('No deCONZ config available')
python
def start(self) -> None: if self.config: self.websocket = self.ws_client( self.loop, self.session, self.host, self.config.websocketport, self.async_session_handler) self.websocket.start() else: _LOGGER.error('No deCONZ config available')
[ "def", "start", "(", "self", ")", "->", "None", ":", "if", "self", ".", "config", ":", "self", ".", "websocket", "=", "self", ".", "ws_client", "(", "self", ".", "loop", ",", "self", ".", "session", ",", "self", ".", "host", ",", "self", ".", "co...
Connect websocket to deCONZ.
[ "Connect", "websocket", "to", "deCONZ", "." ]
8a9498dbbc8c168d4a081173ad6c3b1e17fffdf6
https://github.com/Kane610/deconz/blob/8a9498dbbc8c168d4a081173ad6c3b1e17fffdf6/pydeconz/__init__.py#L38-L46
14,976
Kane610/deconz
pydeconz/__init__.py
DeconzSession.async_load_parameters
async def async_load_parameters(self) -> bool: """Load deCONZ parameters. Returns lists of indices of which devices was added. """ data = await self.async_get_state('') _LOGGER.debug(pformat(data)) config = data.get('config', {}) groups = data.get('groups', {}) lights = data.get('lights', {}) sensors = data.get('sensors', {}) if not self.config: self.config = DeconzConfig(config) # Update scene for existing groups for group_id, group in groups.items(): if group_id in self.groups: self.groups[group_id].async_add_scenes( group.get('scenes'), self.async_put_state) self.groups.update({ group_id: DeconzGroup(group_id, group, self.async_put_state) for group_id, group in groups.items() if group_id not in self.groups }) self.lights.update({ light_id: DeconzLight(light_id, light, self.async_put_state) for light_id, light in lights.items() if light_id not in self.lights }) self.update_group_color(self.lights.keys()) self.scenes.update({ group.id + '_' + scene.id: scene for group in self.groups.values() for scene in group.scenes.values() if group.id + '_' + scene.id not in self.scenes }) self.sensors.update({ sensor_id: create_sensor(sensor_id, sensor, self.async_put_state) for sensor_id, sensor in sensors.items() if supported_sensor(sensor) and sensor_id not in self.sensors })
python
async def async_load_parameters(self) -> bool: data = await self.async_get_state('') _LOGGER.debug(pformat(data)) config = data.get('config', {}) groups = data.get('groups', {}) lights = data.get('lights', {}) sensors = data.get('sensors', {}) if not self.config: self.config = DeconzConfig(config) # Update scene for existing groups for group_id, group in groups.items(): if group_id in self.groups: self.groups[group_id].async_add_scenes( group.get('scenes'), self.async_put_state) self.groups.update({ group_id: DeconzGroup(group_id, group, self.async_put_state) for group_id, group in groups.items() if group_id not in self.groups }) self.lights.update({ light_id: DeconzLight(light_id, light, self.async_put_state) for light_id, light in lights.items() if light_id not in self.lights }) self.update_group_color(self.lights.keys()) self.scenes.update({ group.id + '_' + scene.id: scene for group in self.groups.values() for scene in group.scenes.values() if group.id + '_' + scene.id not in self.scenes }) self.sensors.update({ sensor_id: create_sensor(sensor_id, sensor, self.async_put_state) for sensor_id, sensor in sensors.items() if supported_sensor(sensor) and sensor_id not in self.sensors })
[ "async", "def", "async_load_parameters", "(", "self", ")", "->", "bool", ":", "data", "=", "await", "self", ".", "async_get_state", "(", "''", ")", "_LOGGER", ".", "debug", "(", "pformat", "(", "data", ")", ")", "config", "=", "data", ".", "get", "(", ...
Load deCONZ parameters. Returns lists of indices of which devices was added.
[ "Load", "deCONZ", "parameters", "." ]
8a9498dbbc8c168d4a081173ad6c3b1e17fffdf6
https://github.com/Kane610/deconz/blob/8a9498dbbc8c168d4a081173ad6c3b1e17fffdf6/pydeconz/__init__.py#L54-L101
14,977
Kane610/deconz
pydeconz/__init__.py
DeconzSession.async_put_state
async def async_put_state(self, field: str, data: dict) -> dict: """Set state of object in deCONZ. Field is a string representing a specific device in deCONZ e.g. field='/lights/1/state'. Data is a json object with what data you want to alter e.g. data={'on': True}. See Dresden Elektroniks REST API documentation for details: http://dresden-elektronik.github.io/deconz-rest-doc/rest/ """ session = self.session.put url = self.api_url + field jsondata = json.dumps(data) response_dict = await async_request(session, url, data=jsondata) return response_dict
python
async def async_put_state(self, field: str, data: dict) -> dict: session = self.session.put url = self.api_url + field jsondata = json.dumps(data) response_dict = await async_request(session, url, data=jsondata) return response_dict
[ "async", "def", "async_put_state", "(", "self", ",", "field", ":", "str", ",", "data", ":", "dict", ")", "->", "dict", ":", "session", "=", "self", ".", "session", ".", "put", "url", "=", "self", ".", "api_url", "+", "field", "jsondata", "=", "json",...
Set state of object in deCONZ. Field is a string representing a specific device in deCONZ e.g. field='/lights/1/state'. Data is a json object with what data you want to alter e.g. data={'on': True}. See Dresden Elektroniks REST API documentation for details: http://dresden-elektronik.github.io/deconz-rest-doc/rest/
[ "Set", "state", "of", "object", "in", "deCONZ", "." ]
8a9498dbbc8c168d4a081173ad6c3b1e17fffdf6
https://github.com/Kane610/deconz/blob/8a9498dbbc8c168d4a081173ad6c3b1e17fffdf6/pydeconz/__init__.py#L103-L117
14,978
Kane610/deconz
pydeconz/__init__.py
DeconzSession.async_get_state
async def async_get_state(self, field: str) -> dict: """Get state of object in deCONZ. Field is a string representing an API endpoint or lower e.g. field='/lights'. See Dresden Elektroniks REST API documentation for details: http://dresden-elektronik.github.io/deconz-rest-doc/rest/ """ session = self.session.get url = self.api_url + field response_dict = await async_request(session, url) return response_dict
python
async def async_get_state(self, field: str) -> dict: session = self.session.get url = self.api_url + field response_dict = await async_request(session, url) return response_dict
[ "async", "def", "async_get_state", "(", "self", ",", "field", ":", "str", ")", "->", "dict", ":", "session", "=", "self", ".", "session", ".", "get", "url", "=", "self", ".", "api_url", "+", "field", "response_dict", "=", "await", "async_request", "(", ...
Get state of object in deCONZ. Field is a string representing an API endpoint or lower e.g. field='/lights'. See Dresden Elektroniks REST API documentation for details: http://dresden-elektronik.github.io/deconz-rest-doc/rest/
[ "Get", "state", "of", "object", "in", "deCONZ", "." ]
8a9498dbbc8c168d4a081173ad6c3b1e17fffdf6
https://github.com/Kane610/deconz/blob/8a9498dbbc8c168d4a081173ad6c3b1e17fffdf6/pydeconz/__init__.py#L119-L130
14,979
Kane610/deconz
pydeconz/__init__.py
DeconzSession.async_session_handler
def async_session_handler(self, signal: str) -> None: """Signalling from websocket. data - new data available for processing. state - network state has changed. """ if signal == 'data': self.async_event_handler(self.websocket.data) elif signal == 'state': if self.async_connection_status_callback: self.async_connection_status_callback( self.websocket.state == 'running')
python
def async_session_handler(self, signal: str) -> None: if signal == 'data': self.async_event_handler(self.websocket.data) elif signal == 'state': if self.async_connection_status_callback: self.async_connection_status_callback( self.websocket.state == 'running')
[ "def", "async_session_handler", "(", "self", ",", "signal", ":", "str", ")", "->", "None", ":", "if", "signal", "==", "'data'", ":", "self", ".", "async_event_handler", "(", "self", ".", "websocket", ".", "data", ")", "elif", "signal", "==", "'state'", "...
Signalling from websocket. data - new data available for processing. state - network state has changed.
[ "Signalling", "from", "websocket", "." ]
8a9498dbbc8c168d4a081173ad6c3b1e17fffdf6
https://github.com/Kane610/deconz/blob/8a9498dbbc8c168d4a081173ad6c3b1e17fffdf6/pydeconz/__init__.py#L132-L143
14,980
Kane610/deconz
pydeconz/__init__.py
DeconzSession.async_event_handler
def async_event_handler(self, event: dict) -> None: """Receive event from websocket and identifies where the event belong. { "t": "event", "e": "changed", "r": "sensors", "id": "12", "state": { "buttonevent": 2002 } } """ if event['e'] == 'added': if event['r'] == 'lights' and event['id'] not in self.lights: device_type = 'light' device = self.lights[event['id']] = DeconzLight( event['id'], event['light'], self.async_put_state) elif event['r'] == 'sensors' and event['id'] not in self.sensors: if supported_sensor(event['sensor']): device_type = 'sensor' device = self.sensors[event['id']] = create_sensor( event['id'], event['sensor'], self.async_put_state) else: _LOGGER.warning('Unsupported sensor %s', event) return else: _LOGGER.debug('Unsupported event %s', event) return if self.async_add_device_callback: self.async_add_device_callback(device_type, device) elif event['e'] == 'changed': if event['r'] == 'groups' and event['id'] in self.groups: self.groups[event['id']].async_update(event) elif event['r'] == 'lights' and event['id'] in self.lights: self.lights[event['id']].async_update(event) self.update_group_color([event['id']]) elif event['r'] == 'sensors' and event['id'] in self.sensors: self.sensors[event['id']].async_update(event) else: _LOGGER.debug('Unsupported event %s', event) elif event['e'] == 'deleted': _LOGGER.debug('Removed event %s', event) else: _LOGGER.debug('Unsupported event %s', event)
python
def async_event_handler(self, event: dict) -> None: if event['e'] == 'added': if event['r'] == 'lights' and event['id'] not in self.lights: device_type = 'light' device = self.lights[event['id']] = DeconzLight( event['id'], event['light'], self.async_put_state) elif event['r'] == 'sensors' and event['id'] not in self.sensors: if supported_sensor(event['sensor']): device_type = 'sensor' device = self.sensors[event['id']] = create_sensor( event['id'], event['sensor'], self.async_put_state) else: _LOGGER.warning('Unsupported sensor %s', event) return else: _LOGGER.debug('Unsupported event %s', event) return if self.async_add_device_callback: self.async_add_device_callback(device_type, device) elif event['e'] == 'changed': if event['r'] == 'groups' and event['id'] in self.groups: self.groups[event['id']].async_update(event) elif event['r'] == 'lights' and event['id'] in self.lights: self.lights[event['id']].async_update(event) self.update_group_color([event['id']]) elif event['r'] == 'sensors' and event['id'] in self.sensors: self.sensors[event['id']].async_update(event) else: _LOGGER.debug('Unsupported event %s', event) elif event['e'] == 'deleted': _LOGGER.debug('Removed event %s', event) else: _LOGGER.debug('Unsupported event %s', event)
[ "def", "async_event_handler", "(", "self", ",", "event", ":", "dict", ")", "->", "None", ":", "if", "event", "[", "'e'", "]", "==", "'added'", ":", "if", "event", "[", "'r'", "]", "==", "'lights'", "and", "event", "[", "'id'", "]", "not", "in", "se...
Receive event from websocket and identifies where the event belong. { "t": "event", "e": "changed", "r": "sensors", "id": "12", "state": { "buttonevent": 2002 } }
[ "Receive", "event", "from", "websocket", "and", "identifies", "where", "the", "event", "belong", "." ]
8a9498dbbc8c168d4a081173ad6c3b1e17fffdf6
https://github.com/Kane610/deconz/blob/8a9498dbbc8c168d4a081173ad6c3b1e17fffdf6/pydeconz/__init__.py#L145-L198
14,981
Kane610/deconz
pydeconz/__init__.py
DeconzSession.update_group_color
def update_group_color(self, lights: list) -> None: """Update group colors based on light states. deCONZ group updates don't contain any information about the current state of the lights in the group. This method updates the color properties of the group to the current color of the lights in the group. For groups where the lights have different colors the group color will only reflect the color of the latest changed light in the group. """ for group in self.groups.values(): # Skip group if there are no common light ids. if not any({*lights} & {*group.lights}): continue # More than one light means load_parameters called this method. # Then we take first best light to be available. light_ids = lights if len(light_ids) > 1: light_ids = group.lights for light_id in light_ids: if self.lights[light_id].reachable: group.update_color_state(self.lights[light_id]) break
python
def update_group_color(self, lights: list) -> None: for group in self.groups.values(): # Skip group if there are no common light ids. if not any({*lights} & {*group.lights}): continue # More than one light means load_parameters called this method. # Then we take first best light to be available. light_ids = lights if len(light_ids) > 1: light_ids = group.lights for light_id in light_ids: if self.lights[light_id].reachable: group.update_color_state(self.lights[light_id]) break
[ "def", "update_group_color", "(", "self", ",", "lights", ":", "list", ")", "->", "None", ":", "for", "group", "in", "self", ".", "groups", ".", "values", "(", ")", ":", "# Skip group if there are no common light ids.", "if", "not", "any", "(", "{", "*", "l...
Update group colors based on light states. deCONZ group updates don't contain any information about the current state of the lights in the group. This method updates the color properties of the group to the current color of the lights in the group. For groups where the lights have different colors the group color will only reflect the color of the latest changed light in the group.
[ "Update", "group", "colors", "based", "on", "light", "states", "." ]
8a9498dbbc8c168d4a081173ad6c3b1e17fffdf6
https://github.com/Kane610/deconz/blob/8a9498dbbc8c168d4a081173ad6c3b1e17fffdf6/pydeconz/__init__.py#L200-L225
14,982
Kane610/deconz
pydeconz/utils.py
async_get_api_key
async def async_get_api_key(session, host, port, username=None, password=None, **kwargs): """Get a new API key for devicetype.""" url = 'http://{host}:{port}/api'.format(host=host, port=str(port)) auth = None if username and password: auth = aiohttp.BasicAuth(username, password=password) data = b'{"devicetype": "pydeconz"}' response = await async_request(session.post, url, auth=auth, data=data) api_key = response[0]['success']['username'] _LOGGER.info("API key: %s", api_key) return api_key
python
async def async_get_api_key(session, host, port, username=None, password=None, **kwargs): url = 'http://{host}:{port}/api'.format(host=host, port=str(port)) auth = None if username and password: auth = aiohttp.BasicAuth(username, password=password) data = b'{"devicetype": "pydeconz"}' response = await async_request(session.post, url, auth=auth, data=data) api_key = response[0]['success']['username'] _LOGGER.info("API key: %s", api_key) return api_key
[ "async", "def", "async_get_api_key", "(", "session", ",", "host", ",", "port", ",", "username", "=", "None", ",", "password", "=", "None", ",", "*", "*", "kwargs", ")", ":", "url", "=", "'http://{host}:{port}/api'", ".", "format", "(", "host", "=", "host...
Get a new API key for devicetype.
[ "Get", "a", "new", "API", "key", "for", "devicetype", "." ]
8a9498dbbc8c168d4a081173ad6c3b1e17fffdf6
https://github.com/Kane610/deconz/blob/8a9498dbbc8c168d4a081173ad6c3b1e17fffdf6/pydeconz/utils.py#L14-L27
14,983
Kane610/deconz
pydeconz/utils.py
async_delete_api_key
async def async_delete_api_key(session, host, port, api_key): """Delete API key from deCONZ.""" url = 'http://{host}:{port}/api/{api_key}/config/whitelist/{api_key}'.format( host=host, port=str(port), api_key=api_key) response = await async_request(session.delete, url) _LOGGER.info(response)
python
async def async_delete_api_key(session, host, port, api_key): url = 'http://{host}:{port}/api/{api_key}/config/whitelist/{api_key}'.format( host=host, port=str(port), api_key=api_key) response = await async_request(session.delete, url) _LOGGER.info(response)
[ "async", "def", "async_delete_api_key", "(", "session", ",", "host", ",", "port", ",", "api_key", ")", ":", "url", "=", "'http://{host}:{port}/api/{api_key}/config/whitelist/{api_key}'", ".", "format", "(", "host", "=", "host", ",", "port", "=", "str", "(", "por...
Delete API key from deCONZ.
[ "Delete", "API", "key", "from", "deCONZ", "." ]
8a9498dbbc8c168d4a081173ad6c3b1e17fffdf6
https://github.com/Kane610/deconz/blob/8a9498dbbc8c168d4a081173ad6c3b1e17fffdf6/pydeconz/utils.py#L30-L37
14,984
Kane610/deconz
pydeconz/utils.py
async_delete_all_keys
async def async_delete_all_keys(session, host, port, api_key, api_keys=[]): """Delete all API keys except for the ones provided to the method.""" url = 'http://{}:{}/api/{}/config'.format(host, str(port), api_key) response = await async_request(session.get, url) api_keys.append(api_key) for key in response['whitelist'].keys(): if key not in api_keys: await async_delete_api_key(session, host, port, key)
python
async def async_delete_all_keys(session, host, port, api_key, api_keys=[]): url = 'http://{}:{}/api/{}/config'.format(host, str(port), api_key) response = await async_request(session.get, url) api_keys.append(api_key) for key in response['whitelist'].keys(): if key not in api_keys: await async_delete_api_key(session, host, port, key)
[ "async", "def", "async_delete_all_keys", "(", "session", ",", "host", ",", "port", ",", "api_key", ",", "api_keys", "=", "[", "]", ")", ":", "url", "=", "'http://{}:{}/api/{}/config'", ".", "format", "(", "host", ",", "str", "(", "port", ")", ",", "api_k...
Delete all API keys except for the ones provided to the method.
[ "Delete", "all", "API", "keys", "except", "for", "the", "ones", "provided", "to", "the", "method", "." ]
8a9498dbbc8c168d4a081173ad6c3b1e17fffdf6
https://github.com/Kane610/deconz/blob/8a9498dbbc8c168d4a081173ad6c3b1e17fffdf6/pydeconz/utils.py#L40-L49
14,985
Kane610/deconz
pydeconz/utils.py
async_get_bridgeid
async def async_get_bridgeid(session, host, port, api_key, **kwargs): """Get bridge id for bridge.""" url = 'http://{}:{}/api/{}/config'.format(host, str(port), api_key) response = await async_request(session.get, url) bridgeid = response['bridgeid'] _LOGGER.info("Bridge id: %s", bridgeid) return bridgeid
python
async def async_get_bridgeid(session, host, port, api_key, **kwargs): url = 'http://{}:{}/api/{}/config'.format(host, str(port), api_key) response = await async_request(session.get, url) bridgeid = response['bridgeid'] _LOGGER.info("Bridge id: %s", bridgeid) return bridgeid
[ "async", "def", "async_get_bridgeid", "(", "session", ",", "host", ",", "port", ",", "api_key", ",", "*", "*", "kwargs", ")", ":", "url", "=", "'http://{}:{}/api/{}/config'", ".", "format", "(", "host", ",", "str", "(", "port", ")", ",", "api_key", ")", ...
Get bridge id for bridge.
[ "Get", "bridge", "id", "for", "bridge", "." ]
8a9498dbbc8c168d4a081173ad6c3b1e17fffdf6
https://github.com/Kane610/deconz/blob/8a9498dbbc8c168d4a081173ad6c3b1e17fffdf6/pydeconz/utils.py#L52-L60
14,986
Kane610/deconz
pydeconz/utils.py
async_discovery
async def async_discovery(session): """Find bridges allowing gateway discovery.""" bridges = [] response = await async_request(session.get, URL_DISCOVER) if not response: _LOGGER.info("No discoverable bridges available.") return bridges for bridge in response: bridges.append({'bridgeid': bridge['id'], 'host': bridge['internalipaddress'], 'port': bridge['internalport']}) _LOGGER.info("Discovered the following bridges: %s.", bridges) return bridges
python
async def async_discovery(session): bridges = [] response = await async_request(session.get, URL_DISCOVER) if not response: _LOGGER.info("No discoverable bridges available.") return bridges for bridge in response: bridges.append({'bridgeid': bridge['id'], 'host': bridge['internalipaddress'], 'port': bridge['internalport']}) _LOGGER.info("Discovered the following bridges: %s.", bridges) return bridges
[ "async", "def", "async_discovery", "(", "session", ")", ":", "bridges", "=", "[", "]", "response", "=", "await", "async_request", "(", "session", ".", "get", ",", "URL_DISCOVER", ")", "if", "not", "response", ":", "_LOGGER", ".", "info", "(", "\"No discove...
Find bridges allowing gateway discovery.
[ "Find", "bridges", "allowing", "gateway", "discovery", "." ]
8a9498dbbc8c168d4a081173ad6c3b1e17fffdf6
https://github.com/Kane610/deconz/blob/8a9498dbbc8c168d4a081173ad6c3b1e17fffdf6/pydeconz/utils.py#L63-L79
14,987
Kane610/deconz
pydeconz/websocket.py
AIOWSClient.retry
def retry(self): """Retry to connect to deCONZ.""" self.state = STATE_STARTING self.loop.call_later(RETRY_TIMER, self.start) _LOGGER.debug('Reconnecting to deCONZ in %i.', RETRY_TIMER)
python
def retry(self): self.state = STATE_STARTING self.loop.call_later(RETRY_TIMER, self.start) _LOGGER.debug('Reconnecting to deCONZ in %i.', RETRY_TIMER)
[ "def", "retry", "(", "self", ")", ":", "self", ".", "state", "=", "STATE_STARTING", "self", ".", "loop", ".", "call_later", "(", "RETRY_TIMER", ",", "self", ".", "start", ")", "_LOGGER", ".", "debug", "(", "'Reconnecting to deCONZ in %i.'", ",", "RETRY_TIMER...
Retry to connect to deCONZ.
[ "Retry", "to", "connect", "to", "deCONZ", "." ]
8a9498dbbc8c168d4a081173ad6c3b1e17fffdf6
https://github.com/Kane610/deconz/blob/8a9498dbbc8c168d4a081173ad6c3b1e17fffdf6/pydeconz/websocket.py#L88-L92
14,988
Kane610/deconz
pydeconz/websocket.py
WSClient.stop
def stop(self): """Close websocket connection.""" self.state = STATE_STOPPED if self.transport: self.transport.close()
python
def stop(self): self.state = STATE_STOPPED if self.transport: self.transport.close()
[ "def", "stop", "(", "self", ")", ":", "self", ".", "state", "=", "STATE_STOPPED", "if", "self", ".", "transport", ":", "self", ".", "transport", ".", "close", "(", ")" ]
Close websocket connection.
[ "Close", "websocket", "connection", "." ]
8a9498dbbc8c168d4a081173ad6c3b1e17fffdf6
https://github.com/Kane610/deconz/blob/8a9498dbbc8c168d4a081173ad6c3b1e17fffdf6/pydeconz/websocket.py#L146-L150
14,989
Kane610/deconz
pydeconz/websocket.py
WSClient.connection_made
def connection_made(self, transport): """Do the websocket handshake. According to https://tools.ietf.org/html/rfc6455 """ randomness = os.urandom(16) key = base64encode(randomness).decode('utf-8').strip() self.transport = transport message = "GET / HTTP/1.1\r\n" message += "Host: " + self.host + ':' + str(self.port) + '\r\n' message += "User-Agent: Python/3.5 websockets/3.4\r\n" message += "Upgrade: Websocket\r\n" message += "Connection: Upgrade\r\n" message += "Sec-WebSocket-Key: " + key + "\r\n" message += "Sec-WebSocket-Version: 13\r\n" message += "\r\n" _LOGGER.debug('Websocket handshake: %s', message) self.transport.write(message.encode())
python
def connection_made(self, transport): randomness = os.urandom(16) key = base64encode(randomness).decode('utf-8').strip() self.transport = transport message = "GET / HTTP/1.1\r\n" message += "Host: " + self.host + ':' + str(self.port) + '\r\n' message += "User-Agent: Python/3.5 websockets/3.4\r\n" message += "Upgrade: Websocket\r\n" message += "Connection: Upgrade\r\n" message += "Sec-WebSocket-Key: " + key + "\r\n" message += "Sec-WebSocket-Version: 13\r\n" message += "\r\n" _LOGGER.debug('Websocket handshake: %s', message) self.transport.write(message.encode())
[ "def", "connection_made", "(", "self", ",", "transport", ")", ":", "randomness", "=", "os", ".", "urandom", "(", "16", ")", "key", "=", "base64encode", "(", "randomness", ")", ".", "decode", "(", "'utf-8'", ")", ".", "strip", "(", ")", "self", ".", "...
Do the websocket handshake. According to https://tools.ietf.org/html/rfc6455
[ "Do", "the", "websocket", "handshake", "." ]
8a9498dbbc8c168d4a081173ad6c3b1e17fffdf6
https://github.com/Kane610/deconz/blob/8a9498dbbc8c168d4a081173ad6c3b1e17fffdf6/pydeconz/websocket.py#L157-L174
14,990
Kane610/deconz
pydeconz/websocket.py
WSClient.data_received
def data_received(self, data): """Data received over websocket. First received data will allways be handshake accepting connection. We need to check how big the header is so we can send event data as a proper json object. """ if self.state == STATE_STARTING: self.state = STATE_RUNNING _LOGGER.debug('Websocket handshake: %s', data.decode()) return _LOGGER.debug('Websocket data: %s', data) while len(data) > 0: payload, extra_data = self.get_payload(data) self._data = payload ### self.async_session_handler_callback('data')### #self.async_callback(payload) data = extra_data
python
def data_received(self, data): if self.state == STATE_STARTING: self.state = STATE_RUNNING _LOGGER.debug('Websocket handshake: %s', data.decode()) return _LOGGER.debug('Websocket data: %s', data) while len(data) > 0: payload, extra_data = self.get_payload(data) self._data = payload ### self.async_session_handler_callback('data')### #self.async_callback(payload) data = extra_data
[ "def", "data_received", "(", "self", ",", "data", ")", ":", "if", "self", ".", "state", "==", "STATE_STARTING", ":", "self", ".", "state", "=", "STATE_RUNNING", "_LOGGER", ".", "debug", "(", "'Websocket handshake: %s'", ",", "data", ".", "decode", "(", ")"...
Data received over websocket. First received data will allways be handshake accepting connection. We need to check how big the header is so we can send event data as a proper json object.
[ "Data", "received", "over", "websocket", "." ]
8a9498dbbc8c168d4a081173ad6c3b1e17fffdf6
https://github.com/Kane610/deconz/blob/8a9498dbbc8c168d4a081173ad6c3b1e17fffdf6/pydeconz/websocket.py#L176-L194
14,991
Kane610/deconz
pydeconz/websocket.py
WSClient.get_payload
def get_payload(self, data): """Parse length of payload and return it.""" start = 2 length = ord(data[1:2]) if length == 126: # Payload information are an extra 2 bytes. start = 4 length, = unpack(">H", data[2:4]) elif length == 127: # Payload information are an extra 6 bytes. start = 8 length, = unpack(">I", data[2:6]) end = start + length payload = json.loads(data[start:end].decode()) extra_data = data[end:] return payload, extra_data
python
def get_payload(self, data): start = 2 length = ord(data[1:2]) if length == 126: # Payload information are an extra 2 bytes. start = 4 length, = unpack(">H", data[2:4]) elif length == 127: # Payload information are an extra 6 bytes. start = 8 length, = unpack(">I", data[2:6]) end = start + length payload = json.loads(data[start:end].decode()) extra_data = data[end:] return payload, extra_data
[ "def", "get_payload", "(", "self", ",", "data", ")", ":", "start", "=", "2", "length", "=", "ord", "(", "data", "[", "1", ":", "2", "]", ")", "if", "length", "==", "126", ":", "# Payload information are an extra 2 bytes.", "start", "=", "4", "length", ...
Parse length of payload and return it.
[ "Parse", "length", "of", "payload", "and", "return", "it", "." ]
8a9498dbbc8c168d4a081173ad6c3b1e17fffdf6
https://github.com/Kane610/deconz/blob/8a9498dbbc8c168d4a081173ad6c3b1e17fffdf6/pydeconz/websocket.py#L202-L217
14,992
indico/indico-plugins
vc_vidyo/indico_vc_vidyo/task.py
notify_owner
def notify_owner(plugin, vc_room): """Notifies about the deletion of a Vidyo room from the Vidyo server.""" user = vc_room.vidyo_extension.owned_by_user tpl = get_plugin_template_module('emails/remote_deleted.html', plugin=plugin, vc_room=vc_room, event=None, vc_room_event=None, user=user) _send('delete', user, plugin, None, vc_room, tpl)
python
def notify_owner(plugin, vc_room): user = vc_room.vidyo_extension.owned_by_user tpl = get_plugin_template_module('emails/remote_deleted.html', plugin=plugin, vc_room=vc_room, event=None, vc_room_event=None, user=user) _send('delete', user, plugin, None, vc_room, tpl)
[ "def", "notify_owner", "(", "plugin", ",", "vc_room", ")", ":", "user", "=", "vc_room", ".", "vidyo_extension", ".", "owned_by_user", "tpl", "=", "get_plugin_template_module", "(", "'emails/remote_deleted.html'", ",", "plugin", "=", "plugin", ",", "vc_room", "=", ...
Notifies about the deletion of a Vidyo room from the Vidyo server.
[ "Notifies", "about", "the", "deletion", "of", "a", "Vidyo", "room", "from", "the", "Vidyo", "server", "." ]
fe50085cc63be9b8161b09539e662e7b04e4b38e
https://github.com/indico/indico-plugins/blob/fe50085cc63be9b8161b09539e662e7b04e4b38e/vc_vidyo/indico_vc_vidyo/task.py#L51-L56
14,993
indico/indico-plugins
vc_vidyo/indico_vc_vidyo/cli.py
rooms
def rooms(status=None): """Lists all Vidyo rooms""" room_query = VCRoom.find(type='vidyo') table_data = [['ID', 'Name', 'Status', 'Vidyo ID', 'Extension']] if status: room_query = room_query.filter(VCRoom.status == VCRoomStatus.get(status)) for room in room_query: table_data.append([unicode(room.id), room.name, room.status.name, unicode(room.data['vidyo_id']), unicode(room.vidyo_extension.extension)]) table = AsciiTable(table_data) for col in (0, 3, 4): table.justify_columns[col] = 'right' print table.table
python
def rooms(status=None): room_query = VCRoom.find(type='vidyo') table_data = [['ID', 'Name', 'Status', 'Vidyo ID', 'Extension']] if status: room_query = room_query.filter(VCRoom.status == VCRoomStatus.get(status)) for room in room_query: table_data.append([unicode(room.id), room.name, room.status.name, unicode(room.data['vidyo_id']), unicode(room.vidyo_extension.extension)]) table = AsciiTable(table_data) for col in (0, 3, 4): table.justify_columns[col] = 'right' print table.table
[ "def", "rooms", "(", "status", "=", "None", ")", ":", "room_query", "=", "VCRoom", ".", "find", "(", "type", "=", "'vidyo'", ")", "table_data", "=", "[", "[", "'ID'", ",", "'Name'", ",", "'Status'", ",", "'Vidyo ID'", ",", "'Extension'", "]", "]", "i...
Lists all Vidyo rooms
[ "Lists", "all", "Vidyo", "rooms" ]
fe50085cc63be9b8161b09539e662e7b04e4b38e
https://github.com/indico/indico-plugins/blob/fe50085cc63be9b8161b09539e662e7b04e4b38e/vc_vidyo/indico_vc_vidyo/cli.py#L33-L49
14,994
indico/indico-plugins
search/indico_search/util.py
render_engine_or_search_template
def render_engine_or_search_template(template_name, **context): """Renders a template from the engine plugin or the search plugin If the template is available in the engine plugin, it's taken from there, otherwise the template from this plugin is used. :param template_name: name of the template :param context: the variables that should be available in the context of the template. """ from indico_search.plugin import SearchPlugin assert current_plugin == SearchPlugin.instance templates = ('{}:{}'.format(SearchPlugin.instance.engine_plugin.name, template_name), template_name) return render_plugin_template(templates, **context)
python
def render_engine_or_search_template(template_name, **context): from indico_search.plugin import SearchPlugin assert current_plugin == SearchPlugin.instance templates = ('{}:{}'.format(SearchPlugin.instance.engine_plugin.name, template_name), template_name) return render_plugin_template(templates, **context)
[ "def", "render_engine_or_search_template", "(", "template_name", ",", "*", "*", "context", ")", ":", "from", "indico_search", ".", "plugin", "import", "SearchPlugin", "assert", "current_plugin", "==", "SearchPlugin", ".", "instance", "templates", "=", "(", "'{}:{}'"...
Renders a template from the engine plugin or the search plugin If the template is available in the engine plugin, it's taken from there, otherwise the template from this plugin is used. :param template_name: name of the template :param context: the variables that should be available in the context of the template.
[ "Renders", "a", "template", "from", "the", "engine", "plugin", "or", "the", "search", "plugin" ]
fe50085cc63be9b8161b09539e662e7b04e4b38e
https://github.com/indico/indico-plugins/blob/fe50085cc63be9b8161b09539e662e7b04e4b38e/search/indico_search/util.py#L22-L37
14,995
indico/indico-plugins
vc_vidyo/indico_vc_vidyo/util.py
iter_user_identities
def iter_user_identities(user): """Iterates over all existing user identities that can be used with Vidyo""" from indico_vc_vidyo.plugin import VidyoPlugin providers = authenticators_re.split(VidyoPlugin.settings.get('authenticators')) done = set() for provider in providers: for _, identifier in user.iter_identifiers(check_providers=True, providers={provider}): if identifier in done: continue done.add(identifier) yield identifier
python
def iter_user_identities(user): from indico_vc_vidyo.plugin import VidyoPlugin providers = authenticators_re.split(VidyoPlugin.settings.get('authenticators')) done = set() for provider in providers: for _, identifier in user.iter_identifiers(check_providers=True, providers={provider}): if identifier in done: continue done.add(identifier) yield identifier
[ "def", "iter_user_identities", "(", "user", ")", ":", "from", "indico_vc_vidyo", ".", "plugin", "import", "VidyoPlugin", "providers", "=", "authenticators_re", ".", "split", "(", "VidyoPlugin", ".", "settings", ".", "get", "(", "'authenticators'", ")", ")", "don...
Iterates over all existing user identities that can be used with Vidyo
[ "Iterates", "over", "all", "existing", "user", "identities", "that", "can", "be", "used", "with", "Vidyo" ]
fe50085cc63be9b8161b09539e662e7b04e4b38e
https://github.com/indico/indico-plugins/blob/fe50085cc63be9b8161b09539e662e7b04e4b38e/vc_vidyo/indico_vc_vidyo/util.py#L32-L42
14,996
indico/indico-plugins
vc_vidyo/indico_vc_vidyo/util.py
get_user_from_identifier
def get_user_from_identifier(settings, identifier): """Get an actual User object from an identifier""" providers = list(auth.strip() for auth in settings.get('authenticators').split(',')) identities = Identity.find_all(Identity.provider.in_(providers), Identity.identifier == identifier) if identities: return sorted(identities, key=lambda x: providers.index(x.provider))[0].user for provider in providers: try: identity_info = multipass.get_identity(provider, identifier) except IdentityRetrievalFailed: continue if identity_info is None: continue if not identity_info.provider.settings.get('trusted_email'): continue emails = {email.lower() for email in identity_info.data.getlist('email') if email} if not emails: continue user = User.find_first(~User.is_deleted, User.all_emails.in_(list(emails))) if user: return user
python
def get_user_from_identifier(settings, identifier): providers = list(auth.strip() for auth in settings.get('authenticators').split(',')) identities = Identity.find_all(Identity.provider.in_(providers), Identity.identifier == identifier) if identities: return sorted(identities, key=lambda x: providers.index(x.provider))[0].user for provider in providers: try: identity_info = multipass.get_identity(provider, identifier) except IdentityRetrievalFailed: continue if identity_info is None: continue if not identity_info.provider.settings.get('trusted_email'): continue emails = {email.lower() for email in identity_info.data.getlist('email') if email} if not emails: continue user = User.find_first(~User.is_deleted, User.all_emails.in_(list(emails))) if user: return user
[ "def", "get_user_from_identifier", "(", "settings", ",", "identifier", ")", ":", "providers", "=", "list", "(", "auth", ".", "strip", "(", ")", "for", "auth", "in", "settings", ".", "get", "(", "'authenticators'", ")", ".", "split", "(", "','", ")", ")",...
Get an actual User object from an identifier
[ "Get", "an", "actual", "User", "object", "from", "an", "identifier" ]
fe50085cc63be9b8161b09539e662e7b04e4b38e
https://github.com/indico/indico-plugins/blob/fe50085cc63be9b8161b09539e662e7b04e4b38e/vc_vidyo/indico_vc_vidyo/util.py#L45-L65
14,997
indico/indico-plugins
vc_vidyo/indico_vc_vidyo/util.py
update_room_from_obj
def update_room_from_obj(settings, vc_room, room_obj): """Updates a VCRoom DB object using a SOAP room object returned by the API""" vc_room.name = room_obj.name if room_obj.ownerName != vc_room.data['owner_identity']: owner = get_user_from_identifier(settings, room_obj.ownerName) or User.get_system_user() vc_room.vidyo_extension.owned_by_user = owner vc_room.data.update({ 'description': room_obj.description, 'vidyo_id': unicode(room_obj.roomID), 'url': room_obj.RoomMode.roomURL, 'owner_identity': room_obj.ownerName, 'room_pin': room_obj.RoomMode.roomPIN if room_obj.RoomMode.hasPIN else "", 'moderation_pin': room_obj.RoomMode.moderatorPIN if room_obj.RoomMode.hasModeratorPIN else "", }) vc_room.vidyo_extension.extension = int(room_obj.extension)
python
def update_room_from_obj(settings, vc_room, room_obj): vc_room.name = room_obj.name if room_obj.ownerName != vc_room.data['owner_identity']: owner = get_user_from_identifier(settings, room_obj.ownerName) or User.get_system_user() vc_room.vidyo_extension.owned_by_user = owner vc_room.data.update({ 'description': room_obj.description, 'vidyo_id': unicode(room_obj.roomID), 'url': room_obj.RoomMode.roomURL, 'owner_identity': room_obj.ownerName, 'room_pin': room_obj.RoomMode.roomPIN if room_obj.RoomMode.hasPIN else "", 'moderation_pin': room_obj.RoomMode.moderatorPIN if room_obj.RoomMode.hasModeratorPIN else "", }) vc_room.vidyo_extension.extension = int(room_obj.extension)
[ "def", "update_room_from_obj", "(", "settings", ",", "vc_room", ",", "room_obj", ")", ":", "vc_room", ".", "name", "=", "room_obj", ".", "name", "if", "room_obj", ".", "ownerName", "!=", "vc_room", ".", "data", "[", "'owner_identity'", "]", ":", "owner", "...
Updates a VCRoom DB object using a SOAP room object returned by the API
[ "Updates", "a", "VCRoom", "DB", "object", "using", "a", "SOAP", "room", "object", "returned", "by", "the", "API" ]
fe50085cc63be9b8161b09539e662e7b04e4b38e
https://github.com/indico/indico-plugins/blob/fe50085cc63be9b8161b09539e662e7b04e4b38e/vc_vidyo/indico_vc_vidyo/util.py#L80-L95
14,998
CygnusNetworks/pypureomapi
pypureomapi.py
pack_ip
def pack_ip(ipstr): """Converts an ip address given in dotted notation to a four byte string in network byte order. >>> len(pack_ip("127.0.0.1")) 4 >>> pack_ip("foo") Traceback (most recent call last): ... ValueError: given ip address has an invalid number of dots @type ipstr: str @rtype: bytes @raises ValueError: for badly formatted ip addresses """ if not isinstance(ipstr, basestring): raise ValueError("given ip address is not a string") parts = ipstr.split('.') if len(parts) != 4: raise ValueError("given ip address has an invalid number of dots") parts = [int(x) for x in parts] # raises ValueError return int_seq_to_bytes(parts)
python
def pack_ip(ipstr): if not isinstance(ipstr, basestring): raise ValueError("given ip address is not a string") parts = ipstr.split('.') if len(parts) != 4: raise ValueError("given ip address has an invalid number of dots") parts = [int(x) for x in parts] # raises ValueError return int_seq_to_bytes(parts)
[ "def", "pack_ip", "(", "ipstr", ")", ":", "if", "not", "isinstance", "(", "ipstr", ",", "basestring", ")", ":", "raise", "ValueError", "(", "\"given ip address is not a string\"", ")", "parts", "=", "ipstr", ".", "split", "(", "'.'", ")", "if", "len", "(",...
Converts an ip address given in dotted notation to a four byte string in network byte order. >>> len(pack_ip("127.0.0.1")) 4 >>> pack_ip("foo") Traceback (most recent call last): ... ValueError: given ip address has an invalid number of dots @type ipstr: str @rtype: bytes @raises ValueError: for badly formatted ip addresses
[ "Converts", "an", "ip", "address", "given", "in", "dotted", "notation", "to", "a", "four", "byte", "string", "in", "network", "byte", "order", "." ]
ff4459678ec023fd56e64ce518a86860efec26bf
https://github.com/CygnusNetworks/pypureomapi/blob/ff4459678ec023fd56e64ce518a86860efec26bf/pypureomapi.py#L724-L745
14,999
CygnusNetworks/pypureomapi
pypureomapi.py
unpack_ip
def unpack_ip(fourbytes): """Converts an ip address given in a four byte string in network byte order to a string in dotted notation. >>> unpack_ip(b"dead") '100.101.97.100' >>> unpack_ip(b"alive") Traceback (most recent call last): ... ValueError: given buffer is not exactly four bytes long @type fourbytes: bytes @rtype: str @raises ValueError: for bad input """ if not isinstance(fourbytes, bytes): raise ValueError("given buffer is not a string") if len(fourbytes) != 4: raise ValueError("given buffer is not exactly four bytes long") return ".".join([str(x) for x in bytes_to_int_seq(fourbytes)])
python
def unpack_ip(fourbytes): if not isinstance(fourbytes, bytes): raise ValueError("given buffer is not a string") if len(fourbytes) != 4: raise ValueError("given buffer is not exactly four bytes long") return ".".join([str(x) for x in bytes_to_int_seq(fourbytes)])
[ "def", "unpack_ip", "(", "fourbytes", ")", ":", "if", "not", "isinstance", "(", "fourbytes", ",", "bytes", ")", ":", "raise", "ValueError", "(", "\"given buffer is not a string\"", ")", "if", "len", "(", "fourbytes", ")", "!=", "4", ":", "raise", "ValueError...
Converts an ip address given in a four byte string in network byte order to a string in dotted notation. >>> unpack_ip(b"dead") '100.101.97.100' >>> unpack_ip(b"alive") Traceback (most recent call last): ... ValueError: given buffer is not exactly four bytes long @type fourbytes: bytes @rtype: str @raises ValueError: for bad input
[ "Converts", "an", "ip", "address", "given", "in", "a", "four", "byte", "string", "in", "network", "byte", "order", "to", "a", "string", "in", "dotted", "notation", "." ]
ff4459678ec023fd56e64ce518a86860efec26bf
https://github.com/CygnusNetworks/pypureomapi/blob/ff4459678ec023fd56e64ce518a86860efec26bf/pypureomapi.py#L751-L770