Ejemplo n.º 1
0
def get_keyfile_dict():
    # get client secret from either the environment or from a local file
    if 'client_secret_json' in env:
        return parse_json(env['client_secret_json'])
    elif file_exists(CLIENT_SECRET_FILE):
        with open(CLIENT_SECRET_FILE) as fd:
            return parse_json(fd.read())
    raise RuntimeError('cannot find client secret json file')
Ejemplo n.º 2
0
 def patch(self, request):
     request_json = parse_json(request.body)
     if request_json.get("username", None) is not None:
         if request.user.username != request_json["username"]:
             if User.objects.filter(
                     username=request_json["username"]).first() is not None:
                 return JsonResponse({
                     "status": "error",
                     "error": "user_exists"
                 })
             request.user.username = request_json["username"]
             request.user.save()
     if request_json.get("firstName", None) is not None:
         request.user.player.first_name = request_json["firstName"]
         request.user.player.save()
         request.user.first_name = request_json["firstName"]
         request.user.save()
     if request_json.get("lastName", None) is not None:
         request.user.player.last_name = request_json["lastName"]
         request.user.player.save()
         request.user.last_name = request_json["lastName"]
         request.user.save()
     if request_json.get("newPassword", None) is not None:
         if app_authenticate(username=request.user.username,
                             password=request_json["oldPassword"]) is None:
             return JsonResponse({
                 "status": "error",
                 "error": "bad_password"
             })
         request.user.set_password(request_json["newPassword"])
         request.user.save()
         app_login(request, request.user)
     return JsonResponse({"status": "ok"})
Ejemplo n.º 3
0
def fetch_json(resource, filters = {}, fields = [], limit = 40):
	'''
	Fetches a json for given resource and parameters.
	Parameters:
		resource: string
		filters: dictionary
		fields: list of strings
	Returns the json records if positive, an empty list if an error occurred.

	TODO: Maybe a little escaping of the url must be done.
	TODO: Use filters (maybe)
	'''
	req_payload = {RESOURCE_PARAMETER: resource, LIMIT_PARAMETER: int(limit)}
	if filters: req_payload[FILTERS_PARAMETER] = filters
	if fields: req_payload[FIELDS_PARAMETER] = fields
	r = Request(DATA_QUERY_URL, headers = {'Content-Type': 'application/json'}, \
		data = to_json_bytes(req_payload).encode())
	try:
		with urlopen(r) as response:
			if response.code is not 200:
				print("Bad response code: {}".format(response.code))
				return []
			json = parse_json(response.read())
			if json['success'] is not True:
				print("An error occurred: {}".format(json['error']['message']))
				return []
			if 'total' in json['result'] and int(json['result']['total']) > 0:
				return json['result']['records']
			else:
				print("No results found.")
				exit(1)
	except HTTPError as httpe:
		print(httpe)
		exit(1)
Ejemplo n.º 4
0
def get_form_from_request(_request) -> Union[dict, None]:
    if _request.method == "GET":
        d = _request.args.to_dict(
            flat=False
        )  # From MultiDict, obtain all values with the same key as a list
        parsed_d = {}
        for k, v_list in d.items():
            parsed_v_list = []
            for v in v_list:
                try:
                    parsed_v = parse_json(v)
                except JSONDecodeError:
                    parsed_v = v
                if isinstance(parsed_v, list):
                    parsed_v_list.extend(parsed_v)
                else:
                    parsed_v_list.append(v)
            if len(parsed_v_list) == 1:  # Flatten single-value lists
                parsed_d[k] = parsed_v_list[0]
            else:
                parsed_d[k] = parsed_v_list
        return parsed_d
    elif _request.method == "POST":
        return _request.get_json(force=True)
    else:
        return None
Ejemplo n.º 5
0
def get_details(api_call, username, password, *args, **kwargs):
    """
    Get the tx project info through the API.

    This function can also be used to check the existence of a project.
    """
    import base64
    url = (API_URLS[api_call] % (kwargs)).encode('UTF-8')

    req = urllib2.Request(url=url)
    base64string = base64.encodestring('%s:%s' % (username, password))[:-1]
    authheader = "Basic %s" % base64string
    req.add_header("Authorization", authheader)

    try:
        fh = urllib2.urlopen(req)
        raw = fh.read()
        fh.close()
        remote_project = parse_json(raw)
    except urllib2.HTTPError, e:
        if e.code in [401, 403, 404]:
            raise e
        else:
            # For other requests, we should print the message as well
            raise Exception("Remote server replied: %s" % e.read())
Ejemplo n.º 6
0
def get_details(api_call, username, password, *args, **kwargs):
    """
    Get the tx project info through the API.

    This function can also be used to check the existence of a project.
    """
    import base64
    url = (API_URLS[api_call] % (kwargs)).encode('UTF-8')
    verify_ssl(url)

    req = urllib2.Request(url=url)
    base64string = base64.encodestring('%s:%s' % (username, password))[:-1]
    authheader = "Basic %s" % base64string
    req.add_header("Authorization", authheader)

    try:
        fh = urllib2.urlopen(req)
        raw = fh.read()
        fh.close()
        remote_project = parse_json(raw)
    except urllib2.HTTPError, e:
        if e.code in [401, 403, 404]:
            raise e
        else:
            # For other requests, we should print the message as well
            raise Exception("Remote server replied: %s" % e.read())
Ejemplo n.º 7
0
def complete_order():
    try:
        data = request.data
        d = parse_json(data)
    except Exception:
        return bad_request_with_message_code(
            'По ТЗ такое невозможно, но не удалось спарсить '
            'json в теле запроса')

    if len(d) != 3 or d.get('courier_id') is None or \
            d.get('order_id') is None or d.get('complete_time') is None:
        return bad_request_code()

    try:
        courier_id = validator.validate_id(d.get('courier_id'))
    except Exception:
        return bad_request_code()

    try:
        order_id = validator.validate_id(d.get('order_id'))
    except Exception:
        return bad_request_code()

    try:
        complete_time = validator.validate_long_time(d.get('complete_time'))
    except Exception:
        return bad_request_code()

    try:
        services.complete_order(courier_id, order_id, complete_time)
    except Exception:
        return bad_request_code()

    return ok_code(generate_json({"order_id": order_id}))
