def actual_process(self, data):
        cols = data['columns']
        csv = BytesIO(data['csv'])  # The file is Bytes, encoded.
        encoding = self.guess_encoding(data['csv'])
        # TODO: Delivery?

        try:
            dialect = Sniffer().sniff(csv.readline(), [',', '\t'])
        except err:
            dialect = excel
        csv.seek(0)
        reader = UnicodeDictReader(csv, cols, encoding=encoding, dialect=dialect)
        profiles = []
        retval = None
        try:
            next(reader)  # Skip the first row (the header)
        except UnicodeDecodeError as e:
            t = guess_content_type(body=data['csv'])[0]
            msg = 'The file is different from what is required. (It '\
                  'appears to be a {0} file.) Please check that  you '\
                  'selected the correct CSV file.'
            m = {'status': -2,
                 'message': [msg.format(t.split('/')[0]), str(e), t]}
            retval = to_json(m)
        except StopIteration:
            msg = 'The file appears to be empty. Please check that you '\
                  'generated the CSV file correctly.'
            m = {'status': -5, 'message': [msg, 'no-rows']}
            retval = to_json(m)
        else:
            rowCount = 0
            for row in reader:
                rowCount += 1
                if len(row) != len(cols):
                    # *Technically* the number of columns in CSV rows can be
                    # arbitary. However, I am enforcing a strict
                    # interpretation for sanity's sake.
                    msg = 'Row {0} had {1} columns, rather than {2}. ' \
                          'Please check the file.'
                    # Name hack.
                    m = {'status': -3,
                         'message': [msg.format(rowCount, len(row),
                                     len(cols))]}
                    retval = to_json(m)
                    profiles = []
                    # --=mpj17=-- I think this is the first time I have used
                    # break in actual code. Wow.
                    break
                profiles.append(row)
        if profiles and (not retval):
            retval = to_json(profiles)
        elif (not profiles) and not(retval):
            msg = 'No rows were found in the CSV file. '\
                  'Please check that  you selected the correct CSV file.'
            m = {'status': -4,
                 'message': [msg, 'no-rows']}
            retval = to_json(m)
        assert retval, 'No retval'
        return retval
Esempio n. 2
0
    def persist_pact(self, pact, interactions):
        file_name = '{}-{}.json'.format(self.consumer.name, self.provider.name).lower().replace(' ', '_')
        pact['interactions'] = interactions
        pact['metadata'] = metadata

        self.path = join_path(self.path, file_name)

        with open(self.path, 'w') as outfile:
            to_json(pact, outfile, sort_keys=True, indent=4, separators=(',', ':'))
Esempio n. 3
0
 def delete_handler(id):
     fp = "./static/tmp/%s.%s" % (id, config["image_type"])
     if not os.path.exists(fp):
         return self._response(to_json({"message": "File not found!"}),
                               "application/json", 404)
     os.remove(fp)
     return self._response(
         to_json({"message": "Delete file successful!"}),
         "application/json")
def main():
    if len(sys.argv) != 2:
        print 'Run as `python {0} <course-id>`'.format(sys.argv[0])
        sys.exit(1)
    course_id = sys.argv[1]
    course_info = get_course_info(course_id)
    if course_info is None:
        print 'Could not get information for course with ID ' + course_id
    else:
        print to_json(course_info, indent=4)
Esempio n. 5
0
    def _push_config(self):
        # TODO: push notification should be connected to angularjs and use a
        # broadcast event any suitable controllers will be able to listen and
        # respond accordingly, for now we just use jQuery to manually toggle

        self.execute_script("$('#arch').html('{}')".format(self._config._arch))
        self.execute_script("$('#autostart').toggleClass('icon-check', {})\
            .toggleClass('icon-check-empty', {})".format(
            to_json(self._config._autostart), to_json(
                not self._config._autostart)))
Esempio n. 6
0
    def test_flavor_get_single_with_swap_set_to_empty(self, nc):
        request = self.mock_rest_request()
        nc.flavor_get.return_value\
            .to_dict.return_value = {'name': '1', 'swap': ''}

        response = nova.Flavor().get(request, "1")

        self.assertStatusCode(response, 200)
        self.assertEqual(to_json(response.content.decode('utf-8')),
                         to_json('{"name": "1", "swap": 0}'))
Esempio n. 7
0
    def test_flavor_get_single_with_swap_set_to_empty(self, nc):
        request = self.mock_rest_request()
        nc.flavor_get.return_value\
            .to_dict.return_value = {'name': '1', 'swap': ''}

        response = nova.Flavor().get(request, "1")

        self.assertStatusCode(response, 200)
        self.assertEqual(to_json(response.content.decode('utf-8')),
                         to_json('{"name": "1", "swap": 0}'))
Esempio n. 8
0
 def actual_process(self, data):
     # TODO: Delivery?
     cols = data['columns']
     csv = BytesIO(data['csv'])  # The file is Bytes, encoded.
     reader = UnicodeDictReader(csv, cols)
     profiles = []
     retval = None
     try:
         next(reader)  # Skip the first row (the header)
     except UnicodeDecodeError as e:
         t = guess_content_type(body=data['csv'])[0]
         msg = 'The file is different from what is required. (It '\
               'appears to be a {0} file.) Please check that  you '\
               'selected the correct CSV file.'
         m = {
             'status': -2,
             'message': [msg.format(t.split('/')[0]),
                         str(e), t]
         }
         retval = to_json(m)
     except StopIteration:
         msg = 'The file appears to be empty. Please check that you '\
               'generated the CSV file correctly.'
         m = {'status': -5, 'message': [msg, 'no-rows']}
         retval = to_json(m)
     else:
         rowCount = 0
         for row in reader:
             rowCount += 1
             if len(row) != len(cols):
                 # *Technically* the number of columns in CSV rows can be
                 # arbitary. However, I am enforcing a strict
                 # interpretation for sanity's sake.
                 msg = 'Row {0} had {1} columns, rather than {2}. ' \
                       'Please check the file.'
                 # Name hack.
                 m = {
                     'status': -3,
                     'message': [msg.format(rowCount, len(row), len(cols))]
                 }
                 retval = to_json(m)
                 profiles = []
                 # --=mpj17=-- I think this is the first time I have used
                 # break in actual code. Wow.
                 break
             profiles.append(row)
     if profiles and (not retval):
         retval = to_json(profiles)
     elif (not profiles) and not (retval):
         msg = 'No rows were found in the CSV file. '\
               'Please check that  you selected the correct CSV file.'
         m = {'status': -4, 'message': [msg, 'no-rows']}
         retval = to_json(m)
     assert retval, 'No retval'
     return retval
