Example #1
0
    def update_ref_element(client, ref_table, inner_key, outer_key, value):
        """
        Update the value of an entry for the given ref_table

        :param client: An instantiated rest client for QRadar 
        :type client: AuthInfo
        :param ref_table: the name of the reference table
        :type ref_table: str
        :param inner_key: The inner key of the reference table, similar to the row number for an sql table
        :type inner_key: str
        :param outer_key: The outer key of a given reference table entry
        :type outer_key: str
        :param value: The value to set for a reference table entry
        :type value: str
        :raises RequestError: If the API request is not successful an error is raised
        :return: the result of the API call
        :rtype: dict
        """
        ref_table_link = quote(ref_table, '')
        value = quote(value, '')
        url = u"{}{}/{}?inner_key={}&outer_key={}&value={}".format(client.api_url, REF_TABLE_ENDPOINT,
                                         ref_table_link, inner_key, outer_key, value)
        ret = {}
        try:
            response = client.make_call("POST", url)

            ret = {"status_code": response.status_code,
                   "content": response.json()}

        except Exception as e:
            LOG.error(str(e))
            raise RequestError(
                url, "delete_ref_element failed with exception {}".format(str(e)))

        return ret
Example #2
0
def requests_3_sde(one_input):
    try:
        ipt_data = str(one_input)
    except UnicodeDecodeError:
        sys.exit(0)
    url = "http://{}:{}@test.com/test".format(quote(ipt_data), quote(ipt_data))
    usr, pwd = get_auth_from_url(url)
    assert usr == ipt_data and pwd == ipt_data
 def test_get_auth_from_url(self):
     """ Ensures that username and password in well-encoded URI as per RFC 3986 are correclty extracted """
     from requests.utils import get_auth_from_url
     from requests.compat import quote
     percent_encoding_test_chars = "%!*'();:@&=+$,/?#[] "
     url_address = "request.com/url.html#test"
     url = "http://" + quote(percent_encoding_test_chars, '') + ':' + quote(percent_encoding_test_chars, '') + '@' + url_address
     (username, password) = get_auth_from_url(url)
     assert username == percent_encoding_test_chars
     assert password == percent_encoding_test_chars
Example #4
0
 def test_get_auth_from_url(self):
     """ Ensures that username and password in well-encoded URI as per RFC 3986 are correclty extracted """
     from requests.utils import get_auth_from_url
     from requests.compat import quote
     percent_encoding_test_chars = "%!*'();:@&=+$,/?#[] "
     url_address = "request.com/url.html#test"
     url = "http://" + quote(percent_encoding_test_chars, '') + ':' + quote(percent_encoding_test_chars, '') + '@' + url_address
     (username, password) = get_auth_from_url(url)
     assert username == percent_encoding_test_chars
     assert password == percent_encoding_test_chars
Example #5
0
def graphite_fetch(target, start, until, count=None):
    """
    Simple get taget from locally hosted graphite
    """
    url = 'https://localhost/render/?target=%s&from=%s&until=%s&format=json' % (
        quote(target), quote(start), quote(until))
    response = requests.get(url, verify=False)
    result = response.json()[0]
    if count is not None:
        return {
            'target': result['target'],
            'datapoints': result['datapoints'][:count]
        }
    return result
Example #6
0
    def uri(self):
        """
        """
        uri = self.application_uri

        path_info = quote(self.path_info, safe='/=;,')
        if not self.env['SCRIPT_NAME']:
            uri += path_info[1:]
        else:
            uri += path_info

        if self.query_string:
            uri += '?' + quote(self.query_string, safe='&=;,')

        return uri
Example #7
0
def rebuild_censored_list():
    """Rebuild the censored list."""
    # set of censored items
    results = set()
    for value in itervalues(censored_items):
        if not value:
            continue
        if isinstance(value, collections.Iterable) and not isinstance(value, string_types):
            for item in value:
                if item and item != '0':
                    results.add(item)
        elif value and value != '0':
            results.add(value)

    # set of censored items and urlencoded counterparts
    results |= {quote(item) for item in results}
    # convert set items to unicode and typecast to list
    results = list({item.decode(default_encoding, 'replace')
                    if not isinstance(item, text_type) else item for item in results})
    # sort the list in order of descending length so that entire item is censored
    # e.g. password and password_1 both get censored instead of getting ********_1
    results.sort(key=len, reverse=True)

    # replace
    censored[:] = results
Example #8
0
    def format(self, record):
        """
        Strips censored items from string

        :param record: to censor
        """
        msg = super(CensoredFormatter, self).format(record)

        if not isinstance(msg, six.text_type):
            msg = msg.decode(self.encoding, 'replace')  # Convert to unicode

        # set of censored items
        censored = {item for _, item in six.iteritems(censored_items) if item}
        # set of censored items and urlencoded counterparts
        censored = censored | {quote(item) for item in censored}
        # convert set items to unicode and typecast to list
        censored = list({
            item.decode(self.encoding, 'replace')
            if not isinstance(item, six.text_type) else item
            for item in censored
        })
        # sort the list in order of descending length so that entire item is censored
        # e.g. password and password_1 both get censored instead of getting ********_1
        censored.sort(key=len, reverse=True)

        for item in censored:
            msg = msg.replace(item, len(item) * '*')

        # Needed because Newznab apikey isn't stored as key=value in a section.
        msg = re.sub(r'([&?]r|[&?]apikey|[&?]api_key)(?:=|%3D)[^&]*([&\w]?)',
                     r'\1=**********\2', msg, re.I)
        return msg