Ejemplo n.º 8
0
    def __init__(self):
        """
        Initialize the Project attributes.
        """
        # The path to the root of the project, where .tx lives!
        self.root = find_dot_tx()
        if not self.root:
            print "Cannot find any .tx directory!"
            print "Run 'tx init' to initialize your project first!"
            raise ProjectNotInit()

        # The path to the txdata file (.tx/txdata)
        self.txdata_file = os.path.join(self.root, ".tx", "txdata")
        # Touch the file if it doesn't exist
        if not os.path.exists(self.txdata_file):
            print "Cannot find the txdata file (.tx/txdata)!"
            print "Run 'tx init' to fix this!"
            raise ProjectNotInit()

        # The dictionary which holds the txdata parameters after deser/tion.
        # Read the txdata in memory
        self.txdata = {}
        try:
            self.txdata = parse_json(open(self.txdata_file).read())
        except Exception, err:
            print "WARNING: Cannot open/parse .tx/txdata file", err
            print "Run 'tx init' to fix this!"
            raise ProjectNotInit()
Ejemplo n.º 9
0
 def __call__(self, xtmf_ScenarioNumber, xtmf_WorksheetPath, xtmf_ExportPath, xtmf_ConfigString):
     
     #raise NotImplementedError()
     emme_desktop = _MODELLER.desktop        
     
     try:
         scenario = _MODELLER.emmebank.scenario(xtmf_ScenarioNumber)
         if scenario == None:
             raise Exception("Emme scenario %s not found." %xtmf_ScenarioNumber)
         
         worksheet = emme_desktop.open_worksheet(xtmf_WorksheetPath)
         export_path = xtmf_ExportPath
         
         #Replace single quotes with doubles and backslashes with forwardslashes
         modified_config = xtmf_ConfigString.replace("'", '"').replace('\\', '/')
         try:
             config = parse_json(modified_config)
         except:
             print modified_config
             raise
         
         self._execute(scenario, worksheet, export_path, config)
     except Exception, e:
         msg = str(e) + "\n" + _traceback.format_exc(e)
         raise Exception(msg)
Ejemplo n.º 10
0
 def __call__(self, xtmf_ScenarioNumber, xtmf_WorksheetPath, xtmf_ExportPath, xtmf_ConfigString):
     
     #raise NotImplementedError()
     emme_desktop = _MODELLER.desktop        
     
     try:
         scenario = _MODELLER.emmebank.scenario(xtmf_ScenarioNumber)
         if scenario is None:
             raise Exception("Emme scenario %s not found." %xtmf_ScenarioNumber)
         
         worksheet = emme_desktop.open_worksheet(xtmf_WorksheetPath)
         export_path = xtmf_ExportPath
         
         #Replace single quotes with doubles and backslashes with forwardslashes
         modified_config = xtmf_ConfigString.replace("'", '"').replace('\\', '/')
         try:
             config = parse_json(modified_config)
         except:
             print modified_config
             raise
         
         self._execute(scenario, worksheet, export_path, config)
     except Exception as e:
         msg = str(e) + "\n" + _traceback.format_exc()
         raise Exception(msg)
     return
Ejemplo n.º 11
0
def detail(request, lang_code, prop_id):
	template_dict = generate_base_dict(lang_code, 'detail')

	if request.method == 'POST' and 'property_ids' in request.POST:
		template_dict['property_ids'] = parse_json(request.POST['property_ids'])

	template_dict['curr_property_id'] = prop_id
	template_dict['curr_property_coord'] = [PropertyCoordinate.objects.filter(property__property_id = prop_id)[0].latitude, PropertyCoordinate.objects.filter(property__property_id = prop_id)[0].longitude] 

	template_dict['property'] = Property.objects.filter(property_id = prop_id)[0]
	template_dict['property'].description = PropertyDescription.objects.filter(language__lang_code = lang_code, property__property_id = template_dict['property'].property_id)[0].description

	images = PropertyThumbnail.objects.filter(property__property_id = prop_id)
	template_dict['big_images'] = [sub(r'(.*/)([^/]+)', r'\1detail-big/\2', i.image.name) for i in images if match(r'.*(T|t)humbnail.*', i.image.name) == None]
	
	template_dict['small_images'] = [sub(r'(.*/)([^/]+)', r'\1detail-small/\2', i.image.name) for i in images if match(r'.*(T|t)humbnail.*', i.image.name) == None]
	template_dict['small_images'] = ['images/properties/detail-small/filler.png'] + template_dict['small_images'] + ['images/properties/detail-small/filler.png']

	template_dict = add_searchform(template_dict)
	template_dict = add_testimonials(template_dict)

	# TODO: this needs to be calculated..
	template_dict['small_total_width'] = 150 * (len(template_dict['small_images']) + 2)
	template_dict['big_total_width'] = 768 * len(template_dict['big_images'])
	template_dict['total_images'] = len(template_dict['big_images'])
	
	return render_to_response('pages/detail.html', template_dict, context_instance = RequestContext(request))
Ejemplo n.º 12
0
def get_details(api_call, username, password, *args, **kwargs):
    """
    Get the tx project info through the API.

    This function can also be used to check the existence of a project.
    """
    url = (API_URLS[api_call] % (kwargs)).encode('UTF-8')
    conn = urllib3.connection_from_url(kwargs['hostname'])
    headers = urllib3.util.make_headers(
        basic_auth='{0}:{1}'.format(username, password),
        accept_encoding=True,
        user_agent=user_agent_identifier(),
    )
    try:
        r = conn.request('GET', url, headers=headers)
        if r.status < 200 or r.status >= 400:
            raise Exception(r.data)
        remote_project = parse_json(r.data)
        return remote_project
    except ssl.SSLError:
        logger.error("Invalid SSL certificate")
        raise
    except Exception, e:
        logger.debug(unicode(e))
        raise