Esempio n. 9
0
def parse_stdin():
    """Parse protocol data passed on stdin, previously captured

    example to print all captured sensor id:s
    cat /tmp/packets.txt  | ./script/parse | jq ".sensorId" | sort | uniq
    """
    for line in stdin.readlines():
        line = line.strip()
        if " " in line:
            # assume we have date + raw data
            timestamp, line = line.split()
            timestamp = int(parse_isoformat(timestamp).timestamp())
            print(to_json(decode_packet(line, lastUpdated=timestamp)))
        else:
            print(to_json(decode_packet(line)))
Esempio n. 10
0
 def json(self):
     """Return JSON representation."""
     return to_json(
         OrderedDict(sorted(self.attrs.items())),
         indent=4,
         default=json_serialize,
     )
Esempio n. 11
0
    def request(self, method, url, params=None, data=None, headers=None, cookies=None, files=None, auth=None,
                timeout=None, allow_redirects=True, proxies=None, hooks=None, stream=None, verify=None, cert=None,
                json=None, **kwargs):
        site_id = kwargs.pop('site_id', None)
        force_json = kwargs.pop('force_json', True)

        if isinstance(url, (tuple, list)):
            url = get_service_url(url[0], *url[1:], **kwargs)
        if data:
            if isinstance(data, unicode):
                data = data.encode('utf-8')
            elif force_json and not isinstance(data, six.string_types):
                data = to_json(data, cls=BackendAPIJSONEncoder)

        if site_id:
            if headers is None:
                headers = {}
            headers['X-Site-ID'] = str(site_id)

        self._log('request', method.upper(), url)
        self._debug(data)

        response = super(BackendAPISession, self).request(method, url, params, data, headers, cookies,
                                                          files, auth, timeout, allow_redirects, proxies,
                                                          hooks, stream, verify, cert, json)

        self._log('response', response.status_code, response.url)
        self._debug(response.text)

        return response
Esempio n. 12
0
    def handle_get_people(self, action, data):
        '''The form action for the *Get members* page.

:param action: The button that was clicked.
:param dict data: The form data.'''
        retval = to_json(self.profileIds)
        return retval
Esempio n. 13
0
    def __init__(self,
                 databaseid=str,
                 region=str,
                 username=str,
                 password=str,
                 keyspace=str):
        """
        constructor also generates the needed authtoken to communicate with astra

        :param databaseid: what is the database named ?
        :param region: where is the database located ?
        :param username: the username you want to log in with ?
        :param password: the passowrd coresponding to the user ?
        :param keyspace: what is the keyspace named you want to work with ?
        """
        self.username = username
        self.password = password
        self.databaseid = databaseid
        self.region = region
        self.keyspace = keyspace

        self.url = "https://{uuid}-{region}.apps.astra.datastax.com/api/rest/v1".format(
            uuid=databaseid, region=region)

        payload = {'username': self.username, 'password': self.password}
        x = post(self.url + "/auth", data=to_json(payload))

        self.auth_token = x.json()["authToken"]
Esempio n. 14
0
def add_route(path, ip, port):
    header = {"Authorization": "token %s" % (ctoken)}
    data = to_json({"target": "http://%s:%d/narrative" % (ip, port)})
    print(data)
    url = "http://chp:8001/api/routes/%s" % (path)
    print(url)
    resp = requests.post(url, headers=header, data=data)
Esempio n. 15
0
  def add_deployment_plan(self, workflow_id, task_id, platform, experiment_id, data):
    """Adds new deployment plan information.

      Registers a new deployment plan on the monitoring database. Deployment
      plans are linked to the current workflow ID, task ID, platform, and
      experiment ID.

      Args:
        workflow_id: A unique ID representing the workflow to be registered (e.g., 'ms2_v2')
        task_id: A unique ID representing a specific task within the workflow (e.g., 'task_1')
        platform: Target platform where the task will be executed (e.g., 'hpc_cluster')
        experiment_id: A unique ID as returned by the function 'new_experiment'
        data: JSON object to be stored at the monitoring database (the actual deployment plan)
      Returns:
        a deployment plan ID
      Raises:
        Exception if an error occurred while contacting the database
    """
    debug('add new deployment plan ...')
    workflow_id = workflow_id.lower()
    task_id = task_id.lower()
    platform = platform.lower()

    deployment_url = self.urljoin(self.url,
            self.deployments, workflow_id, task_id, platform, experiment_id)

    print deployment_url

    request = put(deployment_url, data=to_json(data), headers=self.headers)
    if request.status_code == 200:
      info('[ deployment: ' + request.json()['href'] + ' ]')
    else:
      raise Exception(request.json())
    def handle_delete(self, action, data):
        '''Delete an email address

:param action: The button that was clicked.
:param dict data: The form data.'''
        e = data['email'].lower()
        if (e not in self.emailUser):
            m = '{0} ({1}) lacks the address <{2}>'
            msg = m.format(self.userInfo.name, self.userInfo.id, data['email'])
            raise AddressMissing(msg)
        elif ((len(self.emailUser.preferred) == 1)
              and (e in self.emailUser.preferred)):
            msg = 'Not deleting the only preferred address <{0}>'.format(
                data['email'])
            raise RemovingOnlyPreferred(msg)

        msg = self.delete(data['email'])

        r = {
            'status': 0,
            'message': msg,
            'email': email_info(self.siteInfo, self.userInfo),
        }
        retval = to_json(r)
        return retval