Example #9
0
 def send_request(self, endpoint,  querystring, retries=2):
     """Send single request to API and return response or error."""
     url = urljoin(
         self.API_url,
         quote(endpoint)
     )
     try:
         response = request(
             'GET', url=url, headers=self.API_headers, params=querystring
         )
         response.raise_for_status()
     except HTTPError as e:
         logger.exception(f"scraper request: HTTP ERROR, {e}")
         raise Exception(e)
     except ConnectionError as e:
         logger.exception(f"scrapper request: CONNECTION ERROR, {e}")
         raise Exception(e)
     except Timeout as e:
         logger.exception(f"scrapper request: TIMEOUT ERROR, {e}")
         if retries > 0:
             self.send_request(endpoint, querystring, retries=0)
         else:
             raise Exception(e)
     except RequestException as e:
         logger.exception(f"scrapper request: REQUEST EXCEPTION, {e}")
         raise Exception(e)
     else:
         return response.json()
Example #10
0
def percent_quote(query):
    """
    Percent quote
    :param str query: the query
    :return str : quote encode for query
    """
    return compat.quote(query.encode(ENCODING), PERCENT_SAFE)
Example #11
0
 def config(self):
     url = 'job/%s/config.xml' % quote(self.name)
     res = self.server.get(url)
     if res.status_code != 200 or res.headers.get('content-type', '') != 'application/xml':
         msg = 'fetching configuration for job "%s" did not return a xml document'
         raise JenkinsError(msg % self.name)
     return res.text
Example #12
0
    def series(self, metric, entity=None, tags=None, min_insert_date=None, max_insert_date=None):
        """Retrieve series for the specified metric.

        :param metric: `str` | :class:`.Metric`
        :param entity: `str` | :class:`.Entity`
        :param tags: `dict`
        :param min_insert_date: `int` | `str` | None | :class:`datetime`
        :param max_insert_date: `int` | `str` | None | :class:`datetime`

        :return: :class:`.Series`
        """
        metric_name = metric.name if isinstance(metric, Metric) else metric
        _check_name(metric_name)

        params = {}
        if entity is not None:
            params['entity'] = entity.name if isinstance(entity, Entity) else entity
        if tags is not None and isinstance(tags, dict):
            for k, v in tags.items():
                params['tags.%s' % k] = v
        if min_insert_date is not None:
            params['minInsertDate'] = to_iso(min_insert_date)
        if max_insert_date is not None:
            params['maxInsertDate'] = to_iso(max_insert_date)

        try:
            response = self.conn.get(metric_series_url.format(metric=quote(metric_name, '')),
                                     params)
        except ServerException as e:
            if e.status_code == 404:
                return []
            else:
                raise e
        return _jsonutil.deserialize(response, Series)
Example #13
0
    def request(self, method, endpoint, body='', **kwargs):
        """Make a request to the PyCon website, and return the result."""
        # The PyCon website runs using HTTPS, but localhost doesn't.
        # Determine the right thing.
        protocol = 'https'
        if 'localhost' in self.host:
            protocol = 'http'

        # Construct the base URI.
        uri = '/2014/pycon_api/%s/' % endpoint

        # If keyword arguments are provided, append them to
        # the URI.
        if kwargs:
            uri += '?' + '&'.join(
                ['%s=%s' % (k, quote(str(v))) for k, v in kwargs.items()],
            )

        # Construct the full URL.
        url = '{protocol}://{host}{uri}'.format(
            host=self.host,
            protocol=protocol,
            uri=uri,
        )

        # Generate the appropriate request signature to certify
        # that this is a valid request.
        signature = self._sign_request(uri, method, body)

        # Add the appropriate content-type header.
        if method == 'POST':
            signature['Content-Type'] = 'application/json'

        # Make the actual request to the PyCon website.
        r = requests.request(method, url, data=body, headers=signature,
                                          verify=False)

        # Sanity check: Did we get a bad request of some kind?
        if r.status_code >= 400:
            # If we got a 500 response, we can't really know what to do
            if r.status_code >= 500:
                raise InternalServerError(r)

            # What exception class shall I use?
            exc_class = APIError
            if r.status_code == 403:
                exc_class = AuthenticationError
            if r.status_code == 404:
                exc_class = NotFound

            # Create and raise the exception
            try:
                ex = exc_class(r.json()['error'])
            except ValueError:
                raise InternalServerError(r)
            ex.request = r
            raise ex

        # OK, all is well; return the response.
        return r.json()
    def exportm3u(self, hostport, path='', add_ts=False, empty_header=False, archive=False, process_url=True, header=None, fmt=None):
        '''
        Exports m3u playlist
        '''
        if add_ts: hostport = 'ts://' + hostport  # Adding ts:// after http:// for some players

        if header is None: itemlist = self.m3uheader if not empty_header else self.m3uemptyheader
        else: itemlist = header

        self._changeItems()
        items = sorted(self.itemlist, cmp=self.comparator) if self.comparator else self.itemlist

        for i in items:
            item = i.copy()
            item['name'] = item['name'].replace('"', "'").replace(',', '.')
            url = item['url']
            if process_url and url:
                if url.endswith(('.acelive', '.acestream', '.acemedia', '.torrent')): # For .acelive and .torrent
                   item['url'] = 'http://%s%s/url/%s/stream.mp4' % (hostport, path, quote(url,''))
                elif url.startswith('infohash://'): # For INFOHASHes
                   item['url'] = 'http://%s%s/infohash/%s/stream.mp4' % (hostport, path, url.split('/')[2])
                elif url.startswith('acestream://'): # For PIDs
                   item['url'] = 'http://%s%s/content_id/%s/stream.mp4' % (hostport, path, url.split('/')[2])
                elif archive and url.isdigit(): # For archive channel id's
                   item['url'] = 'http://%s%s/archive/play?id=%s' % (hostport, path, url)
                elif not archive and url.isdigit(): # For channel id's
                   item['url'] = 'http://%s%s/channels/play?id=%s' % (hostport, path, url)
                else: # For channel name
                   item['url'] = 'http://%s%s/%s' % (hostport, path, url)

            if fmt: item['url'] += '&fmt=%s' % fmt if '?' in item['url'] else '/?fmt=%s' % fmt
            itemlist += self._generatem3uline(item)

        return itemlist