Ejemplo n.º 13
0
 def post(self, request):
     request_json = parse_json(request.body)
     friend = None
     if request_json.get("username", None) is not None:
         if request_json["username"] == request.user.username:
             return JsonResponse({
                 "status": "error",
                 "error": "self_friending"
             })
         user = User.objects.filter(
             username=request_json["username"]).first()
         if user is None:
             return JsonResponse({
                 "status": "error",
                 "error": "no_matching_username"
             })
         friend = user.player
     if request_json.get("id", None) is not None:
         friend = Player.objects.filter(pk=request_json["id"]).first()
     if friend is None:
         return JsonResponse({
             "status": "error",
             "error": "no_matching_user"
         })
     if Friendship.objects.filter(giver=request.user.player,
                                  taker=friend).first() is not None:
         return JsonResponse({
             "status": "error",
             "error": "friendship_exists"
         })
     friendship = Friendship(giver=request.user.player, taker=friend)
     friendship.save()
     return JsonResponse({"status": "ok", "friend": friend.to_dict()})
Ejemplo n.º 14
0
    def get_server_config(self):
        ssl = True
        try:
            hostmeta_url = 'https://%s/.well-known/host-meta' % self.host
            connection = self.opener.open(hostmeta_url)
            response = connection.read()
        except (urllib2.HTTPError, urllib2.URLError):
            ssl = False
            hostmeta_url = '%s://%s/.well-known/host-meta' % self.host
            connection = self.opener.open(hostmeta_url)
            response = connection.read()
        content_type = connection.headers['content-type']
        connection.close()

        if 'json' in content_type:
            data = parse_json(response)
            for link in data.get('links', []):
                if 'template' in link:
                    template_url = link['template']
                    break
        else:
            tree = ElementTree.fromstring(response)
            for link in tree.findall(QUALIFIED_LINK):
                template_url = link.attrib.get('template')
                if template_url:
                    break

        return ssl, template_url
Ejemplo n.º 15
0
 def get_ui_langs(self):
     our_path = os.path.dirname(os.path.realpath(__file__))
     core_file = our_path + '/../app/config/core.php'
     php_script = ('class Cache {'
                   '    function Config() {}'
                   '}'
                   'class Inflector {'
                   '    function slug() {}'
                   '}'
                   'class Configure {'
                   '    function write($var, $val) {'
                   '        if ($var == "UI.languages") {'
                   '            print json_encode(array_map('
                   '                function($v) {'
                   '                    return $v[0];'
                   '                },'
                   '                $val'
                   '            ));'
                   '        }'
                   '    }'
                   '}'
                   'define("APP_DIR", "");'
                   'include "' + core_file + '";')
     php_cmd = "php -r '" + php_script + "'"
     proc = subprocess.Popen(php_cmd, shell=True, stdout=subprocess.PIPE)
     return parse_json(proc.stdout.read())
Ejemplo n.º 16
0
def search(request, lang_code):
	template_dict = generate_base_dict(lang_code, '/search/')
	if request.method == 'POST':
		query_dict = {}

		if 'shortlist_ids' in request.POST:
			print 'shortlist_ids', request.POST['shortlist_ids']
			query_dict['property_id__in'] = parse_json(request.POST['shortlist_ids'])
		else:
			query_dict['sale_type'] = request.POST['sale_type']
			
			placeholder = TextElementTranslation.objects.filter(language__lang_code = lang_code, element_name__element_name = 'searchboxes-form-location_placeholder')[0].element_text
			if request.POST['district'] != placeholder:
				query_dict['district__name__icontains'] = request.POST['district']
			
			if request.POST['type'] != 'NA':
				query_dict['type'] = request.POST['type']
			
			query_dict['price__gte'] = request.POST['min_price']
			query_dict['price__lte'] = request.POST['max_price']
			query_dict['bed_count__gte'] = request.POST['min_bedrooms']
			query_dict['bed_count__lte'] = request.POST['max_bedrooms']

		print query_dict

		template_dict['search_results'] = Property.objects.filter(**query_dict).order_by('price')
	else:
		template_dict['search_results'] = Property.objects.all().order_by('price')

	template_dict['property_ids'] = [p.property_id for p in template_dict['search_results']]

	# Gathering images for the properties
	for i in range(len(template_dict['search_results'])):
		template_dict['search_results'][i].searchimages = [sub(r'(.*/)([^/]+)', r'\1searchpage/\2', j.image.name) for j in PropertyThumbnail.objects.filter(property__property_id = template_dict['search_results'][i].property_id)][1:5]

	for result in template_dict['search_results']:
		desc = PropertyDescription.objects.filter(language__lang_code = lang_code, property__property_id = result.property_id)[0].description
		if len(desc) >= 150:
			result.description = desc[:150] + '...'
		else:
			result.description = desc
	
	template_dict = add_searchform(template_dict)
	template_dict = add_testimonials(template_dict)

	price_low_to_high_text = TextElementTranslation.objects.filter(language__lang_code = lang_code, element_name__element_name = 'search-ordering_price_low_to_high')[0].element_text
	price_high_to_low_text = TextElementTranslation.objects.filter(language__lang_code = lang_code, element_name__element_name = 'search-ordering_price_high_to_low')[0].element_text
	template_dict['ordering_dict'] = {'plh': price_low_to_high_text, 'phl': price_high_to_low_text};
	template_dict['default_ordering'] = 'plh'

	all_text = TextElementTranslation.objects.filter(language__lang_code = lang_code, element_name__element_name = 'search-all_text')[0].element_text
	template_dict['result_per_page_dict'] = [(5,'5'), (10,'10'), (15,'15'), (99999999, all_text)]
	template_dict['default_result_per_page'] = 5
	
	print template_dict

	return render_to_response('pages/search.html', template_dict, context_instance = RequestContext(request))
Ejemplo n.º 17
0
 def post(self, request):
     request_json = parse_json(request.body)
     if Table.objects.filter(name=request_json["name"]).first() is not None:
         return JsonResponse({"status": "error", "error": "table_exists"})
     table = Table(name=request_json["name"], creator=request.user.player)
     table.save()
     table_member = TableMember(table=table, player=request.user.player)
     table_member.save()
     return JsonResponse({'status': 'ok', 'table': table.to_dict()})