Esempio n. 17
0
    def handle_get_groups(self, action, data):
        '''The form action for the *Get groups* page.

:param action: The button that was clicked.
:param dict data: The form data.'''
        retval = to_json(self.groups)
        return retval
Esempio n. 18
0
def SaveToJson(df, destination):
    for x in df["Year"].unique():
        json = df[df["Year"] == x]
        json = json.to_json(orient="records")
        with open(f"{destination}\Real Estate Sales Data - {x}.json",
                  "w") as f:
            f.write(json)
Esempio n. 19
0
    def _createHelpMenu(self, selection):
        '''
        NOT CACHED
        :param selection:
        :return:
        '''
        helpButtons = [('Tribes', 'tribeUI.help.tribes'), ('Door System', 'tribeUI.help.doors'), ('Offline Protection', 'tribeUI.help.op'),('Building takeover', 'tribeUI.help.take'),('Server Info', 'tribeUI.help.server'), ('Commands', 'tribeUI.help.commands')]
        gui = []

        anchormin_x = 0.05
        anchormin_y = 0.95
        anchormax_x = 0.19
        anchormax_y = 0.99

        gui.append(self.componentUIImage("helpView", parent="TribeBgUI", color="0.2 0.1 0.1 0.25", anchormin="0.000 0.08", anchormax="0.999 0.88"))

        for i, button in enumerate(helpButtons):
            if button[1] == selection:
                color = "1.0 0.3 0.3 0.95"
            else:
                color = "1.0 0.9 0.9 0.95"
            anchormin = str(anchormin_x)+' '+str(anchormin_y)
            anchormax = str(anchormax_x)+' '+str(anchormax_y)
            gui.append(self.componentUIText(text=button[0], parent="helpView", color=color, align="MiddleCenter", fontSize="13", anchormin=anchormin, anchormax=anchormax))
            gui.append(self.componentUIButton(command=button[1], parent="helpView", color="0.6 0.6 0.6 0.25", anchormin=anchormin, anchormax=anchormax))
            anchormin_x += 0.15
            anchormax_x += 0.15


        helpView = json.to_json(gui)
        objectList = json.makepretty(helpView)

        return objectList
    def handle_get_groups(self, action, data):
        '''The form action for the *Leave group* page.

:param action: The button that was clicked.
:param dict data: The form data.'''
        r = OrderedDict()
        groupInfo = createObject('groupserver.GroupInfo', self.context, data['groupId'])
        userInfo = createObject('groupserver.UserFromId', self.context, data['userId'])
        if groupInfo.groupObj is None:
            r['status'] = self.NO_GROUP
            r['message'] = 'No such group "{0}"'.format(data['groupId'])
        elif userInfo.anonymous:
            r['status'] = self.NO_USER
            r['message'] = 'No such user "{0}"'.format(data['userId'])
        elif user_member_of_group(userInfo, groupInfo):
            leave_group(groupInfo, userInfo, self.request)
            r['status'] = self.SUCCESS
            r['message'] = '{0} has left {1}'.format(userInfo.name, groupInfo.name)
        else:
            r['status'] = self.NOT_MEMBER
            r['message'] = '{0} is not a member {1}'.format(userInfo.name, groupInfo.name)
        r['groupId'] = data['groupId']
        r['userId'] = data['userId']
        retval = to_json(r)
        return retval
Esempio n. 21
0
def get_json(rv):
    """
    Retrieve the JSON data from the return values.
    :param rv: the return value.
    :return: (dict) the JSON data.
    """
    return to_json(rv.get_data(as_text=True))
Esempio n. 22
0
def type_by_id(request, pk):
    try:
        requested_type = Type.objects.get(pk=pk)
    except Type.DoesNotExist:
        return HttpResponse('not found')
    json = to_json(requested_type.to_json())
    return HttpResponse(json)
    def handle_prefer(self, action, data):
        '''Prefer an email address

:param action: The button that was clicked.
:param dict data: The form data.'''
        e = data['email'].lower()
        if (e not in self.emailUser):
            m = '{0} ({1}) lacks the address <{2}>'
            msg = m.format(self.userInfo.name, self.userInfo.id, data['email'])
            raise AddressMissing(msg)
        elif (e in self.emailUser.preferred):
            msg = 'The address <{0}> is already preferred'.format(
                data['email'])
            raise AddressPreferred(msg)
        elif (e in self.emailUser.unverified):
            msg = 'The address <{0}> is unverified'.format(data['email'])
            raise AddressUnverified(msg)

        msg = self.prefer(data['email'])

        r = {
            'status': 0,
            'message': msg,
            'email': email_info(self.siteInfo, self.userInfo),
        }
        retval = to_json(r)
        return retval
    def handle_prefer(self, action, data):
        '''Prefer an email address

:param action: The button that was clicked.
:param dict data: The form data.'''
        e = data['email'].lower()
        if (e not in self.emailUser):
            m = '{0} ({1}) lacks the address <{2}>'
            msg = m.format(self.userInfo.name, self.userInfo.id, data['email'])
            raise AddressMissing(msg)
        elif (e in self.emailUser.preferred):
            msg = 'The address <{0}> is already preferred'.format(data['email'])
            raise AddressPreferred(msg)
        elif (e in self.emailUser.unverified):
            msg = 'The address <{0}> is unverified'.format(data['email'])
            raise AddressUnverified(msg)

        msg = self.prefer(data['email'])

        r = {
            'status': 0,
            'message': msg,
            'email': email_info(self.siteInfo, self.userInfo), }
        retval = to_json(r)
        return retval
