def create_service_control_policy(**kwargs): try: name = kwargs['service_control_policy']['name'] description = kwargs['service_control_policy']['description'] policy = kwargs['service_control_policy']['policy'] response = kwargs['client'].create_policy( Type='SERVICE_CONTROL_POLICY', Name=name, Description=description, Content=json.dumps(policy)) return response['Policy']['PolicySummary'] except kwargs['client'].exceptions.DuplicatePolicyException as exception: # find the policy and return what we know about it print('Service control policy %s already exists' % (name)) response = utils.get_results_from_paginator( client=kwargs['client'], api_call='list_policies', response_key='Policies', args={'Filter': 'SERVICE_CONTROL_POLICY'}) for policy in response: if policy['Name'] == name: return policy except Exception as exception: utils.handle_exception( friendly_description='Unable to make a service control policy', exception=exception)
def create_account_status(**kwargs): while True: try: response = kwargs['client'].describe_create_account_status( CreateAccountRequestId=kwargs['create_account_request_id']) if response['CreateAccountStatus']['State'] == 'SUCCEEDED': print('%s (%s) created.' % (kwargs['account_name'], response['CreateAccountStatus']['AccountId'])) return response['CreateAccountStatus']['AccountId'] elif response['CreateAccountStatus'][ 'State'] == 'FAILED' and response['CreateAccountStatus'][ 'FailureReason'] == 'EMAIL_ALREADY_EXISTS': response = get_account(client=kwargs['client'], email=kwargs['account_email']) print('Account %s (%s, %s) already exists.' % (kwargs['account_name'], kwargs['account_email'], response['Id'])) return response['Id'] elif response['CreateAccountStatus']['State'] == 'FAILED': print('%s failed to be created: %s' % (kwargs['account_name'], response['CreateAccountStatus']['FailureReason'])) exit(1) else: print('Waiting for account %s to be created' % (kwargs['account_name'])) time.sleep(2) except Exception as exception: utils.handle_exception( friendly_description='Unable to check account creation status', exception=exception)
def move_account_to_organization_unit(**kwargs): print( 'Moving account %s (%s, %s) from under %s (%s) to under %s (%s)' % (kwargs['account_name'], kwargs['account_email'], kwargs['account_id'], kwargs['source_parent_name'], kwargs['source_parent_id'], kwargs['destination_parent_name'], kwargs['destination_parent_id'])) parent_accounts = None try: parent_accounts = kwargs['client'].list_parents( ChildId=kwargs['account_id']) except Exception as exception: utils.handle_exception( friendly_description='Unable to list parents of the account', exception=exception) if parent_accounts['Parents'][0]['Id'] == kwargs['destination_parent_id']: print('Account %s (%s, %s) is already under %s (%s)' % (kwargs['account_name'], kwargs['account_email'], kwargs['account_id'], kwargs['destination_parent_name'], kwargs['destination_parent_id'])) else: try: kwargs['client'].move_account( SourceParentId=parent_accounts['Parents'][0]['Id'], DestinationParentId=kwargs['destination_parent_id'], AccountId=kwargs['account_id']) print('Moved account %s (%s, %s) is under %s (%s)' % (kwargs['account_name'], kwargs['account_email'], kwargs['account_id'], kwargs['destination_parent_name'], kwargs['destination_parent_id'])) except Exception as exception: utils.handle_exception( friendly_description= 'Unable to move the account under an organizational unit', exception=exception)
async def get_product_image_url(product_url: str) -> str: """Get product image url from product page.""" response = await utils.make_get_request(product_url, headers=db_aps.PRODUCT_HEADERS) if not response: avito_parser_logger.debug( 'Failed to parse product image. Set default url') return DEFAULT_IMG page_data = BeautifulSoup(response.text, 'lxml') img_frame_selectors = ('.gallery-img-frame', '.image-frame-wrapper-2FMhm') try: for selector in img_frame_selectors: img_url = page_data.select_one(selector) if img_url: break img_url = str(img_url['data-url']) except TypeError: # Sometimes request fetch page with no product image, # so there is no gallery-img-frame in it. logger = utils.get_logger_bot() chat_id = os.environ.get('TG_LOG_CHAT_ID') text = f'into_image_parse: response.code: {response.status_code}\nurl: {product_url}' await utils.handle_exception('avito_parser_logger', text) try: await logger.send_document( chat_id, ('resp_text_page.html', response.text.encode())) await logger.send_document( chat_id, ('product_page.html', page_data.encode())) except Exception: utils.handle_exception('avito_parser_logger', 'into_image_parse') finally: return DEFAULT_IMG avito_parser_logger.debug(f'Got product image url: {img_url}') return img_url
def get_organization(**kwargs): try: response = kwargs['client'].describe_organization() return response['Organization'] except Exception as exception: utils.handle_exception( friendly_description='Unable to list root organizational units', exception=exception)
def create_account(**kwargs): try: response = kwargs['client'].create_account( Email=kwargs['account_email'], AccountName=kwargs['account_name'], IamUserAccessToBilling='ALLOW') return response except Exception as exception: utils.handle_exception( friendly_description='Unable to create accounts', exception=exception)
def create_organization(**kwargs): try: response = kwargs['client'].create_organization( FeatureSet=kwargs['FeatureSet']) print('Created organization') return response except kwargs[ 'client'].exceptions.AlreadyInOrganizationException as exception: print('A root organization was already made') return kwargs['client'].describe_organization() except Exception as exception: utils.handle_exception( friendly_description='Unable to make an organization', exception=exception)
def enable_service_control_policy(**kwargs): try: kwargs['client'].enable_policy_type( RootId=kwargs['root_id'], PolicyType='SERVICE_CONTROL_POLICY') print('Enabled service control policy SERVICE_CONTROL_POLICY') except kwargs[ 'client'].exceptions.PolicyTypeAlreadyEnabledException as exception: print( 'Service control policy SERVICE_CONTROL_POLICY is already enabled') except Exception as exception: utils.handle_exception( friendly_description= 'Unable to enable policy type SERVICE_CONTROL_POLICY', exception=exception)
def attach_service_control_policy(**kwargs): try: print('Attaching policy %s (%s) to %s (%s)' % (kwargs['policy_name'], kwargs['policy_id'], kwargs['target_name'], kwargs['target_id'])) kwargs['client'].attach_policy(PolicyId=kwargs['policy_id'], TargetId=kwargs['target_id']) except kwargs[ 'client'].exceptions.DuplicatePolicyAttachmentException as exception: print('Policy %s (%s) is already attached to %s (%s)' % (kwargs['policy_name'], kwargs['policy_id'], kwargs['target_name'], kwargs['target_id'])) except Exception as exception: utils.handle_exception( friendly_description='Unable to attach a service control policy', exception=exception)
def get_account(**kwargs): try: accounts = [] if 'parent_id' in kwargs: accounts = utils.get_results_from_paginator( client=kwargs['client'], api_call='list_accounts_for_parent', response_key='Accounts', args={'ParentId': kwargs['parent_id']}) else: accounts = utils.get_results_from_paginator( client=kwargs['client'], api_call='list_accounts', response_key='Accounts') for account in accounts: if account['Email'] == kwargs['email']: return account except Exception as exception: utils.handle_exception(friendly_description='Unable to get account', exception=exception)
else: point = endpoints.TCP4ClientEndpoint( reactor, options.server, options.port) yield point.connect(SendStreamFactory(buffer)) except Exception as x: LOG.error('Exception: {}'.format(x)) if __name__ == '__main__': from optparse import OptionParser parser = OptionParser() parser.add_option('--server', dest='server', default='127.0.0.1') parser.add_option('--port', '-p', dest='port', default=7777) parser.add_option('--nocompress', dest='nocompress', action='store_true', default=False) parser.add_option('--twisted', dest='twisted', action='store_true', default=False) options, args = parser.parse_args() LOG.info('options={}\nargs={}\n'.format(options, args)) SendStream.compress = not options.nocompress try: reactor.callLater(0, main_loop) reactor.run() except Exception as x: handle_exception(LOG, x, stack=True)
s = line.split() # Only yield if this was a valid line (allows for blank lines in STDIN) if len(s) == 3: if s[1] == '*': for i in xrange(NUM_QUARTERS): yield [s[0], str(i), s[2]] else: yield s if __name__ == "__main__": # Set up the command line argument parser. parser = ArgumentParser(description="Retrieve Kepler lightcurve data given " "a set of Kepler IDs and Quarter numbers from STDIN.") parser.add_argument("source", action="store", choices=['mast','disk'], help="Select the source where Kepler FITS files should be retrieved.") parser.add_argument("datapath", action="store", nargs='?', default=os.curdir+os.sep, help="(Root) path to the Kepler lightcurve " "data, such that root is the path part <root>/<nnnn>/<nnnnnnnnn>/. " "Defaults to the current working directory.") args = parser.parse_args() try: main(args.source, os.path.normpath(args.datapath), instream=sys.stdin, outstream=sys.stdout) except: handle_exception(sys.exc_info()) sys.exit(1)