Example #15
0
 def _build_url(self, params):
     query_parts = [
         "{}={}".format(name, quote(value))
         for name, value in params.items()
     ]
     query = "&".join(query_parts)
     return "{}?{}".format(self.base_url(), query)
Example #16
0
    def metrics(self, entity, expression=None, min_insert_date=None, max_insert_date=None, use_entity_insert_time=False,
                limit=None, tags=None):
        """Retrieve a `list` of metrics matching the specified filters.

        :param entity: `str` | :class:`.Entity`
        :param expression: `str`
        :param min_insert_date: `int` | `str` | None | :class:`datetime`
        :param max_insert_date: `int` | `str` | None | :class:`datetime`
        :param use_entity_insert_time: `bool` If true, last_insert_date is calculated for the specified entity and metric
        :param limit: `int`
        :param tags: `str`
        :return: :class:`.Metric` objects
        """
        entity_name = entity.name if isinstance(entity, Entity) else entity
        _check_name(entity_name)
        params = {}
        if expression is not None:
            params['expression'] = expression
        if min_insert_date is not None:
            params['minInsertDate'] = to_iso(min_insert_date)
        if max_insert_date is not None:
            params['maxInsertDate'] = to_iso(max_insert_date)
        params['useEntityInsertTime'] = use_entity_insert_time
        if limit is not None:
            params['limit'] = limit
        if tags is not None:
            params['tags'] = tags
        response = self.conn.get(ent_metrics_url.format(entity=quote(entity_name, '')), params)
        return _jsonutil.deserialize(response, Metric)
Example #17
0
    def get_entities(self,
                     group_name,
                     expression=None,
                     min_insert_date=None,
                     max_insert_date=None,
                     tags=None,
                     limit=None):
        """Retrieve a list of entities that are members of the specified entity group and match the specified expression filter.

        :param group_name: `str`
        :param expression: `str`
        :param min_insert_date: `str` | `int` | :class:`datetime`
        :param max_insert_date: `str` | `int` | :class:`datetime`
        :param tags: `dict`
        :param limit: `int`
        :return: `list` of :class:`.Entity` objects
        """
        _check_name(group_name)
        params = {}
        if expression is not None:
            params["expression"] = expression
        if min_insert_date is not None:
            params["minInsertDate"] = to_iso(min_insert_date)
        if max_insert_date is not None:
            params["maxInsertDate"] = to_iso(max_insert_date)
        if tags is not None:
            params["tags"] = tags
        if limit is not None:
            params["limit"] = limit
        resp = self.conn.get(
            eg_get_entities_url.format(group=quote(group_name, '')), params)
        return _jsonutil.deserialize(resp, Entity)
Example #18
0
    def get_entities(self, group_name, expression=None, min_insert_date=None, max_insert_date=None, tags=None,
                     limit=None):
        """Retrieve a list of entities that are members of the specified entity group and match the specified expression filter.

        :param group_name: `str`
        :param expression: `str`
        :param min_insert_date: `str` | `int` | :class:`datetime`
        :param max_insert_date: `str` | `int` | :class:`datetime`
        :param tags: `dict`
        :param limit: `int`
        :return: `list` of :class:`.Entity` objects
        """
        _check_name(group_name)
        params = {}
        if expression is not None:
            params["expression"] = expression
        if min_insert_date is not None:
            params["minInsertDate"] = to_iso(min_insert_date)
        if max_insert_date is not None:
            params["maxInsertDate"] = to_iso(max_insert_date)
        if tags is not None:
            params["tags"] = tags
        if limit is not None:
            params["limit"] = limit
        resp = self.conn.get(eg_get_entities_url.format(group=quote(group_name, '')), params)
        return _jsonutil.deserialize(resp, Entity)