Ejemplo n.º 18
0
 def handle_enums(self, msg, service):
     if not msg[MSG_KEY_STATUS] and service in self._service_infos:
         enum_list = parse_json(msg[MSG_KEY_PAYLOAD])
         if not enum_list == None:
             self._service_infos[service]['raw_enums'] = enum_list and enum_list[0] or []
             self._service_infos[service]['parsed_enums'] = True
             if self.check_map_complete('parsed_enums'):
                 self.request_infos()
     else:
         print "handling of message failed in handle_messages in MessageMap:"
         print msg 
Ejemplo n.º 19
0
        def post(self, request):
            registration_json = parse_json(request.body)
            credentials_json = registration_json["credentials"]
            player_json = registration_json["player"]

            username = credentials_json['username']
            password = credentials_json['password']
            invitation_code = credentials_json['invitationCode']

            invitation = Invitation.objects.filter(
                code=invitation_code).first()
            if invitation is None:
                return JsonResponse({
                    'status': 'error',
                    'error': "invalid_invitation"
                })

            existing_user = User.objects.filter(username=username).first()
            if existing_user is not None:
                return JsonResponse({
                    'status': 'error',
                    'error': "user_exists"
                })

            user = User.objects.create_user(username, username, password)
            app_login(request, user)

            user.first_name = player_json["firstName"]
            user.last_name = player_json["lastName"]
            user.save()

            if invitation.is_guest_code:
                guest = Player.objects.filter(guest_invitation=invitation,
                                              is_guest=True).first()
                if guest is None:
                    return JsonResponse({
                        "status": "error",
                        "error": "no_matching_guest"
                    })
                guest.first_name = user.first_name
                guest.last_name = user.last_name
                guest.user = user
                guest.is_guest = False
                guest.guest_invitation = None
                guest.save()
                invitation.delete()
            else:
                player = Player(first_name=user.first_name,
                                last_name=user.last_name,
                                user=user)
                player.save()

            return JsonResponse({'status': 'ok', 'user': user.to_dict()})
Ejemplo n.º 20
0
def test_ok_bigdoc(test_tools, lambda_function):

    bigdoc_file = open('res/bigdoc.json')
    bigdoc = parse_json(bigdoc_file.read())

    response = lambda_function.call(bigdoc)

    body = test_tools.assert_response(response)
    test_tools.assert_response_ok(response, body)

    assert 'f' in body
    assert 'nST' in body
    assert type(body['nST']) is int
Ejemplo n.º 21
0
def get_details(api_call, username, password, *args, **kwargs):
    """
    Get the tx project info through the API.
    This function can also be used to check the existence of a project.
    """
    url = API_URLS[api_call] % kwargs
    try:
        data, charset = make_request('GET', kwargs['hostname'], url, username,
                                     password)
        return parse_json(data)
    except Exception as e:
        logger.debug(six.u(str(e)))
        raise
Ejemplo n.º 22
0
def get_details(api_call, username, password, *args, **kwargs):
    """
    Get the tx project info through the API.

    This function can also be used to check the existence of a project.
    """
    url = API_URLS[api_call] % kwargs
    try:
        data, charset = make_request('GET', kwargs['hostname'], url, username, password)
        return parse_json(data)
    except Exception as e:
        logger.debug(six.u(str(e)))
        raise
Ejemplo n.º 23
0
 def handle_messages(self, msg, service):
     if not msg[MSG_KEY_STATUS] and service in self._service_infos:
         message_list = parse_json(msg[MSG_KEY_PAYLOAD])
         self._service_infos[service]['raw_messages'] = message_list
         # the message list can be empty (e.g. for the 'core' service)
         if message_list:
             self.parse_raw_lists(service)
         self._service_infos[service]['parsed'] = True
         if self.check_map_complete('parsed'):
             self.finalize()
     else:
         print "handling of message failed in handle_messages in MessageMap:"
         print msg
Ejemplo n.º 24
0
def update_courier_by_id(courier_id):
    try:
        data = request.data
        d = parse_json(data)
    except Exception:
        return bad_request_with_message_code(
            'Не удалось спарсить json в теле запроса')

    try:
        courier = services.get_courier_by_id(courier_id)
    except Exception:
        return not_found_code()  # Если курьер не существует, то вернуть 404

    # Поля которые могут присутствовать в этом запросе
    accepted_fields = ['courier_type', 'regions', 'working_hours']
    for key in list(d.keys()):
        if key not in accepted_fields:
            return bad_request_with_message_code(
                'В запросе присутствуют неописанные поля')

    if d.get('courier_type') is not None:
        try:
            t = validator.validate_type(d.get('courier_type'))
            courier.courier_type = t
        except Exception as e:
            return bad_request_with_message_code(
                'Неверное поле courier_type: ' + str(e))

    if d.get('regions') is not None:
        try:
            regs = validator.validate_regions(d.get('regions'))
            courier.regions = regs
        except Exception as e:
            return bad_request_with_message_code('Неверное поле regions: ' +
                                                 str(e))

    if d.get('working_hours') is not None:
        try:
            hours = validator.validate_time_list(d.get('working_hours'))
            courier.working_hours = hours
        except Exception as e:
            return bad_request_with_message_code(
                'Неверное поле working_hours: ' + str(e))

    try:
        dal.update_courier(courier)
    except Exception:
        return bad_request_with_message_code(
            'Произошла какая-то шибка в базе данных')

    return ok_code(generate_json(courier.to_dict()))
def _get_option_values_parsed(raw_options):
    options = {}
    for (option_name, option_value) in raw_options.items():
        try:
            decoded_option_value = parse_json(option_value)
        except ValueError:
            raise InvalidSettingValueError(
                'Could not decode value for option %r: %r' % (
                    option_name,
                    option_value,
                    ),
                )
        options[option_name] = decoded_option_value
    return options