Esempio n. 25
0
    def _createTribesView(self, tribeData):
        gui = []

        gui.append(self.componentUIImage('tribesView', parent="TribeBgUI", color="0.1 0.1 0.1 0.90", anchormin="0.000 0.08", anchormax="0.999 0.88"))

        anchormin_x = 0.002
        anchormin_y = 0.9
        anchormax_x = 0.15
        anchormax_y = 0.99

        for i, tribeName in enumerate(self.tribeNames):
            if i!=0  and i%9 == 0:
                anchormin_x += 0.15
                anchormin_y = 0.9
                anchormax_x += 0.15
                anchormax_y = 0.99
            anchormin = str(anchormin_x) + ' ' +str(anchormin_y)
            anchormax = str(anchormax_x) + ' '+ str(anchormax_y)
            name = tribeName[0]+' ('+str(tribeName[1])+' members)'
            gui.append(self.componentUIText(text=name, parent="tribesView", color="1.0 0.9 0.9 0.95", align="MiddleCenter", fontSize="13", anchormin=anchormin, anchormax=anchormax))
            gui.append(self.componentUIButton(command="tribe.members "+tribeName[0], parent="tribesView", color="0.6 0.6 0.6 0.55", anchormin=anchormin, anchormax=anchormax))
            anchormin_y -= 0.1
            anchormax_y -= 0.1

        playerListUI = json.to_json(gui)
        objectList = json.makepretty(playerListUI)

        return objectList
Esempio n. 26
0
  def update(self, workflow_id, experiment_id, data, task_id=None):
    """Sends metric data to the monitoring database.

      Sends the given metric data (JSON object) to the monitoring database. In
      order to associate the data with the corresponding workflow, the workflow
      ID, experiment ID, and the task ID (only required if the application is
      workflow-based) have to be specified as well.

      Args:
        workflow_id: A unique ID representing the workflow to be registered (e.g., 'ms2_v2')
        experiment_id: A unique ID as returned by the function 'new_experiment'
        data: JSON object to be stored at the monitoring database
        task_id: A unique ID representing a specific task within the workflow (e.g., 'task_1')
      Raises:
        Exception if an error occurred while contacting the database
    """
    debug('send metric data ...')
    workflow_id = workflow_id.lower()
    metrics_url = self.urljoin(self.url, self.metrics, workflow_id, experiment_id)
    if task_id is None:
      task_id = "_all";
    if task_id is not None:
      task_id = task_id.lower()
      metrics_url = metrics_url + '?task=' + task_id

    self.extend_data(data, task_id)

    request = post(metrics_url, data=to_json(data), headers=self.headers)
    if request.status_code == 200:
      metric_id = request.json().iterkeys().next()
      info('[ profile: ' + request.json()[metric_id]['href'] + ' ]')
    else:
      raise Exception(request.json())
Esempio n. 27
0
    def handle_get_list(self, action, data):
        '''The form action for the *simple* list

:param action: The button that was clicked.
:param dict data: The form data.'''
        retval = to_json(self.siteMembers.memberIds)
        return retval
Esempio n. 28
0
    def _createPlayersView(self, selection):
        '''
        NOT CACHED
        :param selection:
        :return:
        '''
        playersViewButtons = [('Online', 'tribeUI.players.online'), ('Offline', 'tribeUI.players.offline')]
        gui = []

        anchormin_x = 0.105
        anchormin_y = 0.95
        anchormax_x = 0.19
        anchormax_y = 0.99

        gui.append(self.componentUIImage("playersView", parent="TribeBgUI", color="0.2 0.1 0.1 0.25", anchormin="0.000 0.08", anchormax="0.999 0.88"))

        for i, button in enumerate(playersViewButtons):
            if button[0] == selection:
                color = "1.0 0.3 0.3 0.95"
            else:
                color = "1.0 0.9 0.9 0.95"
            anchormin = str(anchormin_x)+' '+str(anchormin_y)
            anchormax = str(anchormax_x)+' '+str(anchormax_y)
            gui.append(self.componentUIText(text=button[0], parent="playersView", color=color, align="MiddleCenter", fontSize="16", anchormin=anchormin, anchormax=anchormax))
            gui.append(self.componentUIButton(command=button[1], parent="playersView", color="0.6 0.6 0.6 0.35", anchormin=anchormin, anchormax=anchormax))
            anchormin_x += 0.1
            anchormax_x += 0.1


        playersView = json.to_json(gui)
        objectList = json.makepretty(playersView)

        return objectList
Esempio n. 29
0
    def handle_get_people(self, action, data):
        '''The form action for the *Get members* page.

:param action: The button that was clicked.
:param dict data: The form data.'''
        retval = to_json(self.profileIds)
        return retval
Esempio n. 30
0
 def _format_data(self, data, url, parameters):
     return to_json(
         {
             "view": url,
             "content": data
         }
     )
Esempio n. 31
0
    def _playerListObject(self, playerList, selection):
        '''
        :param playerList: list
        :return: objectList for UI generation
        '''


        gui = []

        gui.append(self.componentUIImage("playerList", parent="playersView", color="0.1 0.1 0.1 0.90", anchormin="0.001 0.0", anchormax="0.999 0.93"))

        anchormin_x = 0.002
        anchormin_y = 0.955
        anchormax_x = 0.097
        anchormax_y = 0.995



        for i, pl in enumerate(playerList):
            if i!=0  and i%20 == 0:
                anchormin_x += 0.1
                anchormin_y = 0.955
                anchormax_x += 0.1
                anchormax_y = 0.995
            anchormin = str(anchormin_x) + ' ' +str(anchormin_y)
            anchormax = str(anchormax_x) + ' '+ str(anchormax_y)
            gui.append(self.componentUIText(text=pl[1], parent="playerList", color="1.0 0.9 0.9 0.95", align="MiddleCenter", fontSize="11", anchormin=anchormin, anchormax=anchormax))
            gui.append(self.componentUIButton(command='tribe.player '+str(pl[0]), parent="playerList", color="0.6 0.6 0.6 0.55", anchormin=anchormin, anchormax=anchormax))
            anchormin_y -= 0.05
            anchormax_y -= 0.05

        playerListUI = json.to_json(gui)
        objectList = json.makepretty(playerListUI)

        return objectList