Example #19
0
    def update(self, entity):
        """Update the specified entity.

        :param entity: :class:`.Entity`
        :return: True if success
        """
        self.conn.patch(ent_update_url.format(entity=quote(entity.name, '')), entity)
        return True
Example #20
0
def getTTL(version, filename):
    '''Get a version of the sparc metadata file, save it to `filename`'''
    url = BASE_URL + quote(version) + '/curation-export.ttl'
    log.info(url)
    with requests.get(url, stream=True) as r:
        r.raise_for_status()
        with open(filename, 'wb') as f:
            copyfileobj(r.raw, f)
Example #21
0
    def create_or_replace(self, entity):
        """Create an entity or update an existing entity.

        :param entity: :class:`.Entity`
        :return: True if success
        """
        self.conn.put(ent_create_or_replace_url.format(entity=quote(entity.name, '')), entity)
        return True
Example #22
0
    def format_endpoint(self, resource_id):
        """
        This method concatenates the resource's endpoint with a specified
        identifier.

        Example: endpoint/<pk>/
        """
        return urljoin(self.endpoint, quote(str(resource_id))) + TRAILING_SLASH
Example #23
0
    def update(self, metric):
        """Update the specified metric.

        :param metric: :class:`.Metric`
        :return: True if success
        """
        self.conn.patch(metric_update_url.format(metric=quote(metric.name, '')), metric)
        return True
Example #24
0
 def config(self):
     url = 'view/%s/config.xml' % quote(self.name)
     res = self.server.get(url)
     if res.status_code != 200 or res.headers.get('content-type',
                                                  '') != 'application/xml':
         msg = 'fetching configuration for view "%s" did not return an xml document'
         raise JenkinsError(msg % self.name)
     return res.text
Example #25
0
    def create_or_replace(self, metric):
        """Create a metric or replace an existing metric.

        :param metric: :class:`.Metric`
        :return: True if success
        """
        self.conn.put(metric_create_or_replace_url.format(metric=quote(metric.name, '')), metric)
        return True
Example #26
0
    def create_or_replace(self, group):
        """Create an entity group or replace an existing entity group.

        :param group: :class:`.EntityGroup`
        :return: True if successful
        """
        self.conn.put(eg_create_or_replace_url.format(group=quote(group.name, '')), group)
        return True
Example #27
0
    def delete(self, metric_name):
        """Delete the specified metric.

        :param metric_name: :class:`.Metric`
        :return: True if success
        """
        self.conn.delete(metric_delete_url.format(metric=quote(metric_name, '')))
        return True
Example #28
0
    def update(self, metric):
        """Update the specified metric.

        :param metric: :class:`.Metric`
        :return: True if success
        """
        self.conn.patch(
            metric_update_url.format(metric=quote(metric.name, '')), metric)
        return True
Example #29
0
    def delete(self, metric_name):
        """Delete the specified metric.

        :param metric_name: :class:`.Metric`
        :return: True if success
        """
        self.conn.delete(
            metric_delete_url.format(metric=quote(metric_name, '')))
        return True
Example #30
0
    def update(self, entity):
        """Update the specified entity.

        :param entity: :class:`.Entity`
        :return: True if success
        """
        self.conn.patch(ent_update_url.format(entity=quote(entity.name, '')),
                        entity)
        return True
Example #31
0
    def delete(self, entity):
        """Delete the specified entity.

        :param entity: :class:`.Entity` | `str` Entity name.
        :return: True if success
        """
        entity_name = entity.name if isinstance(entity, Entity) else entity
        self.conn.delete(ent_delete_url.format(entity=quote(entity_name, '')))
        return True