Ejemplo n.º 26
0
    def _process_event(self, evt):
        data = parse_json(evt.data)

        for i, key in enumerate(data):
            if key in self._handlers:
                handler = self._handlers[key]
                processed_item = handler.return_type(
                    data=data, column_map=handler.column_map)
                if key == "player":
                    self._process_player_state(processed_item)
                else:
                    if handler.attach_attr is not None:
                        setattr(self, handler.attach_attr, processed_item)
                    for callback in handler.callbacks:
                        callback(processed_item)
Ejemplo n.º 27
0
 def handle_host_info(self, msg):
     if not msg[MSG_KEY_STATUS]:
         host_info = parse_json(msg[MSG_KEY_PAYLOAD])
         if host_info:
             for service in host_info[5]:
                 if service[0] == "scope":
                     versions = map(int, service[1].split('.'))
                     self.scope_major_version = versions[0]
                     self.scope_minor_version = versions[1]
             if self.scope_minor_version >= 1:
                 self.request_enums()
             else:
                 self.request_infos()
     else:
         print "getting host info failed"
Ejemplo n.º 28
0
    def updateAppstreamPackages(self):
        """
        This methode update appstream package and download package who need to be
        download. It can effectly work only one at a time.
        @rtype: bool
        @return: True if done,False if already working.
        """
        # if an other update is working, don't update.
        if self.update:
            return False
        self.update = True

        tempfile.tempdir = '/var/tmp/'
        logger = logging.getLogger()
        appstream_url = PkgsConfig("pkgs").appstream_url

        #add non downloaded package to download package list
        for pkg, details in getActivatedAppstreamPackages().iteritems():

            try:
                # Creating requests session
                s = requests.Session()
                s.auth = ('appstream', details['key'])
                base_url = '%s/%s/' % (appstream_url, pkg)

                # Get Package uuid
                r = s.get(base_url + 'info.json')
                if not r.ok:
                    raise Exception("Cannot get package metadata. Status: %d" %
                                    (r.status_code))
                info = parse_json(r.content.strip())
                uuid = info['uuid']

                # If package is already downloaded, skip
                logger.debug('Got UUID %s, checking if it already exists ...' %
                             uuid)
                if not uuid or os.path.exists(
                        '/var/lib/pulse2/appstream_packages/%s/' % uuid):
                    continue
                #add package to download package list
                self._add_appstream(pkg)

            except Exception, e:
                logger.error('Appstream: Error while fetching package %s' %
                             pkg)
                logger.error(str(e))
Ejemplo n.º 29
0
 def handle_info(self, msg, service):
     if not msg[MSG_KEY_STATUS] and service in self._service_infos:
         command_list = parse_json(msg[MSG_KEY_PAYLOAD])
         if command_list:
             self._service_infos[service]['raw_infos'] = command_list
             tag = tag_manager.set_callback(self.handle_messages, {'service': service})
             self._connection.send_command_STP_1({
                 MSG_KEY_TYPE: MSG_VALUE_COMMAND,
                 MSG_KEY_SERVICE: "scope",
                 MSG_KEY_COMMAND_ID: self.COMMAND_MESSAGE_INFO,
                 MSG_KEY_FORMAT: MSG_VALUE_FORMAT_JSON,
                 MSG_KEY_TAG: tag,
                 MSG_KEY_PAYLOAD: '["%s", [], 1, 1]' % service
                 })
     else:
         print "handling of message failed in handle_info in MessageMap:"
         print msg
Ejemplo n.º 30
0
 def get_ui_langs(self):
     our_path = os.path.dirname(os.path.realpath(__file__))
     core_file = our_path + '/../app/Config/core.php'
     php_script = ('define("WWW_ROOT", "");'
                   'define("ROOT", "");'
                   'define("DS", "");'
                   'include("src/Lib/LanguagesLib.php");'
                   '$conf = include("config/app_local.php");'
                   'print json_encode(array_map('
                   '    function($v) {'
                   '        return \App\Lib\LanguagesLib::languageTag($v[0]);'
                   '    },'
                   '    $conf["UI"]["languages"]'
                   '));')
     php_cmd = "php -r '" + php_script + "'"
     proc = subprocess.Popen(php_cmd, shell=True, stdout=subprocess.PIPE)
     return parse_json(proc.stdout.read())
Ejemplo n.º 31
0
 def __init__(self, path):
     self.path = path
     with open(path, "r") as f:
         self._data = parse_json(f.read())
     try:
         self.title = self._data["title"]
         self.timestamp = Timestamp(
             self._data["photoTakenTime"]["formatted"],
             self._data["creationTime"]["formatted"],
             self._data["modificationTime"]["formatted"])
         self.location = Location(self._data["geoDataExif"]["latitude"],
                                  self._data["geoDataExif"]["longitude"],
                                  self._data["geoDataExif"]["altitude"])
     except KeyError:
         raise ValueError(
             f"warning: insufficient metadata in JSON file {path}. ignoring..."
         )
Ejemplo n.º 32
0
 def get_finger_data(self, template):
     data_url = template.replace('{uri}', 'acct:%s@%s' %
         (self.user, self.host))
     connection = self.opener.open(data_url)
     response = connection.read()
     connection.close()
     if 'json' in connection.headers['content-type']:
         for link in parse_json(response).get('links', []):
             if link.get('rel', '') == 'remoteStorage':
                 return link
     else:
         tree = ElementTree.fromstring(response)
         for link in tree.findall(QUALIFIED_LINK):
             rel = link.attrib.get('rel')
             if rel == 'remoteStorage':
                 return {'template': link.attrib.get('template'),
                         'api': link.attrib.get('api'),
                         'auth': link.attrib.get('auth')}
Ejemplo n.º 33
0
 def post(self, request):
     credentials_json = parse_json(request.body)
     username = credentials_json['username']
     password = credentials_json['password']
     user = app_authenticate(request,
                             username=username,
                             password=password)
     if user is not None:
         app_login(request, user)
         return JsonResponse({
             'status': 'ok',
             'user': request.user.to_dict()
         })
     else:
         return JsonResponse({
             'status': 'error',
             'error': "invalid_login_credentials"
         })