Esempio n. 32
0
def work():
    global wait_for

    try:
        ids, messages = fetch_new_emails()
    except OSError:
        log("Failed to connect with e-mail server to parse messages.")
        return
    except NoMessages:
        debug("No e-mails to parse.")
        return
    except:
        log("Unexpected Error.")
        return

    data = parse_messages(messages)

    request_data = to_json({"token": AUTH_TOKEN,
                            "data": data})
    debug("JSON: {}".format(request_data))

    try:
        debug("Connecting to Server to register parsed events.")
        response = post_request(API_ENDPOINT,
                                headers={'Content-Type': 'application/json'},
                                data=request_data)
        debug("Events registered.")

        # Server returns wait_for until next run (in minutes)
        received_wait_for = int(response.text)

        if received_wait_for == -1:
            log("Invalid token.")
            if not DEBUG:
                mark_as_unread(ids)
            return
        elif received_wait_for == -2:
            log("Database error.")
            if not DEBUG:
                mark_as_unread(ids)
            return

        debug("Received {} (minutes) from the server, "
              "to wait until next execution.".format(
              received_wait_for))

        if 0 < received_wait_for <= MAX_WAITING_PERIOD:
            wait_for = received_wait_for
        else:
            debug("Ignoring {} as it's not between 1 and {}".format(
                  received_wait_for,
                  MAX_WAITING_PERIOD))
    except RequestException:
        log("Failed to connect to Server")
        if not DEBUG:
            mark_as_unread(ids)
    except ValueError:
        log("Received {} from the Server, failed to convert to int "
            "to wait for (in minutes)".format(response.text))
Esempio n. 33
0
    def actual_add(self, data):
        retval = {}

        adder = Adder(self.context, self.groupInfo, self.loggedInUser)
        toAddr = sanitise_address(data['toAddr'])
        linked_groupname = groupInfo_to_anchor(self.groupInfo)

        msg, userInfo, status = adder.add(toAddr, data)
        linked_username = userInfo_to_anchor(userInfo)

        # Tell the user
        if status == ADD_NEW_USER:
            notifier = NotifyAdd(self.context, self.request)
            fromAddr = sanitise_address(data['fromAddr'])
            passwd = self.get_password_reset(userInfo, toAddr)
            notifier.notify(self.loggedInUser, userInfo, fromAddr, toAddr,
                            passwd)

            retval['status'] = 1
            m = []
            m.append('A profile for {0} has been created, and given the '
                     'email address <code>{1}</code>.')
            m.append('{0} has been added to {2}.')
            m = [i.format(linked_username, toAddr, linked_groupname)
                 for i in m]
            retval['message'] = m

        elif status == ADD_OLD_USER:
            notifier = NotifyJoin(self.context, self.request)
            notifier.notify(userInfo)

            retval['status'] = 2
            m = []
            m.append('Added the existing person with the email address '
                     '<code>{0}</code> ({1}) to {2}.')
            m = [i.format(toAddr, linked_username, linked_groupname)
                 for i in m]
            retval['message'] = m
        elif status == ADD_EXISTING_MEMBER:
            retval['status'] = 3
            m = []
            m.append('The person with the email address <code>{0}</code> '
                     '({1}) is already a member of {2}.')
            m.append('No changes to the profile of {1} have been made.')
            m = [i.format(toAddr, linked_username, linked_groupname)
                 for i in m]
            retval['message'] = m
        else:
            retval['status'] = 100
            retval['message'] = 'An unknown event occurred.'

        # Tell the administrator
        if status in (ADD_NEW_USER, ADD_OLD_USER):
            adminNotifier = NotifyAdmin(self.context, self.request)
            for adminInfo in self.groupInfo.group_admins:
                adminNotifier.notify(adminInfo, userInfo)

        retval = to_json(retval, indent=4)
        return retval