Example #32
0
    def type_query(self, entity):
        """Returns an array of property types for the entity.

        :param entity: :class:`.Entity`
        :return: returns `list` of property types for the entity.
        """
        entity_name = entity.name if isinstance(entity, Entity) else entity
        response = self.conn.get(properties_types_url.format(entity=quote(entity_name, '')))
        return response
    def _build_endpoint(self, api_path, params=None, query_params=None):
        """
		Helper function to form API URL. The base URL is
		'<protocol>://<hostname>[:<port>]/<api base url>' e.g. 'https://to.somedomain.net/api/0.1/'

		:param api_path: The path to the API end-point that you want to call which does not include the base URL e.g. ``user/login``, ``servers``, etc. This string can contain substitution parameters as denoted by a valid field_name replacement field specification as per :meth:`str.format` e.g. ``cachegroups/{id}`` or ``cachegroups/{id:d}``
		:type api_path: str
		:param params: If :meth:`str.format` field_name replacement field specifications exists in the ``api_path`` use this dictionary to perform replacements of the specifications with the value(s) in the dictionary that match the parameter name(s) e.g. ``{param_id}`` or ``{param_id:d}`` in ``api_string`` is replaced by value in ``params['param_id']``.
		:type params: Union[Dict[str, Any], None]
		:param query_params: URL query params to provide to the end-point e.g. ``{ 'sort': 'asc', 'maxresults': 200 }`` which translates to something like ``?sort=asc&maxresults=200`` which is appended to the request URL
		:type query_params: Union[Dict[str, Any], None]
		:return: The base url plus the passed and possibly substituted ``api_path`` to form a complete URL to the API resource to request
		:rtype: str
		:raises: ValueError
		"""

        new_api_path = api_path

        # Replace all parameters in the new_api_path path, if required
        try:
            # Make the parameters values safe for adding to URLs
            url_params = {k: compat.quote(str(v)) if isinstance(v, str)\
                                                  else v for k, v in iteritems(params)}

            log_with_debug_info(
                logging.DEBUG, u'URL parameters are: [{0}]'.format(url_params))

            qparams = u''
            if query_params:
                # Process the URL query parameters
                qparams = u'?{0}'.format(compat.urlencode(query_params))
                log_with_debug_info(
                    logging.DEBUG,
                    u'URL query parameters are: [{0}]'.format(qparams))

            new_api_path = api_path.format(**url_params) + qparams
        except KeyError as e:
            msg = (
                u'Expecting a value for keyword argument [{0}] for format field '
                u'specification [{1!r}]')
            msg = msg.format(e, api_path)
            log_with_debug_info(logging.ERROR, msg)
            raise ValueError(msg)
        except ValueError as e:
            msg = (
                u'One or more values do not match the format field specification '
                u'[{0!r}]; Supplied values: {1!r} ')
            msg = msg.format(api_path, params)
            log_with_debug_info(logging.ERROR, msg)
            raise ValueError(msg)

        retval = compat.urljoin(self.api_base_url, new_api_path)

        log_with_debug_info(logging.DEBUG,
                            u'Built end-point to return: {0}'.format(retval))

        return retval
Example #34
0
    def update(self, group):
        """Update the specified entity group.
        Unlike replace method, fields and tags not specified in the request remain unchanged.

        :param group: :class:`.EntityGroup`
        :return: True if success
        """
        self.conn.patch(eg_update_url.format(group=quote(group.name, '')), group)
        return True
Example #35
0
    def delete(self):
        '''Permanently remove job.'''
        self._not_exist_raise()
        url = 'job/%s/doDelete' % quote(self.name)

        res = self.server.post(url, throw=False)
        if self.exists:
            raise JenkinsError('delete of job "%s" failed' % self.name)
        return res
Example #36
0
    def delete(self):
        '''Permanently remove job.'''

        self._not_exist_raise()
        url = 'job/%s/doDelete' % quote(self.name)

        self.server.post(url)
        if self.exists():
            raise JenkinsError('delete of job "%s" failed' % self.name)
Example #37
0
    def delete(self, entity):
        """Delete the specified entity.

        :param entity: :class:`.Entity` | `str` Entity name.
        :return: True if success
        """
        entity_name = entity.name if isinstance(entity, Entity) else entity
        self.conn.delete(ent_delete_url.format(entity=quote(entity_name, '')))
        return True
Example #38
0
    def delete(self, group):
        """Delete the specified entity group.
        Member entities and their data are not affected by this operation.

        :param group: :class:`.EntityGroup` | `str` Entity Group name.
        :return: True if success
        """
        group_name = group.name if isinstance(group, EntityGroup) else group
        self.conn.delete(eg_delete_url.format(group=quote(group_name, '')))
        return True
Example #39
0
    def create_or_replace(self, metric):
        """Create a metric or replace an existing metric.

        :param metric: :class:`.Metric`
        :return: True if success
        """
        self.conn.put(
            metric_create_or_replace_url.format(metric=quote(metric.name, '')),
            metric)
        return True
Example #40
0
    def create_or_replace(self, entity):
        """Create an entity or update an existing entity.

        :param entity: :class:`.Entity`
        :return: True if success
        """
        self.conn.put(
            ent_create_or_replace_url.format(entity=quote(entity.name, '')),
            entity)
        return True
Example #41
0
    def __init__(self, content_type="application/json", sock_path=None):
        if sock_path is None:
            sock_path = self.secrets_sock_path

        self.content_type = content_type
        self.session = requests.Session()
        self.session.mount("http+unix://", HTTPUnixAdapter())
        self.headers = dict({"Content-Type": content_type})
        self.url = "http+unix://" + quote(sock_path, safe="") + "/" + self.secrets_container
        self._last_response = None
Example #42
0
    def update(self, group):
        """Update the specified entity group.
        Unlike replace method, fields and tags not specified in the request remain unchanged.

        :param group: :class:`.EntityGroup`
        :return: True if success
        """
        self.conn.patch(eg_update_url.format(group=quote(group.name, '')),
                        group)
        return True