Ejemplo n.º 34
0
    def updateAppstreamPackages(self):
        """
        This methode update appstream package and download package who need to be
        download. It can effectly work only one at a time.
        @rtype: bool
        @return: True if done,False if already working.
        """
        # if an other update is working, don't update.
        if self.update:
            return False
        self.update = True

        tempfile.tempdir = "/var/tmp/"
        logger = logging.getLogger()
        appstream_url = PkgsConfig("pkgs").appstream_url

        # add non downloaded package to download package list
        for pkg, details in getActivatedAppstreamPackages().iteritems():

            try:
                # Creating requests session
                s = requests.Session()
                s.auth = ("appstream", details["key"])
                base_url = "%s/%s/" % (appstream_url, pkg)

                # Get Package uuid
                r = s.get(base_url + "info.json")
                if not r.ok:
                    raise Exception("Cannot get package metadata. Status: %d" % (r.status_code))
                info = parse_json(r.content.strip())
                uuid = info["uuid"]

                # If package is already downloaded, skip
                logger.debug("Got UUID %s, checking if it already exists ..." % uuid)
                if not uuid or os.path.exists("/var/lib/pulse2/appstream_packages/%s/" % uuid):
                    continue
                # add package to download package list
                self._add_appstream(pkg)

            except Exception, e:
                logger.error("Appstream: Error while fetching package %s" % pkg)
                logger.error(str(e))
Ejemplo n.º 35
0
def open_or_download(crypto: str, to_time: int, limit: int) -> Dict[str, any]:
    filename = Path("data") / "{0}-{1}-{2}.json".format(crypto, to_time, limit)

    try:
        with open(filename) as f:
            return parse_json(f.read())
    except:
        sleep(0.5)
        r = requests.get("https://min-api.cryptocompare.com/data/histominute",
                         params={
                             "fsym": crypto,
                             "tsym": "GBP",
                             "limit": limit,
                             "toTs": to_time
                         })

        data = r.json()
        with open(filename, "w") as f:
            f.write(r.text)

        return data
Ejemplo n.º 36
0
def get_project_info(hostname, username, passwd, project_slug):
    """
    Get the tx project info through the API.
    
    This function can also be used to check the existence of a project.
    """
    url = API_URLS['project_get'] % {'hostname':hostname, 'project':project_slug}
    opener = get_opener(hostname, username, passwd)
    urllib2.install_opener(opener)
    req = urllib2.Request(url=url)
    try:
        fh = urllib2.urlopen(req)
        raw = fh.read()
        fh.close()
        remote_project = parse_json(raw)
        return remote_project
    except urllib2.HTTPError:
        raise
        print "tx: The given project does not exist."
        print "Check your url and try again."
    return None
Ejemplo n.º 37
0
def pretty_print(prelude, msg, format, format_payload, verbose_debug=False):
    service = msg[MSG_KEY_SERVICE]
    command_def = message_map.get(service, {}).get(msg[MSG_KEY_COMMAND_ID], None)
    command_name = command_def and command_def.get("name", None) or \
                                    '<id: %d>' % msg[MSG_KEY_COMMAND_ID]
    message_type = message_type_map[msg[MSG_KEY_TYPE]]
    if not MessageMap.filter or check_message(service, command_name, message_type): 
        print prelude
        if format:
            print "  message type:", message_type
            print "  service:", service
            print "  command:", command_name
            print "  format:", format_type_map[msg[MSG_KEY_FORMAT]]
            if MSG_KEY_STATUS in msg:
                print "  status:", status_map[msg[MSG_KEY_STATUS]]
            if MSG_KEY_CLIENT_ID in msg:
                print "  cid:", msg[MSG_KEY_CLIENT_ID]
            if MSG_KEY_UUID in msg:
                print "  uuid:", msg[MSG_KEY_UUID]
            if MSG_KEY_TAG in msg:
                print "  tag:", msg[MSG_KEY_TAG]
            if format_payload and not msg[MSG_KEY_TYPE] == MSG_TYPE_ERROR:
                payload = parse_json(msg[MSG_KEY_PAYLOAD])
                print "  payload:"
                if payload and command_def:
                    definition = command_def.get(msg[MSG_KEY_TYPE], None)
                    try:
                        pretty_print_payload(payload, definition, verbose_debug=verbose_debug)
                    except Exception, msg:
                        # print msg 
                        print "failed to pretty print the paylod. wrong message structure?"
                        print "%spayload: %s" % (INDENT, payload)
                        print "%sdefinition: %s" % (INDENT, definition)
                else:
                    print "    ", msg[MSG_KEY_PAYLOAD]
                print "\n"
            else:
                print "  payload:", msg[MSG_KEY_PAYLOAD], "\n"
        else:
            print msg
Ejemplo n.º 38
0
    def push(self, force=False):
        """
        Push all the resources
        """

        raw = self.do_url_request('get_resources',
                                  project=self.get_project_slug())
        remote_resources = parse_json(raw)

        local_resources = self.txdata['resources']
        for remote_resource in remote_resources:
            name = remote_resource['name']
            for i, resource in enumerate(local_resources):
                if name in resource['resource_name'] :
                    del(local_resources[i])

        if local_resources != [] and not force:
            print "Following resources are not available on remote machine:", ", ".join([i['resource_name'] for i in local_resources])
            print "Use -f to force creation of new resources"
            exit(1)
        else:
            for resource in self.txdata['resources']:
                # Push source file
                print "Pushing source file %s" % resource['source_file']
                self.do_url_request('push_source', multipart=True,
                     files=[( "%s_%s" % (resource['resource_name'],
                                         resource['source_lang']),
                             self.get_full_path(resource['source_file']))],
                     project=self.get_project_slug())

                # Push translation files one by one
                for lang, f_obj in resource['translations'].iteritems():
                    print "Pushing %s to %s" % (lang, f_obj['file'])
                    self.do_url_request('push_source', multipart=True,
                         files=[( "%s_%s" % (resource['resource_name'],
                                             lang),
                                 self.get_full_path(f_obj['file']))],
                         project=self.get_project_slug())