Esempio n. 34
0
    def handle_send(self, action, data):
        '''The form action for the *Send notification* page.

:param action: The button that was clicked.
:param dict data: The form data.'''
        userInfo = createObject('groupserver.UserFromId', self.context,
                                data['profileId'])
        statusUser = StatusUser(self.context, userInfo)
        if statusUser.anonymous:
            self.auditor.info(SKIPPED_STATUS_ANON,
                              instanceDatum=data['profileId'])
            m = 'Cannot find the user object for the user ID ({0})'
            msg = m.format(data['profileId'])
            r = {'status': Status.no_user.value, 'message': msg}
        elif statusUser.hasSkip:
            # --=mpj17=-- This should never happen, but I have it here just in case
            self.auditor.info(SKIPPED_STATUS_EXPLICIT, statusUser)
            m = 'Skipping the monthly profile-status notification for '\
                '{0} ({1}): explicitly opted out'
            msg = m.format(statusUser.name, statusUser.id)
            r = {'status': Status.skip.value, 'message': msg}
        elif statusUser.inGroups:
            if statusUser.addresses:
                if statusUser.hasActivity:
                    # --=mpj17=-- To summarise, if we are here then the person is in some groups
                    # (not just sites) *and* they have at least one working email address *and*
                    # there has been some activity in at least one group group this last month.
                    notifier = StatusNotifier(statusUser.user, self.request)
                    notifier.notify()
                    self.auditor.info(SENT_STATUS, statusUser,
                                      repr(statusUser.addresses))
                    m = 'Sent the monthly profile-status notification to {0} '\
                        '({1})'
                    msg = m.format(statusUser.name, statusUser.id)
                    r = {'status': Status.ok.value, 'message': msg}
                else:  # no activity
                    self.auditor.info(SKIPPED_STATUS_INACTIVE, statusUser)
                    m = 'Skipping the monthly profile-status notification for '\
                        '{0} ({1}): no activity in any groups this month'
                    msg = m.format(statusUser.name, statusUser.id)
                    r = {'status': Status.no_activity.value, 'message': msg}
            else:  # No email addresses
                self.auditor.info(SKIPPED_STATUS_EMAIL, statusUser)
                m = 'Skipping the monthly profile-status notification for '\
                    '{0} ({1}): no verified email addresses'
                msg = m.format(statusUser.name, statusUser.id)
                r = {'status': Status.no_email.value, 'message': msg}
            assert type(r) == dict
        else:  # No groups
            # --=mpj17=-- The groups is calculated first, even though it is very expensive in
            # terms of ZODB access  I do this to get a nice list of people that we may want to
            # drop from the ZODB.
            self.auditor.info(SKIPPED_STATUS_GROUPS, statusUser)
            m = 'Skipping the monthly profile-status notification for '\
                '{0} ({1}): not in any groups'
            msg = m.format(statusUser.name, statusUser.id)
            r = {'status': Status.no_groups.value, 'message': msg}
        retval = to_json(r)
        return retval
Esempio n. 35
0
 def _format_data(self, code, data, url, parameters):
     return to_json(
         {
             "view": url,
             "content": data,
             "code": code
         }
     )
Esempio n. 36
0
    def handle_get_all_digests(self, action, data):
        '''The form action for the *Get digest groups* page.

:param action: The button that was clicked.
:param dict data: The form data.'''
        log.info('Getting the digest groups')
        retval = self.get_groups()
        return to_json(retval)
    def handle_get_all_digests(self, action, data):
        '''The form action for the *Get digest groups* page.

:param action: The button that was clicked.
:param dict data: The form data.'''
        log.info('Getting the digest groups')
        retval = self.get_groups()
        return to_json(retval)
Esempio n. 38
0
    def test_flavor_get_single_with_access_list(self, nc):
        request = self.mock_rest_request(GET={'get_access_list': 'tRuE'})
        nc.flavor_get.return_value.to_dict.return_value = {'name': '1'}
        nc.flavor_get.return_value.is_public = False

        nc.flavor_access_list.return_value = [
            mock.Mock(**{'tenant_id': '11'}),
            mock.Mock(**{'tenant_id': '22'}),
        ]

        response = nova.Flavor().get(request, "1")

        self.assertStatusCode(response, 200)
        self.assertEqual(to_json(response.content.decode('utf-8')),
                         to_json('{"access-list": ["11", "22"], "name": "1"}'))

        nc.flavor_get.assert_called_once_with(request, "1", get_extras=False)
    def json(self):
        def serialize(obj):
            if isinstance(obj, datetime):
                return obj.isoformat()

        return to_json(OrderedDict(sorted(self.data.items())),
                       indent=4,
                       default=serialize)
Esempio n. 40
0
    def handle_send(self, action, data):
        '''The form action for the *Send notification* page.

:param action: The button that was clicked.
:param dict data: The form data.'''
        userInfo = createObject('groupserver.UserFromId',
                                self.context, data['profileId'])
        statusUser = StatusUser(self.context, userInfo)
        if statusUser.anonymous:
            self.auditor.info(SKIPPED_STATUS_ANON,
                              instanceDatum=data['profileId'])
            m = 'Cannot find the user object for the user ID ({0})'
            msg = m.format(data['profileId'])
            r = {'status': Status.no_user.value, 'message': msg}
        elif statusUser.hasSkip:
            # --=mpj17=-- This should never happen, but I have it here just in case
            self.auditor.info(SKIPPED_STATUS_EXPLICIT, statusUser)
            m = 'Skipping the monthly profile-status notification for '\
                '{0} ({1}): explicitly opted out'
            msg = m.format(statusUser.name, statusUser.id)
            r = {'status': Status.skip.value, 'message': msg}
        elif statusUser.inGroups:
            if statusUser.addresses:
                if statusUser.hasActivity:
                    # --=mpj17=-- To summarise, if we are here then the person is in some groups
                    # (not just sites) *and* they have at least one working email address *and*
                    # there has been some activity in at least one group group this last month.
                    notifier = StatusNotifier(statusUser.user, self.request)
                    notifier.notify()
                    self.auditor.info(SENT_STATUS, statusUser, repr(statusUser.addresses))
                    m = 'Sent the monthly profile-status notification to {0} '\
                        '({1})'
                    msg = m.format(statusUser.name, statusUser.id)
                    r = {'status': Status.ok.value, 'message': msg}
                else:  # no activity
                    self.auditor.info(SKIPPED_STATUS_INACTIVE, statusUser)
                    m = 'Skipping the monthly profile-status notification for '\
                        '{0} ({1}): no activity in any groups this month'
                    msg = m.format(statusUser.name, statusUser.id)
                    r = {'status': Status.no_activity.value, 'message': msg}
            else:  # No email addresses
                self.auditor.info(SKIPPED_STATUS_EMAIL, statusUser)
                m = 'Skipping the monthly profile-status notification for '\
                    '{0} ({1}): no verified email addresses'
                msg = m.format(statusUser.name, statusUser.id)
                r = {'status': Status.no_email.value, 'message': msg}
            assert type(r) == dict
        else:  # No groups
            # --=mpj17=-- The groups is calculated first, even though it is very expensive in
            # terms of ZODB access  I do this to get a nice list of people that we may want to
            # drop from the ZODB.
            self.auditor.info(SKIPPED_STATUS_GROUPS, statusUser)
            m = 'Skipping the monthly profile-status notification for '\
                '{0} ({1}): not in any groups'
            msg = m.format(statusUser.name, statusUser.id)
            r = {'status': Status.no_groups.value, 'message': msg}
        retval = to_json(r)
        return retval