Example #43
0
def quote_branch(arguments):
    """
    Quote is used here to escape the '/' in branch name. By
    default '/' is listed in 'safe' characters which aren't escaped.
    quote is not used in the data of the PUT request, as quoting for
    arguments is handled by the request library
    """
    new_args = arguments.copy()
    new_args['branch'] = quote(new_args['branch'], '')
    return new_args
Example #44
0
    def type_query(self, entity):
        """Returns an array of property types for the entity.

        :param entity: :class:`.Entity`
        :return: returns `list` of property types for the entity.
        """
        entity_name = entity.name if isinstance(entity, Entity) else entity
        response = self.conn.get(
            properties_types_url.format(entity=quote(entity_name, '')))
        return response
Example #45
0
    def create_or_replace(self, group):
        """Create an entity group or replace an existing entity group.

        :param group: :class:`.EntityGroup`
        :return: True if successful
        """
        self.conn.put(
            eg_create_or_replace_url.format(group=quote(group.name, '')),
            group)
        return True
Example #46
0
    def delete(self, group):
        """Delete the specified entity group.
        Member entities and their data are not affected by this operation.

        :param group: :class:`.EntityGroup` | `str` Entity Group name.
        :return: True if success
        """
        group_name = group.name if isinstance(group, EntityGroup) else group
        self.conn.delete(eg_delete_url.format(group=quote(group_name, '')))
        return True
Example #47
0
    def _sendFreeMobileSMS(self, title, msg, cust_id=None, apiKey=None):
        """
        Sends a SMS notification

        msg: The message to send (unicode)
        title: The title of the message
        userKey: The pushover user id to send the message to (or to subscribe with)

        returns: True if the message succeeded, False otherwise
        """

        if cust_id is None:
            cust_id = sickbeard.FREEMOBILE_ID
        if apiKey is None:
            apiKey = sickbeard.FREEMOBILE_APIKEY

        logger.log(u"Free Mobile in use with API KEY: " + apiKey, logger.DEBUG)

        # build up the URL and parameters
        msg = msg.strip()
        msg_quoted = quote(title.encode('utf-8') + ": " + msg.encode('utf-8'))
        URL = "https://smsapi.free-mobile.fr/sendmsg?user="******"&pass="******"&msg=" + msg_quoted

        req = Request(URL)
        # send the request to Free Mobile
        try:
            urlopen(req)
        except IOError as e:
            if hasattr(e, 'code'):
                if e.code == 400:
                    message = "Missing parameter(s)."
                    logger.log(message, logger.ERROR)
                    return False, message
                if e.code == 402:
                    message = "Too much SMS sent in a short time."
                    logger.log(message, logger.ERROR)
                    return False, message
                if e.code == 403:
                    message = "API service isn't enabled in your account or ID / API key is incorrect."
                    logger.log(message, logger.ERROR)
                    return False, message
                if e.code == 500:
                    message = "Server error. Please retry in few moment."
                    logger.log(message, logger.ERROR)
                    return False, message
        except Exception as e:
            message = u"Error while sending SMS: {0}".format(e)
            logger.log(message, logger.ERROR)
            return False, message

        message = "Free Mobile SMS successful."
        logger.log(message, logger.INFO)
        return True, message
Example #48
0
    def _sendFreeMobileSMS(self, title, msg, cust_id=None, apiKey=None):
        """
        Sends a SMS notification

        msg: The message to send (unicode)
        title: The title of the message
        userKey: The pushover user id to send the message to (or to subscribe with)

        returns: True if the message succeeded, False otherwise
        """

        if cust_id is None:
            cust_id = app.FREEMOBILE_ID
        if apiKey is None:
            apiKey = app.FREEMOBILE_APIKEY

        logger.log(u"Free Mobile in use with API KEY: " + apiKey, logger.DEBUG)

        # build up the URL and parameters
        msg = msg.strip()
        msg_quoted = quote(title.encode('utf-8') + ": " + msg.encode('utf-8'))
        URL = "https://smsapi.free-mobile.fr/sendmsg?user="******"&pass="******"&msg=" + msg_quoted

        req = Request(URL)
        # send the request to Free Mobile
        try:
            urlopen(req)
        except IOError as e:
            if hasattr(e, 'code'):
                if e.code == 400:
                    message = "Missing parameter(s)."
                    logger.log(message, logger.ERROR)
                    return False, message
                if e.code == 402:
                    message = "Too much SMS sent in a short time."
                    logger.log(message, logger.ERROR)
                    return False, message
                if e.code == 403:
                    message = "API service isn't enabled in your account or ID / API key is incorrect."
                    logger.log(message, logger.ERROR)
                    return False, message
                if e.code == 500:
                    message = "Server error. Please retry in few moment."
                    logger.log(message, logger.ERROR)
                    return False, message
        except Exception as e:
            message = u"Error while sending SMS: {0}".format(e)
            logger.log(message, logger.ERROR)
            return False, message

        message = "Free Mobile SMS successful."
        logger.log(message, logger.INFO)
        return True, message