Ejemplo n.º 39
0
def assign_orders():
    try:
        data = request.data
        d = parse_json(data)
    except Exception:
        return bad_request_with_message_code(
            'По ТЗ такое невозможно, но не удалось спарсить '
            'json в теле запроса')

    if len(d) != 1 or d.get('courier_id') is None:
        return bad_request_code()

    try:
        id = validator.validate_id(d.get('courier_id'))
        # Проверим, существует ли курьер
        services.get_courier_by_id(d.get('courier_id'))
    except Exception:
        return bad_request_code()

    try:
        result = services.assign_orders(id)
        if type(result) is tuple:
            order_ids = result[0]
            assign_time = result[1].isoformat()[:-4] + 'Z'
            ans = generate_json({
                "orders": [{
                    "id": x
                } for x in order_ids],
                "assign_time": assign_time
            })
            return ok_code(ans)
        else:
            ans = generate_json({"orders": [{"id": x} for x in result]})
            return ok_code(ans)
    except Exception:
        return bad_request_with_message_code(
            'Во время выполнения запроса произошла ошибка.')
Ejemplo n.º 40
0
#! /usr/bin/env python3
from bs4 import BeautifulSoup
from collections import OrderedDict
from datetime import datetime
from json import load as parse_json, dump as dump_json
from os import stat
from os.path import basename
from urllib.parse import quote as urlquote
import optparse
import requests

with open('config.json') as json_data_file:
    configuration = parse_json(json_data_file)
    canvas = configuration['canvas']
    access_token= canvas["access_token"]
    baseUrl = 'https://%s/api/v1/courses/' % canvas.get('host', 'kth.instructure.com')
    header = {'Authorization' : 'Bearer ' + access_token}
    lmsapiurl = configuration['lmsapi']

