Python json.dumps() Examples

The following are code examples for showing how to use json.dumps(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: paws   Author: funkybob   File: response.py    BSD 3-Clause "New" or "Revised" License 7 votes vote down vote up
def response(body='', status=200, headers=None, binary=False):
    '''
    Generate a response dict for Lambda Proxy
    '''
    if headers is None:
        headers = {}
    if binary:
        body = b64encode(body)
    elif not isinstance(body, (str, bytes)):
        body = json.dumps(body, default=str)
        headers.setdefault('Content-Type', 'application/json')
    return {
        'statusCode': status,
        'headers': headers,
        'body': body,
        'isBase64Encoded': binary,
    } 
Example 2
Project: autolims   Author: scottbecker   File: utils.py    MIT License 7 votes vote down vote up
def put_well_data(container_id, well_index, data_obj, headers=None, org_name=None,container_json=None):
    """Update a well with new data"""
    
    initialize_config()
        
    headers = headers if headers else TSC_HEADERS
    org_name = org_name if org_name else ORG_NAME    
    
    def _well_url(container_id, well_index):
        return 'https://secure.transcriptic.com/{}/inventory/samples/{}/{}'.format(org_name, container_id, well_index)

    headers['content-type'] = 'application/json'
   
    response = requests.put(_well_url(container_id, well_index), headers=headers,
                            data=json.dumps(data_obj),
                            verify=False
                            )
    
    response.raise_for_status() 
Example 3
Project: jumpserver-python-sdk   Author: jumpserver   File: request.py    GNU General Public License v2.0 7 votes vote down vote up
def __init__(self, url, method='get', data=None, params=None,
                 headers=None, content_type='application/json', **kwargs):
        self.url = url
        self.method = method
        self.params = params or {}
        self.kwargs = kwargs

        if not isinstance(headers, dict):
            headers = {}
        self.headers = CaseInsensitiveDict(headers)
        if content_type:
            self.headers['Content-Type'] = content_type
        if data:
            self.data = json.dumps(data)
        else:
            self.data = {} 
Example 4
Project: autolims   Author: scottbecker   File: custom_connection.py    MIT License 6 votes vote down vote up
def analyze_run(self, protocol, test_mode=False, bsl=1):
        """Analyze given protocol"""
        if isinstance(protocol, Protocol):
            protocol = protocol.as_dict()
        if "errors" in protocol:
            raise AnalysisException(("Error%s in protocol:\n%s" %
                                     (("s" if len(protocol["errors"]) > 1 else ""),
                                      "".join(["- " + e['message'] + "\n" for
                                               e in protocol["errors"]]))))
    
        def error_string(r):
            return AnalysisException("Error%s in protocol:\n%s" %
                                     (("s" if len(r.json()['protocol']) > 1 else ""),
                                      "".join(["- " + e['message'] + "\n" for e in r.json()['protocol']])
                                      ))
    
        return self.post(self.get_route('analyze_run'),
                         data=json.dumps({
                             "protocol": protocol,
                             "test_mode": test_mode,
                             "bsl": bsl
                             }),
                         status_response={'422': lambda response: error_string(response)}) 
Example 5
Project: autolims   Author: scottbecker   File: custom_connection.py    MIT License 6 votes vote down vote up
def create_post(self, project_id, run_id, post_text):
        conversation_id = self._get_conversation_for_run(project_id, run_id)
        
        conversation = self.post(self.get_route('conversation_posts', conversation_id=conversation_id),
                                     data=json.dumps({
                                         "post": {
                                                 "text": post_text,
                                                 "viewable_by_users": True,
                                                 "attachments": {
                                                         "attachments": []
                                                 }
                                         }
                                 }),
                                    status_response={
                                         '404': lambda resp: AnalysisException("Error: Couldn't create post (404)."),
                                         '422': lambda resp: AnalysisException("Error creating post: %s" % resp.text)
                                     })        
        
        return conversation 
Example 6
Project: Coulomb   Author: DynamoDS   File: stacktrace_extractor.py    MIT License 6 votes vote down vote up
def writeDataToFile():
            if (len(stackTraces) == 0): # If no stack traces, skip
                if os.path.exists(out_path):
                    os.remove(out_path)
                return

            print (json.dumps(
                {
                    "StackTraces" : stackTraces,
                    "Tags" : list(tags),
                    "UserID": userId,
                    "WorkspaceVersion": version,
                    "SessionDuration": sessionEndMicroTime - sessionStartMicroTime,
                    "Date": sessionDate
                }), file=fo)


        # Process each line of the session file 
Example 7
Project: Coulomb   Author: DynamoDS   File: feature_usage_inmemory_shuffler.py    MIT License 6 votes vote down vote up
def flush():
    # Create one file per feature version
    for k in feature_versions_map.keys():
        out_full_path = out_path + "." + k + '.json'
        data_to_dump = {
            "feature_version" : k,
            "sessions" : feature_versions_map[k]
        }

        with open(out_full_path, 'w') as f:
            if pretty_print_json_output:
                f.write(json.dumps(data_to_dump, sort_keys=True, indent=2))
            else:
                f.write(json.dumps(data_to_dump))


# Main function 
Example 8
Project: Coulomb   Author: DynamoDS   File: feature_usage_extractor.py    MIT License 6 votes vote down vote up
def writeDataToFile():
            if isJSON: # Don't write results for JSON files for now
                return 

            print (json.dumps(
                {
                    "Searches" : searchMap,
                    "SearchesNodeAdded" : searchNodeAdded,
                    "Tags" : list(tags),
                    "FeatureUsageMap" : featureUsageMap,
                    "NodeUsageMap" : nodeUsageMap,
                    "UserID": userId,
                    "WorkspaceVersion": version,
                    "SessionDuration": sessionEndMicroTime - sessionStartMicroTime,
                    "Date": sessionDate
                }), file=fo)


        # Process each line of the session file 
Example 9
Project: mycode   Author: gmraabe   File: inventory.py    GNU General Public License v3.0 6 votes vote down vote up
def search_entry(search_str):    # function for searching inventory
    with open(filename, 'r') as myfile:            # open file with implied close (with)
        myDict = json.loads(myfile.read())           # read file into a dictionary
    print('The Location for ' + search_str + ' is: ' + myDict.get(search_str, "not found"))
    if type(myDict.get(search_str))() is None:     # check to see if no entry is found
        input('Press enter to continue')             # if no entry then only option is to return to Main Menu
    else:                                          # if an entry is found
        while True:                                  # loop until a valid selection is made
            print('Enter 1 to delete ' + search_str + ' or 2 to return to Main Menu')
            selection = input()                        # get input from user
            if selection == '1':                         # check if user wishes to delete the found search item
                myDict.pop(search_str)                     # remove search string from dictionary
                with open(filename, 'w') as myfile:        # open file with implied close (with)
                    myfile.write(json.dumps(myDict))         # write dictionary to file
                print(search_str + ' is deleted')          # print confirmation message
                input('Press enter to continue')           # wait for user before continuing
                break
            elif selection == '2':                     # if user wishes to exit to main menu
                break
            else:                                      # if anything else then loop
                print('Invalid selection') 
Example 10
Project: hydrus   Author: HTTP-APIs   File: test_app.py    MIT License 6 votes vote down vote up
def test_object_PUT_at_id(self):
        """Create object in collection using PUT at specific ID."""
        index = self.client.get("/{}".format(self.API_NAME))
        assert index.status_code == 200
        endpoints = json.loads(index.data.decode('utf-8'))
        for endpoint in endpoints:
            collection_name = "/".join(endpoints[endpoint].split(
                "/{}/".format(self.API_NAME))[1:])
            if collection_name in self.doc.collections:
                collection = self.doc.collections[collection_name]["collection"]
                class_ = self.doc.parsed_classes[collection.class_.title]["class"]
                class_methods = [x.method for x in class_.supportedOperation]
                dummy_object = gen_dummy_object(
                    collection.class_.title, self.doc)
                if "PUT" in class_methods:
                    dummy_object = gen_dummy_object(
                        collection.class_.title, self.doc)
                    put_response = self.client.put('{}/{}'.format(
                        endpoints[endpoint], uuid.uuid4()), data=json.dumps(dummy_object))
                    assert put_response.status_code == 201 
Example 11
Project: hydrus   Author: HTTP-APIs   File: test_app.py    MIT License 6 votes vote down vote up
def test_endpointClass_POST(self):
        """Check non collection Class POST."""
        index = self.client.get("/{}".format(self.API_NAME))
        assert index.status_code == 200
        endpoints = json.loads(index.data.decode('utf-8'))
        for endpoint in endpoints:
            if endpoint not in ["@context", "@id", "@type"]:
                class_name = "/".join(endpoints[endpoint].split(
                    "/{}/".format(self.API_NAME))[1:])
                if class_name not in self.doc.collections:
                    class_ = self.doc.parsed_classes[class_name]["class"]
                    class_methods = [
                        x.method for x in class_.supportedOperation]
                    if "POST" in class_methods:
                        dummy_object = gen_dummy_object(class_.title, self.doc)
                        post_response = self.client.post(
                            endpoints[endpoint], data=json.dumps(dummy_object))
                        assert post_response.status_code == 200 
Example 12
Project: hydrus   Author: HTTP-APIs   File: test_app.py    MIT License 6 votes vote down vote up
def test_required_props(self):
        index = self.client.get("/{}".format(self.API_NAME))
        assert index.status_code == 200
        endpoints = json.loads(index.data.decode('utf-8'))
        for endpoint in endpoints:
            if endpoint not in ["@context", "@id", "@type"]:
                class_name = "/".join(endpoints[endpoint].split(
                    "/{}/".format(self.API_NAME))[1:])
                if class_name not in self.doc.collections:
                    class_ = self.doc.parsed_classes[class_name]["class"]
                    class_methods = [
                        x.method for x in class_.supportedOperation]
                    if "PUT" in class_methods:
                        dummy_object = gen_dummy_object(class_.title, self.doc)
                        required_prop = ""
                        for prop in class_.supportedProperty:
                            if prop.required:
                                required_prop = prop.title
                                break
                        if required_prop:
                            del dummy_object[required_prop]
                            put_response = self.client.put(
                                endpoints[endpoint], data=json.dumps(dummy_object))
                            assert put_response.status_code == 400 
Example 13
Project: pyblish-win   Author: pyblish   File: test_logging.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_listen_config_10_ok(self):
        with captured_stdout() as output:
            self.setup_via_listener(json.dumps(self.config10))
            logger = logging.getLogger("compiler.parser")
            logger.warning(self.next_message())
            logger = logging.getLogger('compiler')
            #Not output, because filtered
            logger.warning(self.next_message())
            logger = logging.getLogger('compiler.lexer')
            #Not output, because filtered
            logger.warning(self.next_message())
            logger = logging.getLogger("compiler.parser.codegen")
            #Output, as not filtered
            logger.error(self.next_message())
            self.assert_log_lines([
                ('WARNING', '1'),
                ('ERROR', '4'),
            ], stream=output) 
Example 14
Project: hanuman   Author: sunlightlabs   File: extract.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def identify_bio_elements(self, firm, urls):
        fts = FirmTrainingSet.get_for_firm(firm)
        spider = Spider(firm.domain, os.path.join(self.scrape_dir, str(fts.id) + ".db"), workers=4, retry_attempts=2)

        self.stdout.write("Identifying bio elements...\n")

        model = ElementClassifier(os.path.join(settings.MODEL_DIR, 'model', str(fts.id) + "_element.tgm"))
        model.load()

        self.stdout.write('Retrieving element features...\n')

        elements_out = []
        for url in urls:
            page = spider.get(url)
            out_data = model.extract(page, format="html")
            out_data['url'] = url

            out_data = dict(out_data)
            print json.dumps(out_data, indent=4)
            elements_out.append(out_data)

        self.stdout.write('Done.\n')

        return elements_out 
Example 15
Project: backtrader-cn   Author: pandalibin   File: daily_alert.py    GNU General Public License v3.0 6 votes vote down vote up
def send_daily_alert():
    date = dt.datetime.now().strftime('%Y-%m-%d')
    msg = get_market_signal_by_date(date)

    # send notification via wechat
    wx_client = WeChatClient({
        'APP_ID': conf.WECHAT_APP_ID,
        'APP_SECRET': conf.WECHAT_APP_SECRET,
    })

    try:
        response = wx_client.send_all_text_message(
            json.dumps(msg, ensure_ascii=False))
        logger.debug(response)
    except Exception as e:
        logger.error(e, exc_info=True) 
Example 16
Project: drydock   Author: airshipit   File: designs.py    Apache License 2.0 6 votes vote down vote up
def on_get(self, req, resp):
        """Method handler for GET requests.

        :param req: Falcon request object
        :param resp: Falcon response object
        """
        state = self.state_manager

        try:
            designs = list(state.designs.keys())

            resp.body = json.dumps(designs)
            resp.status = falcon.HTTP_200
        except Exception as ex:
            self.error(req.context, "Exception raised: %s" % str(ex))
            self.return_error(
                resp,
                falcon.HTTP_500,
                message="Error accessing design list",
                retry=True) 
Example 17
Project: drydock   Author: airshipit   File: designs.py    Apache License 2.0 6 votes vote down vote up
def on_get(self, req, resp, design_id):
        """Method Handler for GET design singleton.

        :param req: Falcon request object
        :param resp: Falcon response object
        :param design_id: UUID of the design resource
        """
        source = req.params.get('source', 'designed')

        try:
            design = None
            if source == 'compiled':
                design = self.orchestrator.get_effective_site(design_id)
            elif source == 'designed':
                design = self.orchestrator.get_described_site(design_id)

            resp.body = json.dumps(design.obj_to_simple())
        except errors.DesignError:
            self.error(req.context, "Design %s not found" % design_id)
            self.return_error(
                resp,
                falcon.HTTP_404,
                message="Design %s not found" % design_id,
                retry=False) 
Example 18
Project: drydock   Author: airshipit   File: nodes.py    Apache License 2.0 6 votes vote down vote up
def on_get(self, req, resp, hostname):
        try:
            latest = req.params.get('latest', 'false').upper()
            latest = True if latest == 'TRUE' else False

            node_bd = self.state_manager.get_build_data(
                node_name=hostname, latest=latest)

            if not node_bd:
                self.return_error(
                    resp,
                    falcon.HTTP_404,
                    message="No build data found",
                    retry=False)
            else:
                node_bd = [bd.to_dict() for bd in node_bd]
                resp.status = falcon.HTTP_200
                resp.body = json.dumps(node_bd)
                resp.content_type = falcon.MEDIA_JSON
        except Exception as ex:
            self.error(req.context, "Unknown error: %s" % str(ex), exc_info=ex)
            self.return_error(
                resp, falcon.HTTP_500, message="Unknown error", retry=False) 
Example 19
Project: drydock   Author: airshipit   File: nodes.py    Apache License 2.0 6 votes vote down vote up
def on_post(self, req, resp):
        try:
            json_data = self.req_json(req)
            node_filter = json_data.get('node_filter', None)
            design_ref = json_data.get('design_ref', None)
            if design_ref is None:
                self.info(req.context,
                          'Missing required input value: design_ref')
                self.return_error(
                    resp,
                    falcon.HTTP_400,
                    message='Missing input required value: design_ref',
                    retry=False)
                return
            _, site_design = self.orchestrator.get_effective_site(design_ref)
            nodes = self.orchestrator.process_node_filter(
                node_filter=node_filter, site_design=site_design)
            resp_list = [n.name for n in nodes if nodes]

            resp.body = json.dumps(resp_list)
            resp.status = falcon.HTTP_200
        except Exception as ex:
            self.error(req.context, "Unknown error: %s" % str(ex), exc_info=ex)
            self.return_error(
                resp, falcon.HTTP_500, message="Unknown error", retry=False) 
Example 20
Project: drydock   Author: airshipit   File: tasks.py    Apache License 2.0 6 votes vote down vote up
def task_validate_design(self, req, resp, json_data):
        """Create async task for validate design."""
        action = json_data.get('action', None)

        if action != 'validate_design':
            self.error(
                req.context,
                "Task body ended up in wrong handler: action %s in task_validate_design"
                % action)
            self.return_error(
                resp, falcon.HTTP_500, message="Error", retry=False)

        try:
            task = self.create_task(json_data, req.context)
            resp.body = json.dumps(task.to_dict())
            resp.append_header('Location',
                               "/api/v1.0/tasks/%s" % str(task.task_id))
            resp.status = falcon.HTTP_201
        except errors.InvalidFormat as ex:
            self.error(req.context, ex.msg)
            self.return_error(
                resp, falcon.HTTP_400, message=ex.msg, retry=False) 
Example 21
Project: drydock   Author: airshipit   File: tasks.py    Apache License 2.0 6 votes vote down vote up
def task_verify_site(self, req, resp, json_data):
        """Create async task for verify site."""
        action = json_data.get('action', None)

        if action != 'verify_site':
            self.error(
                req.context,
                "Task body ended up in wrong handler: action %s in task_verify_site"
                % action)
            self.return_error(
                resp, falcon.HTTP_500, message="Error", retry=False)

        try:
            task = self.create_task(json_data, req.context)
            resp.body = json.dumps(task.to_dict())
            resp.append_header('Location',
                               "/api/v1.0/tasks/%s" % str(task.task_id))
            resp.status = falcon.HTTP_201
        except errors.InvalidFormat as ex:
            self.error(req.context, ex.msg)
            self.return_error(
                resp, falcon.HTTP_400, message=ex.msg, retry=False) 
Example 22
Project: drydock   Author: airshipit   File: tasks.py    Apache License 2.0 6 votes vote down vote up
def task_prepare_site(self, req, resp, json_data):
        """Create async task for prepare site."""
        action = json_data.get('action', None)

        if action != 'prepare_site':
            self.error(
                req.context,
                "Task body ended up in wrong handler: action %s in task_prepare_site"
                % action)
            self.return_error(
                resp, falcon.HTTP_500, message="Error", retry=False)

        try:
            task = self.create_task(json_data, req.context)
            resp.body = json.dumps(task.to_dict())
            resp.append_header('Location',
                               "/api/v1.0/tasks/%s" % str(task.task_id))
            resp.status = falcon.HTTP_201
        except errors.InvalidFormat as ex:
            self.error(req.context, ex.msg)
            self.return_error(
                resp, falcon.HTTP_400, message=ex.msg, retry=False) 
Example 23
Project: drydock   Author: airshipit   File: tasks.py    Apache License 2.0 6 votes vote down vote up
def task_verify_nodes(self, req, resp, json_data):
        """Create async task for verify node."""
        action = json_data.get('action', None)

        if action != 'verify_nodes':
            self.error(
                req.context,
                "Task body ended up in wrong handler: action %s in task_verify_nodes"
                % action)
            self.return_error(
                resp, falcon.HTTP_500, message="Error", retry=False)

        try:
            task = self.create_task(json_data, req.context)
            resp.body = json.dumps(task.to_dict())
            resp.append_header('Location',
                               "/api/v1.0/tasks/%s" % str(task.task_id))
            resp.status = falcon.HTTP_201
        except errors.InvalidFormat as ex:
            self.error(req.context, ex.msg)
            self.return_error(
                resp, falcon.HTTP_400, message=ex.msg, retry=False) 
Example 24
Project: drydock   Author: airshipit   File: tasks.py    Apache License 2.0 6 votes vote down vote up
def task_prepare_nodes(self, req, resp, json_data):
        """Create async task for prepare node."""
        action = json_data.get('action', None)

        if action != 'prepare_nodes':
            self.error(
                req.context,
                "Task body ended up in wrong handler: action %s in task_prepare_nodes"
                % action)
            self.return_error(
                resp, falcon.HTTP_500, message="Error", retry=False)

        try:
            task = self.create_task(json_data, req.context)
            resp.body = json.dumps(task.to_dict())
            resp.append_header('Location',
                               "/api/v1.0/tasks/%s" % str(task.task_id))
            resp.status = falcon.HTTP_201
        except errors.InvalidFormat as ex:
            self.error(req.context, ex.msg)
            self.return_error(
                resp, falcon.HTTP_400, message=ex.msg, retry=False) 
Example 25
Project: BERT-Classification-Tutorial   Author: Socialbird-AILab   File: modeling.py    Apache License 2.0 5 votes vote down vote up
def to_json_string(self):
        """Serializes this instance to a JSON string."""
        return json.dumps(self.to_dict(), indent=2, sort_keys=True) + "\n" 
Example 26
Project: fs_image   Author: facebookincubator   File: update_package_db.py    MIT License 5 votes vote down vote up
def _write_json_dir_db(db: PackageTagDb, path: Path, how_to_generate: str):
    with populate_temp_dir_and_rename(path, overwrite=True) as td:
        for package, tag_to_info in db.items():
            os.mkdir(td / package)
            for tag, info in tag_to_info.items():
                with open(td / package / (tag + _JSON), 'w') as outf:
                    outf.write(_with_generated_header(
                        json.dumps(info, sort_keys=True, indent=4) + '\n',
                        how_to_generate,
                    )) 
Example 27
Project: fs_image   Author: facebookincubator   File: repo_snapshot.py    MIT License 5 votes vote down vote up
def _gen_object_rows(
        self,
        repo: str,
        sid_to_obj: Union[
            Mapping[MaybeStorageID, Union[Rpm]],
            Mapping[MaybeStorageID, Union[Repodata]],
        ],
        expected_columns: Iterable[str],
        get_other_cols_fn: Union[
            Callable[[Rpm], Mapping[str, Any]],
            Callable[[Repodata], Mapping[str, Any]],
        ],
    ):
        for sid, obj in sid_to_obj.items():
            if isinstance(sid, ReportableError):
                error_dict = sid.to_dict()
                error = error_dict.pop('error')
                sid = error_dict.pop('storage_id', None)
            else:
                error_dict = None
                error = None
            d = {
                'repo': repo,
                'path': obj.location,
                'build_timestamp': obj.build_timestamp,
                'checksum': str(obj.best_checksum()),
                'error': error,
                'error_json': json.dumps(error_dict, sort_keys=True)
                    if error_dict else None,
                'size': obj.size,
                'storage_id': sid,
            }
            other_d = get_other_cols_fn(obj)
            assert not (set(d) & set(other_d)), (d, other_d)
            d.update(other_d)
            assert set(d) == set(expected_columns), (d, expected_columns)
            yield d 
Example 28
Project: fs_image   Author: facebookincubator   File: test_snapshot_repos.py    MIT License 5 votes vote down vote up
def test_snapshot(self):
        with temp_repos.temp_repos_steps(repo_change_steps=[{
            'cat': temp_repos.SAMPLE_STEPS[0]['cat'],
            'dog': temp_repos.SAMPLE_STEPS[0]['dog'],
        }]) as repos_root, temp_dir() as td:
            storage_dict = {
                'key': 'test',
                'kind': 'filesystem',
                'base_dir': (td / 'storage').decode(),
            }
            snapshot_repos_from_args([
                '--dnf-conf', (repos_root / '0/dnf.conf').decode(),
                '--yum-conf', (repos_root / '0/yum.conf').decode(),
                '--gpg-key-whitelist-dir', (td / 'gpg_whitelist').decode(),
                '--snapshot-dir', (td / 'snap').decode(),
                '--storage', json.dumps(storage_dict),
                '--db', json.dumps({
                    'kind': 'sqlite',
                    'db_path': (td / 'db.sqlite3').decode(),
                }),
            ])
            # As with `test_snapshot_repo`, this is just a sanity check --
            # the lower-level details are checked by lower-level tests.
            with sqlite3.connect(RepoSnapshot.fetch_sqlite_from_storage(
                Storage.make(**storage_dict),
                td / 'snap',
                td / 'snapshot.sql3',
            )) as db:
                self.assertEqual({
                    'cat-pkgs/rpm-test-carrot-1-lockme.x86_64.rpm',
                    'cat-pkgs/rpm-test-mice-0.1-a.x86_64.rpm',
                    'cat-pkgs/rpm-test-milk-2.71-8.x86_64.rpm',
                    'dog-pkgs/rpm-test-carrot-2-rc0.x86_64.rpm',
                    'dog-pkgs/rpm-test-mice-0.1-a.x86_64.rpm',
                    'dog-pkgs/rpm-test-milk-1.41-42.x86_64.rpm',
                }, {
                    path for path, in db.execute('''
                        SELECT "path" FROM "rpm"
                        WHERE "repo" in ("cat", "dog")
                        ''').fetchall()
                }) 
Example 29
Project: fs_image   Author: facebookincubator   File: test_yum_dnf_from_snapshot.py    MIT License 5 votes vote down vote up
def _install(self, *, protected_paths, version_lock=None):
        install_root = Path(tempfile.mkdtemp())
        try:
            # IMAGE_ROOT/meta/ is always required since it's always protected
            for p in set(protected_paths) | {'meta/'}:
                if p.endswith('/'):
                    os.makedirs(install_root / p)
                else:
                    os.makedirs(os.path.dirname(install_root / p))
                    with open(install_root / p, 'wb'):
                        pass
            snapshot_dir = Path(load_location('rpm', 'repo-snapshot'))
            # Note: this can't use `_yum_using_build_appliance` because that
            # would lose coverage info on `yum_dnf_from_snapshot.py`.  On
            # the other hand, running this test against the host is fragile
            # since it depends on the system packages available on CI
            # containers.  For this reason, this entire test is an
            # `image.python_unittest` that runs in a build appliance.
            with tempfile.NamedTemporaryFile(mode='w') as tf:
                if version_lock:
                    tf.write('\n'.join(version_lock) + '\n')
                tf.flush()
                yum_dnf_from_snapshot(
                    yum_dnf=self._YUM_DNF,
                    repo_server_bin=Path(load_location('rpm', 'repo-server')),
                    storage_cfg=json.dumps({
                        'key': 'test',
                        'kind': 'filesystem',
                        'base_dir': (snapshot_dir / 'storage').decode(),
                    }),
                    snapshot_dir=snapshot_dir,
                    install_root=Path(install_root),
                    protected_paths=protected_paths,
                    versionlock_list=tf.name,
                    yum_dnf_args=_INSTALL_ARGS,
                )
            yield install_root
        finally:
            assert install_root != '/'
            # Courtesy of `yum`, the `install_root` is now owned by root.
            subprocess.run(['sudo', 'rm', '-rf', install_root], check=True) 
Example 30
Project: fs_image   Author: facebookincubator   File: subvolume_on_disk.py    MIT License 5 votes vote down vote up
def to_json_file(self, outfile):
        outfile.write(json.dumps(self.to_serializable_dict())) 
Example 31
Project: leapp-repository   Author: oamg   File: systemfacts.py    Apache License 2.0 5 votes vote down vote up
def _get_repositories():
    def asbool(x):
        return x == '1'

    @aslist
    def _parse(r):
        with open(r, mode='r') as fp:
            cp = configparser.ConfigParser()
            cp.readfp(fp)
            for section in cp.sections():
                prepared = {'repoid': section, 'additional_fields': {}}
                data = dict(cp.items(section))
                for key in data.keys():
                    if key in RepositoryData.fields:
                        if isinstance(RepositoryData.fields[key], fields.Boolean):
                            data[key] = asbool(data[key])
                        prepared[key] = data[key]
                    else:
                        prepared['additional_fields'][key] = data[key]
                prepared['additional_fields'] = json.dumps(prepared['additional_fields'])
                yield RepositoryData(**prepared)

    repos = run(
        ['find', '/etc/yum.repos.d/', '-type', 'f', '-name', '*.repo'],
        split=True
    )['stdout']
    for repo in repos:
        yield RepositoryFile(file=repo, data=_parse(repo)) 
Example 32
Project: autolims   Author: scottbecker   File: run_tags.py    MIT License 5 votes vote down vote up
def format_json(d):
    """
    Covert a label like 'plate 1' to plate_1
    """
    return json.dumps(d) 
Example 33
Project: incubator-spot   Author: apache   File: graphql.py    Apache License 2.0 5 votes vote down vote up
def send_query(self):
        assert(self.url is not None)
        assert(type(self.url) is str)
        assert(self.query is not None)
        assert(type(self.query) is str)

        data = {
            'query': self.query
        }

        if self.variables is not None and type(self.variables) is dict:
            data['variables'] = self.variables

        encoded_data = json.dumps(data).encode('utf-8')

        http = urllib3.PoolManager()

        response = http.request(
            'POST',
            self.url,
            body=encoded_data,
            headers={
                'Accept': 'application/json',
                'Content-type': 'application/json'
            }
        )

        try:
            return json.loads(response.data.decode('utf-8'))
        except:
            return {
                'errors': [
                    {
                        'status': response.status,
                        'message': 'Failed to contact GraphQL endpoint. Is "{}" the correct URL?'.format(self.url)
                    }
                ]
            } 
Example 34
Project: jumpserver-python-sdk   Author: jumpserver   File: test_request.py    GNU General Public License v2.0 5 votes vote down vote up
def setUp(self):
        self.url = 'http://www.jumpserver.org'
        self.method = "post"
        self.data = {"data": 123}
        self.data_json = json.dumps(self.data)
        self.param = {"hello": "world"}
        self.headers = {"Cookie": "123"}
        self.request = HttpRequest(self.url, method=self.method, data=self.data,
                                   params=self.param, headers=self.headers) 
Example 35
Project: Coulomb   Author: DynamoDS   File: feature_usage_jsons_shuffler.py    MIT License 5 votes vote down vote up
def flush():
    # Create one file per feature version
    for k in feature_versions_map.keys():
        out_full_path = out_path + "." + k + '.jsons'
        
        # Ensure we created all of the data files
        if out_full_path not in known_files:
            known_files.add(out_full_path)
            if os.path.exists(out_full_path):
                print ("Removing existing file: " + out_full_path)
                os.remove(out_full_path)
        
        sessions = feature_versions_map[k]
        feature_version = k

        with open(out_full_path, 'a') as f:
            for session_id in sessions.keys():
                data_to_dump = {
                    'feature_version' : feature_version,
                    'session_id' : session_id,
                    'features' : sessions[session_id]
                }

                if pretty_print_json_output:
                    f.write(json.dumps(data_to_dump, sort_keys=True, indent=2) + "\n")
                else:
                    f.write(json.dumps(data_to_dump) + "\n")
            f.flush()
    feature_versions_map.clear()


# Main function 
Example 36
Project: telegram-innovation-chatbot   Author: zaoldyeck   File: olami.py    MIT License 5 votes vote down vote up
def _gen_rq(self, text):
        obj = {'data_type': 'stt', 'data': {'input_type': self.input_type, 'text': text}}
        return json.dumps(obj) 
Example 37
Project: mycode   Author: gmraabe   File: inventory.py    GNU General Public License v3.0 5 votes vote down vote up
def new_entry(new_item, new_loc):                # function for creating a new entry
    with open(filename, 'r') as myfile:            # open file with implied close (with)
        myDict = json.loads(myfile.read())           # read file into a dictionary
    myDict[new_item] = new_loc                     # append new item and location to dictionary
    with open(filename, 'w') as myfile:            # open file with implied close (with)
        myfile.write(json.dumps(myDict))             # write dictionary to file
    print(new_item + ' added to inventory')
    input('Press enter to continue') 
Example 38
Project: kicker-module   Author: EvanTheB   File: ladder_web.py    GNU General Public License v3.0 5 votes vote down vote up
def GET(self):
        user_input = web.input(command='')
        user_command = urllib.unquote(user_input.command)
        print user_command
        ret = k.ladder_command(user_command.split())
        web.header('Content-Type', 'application/json')
        return json.dumps(ret) 
Example 39
Project: clikit   Author: sdispater   File: abstract_help.py    MIT License 5 votes vote down vote up
def _format_value(self, value):  # type: (Any) -> str
        return json.dumps(value) 
Example 40
Project: payroll   Author: andela-sjames   File: views.py    MIT License 5 votes vote down vote up
def get(self, request, *args, **kwargs):

        # cache this view for 1 hour
        cache_key = request.build_absolute_uri().split("?")[0]
        serialized_q = cache.get(cache_key)

        if not serialized_q:

            # query the DB for the report by id
            report_id = self.kwargs.get('report_id')
            try:
                report = Report.objects.get(report_id=report_id)
            except Report.DoesNotExist:
                return JsonResponse({
                    "msg": f"No Payroll report with id {report_id}",
                    "status": "failure",
                })

            queryset = report.payroll.values(
                'employee_id', 'pay_period', 'amount'
            )
            queryset = self.normalize_payroll(queryset)
            serialized_q = json.dumps(list(queryset), cls=DjangoJSONEncoder)

        cache.set(cache_key, serialized_q)

        # return payroll at this point.
        return JsonResponse({
            "msg": "payroll loading",
            "status": "success",
            "data": serialized_q
        }) 
Example 41
Project: payroll   Author: andela-sjames   File: views.py    MIT License 5 votes vote down vote up
def get(self, request, *args, **kwargs):

        # cache is set to cache for 1 hour
        # from the settings file.
        cache_key = request.build_absolute_uri().split("?")[0]
        serialized_q = cache.get(cache_key)

        if not serialized_q:

            # query the DB for the report by id
            report_id = self.kwargs.get('report_id')
            try:
                report = Report.objects.get(report_id=report_id)
            except Report.DoesNotExist:
                return JsonResponse({
                    "msg": f"No Pay report with id {report_id}",
                    "status": "failure",
                })

            queryset = report.pay.values(
                'date', 'hours', 'employee_id', 'job_group'
            )
            queryset = self.normalize_report(queryset)
            serialized_q = json.dumps(list(queryset), cls=DjangoJSONEncoder)

        cache.set(cache_key, serialized_q)

        # return result at this point.
        return JsonResponse({
            "msg": "report loading",
            "status": "success",
            "data": serialized_q
        }) 
Example 42
Project: heroku-log-lights   Author: codingjoe   File: io.py    MIT License 5 votes vote down vote up
def get_stream_url(app_name, token):
    url = 'https://api.heroku.com/apps/%s/log-sessions' % app_name
    async with aiohttp.ClientSession() as session:
        headers = {
            'Content-Type': 'application/json',
            'Accept': 'application/vnd.heroku+json; version=3',
            'Authorization': 'Bearer %s' % token
        }
        response = await session.post(url, data=json.dumps(log_config), headers=headers)
        if response.status == 201:
            data = await response.json()
            return data['logplex_url']
        else:
            raise IOError 
Example 43
Project: chainer-openai-transformer-lm   Author: soskek   File: utils.py    MIT License 5 votes vote down vote up
def __init__(self, path, *args, **kwargs):
        if 'time' not in kwargs:
            kwargs['time'] = time.time()
        self.f_log = open(make_path(path), 'w')
        self.f_log.write(json.dumps(kwargs) + '\n') 
Example 44
Project: chainer-openai-transformer-lm   Author: soskek   File: utils.py    MIT License 5 votes vote down vote up
def log(self, **kwargs):
        if 'time' not in kwargs:
            kwargs['time'] = time.time()
        self.f_log.write(json.dumps(kwargs) + '\n')
        self.f_log.flush() 
Example 45
Project: invenio-openaire   Author: inveniosoftware   File: loaders.py    MIT License 5 votes vote down vote up
def dump(self, as_json=True, commit_batch_size=100):
        """
        Dump the grant information to a local storage.

        :param as_json: Convert XML to JSON before saving (default: True).
        """
        connection = sqlite3.connect(self.destination)
        format_ = 'json' if as_json else 'xml'
        if not self._db_exists(connection):
            connection.execute(
                "CREATE TABLE grants (data text, format text)")

        # This will call the RemoteOAIRELoader.iter_grants and fetch
        # records from remote location.
        grants_iterator = self.loader.iter_grants(as_json=as_json)
        for idx, grant_data in enumerate(grants_iterator, 1):
            if as_json:
                grant_data = json.dumps(grant_data, indent=2)
            connection.execute(
                "INSERT INTO grants VALUES (?, ?)", (grant_data, format_))

            # Commit to database every N records
            if idx % commit_batch_size == 0:
                connection.commit()
        connection.commit()
        connection.close() 
Example 46
Project: alfred-yubikey-otp   Author: robertoriv   File: workflow3.py    MIT License 5 votes vote down vote up
def __unicode__(self):
        """Convert to ``alfredworkflow`` JSON object.

        Returns:
            unicode: ``alfredworkflow`` JSON object

        """
        if not self and not self.config:
            if self.arg:
                return self.arg
            else:
                return u''

        return json.dumps(self.obj) 
Example 47
Project: hydrus   Author: HTTP-APIs   File: test_auth.py    MIT License 5 votes vote down vote up
def test_wrongID_POST(self):
        """Test for the index."""
        response_get = self.client.get("/{}".format(self.API_NAME))
        endpoints = json.loads(response_get.data.decode('utf-8'))
        for endpoint in endpoints:
            if endpoint in self.doc.collections:
                response_get = self.client.get(endpoints[endpoint])
                self.wrong_id['X-Authentication'] = response_get.headers['X-Authentication']
                response_get = self.client.post(
                    endpoints[endpoint], headers=self.wrong_id, data=json.dumps(dict(foo="bar")))
                assert response_get.status_code == 401 or response_get.status_code == 400 
Example 48
Project: hydrus   Author: HTTP-APIs   File: test_auth.py    MIT License 5 votes vote down vote up
def test_wrongPass_POST(self):
        """Test for the index."""
        response_get = self.client.get("/{}".format(self.API_NAME))
        endpoints = json.loads(response_get.data.decode('utf-8'))
        for endpoint in endpoints:
            if endpoint in self.doc.collections:
                response_get = self.client.get(endpoints[endpoint])
                self.wrong_pass['X-Authentication'] = response_get.headers['X-Authentication']
                response_get = self.client.post(
                    endpoints[endpoint], headers=self.wrong_pass, data=json.dumps(dict(foo="bar")))
                assert response_get.status_code == 401 
Example 49
Project: hydrus   Author: HTTP-APIs   File: test_auth.py    MIT License 5 votes vote down vote up
def test_wrong_nonce_post(self):
        """Test for the index."""
        response_get = self.client.get("/{}".format(self.API_NAME))
        endpoints = json.loads(response_get.data.decode('utf-8'))
        for endpoint in endpoints:
            if endpoint in self.doc.collections:
                self.auth_header['X-authentication'] = "random-string"
                response_get = self.client.post(
                    endpoints[endpoint], headers=self.auth_header, data=json.dumps(dict(foo="bar")))
                assert response_get.status_code == 401 
Example 50
Project: hydrus   Author: HTTP-APIs   File: test_auth.py    MIT License 5 votes vote down vote up
def test_Auth_POST(self):
        """Test for the index."""
        response_get = self.client.get("/{}".format(self.API_NAME))
        endpoints = json.loads(response_get.data.decode('utf-8'))
        for endpoint in endpoints:
            if endpoint in self.doc.collections:
                response_get = self.client.get(endpoints[endpoint])
                self.auth_header['X-Authentication'] = response_get.headers['X-Authentication']
                response_get = self.client.post(
                    endpoints[endpoint], headers=self.auth_header, data=json.dumps(dict(foo="bar")))
                assert response_get.status_code != 401