Example #49
0
def submit_grade(session, attempt_id, is_group_assignment, grade, text,
                 filenames, rubrics):
    assert isinstance(session, BlackboardSession)
    if is_group_assignment:
        url = ('https://%s/webapps/assignment/' % DOMAIN +
               'gradeAssignmentRedirector' +
               '?course_id=%s' % session.course_id +
               '&groupAttemptId=%s' % attempt_id)
    else:
        url = ('https://%s/webapps/assignment/' % DOMAIN +
               'gradeAssignmentRedirector' +
               '?course_id=%s' % session.course_id +
               '&attempt_id=%s' % attempt_id)
    form = Form(session, url, './/h:form[@id="currentAttempt_form"]')

    form.set('grade', str(grade))
    form.set('feedbacktext', text)
    form.set('gradingNotestext',
             'Submitted with https://github.com/Mortal/bbfetch')

    if rubrics:
        rubric_input = '%s_rubricEvaluation' % attempt_id
        rubric_data_str = form.get(rubric_input)
        rubric_data = json.loads(unquote(rubric_data_str))
        for rubric_cells, rubric in zip(rubrics, rubric_data['rubrics']):
            rubric['client_changed'] = True
            for input_row, row in zip(rubric_cells, rubric['rows']):
                row['cell_id'] = input_row
        rubric_data_str = quote(json.dumps(rubric_data))
        form.set(rubric_input, rubric_data_str)

    for i, filename in enumerate(filenames):
        base = os.path.basename(filename)
        form.extend([
            ('feedbackFiles_attachmentType', 'L'),
            ('feedbackFiles_fileId', 'new'),
            ('feedbackFiles_artifactFileId', 'undefined'),
            ('feedbackFiles_artifactType', 'undefined'),
            ('feedbackFiles_artifactTypeResourceKey', 'undefined'),
            ('feedbackFiles_linkTitle', base),
        ])
        with open(filename, 'rb') as fp:
            fdata = fp.read()
        form.files.append(('feedbackFiles_LocalFile%d' % i, (base, fdata)))
    if is_group_assignment:
        post_url = (
            'https://%s/webapps/assignment//gradeGroupAssignment/submit' %
            DOMAIN)
    else:
        post_url = ('https://%s/webapps/assignment//gradeAssignment/submit' %
                    DOMAIN)
    response = form.submit(post_url)
    form.require_success_message(response)
Example #50
0
File: secrets.py Project: SSSD/sssd
    def __init__(self, content_type='application/json', sock_path=None):
        if sock_path is None:
            sock_path = self.secrets_sock_path

        self.content_type = content_type
        self.session = requests.Session()
        self.session.mount('http+unix://', HTTPUnixAdapter())
        self.headers = dict({'Content-Type': content_type})
        self.url = 'http+unix://' + \
            quote(sock_path, safe='') + \
            '/' + \
            self.secrets_container
        self._last_response = None
Example #51
0
    def get(self, entity_name):
        """Retrieve the entity

        :param entity_name: `str` entity name
        :return: :class:`.Entity`
        """
        _check_name(entity_name)
        try:
            response = self.conn.get(ent_get_url.format(entity=quote(entity_name, '')))
        except ServerException as e:
            if e.status_code == 404:
                return None
            else:
                raise e
        return _jsonutil.deserialize(response, Entity)
Example #52
0
    def get(self, group_name):
        """Retrieve the specified entity group.

        :param group_name: `str` entity group name
        :return: :class:`.EntityGroup`
        """
        _check_name(group_name)
        try:
            resp = self.conn.get(eg_get_url.format(group=quote(group_name, '')))
        except ServerException as e:
            if e.status_code == 404:
                return None
            else:
                raise e
        return _jsonutil.deserialize(resp, EntityGroup)
Example #53
0
    def get(self, name):
        """Retrieve metric.

        :param name: `str` metric name
        :return: :class:`.Metric`
        """
        _check_name(name)
        try:
            response = self.conn.get(metric_get_url.format(metric=quote(name, '')))
        except ServerException as e:
            if e.status_code == 404:
                return None
            else:
                raise e
        return _jsonutil.deserialize(response, Metric)
Example #54
0
    def format(self, record):
        """Strip censored items from string.

        :param record: to censor
        :type record: logging.LogRecord
        :return:
        :rtype: str
        """
        privacy_level = sickbeard.common.privacy_levels[sickbeard.PRIVACY_LEVEL]
        if not privacy_level:
            return super(CensoredFormatter, self).format(record)
        elif privacy_level == sickbeard.common.privacy_levels['absurd']:
            return re.sub(r'[\d\w]', '*', super(CensoredFormatter, self).format(record))
        else:
            msg = super(CensoredFormatter, self).format(record)

        if not isinstance(msg, text_type):
            msg = msg.decode(self.encoding, 'replace')  # Convert to unicode

        # Change the SSL error to a warning with a link to information about how to fix it.
        # Check for u'error [SSL: SSLV3_ALERT_HANDSHAKE_FAILURE] sslv3 alert handshake failure (_ssl.c:590)'
        for ssl_error in SSL_ERRORS:
            if re.findall(ssl_error, msg):
                record.levelno = WARNING
                record.levelname = logging.getLevelName(record.levelno)
                msg = super(CensoredFormatter, self).format(record)
                msg = re.sub(ssl_error, SSL_ERROR_HELP_MSG, msg)

        # set of censored items
        censored = {value for value in itervalues(censored_items) if value}
        # set of censored items and urlencoded counterparts
        censored = censored | {quote(item) for item in censored}
        # convert set items to unicode and typecast to list
        censored = list({
                            item.decode(self.encoding, 'replace')
                            if not isinstance(item, text_type) else item
                            for item in censored
                            })
        # sort the list in order of descending length so that entire item is censored
        # e.g. password and password_1 both get censored instead of getting ********_1
        censored.sort(key=len, reverse=True)

        for item in censored:
            msg = msg.replace(item, len(item) * '*')

        # Needed because Newznab apikey isn't stored as key=value in a section.
        msg = re.sub(r'([&?]r|[&?]apikey|[&?]api_key)(?:=|%3D)[^&]*([&\w]?)', r'\1=**********\2', msg, re.I)
        return msg
