repo
stringlengths
7
55
path
stringlengths
4
223
func_name
stringlengths
1
134
original_string
stringlengths
75
104k
language
stringclasses
1 value
code
stringlengths
75
104k
code_tokens
listlengths
19
28.4k
docstring
stringlengths
1
46.9k
docstring_tokens
listlengths
1
1.97k
sha
stringlengths
40
40
url
stringlengths
87
315
partition
stringclasses
1 value
glomex/gcdt
gcdt/ramuda_core.py
logs
def logs(awsclient, function_name, start_dt, end_dt=None, tail=False): """Send a ping request to a lambda function. :param awsclient: :param function_name: :param start_dt: :param end_dt: :param tail: :return: """ log.debug('Getting cloudwatch logs for: %s', function_name) log_group_name = '/aws/lambda/%s' % function_name current_date = None start_ts = datetime_to_timestamp(start_dt) if end_dt: end_ts = datetime_to_timestamp(end_dt) else: end_ts = None # tail mode # we assume that logs can arrive late but not out of order # so we hold the timestamp of the last logentry and start the next iteration # from there while True: logentries = filter_log_events(awsclient, log_group_name, start_ts=start_ts, end_ts=end_ts) if logentries: for e in logentries: actual_date, actual_time = decode_format_timestamp(e['timestamp']) if current_date != actual_date: # print the date only when it changed current_date = actual_date log.info(current_date) log.info('%s %s' % (actual_time, e['message'].strip())) if tail: if logentries: start_ts = logentries[-1]['timestamp'] + 1 time.sleep(2) continue break
python
def logs(awsclient, function_name, start_dt, end_dt=None, tail=False): """Send a ping request to a lambda function. :param awsclient: :param function_name: :param start_dt: :param end_dt: :param tail: :return: """ log.debug('Getting cloudwatch logs for: %s', function_name) log_group_name = '/aws/lambda/%s' % function_name current_date = None start_ts = datetime_to_timestamp(start_dt) if end_dt: end_ts = datetime_to_timestamp(end_dt) else: end_ts = None # tail mode # we assume that logs can arrive late but not out of order # so we hold the timestamp of the last logentry and start the next iteration # from there while True: logentries = filter_log_events(awsclient, log_group_name, start_ts=start_ts, end_ts=end_ts) if logentries: for e in logentries: actual_date, actual_time = decode_format_timestamp(e['timestamp']) if current_date != actual_date: # print the date only when it changed current_date = actual_date log.info(current_date) log.info('%s %s' % (actual_time, e['message'].strip())) if tail: if logentries: start_ts = logentries[-1]['timestamp'] + 1 time.sleep(2) continue break
[ "def", "logs", "(", "awsclient", ",", "function_name", ",", "start_dt", ",", "end_dt", "=", "None", ",", "tail", "=", "False", ")", ":", "log", ".", "debug", "(", "'Getting cloudwatch logs for: %s'", ",", "function_name", ")", "log_group_name", "=", "'/aws/lambda/%s'", "%", "function_name", "current_date", "=", "None", "start_ts", "=", "datetime_to_timestamp", "(", "start_dt", ")", "if", "end_dt", ":", "end_ts", "=", "datetime_to_timestamp", "(", "end_dt", ")", "else", ":", "end_ts", "=", "None", "# tail mode", "# we assume that logs can arrive late but not out of order", "# so we hold the timestamp of the last logentry and start the next iteration", "# from there", "while", "True", ":", "logentries", "=", "filter_log_events", "(", "awsclient", ",", "log_group_name", ",", "start_ts", "=", "start_ts", ",", "end_ts", "=", "end_ts", ")", "if", "logentries", ":", "for", "e", "in", "logentries", ":", "actual_date", ",", "actual_time", "=", "decode_format_timestamp", "(", "e", "[", "'timestamp'", "]", ")", "if", "current_date", "!=", "actual_date", ":", "# print the date only when it changed", "current_date", "=", "actual_date", "log", ".", "info", "(", "current_date", ")", "log", ".", "info", "(", "'%s %s'", "%", "(", "actual_time", ",", "e", "[", "'message'", "]", ".", "strip", "(", ")", ")", ")", "if", "tail", ":", "if", "logentries", ":", "start_ts", "=", "logentries", "[", "-", "1", "]", "[", "'timestamp'", "]", "+", "1", "time", ".", "sleep", "(", "2", ")", "continue", "break" ]
Send a ping request to a lambda function. :param awsclient: :param function_name: :param start_dt: :param end_dt: :param tail: :return:
[ "Send", "a", "ping", "request", "to", "a", "lambda", "function", "." ]
cd67cf416371337b83cb9ca3f696277125703339
https://github.com/glomex/gcdt/blob/cd67cf416371337b83cb9ca3f696277125703339/gcdt/ramuda_core.py#L703-L743
train
thombashi/typepy
typepy/converter/_datetime.py
DateTimeConverter.__validate_datetime_string
def __validate_datetime_string(self): """ This will require validating version string (such as "3.3.5"). A version string could be converted to a datetime value if this validation is not executed. """ try: try: StrictVersion(self._value) raise TypeConversionError( "invalid datetime string: version string found {}".format(self._value) ) except ValueError: pass except TypeError: raise TypeConversionError("invalid datetime string: type={}".format(type(self._value)))
python
def __validate_datetime_string(self): """ This will require validating version string (such as "3.3.5"). A version string could be converted to a datetime value if this validation is not executed. """ try: try: StrictVersion(self._value) raise TypeConversionError( "invalid datetime string: version string found {}".format(self._value) ) except ValueError: pass except TypeError: raise TypeConversionError("invalid datetime string: type={}".format(type(self._value)))
[ "def", "__validate_datetime_string", "(", "self", ")", ":", "try", ":", "try", ":", "StrictVersion", "(", "self", ".", "_value", ")", "raise", "TypeConversionError", "(", "\"invalid datetime string: version string found {}\"", ".", "format", "(", "self", ".", "_value", ")", ")", "except", "ValueError", ":", "pass", "except", "TypeError", ":", "raise", "TypeConversionError", "(", "\"invalid datetime string: type={}\"", ".", "format", "(", "type", "(", "self", ".", "_value", ")", ")", ")" ]
This will require validating version string (such as "3.3.5"). A version string could be converted to a datetime value if this validation is not executed.
[ "This", "will", "require", "validating", "version", "string", "(", "such", "as", "3", ".", "3", ".", "5", ")", ".", "A", "version", "string", "could", "be", "converted", "to", "a", "datetime", "value", "if", "this", "validation", "is", "not", "executed", "." ]
8209d1df4f2a7f196a9fa4bfb0708c5ff648461f
https://github.com/thombashi/typepy/blob/8209d1df4f2a7f196a9fa4bfb0708c5ff648461f/typepy/converter/_datetime.py#L131-L147
train
DeepHorizons/iarm
iarm/arm_instructions/memory.py
Memory.ADR
def ADR(self, params): """ ADR Ra, [PC, #imm10_4] ADR Ra, label Load the address of label or the PC offset into Ra Ra must be a low register """ # TODO may need to rethink how I do PC, may need to be byte alligned # TODO This is wrong as each address is a word, not a byte. The filled value with its location (Do we want that, or the value at that location [Decompiled instruction]) try: Ra, Rb, Rc = self.get_three_parameters(self.THREE_PARAMETER_WITH_BRACKETS, params) except iarm.exceptions.ParsingError: Ra, label = self.get_two_parameters(self.TWO_PARAMETER_COMMA_SEPARATED, params) # TODO the address must be within 1020 bytes of current PC self.check_arguments(low_registers=(Ra,), label_exists=(label,)) def ADR_func(): self.register[Ra] = self.labels[label] # TODO is this correct? return ADR_func self.check_arguments(low_registers=(Ra,), imm10_4=(Rc,)) if Rb != 'PC': raise iarm.exceptions.IarmError("Second position argument is not PC: {}".format(Rb)) def ADR_func(): self.register[Ra] = self.register[Rb] + self.convert_to_integer(Rc[1:]) return ADR_func
python
def ADR(self, params): """ ADR Ra, [PC, #imm10_4] ADR Ra, label Load the address of label or the PC offset into Ra Ra must be a low register """ # TODO may need to rethink how I do PC, may need to be byte alligned # TODO This is wrong as each address is a word, not a byte. The filled value with its location (Do we want that, or the value at that location [Decompiled instruction]) try: Ra, Rb, Rc = self.get_three_parameters(self.THREE_PARAMETER_WITH_BRACKETS, params) except iarm.exceptions.ParsingError: Ra, label = self.get_two_parameters(self.TWO_PARAMETER_COMMA_SEPARATED, params) # TODO the address must be within 1020 bytes of current PC self.check_arguments(low_registers=(Ra,), label_exists=(label,)) def ADR_func(): self.register[Ra] = self.labels[label] # TODO is this correct? return ADR_func self.check_arguments(low_registers=(Ra,), imm10_4=(Rc,)) if Rb != 'PC': raise iarm.exceptions.IarmError("Second position argument is not PC: {}".format(Rb)) def ADR_func(): self.register[Ra] = self.register[Rb] + self.convert_to_integer(Rc[1:]) return ADR_func
[ "def", "ADR", "(", "self", ",", "params", ")", ":", "# TODO may need to rethink how I do PC, may need to be byte alligned", "# TODO This is wrong as each address is a word, not a byte. The filled value with its location (Do we want that, or the value at that location [Decompiled instruction])", "try", ":", "Ra", ",", "Rb", ",", "Rc", "=", "self", ".", "get_three_parameters", "(", "self", ".", "THREE_PARAMETER_WITH_BRACKETS", ",", "params", ")", "except", "iarm", ".", "exceptions", ".", "ParsingError", ":", "Ra", ",", "label", "=", "self", ".", "get_two_parameters", "(", "self", ".", "TWO_PARAMETER_COMMA_SEPARATED", ",", "params", ")", "# TODO the address must be within 1020 bytes of current PC", "self", ".", "check_arguments", "(", "low_registers", "=", "(", "Ra", ",", ")", ",", "label_exists", "=", "(", "label", ",", ")", ")", "def", "ADR_func", "(", ")", ":", "self", ".", "register", "[", "Ra", "]", "=", "self", ".", "labels", "[", "label", "]", "# TODO is this correct?", "return", "ADR_func", "self", ".", "check_arguments", "(", "low_registers", "=", "(", "Ra", ",", ")", ",", "imm10_4", "=", "(", "Rc", ",", ")", ")", "if", "Rb", "!=", "'PC'", ":", "raise", "iarm", ".", "exceptions", ".", "IarmError", "(", "\"Second position argument is not PC: {}\"", ".", "format", "(", "Rb", ")", ")", "def", "ADR_func", "(", ")", ":", "self", ".", "register", "[", "Ra", "]", "=", "self", ".", "register", "[", "Rb", "]", "+", "self", ".", "convert_to_integer", "(", "Rc", "[", "1", ":", "]", ")", "return", "ADR_func" ]
ADR Ra, [PC, #imm10_4] ADR Ra, label Load the address of label or the PC offset into Ra Ra must be a low register
[ "ADR", "Ra", "[", "PC", "#imm10_4", "]", "ADR", "Ra", "label" ]
b913c9fd577b793a6bbced78b78a5d8d7cd88de4
https://github.com/DeepHorizons/iarm/blob/b913c9fd577b793a6bbced78b78a5d8d7cd88de4/iarm/arm_instructions/memory.py#L9-L39
train
DeepHorizons/iarm
iarm/arm_instructions/memory.py
Memory.LDR
def LDR(self, params): """ LDR Ra, [PC, #imm10_4] LDR Ra, label LDR Ra, =equate LDR Ra, [Rb, Rc] LDR Ra, [Rb, #imm7_4] LDR Ra, [SP, #imm10_4] Load a word from memory into Ra Ra, Rb, and Rc must be low registers """ # TODO definition for PC is Ra <- M[PC + Imm10_4], Imm10_4 = PC - label, need to figure this one out try: Ra, Rb, Rc = self.get_three_parameters(self.THREE_PARAMETER_WITH_BRACKETS, params) except iarm.exceptions.ParsingError: Ra, label_name = self.get_two_parameters(self.TWO_PARAMETER_COMMA_SEPARATED, params) if label_name.startswith('='): # This is a pseudoinstructions label_name = label_name[1:] # TODO add check that label is a 32 bit number # TODO This does not work on instruction loading. This interpreter follows a harvard like architecture, # TODO while ARMv6-M (Cortex-M0+) is a Von Neumann architeture. Instructions will not be decompiled self.check_arguments(low_registers=(Ra,)) if label_name in self.labels: label_value = self.labels[label_name] elif label_name in self.equates: label_value = self.equates[label_name] else: try: label_value = int(self.convert_to_integer(label_name)) except ValueError: warnings.warn(iarm.exceptions.LabelDoesNotExist("Label `{}` does not exist or is not a parsable number. If it is a label, make sure it exists before running".format(label_name))) label_value = None if label_value is not None and int(label_value) % 4 != 0: # Make sure we are word aligned raise iarm.exceptions.IarmError("Memory access not word aligned; Immediate: {}".format(int(label_value))) elif label_name.startswith('[') and label_name.endswith(']'): # TODO improve this Rb = label_name[1:-1] if Rb == 'SP' or Rb == 'R13': self.check_arguments(low_registers=(Ra,)) else: self.check_arguments(low_registers=(Ra, label_name)) def LDR_func(): if self.memory[Rb] % 4 != 0: raise iarm.exceptions.HardFault( "Memory access not word aligned; Register: {} Immediate: {}".format(self.register[Rb], self.convert_to_integer( Rc[1:]))) self.register[Ra] = 0 for i in range(4): self.register[Ra] |= (self.memory[self.register[Rb] + i] << (8 * i)) return LDR_func else: self.check_arguments(low_registers=(Ra,), label_exists=(label_name,)) try: label_value = self.labels[label_name] if label_value >= 1024: raise iarm.exceptions.IarmError("Label {} has value {} and is greater than 1020".format(label_name, label_value)) if label_value % 4 != 0: raise iarm.exceptions.IarmError("Label {} has value {} and is not word aligned".format(label_name, label_value)) except KeyError: # Label doesn't exist, nothing we can do about that except maybe raise an exception now, # But we're avoiding that elsewhere, might as well avoid it here too pass def LDR_func(): nonlocal label_value # Since we can get a label that didn't exist in the creation step, We need to check it here # TODO is there a way for label_value to not exist? if label_value is None: # Try to get it again if label_name in self.labels: label_value = self.labels[label_name] elif label_name in self.equates: label_value = self.equates[label_name] # If it is still None, then it never got allocated if label_value is None: raise iarm.exceptions.IarmError("label `{}` does not exist. Was space allocated?".format(label_name)) # It does exist, make sure its word aligned if int(label_value) % 4 != 0: raise iarm.exceptions.IarmError("Memory access not word aligned; Immediate: {}".format(int(label_value))) try: self.register[Ra] = int(label_value) except ValueError: # TODO Can we even get to this path now? self.register[Ra] = self.labels[label_name] return LDR_func if self.is_immediate(Rc): if Rb == 'SP' or Rb == 'R15': self.check_arguments(low_registers=(Ra,), imm10_4=(Rc,)) else: self.check_arguments(low_registers=(Ra, Rb), imm7_4=(Rc,)) def LDR_func(): # TODO does memory read up? if (self.register[Rb] + self.convert_to_integer(Rc[1:])) % 4 != 0: raise iarm.exceptions.HardFault("Memory access not word aligned; Register: {} Immediate: {}".format(self.register[Rb], self.convert_to_integer(Rc[1:]))) self.register[Ra] = 0 for i in range(4): self.register[Ra] |= (self.memory[self.register[Rb] + self.convert_to_integer(Rc[1:]) + i] << (8 * i)) else: self.check_arguments(low_registers=(Ra, Rb, Rc)) def LDR_func(): # TODO does memory read up? if (self.register[Rb] + self.register[Rc]) % 4 != 0: raise iarm.exceptions.HardFault( "Memory access not word aligned; Register: {} Immediate: {}".format(self.register[Rb], self.convert_to_integer( Rc[1:]))) self.register[Ra] = 0 for i in range(4): self.register[Ra] |= (self.memory[self.register[Rb] + self.register[Rc] + i] << (8 * i)) return LDR_func
python
def LDR(self, params): """ LDR Ra, [PC, #imm10_4] LDR Ra, label LDR Ra, =equate LDR Ra, [Rb, Rc] LDR Ra, [Rb, #imm7_4] LDR Ra, [SP, #imm10_4] Load a word from memory into Ra Ra, Rb, and Rc must be low registers """ # TODO definition for PC is Ra <- M[PC + Imm10_4], Imm10_4 = PC - label, need to figure this one out try: Ra, Rb, Rc = self.get_three_parameters(self.THREE_PARAMETER_WITH_BRACKETS, params) except iarm.exceptions.ParsingError: Ra, label_name = self.get_two_parameters(self.TWO_PARAMETER_COMMA_SEPARATED, params) if label_name.startswith('='): # This is a pseudoinstructions label_name = label_name[1:] # TODO add check that label is a 32 bit number # TODO This does not work on instruction loading. This interpreter follows a harvard like architecture, # TODO while ARMv6-M (Cortex-M0+) is a Von Neumann architeture. Instructions will not be decompiled self.check_arguments(low_registers=(Ra,)) if label_name in self.labels: label_value = self.labels[label_name] elif label_name in self.equates: label_value = self.equates[label_name] else: try: label_value = int(self.convert_to_integer(label_name)) except ValueError: warnings.warn(iarm.exceptions.LabelDoesNotExist("Label `{}` does not exist or is not a parsable number. If it is a label, make sure it exists before running".format(label_name))) label_value = None if label_value is not None and int(label_value) % 4 != 0: # Make sure we are word aligned raise iarm.exceptions.IarmError("Memory access not word aligned; Immediate: {}".format(int(label_value))) elif label_name.startswith('[') and label_name.endswith(']'): # TODO improve this Rb = label_name[1:-1] if Rb == 'SP' or Rb == 'R13': self.check_arguments(low_registers=(Ra,)) else: self.check_arguments(low_registers=(Ra, label_name)) def LDR_func(): if self.memory[Rb] % 4 != 0: raise iarm.exceptions.HardFault( "Memory access not word aligned; Register: {} Immediate: {}".format(self.register[Rb], self.convert_to_integer( Rc[1:]))) self.register[Ra] = 0 for i in range(4): self.register[Ra] |= (self.memory[self.register[Rb] + i] << (8 * i)) return LDR_func else: self.check_arguments(low_registers=(Ra,), label_exists=(label_name,)) try: label_value = self.labels[label_name] if label_value >= 1024: raise iarm.exceptions.IarmError("Label {} has value {} and is greater than 1020".format(label_name, label_value)) if label_value % 4 != 0: raise iarm.exceptions.IarmError("Label {} has value {} and is not word aligned".format(label_name, label_value)) except KeyError: # Label doesn't exist, nothing we can do about that except maybe raise an exception now, # But we're avoiding that elsewhere, might as well avoid it here too pass def LDR_func(): nonlocal label_value # Since we can get a label that didn't exist in the creation step, We need to check it here # TODO is there a way for label_value to not exist? if label_value is None: # Try to get it again if label_name in self.labels: label_value = self.labels[label_name] elif label_name in self.equates: label_value = self.equates[label_name] # If it is still None, then it never got allocated if label_value is None: raise iarm.exceptions.IarmError("label `{}` does not exist. Was space allocated?".format(label_name)) # It does exist, make sure its word aligned if int(label_value) % 4 != 0: raise iarm.exceptions.IarmError("Memory access not word aligned; Immediate: {}".format(int(label_value))) try: self.register[Ra] = int(label_value) except ValueError: # TODO Can we even get to this path now? self.register[Ra] = self.labels[label_name] return LDR_func if self.is_immediate(Rc): if Rb == 'SP' or Rb == 'R15': self.check_arguments(low_registers=(Ra,), imm10_4=(Rc,)) else: self.check_arguments(low_registers=(Ra, Rb), imm7_4=(Rc,)) def LDR_func(): # TODO does memory read up? if (self.register[Rb] + self.convert_to_integer(Rc[1:])) % 4 != 0: raise iarm.exceptions.HardFault("Memory access not word aligned; Register: {} Immediate: {}".format(self.register[Rb], self.convert_to_integer(Rc[1:]))) self.register[Ra] = 0 for i in range(4): self.register[Ra] |= (self.memory[self.register[Rb] + self.convert_to_integer(Rc[1:]) + i] << (8 * i)) else: self.check_arguments(low_registers=(Ra, Rb, Rc)) def LDR_func(): # TODO does memory read up? if (self.register[Rb] + self.register[Rc]) % 4 != 0: raise iarm.exceptions.HardFault( "Memory access not word aligned; Register: {} Immediate: {}".format(self.register[Rb], self.convert_to_integer( Rc[1:]))) self.register[Ra] = 0 for i in range(4): self.register[Ra] |= (self.memory[self.register[Rb] + self.register[Rc] + i] << (8 * i)) return LDR_func
[ "def", "LDR", "(", "self", ",", "params", ")", ":", "# TODO definition for PC is Ra <- M[PC + Imm10_4], Imm10_4 = PC - label, need to figure this one out", "try", ":", "Ra", ",", "Rb", ",", "Rc", "=", "self", ".", "get_three_parameters", "(", "self", ".", "THREE_PARAMETER_WITH_BRACKETS", ",", "params", ")", "except", "iarm", ".", "exceptions", ".", "ParsingError", ":", "Ra", ",", "label_name", "=", "self", ".", "get_two_parameters", "(", "self", ".", "TWO_PARAMETER_COMMA_SEPARATED", ",", "params", ")", "if", "label_name", ".", "startswith", "(", "'='", ")", ":", "# This is a pseudoinstructions", "label_name", "=", "label_name", "[", "1", ":", "]", "# TODO add check that label is a 32 bit number", "# TODO This does not work on instruction loading. This interpreter follows a harvard like architecture,", "# TODO while ARMv6-M (Cortex-M0+) is a Von Neumann architeture. Instructions will not be decompiled", "self", ".", "check_arguments", "(", "low_registers", "=", "(", "Ra", ",", ")", ")", "if", "label_name", "in", "self", ".", "labels", ":", "label_value", "=", "self", ".", "labels", "[", "label_name", "]", "elif", "label_name", "in", "self", ".", "equates", ":", "label_value", "=", "self", ".", "equates", "[", "label_name", "]", "else", ":", "try", ":", "label_value", "=", "int", "(", "self", ".", "convert_to_integer", "(", "label_name", ")", ")", "except", "ValueError", ":", "warnings", ".", "warn", "(", "iarm", ".", "exceptions", ".", "LabelDoesNotExist", "(", "\"Label `{}` does not exist or is not a parsable number. If it is a label, make sure it exists before running\"", ".", "format", "(", "label_name", ")", ")", ")", "label_value", "=", "None", "if", "label_value", "is", "not", "None", "and", "int", "(", "label_value", ")", "%", "4", "!=", "0", ":", "# Make sure we are word aligned", "raise", "iarm", ".", "exceptions", ".", "IarmError", "(", "\"Memory access not word aligned; Immediate: {}\"", ".", "format", "(", "int", "(", "label_value", ")", ")", ")", "elif", "label_name", ".", "startswith", "(", "'['", ")", "and", "label_name", ".", "endswith", "(", "']'", ")", ":", "# TODO improve this", "Rb", "=", "label_name", "[", "1", ":", "-", "1", "]", "if", "Rb", "==", "'SP'", "or", "Rb", "==", "'R13'", ":", "self", ".", "check_arguments", "(", "low_registers", "=", "(", "Ra", ",", ")", ")", "else", ":", "self", ".", "check_arguments", "(", "low_registers", "=", "(", "Ra", ",", "label_name", ")", ")", "def", "LDR_func", "(", ")", ":", "if", "self", ".", "memory", "[", "Rb", "]", "%", "4", "!=", "0", ":", "raise", "iarm", ".", "exceptions", ".", "HardFault", "(", "\"Memory access not word aligned; Register: {} Immediate: {}\"", ".", "format", "(", "self", ".", "register", "[", "Rb", "]", ",", "self", ".", "convert_to_integer", "(", "Rc", "[", "1", ":", "]", ")", ")", ")", "self", ".", "register", "[", "Ra", "]", "=", "0", "for", "i", "in", "range", "(", "4", ")", ":", "self", ".", "register", "[", "Ra", "]", "|=", "(", "self", ".", "memory", "[", "self", ".", "register", "[", "Rb", "]", "+", "i", "]", "<<", "(", "8", "*", "i", ")", ")", "return", "LDR_func", "else", ":", "self", ".", "check_arguments", "(", "low_registers", "=", "(", "Ra", ",", ")", ",", "label_exists", "=", "(", "label_name", ",", ")", ")", "try", ":", "label_value", "=", "self", ".", "labels", "[", "label_name", "]", "if", "label_value", ">=", "1024", ":", "raise", "iarm", ".", "exceptions", ".", "IarmError", "(", "\"Label {} has value {} and is greater than 1020\"", ".", "format", "(", "label_name", ",", "label_value", ")", ")", "if", "label_value", "%", "4", "!=", "0", ":", "raise", "iarm", ".", "exceptions", ".", "IarmError", "(", "\"Label {} has value {} and is not word aligned\"", ".", "format", "(", "label_name", ",", "label_value", ")", ")", "except", "KeyError", ":", "# Label doesn't exist, nothing we can do about that except maybe raise an exception now,", "# But we're avoiding that elsewhere, might as well avoid it here too", "pass", "def", "LDR_func", "(", ")", ":", "nonlocal", "label_value", "# Since we can get a label that didn't exist in the creation step, We need to check it here", "# TODO is there a way for label_value to not exist?", "if", "label_value", "is", "None", ":", "# Try to get it again", "if", "label_name", "in", "self", ".", "labels", ":", "label_value", "=", "self", ".", "labels", "[", "label_name", "]", "elif", "label_name", "in", "self", ".", "equates", ":", "label_value", "=", "self", ".", "equates", "[", "label_name", "]", "# If it is still None, then it never got allocated", "if", "label_value", "is", "None", ":", "raise", "iarm", ".", "exceptions", ".", "IarmError", "(", "\"label `{}` does not exist. Was space allocated?\"", ".", "format", "(", "label_name", ")", ")", "# It does exist, make sure its word aligned", "if", "int", "(", "label_value", ")", "%", "4", "!=", "0", ":", "raise", "iarm", ".", "exceptions", ".", "IarmError", "(", "\"Memory access not word aligned; Immediate: {}\"", ".", "format", "(", "int", "(", "label_value", ")", ")", ")", "try", ":", "self", ".", "register", "[", "Ra", "]", "=", "int", "(", "label_value", ")", "except", "ValueError", ":", "# TODO Can we even get to this path now?", "self", ".", "register", "[", "Ra", "]", "=", "self", ".", "labels", "[", "label_name", "]", "return", "LDR_func", "if", "self", ".", "is_immediate", "(", "Rc", ")", ":", "if", "Rb", "==", "'SP'", "or", "Rb", "==", "'R15'", ":", "self", ".", "check_arguments", "(", "low_registers", "=", "(", "Ra", ",", ")", ",", "imm10_4", "=", "(", "Rc", ",", ")", ")", "else", ":", "self", ".", "check_arguments", "(", "low_registers", "=", "(", "Ra", ",", "Rb", ")", ",", "imm7_4", "=", "(", "Rc", ",", ")", ")", "def", "LDR_func", "(", ")", ":", "# TODO does memory read up?", "if", "(", "self", ".", "register", "[", "Rb", "]", "+", "self", ".", "convert_to_integer", "(", "Rc", "[", "1", ":", "]", ")", ")", "%", "4", "!=", "0", ":", "raise", "iarm", ".", "exceptions", ".", "HardFault", "(", "\"Memory access not word aligned; Register: {} Immediate: {}\"", ".", "format", "(", "self", ".", "register", "[", "Rb", "]", ",", "self", ".", "convert_to_integer", "(", "Rc", "[", "1", ":", "]", ")", ")", ")", "self", ".", "register", "[", "Ra", "]", "=", "0", "for", "i", "in", "range", "(", "4", ")", ":", "self", ".", "register", "[", "Ra", "]", "|=", "(", "self", ".", "memory", "[", "self", ".", "register", "[", "Rb", "]", "+", "self", ".", "convert_to_integer", "(", "Rc", "[", "1", ":", "]", ")", "+", "i", "]", "<<", "(", "8", "*", "i", ")", ")", "else", ":", "self", ".", "check_arguments", "(", "low_registers", "=", "(", "Ra", ",", "Rb", ",", "Rc", ")", ")", "def", "LDR_func", "(", ")", ":", "# TODO does memory read up?", "if", "(", "self", ".", "register", "[", "Rb", "]", "+", "self", ".", "register", "[", "Rc", "]", ")", "%", "4", "!=", "0", ":", "raise", "iarm", ".", "exceptions", ".", "HardFault", "(", "\"Memory access not word aligned; Register: {} Immediate: {}\"", ".", "format", "(", "self", ".", "register", "[", "Rb", "]", ",", "self", ".", "convert_to_integer", "(", "Rc", "[", "1", ":", "]", ")", ")", ")", "self", ".", "register", "[", "Ra", "]", "=", "0", "for", "i", "in", "range", "(", "4", ")", ":", "self", ".", "register", "[", "Ra", "]", "|=", "(", "self", ".", "memory", "[", "self", ".", "register", "[", "Rb", "]", "+", "self", ".", "register", "[", "Rc", "]", "+", "i", "]", "<<", "(", "8", "*", "i", ")", ")", "return", "LDR_func" ]
LDR Ra, [PC, #imm10_4] LDR Ra, label LDR Ra, =equate LDR Ra, [Rb, Rc] LDR Ra, [Rb, #imm7_4] LDR Ra, [SP, #imm10_4] Load a word from memory into Ra Ra, Rb, and Rc must be low registers
[ "LDR", "Ra", "[", "PC", "#imm10_4", "]", "LDR", "Ra", "label", "LDR", "Ra", "=", "equate", "LDR", "Ra", "[", "Rb", "Rc", "]", "LDR", "Ra", "[", "Rb", "#imm7_4", "]", "LDR", "Ra", "[", "SP", "#imm10_4", "]" ]
b913c9fd577b793a6bbced78b78a5d8d7cd88de4
https://github.com/DeepHorizons/iarm/blob/b913c9fd577b793a6bbced78b78a5d8d7cd88de4/iarm/arm_instructions/memory.py#L64-L187
train
DeepHorizons/iarm
iarm/arm_instructions/memory.py
Memory.LDRB
def LDRB(self, params): """ LDRB Ra, [Rb, Rc] LDRB Ra, [Rb, #imm5] Load a byte from memory into Ra Ra, Rb, and Rc must be low registers """ try: Ra, Rb, Rc = self.get_three_parameters(self.THREE_PARAMETER_WITH_BRACKETS, params) except iarm.exceptions.ParsingError: # LDRB Rn, [Rk] translates to an offset of zero Ra, Rb = self.get_two_parameters(r'\s*([^\s,]*),\s*\[([^\s,]*)\](,\s*[^\s,]*)*\s*', params) Rc = '#0' if self.is_immediate(Rc): self.check_arguments(low_registers=(Ra, Rb), imm5=(Rc,)) def LDRB_func(): self.register[Ra] = self.memory[self.register[Rb] + self.convert_to_integer(Rc[1:])] else: self.check_arguments(low_registers=(Ra, Rb, Rc)) def LDRB_func(): self.register[Ra] = self.memory[self.register[Rb] + self.register[Rc]] return LDRB_func
python
def LDRB(self, params): """ LDRB Ra, [Rb, Rc] LDRB Ra, [Rb, #imm5] Load a byte from memory into Ra Ra, Rb, and Rc must be low registers """ try: Ra, Rb, Rc = self.get_three_parameters(self.THREE_PARAMETER_WITH_BRACKETS, params) except iarm.exceptions.ParsingError: # LDRB Rn, [Rk] translates to an offset of zero Ra, Rb = self.get_two_parameters(r'\s*([^\s,]*),\s*\[([^\s,]*)\](,\s*[^\s,]*)*\s*', params) Rc = '#0' if self.is_immediate(Rc): self.check_arguments(low_registers=(Ra, Rb), imm5=(Rc,)) def LDRB_func(): self.register[Ra] = self.memory[self.register[Rb] + self.convert_to_integer(Rc[1:])] else: self.check_arguments(low_registers=(Ra, Rb, Rc)) def LDRB_func(): self.register[Ra] = self.memory[self.register[Rb] + self.register[Rc]] return LDRB_func
[ "def", "LDRB", "(", "self", ",", "params", ")", ":", "try", ":", "Ra", ",", "Rb", ",", "Rc", "=", "self", ".", "get_three_parameters", "(", "self", ".", "THREE_PARAMETER_WITH_BRACKETS", ",", "params", ")", "except", "iarm", ".", "exceptions", ".", "ParsingError", ":", "# LDRB Rn, [Rk] translates to an offset of zero", "Ra", ",", "Rb", "=", "self", ".", "get_two_parameters", "(", "r'\\s*([^\\s,]*),\\s*\\[([^\\s,]*)\\](,\\s*[^\\s,]*)*\\s*'", ",", "params", ")", "Rc", "=", "'#0'", "if", "self", ".", "is_immediate", "(", "Rc", ")", ":", "self", ".", "check_arguments", "(", "low_registers", "=", "(", "Ra", ",", "Rb", ")", ",", "imm5", "=", "(", "Rc", ",", ")", ")", "def", "LDRB_func", "(", ")", ":", "self", ".", "register", "[", "Ra", "]", "=", "self", ".", "memory", "[", "self", ".", "register", "[", "Rb", "]", "+", "self", ".", "convert_to_integer", "(", "Rc", "[", "1", ":", "]", ")", "]", "else", ":", "self", ".", "check_arguments", "(", "low_registers", "=", "(", "Ra", ",", "Rb", ",", "Rc", ")", ")", "def", "LDRB_func", "(", ")", ":", "self", ".", "register", "[", "Ra", "]", "=", "self", ".", "memory", "[", "self", ".", "register", "[", "Rb", "]", "+", "self", ".", "register", "[", "Rc", "]", "]", "return", "LDRB_func" ]
LDRB Ra, [Rb, Rc] LDRB Ra, [Rb, #imm5] Load a byte from memory into Ra Ra, Rb, and Rc must be low registers
[ "LDRB", "Ra", "[", "Rb", "Rc", "]", "LDRB", "Ra", "[", "Rb", "#imm5", "]" ]
b913c9fd577b793a6bbced78b78a5d8d7cd88de4
https://github.com/DeepHorizons/iarm/blob/b913c9fd577b793a6bbced78b78a5d8d7cd88de4/iarm/arm_instructions/memory.py#L189-L215
train
DeepHorizons/iarm
iarm/arm_instructions/memory.py
Memory.LDRH
def LDRH(self, params): """ LDRH Ra, [Rb, Rc] LDRH Ra, [Rb, #imm6_2] Load a half word from memory into Ra Ra, Rb, and Rc must be low registers """ try: Ra, Rb, Rc = self.get_three_parameters(self.THREE_PARAMETER_WITH_BRACKETS, params) except iarm.exceptions.ParsingError: # LDRB Rn, [Rk] translates to an offset of zero Ra, Rb = self.get_two_parameters(r'\s*([^\s,]*),\s*\[([^\s,]*)\](,\s*[^\s,]*)*\s*', params) Rc = '#0' if self.is_immediate(Rc): self.check_arguments(low_registers=(Ra, Rb), imm6_2=(Rc,)) def LDRH_func(): # TODO does memory read up? if (self.register[Rb]) % 2 != 0: raise iarm.exceptions.HardFault( "Memory access not half word aligned; Register: {} Immediate: {}".format(self.register[Rb], self.convert_to_integer( Rc[1:]))) self.register[Ra] = 0 for i in range(2): self.register[Ra] |= (self.memory[self.register[Rb] + self.convert_to_integer(Rc[1:]) + i] << (8 * i)) else: self.check_arguments(low_registers=(Ra, Rb, Rc)) def LDRH_func(): # TODO does memory read up? if (self.register[Rb] + self.register[Rc]) % 2 != 0: raise iarm.exceptions.HardFault( "Memory access not half word aligned; Register: {} Immediate: {}".format(self.register[Rb], self.register[Rc])) self.register[Ra] = 0 for i in range(2): self.register[Ra] |= (self.memory[self.register[Rb] + self.register[Rc] + i] << (8 * i)) return LDRH_func
python
def LDRH(self, params): """ LDRH Ra, [Rb, Rc] LDRH Ra, [Rb, #imm6_2] Load a half word from memory into Ra Ra, Rb, and Rc must be low registers """ try: Ra, Rb, Rc = self.get_three_parameters(self.THREE_PARAMETER_WITH_BRACKETS, params) except iarm.exceptions.ParsingError: # LDRB Rn, [Rk] translates to an offset of zero Ra, Rb = self.get_two_parameters(r'\s*([^\s,]*),\s*\[([^\s,]*)\](,\s*[^\s,]*)*\s*', params) Rc = '#0' if self.is_immediate(Rc): self.check_arguments(low_registers=(Ra, Rb), imm6_2=(Rc,)) def LDRH_func(): # TODO does memory read up? if (self.register[Rb]) % 2 != 0: raise iarm.exceptions.HardFault( "Memory access not half word aligned; Register: {} Immediate: {}".format(self.register[Rb], self.convert_to_integer( Rc[1:]))) self.register[Ra] = 0 for i in range(2): self.register[Ra] |= (self.memory[self.register[Rb] + self.convert_to_integer(Rc[1:]) + i] << (8 * i)) else: self.check_arguments(low_registers=(Ra, Rb, Rc)) def LDRH_func(): # TODO does memory read up? if (self.register[Rb] + self.register[Rc]) % 2 != 0: raise iarm.exceptions.HardFault( "Memory access not half word aligned; Register: {} Immediate: {}".format(self.register[Rb], self.register[Rc])) self.register[Ra] = 0 for i in range(2): self.register[Ra] |= (self.memory[self.register[Rb] + self.register[Rc] + i] << (8 * i)) return LDRH_func
[ "def", "LDRH", "(", "self", ",", "params", ")", ":", "try", ":", "Ra", ",", "Rb", ",", "Rc", "=", "self", ".", "get_three_parameters", "(", "self", ".", "THREE_PARAMETER_WITH_BRACKETS", ",", "params", ")", "except", "iarm", ".", "exceptions", ".", "ParsingError", ":", "# LDRB Rn, [Rk] translates to an offset of zero", "Ra", ",", "Rb", "=", "self", ".", "get_two_parameters", "(", "r'\\s*([^\\s,]*),\\s*\\[([^\\s,]*)\\](,\\s*[^\\s,]*)*\\s*'", ",", "params", ")", "Rc", "=", "'#0'", "if", "self", ".", "is_immediate", "(", "Rc", ")", ":", "self", ".", "check_arguments", "(", "low_registers", "=", "(", "Ra", ",", "Rb", ")", ",", "imm6_2", "=", "(", "Rc", ",", ")", ")", "def", "LDRH_func", "(", ")", ":", "# TODO does memory read up?", "if", "(", "self", ".", "register", "[", "Rb", "]", ")", "%", "2", "!=", "0", ":", "raise", "iarm", ".", "exceptions", ".", "HardFault", "(", "\"Memory access not half word aligned; Register: {} Immediate: {}\"", ".", "format", "(", "self", ".", "register", "[", "Rb", "]", ",", "self", ".", "convert_to_integer", "(", "Rc", "[", "1", ":", "]", ")", ")", ")", "self", ".", "register", "[", "Ra", "]", "=", "0", "for", "i", "in", "range", "(", "2", ")", ":", "self", ".", "register", "[", "Ra", "]", "|=", "(", "self", ".", "memory", "[", "self", ".", "register", "[", "Rb", "]", "+", "self", ".", "convert_to_integer", "(", "Rc", "[", "1", ":", "]", ")", "+", "i", "]", "<<", "(", "8", "*", "i", ")", ")", "else", ":", "self", ".", "check_arguments", "(", "low_registers", "=", "(", "Ra", ",", "Rb", ",", "Rc", ")", ")", "def", "LDRH_func", "(", ")", ":", "# TODO does memory read up?", "if", "(", "self", ".", "register", "[", "Rb", "]", "+", "self", ".", "register", "[", "Rc", "]", ")", "%", "2", "!=", "0", ":", "raise", "iarm", ".", "exceptions", ".", "HardFault", "(", "\"Memory access not half word aligned; Register: {} Immediate: {}\"", ".", "format", "(", "self", ".", "register", "[", "Rb", "]", ",", "self", ".", "register", "[", "Rc", "]", ")", ")", "self", ".", "register", "[", "Ra", "]", "=", "0", "for", "i", "in", "range", "(", "2", ")", ":", "self", ".", "register", "[", "Ra", "]", "|=", "(", "self", ".", "memory", "[", "self", ".", "register", "[", "Rb", "]", "+", "self", ".", "register", "[", "Rc", "]", "+", "i", "]", "<<", "(", "8", "*", "i", ")", ")", "return", "LDRH_func" ]
LDRH Ra, [Rb, Rc] LDRH Ra, [Rb, #imm6_2] Load a half word from memory into Ra Ra, Rb, and Rc must be low registers
[ "LDRH", "Ra", "[", "Rb", "Rc", "]", "LDRH", "Ra", "[", "Rb", "#imm6_2", "]" ]
b913c9fd577b793a6bbced78b78a5d8d7cd88de4
https://github.com/DeepHorizons/iarm/blob/b913c9fd577b793a6bbced78b78a5d8d7cd88de4/iarm/arm_instructions/memory.py#L217-L258
train
DeepHorizons/iarm
iarm/arm_instructions/memory.py
Memory.LDRSB
def LDRSB(self, params): """ LDRSB Ra, [Rb, Rc] Load a byte from memory, sign extend, and put into Ra Ra, Rb, and Rc must be low registers """ # TODO LDRSB cant use immediates Ra, Rb, Rc = self.get_three_parameters(self.THREE_PARAMETER_WITH_BRACKETS, params) self.check_arguments(low_registers=(Ra, Rb, Rc)) def LDRSB_func(): # TODO does memory read up? self.register[Ra] = 0 self.register[Ra] |= self.memory[self.register[Rb] + self.register[Rc]] if self.register[Ra] & (1 << 7): self.register[Ra] |= (0xFFFFFF << 8) return LDRSB_func
python
def LDRSB(self, params): """ LDRSB Ra, [Rb, Rc] Load a byte from memory, sign extend, and put into Ra Ra, Rb, and Rc must be low registers """ # TODO LDRSB cant use immediates Ra, Rb, Rc = self.get_three_parameters(self.THREE_PARAMETER_WITH_BRACKETS, params) self.check_arguments(low_registers=(Ra, Rb, Rc)) def LDRSB_func(): # TODO does memory read up? self.register[Ra] = 0 self.register[Ra] |= self.memory[self.register[Rb] + self.register[Rc]] if self.register[Ra] & (1 << 7): self.register[Ra] |= (0xFFFFFF << 8) return LDRSB_func
[ "def", "LDRSB", "(", "self", ",", "params", ")", ":", "# TODO LDRSB cant use immediates", "Ra", ",", "Rb", ",", "Rc", "=", "self", ".", "get_three_parameters", "(", "self", ".", "THREE_PARAMETER_WITH_BRACKETS", ",", "params", ")", "self", ".", "check_arguments", "(", "low_registers", "=", "(", "Ra", ",", "Rb", ",", "Rc", ")", ")", "def", "LDRSB_func", "(", ")", ":", "# TODO does memory read up?", "self", ".", "register", "[", "Ra", "]", "=", "0", "self", ".", "register", "[", "Ra", "]", "|=", "self", ".", "memory", "[", "self", ".", "register", "[", "Rb", "]", "+", "self", ".", "register", "[", "Rc", "]", "]", "if", "self", ".", "register", "[", "Ra", "]", "&", "(", "1", "<<", "7", ")", ":", "self", ".", "register", "[", "Ra", "]", "|=", "(", "0xFFFFFF", "<<", "8", ")", "return", "LDRSB_func" ]
LDRSB Ra, [Rb, Rc] Load a byte from memory, sign extend, and put into Ra Ra, Rb, and Rc must be low registers
[ "LDRSB", "Ra", "[", "Rb", "Rc", "]" ]
b913c9fd577b793a6bbced78b78a5d8d7cd88de4
https://github.com/DeepHorizons/iarm/blob/b913c9fd577b793a6bbced78b78a5d8d7cd88de4/iarm/arm_instructions/memory.py#L260-L279
train
DeepHorizons/iarm
iarm/arm_instructions/memory.py
Memory.LDRSH
def LDRSH(self, params): """ LDRSH Ra, [Rb, Rc] Load a half word from memory, sign extend, and put into Ra Ra, Rb, and Rc must be low registers """ # TODO LDRSH cant use immediates Ra, Rb, Rc = self.get_three_parameters(self.THREE_PARAMETER_WITH_BRACKETS, params) self.check_arguments(low_registers=(Ra, Rb, Rc)) def LDRSH_func(): # TODO does memory read up? if (self.register[Rb] + self.register[Rc]) % 2 != 0: raise iarm.exceptions.HardFault( "Memory access not half word aligned\nR{}: {}\nR{}: {}".format(Rb, self.register[Rb], Rc, self.register[Rc])) self.register[Ra] = 0 for i in range(2): self.register[Ra] |= (self.memory[self.register[Rb] + self.register[Rc] + i] << (8 * i)) if self.register[Ra] & (1 << 15): self.register[Ra] |= (0xFFFF << 16) return LDRSH_func
python
def LDRSH(self, params): """ LDRSH Ra, [Rb, Rc] Load a half word from memory, sign extend, and put into Ra Ra, Rb, and Rc must be low registers """ # TODO LDRSH cant use immediates Ra, Rb, Rc = self.get_three_parameters(self.THREE_PARAMETER_WITH_BRACKETS, params) self.check_arguments(low_registers=(Ra, Rb, Rc)) def LDRSH_func(): # TODO does memory read up? if (self.register[Rb] + self.register[Rc]) % 2 != 0: raise iarm.exceptions.HardFault( "Memory access not half word aligned\nR{}: {}\nR{}: {}".format(Rb, self.register[Rb], Rc, self.register[Rc])) self.register[Ra] = 0 for i in range(2): self.register[Ra] |= (self.memory[self.register[Rb] + self.register[Rc] + i] << (8 * i)) if self.register[Ra] & (1 << 15): self.register[Ra] |= (0xFFFF << 16) return LDRSH_func
[ "def", "LDRSH", "(", "self", ",", "params", ")", ":", "# TODO LDRSH cant use immediates", "Ra", ",", "Rb", ",", "Rc", "=", "self", ".", "get_three_parameters", "(", "self", ".", "THREE_PARAMETER_WITH_BRACKETS", ",", "params", ")", "self", ".", "check_arguments", "(", "low_registers", "=", "(", "Ra", ",", "Rb", ",", "Rc", ")", ")", "def", "LDRSH_func", "(", ")", ":", "# TODO does memory read up?", "if", "(", "self", ".", "register", "[", "Rb", "]", "+", "self", ".", "register", "[", "Rc", "]", ")", "%", "2", "!=", "0", ":", "raise", "iarm", ".", "exceptions", ".", "HardFault", "(", "\"Memory access not half word aligned\\nR{}: {}\\nR{}: {}\"", ".", "format", "(", "Rb", ",", "self", ".", "register", "[", "Rb", "]", ",", "Rc", ",", "self", ".", "register", "[", "Rc", "]", ")", ")", "self", ".", "register", "[", "Ra", "]", "=", "0", "for", "i", "in", "range", "(", "2", ")", ":", "self", ".", "register", "[", "Ra", "]", "|=", "(", "self", ".", "memory", "[", "self", ".", "register", "[", "Rb", "]", "+", "self", ".", "register", "[", "Rc", "]", "+", "i", "]", "<<", "(", "8", "*", "i", ")", ")", "if", "self", ".", "register", "[", "Ra", "]", "&", "(", "1", "<<", "15", ")", ":", "self", ".", "register", "[", "Ra", "]", "|=", "(", "0xFFFF", "<<", "16", ")", "return", "LDRSH_func" ]
LDRSH Ra, [Rb, Rc] Load a half word from memory, sign extend, and put into Ra Ra, Rb, and Rc must be low registers
[ "LDRSH", "Ra", "[", "Rb", "Rc", "]" ]
b913c9fd577b793a6bbced78b78a5d8d7cd88de4
https://github.com/DeepHorizons/iarm/blob/b913c9fd577b793a6bbced78b78a5d8d7cd88de4/iarm/arm_instructions/memory.py#L281-L305
train
DeepHorizons/iarm
iarm/arm_instructions/memory.py
Memory.POP
def POP(self, params): """ POP {RPopList} Pop from the stack into the list of registers List must contain only low registers or PC """ # TODO verify pop order # TODO pop list is comma separate, right? # TODO what registeres are allowed to POP to? Low Registers and PC # TODO need to support ranges, ie {R2, R5-R7} # TODO PUSH should reverse the list, not POP RPopList = self.get_one_parameter(r'\s*{(.*)}(.*)', params).split(',') RPopList.reverse() RPopList = [i.strip() for i in RPopList] def POP_func(): for register in RPopList: # Get 4 bytes value = 0 for i in range(4): # TODO use memory width instead of constants value |= self.memory[self.register['SP'] + i] << (8 * i) self.register[register] = value self.register['SP'] += 4 return POP_func
python
def POP(self, params): """ POP {RPopList} Pop from the stack into the list of registers List must contain only low registers or PC """ # TODO verify pop order # TODO pop list is comma separate, right? # TODO what registeres are allowed to POP to? Low Registers and PC # TODO need to support ranges, ie {R2, R5-R7} # TODO PUSH should reverse the list, not POP RPopList = self.get_one_parameter(r'\s*{(.*)}(.*)', params).split(',') RPopList.reverse() RPopList = [i.strip() for i in RPopList] def POP_func(): for register in RPopList: # Get 4 bytes value = 0 for i in range(4): # TODO use memory width instead of constants value |= self.memory[self.register['SP'] + i] << (8 * i) self.register[register] = value self.register['SP'] += 4 return POP_func
[ "def", "POP", "(", "self", ",", "params", ")", ":", "# TODO verify pop order", "# TODO pop list is comma separate, right?", "# TODO what registeres are allowed to POP to? Low Registers and PC", "# TODO need to support ranges, ie {R2, R5-R7}", "# TODO PUSH should reverse the list, not POP", "RPopList", "=", "self", ".", "get_one_parameter", "(", "r'\\s*{(.*)}(.*)'", ",", "params", ")", ".", "split", "(", "','", ")", "RPopList", ".", "reverse", "(", ")", "RPopList", "=", "[", "i", ".", "strip", "(", ")", "for", "i", "in", "RPopList", "]", "def", "POP_func", "(", ")", ":", "for", "register", "in", "RPopList", ":", "# Get 4 bytes", "value", "=", "0", "for", "i", "in", "range", "(", "4", ")", ":", "# TODO use memory width instead of constants", "value", "|=", "self", ".", "memory", "[", "self", ".", "register", "[", "'SP'", "]", "+", "i", "]", "<<", "(", "8", "*", "i", ")", "self", ".", "register", "[", "register", "]", "=", "value", "self", ".", "register", "[", "'SP'", "]", "+=", "4", "return", "POP_func" ]
POP {RPopList} Pop from the stack into the list of registers List must contain only low registers or PC
[ "POP", "{", "RPopList", "}" ]
b913c9fd577b793a6bbced78b78a5d8d7cd88de4
https://github.com/DeepHorizons/iarm/blob/b913c9fd577b793a6bbced78b78a5d8d7cd88de4/iarm/arm_instructions/memory.py#L307-L334
train
DeepHorizons/iarm
iarm/arm_instructions/memory.py
Memory.PUSH
def PUSH(self, params): """ PUSH {RPushList} Push to the stack from a list of registers List must contain only low registers or LR """ # TODO what registers are allowed to PUSH to? Low registers and LR # TODO PUSH should reverse the list, not POP RPushList = self.get_one_parameter(r'\s*{(.*)}(.*)', params).split(',') RPushList = [i.strip() for i in RPushList] # TODO should we make sure the register exists? probably not def PUSH_func(): for register in RPushList: self.register['SP'] -= 4 for i in range(4): # TODO is this the same as with POP? self.memory[self.register['SP'] + i] = ((self.register[register] >> (8 * i)) & 0xFF) return PUSH_func
python
def PUSH(self, params): """ PUSH {RPushList} Push to the stack from a list of registers List must contain only low registers or LR """ # TODO what registers are allowed to PUSH to? Low registers and LR # TODO PUSH should reverse the list, not POP RPushList = self.get_one_parameter(r'\s*{(.*)}(.*)', params).split(',') RPushList = [i.strip() for i in RPushList] # TODO should we make sure the register exists? probably not def PUSH_func(): for register in RPushList: self.register['SP'] -= 4 for i in range(4): # TODO is this the same as with POP? self.memory[self.register['SP'] + i] = ((self.register[register] >> (8 * i)) & 0xFF) return PUSH_func
[ "def", "PUSH", "(", "self", ",", "params", ")", ":", "# TODO what registers are allowed to PUSH to? Low registers and LR", "# TODO PUSH should reverse the list, not POP", "RPushList", "=", "self", ".", "get_one_parameter", "(", "r'\\s*{(.*)}(.*)'", ",", "params", ")", ".", "split", "(", "','", ")", "RPushList", "=", "[", "i", ".", "strip", "(", ")", "for", "i", "in", "RPushList", "]", "# TODO should we make sure the register exists? probably not", "def", "PUSH_func", "(", ")", ":", "for", "register", "in", "RPushList", ":", "self", ".", "register", "[", "'SP'", "]", "-=", "4", "for", "i", "in", "range", "(", "4", ")", ":", "# TODO is this the same as with POP?", "self", ".", "memory", "[", "self", ".", "register", "[", "'SP'", "]", "+", "i", "]", "=", "(", "(", "self", ".", "register", "[", "register", "]", ">>", "(", "8", "*", "i", ")", ")", "&", "0xFF", ")", "return", "PUSH_func" ]
PUSH {RPushList} Push to the stack from a list of registers List must contain only low registers or LR
[ "PUSH", "{", "RPushList", "}" ]
b913c9fd577b793a6bbced78b78a5d8d7cd88de4
https://github.com/DeepHorizons/iarm/blob/b913c9fd577b793a6bbced78b78a5d8d7cd88de4/iarm/arm_instructions/memory.py#L336-L357
train
DeepHorizons/iarm
iarm/arm_instructions/memory.py
Memory.STM
def STM(self, params): """ STM Ra!, {RLoList} Store multiple registers into memory """ # TODO what registers can be stored? Ra, RLoList = self.get_two_parameters(r'\s*([^\s,]*)!,\s*{(.*)}(.*)', params).split(',') RLoList = RLoList.split(',') RLoList = [i.strip() for i in RLoList] self.check_arguments(low_registers=[Ra] + RLoList) def STM_func(): for i in range(len(RLoList)): for j in range(4): self.memory[self.register[Ra] + 4*i + j] = ((self.register[RLoList[i]] >> (8 * j)) & 0xFF) self.register[Ra] += 4*len(RLoList) return STM_func
python
def STM(self, params): """ STM Ra!, {RLoList} Store multiple registers into memory """ # TODO what registers can be stored? Ra, RLoList = self.get_two_parameters(r'\s*([^\s,]*)!,\s*{(.*)}(.*)', params).split(',') RLoList = RLoList.split(',') RLoList = [i.strip() for i in RLoList] self.check_arguments(low_registers=[Ra] + RLoList) def STM_func(): for i in range(len(RLoList)): for j in range(4): self.memory[self.register[Ra] + 4*i + j] = ((self.register[RLoList[i]] >> (8 * j)) & 0xFF) self.register[Ra] += 4*len(RLoList) return STM_func
[ "def", "STM", "(", "self", ",", "params", ")", ":", "# TODO what registers can be stored?", "Ra", ",", "RLoList", "=", "self", ".", "get_two_parameters", "(", "r'\\s*([^\\s,]*)!,\\s*{(.*)}(.*)'", ",", "params", ")", ".", "split", "(", "','", ")", "RLoList", "=", "RLoList", ".", "split", "(", "','", ")", "RLoList", "=", "[", "i", ".", "strip", "(", ")", "for", "i", "in", "RLoList", "]", "self", ".", "check_arguments", "(", "low_registers", "=", "[", "Ra", "]", "+", "RLoList", ")", "def", "STM_func", "(", ")", ":", "for", "i", "in", "range", "(", "len", "(", "RLoList", ")", ")", ":", "for", "j", "in", "range", "(", "4", ")", ":", "self", ".", "memory", "[", "self", ".", "register", "[", "Ra", "]", "+", "4", "*", "i", "+", "j", "]", "=", "(", "(", "self", ".", "register", "[", "RLoList", "[", "i", "]", "]", ">>", "(", "8", "*", "j", ")", ")", "&", "0xFF", ")", "self", ".", "register", "[", "Ra", "]", "+=", "4", "*", "len", "(", "RLoList", ")", "return", "STM_func" ]
STM Ra!, {RLoList} Store multiple registers into memory
[ "STM", "Ra!", "{", "RLoList", "}" ]
b913c9fd577b793a6bbced78b78a5d8d7cd88de4
https://github.com/DeepHorizons/iarm/blob/b913c9fd577b793a6bbced78b78a5d8d7cd88de4/iarm/arm_instructions/memory.py#L359-L378
train
DeepHorizons/iarm
iarm/arm_instructions/memory.py
Memory.STR
def STR(self, params): """ STR Ra, [Rb, Rc] STR Ra, [Rb, #imm7_4] STR Ra, [SP, #imm10_4] Store Ra into memory as a word Ra, Rb, and Rc must be low registers """ Ra, Rb, Rc = self.get_three_parameters(self.THREE_PARAMETER_WITH_BRACKETS, params) if self.is_immediate(Rc): if Rb == 'SP' or Rb == 'FP': self.check_arguments(low_registers=(Ra,), imm10_4=(Rc,)) else: self.check_arguments(low_registers=(Ra, Rb), imm7_4=(Rc,)) def STR_func(): for i in range(4): self.memory[self.register[Rb] + self.convert_to_integer(Rc[1:]) + i] = ((self.register[Ra] >> (8 * i)) & 0xFF) else: self.check_arguments(low_registers=(Ra, Rb, Rc)) def STR_func(): for i in range(4): self.memory[self.register[Rb] + self.register[Rc] + i] = ((self.register[Ra] >> (8 * i)) & 0xFF) return STR_func
python
def STR(self, params): """ STR Ra, [Rb, Rc] STR Ra, [Rb, #imm7_4] STR Ra, [SP, #imm10_4] Store Ra into memory as a word Ra, Rb, and Rc must be low registers """ Ra, Rb, Rc = self.get_three_parameters(self.THREE_PARAMETER_WITH_BRACKETS, params) if self.is_immediate(Rc): if Rb == 'SP' or Rb == 'FP': self.check_arguments(low_registers=(Ra,), imm10_4=(Rc,)) else: self.check_arguments(low_registers=(Ra, Rb), imm7_4=(Rc,)) def STR_func(): for i in range(4): self.memory[self.register[Rb] + self.convert_to_integer(Rc[1:]) + i] = ((self.register[Ra] >> (8 * i)) & 0xFF) else: self.check_arguments(low_registers=(Ra, Rb, Rc)) def STR_func(): for i in range(4): self.memory[self.register[Rb] + self.register[Rc] + i] = ((self.register[Ra] >> (8 * i)) & 0xFF) return STR_func
[ "def", "STR", "(", "self", ",", "params", ")", ":", "Ra", ",", "Rb", ",", "Rc", "=", "self", ".", "get_three_parameters", "(", "self", ".", "THREE_PARAMETER_WITH_BRACKETS", ",", "params", ")", "if", "self", ".", "is_immediate", "(", "Rc", ")", ":", "if", "Rb", "==", "'SP'", "or", "Rb", "==", "'FP'", ":", "self", ".", "check_arguments", "(", "low_registers", "=", "(", "Ra", ",", ")", ",", "imm10_4", "=", "(", "Rc", ",", ")", ")", "else", ":", "self", ".", "check_arguments", "(", "low_registers", "=", "(", "Ra", ",", "Rb", ")", ",", "imm7_4", "=", "(", "Rc", ",", ")", ")", "def", "STR_func", "(", ")", ":", "for", "i", "in", "range", "(", "4", ")", ":", "self", ".", "memory", "[", "self", ".", "register", "[", "Rb", "]", "+", "self", ".", "convert_to_integer", "(", "Rc", "[", "1", ":", "]", ")", "+", "i", "]", "=", "(", "(", "self", ".", "register", "[", "Ra", "]", ">>", "(", "8", "*", "i", ")", ")", "&", "0xFF", ")", "else", ":", "self", ".", "check_arguments", "(", "low_registers", "=", "(", "Ra", ",", "Rb", ",", "Rc", ")", ")", "def", "STR_func", "(", ")", ":", "for", "i", "in", "range", "(", "4", ")", ":", "self", ".", "memory", "[", "self", ".", "register", "[", "Rb", "]", "+", "self", ".", "register", "[", "Rc", "]", "+", "i", "]", "=", "(", "(", "self", ".", "register", "[", "Ra", "]", ">>", "(", "8", "*", "i", ")", ")", "&", "0xFF", ")", "return", "STR_func" ]
STR Ra, [Rb, Rc] STR Ra, [Rb, #imm7_4] STR Ra, [SP, #imm10_4] Store Ra into memory as a word Ra, Rb, and Rc must be low registers
[ "STR", "Ra", "[", "Rb", "Rc", "]", "STR", "Ra", "[", "Rb", "#imm7_4", "]", "STR", "Ra", "[", "SP", "#imm10_4", "]" ]
b913c9fd577b793a6bbced78b78a5d8d7cd88de4
https://github.com/DeepHorizons/iarm/blob/b913c9fd577b793a6bbced78b78a5d8d7cd88de4/iarm/arm_instructions/memory.py#L380-L407
train
DeepHorizons/iarm
iarm/arm_instructions/memory.py
Memory.STRB
def STRB(self, params): """ STRB Ra, [Rb, Rc] STRB Ra, [Rb, #imm5] Store Ra into memory as a byte Ra, Rb, and Rc must be low registers """ Ra, Rb, Rc = self.get_three_parameters(self.THREE_PARAMETER_WITH_BRACKETS, params) if self.is_immediate(Rc): self.check_arguments(low_registers=(Ra, Rb), imm5=(Rc,)) def STRB_func(): self.memory[self.register[Rb] + self.convert_to_integer(Rc[1:])] = (self.register[Ra] & 0xFF) else: self.check_arguments(low_registers=(Ra, Rb, Rc)) def STRB_func(): self.memory[self.register[Rb] + self.register[Rc]] = (self.register[Ra] & 0xFF) return STRB_func
python
def STRB(self, params): """ STRB Ra, [Rb, Rc] STRB Ra, [Rb, #imm5] Store Ra into memory as a byte Ra, Rb, and Rc must be low registers """ Ra, Rb, Rc = self.get_three_parameters(self.THREE_PARAMETER_WITH_BRACKETS, params) if self.is_immediate(Rc): self.check_arguments(low_registers=(Ra, Rb), imm5=(Rc,)) def STRB_func(): self.memory[self.register[Rb] + self.convert_to_integer(Rc[1:])] = (self.register[Ra] & 0xFF) else: self.check_arguments(low_registers=(Ra, Rb, Rc)) def STRB_func(): self.memory[self.register[Rb] + self.register[Rc]] = (self.register[Ra] & 0xFF) return STRB_func
[ "def", "STRB", "(", "self", ",", "params", ")", ":", "Ra", ",", "Rb", ",", "Rc", "=", "self", ".", "get_three_parameters", "(", "self", ".", "THREE_PARAMETER_WITH_BRACKETS", ",", "params", ")", "if", "self", ".", "is_immediate", "(", "Rc", ")", ":", "self", ".", "check_arguments", "(", "low_registers", "=", "(", "Ra", ",", "Rb", ")", ",", "imm5", "=", "(", "Rc", ",", ")", ")", "def", "STRB_func", "(", ")", ":", "self", ".", "memory", "[", "self", ".", "register", "[", "Rb", "]", "+", "self", ".", "convert_to_integer", "(", "Rc", "[", "1", ":", "]", ")", "]", "=", "(", "self", ".", "register", "[", "Ra", "]", "&", "0xFF", ")", "else", ":", "self", ".", "check_arguments", "(", "low_registers", "=", "(", "Ra", ",", "Rb", ",", "Rc", ")", ")", "def", "STRB_func", "(", ")", ":", "self", ".", "memory", "[", "self", ".", "register", "[", "Rb", "]", "+", "self", ".", "register", "[", "Rc", "]", "]", "=", "(", "self", ".", "register", "[", "Ra", "]", "&", "0xFF", ")", "return", "STRB_func" ]
STRB Ra, [Rb, Rc] STRB Ra, [Rb, #imm5] Store Ra into memory as a byte Ra, Rb, and Rc must be low registers
[ "STRB", "Ra", "[", "Rb", "Rc", "]", "STRB", "Ra", "[", "Rb", "#imm5", "]" ]
b913c9fd577b793a6bbced78b78a5d8d7cd88de4
https://github.com/DeepHorizons/iarm/blob/b913c9fd577b793a6bbced78b78a5d8d7cd88de4/iarm/arm_instructions/memory.py#L409-L430
train
DeepHorizons/iarm
iarm/arm_instructions/memory.py
Memory.STRH
def STRH(self, params): """ STRH Ra, [Rb, Rc] STRH Ra, [Rb, #imm6_2] Store Ra into memory as a half word Ra, Rb, and Rc must be low registers """ Ra, Rb, Rc = self.get_three_parameters(self.THREE_PARAMETER_WITH_BRACKETS, params) if self.is_immediate(Rc): self.check_arguments(low_registers=(Ra, Rb), imm5=(Rc,)) def STRH_func(): for i in range(2): self.memory[self.register[Rb] + self.convert_to_integer(Rc[1:]) + i] = ((self.register[Ra] >> (8 * i)) & 0xFF) else: self.check_arguments(low_registers=(Ra, Rb, Rc)) def STRH_func(): for i in range(2): self.memory[self.register[Rb] + self.register[Rc] + i] = ((self.register[Ra] >> (8 * i)) & 0xFF) return STRH_func
python
def STRH(self, params): """ STRH Ra, [Rb, Rc] STRH Ra, [Rb, #imm6_2] Store Ra into memory as a half word Ra, Rb, and Rc must be low registers """ Ra, Rb, Rc = self.get_three_parameters(self.THREE_PARAMETER_WITH_BRACKETS, params) if self.is_immediate(Rc): self.check_arguments(low_registers=(Ra, Rb), imm5=(Rc,)) def STRH_func(): for i in range(2): self.memory[self.register[Rb] + self.convert_to_integer(Rc[1:]) + i] = ((self.register[Ra] >> (8 * i)) & 0xFF) else: self.check_arguments(low_registers=(Ra, Rb, Rc)) def STRH_func(): for i in range(2): self.memory[self.register[Rb] + self.register[Rc] + i] = ((self.register[Ra] >> (8 * i)) & 0xFF) return STRH_func
[ "def", "STRH", "(", "self", ",", "params", ")", ":", "Ra", ",", "Rb", ",", "Rc", "=", "self", ".", "get_three_parameters", "(", "self", ".", "THREE_PARAMETER_WITH_BRACKETS", ",", "params", ")", "if", "self", ".", "is_immediate", "(", "Rc", ")", ":", "self", ".", "check_arguments", "(", "low_registers", "=", "(", "Ra", ",", "Rb", ")", ",", "imm5", "=", "(", "Rc", ",", ")", ")", "def", "STRH_func", "(", ")", ":", "for", "i", "in", "range", "(", "2", ")", ":", "self", ".", "memory", "[", "self", ".", "register", "[", "Rb", "]", "+", "self", ".", "convert_to_integer", "(", "Rc", "[", "1", ":", "]", ")", "+", "i", "]", "=", "(", "(", "self", ".", "register", "[", "Ra", "]", ">>", "(", "8", "*", "i", ")", ")", "&", "0xFF", ")", "else", ":", "self", ".", "check_arguments", "(", "low_registers", "=", "(", "Ra", ",", "Rb", ",", "Rc", ")", ")", "def", "STRH_func", "(", ")", ":", "for", "i", "in", "range", "(", "2", ")", ":", "self", ".", "memory", "[", "self", ".", "register", "[", "Rb", "]", "+", "self", ".", "register", "[", "Rc", "]", "+", "i", "]", "=", "(", "(", "self", ".", "register", "[", "Ra", "]", ">>", "(", "8", "*", "i", ")", ")", "&", "0xFF", ")", "return", "STRH_func" ]
STRH Ra, [Rb, Rc] STRH Ra, [Rb, #imm6_2] Store Ra into memory as a half word Ra, Rb, and Rc must be low registers
[ "STRH", "Ra", "[", "Rb", "Rc", "]", "STRH", "Ra", "[", "Rb", "#imm6_2", "]" ]
b913c9fd577b793a6bbced78b78a5d8d7cd88de4
https://github.com/DeepHorizons/iarm/blob/b913c9fd577b793a6bbced78b78a5d8d7cd88de4/iarm/arm_instructions/memory.py#L432-L455
train
glomex/gcdt
gcdt/kumo_start_stop.py
_stop_ec2_instances
def _stop_ec2_instances(awsclient, ec2_instances, wait=True): """Helper to stop ec2 instances. By default it waits for instances to stop. :param awsclient: :param ec2_instances: :param wait: waits for instances to stop :return: """ if len(ec2_instances) == 0: return client_ec2 = awsclient.get_client('ec2') # get running instances running_instances = all_pages( client_ec2.describe_instance_status, { 'InstanceIds': ec2_instances, 'Filters': [{ 'Name': 'instance-state-name', 'Values': ['pending', 'running'] }] }, lambda r: [i['InstanceId'] for i in r.get('InstanceStatuses', [])], ) if running_instances: log.info('Stopping EC2 instances: %s', running_instances) client_ec2.stop_instances(InstanceIds=running_instances) if wait: # wait for instances to stop waiter_inst_stopped = client_ec2.get_waiter('instance_stopped') waiter_inst_stopped.wait(InstanceIds=running_instances)
python
def _stop_ec2_instances(awsclient, ec2_instances, wait=True): """Helper to stop ec2 instances. By default it waits for instances to stop. :param awsclient: :param ec2_instances: :param wait: waits for instances to stop :return: """ if len(ec2_instances) == 0: return client_ec2 = awsclient.get_client('ec2') # get running instances running_instances = all_pages( client_ec2.describe_instance_status, { 'InstanceIds': ec2_instances, 'Filters': [{ 'Name': 'instance-state-name', 'Values': ['pending', 'running'] }] }, lambda r: [i['InstanceId'] for i in r.get('InstanceStatuses', [])], ) if running_instances: log.info('Stopping EC2 instances: %s', running_instances) client_ec2.stop_instances(InstanceIds=running_instances) if wait: # wait for instances to stop waiter_inst_stopped = client_ec2.get_waiter('instance_stopped') waiter_inst_stopped.wait(InstanceIds=running_instances)
[ "def", "_stop_ec2_instances", "(", "awsclient", ",", "ec2_instances", ",", "wait", "=", "True", ")", ":", "if", "len", "(", "ec2_instances", ")", "==", "0", ":", "return", "client_ec2", "=", "awsclient", ".", "get_client", "(", "'ec2'", ")", "# get running instances", "running_instances", "=", "all_pages", "(", "client_ec2", ".", "describe_instance_status", ",", "{", "'InstanceIds'", ":", "ec2_instances", ",", "'Filters'", ":", "[", "{", "'Name'", ":", "'instance-state-name'", ",", "'Values'", ":", "[", "'pending'", ",", "'running'", "]", "}", "]", "}", ",", "lambda", "r", ":", "[", "i", "[", "'InstanceId'", "]", "for", "i", "in", "r", ".", "get", "(", "'InstanceStatuses'", ",", "[", "]", ")", "]", ",", ")", "if", "running_instances", ":", "log", ".", "info", "(", "'Stopping EC2 instances: %s'", ",", "running_instances", ")", "client_ec2", ".", "stop_instances", "(", "InstanceIds", "=", "running_instances", ")", "if", "wait", ":", "# wait for instances to stop", "waiter_inst_stopped", "=", "client_ec2", ".", "get_waiter", "(", "'instance_stopped'", ")", "waiter_inst_stopped", ".", "wait", "(", "InstanceIds", "=", "running_instances", ")" ]
Helper to stop ec2 instances. By default it waits for instances to stop. :param awsclient: :param ec2_instances: :param wait: waits for instances to stop :return:
[ "Helper", "to", "stop", "ec2", "instances", ".", "By", "default", "it", "waits", "for", "instances", "to", "stop", "." ]
cd67cf416371337b83cb9ca3f696277125703339
https://github.com/glomex/gcdt/blob/cd67cf416371337b83cb9ca3f696277125703339/gcdt/kumo_start_stop.py#L11-L44
train
glomex/gcdt
gcdt/kumo_start_stop.py
_start_ec2_instances
def _start_ec2_instances(awsclient, ec2_instances, wait=True): """Helper to start ec2 instances :param awsclient: :param ec2_instances: :param wait: waits for instances to start :return: """ if len(ec2_instances) == 0: return client_ec2 = awsclient.get_client('ec2') # get stopped instances stopped_instances = all_pages( client_ec2.describe_instance_status, { 'InstanceIds': ec2_instances, 'Filters': [{ 'Name': 'instance-state-name', 'Values': ['stopping', 'stopped'] }], 'IncludeAllInstances': True }, lambda r: [i['InstanceId'] for i in r.get('InstanceStatuses', [])], ) if stopped_instances: # start all stopped instances log.info('Starting EC2 instances: %s', stopped_instances) client_ec2.start_instances(InstanceIds=stopped_instances) if wait: # wait for instances to come up waiter_inst_running = client_ec2.get_waiter('instance_running') waiter_inst_running.wait(InstanceIds=stopped_instances) # wait for status checks waiter_status_ok = client_ec2.get_waiter('instance_status_ok') waiter_status_ok.wait(InstanceIds=stopped_instances)
python
def _start_ec2_instances(awsclient, ec2_instances, wait=True): """Helper to start ec2 instances :param awsclient: :param ec2_instances: :param wait: waits for instances to start :return: """ if len(ec2_instances) == 0: return client_ec2 = awsclient.get_client('ec2') # get stopped instances stopped_instances = all_pages( client_ec2.describe_instance_status, { 'InstanceIds': ec2_instances, 'Filters': [{ 'Name': 'instance-state-name', 'Values': ['stopping', 'stopped'] }], 'IncludeAllInstances': True }, lambda r: [i['InstanceId'] for i in r.get('InstanceStatuses', [])], ) if stopped_instances: # start all stopped instances log.info('Starting EC2 instances: %s', stopped_instances) client_ec2.start_instances(InstanceIds=stopped_instances) if wait: # wait for instances to come up waiter_inst_running = client_ec2.get_waiter('instance_running') waiter_inst_running.wait(InstanceIds=stopped_instances) # wait for status checks waiter_status_ok = client_ec2.get_waiter('instance_status_ok') waiter_status_ok.wait(InstanceIds=stopped_instances)
[ "def", "_start_ec2_instances", "(", "awsclient", ",", "ec2_instances", ",", "wait", "=", "True", ")", ":", "if", "len", "(", "ec2_instances", ")", "==", "0", ":", "return", "client_ec2", "=", "awsclient", ".", "get_client", "(", "'ec2'", ")", "# get stopped instances", "stopped_instances", "=", "all_pages", "(", "client_ec2", ".", "describe_instance_status", ",", "{", "'InstanceIds'", ":", "ec2_instances", ",", "'Filters'", ":", "[", "{", "'Name'", ":", "'instance-state-name'", ",", "'Values'", ":", "[", "'stopping'", ",", "'stopped'", "]", "}", "]", ",", "'IncludeAllInstances'", ":", "True", "}", ",", "lambda", "r", ":", "[", "i", "[", "'InstanceId'", "]", "for", "i", "in", "r", ".", "get", "(", "'InstanceStatuses'", ",", "[", "]", ")", "]", ",", ")", "if", "stopped_instances", ":", "# start all stopped instances", "log", ".", "info", "(", "'Starting EC2 instances: %s'", ",", "stopped_instances", ")", "client_ec2", ".", "start_instances", "(", "InstanceIds", "=", "stopped_instances", ")", "if", "wait", ":", "# wait for instances to come up", "waiter_inst_running", "=", "client_ec2", ".", "get_waiter", "(", "'instance_running'", ")", "waiter_inst_running", ".", "wait", "(", "InstanceIds", "=", "stopped_instances", ")", "# wait for status checks", "waiter_status_ok", "=", "client_ec2", ".", "get_waiter", "(", "'instance_status_ok'", ")", "waiter_status_ok", ".", "wait", "(", "InstanceIds", "=", "stopped_instances", ")" ]
Helper to start ec2 instances :param awsclient: :param ec2_instances: :param wait: waits for instances to start :return:
[ "Helper", "to", "start", "ec2", "instances" ]
cd67cf416371337b83cb9ca3f696277125703339
https://github.com/glomex/gcdt/blob/cd67cf416371337b83cb9ca3f696277125703339/gcdt/kumo_start_stop.py#L47-L85
train
glomex/gcdt
gcdt/kumo_start_stop.py
_filter_db_instances_by_status
def _filter_db_instances_by_status(awsclient, db_instances, status_list): """helper to select dbinstances. :param awsclient: :param db_instances: :param status_list: :return: list of db_instances that match the filter """ client_rds = awsclient.get_client('rds') db_instances_with_status = [] for db in db_instances: response = client_rds.describe_db_instances( DBInstanceIdentifier=db ) for entry in response.get('DBInstances', []): if entry['DBInstanceStatus'] in status_list: db_instances_with_status.append(db) return db_instances_with_status
python
def _filter_db_instances_by_status(awsclient, db_instances, status_list): """helper to select dbinstances. :param awsclient: :param db_instances: :param status_list: :return: list of db_instances that match the filter """ client_rds = awsclient.get_client('rds') db_instances_with_status = [] for db in db_instances: response = client_rds.describe_db_instances( DBInstanceIdentifier=db ) for entry in response.get('DBInstances', []): if entry['DBInstanceStatus'] in status_list: db_instances_with_status.append(db) return db_instances_with_status
[ "def", "_filter_db_instances_by_status", "(", "awsclient", ",", "db_instances", ",", "status_list", ")", ":", "client_rds", "=", "awsclient", ".", "get_client", "(", "'rds'", ")", "db_instances_with_status", "=", "[", "]", "for", "db", "in", "db_instances", ":", "response", "=", "client_rds", ".", "describe_db_instances", "(", "DBInstanceIdentifier", "=", "db", ")", "for", "entry", "in", "response", ".", "get", "(", "'DBInstances'", ",", "[", "]", ")", ":", "if", "entry", "[", "'DBInstanceStatus'", "]", "in", "status_list", ":", "db_instances_with_status", ".", "append", "(", "db", ")", "return", "db_instances_with_status" ]
helper to select dbinstances. :param awsclient: :param db_instances: :param status_list: :return: list of db_instances that match the filter
[ "helper", "to", "select", "dbinstances", "." ]
cd67cf416371337b83cb9ca3f696277125703339
https://github.com/glomex/gcdt/blob/cd67cf416371337b83cb9ca3f696277125703339/gcdt/kumo_start_stop.py#L88-L107
train
glomex/gcdt
gcdt/kumo_start_stop.py
_stop_ecs_services
def _stop_ecs_services(awsclient, services, template, parameters, wait=True): """Helper to change desiredCount of ECS services to zero. By default it waits for this to complete. Docs here: http://docs.aws.amazon.com/cli/latest/reference/ecs/update-service.html :param awsclient: :param services: :param template: the cloudformation template :param parameters: the parameters used for the cloudformation template :param wait: waits for services to stop :return: """ if len(services) == 0: return client_ecs = awsclient.get_client('ecs') for service in services: log.info('Resize ECS service \'%s\' to desiredCount=0', service['LogicalResourceId']) cluster, desired_count = _get_service_cluster_desired_count( template, parameters, service['LogicalResourceId']) log.debug('cluster: %s' % cluster) response = client_ecs.update_service( cluster=cluster, service=service['PhysicalResourceId'], desiredCount=0 )
python
def _stop_ecs_services(awsclient, services, template, parameters, wait=True): """Helper to change desiredCount of ECS services to zero. By default it waits for this to complete. Docs here: http://docs.aws.amazon.com/cli/latest/reference/ecs/update-service.html :param awsclient: :param services: :param template: the cloudformation template :param parameters: the parameters used for the cloudformation template :param wait: waits for services to stop :return: """ if len(services) == 0: return client_ecs = awsclient.get_client('ecs') for service in services: log.info('Resize ECS service \'%s\' to desiredCount=0', service['LogicalResourceId']) cluster, desired_count = _get_service_cluster_desired_count( template, parameters, service['LogicalResourceId']) log.debug('cluster: %s' % cluster) response = client_ecs.update_service( cluster=cluster, service=service['PhysicalResourceId'], desiredCount=0 )
[ "def", "_stop_ecs_services", "(", "awsclient", ",", "services", ",", "template", ",", "parameters", ",", "wait", "=", "True", ")", ":", "if", "len", "(", "services", ")", "==", "0", ":", "return", "client_ecs", "=", "awsclient", ".", "get_client", "(", "'ecs'", ")", "for", "service", "in", "services", ":", "log", ".", "info", "(", "'Resize ECS service \\'%s\\' to desiredCount=0'", ",", "service", "[", "'LogicalResourceId'", "]", ")", "cluster", ",", "desired_count", "=", "_get_service_cluster_desired_count", "(", "template", ",", "parameters", ",", "service", "[", "'LogicalResourceId'", "]", ")", "log", ".", "debug", "(", "'cluster: %s'", "%", "cluster", ")", "response", "=", "client_ecs", ".", "update_service", "(", "cluster", "=", "cluster", ",", "service", "=", "service", "[", "'PhysicalResourceId'", "]", ",", "desiredCount", "=", "0", ")" ]
Helper to change desiredCount of ECS services to zero. By default it waits for this to complete. Docs here: http://docs.aws.amazon.com/cli/latest/reference/ecs/update-service.html :param awsclient: :param services: :param template: the cloudformation template :param parameters: the parameters used for the cloudformation template :param wait: waits for services to stop :return:
[ "Helper", "to", "change", "desiredCount", "of", "ECS", "services", "to", "zero", ".", "By", "default", "it", "waits", "for", "this", "to", "complete", ".", "Docs", "here", ":", "http", ":", "//", "docs", ".", "aws", ".", "amazon", ".", "com", "/", "cli", "/", "latest", "/", "reference", "/", "ecs", "/", "update", "-", "service", ".", "html" ]
cd67cf416371337b83cb9ca3f696277125703339
https://github.com/glomex/gcdt/blob/cd67cf416371337b83cb9ca3f696277125703339/gcdt/kumo_start_stop.py#L110-L136
train
glomex/gcdt
gcdt/kumo_start_stop.py
stop_stack
def stop_stack(awsclient, stack_name, use_suspend=False): """Stop an existing stack on AWS cloud. :param awsclient: :param stack_name: :param use_suspend: use suspend and resume on the autoscaling group :return: exit_code """ exit_code = 0 # check for DisableStop #disable_stop = conf.get('deployment', {}).get('DisableStop', False) #if disable_stop: # log.warn('\'DisableStop\' is set - nothing to do!') #else: if not stack_exists(awsclient, stack_name): log.warn('Stack \'%s\' not deployed - nothing to do!', stack_name) else: client_cfn = awsclient.get_client('cloudformation') client_autoscaling = awsclient.get_client('autoscaling') client_rds = awsclient.get_client('rds') client_ec2 = awsclient.get_client('ec2') resources = all_pages( client_cfn.list_stack_resources, { 'StackName': stack_name }, lambda r: r['StackResourceSummaries'] ) autoscaling_groups = [ r for r in resources if r['ResourceType'] == 'AWS::AutoScaling::AutoScalingGroup' ] # lookup all types of scaling processes # [Launch, Terminate, HealthCheck, ReplaceUnhealthy, AZRebalance # AlarmNotification, ScheduledActions, AddToLoadBalancer] response = client_autoscaling.describe_scaling_process_types() scaling_process_types = [t['ProcessName'] for t in response.get('Processes', [])] for asg in autoscaling_groups: # find instances in autoscaling group ec2_instances = all_pages( client_autoscaling.describe_auto_scaling_instances, {}, lambda r: [i['InstanceId'] for i in r.get('AutoScalingInstances', []) if i['AutoScalingGroupName'] == asg['PhysicalResourceId']], ) if use_suspend: # alternative implementation to speed up start # only problem is that instances must survive stop & start # suspend all autoscaling processes log.info('Suspending all autoscaling processes for \'%s\'', asg['LogicalResourceId']) response = client_autoscaling.suspend_processes( AutoScalingGroupName=asg['PhysicalResourceId'], ScalingProcesses=scaling_process_types ) _stop_ec2_instances(awsclient, ec2_instances) else: # resize autoscaling group (min, max = 0) log.info('Resize autoscaling group \'%s\' to minSize=0, maxSize=0', asg['LogicalResourceId']) response = client_autoscaling.update_auto_scaling_group( AutoScalingGroupName=asg['PhysicalResourceId'], MinSize=0, MaxSize=0 ) if ec2_instances: running_instances = all_pages( client_ec2.describe_instance_status, { 'InstanceIds': ec2_instances, 'Filters': [{ 'Name': 'instance-state-name', 'Values': ['pending', 'running'] }] }, lambda r: [i['InstanceId'] for i in r.get('InstanceStatuses', [])], ) if running_instances: # wait for instances to terminate waiter_inst_terminated = client_ec2.get_waiter('instance_terminated') waiter_inst_terminated.wait(InstanceIds=running_instances) # setting ECS desiredCount to zero services = [ r for r in resources if r['ResourceType'] == 'AWS::ECS::Service' ] if services: template, parameters = _get_template_parameters(awsclient, stack_name) _stop_ecs_services(awsclient, services, template, parameters) # stopping ec2 instances instances = [ r['PhysicalResourceId'] for r in resources if r['ResourceType'] == 'AWS::EC2::Instance' ] _stop_ec2_instances(awsclient, instances) # stopping db instances db_instances = [ r['PhysicalResourceId'] for r in resources if r['ResourceType'] == 'AWS::RDS::DBInstance' ] running_db_instances = _filter_db_instances_by_status( awsclient, db_instances, ['available'] ) for db in running_db_instances: log.info('Stopping RDS instance \'%s\'', db) client_rds.stop_db_instance(DBInstanceIdentifier=db) return exit_code
python
def stop_stack(awsclient, stack_name, use_suspend=False): """Stop an existing stack on AWS cloud. :param awsclient: :param stack_name: :param use_suspend: use suspend and resume on the autoscaling group :return: exit_code """ exit_code = 0 # check for DisableStop #disable_stop = conf.get('deployment', {}).get('DisableStop', False) #if disable_stop: # log.warn('\'DisableStop\' is set - nothing to do!') #else: if not stack_exists(awsclient, stack_name): log.warn('Stack \'%s\' not deployed - nothing to do!', stack_name) else: client_cfn = awsclient.get_client('cloudformation') client_autoscaling = awsclient.get_client('autoscaling') client_rds = awsclient.get_client('rds') client_ec2 = awsclient.get_client('ec2') resources = all_pages( client_cfn.list_stack_resources, { 'StackName': stack_name }, lambda r: r['StackResourceSummaries'] ) autoscaling_groups = [ r for r in resources if r['ResourceType'] == 'AWS::AutoScaling::AutoScalingGroup' ] # lookup all types of scaling processes # [Launch, Terminate, HealthCheck, ReplaceUnhealthy, AZRebalance # AlarmNotification, ScheduledActions, AddToLoadBalancer] response = client_autoscaling.describe_scaling_process_types() scaling_process_types = [t['ProcessName'] for t in response.get('Processes', [])] for asg in autoscaling_groups: # find instances in autoscaling group ec2_instances = all_pages( client_autoscaling.describe_auto_scaling_instances, {}, lambda r: [i['InstanceId'] for i in r.get('AutoScalingInstances', []) if i['AutoScalingGroupName'] == asg['PhysicalResourceId']], ) if use_suspend: # alternative implementation to speed up start # only problem is that instances must survive stop & start # suspend all autoscaling processes log.info('Suspending all autoscaling processes for \'%s\'', asg['LogicalResourceId']) response = client_autoscaling.suspend_processes( AutoScalingGroupName=asg['PhysicalResourceId'], ScalingProcesses=scaling_process_types ) _stop_ec2_instances(awsclient, ec2_instances) else: # resize autoscaling group (min, max = 0) log.info('Resize autoscaling group \'%s\' to minSize=0, maxSize=0', asg['LogicalResourceId']) response = client_autoscaling.update_auto_scaling_group( AutoScalingGroupName=asg['PhysicalResourceId'], MinSize=0, MaxSize=0 ) if ec2_instances: running_instances = all_pages( client_ec2.describe_instance_status, { 'InstanceIds': ec2_instances, 'Filters': [{ 'Name': 'instance-state-name', 'Values': ['pending', 'running'] }] }, lambda r: [i['InstanceId'] for i in r.get('InstanceStatuses', [])], ) if running_instances: # wait for instances to terminate waiter_inst_terminated = client_ec2.get_waiter('instance_terminated') waiter_inst_terminated.wait(InstanceIds=running_instances) # setting ECS desiredCount to zero services = [ r for r in resources if r['ResourceType'] == 'AWS::ECS::Service' ] if services: template, parameters = _get_template_parameters(awsclient, stack_name) _stop_ecs_services(awsclient, services, template, parameters) # stopping ec2 instances instances = [ r['PhysicalResourceId'] for r in resources if r['ResourceType'] == 'AWS::EC2::Instance' ] _stop_ec2_instances(awsclient, instances) # stopping db instances db_instances = [ r['PhysicalResourceId'] for r in resources if r['ResourceType'] == 'AWS::RDS::DBInstance' ] running_db_instances = _filter_db_instances_by_status( awsclient, db_instances, ['available'] ) for db in running_db_instances: log.info('Stopping RDS instance \'%s\'', db) client_rds.stop_db_instance(DBInstanceIdentifier=db) return exit_code
[ "def", "stop_stack", "(", "awsclient", ",", "stack_name", ",", "use_suspend", "=", "False", ")", ":", "exit_code", "=", "0", "# check for DisableStop", "#disable_stop = conf.get('deployment', {}).get('DisableStop', False)", "#if disable_stop:", "# log.warn('\\'DisableStop\\' is set - nothing to do!')", "#else:", "if", "not", "stack_exists", "(", "awsclient", ",", "stack_name", ")", ":", "log", ".", "warn", "(", "'Stack \\'%s\\' not deployed - nothing to do!'", ",", "stack_name", ")", "else", ":", "client_cfn", "=", "awsclient", ".", "get_client", "(", "'cloudformation'", ")", "client_autoscaling", "=", "awsclient", ".", "get_client", "(", "'autoscaling'", ")", "client_rds", "=", "awsclient", ".", "get_client", "(", "'rds'", ")", "client_ec2", "=", "awsclient", ".", "get_client", "(", "'ec2'", ")", "resources", "=", "all_pages", "(", "client_cfn", ".", "list_stack_resources", ",", "{", "'StackName'", ":", "stack_name", "}", ",", "lambda", "r", ":", "r", "[", "'StackResourceSummaries'", "]", ")", "autoscaling_groups", "=", "[", "r", "for", "r", "in", "resources", "if", "r", "[", "'ResourceType'", "]", "==", "'AWS::AutoScaling::AutoScalingGroup'", "]", "# lookup all types of scaling processes", "# [Launch, Terminate, HealthCheck, ReplaceUnhealthy, AZRebalance", "# AlarmNotification, ScheduledActions, AddToLoadBalancer]", "response", "=", "client_autoscaling", ".", "describe_scaling_process_types", "(", ")", "scaling_process_types", "=", "[", "t", "[", "'ProcessName'", "]", "for", "t", "in", "response", ".", "get", "(", "'Processes'", ",", "[", "]", ")", "]", "for", "asg", "in", "autoscaling_groups", ":", "# find instances in autoscaling group", "ec2_instances", "=", "all_pages", "(", "client_autoscaling", ".", "describe_auto_scaling_instances", ",", "{", "}", ",", "lambda", "r", ":", "[", "i", "[", "'InstanceId'", "]", "for", "i", "in", "r", ".", "get", "(", "'AutoScalingInstances'", ",", "[", "]", ")", "if", "i", "[", "'AutoScalingGroupName'", "]", "==", "asg", "[", "'PhysicalResourceId'", "]", "]", ",", ")", "if", "use_suspend", ":", "# alternative implementation to speed up start", "# only problem is that instances must survive stop & start", "# suspend all autoscaling processes", "log", ".", "info", "(", "'Suspending all autoscaling processes for \\'%s\\''", ",", "asg", "[", "'LogicalResourceId'", "]", ")", "response", "=", "client_autoscaling", ".", "suspend_processes", "(", "AutoScalingGroupName", "=", "asg", "[", "'PhysicalResourceId'", "]", ",", "ScalingProcesses", "=", "scaling_process_types", ")", "_stop_ec2_instances", "(", "awsclient", ",", "ec2_instances", ")", "else", ":", "# resize autoscaling group (min, max = 0)", "log", ".", "info", "(", "'Resize autoscaling group \\'%s\\' to minSize=0, maxSize=0'", ",", "asg", "[", "'LogicalResourceId'", "]", ")", "response", "=", "client_autoscaling", ".", "update_auto_scaling_group", "(", "AutoScalingGroupName", "=", "asg", "[", "'PhysicalResourceId'", "]", ",", "MinSize", "=", "0", ",", "MaxSize", "=", "0", ")", "if", "ec2_instances", ":", "running_instances", "=", "all_pages", "(", "client_ec2", ".", "describe_instance_status", ",", "{", "'InstanceIds'", ":", "ec2_instances", ",", "'Filters'", ":", "[", "{", "'Name'", ":", "'instance-state-name'", ",", "'Values'", ":", "[", "'pending'", ",", "'running'", "]", "}", "]", "}", ",", "lambda", "r", ":", "[", "i", "[", "'InstanceId'", "]", "for", "i", "in", "r", ".", "get", "(", "'InstanceStatuses'", ",", "[", "]", ")", "]", ",", ")", "if", "running_instances", ":", "# wait for instances to terminate", "waiter_inst_terminated", "=", "client_ec2", ".", "get_waiter", "(", "'instance_terminated'", ")", "waiter_inst_terminated", ".", "wait", "(", "InstanceIds", "=", "running_instances", ")", "# setting ECS desiredCount to zero", "services", "=", "[", "r", "for", "r", "in", "resources", "if", "r", "[", "'ResourceType'", "]", "==", "'AWS::ECS::Service'", "]", "if", "services", ":", "template", ",", "parameters", "=", "_get_template_parameters", "(", "awsclient", ",", "stack_name", ")", "_stop_ecs_services", "(", "awsclient", ",", "services", ",", "template", ",", "parameters", ")", "# stopping ec2 instances", "instances", "=", "[", "r", "[", "'PhysicalResourceId'", "]", "for", "r", "in", "resources", "if", "r", "[", "'ResourceType'", "]", "==", "'AWS::EC2::Instance'", "]", "_stop_ec2_instances", "(", "awsclient", ",", "instances", ")", "# stopping db instances", "db_instances", "=", "[", "r", "[", "'PhysicalResourceId'", "]", "for", "r", "in", "resources", "if", "r", "[", "'ResourceType'", "]", "==", "'AWS::RDS::DBInstance'", "]", "running_db_instances", "=", "_filter_db_instances_by_status", "(", "awsclient", ",", "db_instances", ",", "[", "'available'", "]", ")", "for", "db", "in", "running_db_instances", ":", "log", ".", "info", "(", "'Stopping RDS instance \\'%s\\''", ",", "db", ")", "client_rds", ".", "stop_db_instance", "(", "DBInstanceIdentifier", "=", "db", ")", "return", "exit_code" ]
Stop an existing stack on AWS cloud. :param awsclient: :param stack_name: :param use_suspend: use suspend and resume on the autoscaling group :return: exit_code
[ "Stop", "an", "existing", "stack", "on", "AWS", "cloud", "." ]
cd67cf416371337b83cb9ca3f696277125703339
https://github.com/glomex/gcdt/blob/cd67cf416371337b83cb9ca3f696277125703339/gcdt/kumo_start_stop.py#L169-L284
train
glomex/gcdt
gcdt/kumo_start_stop.py
_get_autoscaling_min_max
def _get_autoscaling_min_max(template, parameters, asg_name): """Helper to extract the configured MinSize, MaxSize attributes from the template. :param template: cloudformation template (json) :param parameters: list of {'ParameterKey': 'x1', 'ParameterValue': 'y1'} :param asg_name: logical resource name of the autoscaling group :return: MinSize, MaxSize """ params = {e['ParameterKey']: e['ParameterValue'] for e in parameters} asg = template.get('Resources', {}).get(asg_name, None) if asg: assert asg['Type'] == 'AWS::AutoScaling::AutoScalingGroup' min = asg.get('Properties', {}).get('MinSize', None) max = asg.get('Properties', {}).get('MaxSize', None) if 'Ref' in min: min = params.get(min['Ref'], None) if 'Ref' in max: max = params.get(max['Ref'], None) if min and max: return int(min), int(max)
python
def _get_autoscaling_min_max(template, parameters, asg_name): """Helper to extract the configured MinSize, MaxSize attributes from the template. :param template: cloudformation template (json) :param parameters: list of {'ParameterKey': 'x1', 'ParameterValue': 'y1'} :param asg_name: logical resource name of the autoscaling group :return: MinSize, MaxSize """ params = {e['ParameterKey']: e['ParameterValue'] for e in parameters} asg = template.get('Resources', {}).get(asg_name, None) if asg: assert asg['Type'] == 'AWS::AutoScaling::AutoScalingGroup' min = asg.get('Properties', {}).get('MinSize', None) max = asg.get('Properties', {}).get('MaxSize', None) if 'Ref' in min: min = params.get(min['Ref'], None) if 'Ref' in max: max = params.get(max['Ref'], None) if min and max: return int(min), int(max)
[ "def", "_get_autoscaling_min_max", "(", "template", ",", "parameters", ",", "asg_name", ")", ":", "params", "=", "{", "e", "[", "'ParameterKey'", "]", ":", "e", "[", "'ParameterValue'", "]", "for", "e", "in", "parameters", "}", "asg", "=", "template", ".", "get", "(", "'Resources'", ",", "{", "}", ")", ".", "get", "(", "asg_name", ",", "None", ")", "if", "asg", ":", "assert", "asg", "[", "'Type'", "]", "==", "'AWS::AutoScaling::AutoScalingGroup'", "min", "=", "asg", ".", "get", "(", "'Properties'", ",", "{", "}", ")", ".", "get", "(", "'MinSize'", ",", "None", ")", "max", "=", "asg", ".", "get", "(", "'Properties'", ",", "{", "}", ")", ".", "get", "(", "'MaxSize'", ",", "None", ")", "if", "'Ref'", "in", "min", ":", "min", "=", "params", ".", "get", "(", "min", "[", "'Ref'", "]", ",", "None", ")", "if", "'Ref'", "in", "max", ":", "max", "=", "params", ".", "get", "(", "max", "[", "'Ref'", "]", ",", "None", ")", "if", "min", "and", "max", ":", "return", "int", "(", "min", ")", ",", "int", "(", "max", ")" ]
Helper to extract the configured MinSize, MaxSize attributes from the template. :param template: cloudformation template (json) :param parameters: list of {'ParameterKey': 'x1', 'ParameterValue': 'y1'} :param asg_name: logical resource name of the autoscaling group :return: MinSize, MaxSize
[ "Helper", "to", "extract", "the", "configured", "MinSize", "MaxSize", "attributes", "from", "the", "template", "." ]
cd67cf416371337b83cb9ca3f696277125703339
https://github.com/glomex/gcdt/blob/cd67cf416371337b83cb9ca3f696277125703339/gcdt/kumo_start_stop.py#L287-L307
train
glomex/gcdt
gcdt/kumo_start_stop.py
_get_service_cluster_desired_count
def _get_service_cluster_desired_count(template, parameters, service_name): """Helper to extract the configured desiredCount attribute from the template. :param template: cloudformation template (json) :param parameters: list of {'ParameterKey': 'x1', 'ParameterValue': 'y1'} :param service_name: logical resource name of the ECS service :return: cluster, desiredCount """ params = {e['ParameterKey']: e['ParameterValue'] for e in parameters} service = template.get('Resources', {}).get(service_name, None) if service: assert service['Type'] == 'AWS::ECS::Service' cluster = service.get('Properties', {}).get('Cluster', None) desired_count = service.get('Properties', {}).get('DesiredCount', None) if 'Ref' in cluster: cluster = params.get(cluster['Ref'], None) if not isinstance(desired_count, int) and 'Ref' in desired_count: desired_count = params.get(desired_count['Ref'], None) return cluster, int(desired_count)
python
def _get_service_cluster_desired_count(template, parameters, service_name): """Helper to extract the configured desiredCount attribute from the template. :param template: cloudformation template (json) :param parameters: list of {'ParameterKey': 'x1', 'ParameterValue': 'y1'} :param service_name: logical resource name of the ECS service :return: cluster, desiredCount """ params = {e['ParameterKey']: e['ParameterValue'] for e in parameters} service = template.get('Resources', {}).get(service_name, None) if service: assert service['Type'] == 'AWS::ECS::Service' cluster = service.get('Properties', {}).get('Cluster', None) desired_count = service.get('Properties', {}).get('DesiredCount', None) if 'Ref' in cluster: cluster = params.get(cluster['Ref'], None) if not isinstance(desired_count, int) and 'Ref' in desired_count: desired_count = params.get(desired_count['Ref'], None) return cluster, int(desired_count)
[ "def", "_get_service_cluster_desired_count", "(", "template", ",", "parameters", ",", "service_name", ")", ":", "params", "=", "{", "e", "[", "'ParameterKey'", "]", ":", "e", "[", "'ParameterValue'", "]", "for", "e", "in", "parameters", "}", "service", "=", "template", ".", "get", "(", "'Resources'", ",", "{", "}", ")", ".", "get", "(", "service_name", ",", "None", ")", "if", "service", ":", "assert", "service", "[", "'Type'", "]", "==", "'AWS::ECS::Service'", "cluster", "=", "service", ".", "get", "(", "'Properties'", ",", "{", "}", ")", ".", "get", "(", "'Cluster'", ",", "None", ")", "desired_count", "=", "service", ".", "get", "(", "'Properties'", ",", "{", "}", ")", ".", "get", "(", "'DesiredCount'", ",", "None", ")", "if", "'Ref'", "in", "cluster", ":", "cluster", "=", "params", ".", "get", "(", "cluster", "[", "'Ref'", "]", ",", "None", ")", "if", "not", "isinstance", "(", "desired_count", ",", "int", ")", "and", "'Ref'", "in", "desired_count", ":", "desired_count", "=", "params", ".", "get", "(", "desired_count", "[", "'Ref'", "]", ",", "None", ")", "return", "cluster", ",", "int", "(", "desired_count", ")" ]
Helper to extract the configured desiredCount attribute from the template. :param template: cloudformation template (json) :param parameters: list of {'ParameterKey': 'x1', 'ParameterValue': 'y1'} :param service_name: logical resource name of the ECS service :return: cluster, desiredCount
[ "Helper", "to", "extract", "the", "configured", "desiredCount", "attribute", "from", "the", "template", "." ]
cd67cf416371337b83cb9ca3f696277125703339
https://github.com/glomex/gcdt/blob/cd67cf416371337b83cb9ca3f696277125703339/gcdt/kumo_start_stop.py#L310-L329
train
glomex/gcdt
gcdt/kumo_start_stop.py
start_stack
def start_stack(awsclient, stack_name, use_suspend=False): """Start an existing stack on AWS cloud. :param awsclient: :param stack_name: :param use_suspend: use suspend and resume on the autoscaling group :return: exit_code """ exit_code = 0 # check for DisableStop #disable_stop = conf.get('deployment', {}).get('DisableStop', False) #if disable_stop: # log.warn('\'DisableStop\' is set - nothing to do!') #else: if not stack_exists(awsclient, stack_name): log.warn('Stack \'%s\' not deployed - nothing to do!', stack_name) else: client_cfn = awsclient.get_client('cloudformation') client_autoscaling = awsclient.get_client('autoscaling') client_rds = awsclient.get_client('rds') resources = all_pages( client_cfn.list_stack_resources, { 'StackName': stack_name }, lambda r: r['StackResourceSummaries'] ) autoscaling_groups = [ r for r in resources if r['ResourceType'] == 'AWS::AutoScaling::AutoScalingGroup' ] # lookup all types of scaling processes # [Launch, Terminate, HealthCheck, ReplaceUnhealthy, AZRebalance # AlarmNotification, ScheduledActions, AddToLoadBalancer] response = client_autoscaling.describe_scaling_process_types() scaling_process_types = [t['ProcessName'] for t in response.get('Processes', [])] # starting db instances db_instances = [ r['PhysicalResourceId'] for r in resources if r['ResourceType'] == 'AWS::RDS::DBInstance' ] stopped_db_instances = _filter_db_instances_by_status( awsclient, db_instances, ['stopped'] ) for db in stopped_db_instances: log.info('Starting RDS instance \'%s\'', db) client_rds.start_db_instance(DBInstanceIdentifier=db) # wait for db instances to become available for db in stopped_db_instances: waiter_db_available = client_rds.get_waiter('db_instance_available') waiter_db_available.wait(DBInstanceIdentifier=db) # starting ec2 instances instances = [ r['PhysicalResourceId'] for r in resources if r['ResourceType'] == 'AWS::EC2::Instance' ] _start_ec2_instances(awsclient, instances) services = [ r for r in resources if r['ResourceType'] == 'AWS::ECS::Service' ] if (autoscaling_groups and not use_suspend) or services: template, parameters = _get_template_parameters(awsclient, stack_name) # setting ECS desiredCount back if services: _start_ecs_services(awsclient, services, template, parameters) for asg in autoscaling_groups: if use_suspend: # alternative implementation to speed up start # only problem is that instances must survive stop & start # find instances in autoscaling group instances = all_pages( client_autoscaling.describe_auto_scaling_instances, {}, lambda r: [i['InstanceId'] for i in r.get('AutoScalingInstances', []) if i['AutoScalingGroupName'] == asg['PhysicalResourceId']], ) _start_ec2_instances(awsclient, instances) # resume all autoscaling processes log.info('Resuming all autoscaling processes for \'%s\'', asg['LogicalResourceId']) response = client_autoscaling.resume_processes( AutoScalingGroupName=asg['PhysicalResourceId'], ScalingProcesses=scaling_process_types ) else: # resize autoscaling group back to its original values log.info('Resize autoscaling group \'%s\' back to original values', asg['LogicalResourceId']) min, max = _get_autoscaling_min_max( template, parameters, asg['LogicalResourceId']) response = client_autoscaling.update_auto_scaling_group( AutoScalingGroupName=asg['PhysicalResourceId'], MinSize=min, MaxSize=max ) return exit_code
python
def start_stack(awsclient, stack_name, use_suspend=False): """Start an existing stack on AWS cloud. :param awsclient: :param stack_name: :param use_suspend: use suspend and resume on the autoscaling group :return: exit_code """ exit_code = 0 # check for DisableStop #disable_stop = conf.get('deployment', {}).get('DisableStop', False) #if disable_stop: # log.warn('\'DisableStop\' is set - nothing to do!') #else: if not stack_exists(awsclient, stack_name): log.warn('Stack \'%s\' not deployed - nothing to do!', stack_name) else: client_cfn = awsclient.get_client('cloudformation') client_autoscaling = awsclient.get_client('autoscaling') client_rds = awsclient.get_client('rds') resources = all_pages( client_cfn.list_stack_resources, { 'StackName': stack_name }, lambda r: r['StackResourceSummaries'] ) autoscaling_groups = [ r for r in resources if r['ResourceType'] == 'AWS::AutoScaling::AutoScalingGroup' ] # lookup all types of scaling processes # [Launch, Terminate, HealthCheck, ReplaceUnhealthy, AZRebalance # AlarmNotification, ScheduledActions, AddToLoadBalancer] response = client_autoscaling.describe_scaling_process_types() scaling_process_types = [t['ProcessName'] for t in response.get('Processes', [])] # starting db instances db_instances = [ r['PhysicalResourceId'] for r in resources if r['ResourceType'] == 'AWS::RDS::DBInstance' ] stopped_db_instances = _filter_db_instances_by_status( awsclient, db_instances, ['stopped'] ) for db in stopped_db_instances: log.info('Starting RDS instance \'%s\'', db) client_rds.start_db_instance(DBInstanceIdentifier=db) # wait for db instances to become available for db in stopped_db_instances: waiter_db_available = client_rds.get_waiter('db_instance_available') waiter_db_available.wait(DBInstanceIdentifier=db) # starting ec2 instances instances = [ r['PhysicalResourceId'] for r in resources if r['ResourceType'] == 'AWS::EC2::Instance' ] _start_ec2_instances(awsclient, instances) services = [ r for r in resources if r['ResourceType'] == 'AWS::ECS::Service' ] if (autoscaling_groups and not use_suspend) or services: template, parameters = _get_template_parameters(awsclient, stack_name) # setting ECS desiredCount back if services: _start_ecs_services(awsclient, services, template, parameters) for asg in autoscaling_groups: if use_suspend: # alternative implementation to speed up start # only problem is that instances must survive stop & start # find instances in autoscaling group instances = all_pages( client_autoscaling.describe_auto_scaling_instances, {}, lambda r: [i['InstanceId'] for i in r.get('AutoScalingInstances', []) if i['AutoScalingGroupName'] == asg['PhysicalResourceId']], ) _start_ec2_instances(awsclient, instances) # resume all autoscaling processes log.info('Resuming all autoscaling processes for \'%s\'', asg['LogicalResourceId']) response = client_autoscaling.resume_processes( AutoScalingGroupName=asg['PhysicalResourceId'], ScalingProcesses=scaling_process_types ) else: # resize autoscaling group back to its original values log.info('Resize autoscaling group \'%s\' back to original values', asg['LogicalResourceId']) min, max = _get_autoscaling_min_max( template, parameters, asg['LogicalResourceId']) response = client_autoscaling.update_auto_scaling_group( AutoScalingGroupName=asg['PhysicalResourceId'], MinSize=min, MaxSize=max ) return exit_code
[ "def", "start_stack", "(", "awsclient", ",", "stack_name", ",", "use_suspend", "=", "False", ")", ":", "exit_code", "=", "0", "# check for DisableStop", "#disable_stop = conf.get('deployment', {}).get('DisableStop', False)", "#if disable_stop:", "# log.warn('\\'DisableStop\\' is set - nothing to do!')", "#else:", "if", "not", "stack_exists", "(", "awsclient", ",", "stack_name", ")", ":", "log", ".", "warn", "(", "'Stack \\'%s\\' not deployed - nothing to do!'", ",", "stack_name", ")", "else", ":", "client_cfn", "=", "awsclient", ".", "get_client", "(", "'cloudformation'", ")", "client_autoscaling", "=", "awsclient", ".", "get_client", "(", "'autoscaling'", ")", "client_rds", "=", "awsclient", ".", "get_client", "(", "'rds'", ")", "resources", "=", "all_pages", "(", "client_cfn", ".", "list_stack_resources", ",", "{", "'StackName'", ":", "stack_name", "}", ",", "lambda", "r", ":", "r", "[", "'StackResourceSummaries'", "]", ")", "autoscaling_groups", "=", "[", "r", "for", "r", "in", "resources", "if", "r", "[", "'ResourceType'", "]", "==", "'AWS::AutoScaling::AutoScalingGroup'", "]", "# lookup all types of scaling processes", "# [Launch, Terminate, HealthCheck, ReplaceUnhealthy, AZRebalance", "# AlarmNotification, ScheduledActions, AddToLoadBalancer]", "response", "=", "client_autoscaling", ".", "describe_scaling_process_types", "(", ")", "scaling_process_types", "=", "[", "t", "[", "'ProcessName'", "]", "for", "t", "in", "response", ".", "get", "(", "'Processes'", ",", "[", "]", ")", "]", "# starting db instances", "db_instances", "=", "[", "r", "[", "'PhysicalResourceId'", "]", "for", "r", "in", "resources", "if", "r", "[", "'ResourceType'", "]", "==", "'AWS::RDS::DBInstance'", "]", "stopped_db_instances", "=", "_filter_db_instances_by_status", "(", "awsclient", ",", "db_instances", ",", "[", "'stopped'", "]", ")", "for", "db", "in", "stopped_db_instances", ":", "log", ".", "info", "(", "'Starting RDS instance \\'%s\\''", ",", "db", ")", "client_rds", ".", "start_db_instance", "(", "DBInstanceIdentifier", "=", "db", ")", "# wait for db instances to become available", "for", "db", "in", "stopped_db_instances", ":", "waiter_db_available", "=", "client_rds", ".", "get_waiter", "(", "'db_instance_available'", ")", "waiter_db_available", ".", "wait", "(", "DBInstanceIdentifier", "=", "db", ")", "# starting ec2 instances", "instances", "=", "[", "r", "[", "'PhysicalResourceId'", "]", "for", "r", "in", "resources", "if", "r", "[", "'ResourceType'", "]", "==", "'AWS::EC2::Instance'", "]", "_start_ec2_instances", "(", "awsclient", ",", "instances", ")", "services", "=", "[", "r", "for", "r", "in", "resources", "if", "r", "[", "'ResourceType'", "]", "==", "'AWS::ECS::Service'", "]", "if", "(", "autoscaling_groups", "and", "not", "use_suspend", ")", "or", "services", ":", "template", ",", "parameters", "=", "_get_template_parameters", "(", "awsclient", ",", "stack_name", ")", "# setting ECS desiredCount back", "if", "services", ":", "_start_ecs_services", "(", "awsclient", ",", "services", ",", "template", ",", "parameters", ")", "for", "asg", "in", "autoscaling_groups", ":", "if", "use_suspend", ":", "# alternative implementation to speed up start", "# only problem is that instances must survive stop & start", "# find instances in autoscaling group", "instances", "=", "all_pages", "(", "client_autoscaling", ".", "describe_auto_scaling_instances", ",", "{", "}", ",", "lambda", "r", ":", "[", "i", "[", "'InstanceId'", "]", "for", "i", "in", "r", ".", "get", "(", "'AutoScalingInstances'", ",", "[", "]", ")", "if", "i", "[", "'AutoScalingGroupName'", "]", "==", "asg", "[", "'PhysicalResourceId'", "]", "]", ",", ")", "_start_ec2_instances", "(", "awsclient", ",", "instances", ")", "# resume all autoscaling processes", "log", ".", "info", "(", "'Resuming all autoscaling processes for \\'%s\\''", ",", "asg", "[", "'LogicalResourceId'", "]", ")", "response", "=", "client_autoscaling", ".", "resume_processes", "(", "AutoScalingGroupName", "=", "asg", "[", "'PhysicalResourceId'", "]", ",", "ScalingProcesses", "=", "scaling_process_types", ")", "else", ":", "# resize autoscaling group back to its original values", "log", ".", "info", "(", "'Resize autoscaling group \\'%s\\' back to original values'", ",", "asg", "[", "'LogicalResourceId'", "]", ")", "min", ",", "max", "=", "_get_autoscaling_min_max", "(", "template", ",", "parameters", ",", "asg", "[", "'LogicalResourceId'", "]", ")", "response", "=", "client_autoscaling", ".", "update_auto_scaling_group", "(", "AutoScalingGroupName", "=", "asg", "[", "'PhysicalResourceId'", "]", ",", "MinSize", "=", "min", ",", "MaxSize", "=", "max", ")", "return", "exit_code" ]
Start an existing stack on AWS cloud. :param awsclient: :param stack_name: :param use_suspend: use suspend and resume on the autoscaling group :return: exit_code
[ "Start", "an", "existing", "stack", "on", "AWS", "cloud", "." ]
cd67cf416371337b83cb9ca3f696277125703339
https://github.com/glomex/gcdt/blob/cd67cf416371337b83cb9ca3f696277125703339/gcdt/kumo_start_stop.py#L348-L455
train
gmr/helper
helper/controller.py
Controller.is_running
def is_running(self): """Property method that returns a bool specifying if the process is currently running. This will return true if the state is active, idle or initializing. :rtype: bool """ return self._state in [self.STATE_ACTIVE, self.STATE_IDLE, self.STATE_INITIALIZING]
python
def is_running(self): """Property method that returns a bool specifying if the process is currently running. This will return true if the state is active, idle or initializing. :rtype: bool """ return self._state in [self.STATE_ACTIVE, self.STATE_IDLE, self.STATE_INITIALIZING]
[ "def", "is_running", "(", "self", ")", ":", "return", "self", ".", "_state", "in", "[", "self", ".", "STATE_ACTIVE", ",", "self", ".", "STATE_IDLE", ",", "self", ".", "STATE_INITIALIZING", "]" ]
Property method that returns a bool specifying if the process is currently running. This will return true if the state is active, idle or initializing. :rtype: bool
[ "Property", "method", "that", "returns", "a", "bool", "specifying", "if", "the", "process", "is", "currently", "running", ".", "This", "will", "return", "true", "if", "the", "state", "is", "active", "idle", "or", "initializing", "." ]
fe8e45fc8eabf619429b2940c682c252ee33c082
https://github.com/gmr/helper/blob/fe8e45fc8eabf619429b2940c682c252ee33c082/helper/controller.py#L143-L153
train
gmr/helper
helper/controller.py
Controller.process_signal
def process_signal(self, signum): """Invoked whenever a signal is added to the stack. :param int signum: The signal that was added """ if signum == signal.SIGTERM: LOGGER.info('Received SIGTERM, initiating shutdown') self.stop() elif signum == signal.SIGHUP: LOGGER.info('Received SIGHUP') if self.config.reload(): LOGGER.info('Configuration reloaded') logging.config.dictConfig(self.config.logging) self.on_configuration_reloaded() elif signum == signal.SIGUSR1: self.on_sigusr1() elif signum == signal.SIGUSR2: self.on_sigusr2()
python
def process_signal(self, signum): """Invoked whenever a signal is added to the stack. :param int signum: The signal that was added """ if signum == signal.SIGTERM: LOGGER.info('Received SIGTERM, initiating shutdown') self.stop() elif signum == signal.SIGHUP: LOGGER.info('Received SIGHUP') if self.config.reload(): LOGGER.info('Configuration reloaded') logging.config.dictConfig(self.config.logging) self.on_configuration_reloaded() elif signum == signal.SIGUSR1: self.on_sigusr1() elif signum == signal.SIGUSR2: self.on_sigusr2()
[ "def", "process_signal", "(", "self", ",", "signum", ")", ":", "if", "signum", "==", "signal", ".", "SIGTERM", ":", "LOGGER", ".", "info", "(", "'Received SIGTERM, initiating shutdown'", ")", "self", ".", "stop", "(", ")", "elif", "signum", "==", "signal", ".", "SIGHUP", ":", "LOGGER", ".", "info", "(", "'Received SIGHUP'", ")", "if", "self", ".", "config", ".", "reload", "(", ")", ":", "LOGGER", ".", "info", "(", "'Configuration reloaded'", ")", "logging", ".", "config", ".", "dictConfig", "(", "self", ".", "config", ".", "logging", ")", "self", ".", "on_configuration_reloaded", "(", ")", "elif", "signum", "==", "signal", ".", "SIGUSR1", ":", "self", ".", "on_sigusr1", "(", ")", "elif", "signum", "==", "signal", ".", "SIGUSR2", ":", "self", ".", "on_sigusr2", "(", ")" ]
Invoked whenever a signal is added to the stack. :param int signum: The signal that was added
[ "Invoked", "whenever", "a", "signal", "is", "added", "to", "the", "stack", "." ]
fe8e45fc8eabf619429b2940c682c252ee33c082
https://github.com/gmr/helper/blob/fe8e45fc8eabf619429b2940c682c252ee33c082/helper/controller.py#L225-L243
train
gmr/helper
helper/controller.py
Controller.run
def run(self): """The core method for starting the application. Will setup logging, toggle the runtime state flag, block on loop, then call shutdown. Redefine this method if you intend to use an IO Loop or some other long running process. """ LOGGER.info('%s v%s started', self.APPNAME, self.VERSION) self.setup() while not any([self.is_stopping, self.is_stopped]): self.set_state(self.STATE_SLEEPING) try: signum = self.pending_signals.get(True, self.wake_interval) except queue.Empty: pass else: self.process_signal(signum) if any([self.is_stopping, self.is_stopped]): break self.set_state(self.STATE_ACTIVE) self.process()
python
def run(self): """The core method for starting the application. Will setup logging, toggle the runtime state flag, block on loop, then call shutdown. Redefine this method if you intend to use an IO Loop or some other long running process. """ LOGGER.info('%s v%s started', self.APPNAME, self.VERSION) self.setup() while not any([self.is_stopping, self.is_stopped]): self.set_state(self.STATE_SLEEPING) try: signum = self.pending_signals.get(True, self.wake_interval) except queue.Empty: pass else: self.process_signal(signum) if any([self.is_stopping, self.is_stopped]): break self.set_state(self.STATE_ACTIVE) self.process()
[ "def", "run", "(", "self", ")", ":", "LOGGER", ".", "info", "(", "'%s v%s started'", ",", "self", ".", "APPNAME", ",", "self", ".", "VERSION", ")", "self", ".", "setup", "(", ")", "while", "not", "any", "(", "[", "self", ".", "is_stopping", ",", "self", ".", "is_stopped", "]", ")", ":", "self", ".", "set_state", "(", "self", ".", "STATE_SLEEPING", ")", "try", ":", "signum", "=", "self", ".", "pending_signals", ".", "get", "(", "True", ",", "self", ".", "wake_interval", ")", "except", "queue", ".", "Empty", ":", "pass", "else", ":", "self", ".", "process_signal", "(", "signum", ")", "if", "any", "(", "[", "self", ".", "is_stopping", ",", "self", ".", "is_stopped", "]", ")", ":", "break", "self", ".", "set_state", "(", "self", ".", "STATE_ACTIVE", ")", "self", ".", "process", "(", ")" ]
The core method for starting the application. Will setup logging, toggle the runtime state flag, block on loop, then call shutdown. Redefine this method if you intend to use an IO Loop or some other long running process.
[ "The", "core", "method", "for", "starting", "the", "application", ".", "Will", "setup", "logging", "toggle", "the", "runtime", "state", "flag", "block", "on", "loop", "then", "call", "shutdown", "." ]
fe8e45fc8eabf619429b2940c682c252ee33c082
https://github.com/gmr/helper/blob/fe8e45fc8eabf619429b2940c682c252ee33c082/helper/controller.py#L245-L266
train
gmr/helper
helper/controller.py
Controller.start
def start(self): """Important: Do not extend this method, rather redefine Controller.run """ for signum in [signal.SIGHUP, signal.SIGTERM, signal.SIGUSR1, signal.SIGUSR2]: signal.signal(signum, self._on_signal) self.run()
python
def start(self): """Important: Do not extend this method, rather redefine Controller.run """ for signum in [signal.SIGHUP, signal.SIGTERM, signal.SIGUSR1, signal.SIGUSR2]: signal.signal(signum, self._on_signal) self.run()
[ "def", "start", "(", "self", ")", ":", "for", "signum", "in", "[", "signal", ".", "SIGHUP", ",", "signal", ".", "SIGTERM", ",", "signal", ".", "SIGUSR1", ",", "signal", ".", "SIGUSR2", "]", ":", "signal", ".", "signal", "(", "signum", ",", "self", ".", "_on_signal", ")", "self", ".", "run", "(", ")" ]
Important: Do not extend this method, rather redefine Controller.run
[ "Important", ":" ]
fe8e45fc8eabf619429b2940c682c252ee33c082
https://github.com/gmr/helper/blob/fe8e45fc8eabf619429b2940c682c252ee33c082/helper/controller.py#L268-L277
train
gmr/helper
helper/controller.py
Controller.set_state
def set_state(self, state): """Set the runtime state of the Controller. Use the internal constants to ensure proper state values: - :attr:`Controller.STATE_INITIALIZING` - :attr:`Controller.STATE_ACTIVE` - :attr:`Controller.STATE_IDLE` - :attr:`Controller.STATE_SLEEPING` - :attr:`Controller.STATE_STOP_REQUESTED` - :attr:`Controller.STATE_STOPPING` - :attr:`Controller.STATE_STOPPED` :param int state: The runtime state :raises: ValueError """ if state == self._state: return elif state not in self._STATES.keys(): raise ValueError('Invalid state {}'.format(state)) # Check for invalid transitions if self.is_waiting_to_stop and state not in [self.STATE_STOPPING, self.STATE_STOPPED]: LOGGER.warning('Attempt to set invalid state while waiting to ' 'shutdown: %s ', self._STATES[state]) return elif self.is_stopping and state != self.STATE_STOPPED: LOGGER.warning('Attempt to set invalid post shutdown state: %s', self._STATES[state]) return elif self.is_running and state not in [self.STATE_ACTIVE, self.STATE_IDLE, self.STATE_SLEEPING, self.STATE_STOP_REQUESTED, self.STATE_STOPPING]: LOGGER.warning('Attempt to set invalid post running state: %s', self._STATES[state]) return elif self.is_sleeping and state not in [self.STATE_ACTIVE, self.STATE_IDLE, self.STATE_STOP_REQUESTED, self.STATE_STOPPING]: LOGGER.warning('Attempt to set invalid post sleeping state: %s', self._STATES[state]) return LOGGER.debug('State changed from %s to %s', self._STATES[self._state], self._STATES[state]) self._state = state
python
def set_state(self, state): """Set the runtime state of the Controller. Use the internal constants to ensure proper state values: - :attr:`Controller.STATE_INITIALIZING` - :attr:`Controller.STATE_ACTIVE` - :attr:`Controller.STATE_IDLE` - :attr:`Controller.STATE_SLEEPING` - :attr:`Controller.STATE_STOP_REQUESTED` - :attr:`Controller.STATE_STOPPING` - :attr:`Controller.STATE_STOPPED` :param int state: The runtime state :raises: ValueError """ if state == self._state: return elif state not in self._STATES.keys(): raise ValueError('Invalid state {}'.format(state)) # Check for invalid transitions if self.is_waiting_to_stop and state not in [self.STATE_STOPPING, self.STATE_STOPPED]: LOGGER.warning('Attempt to set invalid state while waiting to ' 'shutdown: %s ', self._STATES[state]) return elif self.is_stopping and state != self.STATE_STOPPED: LOGGER.warning('Attempt to set invalid post shutdown state: %s', self._STATES[state]) return elif self.is_running and state not in [self.STATE_ACTIVE, self.STATE_IDLE, self.STATE_SLEEPING, self.STATE_STOP_REQUESTED, self.STATE_STOPPING]: LOGGER.warning('Attempt to set invalid post running state: %s', self._STATES[state]) return elif self.is_sleeping and state not in [self.STATE_ACTIVE, self.STATE_IDLE, self.STATE_STOP_REQUESTED, self.STATE_STOPPING]: LOGGER.warning('Attempt to set invalid post sleeping state: %s', self._STATES[state]) return LOGGER.debug('State changed from %s to %s', self._STATES[self._state], self._STATES[state]) self._state = state
[ "def", "set_state", "(", "self", ",", "state", ")", ":", "if", "state", "==", "self", ".", "_state", ":", "return", "elif", "state", "not", "in", "self", ".", "_STATES", ".", "keys", "(", ")", ":", "raise", "ValueError", "(", "'Invalid state {}'", ".", "format", "(", "state", ")", ")", "# Check for invalid transitions", "if", "self", ".", "is_waiting_to_stop", "and", "state", "not", "in", "[", "self", ".", "STATE_STOPPING", ",", "self", ".", "STATE_STOPPED", "]", ":", "LOGGER", ".", "warning", "(", "'Attempt to set invalid state while waiting to '", "'shutdown: %s '", ",", "self", ".", "_STATES", "[", "state", "]", ")", "return", "elif", "self", ".", "is_stopping", "and", "state", "!=", "self", ".", "STATE_STOPPED", ":", "LOGGER", ".", "warning", "(", "'Attempt to set invalid post shutdown state: %s'", ",", "self", ".", "_STATES", "[", "state", "]", ")", "return", "elif", "self", ".", "is_running", "and", "state", "not", "in", "[", "self", ".", "STATE_ACTIVE", ",", "self", ".", "STATE_IDLE", ",", "self", ".", "STATE_SLEEPING", ",", "self", ".", "STATE_STOP_REQUESTED", ",", "self", ".", "STATE_STOPPING", "]", ":", "LOGGER", ".", "warning", "(", "'Attempt to set invalid post running state: %s'", ",", "self", ".", "_STATES", "[", "state", "]", ")", "return", "elif", "self", ".", "is_sleeping", "and", "state", "not", "in", "[", "self", ".", "STATE_ACTIVE", ",", "self", ".", "STATE_IDLE", ",", "self", ".", "STATE_STOP_REQUESTED", ",", "self", ".", "STATE_STOPPING", "]", ":", "LOGGER", ".", "warning", "(", "'Attempt to set invalid post sleeping state: %s'", ",", "self", ".", "_STATES", "[", "state", "]", ")", "return", "LOGGER", ".", "debug", "(", "'State changed from %s to %s'", ",", "self", ".", "_STATES", "[", "self", ".", "_state", "]", ",", "self", ".", "_STATES", "[", "state", "]", ")", "self", ".", "_state", "=", "state" ]
Set the runtime state of the Controller. Use the internal constants to ensure proper state values: - :attr:`Controller.STATE_INITIALIZING` - :attr:`Controller.STATE_ACTIVE` - :attr:`Controller.STATE_IDLE` - :attr:`Controller.STATE_SLEEPING` - :attr:`Controller.STATE_STOP_REQUESTED` - :attr:`Controller.STATE_STOPPING` - :attr:`Controller.STATE_STOPPED` :param int state: The runtime state :raises: ValueError
[ "Set", "the", "runtime", "state", "of", "the", "Controller", ".", "Use", "the", "internal", "constants", "to", "ensure", "proper", "state", "values", ":" ]
fe8e45fc8eabf619429b2940c682c252ee33c082
https://github.com/gmr/helper/blob/fe8e45fc8eabf619429b2940c682c252ee33c082/helper/controller.py#L279-L332
train
gmr/helper
helper/controller.py
Controller.stop
def stop(self): """Override to implement shutdown steps.""" LOGGER.info('Attempting to stop the process') self.set_state(self.STATE_STOP_REQUESTED) # Call shutdown for classes to add shutdown steps self.shutdown() # Wait for the current run to finish while self.is_running and self.is_waiting_to_stop: LOGGER.info('Waiting for the process to finish') time.sleep(self.SLEEP_UNIT) # Change the state to shutting down if not self.is_stopping: self.set_state(self.STATE_STOPPING) # Call a method that may be overwritten to cleanly shutdown self.on_shutdown() # Change our state self.set_state(self.STATE_STOPPED)
python
def stop(self): """Override to implement shutdown steps.""" LOGGER.info('Attempting to stop the process') self.set_state(self.STATE_STOP_REQUESTED) # Call shutdown for classes to add shutdown steps self.shutdown() # Wait for the current run to finish while self.is_running and self.is_waiting_to_stop: LOGGER.info('Waiting for the process to finish') time.sleep(self.SLEEP_UNIT) # Change the state to shutting down if not self.is_stopping: self.set_state(self.STATE_STOPPING) # Call a method that may be overwritten to cleanly shutdown self.on_shutdown() # Change our state self.set_state(self.STATE_STOPPED)
[ "def", "stop", "(", "self", ")", ":", "LOGGER", ".", "info", "(", "'Attempting to stop the process'", ")", "self", ".", "set_state", "(", "self", ".", "STATE_STOP_REQUESTED", ")", "# Call shutdown for classes to add shutdown steps", "self", ".", "shutdown", "(", ")", "# Wait for the current run to finish", "while", "self", ".", "is_running", "and", "self", ".", "is_waiting_to_stop", ":", "LOGGER", ".", "info", "(", "'Waiting for the process to finish'", ")", "time", ".", "sleep", "(", "self", ".", "SLEEP_UNIT", ")", "# Change the state to shutting down", "if", "not", "self", ".", "is_stopping", ":", "self", ".", "set_state", "(", "self", ".", "STATE_STOPPING", ")", "# Call a method that may be overwritten to cleanly shutdown", "self", ".", "on_shutdown", "(", ")", "# Change our state", "self", ".", "set_state", "(", "self", ".", "STATE_STOPPED", ")" ]
Override to implement shutdown steps.
[ "Override", "to", "implement", "shutdown", "steps", "." ]
fe8e45fc8eabf619429b2940c682c252ee33c082
https://github.com/gmr/helper/blob/fe8e45fc8eabf619429b2940c682c252ee33c082/helper/controller.py#L342-L363
train
glomex/gcdt
gcdt/servicediscovery.py
parse_ts
def parse_ts(ts): """ parse timestamp. :param ts: timestamp in ISO8601 format :return: tbd!!! """ # ISO8601 = '%Y-%m-%dT%H:%M:%SZ' # ISO8601_MS = '%Y-%m-%dT%H:%M:%S.%fZ' # RFC1123 = '%a, %d %b %Y %H:%M:%S %Z' dt = maya.parse(ts.strip()) return dt.datetime(naive=True)
python
def parse_ts(ts): """ parse timestamp. :param ts: timestamp in ISO8601 format :return: tbd!!! """ # ISO8601 = '%Y-%m-%dT%H:%M:%SZ' # ISO8601_MS = '%Y-%m-%dT%H:%M:%S.%fZ' # RFC1123 = '%a, %d %b %Y %H:%M:%S %Z' dt = maya.parse(ts.strip()) return dt.datetime(naive=True)
[ "def", "parse_ts", "(", "ts", ")", ":", "# ISO8601 = '%Y-%m-%dT%H:%M:%SZ'", "# ISO8601_MS = '%Y-%m-%dT%H:%M:%S.%fZ'", "# RFC1123 = '%a, %d %b %Y %H:%M:%S %Z'", "dt", "=", "maya", ".", "parse", "(", "ts", ".", "strip", "(", ")", ")", "return", "dt", ".", "datetime", "(", "naive", "=", "True", ")" ]
parse timestamp. :param ts: timestamp in ISO8601 format :return: tbd!!!
[ "parse", "timestamp", ".", ":", "param", "ts", ":", "timestamp", "in", "ISO8601", "format", ":", "return", ":", "tbd!!!" ]
cd67cf416371337b83cb9ca3f696277125703339
https://github.com/glomex/gcdt/blob/cd67cf416371337b83cb9ca3f696277125703339/gcdt/servicediscovery.py#L18-L29
train
glomex/gcdt
gcdt/servicediscovery.py
get_outputs_for_stack
def get_outputs_for_stack(awsclient, stack_name): """ Read environment from ENV and mangle it to a (lower case) representation Note: gcdt.servicediscovery get_outputs_for_stack((awsclient, stack_name) is used in many cloudformation.py templates! :param awsclient: :param stack_name: :return: dictionary containing the stack outputs """ client_cf = awsclient.get_client('cloudformation') response = client_cf.describe_stacks(StackName=stack_name) if response['Stacks'] and 'Outputs' in response['Stacks'][0]: result = {} for output in response['Stacks'][0]['Outputs']: result[output['OutputKey']] = output['OutputValue'] return result
python
def get_outputs_for_stack(awsclient, stack_name): """ Read environment from ENV and mangle it to a (lower case) representation Note: gcdt.servicediscovery get_outputs_for_stack((awsclient, stack_name) is used in many cloudformation.py templates! :param awsclient: :param stack_name: :return: dictionary containing the stack outputs """ client_cf = awsclient.get_client('cloudformation') response = client_cf.describe_stacks(StackName=stack_name) if response['Stacks'] and 'Outputs' in response['Stacks'][0]: result = {} for output in response['Stacks'][0]['Outputs']: result[output['OutputKey']] = output['OutputValue'] return result
[ "def", "get_outputs_for_stack", "(", "awsclient", ",", "stack_name", ")", ":", "client_cf", "=", "awsclient", ".", "get_client", "(", "'cloudformation'", ")", "response", "=", "client_cf", ".", "describe_stacks", "(", "StackName", "=", "stack_name", ")", "if", "response", "[", "'Stacks'", "]", "and", "'Outputs'", "in", "response", "[", "'Stacks'", "]", "[", "0", "]", ":", "result", "=", "{", "}", "for", "output", "in", "response", "[", "'Stacks'", "]", "[", "0", "]", "[", "'Outputs'", "]", ":", "result", "[", "output", "[", "'OutputKey'", "]", "]", "=", "output", "[", "'OutputValue'", "]", "return", "result" ]
Read environment from ENV and mangle it to a (lower case) representation Note: gcdt.servicediscovery get_outputs_for_stack((awsclient, stack_name) is used in many cloudformation.py templates! :param awsclient: :param stack_name: :return: dictionary containing the stack outputs
[ "Read", "environment", "from", "ENV", "and", "mangle", "it", "to", "a", "(", "lower", "case", ")", "representation", "Note", ":", "gcdt", ".", "servicediscovery", "get_outputs_for_stack", "((", "awsclient", "stack_name", ")", "is", "used", "in", "many", "cloudformation", ".", "py", "templates!" ]
cd67cf416371337b83cb9ca3f696277125703339
https://github.com/glomex/gcdt/blob/cd67cf416371337b83cb9ca3f696277125703339/gcdt/servicediscovery.py#L33-L49
train
glomex/gcdt
gcdt/servicediscovery.py
get_base_ami
def get_base_ami(awsclient, owners): """ DEPRECATED!!! return the latest version of our base AMI we can't use tags for this, so we have only the name as resource note: this functionality is deprecated since this only works for "old" baseami. """ client_ec2 = awsclient.get_client('ec2') image_filter = [ { 'Name': 'state', 'Values': [ 'available', ] }, ] latest_ts = maya.MayaDT(0).datetime(naive=True) latest_version = StrictVersion('0.0.0') latest_id = None for i in client_ec2.describe_images( Owners=owners, Filters=image_filter )['Images']: m = re.search(r'(Ops_Base-Image)_(\d+.\d+.\d+)_(\d+)$', i['Name']) if m: version = StrictVersion(m.group(2)) #timestamp = m.group(3) creation_date = parse_ts(i['CreationDate']) if creation_date > latest_ts and version >=latest_version: latest_id = i['ImageId'] latest_ts = creation_date latest_version = version return latest_id
python
def get_base_ami(awsclient, owners): """ DEPRECATED!!! return the latest version of our base AMI we can't use tags for this, so we have only the name as resource note: this functionality is deprecated since this only works for "old" baseami. """ client_ec2 = awsclient.get_client('ec2') image_filter = [ { 'Name': 'state', 'Values': [ 'available', ] }, ] latest_ts = maya.MayaDT(0).datetime(naive=True) latest_version = StrictVersion('0.0.0') latest_id = None for i in client_ec2.describe_images( Owners=owners, Filters=image_filter )['Images']: m = re.search(r'(Ops_Base-Image)_(\d+.\d+.\d+)_(\d+)$', i['Name']) if m: version = StrictVersion(m.group(2)) #timestamp = m.group(3) creation_date = parse_ts(i['CreationDate']) if creation_date > latest_ts and version >=latest_version: latest_id = i['ImageId'] latest_ts = creation_date latest_version = version return latest_id
[ "def", "get_base_ami", "(", "awsclient", ",", "owners", ")", ":", "client_ec2", "=", "awsclient", ".", "get_client", "(", "'ec2'", ")", "image_filter", "=", "[", "{", "'Name'", ":", "'state'", ",", "'Values'", ":", "[", "'available'", ",", "]", "}", ",", "]", "latest_ts", "=", "maya", ".", "MayaDT", "(", "0", ")", ".", "datetime", "(", "naive", "=", "True", ")", "latest_version", "=", "StrictVersion", "(", "'0.0.0'", ")", "latest_id", "=", "None", "for", "i", "in", "client_ec2", ".", "describe_images", "(", "Owners", "=", "owners", ",", "Filters", "=", "image_filter", ")", "[", "'Images'", "]", ":", "m", "=", "re", ".", "search", "(", "r'(Ops_Base-Image)_(\\d+.\\d+.\\d+)_(\\d+)$'", ",", "i", "[", "'Name'", "]", ")", "if", "m", ":", "version", "=", "StrictVersion", "(", "m", ".", "group", "(", "2", ")", ")", "#timestamp = m.group(3)", "creation_date", "=", "parse_ts", "(", "i", "[", "'CreationDate'", "]", ")", "if", "creation_date", ">", "latest_ts", "and", "version", ">=", "latest_version", ":", "latest_id", "=", "i", "[", "'ImageId'", "]", "latest_ts", "=", "creation_date", "latest_version", "=", "version", "return", "latest_id" ]
DEPRECATED!!! return the latest version of our base AMI we can't use tags for this, so we have only the name as resource note: this functionality is deprecated since this only works for "old" baseami.
[ "DEPRECATED!!!", "return", "the", "latest", "version", "of", "our", "base", "AMI", "we", "can", "t", "use", "tags", "for", "this", "so", "we", "have", "only", "the", "name", "as", "resource", "note", ":", "this", "functionality", "is", "deprecated", "since", "this", "only", "works", "for", "old", "baseami", "." ]
cd67cf416371337b83cb9ca3f696277125703339
https://github.com/glomex/gcdt/blob/cd67cf416371337b83cb9ca3f696277125703339/gcdt/servicediscovery.py#L71-L107
train
gmr/helper
helper/parser.py
_add_default_arguments
def _add_default_arguments(parser): """Add the default arguments to the parser. :param argparse.ArgumentParser parser: The argument parser """ parser.add_argument('-c', '--config', action='store', dest='config', help='Path to the configuration file') parser.add_argument('-f', '--foreground', action='store_true', dest='foreground', help='Run the application interactively')
python
def _add_default_arguments(parser): """Add the default arguments to the parser. :param argparse.ArgumentParser parser: The argument parser """ parser.add_argument('-c', '--config', action='store', dest='config', help='Path to the configuration file') parser.add_argument('-f', '--foreground', action='store_true', dest='foreground', help='Run the application interactively')
[ "def", "_add_default_arguments", "(", "parser", ")", ":", "parser", ".", "add_argument", "(", "'-c'", ",", "'--config'", ",", "action", "=", "'store'", ",", "dest", "=", "'config'", ",", "help", "=", "'Path to the configuration file'", ")", "parser", ".", "add_argument", "(", "'-f'", ",", "'--foreground'", ",", "action", "=", "'store_true'", ",", "dest", "=", "'foreground'", ",", "help", "=", "'Run the application interactively'", ")" ]
Add the default arguments to the parser. :param argparse.ArgumentParser parser: The argument parser
[ "Add", "the", "default", "arguments", "to", "the", "parser", "." ]
fe8e45fc8eabf619429b2940c682c252ee33c082
https://github.com/gmr/helper/blob/fe8e45fc8eabf619429b2940c682c252ee33c082/helper/parser.py#L62-L71
train
valhallasw/flask-mwoauth
flask_mwoauth/__init__.py
_str
def _str(val): """ Ensures that the val is the default str() type for python2 or 3 """ if str == bytes: if isinstance(val, str): return val else: return str(val) else: if isinstance(val, str): return val else: return str(val, 'ascii')
python
def _str(val): """ Ensures that the val is the default str() type for python2 or 3 """ if str == bytes: if isinstance(val, str): return val else: return str(val) else: if isinstance(val, str): return val else: return str(val, 'ascii')
[ "def", "_str", "(", "val", ")", ":", "if", "str", "==", "bytes", ":", "if", "isinstance", "(", "val", ",", "str", ")", ":", "return", "val", "else", ":", "return", "str", "(", "val", ")", "else", ":", "if", "isinstance", "(", "val", ",", "str", ")", ":", "return", "val", "else", ":", "return", "str", "(", "val", ",", "'ascii'", ")" ]
Ensures that the val is the default str() type for python2 or 3
[ "Ensures", "that", "the", "val", "is", "the", "default", "str", "()", "type", "for", "python2", "or", "3" ]
216aa9c1ead07d99a9e11deb7642e33a70aa59d7
https://github.com/valhallasw/flask-mwoauth/blob/216aa9c1ead07d99a9e11deb7642e33a70aa59d7/flask_mwoauth/__init__.py#L174-L187
train
valhallasw/flask-mwoauth
flask_mwoauth/__init__.py
MWOAuth._prepare_long_request
def _prepare_long_request(self, url, api_query): """ Use requests.Request and requests.PreparedRequest to produce the body (and boundary value) of a multipart/form-data; POST request as detailed in https://www.mediawiki.org/wiki/API:Edit#Large_texts """ partlist = [] for k, v in iteritems(api_query): if k in ('title', 'text', 'summary'): # title, text and summary values in the request # should be utf-8 encoded part = (k, (None, v.encode('utf-8'), 'text/plain; charset=UTF-8', {'Content-Transfer-Encoding': '8bit'} ) ) else: part = (k, (None, v)) partlist.append(part) auth1 = OAuth1( self.consumer_token.key, client_secret=self.consumer_token.secret, resource_owner_key=session['mwoauth_access_token']['key'], resource_owner_secret=session['mwoauth_access_token']['secret']) return Request( url=url, files=partlist, auth=auth1, method="post").prepare()
python
def _prepare_long_request(self, url, api_query): """ Use requests.Request and requests.PreparedRequest to produce the body (and boundary value) of a multipart/form-data; POST request as detailed in https://www.mediawiki.org/wiki/API:Edit#Large_texts """ partlist = [] for k, v in iteritems(api_query): if k in ('title', 'text', 'summary'): # title, text and summary values in the request # should be utf-8 encoded part = (k, (None, v.encode('utf-8'), 'text/plain; charset=UTF-8', {'Content-Transfer-Encoding': '8bit'} ) ) else: part = (k, (None, v)) partlist.append(part) auth1 = OAuth1( self.consumer_token.key, client_secret=self.consumer_token.secret, resource_owner_key=session['mwoauth_access_token']['key'], resource_owner_secret=session['mwoauth_access_token']['secret']) return Request( url=url, files=partlist, auth=auth1, method="post").prepare()
[ "def", "_prepare_long_request", "(", "self", ",", "url", ",", "api_query", ")", ":", "partlist", "=", "[", "]", "for", "k", ",", "v", "in", "iteritems", "(", "api_query", ")", ":", "if", "k", "in", "(", "'title'", ",", "'text'", ",", "'summary'", ")", ":", "# title, text and summary values in the request", "# should be utf-8 encoded", "part", "=", "(", "k", ",", "(", "None", ",", "v", ".", "encode", "(", "'utf-8'", ")", ",", "'text/plain; charset=UTF-8'", ",", "{", "'Content-Transfer-Encoding'", ":", "'8bit'", "}", ")", ")", "else", ":", "part", "=", "(", "k", ",", "(", "None", ",", "v", ")", ")", "partlist", ".", "append", "(", "part", ")", "auth1", "=", "OAuth1", "(", "self", ".", "consumer_token", ".", "key", ",", "client_secret", "=", "self", ".", "consumer_token", ".", "secret", ",", "resource_owner_key", "=", "session", "[", "'mwoauth_access_token'", "]", "[", "'key'", "]", ",", "resource_owner_secret", "=", "session", "[", "'mwoauth_access_token'", "]", "[", "'secret'", "]", ")", "return", "Request", "(", "url", "=", "url", ",", "files", "=", "partlist", ",", "auth", "=", "auth1", ",", "method", "=", "\"post\"", ")", ".", "prepare", "(", ")" ]
Use requests.Request and requests.PreparedRequest to produce the body (and boundary value) of a multipart/form-data; POST request as detailed in https://www.mediawiki.org/wiki/API:Edit#Large_texts
[ "Use", "requests", ".", "Request", "and", "requests", ".", "PreparedRequest", "to", "produce", "the", "body", "(", "and", "boundary", "value", ")", "of", "a", "multipart", "/", "form", "-", "data", ";", "POST", "request", "as", "detailed", "in", "https", ":", "//", "www", ".", "mediawiki", ".", "org", "/", "wiki", "/", "API", ":", "Edit#Large_texts" ]
216aa9c1ead07d99a9e11deb7642e33a70aa59d7
https://github.com/valhallasw/flask-mwoauth/blob/216aa9c1ead07d99a9e11deb7642e33a70aa59d7/flask_mwoauth/__init__.py#L93-L122
train
valhallasw/flask-mwoauth
flask_mwoauth/__init__.py
MWOAuth.request
def request(self, api_query, url=None): """ e.g. {'action': 'query', 'meta': 'userinfo'}. format=json not required function returns a python dict that resembles the api's json response """ api_query['format'] = 'json' if url is not None: api_url = url + "/api.php" else: api_url = self.api_url size = sum([sys.getsizeof(v) for k, v in iteritems(api_query)]) if size > (1024 * 8): # if request is bigger than 8 kB (the limit is somewhat arbitrary, # see https://www.mediawiki.org/wiki/API:Edit#Large_texts) then # transmit as multipart message req = self._prepare_long_request(url=api_url, api_query=api_query) req.send() if self.return_json: return req.response.json() else: return req.response.text else: auth1 = OAuth1( self.consumer_token.key, client_secret=self.consumer_token.secret, resource_owner_key=session['mwoauth_access_token']['key'], resource_owner_secret=session['mwoauth_access_token']['secret']) if self.return_json: return requests.post(api_url, data=api_query, auth=auth1).json() else: return requests.post(api_url, data=api_query, auth=auth1).text
python
def request(self, api_query, url=None): """ e.g. {'action': 'query', 'meta': 'userinfo'}. format=json not required function returns a python dict that resembles the api's json response """ api_query['format'] = 'json' if url is not None: api_url = url + "/api.php" else: api_url = self.api_url size = sum([sys.getsizeof(v) for k, v in iteritems(api_query)]) if size > (1024 * 8): # if request is bigger than 8 kB (the limit is somewhat arbitrary, # see https://www.mediawiki.org/wiki/API:Edit#Large_texts) then # transmit as multipart message req = self._prepare_long_request(url=api_url, api_query=api_query) req.send() if self.return_json: return req.response.json() else: return req.response.text else: auth1 = OAuth1( self.consumer_token.key, client_secret=self.consumer_token.secret, resource_owner_key=session['mwoauth_access_token']['key'], resource_owner_secret=session['mwoauth_access_token']['secret']) if self.return_json: return requests.post(api_url, data=api_query, auth=auth1).json() else: return requests.post(api_url, data=api_query, auth=auth1).text
[ "def", "request", "(", "self", ",", "api_query", ",", "url", "=", "None", ")", ":", "api_query", "[", "'format'", "]", "=", "'json'", "if", "url", "is", "not", "None", ":", "api_url", "=", "url", "+", "\"/api.php\"", "else", ":", "api_url", "=", "self", ".", "api_url", "size", "=", "sum", "(", "[", "sys", ".", "getsizeof", "(", "v", ")", "for", "k", ",", "v", "in", "iteritems", "(", "api_query", ")", "]", ")", "if", "size", ">", "(", "1024", "*", "8", ")", ":", "# if request is bigger than 8 kB (the limit is somewhat arbitrary,", "# see https://www.mediawiki.org/wiki/API:Edit#Large_texts) then", "# transmit as multipart message", "req", "=", "self", ".", "_prepare_long_request", "(", "url", "=", "api_url", ",", "api_query", "=", "api_query", ")", "req", ".", "send", "(", ")", "if", "self", ".", "return_json", ":", "return", "req", ".", "response", ".", "json", "(", ")", "else", ":", "return", "req", ".", "response", ".", "text", "else", ":", "auth1", "=", "OAuth1", "(", "self", ".", "consumer_token", ".", "key", ",", "client_secret", "=", "self", ".", "consumer_token", ".", "secret", ",", "resource_owner_key", "=", "session", "[", "'mwoauth_access_token'", "]", "[", "'key'", "]", ",", "resource_owner_secret", "=", "session", "[", "'mwoauth_access_token'", "]", "[", "'secret'", "]", ")", "if", "self", ".", "return_json", ":", "return", "requests", ".", "post", "(", "api_url", ",", "data", "=", "api_query", ",", "auth", "=", "auth1", ")", ".", "json", "(", ")", "else", ":", "return", "requests", ".", "post", "(", "api_url", ",", "data", "=", "api_query", ",", "auth", "=", "auth1", ")", ".", "text" ]
e.g. {'action': 'query', 'meta': 'userinfo'}. format=json not required function returns a python dict that resembles the api's json response
[ "e", ".", "g", ".", "{", "action", ":", "query", "meta", ":", "userinfo", "}", ".", "format", "=", "json", "not", "required", "function", "returns", "a", "python", "dict", "that", "resembles", "the", "api", "s", "json", "response" ]
216aa9c1ead07d99a9e11deb7642e33a70aa59d7
https://github.com/valhallasw/flask-mwoauth/blob/216aa9c1ead07d99a9e11deb7642e33a70aa59d7/flask_mwoauth/__init__.py#L124-L158
train
CitrineInformatics/pypif
pypif/pif.py
dump
def dump(pif, fp, **kwargs): """ Convert a single Physical Information Object, or a list of such objects, into a JSON-encoded text file. :param pif: Object or list of objects to serialize. :param fp: File-like object supporting .write() method to write the serialized object(s) to. :param kwargs: Any options available to json.dump(). """ return json.dump(pif, fp, cls=PifEncoder, **kwargs)
python
def dump(pif, fp, **kwargs): """ Convert a single Physical Information Object, or a list of such objects, into a JSON-encoded text file. :param pif: Object or list of objects to serialize. :param fp: File-like object supporting .write() method to write the serialized object(s) to. :param kwargs: Any options available to json.dump(). """ return json.dump(pif, fp, cls=PifEncoder, **kwargs)
[ "def", "dump", "(", "pif", ",", "fp", ",", "*", "*", "kwargs", ")", ":", "return", "json", ".", "dump", "(", "pif", ",", "fp", ",", "cls", "=", "PifEncoder", ",", "*", "*", "kwargs", ")" ]
Convert a single Physical Information Object, or a list of such objects, into a JSON-encoded text file. :param pif: Object or list of objects to serialize. :param fp: File-like object supporting .write() method to write the serialized object(s) to. :param kwargs: Any options available to json.dump().
[ "Convert", "a", "single", "Physical", "Information", "Object", "or", "a", "list", "of", "such", "objects", "into", "a", "JSON", "-", "encoded", "text", "file", "." ]
938348a8ff7b10b330770cccaaeb2109922f681b
https://github.com/CitrineInformatics/pypif/blob/938348a8ff7b10b330770cccaaeb2109922f681b/pypif/pif.py#L8-L16
train
CitrineInformatics/pypif
pypif/pif.py
load
def load(fp, class_=None, **kwargs): """ Convert content in a JSON-encoded text file to a Physical Information Object or a list of such objects. :param fp: File-like object supporting .read() method to deserialize from. :param class_: Subclass of :class:`.Pio` to produce, if not unambiguous :param kwargs: Any options available to json.load(). :return: Single object derived from :class:`.Pio` or a list of such object. """ return loado(json.load(fp, **kwargs), class_=class_)
python
def load(fp, class_=None, **kwargs): """ Convert content in a JSON-encoded text file to a Physical Information Object or a list of such objects. :param fp: File-like object supporting .read() method to deserialize from. :param class_: Subclass of :class:`.Pio` to produce, if not unambiguous :param kwargs: Any options available to json.load(). :return: Single object derived from :class:`.Pio` or a list of such object. """ return loado(json.load(fp, **kwargs), class_=class_)
[ "def", "load", "(", "fp", ",", "class_", "=", "None", ",", "*", "*", "kwargs", ")", ":", "return", "loado", "(", "json", ".", "load", "(", "fp", ",", "*", "*", "kwargs", ")", ",", "class_", "=", "class_", ")" ]
Convert content in a JSON-encoded text file to a Physical Information Object or a list of such objects. :param fp: File-like object supporting .read() method to deserialize from. :param class_: Subclass of :class:`.Pio` to produce, if not unambiguous :param kwargs: Any options available to json.load(). :return: Single object derived from :class:`.Pio` or a list of such object.
[ "Convert", "content", "in", "a", "JSON", "-", "encoded", "text", "file", "to", "a", "Physical", "Information", "Object", "or", "a", "list", "of", "such", "objects", "." ]
938348a8ff7b10b330770cccaaeb2109922f681b
https://github.com/CitrineInformatics/pypif/blob/938348a8ff7b10b330770cccaaeb2109922f681b/pypif/pif.py#L29-L38
train
CitrineInformatics/pypif
pypif/pif.py
loads
def loads(s, class_=None, **kwargs): """ Convert content in a JSON-encoded string to a Physical Information Object or a list of such objects. :param s: String to deserialize. :param class_: Subclass of :class:`.Pio` to produce, if not unambiguous :param kwargs: Any options available to json.loads(). :return: Single object derived from :class:`.Pio` or a list of such object. """ return loado(json.loads(s, **kwargs), class_=class_)
python
def loads(s, class_=None, **kwargs): """ Convert content in a JSON-encoded string to a Physical Information Object or a list of such objects. :param s: String to deserialize. :param class_: Subclass of :class:`.Pio` to produce, if not unambiguous :param kwargs: Any options available to json.loads(). :return: Single object derived from :class:`.Pio` or a list of such object. """ return loado(json.loads(s, **kwargs), class_=class_)
[ "def", "loads", "(", "s", ",", "class_", "=", "None", ",", "*", "*", "kwargs", ")", ":", "return", "loado", "(", "json", ".", "loads", "(", "s", ",", "*", "*", "kwargs", ")", ",", "class_", "=", "class_", ")" ]
Convert content in a JSON-encoded string to a Physical Information Object or a list of such objects. :param s: String to deserialize. :param class_: Subclass of :class:`.Pio` to produce, if not unambiguous :param kwargs: Any options available to json.loads(). :return: Single object derived from :class:`.Pio` or a list of such object.
[ "Convert", "content", "in", "a", "JSON", "-", "encoded", "string", "to", "a", "Physical", "Information", "Object", "or", "a", "list", "of", "such", "objects", "." ]
938348a8ff7b10b330770cccaaeb2109922f681b
https://github.com/CitrineInformatics/pypif/blob/938348a8ff7b10b330770cccaaeb2109922f681b/pypif/pif.py#L41-L50
train
CitrineInformatics/pypif
pypif/pif.py
loado
def loado(obj, class_=None): """ Convert a dictionary or a list of dictionaries into a single Physical Information Object or a list of such objects. :param obj: Dictionary or list to convert to Physical Information Objects. :param class_: Subclass of :class:`.Pio` to produce, if not unambiguous :return: Single object derived from :class:`.Pio` or a list of such object. """ if isinstance(obj, list): return [_dict_to_pio(i, class_=class_) for i in obj] elif isinstance(obj, dict): return _dict_to_pio(obj, class_=class_) else: raise ValueError('expecting list or dictionary as outermost structure')
python
def loado(obj, class_=None): """ Convert a dictionary or a list of dictionaries into a single Physical Information Object or a list of such objects. :param obj: Dictionary or list to convert to Physical Information Objects. :param class_: Subclass of :class:`.Pio` to produce, if not unambiguous :return: Single object derived from :class:`.Pio` or a list of such object. """ if isinstance(obj, list): return [_dict_to_pio(i, class_=class_) for i in obj] elif isinstance(obj, dict): return _dict_to_pio(obj, class_=class_) else: raise ValueError('expecting list or dictionary as outermost structure')
[ "def", "loado", "(", "obj", ",", "class_", "=", "None", ")", ":", "if", "isinstance", "(", "obj", ",", "list", ")", ":", "return", "[", "_dict_to_pio", "(", "i", ",", "class_", "=", "class_", ")", "for", "i", "in", "obj", "]", "elif", "isinstance", "(", "obj", ",", "dict", ")", ":", "return", "_dict_to_pio", "(", "obj", ",", "class_", "=", "class_", ")", "else", ":", "raise", "ValueError", "(", "'expecting list or dictionary as outermost structure'", ")" ]
Convert a dictionary or a list of dictionaries into a single Physical Information Object or a list of such objects. :param obj: Dictionary or list to convert to Physical Information Objects. :param class_: Subclass of :class:`.Pio` to produce, if not unambiguous :return: Single object derived from :class:`.Pio` or a list of such object.
[ "Convert", "a", "dictionary", "or", "a", "list", "of", "dictionaries", "into", "a", "single", "Physical", "Information", "Object", "or", "a", "list", "of", "such", "objects", "." ]
938348a8ff7b10b330770cccaaeb2109922f681b
https://github.com/CitrineInformatics/pypif/blob/938348a8ff7b10b330770cccaaeb2109922f681b/pypif/pif.py#L53-L66
train
CitrineInformatics/pypif
pypif/pif.py
_dict_to_pio
def _dict_to_pio(d, class_=None): """ Convert a single dictionary object to a Physical Information Object. :param d: Dictionary to convert. :param class_: Subclass of :class:`.Pio` to produce, if not unambiguous :return: Single object derived from :class:`.Pio`. """ d = keys_to_snake_case(d) if class_: return class_(**d) if 'category' not in d: raise ValueError('Dictionary does not contains a category field: ' + ', '.join(d.keys())) elif d['category'] == 'system': return System(**d) elif d['category'] == 'system.chemical': return ChemicalSystem(**d) elif d['category'] == 'system.chemical.alloy': # Legacy support return Alloy(**d) elif d['category'] == 'system.chemical.alloy.phase': # Legacy support return ChemicalSystem(**d) raise ValueError('Dictionary does not contain a valid top-level category: ' + str(d['category']))
python
def _dict_to_pio(d, class_=None): """ Convert a single dictionary object to a Physical Information Object. :param d: Dictionary to convert. :param class_: Subclass of :class:`.Pio` to produce, if not unambiguous :return: Single object derived from :class:`.Pio`. """ d = keys_to_snake_case(d) if class_: return class_(**d) if 'category' not in d: raise ValueError('Dictionary does not contains a category field: ' + ', '.join(d.keys())) elif d['category'] == 'system': return System(**d) elif d['category'] == 'system.chemical': return ChemicalSystem(**d) elif d['category'] == 'system.chemical.alloy': # Legacy support return Alloy(**d) elif d['category'] == 'system.chemical.alloy.phase': # Legacy support return ChemicalSystem(**d) raise ValueError('Dictionary does not contain a valid top-level category: ' + str(d['category']))
[ "def", "_dict_to_pio", "(", "d", ",", "class_", "=", "None", ")", ":", "d", "=", "keys_to_snake_case", "(", "d", ")", "if", "class_", ":", "return", "class_", "(", "*", "*", "d", ")", "if", "'category'", "not", "in", "d", ":", "raise", "ValueError", "(", "'Dictionary does not contains a category field: '", "+", "', '", ".", "join", "(", "d", ".", "keys", "(", ")", ")", ")", "elif", "d", "[", "'category'", "]", "==", "'system'", ":", "return", "System", "(", "*", "*", "d", ")", "elif", "d", "[", "'category'", "]", "==", "'system.chemical'", ":", "return", "ChemicalSystem", "(", "*", "*", "d", ")", "elif", "d", "[", "'category'", "]", "==", "'system.chemical.alloy'", ":", "# Legacy support", "return", "Alloy", "(", "*", "*", "d", ")", "elif", "d", "[", "'category'", "]", "==", "'system.chemical.alloy.phase'", ":", "# Legacy support", "return", "ChemicalSystem", "(", "*", "*", "d", ")", "raise", "ValueError", "(", "'Dictionary does not contain a valid top-level category: '", "+", "str", "(", "d", "[", "'category'", "]", ")", ")" ]
Convert a single dictionary object to a Physical Information Object. :param d: Dictionary to convert. :param class_: Subclass of :class:`.Pio` to produce, if not unambiguous :return: Single object derived from :class:`.Pio`.
[ "Convert", "a", "single", "dictionary", "object", "to", "a", "Physical", "Information", "Object", "." ]
938348a8ff7b10b330770cccaaeb2109922f681b
https://github.com/CitrineInformatics/pypif/blob/938348a8ff7b10b330770cccaaeb2109922f681b/pypif/pif.py#L69-L90
train
glomex/gcdt
gcdt/gcdt_cmd_dispatcher.py
get_command
def get_command(arguments): """Utility function to extract command from docopt arguments. :param arguments: :return: command """ cmds = list(filter(lambda k: not (k.startswith('-') or k.startswith('<')) and arguments[k], arguments.keys())) if len(cmds) != 1: raise Exception('invalid command line!') return cmds[0]
python
def get_command(arguments): """Utility function to extract command from docopt arguments. :param arguments: :return: command """ cmds = list(filter(lambda k: not (k.startswith('-') or k.startswith('<')) and arguments[k], arguments.keys())) if len(cmds) != 1: raise Exception('invalid command line!') return cmds[0]
[ "def", "get_command", "(", "arguments", ")", ":", "cmds", "=", "list", "(", "filter", "(", "lambda", "k", ":", "not", "(", "k", ".", "startswith", "(", "'-'", ")", "or", "k", ".", "startswith", "(", "'<'", ")", ")", "and", "arguments", "[", "k", "]", ",", "arguments", ".", "keys", "(", ")", ")", ")", "if", "len", "(", "cmds", ")", "!=", "1", ":", "raise", "Exception", "(", "'invalid command line!'", ")", "return", "cmds", "[", "0", "]" ]
Utility function to extract command from docopt arguments. :param arguments: :return: command
[ "Utility", "function", "to", "extract", "command", "from", "docopt", "arguments", "." ]
cd67cf416371337b83cb9ca3f696277125703339
https://github.com/glomex/gcdt/blob/cd67cf416371337b83cb9ca3f696277125703339/gcdt/gcdt_cmd_dispatcher.py#L101-L112
train
glomex/gcdt
gcdt/gcdt_cmd_dispatcher.py
cmd.dispatch
def dispatch(cls, arguments, **kwargs): """Dispatch arguments parsed by docopt to the cmd with matching spec. :param arguments: :param kwargs: :return: exit_code """ # first match wins # spec: all '-' elements must match, all others are False; # '<sth>' elements are converted to call args on order of # appearance # # kwargs are provided to dispatch call and used in func call for spec, func in cls._specs: # if command and arguments.get(command) and match(args): args = [] # specified args in order of appearance options = list(filter(lambda k: k.startswith('-') and (arguments[k] or k in spec), arguments.keys())) cmds = list(filter(lambda k: not (k.startswith('-') or k.startswith('<')) and arguments[k], arguments.keys())) args_spec = list(filter(lambda k: k.startswith('<'), spec)) cmd_spec = list(filter(lambda k: not (k.startswith('-') or k.startswith('<')), spec)) for element in spec: if element.startswith('-'): # element is an option if element in options: args.append(arguments.get(element, False)) options.remove(element) elif element.startswith('<') and \ not arguments.get(element) is False: # element is an argument args.append(arguments.get(element)) if element in args_spec: args_spec.remove(element) else: # element is a command if element in cmds and element in cmd_spec: cmds.remove(element) cmd_spec.remove(element) if options: continue # not all options have been matched if cmds: continue # not all cmds from command line have been matched if args_spec: continue # not all args from spec have been provided if cmd_spec: continue # not all cmds from spec have been provided # all options and cmds matched : call the cmd # TODO leave out all args to deal with "empty" signature exit_code = func(*args, **kwargs) return exit_code # no matching spec found raise Exception('No implementation for spec: %s' % arguments)
python
def dispatch(cls, arguments, **kwargs): """Dispatch arguments parsed by docopt to the cmd with matching spec. :param arguments: :param kwargs: :return: exit_code """ # first match wins # spec: all '-' elements must match, all others are False; # '<sth>' elements are converted to call args on order of # appearance # # kwargs are provided to dispatch call and used in func call for spec, func in cls._specs: # if command and arguments.get(command) and match(args): args = [] # specified args in order of appearance options = list(filter(lambda k: k.startswith('-') and (arguments[k] or k in spec), arguments.keys())) cmds = list(filter(lambda k: not (k.startswith('-') or k.startswith('<')) and arguments[k], arguments.keys())) args_spec = list(filter(lambda k: k.startswith('<'), spec)) cmd_spec = list(filter(lambda k: not (k.startswith('-') or k.startswith('<')), spec)) for element in spec: if element.startswith('-'): # element is an option if element in options: args.append(arguments.get(element, False)) options.remove(element) elif element.startswith('<') and \ not arguments.get(element) is False: # element is an argument args.append(arguments.get(element)) if element in args_spec: args_spec.remove(element) else: # element is a command if element in cmds and element in cmd_spec: cmds.remove(element) cmd_spec.remove(element) if options: continue # not all options have been matched if cmds: continue # not all cmds from command line have been matched if args_spec: continue # not all args from spec have been provided if cmd_spec: continue # not all cmds from spec have been provided # all options and cmds matched : call the cmd # TODO leave out all args to deal with "empty" signature exit_code = func(*args, **kwargs) return exit_code # no matching spec found raise Exception('No implementation for spec: %s' % arguments)
[ "def", "dispatch", "(", "cls", ",", "arguments", ",", "*", "*", "kwargs", ")", ":", "# first match wins", "# spec: all '-' elements must match, all others are False;", "# '<sth>' elements are converted to call args on order of", "# appearance", "#", "# kwargs are provided to dispatch call and used in func call", "for", "spec", ",", "func", "in", "cls", ".", "_specs", ":", "# if command and arguments.get(command) and match(args):", "args", "=", "[", "]", "# specified args in order of appearance", "options", "=", "list", "(", "filter", "(", "lambda", "k", ":", "k", ".", "startswith", "(", "'-'", ")", "and", "(", "arguments", "[", "k", "]", "or", "k", "in", "spec", ")", ",", "arguments", ".", "keys", "(", ")", ")", ")", "cmds", "=", "list", "(", "filter", "(", "lambda", "k", ":", "not", "(", "k", ".", "startswith", "(", "'-'", ")", "or", "k", ".", "startswith", "(", "'<'", ")", ")", "and", "arguments", "[", "k", "]", ",", "arguments", ".", "keys", "(", ")", ")", ")", "args_spec", "=", "list", "(", "filter", "(", "lambda", "k", ":", "k", ".", "startswith", "(", "'<'", ")", ",", "spec", ")", ")", "cmd_spec", "=", "list", "(", "filter", "(", "lambda", "k", ":", "not", "(", "k", ".", "startswith", "(", "'-'", ")", "or", "k", ".", "startswith", "(", "'<'", ")", ")", ",", "spec", ")", ")", "for", "element", "in", "spec", ":", "if", "element", ".", "startswith", "(", "'-'", ")", ":", "# element is an option", "if", "element", "in", "options", ":", "args", ".", "append", "(", "arguments", ".", "get", "(", "element", ",", "False", ")", ")", "options", ".", "remove", "(", "element", ")", "elif", "element", ".", "startswith", "(", "'<'", ")", "and", "not", "arguments", ".", "get", "(", "element", ")", "is", "False", ":", "# element is an argument", "args", ".", "append", "(", "arguments", ".", "get", "(", "element", ")", ")", "if", "element", "in", "args_spec", ":", "args_spec", ".", "remove", "(", "element", ")", "else", ":", "# element is a command", "if", "element", "in", "cmds", "and", "element", "in", "cmd_spec", ":", "cmds", ".", "remove", "(", "element", ")", "cmd_spec", ".", "remove", "(", "element", ")", "if", "options", ":", "continue", "# not all options have been matched", "if", "cmds", ":", "continue", "# not all cmds from command line have been matched", "if", "args_spec", ":", "continue", "# not all args from spec have been provided", "if", "cmd_spec", ":", "continue", "# not all cmds from spec have been provided", "# all options and cmds matched : call the cmd", "# TODO leave out all args to deal with \"empty\" signature", "exit_code", "=", "func", "(", "*", "args", ",", "*", "*", "kwargs", ")", "return", "exit_code", "# no matching spec found", "raise", "Exception", "(", "'No implementation for spec: %s'", "%", "arguments", ")" ]
Dispatch arguments parsed by docopt to the cmd with matching spec. :param arguments: :param kwargs: :return: exit_code
[ "Dispatch", "arguments", "parsed", "by", "docopt", "to", "the", "cmd", "with", "matching", "spec", "." ]
cd67cf416371337b83cb9ca3f696277125703339
https://github.com/glomex/gcdt/blob/cd67cf416371337b83cb9ca3f696277125703339/gcdt/gcdt_cmd_dispatcher.py#L42-L98
train
glomex/gcdt
gcdt/kumo_util.py
StackLookup.get_att
def get_att(self, parameter, as_reference=True): """Retrieves an attribute from an existing stack :param parameter: The output parameter which should be retrieved :param as_reference: Is the parameter a reference (Default) or a string :return: Value of parameter to retrieve """ if as_reference: return troposphere.GetAtt( self.__custom_stack_obj, troposphere.Ref(parameter) ) else: return troposphere.GetAtt( self.__custom_stack_obj, parameter )
python
def get_att(self, parameter, as_reference=True): """Retrieves an attribute from an existing stack :param parameter: The output parameter which should be retrieved :param as_reference: Is the parameter a reference (Default) or a string :return: Value of parameter to retrieve """ if as_reference: return troposphere.GetAtt( self.__custom_stack_obj, troposphere.Ref(parameter) ) else: return troposphere.GetAtt( self.__custom_stack_obj, parameter )
[ "def", "get_att", "(", "self", ",", "parameter", ",", "as_reference", "=", "True", ")", ":", "if", "as_reference", ":", "return", "troposphere", ".", "GetAtt", "(", "self", ".", "__custom_stack_obj", ",", "troposphere", ".", "Ref", "(", "parameter", ")", ")", "else", ":", "return", "troposphere", ".", "GetAtt", "(", "self", ".", "__custom_stack_obj", ",", "parameter", ")" ]
Retrieves an attribute from an existing stack :param parameter: The output parameter which should be retrieved :param as_reference: Is the parameter a reference (Default) or a string :return: Value of parameter to retrieve
[ "Retrieves", "an", "attribute", "from", "an", "existing", "stack", ":", "param", "parameter", ":", "The", "output", "parameter", "which", "should", "be", "retrieved", ":", "param", "as_reference", ":", "Is", "the", "parameter", "a", "reference", "(", "Default", ")", "or", "a", "string", ":", "return", ":", "Value", "of", "parameter", "to", "retrieve" ]
cd67cf416371337b83cb9ca3f696277125703339
https://github.com/glomex/gcdt/blob/cd67cf416371337b83cb9ca3f696277125703339/gcdt/kumo_util.py#L64-L79
train
glomex/gcdt
gcdt/event_source/s3.py
S3EventSource._get_notification_spec
def _get_notification_spec(self, lambda_arn): lambda_name = base.get_lambda_name(lambda_arn) notification_spec = { 'Id': self._make_notification_id(lambda_name), 'Events': [e for e in self._config['events']], 'LambdaFunctionArn': lambda_arn } # Add S3 key filters filter_rules = [] # look for filter rules for filter_type in ['prefix', 'suffix']: if filter_type in self._config: rule = {'Name': filter_type.capitalize(), 'Value': self._config[filter_type] } filter_rules.append(rule) if filter_rules: notification_spec['Filter'] = {'Key': {'FilterRules': filter_rules } } ''' if 'key_filters' in self._config: filters_spec = {'Key': {'FilterRules': [] } } # I do not think this is a useful structure: for filter in self._config['key_filters']: if 'type' in filter and 'value' in filter and filter['type'] in ('prefix', 'suffix'): rule = {'Name': filter['type'].capitalize(), 'Value': filter['value'] } filters_spec['Key']['FilterRules'].append(rule) notification_spec['Filter'] = filters_spec ''' return notification_spec
python
def _get_notification_spec(self, lambda_arn): lambda_name = base.get_lambda_name(lambda_arn) notification_spec = { 'Id': self._make_notification_id(lambda_name), 'Events': [e for e in self._config['events']], 'LambdaFunctionArn': lambda_arn } # Add S3 key filters filter_rules = [] # look for filter rules for filter_type in ['prefix', 'suffix']: if filter_type in self._config: rule = {'Name': filter_type.capitalize(), 'Value': self._config[filter_type] } filter_rules.append(rule) if filter_rules: notification_spec['Filter'] = {'Key': {'FilterRules': filter_rules } } ''' if 'key_filters' in self._config: filters_spec = {'Key': {'FilterRules': [] } } # I do not think this is a useful structure: for filter in self._config['key_filters']: if 'type' in filter and 'value' in filter and filter['type'] in ('prefix', 'suffix'): rule = {'Name': filter['type'].capitalize(), 'Value': filter['value'] } filters_spec['Key']['FilterRules'].append(rule) notification_spec['Filter'] = filters_spec ''' return notification_spec
[ "def", "_get_notification_spec", "(", "self", ",", "lambda_arn", ")", ":", "lambda_name", "=", "base", ".", "get_lambda_name", "(", "lambda_arn", ")", "notification_spec", "=", "{", "'Id'", ":", "self", ".", "_make_notification_id", "(", "lambda_name", ")", ",", "'Events'", ":", "[", "e", "for", "e", "in", "self", ".", "_config", "[", "'events'", "]", "]", ",", "'LambdaFunctionArn'", ":", "lambda_arn", "}", "# Add S3 key filters", "filter_rules", "=", "[", "]", "# look for filter rules", "for", "filter_type", "in", "[", "'prefix'", ",", "'suffix'", "]", ":", "if", "filter_type", "in", "self", ".", "_config", ":", "rule", "=", "{", "'Name'", ":", "filter_type", ".", "capitalize", "(", ")", ",", "'Value'", ":", "self", ".", "_config", "[", "filter_type", "]", "}", "filter_rules", ".", "append", "(", "rule", ")", "if", "filter_rules", ":", "notification_spec", "[", "'Filter'", "]", "=", "{", "'Key'", ":", "{", "'FilterRules'", ":", "filter_rules", "}", "}", "return", "notification_spec" ]
if 'key_filters' in self._config: filters_spec = {'Key': {'FilterRules': [] } } # I do not think this is a useful structure: for filter in self._config['key_filters']: if 'type' in filter and 'value' in filter and filter['type'] in ('prefix', 'suffix'): rule = {'Name': filter['type'].capitalize(), 'Value': filter['value'] } filters_spec['Key']['FilterRules'].append(rule) notification_spec['Filter'] = filters_spec
[ "if", "key_filters", "in", "self", ".", "_config", ":", "filters_spec", "=", "{", "Key", ":", "{", "FilterRules", ":", "[]", "}", "}", "#", "I", "do", "not", "think", "this", "is", "a", "useful", "structure", ":", "for", "filter", "in", "self", ".", "_config", "[", "key_filters", "]", ":", "if", "type", "in", "filter", "and", "value", "in", "filter", "and", "filter", "[", "type", "]", "in", "(", "prefix", "suffix", ")", ":", "rule", "=", "{", "Name", ":", "filter", "[", "type", "]", ".", "capitalize", "()", "Value", ":", "filter", "[", "value", "]", "}", "filters_spec", "[", "Key", "]", "[", "FilterRules", "]", ".", "append", "(", "rule", ")" ]
cd67cf416371337b83cb9ca3f696277125703339
https://github.com/glomex/gcdt/blob/cd67cf416371337b83cb9ca3f696277125703339/gcdt/event_source/s3.py#L51-L80
train
DeepHorizons/iarm
iarm_kernel/iarmkernel.py
ArmKernel.convert_representation
def convert_representation(self, i): """ Return the proper representation for the given integer """ if self.number_representation == 'unsigned': return i elif self.number_representation == 'signed': if i & (1 << self.interpreter._bit_width - 1): return -((~i + 1) & (2**self.interpreter._bit_width - 1)) else: return i elif self.number_representation == 'hex': return hex(i)
python
def convert_representation(self, i): """ Return the proper representation for the given integer """ if self.number_representation == 'unsigned': return i elif self.number_representation == 'signed': if i & (1 << self.interpreter._bit_width - 1): return -((~i + 1) & (2**self.interpreter._bit_width - 1)) else: return i elif self.number_representation == 'hex': return hex(i)
[ "def", "convert_representation", "(", "self", ",", "i", ")", ":", "if", "self", ".", "number_representation", "==", "'unsigned'", ":", "return", "i", "elif", "self", ".", "number_representation", "==", "'signed'", ":", "if", "i", "&", "(", "1", "<<", "self", ".", "interpreter", ".", "_bit_width", "-", "1", ")", ":", "return", "-", "(", "(", "~", "i", "+", "1", ")", "&", "(", "2", "**", "self", ".", "interpreter", ".", "_bit_width", "-", "1", ")", ")", "else", ":", "return", "i", "elif", "self", ".", "number_representation", "==", "'hex'", ":", "return", "hex", "(", "i", ")" ]
Return the proper representation for the given integer
[ "Return", "the", "proper", "representation", "for", "the", "given", "integer" ]
b913c9fd577b793a6bbced78b78a5d8d7cd88de4
https://github.com/DeepHorizons/iarm/blob/b913c9fd577b793a6bbced78b78a5d8d7cd88de4/iarm_kernel/iarmkernel.py#L40-L52
train
DeepHorizons/iarm
iarm_kernel/iarmkernel.py
ArmKernel.magic_generate_random
def magic_generate_random(self, line): """ Set the generate random flag, unset registers and memory will return a random value. Usage: Call the magic by itself or with `true` to have registers and memory return a random value if they are unset and read from, much like how real hardware would work. Defaults to False, or to not generate random values `%generate_random` or `%generate_random true` or `%generate_random false` """ line = line.strip().lower() if not line or line == 'true': self.interpreter.generate_random = True elif line == 'false': self.interpreter.generate_random = False else: stream_content = {'name': 'stderr', 'text': "unknwon value '{}'".format(line)} self.send_response(self.iopub_socket, 'stream', stream_content) return {'status': 'error', 'execution_count': self.execution_count, 'ename': ValueError.__name__, 'evalue': "unknwon value '{}'".format(line), 'traceback': '???'}
python
def magic_generate_random(self, line): """ Set the generate random flag, unset registers and memory will return a random value. Usage: Call the magic by itself or with `true` to have registers and memory return a random value if they are unset and read from, much like how real hardware would work. Defaults to False, or to not generate random values `%generate_random` or `%generate_random true` or `%generate_random false` """ line = line.strip().lower() if not line or line == 'true': self.interpreter.generate_random = True elif line == 'false': self.interpreter.generate_random = False else: stream_content = {'name': 'stderr', 'text': "unknwon value '{}'".format(line)} self.send_response(self.iopub_socket, 'stream', stream_content) return {'status': 'error', 'execution_count': self.execution_count, 'ename': ValueError.__name__, 'evalue': "unknwon value '{}'".format(line), 'traceback': '???'}
[ "def", "magic_generate_random", "(", "self", ",", "line", ")", ":", "line", "=", "line", ".", "strip", "(", ")", ".", "lower", "(", ")", "if", "not", "line", "or", "line", "==", "'true'", ":", "self", ".", "interpreter", ".", "generate_random", "=", "True", "elif", "line", "==", "'false'", ":", "self", ".", "interpreter", ".", "generate_random", "=", "False", "else", ":", "stream_content", "=", "{", "'name'", ":", "'stderr'", ",", "'text'", ":", "\"unknwon value '{}'\"", ".", "format", "(", "line", ")", "}", "self", ".", "send_response", "(", "self", ".", "iopub_socket", ",", "'stream'", ",", "stream_content", ")", "return", "{", "'status'", ":", "'error'", ",", "'execution_count'", ":", "self", ".", "execution_count", ",", "'ename'", ":", "ValueError", ".", "__name__", ",", "'evalue'", ":", "\"unknwon value '{}'\"", ".", "format", "(", "line", ")", ",", "'traceback'", ":", "'???'", "}" ]
Set the generate random flag, unset registers and memory will return a random value. Usage: Call the magic by itself or with `true` to have registers and memory return a random value if they are unset and read from, much like how real hardware would work. Defaults to False, or to not generate random values `%generate_random` or `%generate_random true` or `%generate_random false`
[ "Set", "the", "generate", "random", "flag", "unset", "registers", "and", "memory", "will", "return", "a", "random", "value", "." ]
b913c9fd577b793a6bbced78b78a5d8d7cd88de4
https://github.com/DeepHorizons/iarm/blob/b913c9fd577b793a6bbced78b78a5d8d7cd88de4/iarm_kernel/iarmkernel.py#L54-L81
train
DeepHorizons/iarm
iarm_kernel/iarmkernel.py
ArmKernel.magic_postpone_execution
def magic_postpone_execution(self, line): """ Postpone execution of instructions until explicitly run Usage: Call this magic with `true` or nothing to postpone execution, or call with `false` to execute each instruction when evaluated. This defaults to True. Note that each cell is executed only executed after all lines in the cell have been evaluated properly. `%postpone_execution` or `%postpone_execution true` or `%postpone_execution false` """ line = line.strip().lower() if not line or line == 'true': self.interpreter.postpone_execution = True elif line == 'false': self.interpreter.postpone_execution = False else: stream_content = {'name': 'stderr', 'text': "unknwon value '{}'".format(line)} self.send_response(self.iopub_socket, 'stream', stream_content) return {'status': 'error', 'execution_count': self.execution_count, 'ename': ValueError.__name__, 'evalue': "unknwon value '{}'".format(line), 'traceback': '???'}
python
def magic_postpone_execution(self, line): """ Postpone execution of instructions until explicitly run Usage: Call this magic with `true` or nothing to postpone execution, or call with `false` to execute each instruction when evaluated. This defaults to True. Note that each cell is executed only executed after all lines in the cell have been evaluated properly. `%postpone_execution` or `%postpone_execution true` or `%postpone_execution false` """ line = line.strip().lower() if not line or line == 'true': self.interpreter.postpone_execution = True elif line == 'false': self.interpreter.postpone_execution = False else: stream_content = {'name': 'stderr', 'text': "unknwon value '{}'".format(line)} self.send_response(self.iopub_socket, 'stream', stream_content) return {'status': 'error', 'execution_count': self.execution_count, 'ename': ValueError.__name__, 'evalue': "unknwon value '{}'".format(line), 'traceback': '???'}
[ "def", "magic_postpone_execution", "(", "self", ",", "line", ")", ":", "line", "=", "line", ".", "strip", "(", ")", ".", "lower", "(", ")", "if", "not", "line", "or", "line", "==", "'true'", ":", "self", ".", "interpreter", ".", "postpone_execution", "=", "True", "elif", "line", "==", "'false'", ":", "self", ".", "interpreter", ".", "postpone_execution", "=", "False", "else", ":", "stream_content", "=", "{", "'name'", ":", "'stderr'", ",", "'text'", ":", "\"unknwon value '{}'\"", ".", "format", "(", "line", ")", "}", "self", ".", "send_response", "(", "self", ".", "iopub_socket", ",", "'stream'", ",", "stream_content", ")", "return", "{", "'status'", ":", "'error'", ",", "'execution_count'", ":", "self", ".", "execution_count", ",", "'ename'", ":", "ValueError", ".", "__name__", ",", "'evalue'", ":", "\"unknwon value '{}'\"", ".", "format", "(", "line", ")", ",", "'traceback'", ":", "'???'", "}" ]
Postpone execution of instructions until explicitly run Usage: Call this magic with `true` or nothing to postpone execution, or call with `false` to execute each instruction when evaluated. This defaults to True. Note that each cell is executed only executed after all lines in the cell have been evaluated properly. `%postpone_execution` or `%postpone_execution true` or `%postpone_execution false`
[ "Postpone", "execution", "of", "instructions", "until", "explicitly", "run" ]
b913c9fd577b793a6bbced78b78a5d8d7cd88de4
https://github.com/DeepHorizons/iarm/blob/b913c9fd577b793a6bbced78b78a5d8d7cd88de4/iarm_kernel/iarmkernel.py#L83-L113
train
DeepHorizons/iarm
iarm_kernel/iarmkernel.py
ArmKernel.magic_register
def magic_register(self, line): """ Print out the current value of a register Usage: Pass in the register, or a list of registers separated by spaces A list of registeres can be entered by separating them by a hyphen `%reg R1` or `%reg R0 R5 R6` or `%reg R8-R12` """ message = "" for reg in [i.strip() for i in line.replace(',', '').split()]: if '-' in reg: # We have a range (Rn-Rk) r1, r2 = reg.split('-') # TODO do we want to allow just numbers? n1 = re.search(self.interpreter.REGISTER_REGEX, r1).groups()[0] n2 = re.search(self.interpreter.REGISTER_REGEX, r2).groups()[0] n1 = self.interpreter.convert_to_integer(n1) n2 = self.interpreter.convert_to_integer(n2) for i in range(n1, n2+1): val = self.interpreter.register[r1[0] + str(i)] val = self.convert_representation(val) message += "{}: {}\n".format(r1[0] + str(i), val) else: val = self.interpreter.register[reg] val = self.convert_representation(val) message += "{}: {}\n".format(reg, val) stream_content = {'name': 'stdout', 'text': message} self.send_response(self.iopub_socket, 'stream', stream_content)
python
def magic_register(self, line): """ Print out the current value of a register Usage: Pass in the register, or a list of registers separated by spaces A list of registeres can be entered by separating them by a hyphen `%reg R1` or `%reg R0 R5 R6` or `%reg R8-R12` """ message = "" for reg in [i.strip() for i in line.replace(',', '').split()]: if '-' in reg: # We have a range (Rn-Rk) r1, r2 = reg.split('-') # TODO do we want to allow just numbers? n1 = re.search(self.interpreter.REGISTER_REGEX, r1).groups()[0] n2 = re.search(self.interpreter.REGISTER_REGEX, r2).groups()[0] n1 = self.interpreter.convert_to_integer(n1) n2 = self.interpreter.convert_to_integer(n2) for i in range(n1, n2+1): val = self.interpreter.register[r1[0] + str(i)] val = self.convert_representation(val) message += "{}: {}\n".format(r1[0] + str(i), val) else: val = self.interpreter.register[reg] val = self.convert_representation(val) message += "{}: {}\n".format(reg, val) stream_content = {'name': 'stdout', 'text': message} self.send_response(self.iopub_socket, 'stream', stream_content)
[ "def", "magic_register", "(", "self", ",", "line", ")", ":", "message", "=", "\"\"", "for", "reg", "in", "[", "i", ".", "strip", "(", ")", "for", "i", "in", "line", ".", "replace", "(", "','", ",", "''", ")", ".", "split", "(", ")", "]", ":", "if", "'-'", "in", "reg", ":", "# We have a range (Rn-Rk)", "r1", ",", "r2", "=", "reg", ".", "split", "(", "'-'", ")", "# TODO do we want to allow just numbers?", "n1", "=", "re", ".", "search", "(", "self", ".", "interpreter", ".", "REGISTER_REGEX", ",", "r1", ")", ".", "groups", "(", ")", "[", "0", "]", "n2", "=", "re", ".", "search", "(", "self", ".", "interpreter", ".", "REGISTER_REGEX", ",", "r2", ")", ".", "groups", "(", ")", "[", "0", "]", "n1", "=", "self", ".", "interpreter", ".", "convert_to_integer", "(", "n1", ")", "n2", "=", "self", ".", "interpreter", ".", "convert_to_integer", "(", "n2", ")", "for", "i", "in", "range", "(", "n1", ",", "n2", "+", "1", ")", ":", "val", "=", "self", ".", "interpreter", ".", "register", "[", "r1", "[", "0", "]", "+", "str", "(", "i", ")", "]", "val", "=", "self", ".", "convert_representation", "(", "val", ")", "message", "+=", "\"{}: {}\\n\"", ".", "format", "(", "r1", "[", "0", "]", "+", "str", "(", "i", ")", ",", "val", ")", "else", ":", "val", "=", "self", ".", "interpreter", ".", "register", "[", "reg", "]", "val", "=", "self", ".", "convert_representation", "(", "val", ")", "message", "+=", "\"{}: {}\\n\"", ".", "format", "(", "reg", ",", "val", ")", "stream_content", "=", "{", "'name'", ":", "'stdout'", ",", "'text'", ":", "message", "}", "self", ".", "send_response", "(", "self", ".", "iopub_socket", ",", "'stream'", ",", "stream_content", ")" ]
Print out the current value of a register Usage: Pass in the register, or a list of registers separated by spaces A list of registeres can be entered by separating them by a hyphen `%reg R1` or `%reg R0 R5 R6` or `%reg R8-R12`
[ "Print", "out", "the", "current", "value", "of", "a", "register" ]
b913c9fd577b793a6bbced78b78a5d8d7cd88de4
https://github.com/DeepHorizons/iarm/blob/b913c9fd577b793a6bbced78b78a5d8d7cd88de4/iarm_kernel/iarmkernel.py#L148-L181
train
DeepHorizons/iarm
iarm_kernel/iarmkernel.py
ArmKernel.magic_memory
def magic_memory(self, line): """ Print out the current value of memory Usage: Pass in the byte of memory to read, separated by spaced A list of memory contents can be entered by separating them by a hyphen `%mem 4 5` or `%mem 8-12` """ # TODO add support for directives message = "" for address in [i.strip() for i in line.replace(',', '').split()]: if '-' in address: # We have a range (n-k) m1, m2 = address.split('-') n1 = re.search(self.interpreter.IMMEDIATE_NUMBER, m1).groups()[0] n2 = re.search(self.interpreter.IMMEDIATE_NUMBER, m2).groups()[0] n1 = self.interpreter.convert_to_integer(n1) n2 = self.interpreter.convert_to_integer(n2) for i in range(n1, n2 + 1): val = self.interpreter.memory[i] val = self.convert_representation(val) message += "{}: {}\n".format(str(i), val) else: # TODO fix what is the key for memory (currently it's an int, but registers are strings, should it be the same?) val = self.interpreter.memory[self.interpreter.convert_to_integer(address)] val = self.convert_representation(val) message += "{}: {}\n".format(address, val) stream_content = {'name': 'stdout', 'text': message} self.send_response(self.iopub_socket, 'stream', stream_content)
python
def magic_memory(self, line): """ Print out the current value of memory Usage: Pass in the byte of memory to read, separated by spaced A list of memory contents can be entered by separating them by a hyphen `%mem 4 5` or `%mem 8-12` """ # TODO add support for directives message = "" for address in [i.strip() for i in line.replace(',', '').split()]: if '-' in address: # We have a range (n-k) m1, m2 = address.split('-') n1 = re.search(self.interpreter.IMMEDIATE_NUMBER, m1).groups()[0] n2 = re.search(self.interpreter.IMMEDIATE_NUMBER, m2).groups()[0] n1 = self.interpreter.convert_to_integer(n1) n2 = self.interpreter.convert_to_integer(n2) for i in range(n1, n2 + 1): val = self.interpreter.memory[i] val = self.convert_representation(val) message += "{}: {}\n".format(str(i), val) else: # TODO fix what is the key for memory (currently it's an int, but registers are strings, should it be the same?) val = self.interpreter.memory[self.interpreter.convert_to_integer(address)] val = self.convert_representation(val) message += "{}: {}\n".format(address, val) stream_content = {'name': 'stdout', 'text': message} self.send_response(self.iopub_socket, 'stream', stream_content)
[ "def", "magic_memory", "(", "self", ",", "line", ")", ":", "# TODO add support for directives", "message", "=", "\"\"", "for", "address", "in", "[", "i", ".", "strip", "(", ")", "for", "i", "in", "line", ".", "replace", "(", "','", ",", "''", ")", ".", "split", "(", ")", "]", ":", "if", "'-'", "in", "address", ":", "# We have a range (n-k)", "m1", ",", "m2", "=", "address", ".", "split", "(", "'-'", ")", "n1", "=", "re", ".", "search", "(", "self", ".", "interpreter", ".", "IMMEDIATE_NUMBER", ",", "m1", ")", ".", "groups", "(", ")", "[", "0", "]", "n2", "=", "re", ".", "search", "(", "self", ".", "interpreter", ".", "IMMEDIATE_NUMBER", ",", "m2", ")", ".", "groups", "(", ")", "[", "0", "]", "n1", "=", "self", ".", "interpreter", ".", "convert_to_integer", "(", "n1", ")", "n2", "=", "self", ".", "interpreter", ".", "convert_to_integer", "(", "n2", ")", "for", "i", "in", "range", "(", "n1", ",", "n2", "+", "1", ")", ":", "val", "=", "self", ".", "interpreter", ".", "memory", "[", "i", "]", "val", "=", "self", ".", "convert_representation", "(", "val", ")", "message", "+=", "\"{}: {}\\n\"", ".", "format", "(", "str", "(", "i", ")", ",", "val", ")", "else", ":", "# TODO fix what is the key for memory (currently it's an int, but registers are strings, should it be the same?)", "val", "=", "self", ".", "interpreter", ".", "memory", "[", "self", ".", "interpreter", ".", "convert_to_integer", "(", "address", ")", "]", "val", "=", "self", ".", "convert_representation", "(", "val", ")", "message", "+=", "\"{}: {}\\n\"", ".", "format", "(", "address", ",", "val", ")", "stream_content", "=", "{", "'name'", ":", "'stdout'", ",", "'text'", ":", "message", "}", "self", ".", "send_response", "(", "self", ".", "iopub_socket", ",", "'stream'", ",", "stream_content", ")" ]
Print out the current value of memory Usage: Pass in the byte of memory to read, separated by spaced A list of memory contents can be entered by separating them by a hyphen `%mem 4 5` or `%mem 8-12`
[ "Print", "out", "the", "current", "value", "of", "memory" ]
b913c9fd577b793a6bbced78b78a5d8d7cd88de4
https://github.com/DeepHorizons/iarm/blob/b913c9fd577b793a6bbced78b78a5d8d7cd88de4/iarm_kernel/iarmkernel.py#L183-L215
train
DeepHorizons/iarm
iarm_kernel/iarmkernel.py
ArmKernel.magic_run
def magic_run(self, line): """ Run the current program Usage: Call with a numbe rto run that many steps, or call with no arguments to run to the end of the current program `%run` or `%run 1` """ i = float('inf') if line.strip(): i = int(line) try: with warnings.catch_warnings(record=True) as w: self.interpreter.run(i) for warning_message in w: # TODO should this be stdout or stderr stream_content = {'name': 'stdout', 'text': 'Warning: ' + str(warning_message.message) + '\n'} self.send_response(self.iopub_socket, 'stream', stream_content) except iarm.exceptions.EndOfProgram as e: f_name = self.interpreter.program[self.interpreter.register['PC'] - 1].__name__ f_name = f_name[:f_name.find('_')] message = "Error in {}: ".format(f_name) stream_content = {'name': 'stdout', 'text': message + str(e) + '\n'} self.send_response(self.iopub_socket, 'stream', stream_content) except Exception as e: for err in e.args: stream_content = {'name': 'stderr', 'text': str(err)} self.send_response(self.iopub_socket, 'stream', stream_content) return {'status': 'error', 'execution_count': self.execution_count, 'ename': type(e).__name__, 'evalue': str(e), 'traceback': '???'}
python
def magic_run(self, line): """ Run the current program Usage: Call with a numbe rto run that many steps, or call with no arguments to run to the end of the current program `%run` or `%run 1` """ i = float('inf') if line.strip(): i = int(line) try: with warnings.catch_warnings(record=True) as w: self.interpreter.run(i) for warning_message in w: # TODO should this be stdout or stderr stream_content = {'name': 'stdout', 'text': 'Warning: ' + str(warning_message.message) + '\n'} self.send_response(self.iopub_socket, 'stream', stream_content) except iarm.exceptions.EndOfProgram as e: f_name = self.interpreter.program[self.interpreter.register['PC'] - 1].__name__ f_name = f_name[:f_name.find('_')] message = "Error in {}: ".format(f_name) stream_content = {'name': 'stdout', 'text': message + str(e) + '\n'} self.send_response(self.iopub_socket, 'stream', stream_content) except Exception as e: for err in e.args: stream_content = {'name': 'stderr', 'text': str(err)} self.send_response(self.iopub_socket, 'stream', stream_content) return {'status': 'error', 'execution_count': self.execution_count, 'ename': type(e).__name__, 'evalue': str(e), 'traceback': '???'}
[ "def", "magic_run", "(", "self", ",", "line", ")", ":", "i", "=", "float", "(", "'inf'", ")", "if", "line", ".", "strip", "(", ")", ":", "i", "=", "int", "(", "line", ")", "try", ":", "with", "warnings", ".", "catch_warnings", "(", "record", "=", "True", ")", "as", "w", ":", "self", ".", "interpreter", ".", "run", "(", "i", ")", "for", "warning_message", "in", "w", ":", "# TODO should this be stdout or stderr", "stream_content", "=", "{", "'name'", ":", "'stdout'", ",", "'text'", ":", "'Warning: '", "+", "str", "(", "warning_message", ".", "message", ")", "+", "'\\n'", "}", "self", ".", "send_response", "(", "self", ".", "iopub_socket", ",", "'stream'", ",", "stream_content", ")", "except", "iarm", ".", "exceptions", ".", "EndOfProgram", "as", "e", ":", "f_name", "=", "self", ".", "interpreter", ".", "program", "[", "self", ".", "interpreter", ".", "register", "[", "'PC'", "]", "-", "1", "]", ".", "__name__", "f_name", "=", "f_name", "[", ":", "f_name", ".", "find", "(", "'_'", ")", "]", "message", "=", "\"Error in {}: \"", ".", "format", "(", "f_name", ")", "stream_content", "=", "{", "'name'", ":", "'stdout'", ",", "'text'", ":", "message", "+", "str", "(", "e", ")", "+", "'\\n'", "}", "self", ".", "send_response", "(", "self", ".", "iopub_socket", ",", "'stream'", ",", "stream_content", ")", "except", "Exception", "as", "e", ":", "for", "err", "in", "e", ".", "args", ":", "stream_content", "=", "{", "'name'", ":", "'stderr'", ",", "'text'", ":", "str", "(", "err", ")", "}", "self", ".", "send_response", "(", "self", ".", "iopub_socket", ",", "'stream'", ",", "stream_content", ")", "return", "{", "'status'", ":", "'error'", ",", "'execution_count'", ":", "self", ".", "execution_count", ",", "'ename'", ":", "type", "(", "e", ")", ".", "__name__", ",", "'evalue'", ":", "str", "(", "e", ")", ",", "'traceback'", ":", "'???'", "}" ]
Run the current program Usage: Call with a numbe rto run that many steps, or call with no arguments to run to the end of the current program `%run` or `%run 1`
[ "Run", "the", "current", "program" ]
b913c9fd577b793a6bbced78b78a5d8d7cd88de4
https://github.com/DeepHorizons/iarm/blob/b913c9fd577b793a6bbced78b78a5d8d7cd88de4/iarm_kernel/iarmkernel.py#L217-L254
train
DeepHorizons/iarm
iarm_kernel/iarmkernel.py
ArmKernel.magic_help
def magic_help(self, line): """ Print out the help for magics Usage: Call help with no arguments to list all magics, or call it with a magic to print out it's help info. `%help` or `%help run """ line = line.strip() if not line: for magic in self.magics: stream_content = {'name': 'stdout', 'text': "%{}\n".format(magic)} self.send_response(self.iopub_socket, 'stream', stream_content) elif line in self.magics: # its a magic stream_content = {'name': 'stdout', 'text': "{}\n{}".format(line, self.magics[line].__doc__)} self.send_response(self.iopub_socket, 'stream', stream_content) elif line in self.interpreter.ops: # it's an instruction stream_content = {'name': 'stdout', 'text': "{}\n{}".format(line, self.interpreter.ops[line].__doc__)} self.send_response(self.iopub_socket, 'stream', stream_content) else: stream_content = {'name': 'stderr', 'text': "'{}' not a known magic or instruction".format(line)} self.send_response(self.iopub_socket, 'stream', stream_content)
python
def magic_help(self, line): """ Print out the help for magics Usage: Call help with no arguments to list all magics, or call it with a magic to print out it's help info. `%help` or `%help run """ line = line.strip() if not line: for magic in self.magics: stream_content = {'name': 'stdout', 'text': "%{}\n".format(magic)} self.send_response(self.iopub_socket, 'stream', stream_content) elif line in self.magics: # its a magic stream_content = {'name': 'stdout', 'text': "{}\n{}".format(line, self.magics[line].__doc__)} self.send_response(self.iopub_socket, 'stream', stream_content) elif line in self.interpreter.ops: # it's an instruction stream_content = {'name': 'stdout', 'text': "{}\n{}".format(line, self.interpreter.ops[line].__doc__)} self.send_response(self.iopub_socket, 'stream', stream_content) else: stream_content = {'name': 'stderr', 'text': "'{}' not a known magic or instruction".format(line)} self.send_response(self.iopub_socket, 'stream', stream_content)
[ "def", "magic_help", "(", "self", ",", "line", ")", ":", "line", "=", "line", ".", "strip", "(", ")", "if", "not", "line", ":", "for", "magic", "in", "self", ".", "magics", ":", "stream_content", "=", "{", "'name'", ":", "'stdout'", ",", "'text'", ":", "\"%{}\\n\"", ".", "format", "(", "magic", ")", "}", "self", ".", "send_response", "(", "self", ".", "iopub_socket", ",", "'stream'", ",", "stream_content", ")", "elif", "line", "in", "self", ".", "magics", ":", "# its a magic", "stream_content", "=", "{", "'name'", ":", "'stdout'", ",", "'text'", ":", "\"{}\\n{}\"", ".", "format", "(", "line", ",", "self", ".", "magics", "[", "line", "]", ".", "__doc__", ")", "}", "self", ".", "send_response", "(", "self", ".", "iopub_socket", ",", "'stream'", ",", "stream_content", ")", "elif", "line", "in", "self", ".", "interpreter", ".", "ops", ":", "# it's an instruction", "stream_content", "=", "{", "'name'", ":", "'stdout'", ",", "'text'", ":", "\"{}\\n{}\"", ".", "format", "(", "line", ",", "self", ".", "interpreter", ".", "ops", "[", "line", "]", ".", "__doc__", ")", "}", "self", ".", "send_response", "(", "self", ".", "iopub_socket", ",", "'stream'", ",", "stream_content", ")", "else", ":", "stream_content", "=", "{", "'name'", ":", "'stderr'", ",", "'text'", ":", "\"'{}' not a known magic or instruction\"", ".", "format", "(", "line", ")", "}", "self", ".", "send_response", "(", "self", ".", "iopub_socket", ",", "'stream'", ",", "stream_content", ")" ]
Print out the help for magics Usage: Call help with no arguments to list all magics, or call it with a magic to print out it's help info. `%help` or `%help run
[ "Print", "out", "the", "help", "for", "magics" ]
b913c9fd577b793a6bbced78b78a5d8d7cd88de4
https://github.com/DeepHorizons/iarm/blob/b913c9fd577b793a6bbced78b78a5d8d7cd88de4/iarm_kernel/iarmkernel.py#L256-L283
train
glomex/gcdt
gcdt/yugen_core.py
export_to_swagger
def export_to_swagger(awsclient, api_name, stage_name, api_description, lambdas, custom_hostname=False, custom_base_path=False): """Export the API design as swagger file. :param api_name: :param stage_name: :param api_description: :param lambdas: :param custom_hostname: :param custom_base_path: """ print('Exporting to swagger...') api = _api_by_name(awsclient, api_name) if api is not None: print(json2table(api)) api_id = api['id'] client_api = awsclient.get_client('apigateway') template_variables = _template_variables_to_dict( client_api, api_name, api_description, stage_name, api_id, lambdas, custom_hostname, custom_base_path) content = _compile_template(SWAGGER_FILE, template_variables) swagger_file = open('swagger_export.yaml', 'w') swagger_file.write(content) else: print('API name unknown')
python
def export_to_swagger(awsclient, api_name, stage_name, api_description, lambdas, custom_hostname=False, custom_base_path=False): """Export the API design as swagger file. :param api_name: :param stage_name: :param api_description: :param lambdas: :param custom_hostname: :param custom_base_path: """ print('Exporting to swagger...') api = _api_by_name(awsclient, api_name) if api is not None: print(json2table(api)) api_id = api['id'] client_api = awsclient.get_client('apigateway') template_variables = _template_variables_to_dict( client_api, api_name, api_description, stage_name, api_id, lambdas, custom_hostname, custom_base_path) content = _compile_template(SWAGGER_FILE, template_variables) swagger_file = open('swagger_export.yaml', 'w') swagger_file.write(content) else: print('API name unknown')
[ "def", "export_to_swagger", "(", "awsclient", ",", "api_name", ",", "stage_name", ",", "api_description", ",", "lambdas", ",", "custom_hostname", "=", "False", ",", "custom_base_path", "=", "False", ")", ":", "print", "(", "'Exporting to swagger...'", ")", "api", "=", "_api_by_name", "(", "awsclient", ",", "api_name", ")", "if", "api", "is", "not", "None", ":", "print", "(", "json2table", "(", "api", ")", ")", "api_id", "=", "api", "[", "'id'", "]", "client_api", "=", "awsclient", ".", "get_client", "(", "'apigateway'", ")", "template_variables", "=", "_template_variables_to_dict", "(", "client_api", ",", "api_name", ",", "api_description", ",", "stage_name", ",", "api_id", ",", "lambdas", ",", "custom_hostname", ",", "custom_base_path", ")", "content", "=", "_compile_template", "(", "SWAGGER_FILE", ",", "template_variables", ")", "swagger_file", "=", "open", "(", "'swagger_export.yaml'", ",", "'w'", ")", "swagger_file", ".", "write", "(", "content", ")", "else", ":", "print", "(", "'API name unknown'", ")" ]
Export the API design as swagger file. :param api_name: :param stage_name: :param api_description: :param lambdas: :param custom_hostname: :param custom_base_path:
[ "Export", "the", "API", "design", "as", "swagger", "file", ".", ":", "param", "api_name", ":", ":", "param", "stage_name", ":", ":", "param", "api_description", ":", ":", "param", "lambdas", ":", ":", "param", "custom_hostname", ":", ":", "param", "custom_base_path", ":" ]
cd67cf416371337b83cb9ca3f696277125703339
https://github.com/glomex/gcdt/blob/cd67cf416371337b83cb9ca3f696277125703339/gcdt/yugen_core.py#L21-L54
train
glomex/gcdt
gcdt/yugen_core.py
list_apis
def list_apis(awsclient): """List APIs in account.""" client_api = awsclient.get_client('apigateway') apis = client_api.get_rest_apis()['items'] for api in apis: print(json2table(api))
python
def list_apis(awsclient): """List APIs in account.""" client_api = awsclient.get_client('apigateway') apis = client_api.get_rest_apis()['items'] for api in apis: print(json2table(api))
[ "def", "list_apis", "(", "awsclient", ")", ":", "client_api", "=", "awsclient", ".", "get_client", "(", "'apigateway'", ")", "apis", "=", "client_api", ".", "get_rest_apis", "(", ")", "[", "'items'", "]", "for", "api", "in", "apis", ":", "print", "(", "json2table", "(", "api", ")", ")" ]
List APIs in account.
[ "List", "APIs", "in", "account", "." ]
cd67cf416371337b83cb9ca3f696277125703339
https://github.com/glomex/gcdt/blob/cd67cf416371337b83cb9ca3f696277125703339/gcdt/yugen_core.py#L57-L64
train
glomex/gcdt
gcdt/yugen_core.py
deploy_api
def deploy_api(awsclient, api_name, api_description, stage_name, api_key, lambdas, cache_cluster_enabled, cache_cluster_size, method_settings=None): """Deploy API Gateway to AWS cloud. :param awsclient: :param api_name: :param api_description: :param stage_name: :param api_key: :param lambdas: :param cache_cluster_enabled: :param cache_cluster_size: :param method_settings: """ if not _api_exists(awsclient, api_name): if os.path.isfile(SWAGGER_FILE): # this does an import from swagger file # the next step does not make sense since there is a check in # _import_from_swagger for if api is existent! # _create_api(api_name=api_name, api_description=api_description) _import_from_swagger(awsclient, api_name, api_description, stage_name, lambdas) else: print('No swagger file (%s) found' % SWAGGER_FILE) api = _api_by_name(awsclient, api_name) if api is not None: _ensure_lambdas_permissions(awsclient, lambdas, api) _create_deployment(awsclient, api_name, stage_name, cache_cluster_enabled, cache_cluster_size) _update_stage(awsclient, api['id'], stage_name, method_settings) _wire_api_key(awsclient, api_name, api_key, stage_name) else: print('API name unknown') else: if os.path.isfile(SWAGGER_FILE): _update_from_swagger(awsclient, api_name, api_description, stage_name, lambdas) else: _update_api() api = _api_by_name(awsclient, api_name) if api is not None: _ensure_lambdas_permissions(awsclient, lambdas, api) _create_deployment(awsclient, api_name, stage_name, cache_cluster_enabled, cache_cluster_size) _update_stage(awsclient, api['id'], stage_name, method_settings) else: print('API name unknown')
python
def deploy_api(awsclient, api_name, api_description, stage_name, api_key, lambdas, cache_cluster_enabled, cache_cluster_size, method_settings=None): """Deploy API Gateway to AWS cloud. :param awsclient: :param api_name: :param api_description: :param stage_name: :param api_key: :param lambdas: :param cache_cluster_enabled: :param cache_cluster_size: :param method_settings: """ if not _api_exists(awsclient, api_name): if os.path.isfile(SWAGGER_FILE): # this does an import from swagger file # the next step does not make sense since there is a check in # _import_from_swagger for if api is existent! # _create_api(api_name=api_name, api_description=api_description) _import_from_swagger(awsclient, api_name, api_description, stage_name, lambdas) else: print('No swagger file (%s) found' % SWAGGER_FILE) api = _api_by_name(awsclient, api_name) if api is not None: _ensure_lambdas_permissions(awsclient, lambdas, api) _create_deployment(awsclient, api_name, stage_name, cache_cluster_enabled, cache_cluster_size) _update_stage(awsclient, api['id'], stage_name, method_settings) _wire_api_key(awsclient, api_name, api_key, stage_name) else: print('API name unknown') else: if os.path.isfile(SWAGGER_FILE): _update_from_swagger(awsclient, api_name, api_description, stage_name, lambdas) else: _update_api() api = _api_by_name(awsclient, api_name) if api is not None: _ensure_lambdas_permissions(awsclient, lambdas, api) _create_deployment(awsclient, api_name, stage_name, cache_cluster_enabled, cache_cluster_size) _update_stage(awsclient, api['id'], stage_name, method_settings) else: print('API name unknown')
[ "def", "deploy_api", "(", "awsclient", ",", "api_name", ",", "api_description", ",", "stage_name", ",", "api_key", ",", "lambdas", ",", "cache_cluster_enabled", ",", "cache_cluster_size", ",", "method_settings", "=", "None", ")", ":", "if", "not", "_api_exists", "(", "awsclient", ",", "api_name", ")", ":", "if", "os", ".", "path", ".", "isfile", "(", "SWAGGER_FILE", ")", ":", "# this does an import from swagger file", "# the next step does not make sense since there is a check in", "# _import_from_swagger for if api is existent!", "# _create_api(api_name=api_name, api_description=api_description)", "_import_from_swagger", "(", "awsclient", ",", "api_name", ",", "api_description", ",", "stage_name", ",", "lambdas", ")", "else", ":", "print", "(", "'No swagger file (%s) found'", "%", "SWAGGER_FILE", ")", "api", "=", "_api_by_name", "(", "awsclient", ",", "api_name", ")", "if", "api", "is", "not", "None", ":", "_ensure_lambdas_permissions", "(", "awsclient", ",", "lambdas", ",", "api", ")", "_create_deployment", "(", "awsclient", ",", "api_name", ",", "stage_name", ",", "cache_cluster_enabled", ",", "cache_cluster_size", ")", "_update_stage", "(", "awsclient", ",", "api", "[", "'id'", "]", ",", "stage_name", ",", "method_settings", ")", "_wire_api_key", "(", "awsclient", ",", "api_name", ",", "api_key", ",", "stage_name", ")", "else", ":", "print", "(", "'API name unknown'", ")", "else", ":", "if", "os", ".", "path", ".", "isfile", "(", "SWAGGER_FILE", ")", ":", "_update_from_swagger", "(", "awsclient", ",", "api_name", ",", "api_description", ",", "stage_name", ",", "lambdas", ")", "else", ":", "_update_api", "(", ")", "api", "=", "_api_by_name", "(", "awsclient", ",", "api_name", ")", "if", "api", "is", "not", "None", ":", "_ensure_lambdas_permissions", "(", "awsclient", ",", "lambdas", ",", "api", ")", "_create_deployment", "(", "awsclient", ",", "api_name", ",", "stage_name", ",", "cache_cluster_enabled", ",", "cache_cluster_size", ")", "_update_stage", "(", "awsclient", ",", "api", "[", "'id'", "]", ",", "stage_name", ",", "method_settings", ")", "else", ":", "print", "(", "'API name unknown'", ")" ]
Deploy API Gateway to AWS cloud. :param awsclient: :param api_name: :param api_description: :param stage_name: :param api_key: :param lambdas: :param cache_cluster_enabled: :param cache_cluster_size: :param method_settings:
[ "Deploy", "API", "Gateway", "to", "AWS", "cloud", "." ]
cd67cf416371337b83cb9ca3f696277125703339
https://github.com/glomex/gcdt/blob/cd67cf416371337b83cb9ca3f696277125703339/gcdt/yugen_core.py#L67-L113
train
glomex/gcdt
gcdt/yugen_core.py
delete_api
def delete_api(awsclient, api_name): """Delete the API. :param api_name: """ _sleep() client_api = awsclient.get_client('apigateway') print('deleting api: %s' % api_name) api = _api_by_name(awsclient, api_name) if api is not None: print(json2table(api)) response = client_api.delete_rest_api( restApiId=api['id'] ) print(json2table(response)) else: print('API name unknown')
python
def delete_api(awsclient, api_name): """Delete the API. :param api_name: """ _sleep() client_api = awsclient.get_client('apigateway') print('deleting api: %s' % api_name) api = _api_by_name(awsclient, api_name) if api is not None: print(json2table(api)) response = client_api.delete_rest_api( restApiId=api['id'] ) print(json2table(response)) else: print('API name unknown')
[ "def", "delete_api", "(", "awsclient", ",", "api_name", ")", ":", "_sleep", "(", ")", "client_api", "=", "awsclient", ".", "get_client", "(", "'apigateway'", ")", "print", "(", "'deleting api: %s'", "%", "api_name", ")", "api", "=", "_api_by_name", "(", "awsclient", ",", "api_name", ")", "if", "api", "is", "not", "None", ":", "print", "(", "json2table", "(", "api", ")", ")", "response", "=", "client_api", ".", "delete_rest_api", "(", "restApiId", "=", "api", "[", "'id'", "]", ")", "print", "(", "json2table", "(", "response", ")", ")", "else", ":", "print", "(", "'API name unknown'", ")" ]
Delete the API. :param api_name:
[ "Delete", "the", "API", "." ]
cd67cf416371337b83cb9ca3f696277125703339
https://github.com/glomex/gcdt/blob/cd67cf416371337b83cb9ca3f696277125703339/gcdt/yugen_core.py#L116-L136
train
glomex/gcdt
gcdt/yugen_core.py
create_api_key
def create_api_key(awsclient, api_name, api_key_name): """Create a new API key as reference for api.conf. :param api_name: :param api_key_name: :return: api_key """ _sleep() client_api = awsclient.get_client('apigateway') print('create api key: %s' % api_key_name) response = client_api.create_api_key( name=api_key_name, description='Created for ' + api_name, enabled=True ) #print(json2table(response)) print('Add this api key \'%s\' to your api.conf' % response['id']) return response['id']
python
def create_api_key(awsclient, api_name, api_key_name): """Create a new API key as reference for api.conf. :param api_name: :param api_key_name: :return: api_key """ _sleep() client_api = awsclient.get_client('apigateway') print('create api key: %s' % api_key_name) response = client_api.create_api_key( name=api_key_name, description='Created for ' + api_name, enabled=True ) #print(json2table(response)) print('Add this api key \'%s\' to your api.conf' % response['id']) return response['id']
[ "def", "create_api_key", "(", "awsclient", ",", "api_name", ",", "api_key_name", ")", ":", "_sleep", "(", ")", "client_api", "=", "awsclient", ".", "get_client", "(", "'apigateway'", ")", "print", "(", "'create api key: %s'", "%", "api_key_name", ")", "response", "=", "client_api", ".", "create_api_key", "(", "name", "=", "api_key_name", ",", "description", "=", "'Created for '", "+", "api_name", ",", "enabled", "=", "True", ")", "#print(json2table(response))", "print", "(", "'Add this api key \\'%s\\' to your api.conf'", "%", "response", "[", "'id'", "]", ")", "return", "response", "[", "'id'", "]" ]
Create a new API key as reference for api.conf. :param api_name: :param api_key_name: :return: api_key
[ "Create", "a", "new", "API", "key", "as", "reference", "for", "api", ".", "conf", "." ]
cd67cf416371337b83cb9ca3f696277125703339
https://github.com/glomex/gcdt/blob/cd67cf416371337b83cb9ca3f696277125703339/gcdt/yugen_core.py#L139-L159
train
glomex/gcdt
gcdt/yugen_core.py
delete_api_key
def delete_api_key(awsclient, api_key): """Remove API key. :param api_key: """ _sleep() client_api = awsclient.get_client('apigateway') print('delete api key: %s' % api_key) response = client_api.delete_api_key( apiKey=api_key ) print(json2table(response))
python
def delete_api_key(awsclient, api_key): """Remove API key. :param api_key: """ _sleep() client_api = awsclient.get_client('apigateway') print('delete api key: %s' % api_key) response = client_api.delete_api_key( apiKey=api_key ) print(json2table(response))
[ "def", "delete_api_key", "(", "awsclient", ",", "api_key", ")", ":", "_sleep", "(", ")", "client_api", "=", "awsclient", ".", "get_client", "(", "'apigateway'", ")", "print", "(", "'delete api key: %s'", "%", "api_key", ")", "response", "=", "client_api", ".", "delete_api_key", "(", "apiKey", "=", "api_key", ")", "print", "(", "json2table", "(", "response", ")", ")" ]
Remove API key. :param api_key:
[ "Remove", "API", "key", "." ]
cd67cf416371337b83cb9ca3f696277125703339
https://github.com/glomex/gcdt/blob/cd67cf416371337b83cb9ca3f696277125703339/gcdt/yugen_core.py#L162-L175
train
glomex/gcdt
gcdt/yugen_core.py
list_api_keys
def list_api_keys(awsclient): """Print the defined API keys. """ _sleep() client_api = awsclient.get_client('apigateway') print('listing api keys') response = client_api.get_api_keys()['items'] for item in response: print(json2table(item))
python
def list_api_keys(awsclient): """Print the defined API keys. """ _sleep() client_api = awsclient.get_client('apigateway') print('listing api keys') response = client_api.get_api_keys()['items'] for item in response: print(json2table(item))
[ "def", "list_api_keys", "(", "awsclient", ")", ":", "_sleep", "(", ")", "client_api", "=", "awsclient", ".", "get_client", "(", "'apigateway'", ")", "print", "(", "'listing api keys'", ")", "response", "=", "client_api", ".", "get_api_keys", "(", ")", "[", "'items'", "]", "for", "item", "in", "response", ":", "print", "(", "json2table", "(", "item", ")", ")" ]
Print the defined API keys.
[ "Print", "the", "defined", "API", "keys", "." ]
cd67cf416371337b83cb9ca3f696277125703339
https://github.com/glomex/gcdt/blob/cd67cf416371337b83cb9ca3f696277125703339/gcdt/yugen_core.py#L178-L188
train
glomex/gcdt
gcdt/yugen_core.py
deploy_custom_domain
def deploy_custom_domain(awsclient, api_name, api_target_stage, api_base_path, domain_name, route_53_record, cert_name, cert_arn, hosted_zone_id, ensure_cname): """Add custom domain to your API. :param api_name: :param api_target_stage: :param api_base_path: :param domain_name: :param route_53_record: :param ssl_cert: :param cert_name: :param cert_arn: :param hosted_zone_id: :return: exit_code """ api_base_path = _basepath_to_string_if_null(api_base_path) api = _api_by_name(awsclient, api_name) if not api: print("Api %s does not exist, aborting..." % api_name) # exit(1) return 1 domain = _custom_domain_name_exists(awsclient, domain_name) if not domain: response = _create_custom_domain(awsclient, domain_name, cert_name, cert_arn) cloudfront_distribution = response['distributionDomainName'] else: response = _update_custom_domain(awsclient, domain_name, cert_name, cert_arn) cloudfront_distribution = response['distributionDomainName'] if _base_path_mapping_exists(awsclient, domain_name, api_base_path): _ensure_correct_base_path_mapping(awsclient, domain_name, api_base_path, api['id'], api_target_stage) else: _create_base_path_mapping(awsclient, domain_name, api_base_path, api_target_stage, api['id']) if ensure_cname: record_exists, record_correct = \ _record_exists_and_correct(awsclient, hosted_zone_id, route_53_record, cloudfront_distribution) if record_correct: print('Route53 record correctly set: %s --> %s' % (route_53_record, cloudfront_distribution)) else: _ensure_correct_route_53_record(awsclient, hosted_zone_id, record_name=route_53_record, record_value=cloudfront_distribution) print('Route53 record set: %s --> %s' % (route_53_record, cloudfront_distribution)) else: print('Skipping creating and checking DNS record') return 0
python
def deploy_custom_domain(awsclient, api_name, api_target_stage, api_base_path, domain_name, route_53_record, cert_name, cert_arn, hosted_zone_id, ensure_cname): """Add custom domain to your API. :param api_name: :param api_target_stage: :param api_base_path: :param domain_name: :param route_53_record: :param ssl_cert: :param cert_name: :param cert_arn: :param hosted_zone_id: :return: exit_code """ api_base_path = _basepath_to_string_if_null(api_base_path) api = _api_by_name(awsclient, api_name) if not api: print("Api %s does not exist, aborting..." % api_name) # exit(1) return 1 domain = _custom_domain_name_exists(awsclient, domain_name) if not domain: response = _create_custom_domain(awsclient, domain_name, cert_name, cert_arn) cloudfront_distribution = response['distributionDomainName'] else: response = _update_custom_domain(awsclient, domain_name, cert_name, cert_arn) cloudfront_distribution = response['distributionDomainName'] if _base_path_mapping_exists(awsclient, domain_name, api_base_path): _ensure_correct_base_path_mapping(awsclient, domain_name, api_base_path, api['id'], api_target_stage) else: _create_base_path_mapping(awsclient, domain_name, api_base_path, api_target_stage, api['id']) if ensure_cname: record_exists, record_correct = \ _record_exists_and_correct(awsclient, hosted_zone_id, route_53_record, cloudfront_distribution) if record_correct: print('Route53 record correctly set: %s --> %s' % (route_53_record, cloudfront_distribution)) else: _ensure_correct_route_53_record(awsclient, hosted_zone_id, record_name=route_53_record, record_value=cloudfront_distribution) print('Route53 record set: %s --> %s' % (route_53_record, cloudfront_distribution)) else: print('Skipping creating and checking DNS record') return 0
[ "def", "deploy_custom_domain", "(", "awsclient", ",", "api_name", ",", "api_target_stage", ",", "api_base_path", ",", "domain_name", ",", "route_53_record", ",", "cert_name", ",", "cert_arn", ",", "hosted_zone_id", ",", "ensure_cname", ")", ":", "api_base_path", "=", "_basepath_to_string_if_null", "(", "api_base_path", ")", "api", "=", "_api_by_name", "(", "awsclient", ",", "api_name", ")", "if", "not", "api", ":", "print", "(", "\"Api %s does not exist, aborting...\"", "%", "api_name", ")", "# exit(1)", "return", "1", "domain", "=", "_custom_domain_name_exists", "(", "awsclient", ",", "domain_name", ")", "if", "not", "domain", ":", "response", "=", "_create_custom_domain", "(", "awsclient", ",", "domain_name", ",", "cert_name", ",", "cert_arn", ")", "cloudfront_distribution", "=", "response", "[", "'distributionDomainName'", "]", "else", ":", "response", "=", "_update_custom_domain", "(", "awsclient", ",", "domain_name", ",", "cert_name", ",", "cert_arn", ")", "cloudfront_distribution", "=", "response", "[", "'distributionDomainName'", "]", "if", "_base_path_mapping_exists", "(", "awsclient", ",", "domain_name", ",", "api_base_path", ")", ":", "_ensure_correct_base_path_mapping", "(", "awsclient", ",", "domain_name", ",", "api_base_path", ",", "api", "[", "'id'", "]", ",", "api_target_stage", ")", "else", ":", "_create_base_path_mapping", "(", "awsclient", ",", "domain_name", ",", "api_base_path", ",", "api_target_stage", ",", "api", "[", "'id'", "]", ")", "if", "ensure_cname", ":", "record_exists", ",", "record_correct", "=", "_record_exists_and_correct", "(", "awsclient", ",", "hosted_zone_id", ",", "route_53_record", ",", "cloudfront_distribution", ")", "if", "record_correct", ":", "print", "(", "'Route53 record correctly set: %s --> %s'", "%", "(", "route_53_record", ",", "cloudfront_distribution", ")", ")", "else", ":", "_ensure_correct_route_53_record", "(", "awsclient", ",", "hosted_zone_id", ",", "record_name", "=", "route_53_record", ",", "record_value", "=", "cloudfront_distribution", ")", "print", "(", "'Route53 record set: %s --> %s'", "%", "(", "route_53_record", ",", "cloudfront_distribution", ")", ")", "else", ":", "print", "(", "'Skipping creating and checking DNS record'", ")", "return", "0" ]
Add custom domain to your API. :param api_name: :param api_target_stage: :param api_base_path: :param domain_name: :param route_53_record: :param ssl_cert: :param cert_name: :param cert_arn: :param hosted_zone_id: :return: exit_code
[ "Add", "custom", "domain", "to", "your", "API", "." ]
cd67cf416371337b83cb9ca3f696277125703339
https://github.com/glomex/gcdt/blob/cd67cf416371337b83cb9ca3f696277125703339/gcdt/yugen_core.py#L191-L249
train
glomex/gcdt
gcdt/yugen_core.py
get_lambdas
def get_lambdas(awsclient, config, add_arn=False): """Get the list of lambda functions. :param config: :param add_arn: :return: list containing lambda entries """ if 'lambda' in config: client_lambda = awsclient.get_client('lambda') lambda_entries = config['lambda'].get('entries', []) lmbdas = [] for lambda_entry in lambda_entries: lmbda = { 'name': lambda_entry.get('name', None), 'alias': lambda_entry.get('alias', None), 'swagger_ref': lambda_entry.get('swaggerRef', None) } if add_arn: _sleep() response_lambda = client_lambda.get_function( FunctionName=lmbda['name']) lmbda['arn'] = response_lambda['Configuration']['FunctionArn'] lmbdas.append(lmbda) return lmbdas else: return []
python
def get_lambdas(awsclient, config, add_arn=False): """Get the list of lambda functions. :param config: :param add_arn: :return: list containing lambda entries """ if 'lambda' in config: client_lambda = awsclient.get_client('lambda') lambda_entries = config['lambda'].get('entries', []) lmbdas = [] for lambda_entry in lambda_entries: lmbda = { 'name': lambda_entry.get('name', None), 'alias': lambda_entry.get('alias', None), 'swagger_ref': lambda_entry.get('swaggerRef', None) } if add_arn: _sleep() response_lambda = client_lambda.get_function( FunctionName=lmbda['name']) lmbda['arn'] = response_lambda['Configuration']['FunctionArn'] lmbdas.append(lmbda) return lmbdas else: return []
[ "def", "get_lambdas", "(", "awsclient", ",", "config", ",", "add_arn", "=", "False", ")", ":", "if", "'lambda'", "in", "config", ":", "client_lambda", "=", "awsclient", ".", "get_client", "(", "'lambda'", ")", "lambda_entries", "=", "config", "[", "'lambda'", "]", ".", "get", "(", "'entries'", ",", "[", "]", ")", "lmbdas", "=", "[", "]", "for", "lambda_entry", "in", "lambda_entries", ":", "lmbda", "=", "{", "'name'", ":", "lambda_entry", ".", "get", "(", "'name'", ",", "None", ")", ",", "'alias'", ":", "lambda_entry", ".", "get", "(", "'alias'", ",", "None", ")", ",", "'swagger_ref'", ":", "lambda_entry", ".", "get", "(", "'swaggerRef'", ",", "None", ")", "}", "if", "add_arn", ":", "_sleep", "(", ")", "response_lambda", "=", "client_lambda", ".", "get_function", "(", "FunctionName", "=", "lmbda", "[", "'name'", "]", ")", "lmbda", "[", "'arn'", "]", "=", "response_lambda", "[", "'Configuration'", "]", "[", "'FunctionArn'", "]", "lmbdas", ".", "append", "(", "lmbda", ")", "return", "lmbdas", "else", ":", "return", "[", "]" ]
Get the list of lambda functions. :param config: :param add_arn: :return: list containing lambda entries
[ "Get", "the", "list", "of", "lambda", "functions", "." ]
cd67cf416371337b83cb9ca3f696277125703339
https://github.com/glomex/gcdt/blob/cd67cf416371337b83cb9ca3f696277125703339/gcdt/yugen_core.py#L252-L277
train
glomex/gcdt
gcdt/yugen_core.py
_update_stage
def _update_stage(awsclient, api_id, stage_name, method_settings): """Helper to apply method_settings to stage :param awsclient: :param api_id: :param stage_name: :param method_settings: :return: """ # settings docs in response: https://botocore.readthedocs.io/en/latest/reference/services/apigateway.html#APIGateway.Client.update_stage client_api = awsclient.get_client('apigateway') operations = _convert_method_settings_into_operations(method_settings) if operations: print('update method settings for stage') _sleep() response = client_api.update_stage( restApiId=api_id, stageName=stage_name, patchOperations=operations)
python
def _update_stage(awsclient, api_id, stage_name, method_settings): """Helper to apply method_settings to stage :param awsclient: :param api_id: :param stage_name: :param method_settings: :return: """ # settings docs in response: https://botocore.readthedocs.io/en/latest/reference/services/apigateway.html#APIGateway.Client.update_stage client_api = awsclient.get_client('apigateway') operations = _convert_method_settings_into_operations(method_settings) if operations: print('update method settings for stage') _sleep() response = client_api.update_stage( restApiId=api_id, stageName=stage_name, patchOperations=operations)
[ "def", "_update_stage", "(", "awsclient", ",", "api_id", ",", "stage_name", ",", "method_settings", ")", ":", "# settings docs in response: https://botocore.readthedocs.io/en/latest/reference/services/apigateway.html#APIGateway.Client.update_stage", "client_api", "=", "awsclient", ".", "get_client", "(", "'apigateway'", ")", "operations", "=", "_convert_method_settings_into_operations", "(", "method_settings", ")", "if", "operations", ":", "print", "(", "'update method settings for stage'", ")", "_sleep", "(", ")", "response", "=", "client_api", ".", "update_stage", "(", "restApiId", "=", "api_id", ",", "stageName", "=", "stage_name", ",", "patchOperations", "=", "operations", ")" ]
Helper to apply method_settings to stage :param awsclient: :param api_id: :param stage_name: :param method_settings: :return:
[ "Helper", "to", "apply", "method_settings", "to", "stage" ]
cd67cf416371337b83cb9ca3f696277125703339
https://github.com/glomex/gcdt/blob/cd67cf416371337b83cb9ca3f696277125703339/gcdt/yugen_core.py#L461-L479
train
glomex/gcdt
gcdt/yugen_core.py
_convert_method_settings_into_operations
def _convert_method_settings_into_operations(method_settings=None): """Helper to handle the conversion of method_settings to operations :param method_settings: :return: list of operations """ # operations docs here: https://tools.ietf.org/html/rfc6902#section-4 operations = [] if method_settings: for method in method_settings.keys(): for key, value in method_settings[method].items(): if isinstance(value, bool): if value: value = 'true' else: value = 'false' operations.append({ 'op': 'replace', 'path': method + _resolve_key(key), 'value': value }) return operations
python
def _convert_method_settings_into_operations(method_settings=None): """Helper to handle the conversion of method_settings to operations :param method_settings: :return: list of operations """ # operations docs here: https://tools.ietf.org/html/rfc6902#section-4 operations = [] if method_settings: for method in method_settings.keys(): for key, value in method_settings[method].items(): if isinstance(value, bool): if value: value = 'true' else: value = 'false' operations.append({ 'op': 'replace', 'path': method + _resolve_key(key), 'value': value }) return operations
[ "def", "_convert_method_settings_into_operations", "(", "method_settings", "=", "None", ")", ":", "# operations docs here: https://tools.ietf.org/html/rfc6902#section-4", "operations", "=", "[", "]", "if", "method_settings", ":", "for", "method", "in", "method_settings", ".", "keys", "(", ")", ":", "for", "key", ",", "value", "in", "method_settings", "[", "method", "]", ".", "items", "(", ")", ":", "if", "isinstance", "(", "value", ",", "bool", ")", ":", "if", "value", ":", "value", "=", "'true'", "else", ":", "value", "=", "'false'", "operations", ".", "append", "(", "{", "'op'", ":", "'replace'", ",", "'path'", ":", "method", "+", "_resolve_key", "(", "key", ")", ",", "'value'", ":", "value", "}", ")", "return", "operations" ]
Helper to handle the conversion of method_settings to operations :param method_settings: :return: list of operations
[ "Helper", "to", "handle", "the", "conversion", "of", "method_settings", "to", "operations" ]
cd67cf416371337b83cb9ca3f696277125703339
https://github.com/glomex/gcdt/blob/cd67cf416371337b83cb9ca3f696277125703339/gcdt/yugen_core.py#L503-L524
train
lincolnloop/salmon
salmon/core/runner.py
generate_settings
def generate_settings(): """ This command is run when ``default_path`` doesn't exist, or ``init`` is run and returns a string representing the default data to put into their settings file. """ conf_file = os.path.join(os.path.dirname(base_settings.__file__), 'example', 'conf.py') conf_template = open(conf_file).read() default_url = 'http://salmon.example.com' site_url = raw_input("What will be the URL for Salmon? [{0}]".format( default_url)) site_url = site_url or default_url secret_key = base64.b64encode(os.urandom(KEY_LENGTH)) api_key = base64.b64encode(os.urandom(KEY_LENGTH)) output = conf_template.format(api_key=api_key, secret_key=secret_key, site_url=site_url) return output
python
def generate_settings(): """ This command is run when ``default_path`` doesn't exist, or ``init`` is run and returns a string representing the default data to put into their settings file. """ conf_file = os.path.join(os.path.dirname(base_settings.__file__), 'example', 'conf.py') conf_template = open(conf_file).read() default_url = 'http://salmon.example.com' site_url = raw_input("What will be the URL for Salmon? [{0}]".format( default_url)) site_url = site_url or default_url secret_key = base64.b64encode(os.urandom(KEY_LENGTH)) api_key = base64.b64encode(os.urandom(KEY_LENGTH)) output = conf_template.format(api_key=api_key, secret_key=secret_key, site_url=site_url) return output
[ "def", "generate_settings", "(", ")", ":", "conf_file", "=", "os", ".", "path", ".", "join", "(", "os", ".", "path", ".", "dirname", "(", "base_settings", ".", "__file__", ")", ",", "'example'", ",", "'conf.py'", ")", "conf_template", "=", "open", "(", "conf_file", ")", ".", "read", "(", ")", "default_url", "=", "'http://salmon.example.com'", "site_url", "=", "raw_input", "(", "\"What will be the URL for Salmon? [{0}]\"", ".", "format", "(", "default_url", ")", ")", "site_url", "=", "site_url", "or", "default_url", "secret_key", "=", "base64", ".", "b64encode", "(", "os", ".", "urandom", "(", "KEY_LENGTH", ")", ")", "api_key", "=", "base64", ".", "b64encode", "(", "os", ".", "urandom", "(", "KEY_LENGTH", ")", ")", "output", "=", "conf_template", ".", "format", "(", "api_key", "=", "api_key", ",", "secret_key", "=", "secret_key", ",", "site_url", "=", "site_url", ")", "return", "output" ]
This command is run when ``default_path`` doesn't exist, or ``init`` is run and returns a string representing the default data to put into their settings file.
[ "This", "command", "is", "run", "when", "default_path", "doesn", "t", "exist", "or", "init", "is", "run", "and", "returns", "a", "string", "representing", "the", "default", "data", "to", "put", "into", "their", "settings", "file", "." ]
62a965ad9716707ea1db4afb5d9646766f29b64b
https://github.com/lincolnloop/salmon/blob/62a965ad9716707ea1db4afb5d9646766f29b64b/salmon/core/runner.py#L11-L28
train
lincolnloop/salmon
salmon/core/runner.py
configure_app
def configure_app(**kwargs): """Builds up the settings using the same method as logan""" sys_args = sys.argv args, command, command_args = parse_args(sys_args[1:]) parser = OptionParser() parser.add_option('--config', metavar='CONFIG') (options, logan_args) = parser.parse_args(args) config_path = options.config logan_configure(config_path=config_path, **kwargs)
python
def configure_app(**kwargs): """Builds up the settings using the same method as logan""" sys_args = sys.argv args, command, command_args = parse_args(sys_args[1:]) parser = OptionParser() parser.add_option('--config', metavar='CONFIG') (options, logan_args) = parser.parse_args(args) config_path = options.config logan_configure(config_path=config_path, **kwargs)
[ "def", "configure_app", "(", "*", "*", "kwargs", ")", ":", "sys_args", "=", "sys", ".", "argv", "args", ",", "command", ",", "command_args", "=", "parse_args", "(", "sys_args", "[", "1", ":", "]", ")", "parser", "=", "OptionParser", "(", ")", "parser", ".", "add_option", "(", "'--config'", ",", "metavar", "=", "'CONFIG'", ")", "(", "options", ",", "logan_args", ")", "=", "parser", ".", "parse_args", "(", "args", ")", "config_path", "=", "options", ".", "config", "logan_configure", "(", "config_path", "=", "config_path", ",", "*", "*", "kwargs", ")" ]
Builds up the settings using the same method as logan
[ "Builds", "up", "the", "settings", "using", "the", "same", "method", "as", "logan" ]
62a965ad9716707ea1db4afb5d9646766f29b64b
https://github.com/lincolnloop/salmon/blob/62a965ad9716707ea1db4afb5d9646766f29b64b/salmon/core/runner.py#L42-L50
train
lincolnloop/salmon
salmon/metrics/models.py
Metric._reset_changes
def _reset_changes(self): """Stores current values for comparison later""" self._original = {} if self.last_updated is not None: self._original['last_updated'] = self.last_updated
python
def _reset_changes(self): """Stores current values for comparison later""" self._original = {} if self.last_updated is not None: self._original['last_updated'] = self.last_updated
[ "def", "_reset_changes", "(", "self", ")", ":", "self", ".", "_original", "=", "{", "}", "if", "self", ".", "last_updated", "is", "not", "None", ":", "self", ".", "_original", "[", "'last_updated'", "]", "=", "self", ".", "last_updated" ]
Stores current values for comparison later
[ "Stores", "current", "values", "for", "comparison", "later" ]
62a965ad9716707ea1db4afb5d9646766f29b64b
https://github.com/lincolnloop/salmon/blob/62a965ad9716707ea1db4afb5d9646766f29b64b/salmon/metrics/models.py#L62-L66
train
lincolnloop/salmon
salmon/metrics/models.py
Metric.whisper_filename
def whisper_filename(self): """Build a file path to the Whisper database""" source_name = self.source_id and self.source.name or '' return get_valid_filename("{0}__{1}.wsp".format(source_name, self.name))
python
def whisper_filename(self): """Build a file path to the Whisper database""" source_name = self.source_id and self.source.name or '' return get_valid_filename("{0}__{1}.wsp".format(source_name, self.name))
[ "def", "whisper_filename", "(", "self", ")", ":", "source_name", "=", "self", ".", "source_id", "and", "self", ".", "source", ".", "name", "or", "''", "return", "get_valid_filename", "(", "\"{0}__{1}.wsp\"", ".", "format", "(", "source_name", ",", "self", ".", "name", ")", ")" ]
Build a file path to the Whisper database
[ "Build", "a", "file", "path", "to", "the", "Whisper", "database" ]
62a965ad9716707ea1db4afb5d9646766f29b64b
https://github.com/lincolnloop/salmon/blob/62a965ad9716707ea1db4afb5d9646766f29b64b/salmon/metrics/models.py#L69-L73
train
lincolnloop/salmon
salmon/metrics/models.py
Metric.get_value_display
def get_value_display(self): """Human friendly value output""" if self.display_as == 'percentage': return '{0}%'.format(self.latest_value) if self.display_as == 'boolean': return bool(self.latest_value) if self.display_as == 'byte': return defaultfilters.filesizeformat(self.latest_value) if self.display_as == 'second': return time.strftime('%H:%M:%S', time.gmtime(self.latest_value)) return self.latest_value
python
def get_value_display(self): """Human friendly value output""" if self.display_as == 'percentage': return '{0}%'.format(self.latest_value) if self.display_as == 'boolean': return bool(self.latest_value) if self.display_as == 'byte': return defaultfilters.filesizeformat(self.latest_value) if self.display_as == 'second': return time.strftime('%H:%M:%S', time.gmtime(self.latest_value)) return self.latest_value
[ "def", "get_value_display", "(", "self", ")", ":", "if", "self", ".", "display_as", "==", "'percentage'", ":", "return", "'{0}%'", ".", "format", "(", "self", ".", "latest_value", ")", "if", "self", ".", "display_as", "==", "'boolean'", ":", "return", "bool", "(", "self", ".", "latest_value", ")", "if", "self", ".", "display_as", "==", "'byte'", ":", "return", "defaultfilters", ".", "filesizeformat", "(", "self", ".", "latest_value", ")", "if", "self", ".", "display_as", "==", "'second'", ":", "return", "time", ".", "strftime", "(", "'%H:%M:%S'", ",", "time", ".", "gmtime", "(", "self", ".", "latest_value", ")", ")", "return", "self", ".", "latest_value" ]
Human friendly value output
[ "Human", "friendly", "value", "output" ]
62a965ad9716707ea1db4afb5d9646766f29b64b
https://github.com/lincolnloop/salmon/blob/62a965ad9716707ea1db4afb5d9646766f29b64b/salmon/metrics/models.py#L95-L105
train
lincolnloop/salmon
salmon/metrics/models.py
Metric.time_between_updates
def time_between_updates(self): """Time between current `last_updated` and previous `last_updated`""" if 'last_updated' not in self._original: return 0 last_update = self._original['last_updated'] this_update = self.last_updated return this_update - last_update
python
def time_between_updates(self): """Time between current `last_updated` and previous `last_updated`""" if 'last_updated' not in self._original: return 0 last_update = self._original['last_updated'] this_update = self.last_updated return this_update - last_update
[ "def", "time_between_updates", "(", "self", ")", ":", "if", "'last_updated'", "not", "in", "self", ".", "_original", ":", "return", "0", "last_update", "=", "self", ".", "_original", "[", "'last_updated'", "]", "this_update", "=", "self", ".", "last_updated", "return", "this_update", "-", "last_update" ]
Time between current `last_updated` and previous `last_updated`
[ "Time", "between", "current", "last_updated", "and", "previous", "last_updated" ]
62a965ad9716707ea1db4afb5d9646766f29b64b
https://github.com/lincolnloop/salmon/blob/62a965ad9716707ea1db4afb5d9646766f29b64b/salmon/metrics/models.py#L107-L113
train
lincolnloop/salmon
salmon/metrics/models.py
Metric.do_transform
def do_transform(self): """Apply the transformation (if it exists) to the latest_value""" if not self.transform: return try: self.latest_value = utils.Transform( expr=self.transform, value=self.latest_value, timedelta=self.time_between_updates().total_seconds()).result() except (TypeError, ValueError): logger.warn("Invalid transformation '%s' for metric %s", self.transfrom, self.pk) self.transform = ''
python
def do_transform(self): """Apply the transformation (if it exists) to the latest_value""" if not self.transform: return try: self.latest_value = utils.Transform( expr=self.transform, value=self.latest_value, timedelta=self.time_between_updates().total_seconds()).result() except (TypeError, ValueError): logger.warn("Invalid transformation '%s' for metric %s", self.transfrom, self.pk) self.transform = ''
[ "def", "do_transform", "(", "self", ")", ":", "if", "not", "self", ".", "transform", ":", "return", "try", ":", "self", ".", "latest_value", "=", "utils", ".", "Transform", "(", "expr", "=", "self", ".", "transform", ",", "value", "=", "self", ".", "latest_value", ",", "timedelta", "=", "self", ".", "time_between_updates", "(", ")", ".", "total_seconds", "(", ")", ")", ".", "result", "(", ")", "except", "(", "TypeError", ",", "ValueError", ")", ":", "logger", ".", "warn", "(", "\"Invalid transformation '%s' for metric %s\"", ",", "self", ".", "transfrom", ",", "self", ".", "pk", ")", "self", ".", "transform", "=", "''" ]
Apply the transformation (if it exists) to the latest_value
[ "Apply", "the", "transformation", "(", "if", "it", "exists", ")", "to", "the", "latest_value" ]
62a965ad9716707ea1db4afb5d9646766f29b64b
https://github.com/lincolnloop/salmon/blob/62a965ad9716707ea1db4afb5d9646766f29b64b/salmon/metrics/models.py#L115-L126
train
lincolnloop/salmon
salmon/metrics/models.py
Metric.do_counter_conversion
def do_counter_conversion(self): """Update latest value to the diff between it and the previous value""" if self.is_counter: if self._previous_counter_value is None: prev_value = self.latest_value else: prev_value = self._previous_counter_value self._previous_counter_value = self.latest_value self.latest_value = self.latest_value - prev_value
python
def do_counter_conversion(self): """Update latest value to the diff between it and the previous value""" if self.is_counter: if self._previous_counter_value is None: prev_value = self.latest_value else: prev_value = self._previous_counter_value self._previous_counter_value = self.latest_value self.latest_value = self.latest_value - prev_value
[ "def", "do_counter_conversion", "(", "self", ")", ":", "if", "self", ".", "is_counter", ":", "if", "self", ".", "_previous_counter_value", "is", "None", ":", "prev_value", "=", "self", ".", "latest_value", "else", ":", "prev_value", "=", "self", ".", "_previous_counter_value", "self", ".", "_previous_counter_value", "=", "self", ".", "latest_value", "self", ".", "latest_value", "=", "self", ".", "latest_value", "-", "prev_value" ]
Update latest value to the diff between it and the previous value
[ "Update", "latest", "value", "to", "the", "diff", "between", "it", "and", "the", "previous", "value" ]
62a965ad9716707ea1db4afb5d9646766f29b64b
https://github.com/lincolnloop/salmon/blob/62a965ad9716707ea1db4afb5d9646766f29b64b/salmon/metrics/models.py#L128-L136
train
ninapavlich/django-imagekit-cropper
imagekit_cropper/registry.py
InstanceSourceGroupRegistry.source_group_receiver
def source_group_receiver(self, sender, source, signal, **kwargs): """ Relay source group signals to the appropriate spec strategy. """ from imagekit.cachefiles import ImageCacheFile source_group = sender # Ignore signals from unregistered groups. if source_group not in self._source_groups: return #OVERRIDE HERE -- pass specs into generator object specs = [generator_registry.get(id, source=source, specs=spec_data_field_hash[id]) for id in self._source_groups[source_group]] callback_name = self._signals[signal] #END OVERRIDE for spec in specs: file = ImageCacheFile(spec) call_strategy_method(file, callback_name)
python
def source_group_receiver(self, sender, source, signal, **kwargs): """ Relay source group signals to the appropriate spec strategy. """ from imagekit.cachefiles import ImageCacheFile source_group = sender # Ignore signals from unregistered groups. if source_group not in self._source_groups: return #OVERRIDE HERE -- pass specs into generator object specs = [generator_registry.get(id, source=source, specs=spec_data_field_hash[id]) for id in self._source_groups[source_group]] callback_name = self._signals[signal] #END OVERRIDE for spec in specs: file = ImageCacheFile(spec) call_strategy_method(file, callback_name)
[ "def", "source_group_receiver", "(", "self", ",", "sender", ",", "source", ",", "signal", ",", "*", "*", "kwargs", ")", ":", "from", "imagekit", ".", "cachefiles", "import", "ImageCacheFile", "source_group", "=", "sender", "# Ignore signals from unregistered groups.", "if", "source_group", "not", "in", "self", ".", "_source_groups", ":", "return", "#OVERRIDE HERE -- pass specs into generator object", "specs", "=", "[", "generator_registry", ".", "get", "(", "id", ",", "source", "=", "source", ",", "specs", "=", "spec_data_field_hash", "[", "id", "]", ")", "for", "id", "in", "self", ".", "_source_groups", "[", "source_group", "]", "]", "callback_name", "=", "self", ".", "_signals", "[", "signal", "]", "#END OVERRIDE", "for", "spec", "in", "specs", ":", "file", "=", "ImageCacheFile", "(", "spec", ")", "call_strategy_method", "(", "file", ",", "callback_name", ")" ]
Relay source group signals to the appropriate spec strategy.
[ "Relay", "source", "group", "signals", "to", "the", "appropriate", "spec", "strategy", "." ]
c1c2dc5c3c4724492052e5d244e9de1cc362dbcc
https://github.com/ninapavlich/django-imagekit-cropper/blob/c1c2dc5c3c4724492052e5d244e9de1cc362dbcc/imagekit_cropper/registry.py#L46-L68
train
lincolnloop/salmon
salmon/metrics/utils.py
Transform.replace_variable
def replace_variable(self, variable): """Substitute variables with numeric values""" if variable == 'x': return self.value if variable == 't': return self.timedelta raise ValueError("Invalid variable %s", variable)
python
def replace_variable(self, variable): """Substitute variables with numeric values""" if variable == 'x': return self.value if variable == 't': return self.timedelta raise ValueError("Invalid variable %s", variable)
[ "def", "replace_variable", "(", "self", ",", "variable", ")", ":", "if", "variable", "==", "'x'", ":", "return", "self", ".", "value", "if", "variable", "==", "'t'", ":", "return", "self", ".", "timedelta", "raise", "ValueError", "(", "\"Invalid variable %s\"", ",", "variable", ")" ]
Substitute variables with numeric values
[ "Substitute", "variables", "with", "numeric", "values" ]
62a965ad9716707ea1db4afb5d9646766f29b64b
https://github.com/lincolnloop/salmon/blob/62a965ad9716707ea1db4afb5d9646766f29b64b/salmon/metrics/utils.py#L17-L23
train
lincolnloop/salmon
salmon/metrics/utils.py
Transform.result
def result(self): """Evaluate expression and return result""" # Module(body=[Expr(value=...)]) return self.eval_(ast.parse(self.expr).body[0].value)
python
def result(self): """Evaluate expression and return result""" # Module(body=[Expr(value=...)]) return self.eval_(ast.parse(self.expr).body[0].value)
[ "def", "result", "(", "self", ")", ":", "# Module(body=[Expr(value=...)])", "return", "self", ".", "eval_", "(", "ast", ".", "parse", "(", "self", ".", "expr", ")", ".", "body", "[", "0", "]", ".", "value", ")" ]
Evaluate expression and return result
[ "Evaluate", "expression", "and", "return", "result" ]
62a965ad9716707ea1db4afb5d9646766f29b64b
https://github.com/lincolnloop/salmon/blob/62a965ad9716707ea1db4afb5d9646766f29b64b/salmon/metrics/utils.py#L25-L28
train
acrazing/dbapi
dbapi/People.py
People.get_people
def get_people(self, user_alias=None): """ 获取用户信息 :param user_alias: 用户ID :return: """ user_alias = user_alias or self.api.user_alias content = self.api.req(API_PEOPLE_HOME % user_alias).content xml = self.api.to_xml(re.sub(b'<br />', b'\n', content)) try: xml_user = xml.xpath('//*[@id="profile"]') if not xml_user: return None else: xml_user = xml_user[0] avatar = first(xml_user.xpath('.//img/@src')) city = first(xml_user.xpath('.//div[@class="user-info"]/a/text()')) city_url = first(xml_user.xpath('.//div[@class="user-info"]/a/@href')) text_created_at = xml_user.xpath('.//div[@class="pl"]/text()')[1] created_at = re.match(r'.+(?=加入)', text_created_at.strip()).group() xml_intro = first(xml.xpath('//*[@id="intro_display"]')) intro = xml_intro.xpath('string(.)') if xml_intro is not None else None nickname = first(xml.xpath('//*[@id="db-usr-profile"]//h1/text()'), '').strip() or None signature = first(xml.xpath('//*[@id="display"]/text()')) xml_contact_count = xml.xpath('//*[@id="friend"]/h2')[0] contact_count = int(re.search(r'成员(\d+)', xml_contact_count.xpath('string(.)')).groups()[0]) text_rev_contact_count = xml.xpath('//p[@class="rev-link"]/a/text()')[0] rev_contact_count = int(re.search(r'(\d+)人关注', text_rev_contact_count.strip()).groups()[0]) return { 'alias': user_alias, 'url': API_PEOPLE_HOME % user_alias, 'avatar': avatar, 'city': city, 'city_url': city_url, 'created_at': created_at, 'intro': intro, 'nickname': nickname, 'signature': signature, 'contact_count': contact_count, 'rev_contact_count': rev_contact_count, } except Exception as e: self.api.logger.exception('parse people meta error: %s' % e)
python
def get_people(self, user_alias=None): """ 获取用户信息 :param user_alias: 用户ID :return: """ user_alias = user_alias or self.api.user_alias content = self.api.req(API_PEOPLE_HOME % user_alias).content xml = self.api.to_xml(re.sub(b'<br />', b'\n', content)) try: xml_user = xml.xpath('//*[@id="profile"]') if not xml_user: return None else: xml_user = xml_user[0] avatar = first(xml_user.xpath('.//img/@src')) city = first(xml_user.xpath('.//div[@class="user-info"]/a/text()')) city_url = first(xml_user.xpath('.//div[@class="user-info"]/a/@href')) text_created_at = xml_user.xpath('.//div[@class="pl"]/text()')[1] created_at = re.match(r'.+(?=加入)', text_created_at.strip()).group() xml_intro = first(xml.xpath('//*[@id="intro_display"]')) intro = xml_intro.xpath('string(.)') if xml_intro is not None else None nickname = first(xml.xpath('//*[@id="db-usr-profile"]//h1/text()'), '').strip() or None signature = first(xml.xpath('//*[@id="display"]/text()')) xml_contact_count = xml.xpath('//*[@id="friend"]/h2')[0] contact_count = int(re.search(r'成员(\d+)', xml_contact_count.xpath('string(.)')).groups()[0]) text_rev_contact_count = xml.xpath('//p[@class="rev-link"]/a/text()')[0] rev_contact_count = int(re.search(r'(\d+)人关注', text_rev_contact_count.strip()).groups()[0]) return { 'alias': user_alias, 'url': API_PEOPLE_HOME % user_alias, 'avatar': avatar, 'city': city, 'city_url': city_url, 'created_at': created_at, 'intro': intro, 'nickname': nickname, 'signature': signature, 'contact_count': contact_count, 'rev_contact_count': rev_contact_count, } except Exception as e: self.api.logger.exception('parse people meta error: %s' % e)
[ "def", "get_people", "(", "self", ",", "user_alias", "=", "None", ")", ":", "user_alias", "=", "user_alias", "or", "self", ".", "api", ".", "user_alias", "content", "=", "self", ".", "api", ".", "req", "(", "API_PEOPLE_HOME", "%", "user_alias", ")", ".", "content", "xml", "=", "self", ".", "api", ".", "to_xml", "(", "re", ".", "sub", "(", "b'<br />'", ",", "b'\\n'", ",", "content", ")", ")", "try", ":", "xml_user", "=", "xml", ".", "xpath", "(", "'//*[@id=\"profile\"]'", ")", "if", "not", "xml_user", ":", "return", "None", "else", ":", "xml_user", "=", "xml_user", "[", "0", "]", "avatar", "=", "first", "(", "xml_user", ".", "xpath", "(", "'.//img/@src'", ")", ")", "city", "=", "first", "(", "xml_user", ".", "xpath", "(", "'.//div[@class=\"user-info\"]/a/text()'", ")", ")", "city_url", "=", "first", "(", "xml_user", ".", "xpath", "(", "'.//div[@class=\"user-info\"]/a/@href'", ")", ")", "text_created_at", "=", "xml_user", ".", "xpath", "(", "'.//div[@class=\"pl\"]/text()'", ")", "[", "1", "]", "created_at", "=", "re", ".", "match", "(", "r'.+(?=加入)', te", "x", "_created_at.str", "i", "p()).", "g", "r", "o", "u", "p()", "", "", "xml_intro", "=", "first", "(", "xml", ".", "xpath", "(", "'//*[@id=\"intro_display\"]'", ")", ")", "intro", "=", "xml_intro", ".", "xpath", "(", "'string(.)'", ")", "if", "xml_intro", "is", "not", "None", "else", "None", "nickname", "=", "first", "(", "xml", ".", "xpath", "(", "'//*[@id=\"db-usr-profile\"]//h1/text()'", ")", ",", "''", ")", ".", "strip", "(", ")", "or", "None", "signature", "=", "first", "(", "xml", ".", "xpath", "(", "'//*[@id=\"display\"]/text()'", ")", ")", "xml_contact_count", "=", "xml", ".", "xpath", "(", "'//*[@id=\"friend\"]/h2'", ")", "[", "0", "]", "contact_count", "=", "int", "(", "re", ".", "search", "(", "r'成员(\\d+)', xm", "l", "contact_count.xpa", "t", "h('st", "r", "ing(.)')).g", "r", "o", "u", "ps()[0", "]", ")", "", "", "", "", "text_rev_contact_count", "=", "xml", ".", "xpath", "(", "'//p[@class=\"rev-link\"]/a/text()'", ")", "[", "0", "]", "rev_contact_count", "=", "int", "(", "re", ".", "search", "(", "r'(\\d+)人关注', text", "_", "ev_contact_count.strip", "(", ")).gr", "o", "u", "p", "s", "()[0])", "", "", "", "", "", "", "return", "{", "'alias'", ":", "user_alias", ",", "'url'", ":", "API_PEOPLE_HOME", "%", "user_alias", ",", "'avatar'", ":", "avatar", ",", "'city'", ":", "city", ",", "'city_url'", ":", "city_url", ",", "'created_at'", ":", "created_at", ",", "'intro'", ":", "intro", ",", "'nickname'", ":", "nickname", ",", "'signature'", ":", "signature", ",", "'contact_count'", ":", "contact_count", ",", "'rev_contact_count'", ":", "rev_contact_count", ",", "}", "except", "Exception", "as", "e", ":", "self", ".", "api", ".", "logger", ".", "exception", "(", "'parse people meta error: %s'", "%", "e", ")" ]
获取用户信息 :param user_alias: 用户ID :return:
[ "获取用户信息", ":", "param", "user_alias", ":", "用户ID", ":", "return", ":" ]
8c1f85cb1a051daf7be1fc97a62c4499983e9898
https://github.com/acrazing/dbapi/blob/8c1f85cb1a051daf7be1fc97a62c4499983e9898/dbapi/People.py#L16-L59
train
matthiask/django-authlib
authlib/views.py
email_login
def email_login(request, *, email, **kwargs): """ Given a request, an email and optionally some additional data, ensure that a user with the email address exists, and authenticate & login them right away if the user is active. Returns a tuple consisting of ``(user, created)`` upon success or ``(None, None)`` when authentication fails. """ _u, created = auth.get_user_model()._default_manager.get_or_create(email=email) user = auth.authenticate(request, email=email) if user and user.is_active: # The is_active check is possibly redundant. auth.login(request, user) return user, created return None, None
python
def email_login(request, *, email, **kwargs): """ Given a request, an email and optionally some additional data, ensure that a user with the email address exists, and authenticate & login them right away if the user is active. Returns a tuple consisting of ``(user, created)`` upon success or ``(None, None)`` when authentication fails. """ _u, created = auth.get_user_model()._default_manager.get_or_create(email=email) user = auth.authenticate(request, email=email) if user and user.is_active: # The is_active check is possibly redundant. auth.login(request, user) return user, created return None, None
[ "def", "email_login", "(", "request", ",", "*", ",", "email", ",", "*", "*", "kwargs", ")", ":", "_u", ",", "created", "=", "auth", ".", "get_user_model", "(", ")", ".", "_default_manager", ".", "get_or_create", "(", "email", "=", "email", ")", "user", "=", "auth", ".", "authenticate", "(", "request", ",", "email", "=", "email", ")", "if", "user", "and", "user", ".", "is_active", ":", "# The is_active check is possibly redundant.", "auth", ".", "login", "(", "request", ",", "user", ")", "return", "user", ",", "created", "return", "None", ",", "None" ]
Given a request, an email and optionally some additional data, ensure that a user with the email address exists, and authenticate & login them right away if the user is active. Returns a tuple consisting of ``(user, created)`` upon success or ``(None, None)`` when authentication fails.
[ "Given", "a", "request", "an", "email", "and", "optionally", "some", "additional", "data", "ensure", "that", "a", "user", "with", "the", "email", "address", "exists", "and", "authenticate", "&", "login", "them", "right", "away", "if", "the", "user", "is", "active", "." ]
a142da7e27fe9d30f34a84b12f24f686f9d2c8e1
https://github.com/matthiask/django-authlib/blob/a142da7e27fe9d30f34a84b12f24f686f9d2c8e1/authlib/views.py#L54-L68
train
lincolnloop/salmon
salmon/metrics/views.py
dashboard
def dashboard(request): """Shows the latest results for each source""" sources = (models.Source.objects.all().prefetch_related('metric_set') .order_by('name')) metrics = SortedDict([(src, src.metric_set.all()) for src in sources]) no_source_metrics = models.Metric.objects.filter(source__isnull=True) if no_source_metrics: metrics[''] = no_source_metrics if request.META.get('HTTP_X_PJAX', False): parent_template = 'pjax.html' else: parent_template = 'base.html' return render(request, 'metrics/dashboard.html', { 'source_metrics': metrics, 'parent_template': parent_template })
python
def dashboard(request): """Shows the latest results for each source""" sources = (models.Source.objects.all().prefetch_related('metric_set') .order_by('name')) metrics = SortedDict([(src, src.metric_set.all()) for src in sources]) no_source_metrics = models.Metric.objects.filter(source__isnull=True) if no_source_metrics: metrics[''] = no_source_metrics if request.META.get('HTTP_X_PJAX', False): parent_template = 'pjax.html' else: parent_template = 'base.html' return render(request, 'metrics/dashboard.html', { 'source_metrics': metrics, 'parent_template': parent_template })
[ "def", "dashboard", "(", "request", ")", ":", "sources", "=", "(", "models", ".", "Source", ".", "objects", ".", "all", "(", ")", ".", "prefetch_related", "(", "'metric_set'", ")", ".", "order_by", "(", "'name'", ")", ")", "metrics", "=", "SortedDict", "(", "[", "(", "src", ",", "src", ".", "metric_set", ".", "all", "(", ")", ")", "for", "src", "in", "sources", "]", ")", "no_source_metrics", "=", "models", ".", "Metric", ".", "objects", ".", "filter", "(", "source__isnull", "=", "True", ")", "if", "no_source_metrics", ":", "metrics", "[", "''", "]", "=", "no_source_metrics", "if", "request", ".", "META", ".", "get", "(", "'HTTP_X_PJAX'", ",", "False", ")", ":", "parent_template", "=", "'pjax.html'", "else", ":", "parent_template", "=", "'base.html'", "return", "render", "(", "request", ",", "'metrics/dashboard.html'", ",", "{", "'source_metrics'", ":", "metrics", ",", "'parent_template'", ":", "parent_template", "}", ")" ]
Shows the latest results for each source
[ "Shows", "the", "latest", "results", "for", "each", "source" ]
62a965ad9716707ea1db4afb5d9646766f29b64b
https://github.com/lincolnloop/salmon/blob/62a965ad9716707ea1db4afb5d9646766f29b64b/salmon/metrics/views.py#L32-L48
train
lincolnloop/salmon
salmon/core/graph.py
WhisperDatabase._create
def _create(self): """Create the Whisper file on disk""" if not os.path.exists(settings.SALMON_WHISPER_DB_PATH): os.makedirs(settings.SALMON_WHISPER_DB_PATH) archives = [whisper.parseRetentionDef(retentionDef) for retentionDef in settings.ARCHIVES.split(",")] whisper.create(self.path, archives, xFilesFactor=settings.XFILEFACTOR, aggregationMethod=settings.AGGREGATION_METHOD)
python
def _create(self): """Create the Whisper file on disk""" if not os.path.exists(settings.SALMON_WHISPER_DB_PATH): os.makedirs(settings.SALMON_WHISPER_DB_PATH) archives = [whisper.parseRetentionDef(retentionDef) for retentionDef in settings.ARCHIVES.split(",")] whisper.create(self.path, archives, xFilesFactor=settings.XFILEFACTOR, aggregationMethod=settings.AGGREGATION_METHOD)
[ "def", "_create", "(", "self", ")", ":", "if", "not", "os", ".", "path", ".", "exists", "(", "settings", ".", "SALMON_WHISPER_DB_PATH", ")", ":", "os", ".", "makedirs", "(", "settings", ".", "SALMON_WHISPER_DB_PATH", ")", "archives", "=", "[", "whisper", ".", "parseRetentionDef", "(", "retentionDef", ")", "for", "retentionDef", "in", "settings", ".", "ARCHIVES", ".", "split", "(", "\",\"", ")", "]", "whisper", ".", "create", "(", "self", ".", "path", ",", "archives", ",", "xFilesFactor", "=", "settings", ".", "XFILEFACTOR", ",", "aggregationMethod", "=", "settings", ".", "AGGREGATION_METHOD", ")" ]
Create the Whisper file on disk
[ "Create", "the", "Whisper", "file", "on", "disk" ]
62a965ad9716707ea1db4afb5d9646766f29b64b
https://github.com/lincolnloop/salmon/blob/62a965ad9716707ea1db4afb5d9646766f29b64b/salmon/core/graph.py#L18-L26
train
lincolnloop/salmon
salmon/core/graph.py
WhisperDatabase._update
def _update(self, datapoints): """ This method store in the datapoints in the current database. :datapoints: is a list of tupple with the epoch timestamp and value [(1368977629,10)] """ if len(datapoints) == 1: timestamp, value = datapoints[0] whisper.update(self.path, value, timestamp) else: whisper.update_many(self.path, datapoints)
python
def _update(self, datapoints): """ This method store in the datapoints in the current database. :datapoints: is a list of tupple with the epoch timestamp and value [(1368977629,10)] """ if len(datapoints) == 1: timestamp, value = datapoints[0] whisper.update(self.path, value, timestamp) else: whisper.update_many(self.path, datapoints)
[ "def", "_update", "(", "self", ",", "datapoints", ")", ":", "if", "len", "(", "datapoints", ")", "==", "1", ":", "timestamp", ",", "value", "=", "datapoints", "[", "0", "]", "whisper", ".", "update", "(", "self", ".", "path", ",", "value", ",", "timestamp", ")", "else", ":", "whisper", ".", "update_many", "(", "self", ".", "path", ",", "datapoints", ")" ]
This method store in the datapoints in the current database. :datapoints: is a list of tupple with the epoch timestamp and value [(1368977629,10)]
[ "This", "method", "store", "in", "the", "datapoints", "in", "the", "current", "database", "." ]
62a965ad9716707ea1db4afb5d9646766f29b64b
https://github.com/lincolnloop/salmon/blob/62a965ad9716707ea1db4afb5d9646766f29b64b/salmon/core/graph.py#L31-L42
train
lincolnloop/salmon
salmon/core/graph.py
WhisperDatabase.fetch
def fetch(self, from_time, until_time=None): """ This method fetch data from the database according to the period given fetch(path, fromTime, untilTime=None) fromTime is an datetime untilTime is also an datetime, but defaults to now. Returns a tuple of (timeInfo, valueList) where timeInfo is itself a tuple of (fromTime, untilTime, step) Returns None if no data can be returned """ until_time = until_time or datetime.now() time_info, values = whisper.fetch(self.path, from_time.strftime('%s'), until_time.strftime('%s')) # build up a list of (timestamp, value) start_time, end_time, step = time_info current = start_time times = [] while current <= end_time: times.append(current) current += step return zip(times, values)
python
def fetch(self, from_time, until_time=None): """ This method fetch data from the database according to the period given fetch(path, fromTime, untilTime=None) fromTime is an datetime untilTime is also an datetime, but defaults to now. Returns a tuple of (timeInfo, valueList) where timeInfo is itself a tuple of (fromTime, untilTime, step) Returns None if no data can be returned """ until_time = until_time or datetime.now() time_info, values = whisper.fetch(self.path, from_time.strftime('%s'), until_time.strftime('%s')) # build up a list of (timestamp, value) start_time, end_time, step = time_info current = start_time times = [] while current <= end_time: times.append(current) current += step return zip(times, values)
[ "def", "fetch", "(", "self", ",", "from_time", ",", "until_time", "=", "None", ")", ":", "until_time", "=", "until_time", "or", "datetime", ".", "now", "(", ")", "time_info", ",", "values", "=", "whisper", ".", "fetch", "(", "self", ".", "path", ",", "from_time", ".", "strftime", "(", "'%s'", ")", ",", "until_time", ".", "strftime", "(", "'%s'", ")", ")", "# build up a list of (timestamp, value)", "start_time", ",", "end_time", ",", "step", "=", "time_info", "current", "=", "start_time", "times", "=", "[", "]", "while", "current", "<=", "end_time", ":", "times", ".", "append", "(", "current", ")", "current", "+=", "step", "return", "zip", "(", "times", ",", "values", ")" ]
This method fetch data from the database according to the period given fetch(path, fromTime, untilTime=None) fromTime is an datetime untilTime is also an datetime, but defaults to now. Returns a tuple of (timeInfo, valueList) where timeInfo is itself a tuple of (fromTime, untilTime, step) Returns None if no data can be returned
[ "This", "method", "fetch", "data", "from", "the", "database", "according", "to", "the", "period", "given" ]
62a965ad9716707ea1db4afb5d9646766f29b64b
https://github.com/lincolnloop/salmon/blob/62a965ad9716707ea1db4afb5d9646766f29b64b/salmon/core/graph.py#L44-L70
train
acrazing/dbapi
dbapi/utils.py
build_list_result
def build_list_result(results, xml): """ 构建带翻页的列表 :param results: 已获取的数据列表 :param xml: 原始页面xml :return: {'results': list, 'count': int, 'next_start': int|None} 如果count与results长度不同,则有更多 如果next_start不为None,则可以到下一页 """ xml_count = xml.xpath('//div[@class="paginator"]/span[@class="count"]/text()') xml_next = xml.xpath('//div[@class="paginator"]/span[@class="next"]/a/@href') count = int(re.search(r'\d+', xml_count[0]).group()) if xml_count else len(results) next_start = int(re.search(r'start=(\d+)', xml_next[0]).groups()[0]) if xml_next else None return {'results': results, 'count': count, 'next_start': next_start}
python
def build_list_result(results, xml): """ 构建带翻页的列表 :param results: 已获取的数据列表 :param xml: 原始页面xml :return: {'results': list, 'count': int, 'next_start': int|None} 如果count与results长度不同,则有更多 如果next_start不为None,则可以到下一页 """ xml_count = xml.xpath('//div[@class="paginator"]/span[@class="count"]/text()') xml_next = xml.xpath('//div[@class="paginator"]/span[@class="next"]/a/@href') count = int(re.search(r'\d+', xml_count[0]).group()) if xml_count else len(results) next_start = int(re.search(r'start=(\d+)', xml_next[0]).groups()[0]) if xml_next else None return {'results': results, 'count': count, 'next_start': next_start}
[ "def", "build_list_result", "(", "results", ",", "xml", ")", ":", "xml_count", "=", "xml", ".", "xpath", "(", "'//div[@class=\"paginator\"]/span[@class=\"count\"]/text()'", ")", "xml_next", "=", "xml", ".", "xpath", "(", "'//div[@class=\"paginator\"]/span[@class=\"next\"]/a/@href'", ")", "count", "=", "int", "(", "re", ".", "search", "(", "r'\\d+'", ",", "xml_count", "[", "0", "]", ")", ".", "group", "(", ")", ")", "if", "xml_count", "else", "len", "(", "results", ")", "next_start", "=", "int", "(", "re", ".", "search", "(", "r'start=(\\d+)'", ",", "xml_next", "[", "0", "]", ")", ".", "groups", "(", ")", "[", "0", "]", ")", "if", "xml_next", "else", "None", "return", "{", "'results'", ":", "results", ",", "'count'", ":", "count", ",", "'next_start'", ":", "next_start", "}" ]
构建带翻页的列表 :param results: 已获取的数据列表 :param xml: 原始页面xml :return: {'results': list, 'count': int, 'next_start': int|None} 如果count与results长度不同,则有更多 如果next_start不为None,则可以到下一页
[ "构建带翻页的列表", ":", "param", "results", ":", "已获取的数据列表", ":", "param", "xml", ":", "原始页面xml", ":", "return", ":", "{", "results", ":", "list", "count", ":", "int", "next_start", ":", "int|None", "}", "如果count与results长度不同,则有更多", "如果next_start不为None,则可以到下一页" ]
8c1f85cb1a051daf7be1fc97a62c4499983e9898
https://github.com/acrazing/dbapi/blob/8c1f85cb1a051daf7be1fc97a62c4499983e9898/dbapi/utils.py#L25-L39
train
DeepHorizons/iarm
iarm/arm_instructions/arithmetic.py
Arithmetic.ADCS
def ADCS(self, params): """ ADCS [Ra,] Rb, Rc Add Rb and Rc + the carry bit and store the result in Ra Ra, Rb, and Rc must be low registers if Ra is omitted, then it is assumed to be Rb """ # This instruction allows for an optional destination register # If it is omitted, then it is assumed to be Rb # As defined in http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0662b/index.html try: Ra, Rb, Rc = self.get_three_parameters(self.THREE_PARAMETER_COMMA_SEPARATED, params) except iarm.exceptions.ParsingError: Rb, Rc = self.get_two_parameters(self.TWO_PARAMETER_COMMA_SEPARATED, params) Ra = Rb self.check_arguments(low_registers=(Ra, Rc)) self.match_first_two_parameters(Ra, Rb) # ADCS Ra, Ra, Rb def ADCS_func(): # TODO need to rethink the set_NZCV with the C flag oper_1 = self.register[Ra] oper_2 = self.register[Rc] self.register[Ra] = oper_1 + oper_2 self.register[Ra] += 1 if self.is_C_set() else 0 self.set_NZCV_flags(oper_1, oper_2, self.register[Ra], 'add') return ADCS_func
python
def ADCS(self, params): """ ADCS [Ra,] Rb, Rc Add Rb and Rc + the carry bit and store the result in Ra Ra, Rb, and Rc must be low registers if Ra is omitted, then it is assumed to be Rb """ # This instruction allows for an optional destination register # If it is omitted, then it is assumed to be Rb # As defined in http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0662b/index.html try: Ra, Rb, Rc = self.get_three_parameters(self.THREE_PARAMETER_COMMA_SEPARATED, params) except iarm.exceptions.ParsingError: Rb, Rc = self.get_two_parameters(self.TWO_PARAMETER_COMMA_SEPARATED, params) Ra = Rb self.check_arguments(low_registers=(Ra, Rc)) self.match_first_two_parameters(Ra, Rb) # ADCS Ra, Ra, Rb def ADCS_func(): # TODO need to rethink the set_NZCV with the C flag oper_1 = self.register[Ra] oper_2 = self.register[Rc] self.register[Ra] = oper_1 + oper_2 self.register[Ra] += 1 if self.is_C_set() else 0 self.set_NZCV_flags(oper_1, oper_2, self.register[Ra], 'add') return ADCS_func
[ "def", "ADCS", "(", "self", ",", "params", ")", ":", "# This instruction allows for an optional destination register", "# If it is omitted, then it is assumed to be Rb", "# As defined in http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0662b/index.html", "try", ":", "Ra", ",", "Rb", ",", "Rc", "=", "self", ".", "get_three_parameters", "(", "self", ".", "THREE_PARAMETER_COMMA_SEPARATED", ",", "params", ")", "except", "iarm", ".", "exceptions", ".", "ParsingError", ":", "Rb", ",", "Rc", "=", "self", ".", "get_two_parameters", "(", "self", ".", "TWO_PARAMETER_COMMA_SEPARATED", ",", "params", ")", "Ra", "=", "Rb", "self", ".", "check_arguments", "(", "low_registers", "=", "(", "Ra", ",", "Rc", ")", ")", "self", ".", "match_first_two_parameters", "(", "Ra", ",", "Rb", ")", "# ADCS Ra, Ra, Rb", "def", "ADCS_func", "(", ")", ":", "# TODO need to rethink the set_NZCV with the C flag", "oper_1", "=", "self", ".", "register", "[", "Ra", "]", "oper_2", "=", "self", ".", "register", "[", "Rc", "]", "self", ".", "register", "[", "Ra", "]", "=", "oper_1", "+", "oper_2", "self", ".", "register", "[", "Ra", "]", "+=", "1", "if", "self", ".", "is_C_set", "(", ")", "else", "0", "self", ".", "set_NZCV_flags", "(", "oper_1", ",", "oper_2", ",", "self", ".", "register", "[", "Ra", "]", ",", "'add'", ")", "return", "ADCS_func" ]
ADCS [Ra,] Rb, Rc Add Rb and Rc + the carry bit and store the result in Ra Ra, Rb, and Rc must be low registers if Ra is omitted, then it is assumed to be Rb
[ "ADCS", "[", "Ra", "]", "Rb", "Rc" ]
b913c9fd577b793a6bbced78b78a5d8d7cd88de4
https://github.com/DeepHorizons/iarm/blob/b913c9fd577b793a6bbced78b78a5d8d7cd88de4/iarm/arm_instructions/arithmetic.py#L6-L35
train
DeepHorizons/iarm
iarm/arm_instructions/arithmetic.py
Arithmetic.ADD
def ADD(self, params): """ ADD [Rx,] Ry, [Rz, PC] ADD [Rx,] [SP, PC], #imm10_4 ADD [SP,] SP, #imm9_4 Add Ry and Rz and store the result in Rx Rx, Ry, and Rz can be any register If Rx is omitted, then it is assumed to be Ry """ # This instruction allows for an optional destination register # If it is omitted, then it is assumed to be Rb # As defined in http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0662b/index.html # TODO can we have ADD SP, #imm9_4? try: Rx, Ry, Rz = self.get_three_parameters(self.THREE_PARAMETER_COMMA_SEPARATED, params) except iarm.exceptions.ParsingError: Ry, Rz = self.get_two_parameters(self.TWO_PARAMETER_COMMA_SEPARATED, params) Rx = Ry if self.is_register(Rz): # ADD Rx, Ry, Rz self.check_arguments(any_registers=(Rx, Ry, Rz)) if Rx != Ry: raise iarm.exceptions.RuleError("Second parameter {} does not equal first parameter {}". format(Ry, Rx)) def ADD_func(): self.register[Rx] = self.register[Ry] + self.register[Rz] else: if Rx == 'SP': # ADD SP, SP, #imm9_4 self.check_arguments(imm9_4=(Rz,)) if Rx != Ry: raise iarm.exceptions.RuleError("Second parameter {} is not SP".format(Ry)) else: # ADD Rx, [SP, PC], #imm10_4 self.check_arguments(any_registers=(Rx,), imm10_4=(Rz,)) if Ry not in ('SP', 'PC'): raise iarm.exceptions.RuleError("Second parameter {} is not SP or PC".format(Ry)) def ADD_func(): self.register[Rx] = self.register[Ry] + self.convert_to_integer(Rz[1:]) return ADD_func
python
def ADD(self, params): """ ADD [Rx,] Ry, [Rz, PC] ADD [Rx,] [SP, PC], #imm10_4 ADD [SP,] SP, #imm9_4 Add Ry and Rz and store the result in Rx Rx, Ry, and Rz can be any register If Rx is omitted, then it is assumed to be Ry """ # This instruction allows for an optional destination register # If it is omitted, then it is assumed to be Rb # As defined in http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0662b/index.html # TODO can we have ADD SP, #imm9_4? try: Rx, Ry, Rz = self.get_three_parameters(self.THREE_PARAMETER_COMMA_SEPARATED, params) except iarm.exceptions.ParsingError: Ry, Rz = self.get_two_parameters(self.TWO_PARAMETER_COMMA_SEPARATED, params) Rx = Ry if self.is_register(Rz): # ADD Rx, Ry, Rz self.check_arguments(any_registers=(Rx, Ry, Rz)) if Rx != Ry: raise iarm.exceptions.RuleError("Second parameter {} does not equal first parameter {}". format(Ry, Rx)) def ADD_func(): self.register[Rx] = self.register[Ry] + self.register[Rz] else: if Rx == 'SP': # ADD SP, SP, #imm9_4 self.check_arguments(imm9_4=(Rz,)) if Rx != Ry: raise iarm.exceptions.RuleError("Second parameter {} is not SP".format(Ry)) else: # ADD Rx, [SP, PC], #imm10_4 self.check_arguments(any_registers=(Rx,), imm10_4=(Rz,)) if Ry not in ('SP', 'PC'): raise iarm.exceptions.RuleError("Second parameter {} is not SP or PC".format(Ry)) def ADD_func(): self.register[Rx] = self.register[Ry] + self.convert_to_integer(Rz[1:]) return ADD_func
[ "def", "ADD", "(", "self", ",", "params", ")", ":", "# This instruction allows for an optional destination register", "# If it is omitted, then it is assumed to be Rb", "# As defined in http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0662b/index.html", "# TODO can we have ADD SP, #imm9_4?", "try", ":", "Rx", ",", "Ry", ",", "Rz", "=", "self", ".", "get_three_parameters", "(", "self", ".", "THREE_PARAMETER_COMMA_SEPARATED", ",", "params", ")", "except", "iarm", ".", "exceptions", ".", "ParsingError", ":", "Ry", ",", "Rz", "=", "self", ".", "get_two_parameters", "(", "self", ".", "TWO_PARAMETER_COMMA_SEPARATED", ",", "params", ")", "Rx", "=", "Ry", "if", "self", ".", "is_register", "(", "Rz", ")", ":", "# ADD Rx, Ry, Rz", "self", ".", "check_arguments", "(", "any_registers", "=", "(", "Rx", ",", "Ry", ",", "Rz", ")", ")", "if", "Rx", "!=", "Ry", ":", "raise", "iarm", ".", "exceptions", ".", "RuleError", "(", "\"Second parameter {} does not equal first parameter {}\"", ".", "format", "(", "Ry", ",", "Rx", ")", ")", "def", "ADD_func", "(", ")", ":", "self", ".", "register", "[", "Rx", "]", "=", "self", ".", "register", "[", "Ry", "]", "+", "self", ".", "register", "[", "Rz", "]", "else", ":", "if", "Rx", "==", "'SP'", ":", "# ADD SP, SP, #imm9_4", "self", ".", "check_arguments", "(", "imm9_4", "=", "(", "Rz", ",", ")", ")", "if", "Rx", "!=", "Ry", ":", "raise", "iarm", ".", "exceptions", ".", "RuleError", "(", "\"Second parameter {} is not SP\"", ".", "format", "(", "Ry", ")", ")", "else", ":", "# ADD Rx, [SP, PC], #imm10_4", "self", ".", "check_arguments", "(", "any_registers", "=", "(", "Rx", ",", ")", ",", "imm10_4", "=", "(", "Rz", ",", ")", ")", "if", "Ry", "not", "in", "(", "'SP'", ",", "'PC'", ")", ":", "raise", "iarm", ".", "exceptions", ".", "RuleError", "(", "\"Second parameter {} is not SP or PC\"", ".", "format", "(", "Ry", ")", ")", "def", "ADD_func", "(", ")", ":", "self", ".", "register", "[", "Rx", "]", "=", "self", ".", "register", "[", "Ry", "]", "+", "self", ".", "convert_to_integer", "(", "Rz", "[", "1", ":", "]", ")", "return", "ADD_func" ]
ADD [Rx,] Ry, [Rz, PC] ADD [Rx,] [SP, PC], #imm10_4 ADD [SP,] SP, #imm9_4 Add Ry and Rz and store the result in Rx Rx, Ry, and Rz can be any register If Rx is omitted, then it is assumed to be Ry
[ "ADD", "[", "Rx", "]", "Ry", "[", "Rz", "PC", "]", "ADD", "[", "Rx", "]", "[", "SP", "PC", "]", "#imm10_4", "ADD", "[", "SP", "]", "SP", "#imm9_4" ]
b913c9fd577b793a6bbced78b78a5d8d7cd88de4
https://github.com/DeepHorizons/iarm/blob/b913c9fd577b793a6bbced78b78a5d8d7cd88de4/iarm/arm_instructions/arithmetic.py#L37-L80
train
DeepHorizons/iarm
iarm/arm_instructions/arithmetic.py
Arithmetic.CMN
def CMN(self, params): """ CMN Ra, Rb Add the two registers and set the NZCV flags The result is discarded Ra and Rb must be low registers """ Ra, Rb = self.get_two_parameters(self.TWO_PARAMETER_COMMA_SEPARATED, params) self.check_arguments(low_registers=(Ra, Rb)) # CMN Ra, Rb def CMN_func(): self.set_NZCV_flags(self.register[Ra], self.register[Rb], self.register[Ra] + self.register[Rb], 'add') return CMN_func
python
def CMN(self, params): """ CMN Ra, Rb Add the two registers and set the NZCV flags The result is discarded Ra and Rb must be low registers """ Ra, Rb = self.get_two_parameters(self.TWO_PARAMETER_COMMA_SEPARATED, params) self.check_arguments(low_registers=(Ra, Rb)) # CMN Ra, Rb def CMN_func(): self.set_NZCV_flags(self.register[Ra], self.register[Rb], self.register[Ra] + self.register[Rb], 'add') return CMN_func
[ "def", "CMN", "(", "self", ",", "params", ")", ":", "Ra", ",", "Rb", "=", "self", ".", "get_two_parameters", "(", "self", ".", "TWO_PARAMETER_COMMA_SEPARATED", ",", "params", ")", "self", ".", "check_arguments", "(", "low_registers", "=", "(", "Ra", ",", "Rb", ")", ")", "# CMN Ra, Rb", "def", "CMN_func", "(", ")", ":", "self", ".", "set_NZCV_flags", "(", "self", ".", "register", "[", "Ra", "]", ",", "self", ".", "register", "[", "Rb", "]", ",", "self", ".", "register", "[", "Ra", "]", "+", "self", ".", "register", "[", "Rb", "]", ",", "'add'", ")", "return", "CMN_func" ]
CMN Ra, Rb Add the two registers and set the NZCV flags The result is discarded Ra and Rb must be low registers
[ "CMN", "Ra", "Rb" ]
b913c9fd577b793a6bbced78b78a5d8d7cd88de4
https://github.com/DeepHorizons/iarm/blob/b913c9fd577b793a6bbced78b78a5d8d7cd88de4/iarm/arm_instructions/arithmetic.py#L132-L149
train
DeepHorizons/iarm
iarm/arm_instructions/arithmetic.py
Arithmetic.CMP
def CMP(self, params): """ CMP Rm, Rn CMP Rm, #imm8 Subtract Rn or imm8 from Rm, set the NZCV flags, and discard the result Rm and Rn can be R0-R14 """ Rm, Rn = self.get_two_parameters(self.TWO_PARAMETER_COMMA_SEPARATED, params) if self.is_register(Rn): # CMP Rm, Rn self.check_arguments(R0_thru_R14=(Rm, Rn)) def CMP_func(): self.set_NZCV_flags(self.register[Rm], self.register[Rn], self.register[Rm] - self.register[Rn], 'sub') else: # CMP Rm, #imm8 self.check_arguments(R0_thru_R14=(Rm,), imm8=(Rn,)) def CMP_func(): tmp = self.convert_to_integer(Rn[1:]) self.set_NZCV_flags(self.register[Rm], tmp, self.register[Rm] - tmp, 'sub') return CMP_func
python
def CMP(self, params): """ CMP Rm, Rn CMP Rm, #imm8 Subtract Rn or imm8 from Rm, set the NZCV flags, and discard the result Rm and Rn can be R0-R14 """ Rm, Rn = self.get_two_parameters(self.TWO_PARAMETER_COMMA_SEPARATED, params) if self.is_register(Rn): # CMP Rm, Rn self.check_arguments(R0_thru_R14=(Rm, Rn)) def CMP_func(): self.set_NZCV_flags(self.register[Rm], self.register[Rn], self.register[Rm] - self.register[Rn], 'sub') else: # CMP Rm, #imm8 self.check_arguments(R0_thru_R14=(Rm,), imm8=(Rn,)) def CMP_func(): tmp = self.convert_to_integer(Rn[1:]) self.set_NZCV_flags(self.register[Rm], tmp, self.register[Rm] - tmp, 'sub') return CMP_func
[ "def", "CMP", "(", "self", ",", "params", ")", ":", "Rm", ",", "Rn", "=", "self", ".", "get_two_parameters", "(", "self", ".", "TWO_PARAMETER_COMMA_SEPARATED", ",", "params", ")", "if", "self", ".", "is_register", "(", "Rn", ")", ":", "# CMP Rm, Rn", "self", ".", "check_arguments", "(", "R0_thru_R14", "=", "(", "Rm", ",", "Rn", ")", ")", "def", "CMP_func", "(", ")", ":", "self", ".", "set_NZCV_flags", "(", "self", ".", "register", "[", "Rm", "]", ",", "self", ".", "register", "[", "Rn", "]", ",", "self", ".", "register", "[", "Rm", "]", "-", "self", ".", "register", "[", "Rn", "]", ",", "'sub'", ")", "else", ":", "# CMP Rm, #imm8", "self", ".", "check_arguments", "(", "R0_thru_R14", "=", "(", "Rm", ",", ")", ",", "imm8", "=", "(", "Rn", ",", ")", ")", "def", "CMP_func", "(", ")", ":", "tmp", "=", "self", ".", "convert_to_integer", "(", "Rn", "[", "1", ":", "]", ")", "self", ".", "set_NZCV_flags", "(", "self", ".", "register", "[", "Rm", "]", ",", "tmp", ",", "self", ".", "register", "[", "Rm", "]", "-", "tmp", ",", "'sub'", ")", "return", "CMP_func" ]
CMP Rm, Rn CMP Rm, #imm8 Subtract Rn or imm8 from Rm, set the NZCV flags, and discard the result Rm and Rn can be R0-R14
[ "CMP", "Rm", "Rn", "CMP", "Rm", "#imm8" ]
b913c9fd577b793a6bbced78b78a5d8d7cd88de4
https://github.com/DeepHorizons/iarm/blob/b913c9fd577b793a6bbced78b78a5d8d7cd88de4/iarm/arm_instructions/arithmetic.py#L151-L177
train
DeepHorizons/iarm
iarm/arm_instructions/arithmetic.py
Arithmetic.MULS
def MULS(self, params): """ MULS Ra, Rb, Ra Multiply Rb and Ra together and store the result in Ra. Set the NZ flags. Ra and Rb must be low registers The first and last operand must be the same register """ Ra, Rb, Rc = self.get_three_parameters(self.THREE_PARAMETER_COMMA_SEPARATED, params) self.check_arguments(low_registers=(Ra, Rb, Rc)) if Ra != Rc: raise iarm.exceptions.RuleError("Third parameter {} is not the same as the first parameter {}".format(Rc, Ra)) # MULS Ra, Rb, Ra def MULS_func(): self.register[Ra] = self.register[Rb] * self.register[Rc] self.set_NZ_flags(self.register[Ra]) return MULS_func
python
def MULS(self, params): """ MULS Ra, Rb, Ra Multiply Rb and Ra together and store the result in Ra. Set the NZ flags. Ra and Rb must be low registers The first and last operand must be the same register """ Ra, Rb, Rc = self.get_three_parameters(self.THREE_PARAMETER_COMMA_SEPARATED, params) self.check_arguments(low_registers=(Ra, Rb, Rc)) if Ra != Rc: raise iarm.exceptions.RuleError("Third parameter {} is not the same as the first parameter {}".format(Rc, Ra)) # MULS Ra, Rb, Ra def MULS_func(): self.register[Ra] = self.register[Rb] * self.register[Rc] self.set_NZ_flags(self.register[Ra]) return MULS_func
[ "def", "MULS", "(", "self", ",", "params", ")", ":", "Ra", ",", "Rb", ",", "Rc", "=", "self", ".", "get_three_parameters", "(", "self", ".", "THREE_PARAMETER_COMMA_SEPARATED", ",", "params", ")", "self", ".", "check_arguments", "(", "low_registers", "=", "(", "Ra", ",", "Rb", ",", "Rc", ")", ")", "if", "Ra", "!=", "Rc", ":", "raise", "iarm", ".", "exceptions", ".", "RuleError", "(", "\"Third parameter {} is not the same as the first parameter {}\"", ".", "format", "(", "Rc", ",", "Ra", ")", ")", "# MULS Ra, Rb, Ra", "def", "MULS_func", "(", ")", ":", "self", ".", "register", "[", "Ra", "]", "=", "self", ".", "register", "[", "Rb", "]", "*", "self", ".", "register", "[", "Rc", "]", "self", ".", "set_NZ_flags", "(", "self", ".", "register", "[", "Ra", "]", ")", "return", "MULS_func" ]
MULS Ra, Rb, Ra Multiply Rb and Ra together and store the result in Ra. Set the NZ flags. Ra and Rb must be low registers The first and last operand must be the same register
[ "MULS", "Ra", "Rb", "Ra" ]
b913c9fd577b793a6bbced78b78a5d8d7cd88de4
https://github.com/DeepHorizons/iarm/blob/b913c9fd577b793a6bbced78b78a5d8d7cd88de4/iarm/arm_instructions/arithmetic.py#L179-L199
train
DeepHorizons/iarm
iarm/arm_instructions/arithmetic.py
Arithmetic.RSBS
def RSBS(self, params): """ RSBS [Ra,] Rb, #0 Subtract Rb from zero (0 - Rb) and store the result in Ra Set the NZCV flags Ra and Rb must be low registers if Ra is omitted, then it is assumed to be Rb """ # This instruction allows for an optional destination register # If it is omitted, then it is assumed to be Rb # As defined in http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0662b/index.html try: Ra, Rb, Rc = self.get_three_parameters(self.THREE_PARAMETER_COMMA_SEPARATED, params) except iarm.exceptions.ParsingError: Rb, Rc = self.get_two_parameters(self.TWO_PARAMETER_COMMA_SEPARATED, params) Ra = Rb self.check_arguments(low_registers=(Ra, Rb)) if Rc != '#0': raise iarm.exceptions.RuleError("Third parameter {} is not #0".format(Rc)) # RSBS Ra, Rb, #0 def RSBS_func(): oper_2 = self.register[Rb] self.register[Ra] = 0 - self.register[Rb] self.set_NZCV_flags(0, oper_2, self.register[Ra], 'sub') return RSBS_func
python
def RSBS(self, params): """ RSBS [Ra,] Rb, #0 Subtract Rb from zero (0 - Rb) and store the result in Ra Set the NZCV flags Ra and Rb must be low registers if Ra is omitted, then it is assumed to be Rb """ # This instruction allows for an optional destination register # If it is omitted, then it is assumed to be Rb # As defined in http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0662b/index.html try: Ra, Rb, Rc = self.get_three_parameters(self.THREE_PARAMETER_COMMA_SEPARATED, params) except iarm.exceptions.ParsingError: Rb, Rc = self.get_two_parameters(self.TWO_PARAMETER_COMMA_SEPARATED, params) Ra = Rb self.check_arguments(low_registers=(Ra, Rb)) if Rc != '#0': raise iarm.exceptions.RuleError("Third parameter {} is not #0".format(Rc)) # RSBS Ra, Rb, #0 def RSBS_func(): oper_2 = self.register[Rb] self.register[Ra] = 0 - self.register[Rb] self.set_NZCV_flags(0, oper_2, self.register[Ra], 'sub') return RSBS_func
[ "def", "RSBS", "(", "self", ",", "params", ")", ":", "# This instruction allows for an optional destination register", "# If it is omitted, then it is assumed to be Rb", "# As defined in http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0662b/index.html", "try", ":", "Ra", ",", "Rb", ",", "Rc", "=", "self", ".", "get_three_parameters", "(", "self", ".", "THREE_PARAMETER_COMMA_SEPARATED", ",", "params", ")", "except", "iarm", ".", "exceptions", ".", "ParsingError", ":", "Rb", ",", "Rc", "=", "self", ".", "get_two_parameters", "(", "self", ".", "TWO_PARAMETER_COMMA_SEPARATED", ",", "params", ")", "Ra", "=", "Rb", "self", ".", "check_arguments", "(", "low_registers", "=", "(", "Ra", ",", "Rb", ")", ")", "if", "Rc", "!=", "'#0'", ":", "raise", "iarm", ".", "exceptions", ".", "RuleError", "(", "\"Third parameter {} is not #0\"", ".", "format", "(", "Rc", ")", ")", "# RSBS Ra, Rb, #0", "def", "RSBS_func", "(", ")", ":", "oper_2", "=", "self", ".", "register", "[", "Rb", "]", "self", ".", "register", "[", "Ra", "]", "=", "0", "-", "self", ".", "register", "[", "Rb", "]", "self", ".", "set_NZCV_flags", "(", "0", ",", "oper_2", ",", "self", ".", "register", "[", "Ra", "]", ",", "'sub'", ")", "return", "RSBS_func" ]
RSBS [Ra,] Rb, #0 Subtract Rb from zero (0 - Rb) and store the result in Ra Set the NZCV flags Ra and Rb must be low registers if Ra is omitted, then it is assumed to be Rb
[ "RSBS", "[", "Ra", "]", "Rb", "#0" ]
b913c9fd577b793a6bbced78b78a5d8d7cd88de4
https://github.com/DeepHorizons/iarm/blob/b913c9fd577b793a6bbced78b78a5d8d7cd88de4/iarm/arm_instructions/arithmetic.py#L212-L240
train
DeepHorizons/iarm
iarm/arm_instructions/arithmetic.py
Arithmetic.SUB
def SUB(self, params): """ SUB [SP,] SP, #imm9_4 Subtract an immediate from the Stack Pointer The first SP is optional """ # This instruction allows for an optional destination register # If it is omitted, then it is assumed to be Rb # As defined in http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0662b/index.html try: Ra, Rb, Rc = self.get_three_parameters(self.THREE_PARAMETER_COMMA_SEPARATED, params) except iarm.exceptions.ParsingError: Rb, Rc = self.get_two_parameters(self.TWO_PARAMETER_COMMA_SEPARATED, params) Ra = Rb self.check_arguments(imm9_4=(Rc,)) if Ra != 'SP': raise iarm.exceptions.RuleError("First parameter {} is not equal to SP".format(Ra)) if Rb != 'SP': raise iarm.exceptions.RuleError("Second parameter {} is not equal to SP".format(Rb)) # SUB SP, SP, #imm9_4 def SUB_func(): self.register[Ra] = self.register[Rb] - self.convert_to_integer(Rc[1:]) return SUB_func
python
def SUB(self, params): """ SUB [SP,] SP, #imm9_4 Subtract an immediate from the Stack Pointer The first SP is optional """ # This instruction allows for an optional destination register # If it is omitted, then it is assumed to be Rb # As defined in http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0662b/index.html try: Ra, Rb, Rc = self.get_three_parameters(self.THREE_PARAMETER_COMMA_SEPARATED, params) except iarm.exceptions.ParsingError: Rb, Rc = self.get_two_parameters(self.TWO_PARAMETER_COMMA_SEPARATED, params) Ra = Rb self.check_arguments(imm9_4=(Rc,)) if Ra != 'SP': raise iarm.exceptions.RuleError("First parameter {} is not equal to SP".format(Ra)) if Rb != 'SP': raise iarm.exceptions.RuleError("Second parameter {} is not equal to SP".format(Rb)) # SUB SP, SP, #imm9_4 def SUB_func(): self.register[Ra] = self.register[Rb] - self.convert_to_integer(Rc[1:]) return SUB_func
[ "def", "SUB", "(", "self", ",", "params", ")", ":", "# This instruction allows for an optional destination register", "# If it is omitted, then it is assumed to be Rb", "# As defined in http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0662b/index.html", "try", ":", "Ra", ",", "Rb", ",", "Rc", "=", "self", ".", "get_three_parameters", "(", "self", ".", "THREE_PARAMETER_COMMA_SEPARATED", ",", "params", ")", "except", "iarm", ".", "exceptions", ".", "ParsingError", ":", "Rb", ",", "Rc", "=", "self", ".", "get_two_parameters", "(", "self", ".", "TWO_PARAMETER_COMMA_SEPARATED", ",", "params", ")", "Ra", "=", "Rb", "self", ".", "check_arguments", "(", "imm9_4", "=", "(", "Rc", ",", ")", ")", "if", "Ra", "!=", "'SP'", ":", "raise", "iarm", ".", "exceptions", ".", "RuleError", "(", "\"First parameter {} is not equal to SP\"", ".", "format", "(", "Ra", ")", ")", "if", "Rb", "!=", "'SP'", ":", "raise", "iarm", ".", "exceptions", ".", "RuleError", "(", "\"Second parameter {} is not equal to SP\"", ".", "format", "(", "Rb", ")", ")", "# SUB SP, SP, #imm9_4", "def", "SUB_func", "(", ")", ":", "self", ".", "register", "[", "Ra", "]", "=", "self", ".", "register", "[", "Rb", "]", "-", "self", ".", "convert_to_integer", "(", "Rc", "[", "1", ":", "]", ")", "return", "SUB_func" ]
SUB [SP,] SP, #imm9_4 Subtract an immediate from the Stack Pointer The first SP is optional
[ "SUB", "[", "SP", "]", "SP", "#imm9_4" ]
b913c9fd577b793a6bbced78b78a5d8d7cd88de4
https://github.com/DeepHorizons/iarm/blob/b913c9fd577b793a6bbced78b78a5d8d7cd88de4/iarm/arm_instructions/arithmetic.py#L273-L299
train
DeepHorizons/iarm
iarm/arm_instructions/arithmetic.py
Arithmetic.SUBS
def SUBS(self, params): """ SUBS [Ra,] Rb, Rc SUBS [Ra,] Rb, #imm3 SUBS [Ra,] Ra, #imm8 Subtract Rc or an immediate from Rb and store the result in Ra Ra, Rb, and Rc must be low registers If Ra is omitted, then it is assumed to be Rb """ # This instruction allows for an optional destination register # If it is omitted, then it is assumed to be Rb # As defined in http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0662b/index.html try: Ra, Rb, Rc = self.get_three_parameters(self.THREE_PARAMETER_COMMA_SEPARATED, params) except iarm.exceptions.ParsingError: Rb, Rc = self.get_two_parameters(self.TWO_PARAMETER_COMMA_SEPARATED, params) Ra = Rb if self.is_register(Rc): # SUBS Ra, Rb, Rc self.check_arguments(low_registers=(Ra, Rb, Rc)) def SUBS_func(): oper_1 = self.register[Rb] oper_2 = self.register[Rc] self.register[Ra] = self.register[Rb] - self.register[Rc] self.set_NZCV_flags(oper_1, oper_2, self.register[Ra], 'sub') else: if Ra == Rb: # SUBS Ra, Ra, #imm8 self.check_arguments(low_registers=(Ra,), imm8=(Rc,)) def SUBS_func(): oper_1 = self.register[Ra] self.register[Ra] = self.register[Ra] - self.convert_to_integer(Rc[1:]) self.set_NZCV_flags(oper_1, self.convert_to_integer(Rc[1:]), self.register[Ra], 'sub') else: # SUBS Ra, Rb, #imm3 self.check_arguments(low_registers=(Ra, Rb), imm3=(Rc,)) def SUBS_func(): oper_1 = self.register[Rb] self.register[Ra] = self.register[Rb] - self.convert_to_integer(Rc[1:]) self.set_NZCV_flags(oper_1, self.convert_to_integer(Rc[1:]), self.register[Ra], 'sub') return SUBS_func
python
def SUBS(self, params): """ SUBS [Ra,] Rb, Rc SUBS [Ra,] Rb, #imm3 SUBS [Ra,] Ra, #imm8 Subtract Rc or an immediate from Rb and store the result in Ra Ra, Rb, and Rc must be low registers If Ra is omitted, then it is assumed to be Rb """ # This instruction allows for an optional destination register # If it is omitted, then it is assumed to be Rb # As defined in http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0662b/index.html try: Ra, Rb, Rc = self.get_three_parameters(self.THREE_PARAMETER_COMMA_SEPARATED, params) except iarm.exceptions.ParsingError: Rb, Rc = self.get_two_parameters(self.TWO_PARAMETER_COMMA_SEPARATED, params) Ra = Rb if self.is_register(Rc): # SUBS Ra, Rb, Rc self.check_arguments(low_registers=(Ra, Rb, Rc)) def SUBS_func(): oper_1 = self.register[Rb] oper_2 = self.register[Rc] self.register[Ra] = self.register[Rb] - self.register[Rc] self.set_NZCV_flags(oper_1, oper_2, self.register[Ra], 'sub') else: if Ra == Rb: # SUBS Ra, Ra, #imm8 self.check_arguments(low_registers=(Ra,), imm8=(Rc,)) def SUBS_func(): oper_1 = self.register[Ra] self.register[Ra] = self.register[Ra] - self.convert_to_integer(Rc[1:]) self.set_NZCV_flags(oper_1, self.convert_to_integer(Rc[1:]), self.register[Ra], 'sub') else: # SUBS Ra, Rb, #imm3 self.check_arguments(low_registers=(Ra, Rb), imm3=(Rc,)) def SUBS_func(): oper_1 = self.register[Rb] self.register[Ra] = self.register[Rb] - self.convert_to_integer(Rc[1:]) self.set_NZCV_flags(oper_1, self.convert_to_integer(Rc[1:]), self.register[Ra], 'sub') return SUBS_func
[ "def", "SUBS", "(", "self", ",", "params", ")", ":", "# This instruction allows for an optional destination register", "# If it is omitted, then it is assumed to be Rb", "# As defined in http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0662b/index.html", "try", ":", "Ra", ",", "Rb", ",", "Rc", "=", "self", ".", "get_three_parameters", "(", "self", ".", "THREE_PARAMETER_COMMA_SEPARATED", ",", "params", ")", "except", "iarm", ".", "exceptions", ".", "ParsingError", ":", "Rb", ",", "Rc", "=", "self", ".", "get_two_parameters", "(", "self", ".", "TWO_PARAMETER_COMMA_SEPARATED", ",", "params", ")", "Ra", "=", "Rb", "if", "self", ".", "is_register", "(", "Rc", ")", ":", "# SUBS Ra, Rb, Rc", "self", ".", "check_arguments", "(", "low_registers", "=", "(", "Ra", ",", "Rb", ",", "Rc", ")", ")", "def", "SUBS_func", "(", ")", ":", "oper_1", "=", "self", ".", "register", "[", "Rb", "]", "oper_2", "=", "self", ".", "register", "[", "Rc", "]", "self", ".", "register", "[", "Ra", "]", "=", "self", ".", "register", "[", "Rb", "]", "-", "self", ".", "register", "[", "Rc", "]", "self", ".", "set_NZCV_flags", "(", "oper_1", ",", "oper_2", ",", "self", ".", "register", "[", "Ra", "]", ",", "'sub'", ")", "else", ":", "if", "Ra", "==", "Rb", ":", "# SUBS Ra, Ra, #imm8", "self", ".", "check_arguments", "(", "low_registers", "=", "(", "Ra", ",", ")", ",", "imm8", "=", "(", "Rc", ",", ")", ")", "def", "SUBS_func", "(", ")", ":", "oper_1", "=", "self", ".", "register", "[", "Ra", "]", "self", ".", "register", "[", "Ra", "]", "=", "self", ".", "register", "[", "Ra", "]", "-", "self", ".", "convert_to_integer", "(", "Rc", "[", "1", ":", "]", ")", "self", ".", "set_NZCV_flags", "(", "oper_1", ",", "self", ".", "convert_to_integer", "(", "Rc", "[", "1", ":", "]", ")", ",", "self", ".", "register", "[", "Ra", "]", ",", "'sub'", ")", "else", ":", "# SUBS Ra, Rb, #imm3", "self", ".", "check_arguments", "(", "low_registers", "=", "(", "Ra", ",", "Rb", ")", ",", "imm3", "=", "(", "Rc", ",", ")", ")", "def", "SUBS_func", "(", ")", ":", "oper_1", "=", "self", ".", "register", "[", "Rb", "]", "self", ".", "register", "[", "Ra", "]", "=", "self", ".", "register", "[", "Rb", "]", "-", "self", ".", "convert_to_integer", "(", "Rc", "[", "1", ":", "]", ")", "self", ".", "set_NZCV_flags", "(", "oper_1", ",", "self", ".", "convert_to_integer", "(", "Rc", "[", "1", ":", "]", ")", ",", "self", ".", "register", "[", "Ra", "]", ",", "'sub'", ")", "return", "SUBS_func" ]
SUBS [Ra,] Rb, Rc SUBS [Ra,] Rb, #imm3 SUBS [Ra,] Ra, #imm8 Subtract Rc or an immediate from Rb and store the result in Ra Ra, Rb, and Rc must be low registers If Ra is omitted, then it is assumed to be Rb
[ "SUBS", "[", "Ra", "]", "Rb", "Rc", "SUBS", "[", "Ra", "]", "Rb", "#imm3", "SUBS", "[", "Ra", "]", "Ra", "#imm8" ]
b913c9fd577b793a6bbced78b78a5d8d7cd88de4
https://github.com/DeepHorizons/iarm/blob/b913c9fd577b793a6bbced78b78a5d8d7cd88de4/iarm/arm_instructions/arithmetic.py#L301-L347
train
glomex/gcdt
gcdt/kumo_particle_helper.py
initialize
def initialize(template, service_name, environment='dev'): """Adds SERVICE_NAME, SERVICE_ENVIRONMENT, and DEFAULT_TAGS to the template :param template: :param service_name: :param environment: :return: """ template.SERVICE_NAME = os.getenv('SERVICE_NAME', service_name) template.SERVICE_ENVIRONMENT = os.getenv('ENV', environment).lower() template.DEFAULT_TAGS = troposphere.Tags(**{ 'service-name': template.SERVICE_NAME, 'environment': template.SERVICE_ENVIRONMENT }) template.add_version("2010-09-09") template.add_description("Stack for %s microservice" % service_name)
python
def initialize(template, service_name, environment='dev'): """Adds SERVICE_NAME, SERVICE_ENVIRONMENT, and DEFAULT_TAGS to the template :param template: :param service_name: :param environment: :return: """ template.SERVICE_NAME = os.getenv('SERVICE_NAME', service_name) template.SERVICE_ENVIRONMENT = os.getenv('ENV', environment).lower() template.DEFAULT_TAGS = troposphere.Tags(**{ 'service-name': template.SERVICE_NAME, 'environment': template.SERVICE_ENVIRONMENT }) template.add_version("2010-09-09") template.add_description("Stack for %s microservice" % service_name)
[ "def", "initialize", "(", "template", ",", "service_name", ",", "environment", "=", "'dev'", ")", ":", "template", ".", "SERVICE_NAME", "=", "os", ".", "getenv", "(", "'SERVICE_NAME'", ",", "service_name", ")", "template", ".", "SERVICE_ENVIRONMENT", "=", "os", ".", "getenv", "(", "'ENV'", ",", "environment", ")", ".", "lower", "(", ")", "template", ".", "DEFAULT_TAGS", "=", "troposphere", ".", "Tags", "(", "*", "*", "{", "'service-name'", ":", "template", ".", "SERVICE_NAME", ",", "'environment'", ":", "template", ".", "SERVICE_ENVIRONMENT", "}", ")", "template", ".", "add_version", "(", "\"2010-09-09\"", ")", "template", ".", "add_description", "(", "\"Stack for %s microservice\"", "%", "service_name", ")" ]
Adds SERVICE_NAME, SERVICE_ENVIRONMENT, and DEFAULT_TAGS to the template :param template: :param service_name: :param environment: :return:
[ "Adds", "SERVICE_NAME", "SERVICE_ENVIRONMENT", "and", "DEFAULT_TAGS", "to", "the", "template" ]
cd67cf416371337b83cb9ca3f696277125703339
https://github.com/glomex/gcdt/blob/cd67cf416371337b83cb9ca3f696277125703339/gcdt/kumo_particle_helper.py#L14-L29
train
glomex/gcdt
gcdt/package_utils.py
get_dist
def get_dist(dist_name, lookup_dirs=None): """Get dist for installed version of dist_name avoiding pkg_resources cache """ # note: based on pip/utils/__init__.py, get_installed_version(...) # Create a requirement that we'll look for inside of setuptools. req = pkg_resources.Requirement.parse(dist_name) # We want to avoid having this cached, so we need to construct a new # working set each time. if lookup_dirs is None: working_set = pkg_resources.WorkingSet() else: working_set = pkg_resources.WorkingSet(lookup_dirs) # Get the installed distribution from our working set return working_set.find(req)
python
def get_dist(dist_name, lookup_dirs=None): """Get dist for installed version of dist_name avoiding pkg_resources cache """ # note: based on pip/utils/__init__.py, get_installed_version(...) # Create a requirement that we'll look for inside of setuptools. req = pkg_resources.Requirement.parse(dist_name) # We want to avoid having this cached, so we need to construct a new # working set each time. if lookup_dirs is None: working_set = pkg_resources.WorkingSet() else: working_set = pkg_resources.WorkingSet(lookup_dirs) # Get the installed distribution from our working set return working_set.find(req)
[ "def", "get_dist", "(", "dist_name", ",", "lookup_dirs", "=", "None", ")", ":", "# note: based on pip/utils/__init__.py, get_installed_version(...)", "# Create a requirement that we'll look for inside of setuptools.", "req", "=", "pkg_resources", ".", "Requirement", ".", "parse", "(", "dist_name", ")", "# We want to avoid having this cached, so we need to construct a new", "# working set each time.", "if", "lookup_dirs", "is", "None", ":", "working_set", "=", "pkg_resources", ".", "WorkingSet", "(", ")", "else", ":", "working_set", "=", "pkg_resources", ".", "WorkingSet", "(", "lookup_dirs", ")", "# Get the installed distribution from our working set", "return", "working_set", ".", "find", "(", "req", ")" ]
Get dist for installed version of dist_name avoiding pkg_resources cache
[ "Get", "dist", "for", "installed", "version", "of", "dist_name", "avoiding", "pkg_resources", "cache" ]
cd67cf416371337b83cb9ca3f696277125703339
https://github.com/glomex/gcdt/blob/cd67cf416371337b83cb9ca3f696277125703339/gcdt/package_utils.py#L8-L24
train
glomex/gcdt
gcdt/package_utils.py
get_package_versions
def get_package_versions(package): """Get the package version information (=SetuptoolsVersion) which is comparable. note: we use the pip list_command implementation for this :param package: name of the package :return: installed version, latest available version """ list_command = ListCommand() options, args = list_command.parse_args([]) packages = [get_dist(package)] dists = list_command.iter_packages_latest_infos(packages, options) try: dist = next(dists) return dist.parsed_version, dist.latest_version except StopIteration: return None, None
python
def get_package_versions(package): """Get the package version information (=SetuptoolsVersion) which is comparable. note: we use the pip list_command implementation for this :param package: name of the package :return: installed version, latest available version """ list_command = ListCommand() options, args = list_command.parse_args([]) packages = [get_dist(package)] dists = list_command.iter_packages_latest_infos(packages, options) try: dist = next(dists) return dist.parsed_version, dist.latest_version except StopIteration: return None, None
[ "def", "get_package_versions", "(", "package", ")", ":", "list_command", "=", "ListCommand", "(", ")", "options", ",", "args", "=", "list_command", ".", "parse_args", "(", "[", "]", ")", "packages", "=", "[", "get_dist", "(", "package", ")", "]", "dists", "=", "list_command", ".", "iter_packages_latest_infos", "(", "packages", ",", "options", ")", "try", ":", "dist", "=", "next", "(", "dists", ")", "return", "dist", ".", "parsed_version", ",", "dist", ".", "latest_version", "except", "StopIteration", ":", "return", "None", ",", "None" ]
Get the package version information (=SetuptoolsVersion) which is comparable. note: we use the pip list_command implementation for this :param package: name of the package :return: installed version, latest available version
[ "Get", "the", "package", "version", "information", "(", "=", "SetuptoolsVersion", ")", "which", "is", "comparable", ".", "note", ":", "we", "use", "the", "pip", "list_command", "implementation", "for", "this" ]
cd67cf416371337b83cb9ca3f696277125703339
https://github.com/glomex/gcdt/blob/cd67cf416371337b83cb9ca3f696277125703339/gcdt/package_utils.py#L27-L43
train
glomex/gcdt
gcdt/gcdt_lifecycle.py
_load_hooks
def _load_hooks(path): """Load hook module and register signals. :param path: Absolute or relative path to module. :return: module """ module = imp.load_source(os.path.splitext(os.path.basename(path))[0], path) if not check_hook_mechanism_is_intact(module): # no hooks - do nothing log.debug('No valid hook configuration: \'%s\'. Not using hooks!', path) else: if check_register_present(module): # register the template hooks so they listen to gcdt_signals module.register() return module
python
def _load_hooks(path): """Load hook module and register signals. :param path: Absolute or relative path to module. :return: module """ module = imp.load_source(os.path.splitext(os.path.basename(path))[0], path) if not check_hook_mechanism_is_intact(module): # no hooks - do nothing log.debug('No valid hook configuration: \'%s\'. Not using hooks!', path) else: if check_register_present(module): # register the template hooks so they listen to gcdt_signals module.register() return module
[ "def", "_load_hooks", "(", "path", ")", ":", "module", "=", "imp", ".", "load_source", "(", "os", ".", "path", ".", "splitext", "(", "os", ".", "path", ".", "basename", "(", "path", ")", ")", "[", "0", "]", ",", "path", ")", "if", "not", "check_hook_mechanism_is_intact", "(", "module", ")", ":", "# no hooks - do nothing", "log", ".", "debug", "(", "'No valid hook configuration: \\'%s\\'. Not using hooks!'", ",", "path", ")", "else", ":", "if", "check_register_present", "(", "module", ")", ":", "# register the template hooks so they listen to gcdt_signals", "module", ".", "register", "(", ")", "return", "module" ]
Load hook module and register signals. :param path: Absolute or relative path to module. :return: module
[ "Load", "hook", "module", "and", "register", "signals", "." ]
cd67cf416371337b83cb9ca3f696277125703339
https://github.com/glomex/gcdt/blob/cd67cf416371337b83cb9ca3f696277125703339/gcdt/gcdt_lifecycle.py#L29-L43
train
glomex/gcdt
gcdt/gcdt_lifecycle.py
lifecycle
def lifecycle(awsclient, env, tool, command, arguments): """Tool lifecycle which provides hooks into the different stages of the command execution. See signals for hook details. """ log.debug('### init') load_plugins() context = get_context(awsclient, env, tool, command, arguments) # every tool needs a awsclient so we provide this via the context context['_awsclient'] = awsclient log.debug('### context:') log.debug(context) if 'error' in context: # no need to send an 'error' signal here return 1 ## initialized gcdt_signals.initialized.send(context) log.debug('### initialized') if 'error' in context: log.error(context['error']) return 1 check_gcdt_update() # config is "assembled" by config_reader NOT here! config = {} gcdt_signals.config_read_init.send((context, config)) log.debug('### config_read_init') gcdt_signals.config_read_finalized.send((context, config)) log.debug('### config_read_finalized') # TODO we might want to be able to override config via env variables? # here would be the right place to do this if 'hookfile' in config: # load hooks from hookfile _load_hooks(config['hookfile']) if 'kumo' in config: # deprecated: this needs to be removed once all old-style "cloudformation" entries are gone fix_old_kumo_config(config) # check_credentials gcdt_signals.check_credentials_init.send((context, config)) log.debug('### check_credentials_init') gcdt_signals.check_credentials_finalized.send((context, config)) log.debug('### check_credentials_finalized') if 'error' in context: log.error(context['error']) gcdt_signals.error.send((context, config)) return 1 ## lookup gcdt_signals.lookup_init.send((context, config)) log.debug('### lookup_init') gcdt_signals.lookup_finalized.send((context, config)) log.debug('### lookup_finalized') log.debug('### config after lookup:') log.debug(config) ## config validation gcdt_signals.config_validation_init.send((context, config)) log.debug('### config_validation_init') gcdt_signals.config_validation_finalized.send((context, config)) if context['command'] in \ DEFAULT_CONFIG.get(context['tool'], {}).get('non_config_commands', []): pass # we do not require a config for this command elif tool not in config and tool != 'gcdt': context['error'] = 'Configuration missing for \'%s\'.' % tool log.error(context['error']) gcdt_signals.error.send((context, config)) return 1 log.debug('### config_validation_finalized') ## check credentials are valid (AWS services) # DEPRECATED, use gcdt-logon plugin instead if are_credentials_still_valid(awsclient): context['error'] = \ 'Your credentials have expired... Please renew and try again!' log.error(context['error']) gcdt_signals.error.send((context, config)) return 1 ## bundle step gcdt_signals.bundle_pre.send((context, config)) log.debug('### bundle_pre') gcdt_signals.bundle_init.send((context, config)) log.debug('### bundle_init') gcdt_signals.bundle_finalized.send((context, config)) log.debug('### bundle_finalized') if 'error' in context: log.error(context['error']) gcdt_signals.error.send((context, config)) return 1 ## dispatch command providing context and config (= tooldata) gcdt_signals.command_init.send((context, config)) log.debug('### command_init') try: if tool == 'gcdt': conf = config # gcdt works on the whole config else: conf = config.get(tool, {}) exit_code = cmd.dispatch(arguments, context=context, config=conf) except GracefulExit: raise except Exception as e: log.debug(traceback.format_exc()) context['error'] = str(e) log.error(context['error']) exit_code = 1 if exit_code: if 'error' not in context or context['error'] == '': context['error'] = '\'%s\' command failed with exit code 1' % command gcdt_signals.error.send((context, config)) return 1 gcdt_signals.command_finalized.send((context, config)) log.debug('### command_finalized') # TODO reporting (in case you want to get a summary / output to the user) gcdt_signals.finalized.send(context) log.debug('### finalized') return 0
python
def lifecycle(awsclient, env, tool, command, arguments): """Tool lifecycle which provides hooks into the different stages of the command execution. See signals for hook details. """ log.debug('### init') load_plugins() context = get_context(awsclient, env, tool, command, arguments) # every tool needs a awsclient so we provide this via the context context['_awsclient'] = awsclient log.debug('### context:') log.debug(context) if 'error' in context: # no need to send an 'error' signal here return 1 ## initialized gcdt_signals.initialized.send(context) log.debug('### initialized') if 'error' in context: log.error(context['error']) return 1 check_gcdt_update() # config is "assembled" by config_reader NOT here! config = {} gcdt_signals.config_read_init.send((context, config)) log.debug('### config_read_init') gcdt_signals.config_read_finalized.send((context, config)) log.debug('### config_read_finalized') # TODO we might want to be able to override config via env variables? # here would be the right place to do this if 'hookfile' in config: # load hooks from hookfile _load_hooks(config['hookfile']) if 'kumo' in config: # deprecated: this needs to be removed once all old-style "cloudformation" entries are gone fix_old_kumo_config(config) # check_credentials gcdt_signals.check_credentials_init.send((context, config)) log.debug('### check_credentials_init') gcdt_signals.check_credentials_finalized.send((context, config)) log.debug('### check_credentials_finalized') if 'error' in context: log.error(context['error']) gcdt_signals.error.send((context, config)) return 1 ## lookup gcdt_signals.lookup_init.send((context, config)) log.debug('### lookup_init') gcdt_signals.lookup_finalized.send((context, config)) log.debug('### lookup_finalized') log.debug('### config after lookup:') log.debug(config) ## config validation gcdt_signals.config_validation_init.send((context, config)) log.debug('### config_validation_init') gcdt_signals.config_validation_finalized.send((context, config)) if context['command'] in \ DEFAULT_CONFIG.get(context['tool'], {}).get('non_config_commands', []): pass # we do not require a config for this command elif tool not in config and tool != 'gcdt': context['error'] = 'Configuration missing for \'%s\'.' % tool log.error(context['error']) gcdt_signals.error.send((context, config)) return 1 log.debug('### config_validation_finalized') ## check credentials are valid (AWS services) # DEPRECATED, use gcdt-logon plugin instead if are_credentials_still_valid(awsclient): context['error'] = \ 'Your credentials have expired... Please renew and try again!' log.error(context['error']) gcdt_signals.error.send((context, config)) return 1 ## bundle step gcdt_signals.bundle_pre.send((context, config)) log.debug('### bundle_pre') gcdt_signals.bundle_init.send((context, config)) log.debug('### bundle_init') gcdt_signals.bundle_finalized.send((context, config)) log.debug('### bundle_finalized') if 'error' in context: log.error(context['error']) gcdt_signals.error.send((context, config)) return 1 ## dispatch command providing context and config (= tooldata) gcdt_signals.command_init.send((context, config)) log.debug('### command_init') try: if tool == 'gcdt': conf = config # gcdt works on the whole config else: conf = config.get(tool, {}) exit_code = cmd.dispatch(arguments, context=context, config=conf) except GracefulExit: raise except Exception as e: log.debug(traceback.format_exc()) context['error'] = str(e) log.error(context['error']) exit_code = 1 if exit_code: if 'error' not in context or context['error'] == '': context['error'] = '\'%s\' command failed with exit code 1' % command gcdt_signals.error.send((context, config)) return 1 gcdt_signals.command_finalized.send((context, config)) log.debug('### command_finalized') # TODO reporting (in case you want to get a summary / output to the user) gcdt_signals.finalized.send(context) log.debug('### finalized') return 0
[ "def", "lifecycle", "(", "awsclient", ",", "env", ",", "tool", ",", "command", ",", "arguments", ")", ":", "log", ".", "debug", "(", "'### init'", ")", "load_plugins", "(", ")", "context", "=", "get_context", "(", "awsclient", ",", "env", ",", "tool", ",", "command", ",", "arguments", ")", "# every tool needs a awsclient so we provide this via the context", "context", "[", "'_awsclient'", "]", "=", "awsclient", "log", ".", "debug", "(", "'### context:'", ")", "log", ".", "debug", "(", "context", ")", "if", "'error'", "in", "context", ":", "# no need to send an 'error' signal here", "return", "1", "## initialized", "gcdt_signals", ".", "initialized", ".", "send", "(", "context", ")", "log", ".", "debug", "(", "'### initialized'", ")", "if", "'error'", "in", "context", ":", "log", ".", "error", "(", "context", "[", "'error'", "]", ")", "return", "1", "check_gcdt_update", "(", ")", "# config is \"assembled\" by config_reader NOT here!", "config", "=", "{", "}", "gcdt_signals", ".", "config_read_init", ".", "send", "(", "(", "context", ",", "config", ")", ")", "log", ".", "debug", "(", "'### config_read_init'", ")", "gcdt_signals", ".", "config_read_finalized", ".", "send", "(", "(", "context", ",", "config", ")", ")", "log", ".", "debug", "(", "'### config_read_finalized'", ")", "# TODO we might want to be able to override config via env variables?", "# here would be the right place to do this", "if", "'hookfile'", "in", "config", ":", "# load hooks from hookfile", "_load_hooks", "(", "config", "[", "'hookfile'", "]", ")", "if", "'kumo'", "in", "config", ":", "# deprecated: this needs to be removed once all old-style \"cloudformation\" entries are gone", "fix_old_kumo_config", "(", "config", ")", "# check_credentials", "gcdt_signals", ".", "check_credentials_init", ".", "send", "(", "(", "context", ",", "config", ")", ")", "log", ".", "debug", "(", "'### check_credentials_init'", ")", "gcdt_signals", ".", "check_credentials_finalized", ".", "send", "(", "(", "context", ",", "config", ")", ")", "log", ".", "debug", "(", "'### check_credentials_finalized'", ")", "if", "'error'", "in", "context", ":", "log", ".", "error", "(", "context", "[", "'error'", "]", ")", "gcdt_signals", ".", "error", ".", "send", "(", "(", "context", ",", "config", ")", ")", "return", "1", "## lookup", "gcdt_signals", ".", "lookup_init", ".", "send", "(", "(", "context", ",", "config", ")", ")", "log", ".", "debug", "(", "'### lookup_init'", ")", "gcdt_signals", ".", "lookup_finalized", ".", "send", "(", "(", "context", ",", "config", ")", ")", "log", ".", "debug", "(", "'### lookup_finalized'", ")", "log", ".", "debug", "(", "'### config after lookup:'", ")", "log", ".", "debug", "(", "config", ")", "## config validation", "gcdt_signals", ".", "config_validation_init", ".", "send", "(", "(", "context", ",", "config", ")", ")", "log", ".", "debug", "(", "'### config_validation_init'", ")", "gcdt_signals", ".", "config_validation_finalized", ".", "send", "(", "(", "context", ",", "config", ")", ")", "if", "context", "[", "'command'", "]", "in", "DEFAULT_CONFIG", ".", "get", "(", "context", "[", "'tool'", "]", ",", "{", "}", ")", ".", "get", "(", "'non_config_commands'", ",", "[", "]", ")", ":", "pass", "# we do not require a config for this command", "elif", "tool", "not", "in", "config", "and", "tool", "!=", "'gcdt'", ":", "context", "[", "'error'", "]", "=", "'Configuration missing for \\'%s\\'.'", "%", "tool", "log", ".", "error", "(", "context", "[", "'error'", "]", ")", "gcdt_signals", ".", "error", ".", "send", "(", "(", "context", ",", "config", ")", ")", "return", "1", "log", ".", "debug", "(", "'### config_validation_finalized'", ")", "## check credentials are valid (AWS services)", "# DEPRECATED, use gcdt-logon plugin instead", "if", "are_credentials_still_valid", "(", "awsclient", ")", ":", "context", "[", "'error'", "]", "=", "'Your credentials have expired... Please renew and try again!'", "log", ".", "error", "(", "context", "[", "'error'", "]", ")", "gcdt_signals", ".", "error", ".", "send", "(", "(", "context", ",", "config", ")", ")", "return", "1", "## bundle step", "gcdt_signals", ".", "bundle_pre", ".", "send", "(", "(", "context", ",", "config", ")", ")", "log", ".", "debug", "(", "'### bundle_pre'", ")", "gcdt_signals", ".", "bundle_init", ".", "send", "(", "(", "context", ",", "config", ")", ")", "log", ".", "debug", "(", "'### bundle_init'", ")", "gcdt_signals", ".", "bundle_finalized", ".", "send", "(", "(", "context", ",", "config", ")", ")", "log", ".", "debug", "(", "'### bundle_finalized'", ")", "if", "'error'", "in", "context", ":", "log", ".", "error", "(", "context", "[", "'error'", "]", ")", "gcdt_signals", ".", "error", ".", "send", "(", "(", "context", ",", "config", ")", ")", "return", "1", "## dispatch command providing context and config (= tooldata)", "gcdt_signals", ".", "command_init", ".", "send", "(", "(", "context", ",", "config", ")", ")", "log", ".", "debug", "(", "'### command_init'", ")", "try", ":", "if", "tool", "==", "'gcdt'", ":", "conf", "=", "config", "# gcdt works on the whole config", "else", ":", "conf", "=", "config", ".", "get", "(", "tool", ",", "{", "}", ")", "exit_code", "=", "cmd", ".", "dispatch", "(", "arguments", ",", "context", "=", "context", ",", "config", "=", "conf", ")", "except", "GracefulExit", ":", "raise", "except", "Exception", "as", "e", ":", "log", ".", "debug", "(", "traceback", ".", "format_exc", "(", ")", ")", "context", "[", "'error'", "]", "=", "str", "(", "e", ")", "log", ".", "error", "(", "context", "[", "'error'", "]", ")", "exit_code", "=", "1", "if", "exit_code", ":", "if", "'error'", "not", "in", "context", "or", "context", "[", "'error'", "]", "==", "''", ":", "context", "[", "'error'", "]", "=", "'\\'%s\\' command failed with exit code 1'", "%", "command", "gcdt_signals", ".", "error", ".", "send", "(", "(", "context", ",", "config", ")", ")", "return", "1", "gcdt_signals", ".", "command_finalized", ".", "send", "(", "(", "context", ",", "config", ")", ")", "log", ".", "debug", "(", "'### command_finalized'", ")", "# TODO reporting (in case you want to get a summary / output to the user)", "gcdt_signals", ".", "finalized", ".", "send", "(", "context", ")", "log", ".", "debug", "(", "'### finalized'", ")", "return", "0" ]
Tool lifecycle which provides hooks into the different stages of the command execution. See signals for hook details.
[ "Tool", "lifecycle", "which", "provides", "hooks", "into", "the", "different", "stages", "of", "the", "command", "execution", ".", "See", "signals", "for", "hook", "details", "." ]
cd67cf416371337b83cb9ca3f696277125703339
https://github.com/glomex/gcdt/blob/cd67cf416371337b83cb9ca3f696277125703339/gcdt/gcdt_lifecycle.py#L48-L171
train
glomex/gcdt
gcdt/gcdt_lifecycle.py
main
def main(doc, tool, dispatch_only=None): """gcdt tools parametrized main function to initiate gcdt lifecycle. :param doc: docopt string :param tool: gcdt tool (gcdt, kumo, tenkai, ramuda, yugen) :param dispatch_only: list of commands which do not use gcdt lifecycle :return: exit_code """ # Use signal handler to throw exception which can be caught to allow # graceful exit. # here: https://stackoverflow.com/questions/26414704/how-does-a-python-process-exit-gracefully-after-receiving-sigterm-while-waiting signal.signal(signal.SIGTERM, signal_handler) # Jenkins signal.signal(signal.SIGINT, signal_handler) # Ctrl-C try: arguments = docopt(doc, sys.argv[1:]) command = get_command(arguments) # DEBUG mode (if requested) verbose = arguments.pop('--verbose', False) if verbose: logging_config['loggers']['gcdt']['level'] = 'DEBUG' dictConfig(logging_config) if dispatch_only is None: dispatch_only = ['version'] assert tool in ['gcdt', 'kumo', 'tenkai', 'ramuda', 'yugen'] if command in dispatch_only: # handle commands that do not need a lifecycle # Note: `dispatch_only` commands do not have a check for ENV variable! check_gcdt_update() return cmd.dispatch(arguments) else: env = get_env() if not env: log.error('\'ENV\' environment variable not set!') return 1 awsclient = AWSClient(botocore.session.get_session()) return lifecycle(awsclient, env, tool, command, arguments) except GracefulExit as e: log.info('Received %s signal - exiting command \'%s %s\'', str(e), tool, command) return 1
python
def main(doc, tool, dispatch_only=None): """gcdt tools parametrized main function to initiate gcdt lifecycle. :param doc: docopt string :param tool: gcdt tool (gcdt, kumo, tenkai, ramuda, yugen) :param dispatch_only: list of commands which do not use gcdt lifecycle :return: exit_code """ # Use signal handler to throw exception which can be caught to allow # graceful exit. # here: https://stackoverflow.com/questions/26414704/how-does-a-python-process-exit-gracefully-after-receiving-sigterm-while-waiting signal.signal(signal.SIGTERM, signal_handler) # Jenkins signal.signal(signal.SIGINT, signal_handler) # Ctrl-C try: arguments = docopt(doc, sys.argv[1:]) command = get_command(arguments) # DEBUG mode (if requested) verbose = arguments.pop('--verbose', False) if verbose: logging_config['loggers']['gcdt']['level'] = 'DEBUG' dictConfig(logging_config) if dispatch_only is None: dispatch_only = ['version'] assert tool in ['gcdt', 'kumo', 'tenkai', 'ramuda', 'yugen'] if command in dispatch_only: # handle commands that do not need a lifecycle # Note: `dispatch_only` commands do not have a check for ENV variable! check_gcdt_update() return cmd.dispatch(arguments) else: env = get_env() if not env: log.error('\'ENV\' environment variable not set!') return 1 awsclient = AWSClient(botocore.session.get_session()) return lifecycle(awsclient, env, tool, command, arguments) except GracefulExit as e: log.info('Received %s signal - exiting command \'%s %s\'', str(e), tool, command) return 1
[ "def", "main", "(", "doc", ",", "tool", ",", "dispatch_only", "=", "None", ")", ":", "# Use signal handler to throw exception which can be caught to allow", "# graceful exit.", "# here: https://stackoverflow.com/questions/26414704/how-does-a-python-process-exit-gracefully-after-receiving-sigterm-while-waiting", "signal", ".", "signal", "(", "signal", ".", "SIGTERM", ",", "signal_handler", ")", "# Jenkins", "signal", ".", "signal", "(", "signal", ".", "SIGINT", ",", "signal_handler", ")", "# Ctrl-C", "try", ":", "arguments", "=", "docopt", "(", "doc", ",", "sys", ".", "argv", "[", "1", ":", "]", ")", "command", "=", "get_command", "(", "arguments", ")", "# DEBUG mode (if requested)", "verbose", "=", "arguments", ".", "pop", "(", "'--verbose'", ",", "False", ")", "if", "verbose", ":", "logging_config", "[", "'loggers'", "]", "[", "'gcdt'", "]", "[", "'level'", "]", "=", "'DEBUG'", "dictConfig", "(", "logging_config", ")", "if", "dispatch_only", "is", "None", ":", "dispatch_only", "=", "[", "'version'", "]", "assert", "tool", "in", "[", "'gcdt'", ",", "'kumo'", ",", "'tenkai'", ",", "'ramuda'", ",", "'yugen'", "]", "if", "command", "in", "dispatch_only", ":", "# handle commands that do not need a lifecycle", "# Note: `dispatch_only` commands do not have a check for ENV variable!", "check_gcdt_update", "(", ")", "return", "cmd", ".", "dispatch", "(", "arguments", ")", "else", ":", "env", "=", "get_env", "(", ")", "if", "not", "env", ":", "log", ".", "error", "(", "'\\'ENV\\' environment variable not set!'", ")", "return", "1", "awsclient", "=", "AWSClient", "(", "botocore", ".", "session", ".", "get_session", "(", ")", ")", "return", "lifecycle", "(", "awsclient", ",", "env", ",", "tool", ",", "command", ",", "arguments", ")", "except", "GracefulExit", "as", "e", ":", "log", ".", "info", "(", "'Received %s signal - exiting command \\'%s %s\\''", ",", "str", "(", "e", ")", ",", "tool", ",", "command", ")", "return", "1" ]
gcdt tools parametrized main function to initiate gcdt lifecycle. :param doc: docopt string :param tool: gcdt tool (gcdt, kumo, tenkai, ramuda, yugen) :param dispatch_only: list of commands which do not use gcdt lifecycle :return: exit_code
[ "gcdt", "tools", "parametrized", "main", "function", "to", "initiate", "gcdt", "lifecycle", "." ]
cd67cf416371337b83cb9ca3f696277125703339
https://github.com/glomex/gcdt/blob/cd67cf416371337b83cb9ca3f696277125703339/gcdt/gcdt_lifecycle.py#L174-L217
train
DeepHorizons/iarm
iarm/arm_instructions/data_movement.py
DataMovement.MOV
def MOV(self, params): """ MOV Rx, Ry MOV PC, Ry Move the value of Ry into Rx or PC """ Rx, Ry = self.get_two_parameters(self.TWO_PARAMETER_COMMA_SEPARATED, params) self.check_arguments(any_registers=(Rx, Ry)) def MOV_func(): self.register[Rx] = self.register[Ry] return MOV_func
python
def MOV(self, params): """ MOV Rx, Ry MOV PC, Ry Move the value of Ry into Rx or PC """ Rx, Ry = self.get_two_parameters(self.TWO_PARAMETER_COMMA_SEPARATED, params) self.check_arguments(any_registers=(Rx, Ry)) def MOV_func(): self.register[Rx] = self.register[Ry] return MOV_func
[ "def", "MOV", "(", "self", ",", "params", ")", ":", "Rx", ",", "Ry", "=", "self", ".", "get_two_parameters", "(", "self", ".", "TWO_PARAMETER_COMMA_SEPARATED", ",", "params", ")", "self", ".", "check_arguments", "(", "any_registers", "=", "(", "Rx", ",", "Ry", ")", ")", "def", "MOV_func", "(", ")", ":", "self", ".", "register", "[", "Rx", "]", "=", "self", ".", "register", "[", "Ry", "]", "return", "MOV_func" ]
MOV Rx, Ry MOV PC, Ry Move the value of Ry into Rx or PC
[ "MOV", "Rx", "Ry", "MOV", "PC", "Ry" ]
b913c9fd577b793a6bbced78b78a5d8d7cd88de4
https://github.com/DeepHorizons/iarm/blob/b913c9fd577b793a6bbced78b78a5d8d7cd88de4/iarm/arm_instructions/data_movement.py#L11-L25
train
DeepHorizons/iarm
iarm/arm_instructions/data_movement.py
DataMovement.MOVS
def MOVS(self, params): """ MOVS Ra, Rb MOVS Ra, #imm8 Move the value of Rb or imm8 into Ra Ra and Rb must be low registers """ Ra, Rb = self.get_two_parameters(self.TWO_PARAMETER_COMMA_SEPARATED, params) if self.is_immediate(Rb): self.check_arguments(low_registers=[Ra], imm8=[Rb]) def MOVS_func(): self.register[Ra] = self.convert_to_integer(Rb[1:]) # Set N and Z status flags self.set_NZ_flags(self.register[Ra]) return MOVS_func elif self.is_register(Rb): self.check_arguments(low_registers=(Ra, Rb)) def MOVS_func(): self.register[Ra] = self.register[Rb] self.set_NZ_flags(self.register[Ra]) return MOVS_func else: raise iarm.exceptions.ParsingError("Unknown parameter: {}".format(Rb))
python
def MOVS(self, params): """ MOVS Ra, Rb MOVS Ra, #imm8 Move the value of Rb or imm8 into Ra Ra and Rb must be low registers """ Ra, Rb = self.get_two_parameters(self.TWO_PARAMETER_COMMA_SEPARATED, params) if self.is_immediate(Rb): self.check_arguments(low_registers=[Ra], imm8=[Rb]) def MOVS_func(): self.register[Ra] = self.convert_to_integer(Rb[1:]) # Set N and Z status flags self.set_NZ_flags(self.register[Ra]) return MOVS_func elif self.is_register(Rb): self.check_arguments(low_registers=(Ra, Rb)) def MOVS_func(): self.register[Ra] = self.register[Rb] self.set_NZ_flags(self.register[Ra]) return MOVS_func else: raise iarm.exceptions.ParsingError("Unknown parameter: {}".format(Rb))
[ "def", "MOVS", "(", "self", ",", "params", ")", ":", "Ra", ",", "Rb", "=", "self", ".", "get_two_parameters", "(", "self", ".", "TWO_PARAMETER_COMMA_SEPARATED", ",", "params", ")", "if", "self", ".", "is_immediate", "(", "Rb", ")", ":", "self", ".", "check_arguments", "(", "low_registers", "=", "[", "Ra", "]", ",", "imm8", "=", "[", "Rb", "]", ")", "def", "MOVS_func", "(", ")", ":", "self", ".", "register", "[", "Ra", "]", "=", "self", ".", "convert_to_integer", "(", "Rb", "[", "1", ":", "]", ")", "# Set N and Z status flags", "self", ".", "set_NZ_flags", "(", "self", ".", "register", "[", "Ra", "]", ")", "return", "MOVS_func", "elif", "self", ".", "is_register", "(", "Rb", ")", ":", "self", ".", "check_arguments", "(", "low_registers", "=", "(", "Ra", ",", "Rb", ")", ")", "def", "MOVS_func", "(", ")", ":", "self", ".", "register", "[", "Ra", "]", "=", "self", ".", "register", "[", "Rb", "]", "self", ".", "set_NZ_flags", "(", "self", ".", "register", "[", "Ra", "]", ")", "return", "MOVS_func", "else", ":", "raise", "iarm", ".", "exceptions", ".", "ParsingError", "(", "\"Unknown parameter: {}\"", ".", "format", "(", "Rb", ")", ")" ]
MOVS Ra, Rb MOVS Ra, #imm8 Move the value of Rb or imm8 into Ra Ra and Rb must be low registers
[ "MOVS", "Ra", "Rb", "MOVS", "Ra", "#imm8" ]
b913c9fd577b793a6bbced78b78a5d8d7cd88de4
https://github.com/DeepHorizons/iarm/blob/b913c9fd577b793a6bbced78b78a5d8d7cd88de4/iarm/arm_instructions/data_movement.py#L27-L57
train
DeepHorizons/iarm
iarm/arm_instructions/data_movement.py
DataMovement.MRS
def MRS(self, params): """ MRS Rj, Rspecial Copy the value of Rspecial to Rj Rspecial can be APSR, IPSR, or EPSR """ Rj, Rspecial = self.get_two_parameters(self.TWO_PARAMETER_COMMA_SEPARATED, params) self.check_arguments(LR_or_general_purpose_registers=(Rj,), special_registers=(Rspecial,)) def MRS_func(): # TODO add combination registers IEPSR, IAPSR, and EAPSR # TODO needs to use APSR, IPSR, EPSR, IEPSR, IAPSR, EAPSR, PSR, MSP, PSP, PRIMASK, or CONTROL. # http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0553a/CHDBIBGJ.html if Rspecial == 'PSR': self.register[Rj] = self.register['APSR'] | self.register['IPSR'] | self.register['EPSR'] else: self.register[Rj] = self.register[Rspecial] return MRS_func
python
def MRS(self, params): """ MRS Rj, Rspecial Copy the value of Rspecial to Rj Rspecial can be APSR, IPSR, or EPSR """ Rj, Rspecial = self.get_two_parameters(self.TWO_PARAMETER_COMMA_SEPARATED, params) self.check_arguments(LR_or_general_purpose_registers=(Rj,), special_registers=(Rspecial,)) def MRS_func(): # TODO add combination registers IEPSR, IAPSR, and EAPSR # TODO needs to use APSR, IPSR, EPSR, IEPSR, IAPSR, EAPSR, PSR, MSP, PSP, PRIMASK, or CONTROL. # http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0553a/CHDBIBGJ.html if Rspecial == 'PSR': self.register[Rj] = self.register['APSR'] | self.register['IPSR'] | self.register['EPSR'] else: self.register[Rj] = self.register[Rspecial] return MRS_func
[ "def", "MRS", "(", "self", ",", "params", ")", ":", "Rj", ",", "Rspecial", "=", "self", ".", "get_two_parameters", "(", "self", ".", "TWO_PARAMETER_COMMA_SEPARATED", ",", "params", ")", "self", ".", "check_arguments", "(", "LR_or_general_purpose_registers", "=", "(", "Rj", ",", ")", ",", "special_registers", "=", "(", "Rspecial", ",", ")", ")", "def", "MRS_func", "(", ")", ":", "# TODO add combination registers IEPSR, IAPSR, and EAPSR", "# TODO needs to use APSR, IPSR, EPSR, IEPSR, IAPSR, EAPSR, PSR, MSP, PSP, PRIMASK, or CONTROL.", "# http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0553a/CHDBIBGJ.html", "if", "Rspecial", "==", "'PSR'", ":", "self", ".", "register", "[", "Rj", "]", "=", "self", ".", "register", "[", "'APSR'", "]", "|", "self", ".", "register", "[", "'IPSR'", "]", "|", "self", ".", "register", "[", "'EPSR'", "]", "else", ":", "self", ".", "register", "[", "Rj", "]", "=", "self", ".", "register", "[", "Rspecial", "]", "return", "MRS_func" ]
MRS Rj, Rspecial Copy the value of Rspecial to Rj Rspecial can be APSR, IPSR, or EPSR
[ "MRS", "Rj", "Rspecial" ]
b913c9fd577b793a6bbced78b78a5d8d7cd88de4
https://github.com/DeepHorizons/iarm/blob/b913c9fd577b793a6bbced78b78a5d8d7cd88de4/iarm/arm_instructions/data_movement.py#L59-L79
train
DeepHorizons/iarm
iarm/arm_instructions/data_movement.py
DataMovement.MSR
def MSR(self, params): """ MSR Rspecial, Rj Copy the value of Rj to Rspecial Rspecial can be APSR, IPSR, or EPSR """ Rspecial, Rj = self.get_two_parameters(self.TWO_PARAMETER_COMMA_SEPARATED, params) self.check_arguments(LR_or_general_purpose_registers=(Rj,), special_registers=(Rspecial,)) def MSR_func(): # TODO add combination registers IEPSR, IAPSR, and EAPSR # http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0553a/CHDBIBGJ.html # TODO update N Z C V flags if Rspecial in ('PSR', 'APSR'): # PSR ignores writes to IPSR and EPSR self.register['APSR'] = self.register[Rj] else: # Do nothing pass return MSR_func
python
def MSR(self, params): """ MSR Rspecial, Rj Copy the value of Rj to Rspecial Rspecial can be APSR, IPSR, or EPSR """ Rspecial, Rj = self.get_two_parameters(self.TWO_PARAMETER_COMMA_SEPARATED, params) self.check_arguments(LR_or_general_purpose_registers=(Rj,), special_registers=(Rspecial,)) def MSR_func(): # TODO add combination registers IEPSR, IAPSR, and EAPSR # http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0553a/CHDBIBGJ.html # TODO update N Z C V flags if Rspecial in ('PSR', 'APSR'): # PSR ignores writes to IPSR and EPSR self.register['APSR'] = self.register[Rj] else: # Do nothing pass return MSR_func
[ "def", "MSR", "(", "self", ",", "params", ")", ":", "Rspecial", ",", "Rj", "=", "self", ".", "get_two_parameters", "(", "self", ".", "TWO_PARAMETER_COMMA_SEPARATED", ",", "params", ")", "self", ".", "check_arguments", "(", "LR_or_general_purpose_registers", "=", "(", "Rj", ",", ")", ",", "special_registers", "=", "(", "Rspecial", ",", ")", ")", "def", "MSR_func", "(", ")", ":", "# TODO add combination registers IEPSR, IAPSR, and EAPSR", "# http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0553a/CHDBIBGJ.html", "# TODO update N Z C V flags", "if", "Rspecial", "in", "(", "'PSR'", ",", "'APSR'", ")", ":", "# PSR ignores writes to IPSR and EPSR", "self", ".", "register", "[", "'APSR'", "]", "=", "self", ".", "register", "[", "Rj", "]", "else", ":", "# Do nothing", "pass", "return", "MSR_func" ]
MSR Rspecial, Rj Copy the value of Rj to Rspecial Rspecial can be APSR, IPSR, or EPSR
[ "MSR", "Rspecial", "Rj" ]
b913c9fd577b793a6bbced78b78a5d8d7cd88de4
https://github.com/DeepHorizons/iarm/blob/b913c9fd577b793a6bbced78b78a5d8d7cd88de4/iarm/arm_instructions/data_movement.py#L81-L103
train
DeepHorizons/iarm
iarm/arm_instructions/data_movement.py
DataMovement.MVNS
def MVNS(self, params): """ MVNS Ra, Rb Negate the value in Rb and store it in Ra Ra and Rb must be a low register """ Ra, Rb = self.get_two_parameters(self.TWO_PARAMETER_COMMA_SEPARATED, params) self.check_arguments(low_registers=(Ra, Rb)) def MVNS_func(): self.register[Ra] = ~self.register[Rb] self.set_NZ_flags(self.register[Ra]) return MVNS_func
python
def MVNS(self, params): """ MVNS Ra, Rb Negate the value in Rb and store it in Ra Ra and Rb must be a low register """ Ra, Rb = self.get_two_parameters(self.TWO_PARAMETER_COMMA_SEPARATED, params) self.check_arguments(low_registers=(Ra, Rb)) def MVNS_func(): self.register[Ra] = ~self.register[Rb] self.set_NZ_flags(self.register[Ra]) return MVNS_func
[ "def", "MVNS", "(", "self", ",", "params", ")", ":", "Ra", ",", "Rb", "=", "self", ".", "get_two_parameters", "(", "self", ".", "TWO_PARAMETER_COMMA_SEPARATED", ",", "params", ")", "self", ".", "check_arguments", "(", "low_registers", "=", "(", "Ra", ",", "Rb", ")", ")", "def", "MVNS_func", "(", ")", ":", "self", ".", "register", "[", "Ra", "]", "=", "~", "self", ".", "register", "[", "Rb", "]", "self", ".", "set_NZ_flags", "(", "self", ".", "register", "[", "Ra", "]", ")", "return", "MVNS_func" ]
MVNS Ra, Rb Negate the value in Rb and store it in Ra Ra and Rb must be a low register
[ "MVNS", "Ra", "Rb" ]
b913c9fd577b793a6bbced78b78a5d8d7cd88de4
https://github.com/DeepHorizons/iarm/blob/b913c9fd577b793a6bbced78b78a5d8d7cd88de4/iarm/arm_instructions/data_movement.py#L105-L120
train
DeepHorizons/iarm
iarm/arm_instructions/data_movement.py
DataMovement.REV
def REV(self, params): """ REV Ra, Rb Reverse the byte order in register Rb and store the result in Ra """ Ra, Rb = self.get_two_parameters(self.TWO_PARAMETER_COMMA_SEPARATED, params) self.check_arguments(low_registers=(Ra, Rb)) def REV_func(): self.register[Ra] = ((self.register[Rb] & 0xFF000000) >> 24) | \ ((self.register[Rb] & 0x00FF0000) >> 8) | \ ((self.register[Rb] & 0x0000FF00) << 8) | \ ((self.register[Rb] & 0x000000FF) << 24) return REV_func
python
def REV(self, params): """ REV Ra, Rb Reverse the byte order in register Rb and store the result in Ra """ Ra, Rb = self.get_two_parameters(self.TWO_PARAMETER_COMMA_SEPARATED, params) self.check_arguments(low_registers=(Ra, Rb)) def REV_func(): self.register[Ra] = ((self.register[Rb] & 0xFF000000) >> 24) | \ ((self.register[Rb] & 0x00FF0000) >> 8) | \ ((self.register[Rb] & 0x0000FF00) << 8) | \ ((self.register[Rb] & 0x000000FF) << 24) return REV_func
[ "def", "REV", "(", "self", ",", "params", ")", ":", "Ra", ",", "Rb", "=", "self", ".", "get_two_parameters", "(", "self", ".", "TWO_PARAMETER_COMMA_SEPARATED", ",", "params", ")", "self", ".", "check_arguments", "(", "low_registers", "=", "(", "Ra", ",", "Rb", ")", ")", "def", "REV_func", "(", ")", ":", "self", ".", "register", "[", "Ra", "]", "=", "(", "(", "self", ".", "register", "[", "Rb", "]", "&", "0xFF000000", ")", ">>", "24", ")", "|", "(", "(", "self", ".", "register", "[", "Rb", "]", "&", "0x00FF0000", ")", ">>", "8", ")", "|", "(", "(", "self", ".", "register", "[", "Rb", "]", "&", "0x0000FF00", ")", "<<", "8", ")", "|", "(", "(", "self", ".", "register", "[", "Rb", "]", "&", "0x000000FF", ")", "<<", "24", ")", "return", "REV_func" ]
REV Ra, Rb Reverse the byte order in register Rb and store the result in Ra
[ "REV", "Ra", "Rb" ]
b913c9fd577b793a6bbced78b78a5d8d7cd88de4
https://github.com/DeepHorizons/iarm/blob/b913c9fd577b793a6bbced78b78a5d8d7cd88de4/iarm/arm_instructions/data_movement.py#L122-L138
train