def main():
    parser = optparse.OptionParser(
        usage="Usage: %prog [options] course_code")

    parser.add_option('-v', '--verbose',
                      dest="verbose",
                      default=False,
                      action="store_true",
                      help="Print lots of output to stdout"
    )
    parser.add_option('--canvasid', dest='canvasid',
                      help="Canvas id for the course (or use lms api)"
Ejemplo n.º 41
0
def main():
    parser = optparse.OptionParser(
        usage="Usage: %prog [options] course_code")

    parser.add_option('-v', '--verbose',
                      dest="verbose",
                      default=False,
                      action="store_true",
                      help="Print lots of output to stdout"
    )
    parser.add_option('--canvasid', dest='canvasid',
                      help="Canvas id for the course (or use lms api)"
    )
    parser.add_option('--dir', dest='dumpdir', default='dump',
                      help="Directory to read dumps from"
    )
    parser.add_option('--nop', dest='nop', default=False, action='store_true',
                      help="Only show canvas course for course round."
    )

    options, args = parser.parse_args()
    if options.canvasid and len(args) != 1:
        parser.error("Exactly one course_code is required when giving canvas id")
    elif len(args) == 0:
        parser.error("At least one course_code is required")

    for course_code in args:
        course_id = options.canvasid or find_canvas_id(course_code)
        if not course_id:
            print("Canvas course id not given or found")
            exit(1)
        dumpdir = options.dumpdir
        if options.verbose:
            print("Upload to %s (canvas #%s) from %s" % (
                course_code, course_id, dumpdir))
        if options.nop:
            continue

        course_code = course_code[:6]
        with open('%s/%s/pages.json' % (dumpdir, course_code)) as json:
            dumpdata = parse_json(json)

        uploaded_files = {}
        for data in dumpdata:
            if options.verbose:
                print("Should upload", data)

            # Use the Canvas API to insert the page
            #POST /api/v1/courses/:course_id/pages
            #    wiki_page[title]
            #    wiki_page[body]
            #    wiki_page[published]
            html = BeautifulSoup(open("%s/%s/pages/%s.html" % (dumpdir, course_code, data['slug'])), "html.parser")
            for link in html.findAll(href=True):
                linkdata = next(filter(lambda i: i['url'] == link['href'], data['links']), None)
                if linkdata and linkdata.get('category') == 'file':
                    canvas_url = uploaded_files.get(link['href'])
                    if not canvas_url:
                        canvas_url = create_file(course_id, '%s/%s/pages/%s' % (dumpdir, course_code, linkdata['url']),
                                                 basename(linkdata['url']))
                        print("Uploaded %s to %s for link" % (link['href'], canvas_url))
                        uploaded_files[link['href']] = canvas_url
                    else:
                        print("%s is allready at %s" % (link['href'], canvas_url))
                    link['href'] = canvas_url
                    linkdata['url'] = canvas_url

            for img in html.findAll('img'):
                imgdata = next(filter(lambda i: i['url'] == img.get('src'), data['links']), {})
                if imgdata.get('category') == 'file':
                    canvas_url = uploaded_files.get(img['src'])
                    if not canvas_url:
                        canvas_url = create_file(course_id, '%s/%s/pages/%s' % (dumpdir, course_code, imgdata['url']),
                                                 basename(imgdata['url']))
                        print("Uploaded %s to %s for img" % (img['src'], canvas_url))
                        uploaded_files[img['src']] = canvas_url
                    else:
                        print("%s is allready at %s" % (img['src'], canvas_url))
                    img['src'] = canvas_url
                    imgdata['url'] = canvas_url

            for tex in html.findAll('span', attrs={'role': 'formula', 'data-language': 'tex'}):
                img = html.new_tag('img')
                img['src'] = '/equation_images/' + urlquote(tex.text)
                img['alt'] = tex.text
                img['class'] = tex.get('class')
                tex.replace_with(img)
                if options.verbose:
                    print("Modified formula %s to: %s" % (tex, img))

            url = baseUrl + '%s/pages' % (course_id)
            print("Should post page to", url)
            payload={
                'wiki_page[title]': data['title'],
                'wiki_page[published]': False,
                'wiki_page[body]': str(html)
            }
            if options.verbose:
                print(payload)
            r = requests.post(url, headers = header, data=payload)
            if r.status_code == requests.codes.ok:
                page_response=r.json()
                if options.verbose:
                    print("result of post creating page: %s" % page_response)
                print("Uploaded page to %s" % page_response['html_url'])
                data['url'] = page_response['html_url']
            else:
                print("Failed to upload page %s: %s" % (data['title'], r))
        dumpname = '%s/%s/zzz-import-%s-%s.json' % (
            dumpdir, course_code, course_code, datetime.now().strftime('%Y%m%d-%H%M%S'))
        with open(dumpname, 'w') as json:
            dump_json(dumpdata, json, indent=4)
        result = create_file(course_id, dumpname, basename(dumpname))
        print('Uploaded final result to %s' % result)
Ejemplo n.º 42
0
def process_json(data, json, word):
    data = parse_json(data)
    return render_template(json, items=data['items'])
Ejemplo n.º 43
0
    def parse_errors(self, output):
        # ignore the first line which is not json (WTF !)
        errors = parse_json(output.splitlines()[1])

        return [(e['line'], e['column'], e['description']) for e in errors
                if e['level'] == 'error']
Ejemplo n.º 44
0
def import_couriers():
    try:
        data = request.data
        d = parse_json(data)['data']
    except Exception:
        return bad_request_with_message_code(
            'По ТЗ такое невозможно, но не удалось спарсить json в теле '
            'запроса, возможно отстутствует поле data')

    # Это сообщение первой ошибки валидации
    first_validation_message = None

    success_ids = []
    not_success_ids = []
    new_couriers = []
    for e in d:
        if e.get('courier_id') is None:
            return bad_request_with_message_code(
                'По ТЗ такое невозможно, но у курьера отвутствует '
                'поле courier_id')

        # По ТЗ courier_id всегда присутствует, поэтому
        if len(e) != 4 or e.get('courier_type') is None or e.get('regions') \
                is None or e.get('working_hours') is None:
            not_success_ids.append(e['courier_id'])
            if first_validation_message is None:
                first_validation_message = (f'У курьера #{e["courier_id"]} '
                                            'отсутствует обязательное поле '
                                            'или присутствует неописанное')
            continue

        # Если курьер уже содержится в бд, то бракуем его
        if services.is_couriers_contains_id(e['courier_id']):
            not_success_ids.append(e['courier_id'])
            if first_validation_message is None:
                first_validation_message = (f'Курьер #{e["courier_id"]} уже '
                                            'содержится в базе данных')

        try:
            entity = validator.validate_courier(e)
            new_couriers.append(entity)
            success_ids.append(entity.courier_id)
        except ValidationException as exc:
            not_success_ids.append(e['courier_id'])
            if first_validation_message is None:
                first_validation_message = ('При валидации курьера '
                                            f'#{e["courier_id"]} '
                                            'произошла обшибка: ' + str(exc))
    # Отсортарую id и сделаю их уникальными для красоты
    success_ids = list(set(success_ids))
    success_ids.sort()
    not_success_ids = list(set(not_success_ids))
    not_success_ids.sort()

    if len(not_success_ids) == 0:
        try:
            services.add_couriers(new_couriers)
        except Exception:
            return bad_request_with_message_code('Во время выполнения,'
                                                 'произошла ошибка: cкорее '
                                                 'всего в запросе дублируются '
                                                 'courier_id (по ТЗ такого '
                                                 'быть не должно)')
        ans = generate_json({'couriers': [{'id': x} for x in success_ids]})
        return created_code(ans)
    else:
        ans = generate_json({
            'validation_error': {
                'couriers': [{
                    'id': x
                } for x in not_success_ids],
                'first_error_message': first_validation_message
            }
        })
        return bad_request_code(ans)
Ejemplo n.º 45
0
def import_orders():
    try:
        data = request.data
        d = parse_json(data)['data']
    except Exception:
        return bad_request_with_message_code(
            'По ТЗ такое невозможно, но не удалось спарсить json в теле '
            'запроса, возможно отстутствует поле data')

    # Это сообщение первой ошибки валидации
    first_validation_message = None

    success_ids = []
    not_success_ids = []
    new_orders = []
    for e in d:
        if e.get('order_id') is None:
            return bad_request_with_message_code(
                'По ТЗ такое невозможно, но у заказа отвутствует '
                'поле order_id')

        if len(e) != 4 or e.get('order_id') is None \
                or e.get('weight') is None \
                or e.get('region') is None \
                or e.get('delivery_hours') is None:
            not_success_ids.append(e['order_id'])
            if first_validation_message is None:
                first_validation_message = (f'У заказа  # {e["order_id"]} '
                                            'отсутствует обязательное поле '
                                            'или присутствует неописанное')
            continue

        # Если заказ уже содержится в бд, то бракуем его
        if services.is_orders_contains_id(e['order_id']):
            not_success_ids.append(e['order_id'])
            if first_validation_message is None:
                first_validation_message = (f'Заказ #{e["order_id"]} уже '
                                            'содержится в базе данных')

        try:
            entity = validator.validate_order(e)
            new_orders.append(entity)
            success_ids.append(entity.order_id)
        except Exception as exc:
            not_success_ids.append(e['order_id'])
            if first_validation_message is None:
                first_validation_message = ('При валидации заказа '
                                            f'#{e["order_id"]} произошла '
                                            'обшибка: ' + str(exc))
    # Отсортарую id и сделаю их уникальными
    success_ids = list(set(success_ids))
    success_ids.sort()
    not_success_ids = list(set(not_success_ids))
    not_success_ids.sort()

    if len(not_success_ids) == 0:
        try:
            services.add_orders(new_orders)
        except Exception:
            return bad_request_with_message_code(
                'Во время выполнения, произошла ошибка: '
                'cкорее всего в запросе дублируются '
                'order_id (по ТЗ такого быть не должно)')

        ans = generate_json({'orders': [{'id': x} for x in success_ids]})
        return created_code(ans)
    else:
        ans = generate_json({
            'validation_error': {
                'orders': [{
                    'id': x
                } for x in not_success_ids],
                'first_error_message': first_validation_message
            }
        })
        return bad_request_code(ans)