Esempio n. 41
0
 def store_issue(issue):
     db_record = DbJiraIssues(
         key=issue.key,
         summary=issue.summary,
         status=issue.status,
         reporter=issue.reporter,
         custom_fields=to_json(issue.custom_fields),
     )
     db_record.save()
Esempio n. 42
0
    def test_flavor_get_single_with_access_list(self, nc):
        request = self.mock_rest_request(GET={'get_access_list': 'tRuE'})
        nc.flavor_get.return_value.to_dict.return_value = {'name': '1'}
        nc.flavor_get.return_value.is_public = False

        nc.flavor_access_list.return_value = [
            mock.Mock(**{'tenant_id': '11'}),
            mock.Mock(**{'tenant_id': '22'}),
        ]

        response = nova.Flavor().get(request, "1")

        self.assertStatusCode(response, 200)
        self.assertEqual(to_json(response.content.decode('utf-8')),
                         to_json('{"access-list": ["11", "22"], "name": "1"}'))

        nc.flavor_get.assert_called_once_with(request, "1",
                                              get_extras=False)
Esempio n. 43
0
def calculate_values():
    for title, article in ((file.split('.')[0],
                            open('articles/{}'.format(file),
                                 'r',
                                 encoding='utf-8').read())
                           for file in listdir('articles')):
        filename = 'values/{}.json'.format(title)
        with open(filename, 'w', encoding='utf-8') as file:
            file.write(to_json(word_counts(article)))
Esempio n. 44
0
def __serialize(cursor):
    def marshal(value):
        if isinstance(value, (UUID,long)):
            return str(value)
        return value

    if not hasattr(cursor, "result"):
        return to_json({"void": "Success"})

    rows = {}
    for x in range(cursor.rowcount):
        r = cursor.fetchone()
        rows[r[0]] = []
        for (j, column_value) in enumerate(r[1:]):
            column_name = cursor.description[j+1][0]
            rows[r[0]].append({"name": marshal(column_name),
                    "value": marshal(column_value)})

    return to_json({"rows": rows})
Esempio n. 45
0
 def _format_data(self, data, url, parameters):
     return to_json(
         {
             "view":
                 data[0][self.view_flag]["view"] if
                 type(data[0][self.view_flag]) != list else
                 filter(lambda item: item['slug'] == parameters, data[0][self.view_flag])[0]["view"],
             "content": data
         }
     )
Esempio n. 46
0
    def createPlayerStatistics(self, selection):
        self.destroyOverlay('playerStats')

        gui = []

        gui.append(self.componentUIImage('playerStats', parent="TribeBgUI", color="0.1 0.1 0.1 0.90", anchormin="0.0 0.08", anchormax="0.999 0.88"))
        gui.append(self.componentUIText(text="This is where you'll find your stats. It's under development, and it will be implementation soon (TM).", parent="playerStats", color="1.0 0.9 0.9 0.95", fontSize="16", align="MiddleCenter", anchormin="0.001 0.0", anchormax="0.999 0.93"))
        playerListUI = json.to_json(gui)
        objectList = json.makepretty(playerListUI)
        self.createOverlay(objectList)
Esempio n. 47
0
    def msg(self, url, **kwargs):
        protected = b64url(
            to_json({
                'alg':
                'ES256',
                'kid' if self.acc else 'jwk':
                self.acc or jwk(self.key.public_key()),
                'nonce':
                self.nonce,
                'url':
                url,
            }))
        payload = b64url(to_json(kwargs))
        signature = b64url(self.sig(f'{protected}.{payload}'.encode()))

        return to_json({
            'protected': protected,
            'payload': payload,
            'signature': signature,
        })
Esempio n. 48
0
        def upload_handler():
            f = request.files["file"]
            if f.content_type not in config["acceptable_type"]:
                return self._response(
                    to_json({
                        "message":
                        "File type mast be '%s'" %
                        ', '.join(config["acceptable_type"])
                    }), "application/json", 406)

            media = f.read()
            f_buf = StringIO(media)

            result = {
                "id": media_to_image(f.content_type, f_buf),
                "name": f.filename,
                "type": f.content_type,
                "size": len(media)
            }
            return self._response(to_json([result]), "application/json")
Esempio n. 49
0
 def _format_data(self, code, data, url, parameters):
     return to_json(
         {
             "view":
                 data[0][self.view_flag]["view"] if
                 type(data[0][self.view_flag]) != list else
                 filter(lambda item: item['slug'] == parameters, data[0][self.view_flag])[0]["view"],
             "content": data,
             "code": code
         }
     )
Esempio n. 50
0
def main():
    credentials = get_nova_credentials("")
    print "main {}".format(credentials)
    nova_list(credentials)
    print "fin nova main"
    exit()
    #print credentials
    nc = nvclient.Client(**credentials)
    #exit();
    #for volume in nc.volumes.list():
	#print "Valumen" , repr(volume)
 	#volume.size

    #exit();
    #print nc.flavors.list()
    #print nc.servers.list()
    #print nc.volumes.list()
	#for volume in nc.volumes.get_server_volumes("1af9e615-68c4-48e9-bb8e-1ad8e24ece40"):
#	 print "Volume in server" , repr(volume) 
    #f = open('test.json')
    #data = json.load(f)
    #f.close()

    
    for server in nc.servers.list():
	print "VM id= {}, name = {}".format(server.id,server.name)
        attach = ""
	volumensData = []
	for volume in nc.volumes.get_server_volumes(server.id):
		volumensData.append(volume.id)
	
	#print "VM id= {}, name = {}, attach = {}".format(server.id,server.name,attach)
	data = []
	data.append("casa")
	data.append("casa2")
	json = Foo()
	json.name = server.name
	json.uuid = server.id
	json.volumens = volumensData
        print json.volumens
	print json.to_json()