Example #55
0
    def add_entities(self, group_name, entities, create_entities=None):
        """Add entities as members to the specified entity group.
        Changing members of expression-based groups is not supported.

        :param group_name: `str`
        :param entities: `list` of :class:`.Entity` objects | `list` of `str` entity names
        :param create_entities: `bool` option indicating new entities from the submitted list are created if such
        entities do not exist :return: True if success
        """
        _check_name(group_name)
        data = []
        for e in entities:
            data.append(e.name if isinstance(e, Entity) else e)
        params = {"createEntities": True if create_entities is None else create_entities}
        response = self.conn.post(eg_add_entities_url.format(group=quote(group_name, '')), data,
                                  params=params)
        return True
Example #56
0
    def build_params(self, params):
        """
        quotes values and converts keys of params to camelCase from underscore
        :param params: dict
        :return:
        """
        out = {}
        for key, value in params.iteritems():
            if not isinstance(value, types.StringTypes):
                value = str(value)

            elif isinstance(value, unicode):
                value = value.encode("utf-8")

            key = key.split('_')[0] + ''.join(x.capitalize() for x in key.split('_')[1:])
            out[key] = quote(value)
        return out
Example #57
0
    def _sendFreeMobileSMS(self, title, msg, cust_id=None, apiKey=None):
        """
        Send a SMS notification

        msg: The message to send (unicode)
        title: The title of the message
        userKey: The pushover user id to send the message to (or to subscribe with)

        return: True if the message succeeded, False otherwise
        """
        if cust_id is None:
            cust_id = app.FREEMOBILE_ID
        if apiKey is None:
            apiKey = app.FREEMOBILE_APIKEY

        log.debug(u'Free Mobile in use with API KEY: {0}', apiKey)

        # build up the URL and parameters
        msg = msg.strip()
        msg_quoted = quote(title.encode('utf-8') + ': ' + msg.encode('utf-8'))
        URL = 'https://smsapi.free-mobile.fr/sendmsg?user='******'&pass='******'&msg=' + msg_quoted

        req = Request(URL)
        # send the request to Free Mobile
        try:
            urlopen(req)
        except IOError as e:
            if hasattr(e, 'code'):
                error_message = {
                    400: 'Missing parameter(s).',
                    402: 'Too much SMS sent in a short time.',
                    403: 'API service is not enabled in your account or ID / API key is incorrect.',
                    500: 'Server error. Please retry in few moment.',
                }
                message = error_message.get(e.code)
                if message:
                    log.error(message)
                    return False, message
        except Exception as e:
            message = u'Error while sending SMS: {0}'.format(e)
            log.error(message)
            return False, message

        message = 'Free Mobile SMS successful.'
        log.info(message)
        return True, message
Example #58
0
    def delete_entities(self, group_name, entities):
        """Remove specified entities from members of the specified entity group.
        To delete all entities, submit an empty list [] using the set_entities method.
        Changing members of expression-based groups is not supported.

        :param group_name: `str`
        :param entities: `list` of :class:`.Entity` objects | `list` of `str` entity names
        :return: True if success
        """
        _check_name(group_name)
        data = []
        for e in entities:
            data.append(e.name if isinstance(e, Entity) else e)

        response = self.conn.post(eg_delete_entities_url.format(group=quote(group_name, '')),
                                  data=data)
        return True
Example #59
0
    def set_entities(self, group_name, entities, create_entities=None):
        """Set members of the entity group from the specified entity list.
        All existing members that are not included in the request are removed from members.
        If the array in the request is empty, all entities are removed from the group and are replaced with an empty list.
        Changing members of expression-based groups is not supported.

        :param group_name: `str`
        :param entities: `list` of :class:`.Entity` objects | `list` of `str` entity names 
        :param create_entities: `bool` option indicating if new entities from the submitted list is created if such entities don't exist
        :return: True if success
        """
        _check_name(group_name)
        data = []
        for e in entities:
            data.append(e.name if isinstance(e, Entity) else e)
        params = {"createEntities": True if create_entities is None else create_entities}
        response = self.conn.post(eg_set_entities_url.format(group=quote(group_name, '')), data,
                                  params=params)
        return True