Esempio n. 51
0
def parse_stdin():
    """Parse protocol data passed on stdin, previously captured

    example to print all captured sensor id:s
    script/listen > /tmp/packets.log
    cat /tmp/packets.log  | ./script/parse | jq ".sensorId" | sort | uniq
    """
    for line in stdin.readlines():
        line = line.strip()
        if " " in line:
            # assume we have date + raw data separated by space
            timestamp, line = line.split()
            timestamp = parse_isoformat(timestamp)
            lastUpdated = int(timestamp.timestamp())
            packet = decode_packet(line)
            if packet is None:
                continue
            packet.update(lastUpdated=lastUpdated, time=timestamp.isoformat())
            print(to_json(packet))
        else:
            print(to_json(decode_packet(line)))
Esempio n. 52
0
 def handle_check(self, action, data):
     emailAddr = data['email']
     siteId = self.get_site_id(emailAddr)
     d = {
         'email': emailAddr,
         'siteId': siteId,
         'groupId': self.get_group_id(emailAddr),
         'siteURL': self.get_url_for_site(siteId),
         }
     self.status = 'Done'
     retval = to_json(d)
     return retval
Esempio n. 53
0
    def handle_add(self, action, data):
        '''Add someone to a group

:param action: The button that was clicked.
:param dict data: The form data.'''
        admin = self.siteInfo.site_admins[0]
        groupInfo = createObject('groupserver.GroupInfo', self.context,
                                 data['groupId'])
        adder = Adder(self.context, groupInfo, admin)
        toAddr = sanitise_address(data['email'])
        fromAddr = self.siteInfo.get_support_email()
        msg, userInfo, status = adder.add(toAddr, data)

        r = {'status': 257,
             'message': 'Not processed'}

        if status == ADD_NEW_USER:
            notifier = NotifyAdd(groupInfo.groupObj, self.request)
            fromAddr = sanitise_address(self.siteInfo.get_support_email())
            passwd = self.get_password_reset(userInfo, toAddr)
            notifier.notify(self.loggedInUser, userInfo, fromAddr, toAddr,
                            passwd)
            r['status'] = 0
            m = 'Created a new profile for {0} and added {0} to {1}'
            r['message'] = m.format(userInfo.name, groupInfo.name)
            r['user'] = user_info(self.siteInfo, userInfo)
            r['user']['email'] = email_info(self.siteInfo, userInfo)
            r['user']['groups'] = groups(self.siteInfo, userInfo)
        elif status == ADD_OLD_USER:
            notifier = NotifyJoin(groupInfo.groupObj, self.request)
            notifier.notify(userInfo)
            r['status'] = 1
            r['message'] = 'Added {0} to {1}'.format(userInfo.name, groupInfo.name)
            r['user'] = user_info(self.siteInfo, userInfo)
            r['user']['email'] = email_info(self.siteInfo, userInfo)
            r['user']['groups'] = groups(self.siteInfo, userInfo)
        elif status == ADD_EXISTING_MEMBER:
            r['status'] = 256
            r['message'] = '{0} is alredy a member of {1}'.format(userInfo.name, groupInfo.name)
            r['user'] = user_info(self.siteInfo, userInfo)
            r['user']['email'] = email_info(self.siteInfo, userInfo)
            r['user']['groups'] = groups(self.siteInfo, userInfo)
        else:
            r['status'] = 257
            r['message'] = 'An unknown event occurred.'

        if status in (ADD_NEW_USER, ADD_OLD_USER):
            adminNotifier = NotifyAdmin(groupInfo.groupObj, self.request)
            for adminInfo in groupInfo.group_admins:
                adminNotifier.notify(adminInfo, userInfo)

        retval = to_json(r)
        return retval
Esempio n. 54
0
    def parse(cls, payload_json: str):
        payload = json.loads(payload_json)

        schema_uri = payload.get("schema")
        if not schema_uri:
            raise IgluError(
                "JSON instance is not self-describing (schema property is absent):\n {json}".format(
                    json=json.to_json()
                )
            )

        data = payload.get("data")
        if not data:
            raise IgluError(
                "JSON instance is not self-describing (data proprty is absent):\n {json}".format(
                    json=json.to_json()
                )
            )

        schema_key = SchemaKey.parse_key(schema_uri)
        return cls(schema_key, data)
Esempio n. 55
0
def parse_stdin():
    """Parse protocol data passed on stdin, previously captured

    example to print all captured sensor id:s
    script/listen > /tmp/packets.log
    cat /tmp/packets.log  | ./script/parse | jq ".sensorId" | sort | uniq
    """
    for line in stdin.readlines():
        line = line.strip()
        if " " in line:
            # assume we have date + raw data separated by space
            timestamp, line = line.split(" ", 1)
            timestamp = parse_isoformat(timestamp)
            lastUpdated = int(timestamp.timestamp())
            packet = decode_packet(line)
            if packet is None:
                continue
            packet.update(lastUpdated=lastUpdated, time=timestamp.isoformat())
            print(to_json(packet))
        else:
            print(to_json(decode_packet(line)))
Esempio n. 56
0
def build_content(path, redis_object=None, content_type='page'):
    if redis_object is None:
        global ALL_CONTENT
        redis_object = ALL_CONTENT
    redis_key = join(content_type, path)
    print('++++++++build_content -> redis_key', redis_key)
    if not redis_key in redis_object:
        print('caching', redis_key)
        redis_object[redis_key] = to_json(_build_content(path, content_type))
    else:
        print('from cache:', redis_key)
    return from_json(redis_object[redis_key])