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: autolims   Author: scottbecker   File: utils.py    MIT License 8 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 2
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 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: mycode   Author: gmraabe   File: inventory.py    GNU General Public License v3.0 7 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 5
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 6
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 7
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 8
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 9
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 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 
Example 51
Project: hydrus   Author: HTTP-APIs   File: test_auth.py    MIT License 5 votes vote down vote up
def test_Auth_PUT(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.put(
                    endpoints[endpoint], headers=self.auth_header, data=json.dumps(dict(foo="bar")))
                assert response_get.status_code != 401 
Example 52
Project: hydrus   Author: HTTP-APIs   File: test_app.py    MIT License 5 votes vote down vote up
def test_Collections_PUT(self):
        """Test insert data to the collection."""
        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"]
                dummy_object = gen_dummy_object(
                    collection.class_.title, self.doc)
                good_response_put = self.client.put(
                    endpoints[endpoint], data=json.dumps(dummy_object))
                assert good_response_put.status_code == 201 
Example 53
Project: hydrus   Author: HTTP-APIs   File: test_app.py    MIT License 5 votes vote down vote up
def test_object_POST(self):
        """Test replace of a given object using 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)
                initial_put_response = self.client.put(
                    endpoints[endpoint], data=json.dumps(dummy_object))
                assert initial_put_response.status_code == 201
                response = json.loads(
                    initial_put_response.data.decode('utf-8'))
                regex = r'(.*)ID (.{36})* (.*)'
                matchObj = re.match(regex, response["description"])
                assert matchObj is not None
                id_ = matchObj.group(2)
                if "POST" in class_methods:
                    dummy_object = gen_dummy_object(
                        collection.class_.title, self.doc)
                    post_replace_response = self.client.post(
                        '{}/{}'.format(endpoints[endpoint], id_), data=json.dumps(dummy_object))
                    assert post_replace_response.status_code == 200 
Example 54
Project: hydrus   Author: HTTP-APIs   File: test_app.py    MIT License 5 votes vote down vote up
def test_object_DELETE(self):
        """Test DELETE of a given object using 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)
                initial_put_response = self.client.put(
                    endpoints[endpoint], data=json.dumps(dummy_object))
                assert initial_put_response.status_code == 201
                response = json.loads(
                    initial_put_response.data.decode('utf-8'))
                regex = r'(.*)ID (.{36})* (.*)'
                matchObj = re.match(regex, response["description"])
                assert matchObj is not None
                id_ = matchObj.group(2)
                if "DELETE" in class_methods:
                    delete_response = self.client.delete(
                        '{}/{}'.format(endpoints[endpoint], id_))
                    assert delete_response.status_code == 200 
Example 55
Project: hydrus   Author: HTTP-APIs   File: test_app.py    MIT License 5 votes vote down vote up
def test_object_PUT_at_ids(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:
            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]
                data_ = {"data": list()}
                objects = list()
                ids = ""
                for index in range(3):
                    objects.append(gen_dummy_object(
                        collection.class_.title, self.doc))
                    ids = "{},".format(uuid.uuid4())
                data_["data"] = objects
                if "PUT" in class_methods:
                    put_response = self.client.put(
                        '{}/add/{}'.format(endpoints[endpoint], ids),
                        data=json.dumps(data_))
                    assert put_response.status_code == 201 
Example 56
Project: hydrus   Author: HTTP-APIs   File: test_app.py    MIT License 5 votes vote down vote up
def test_writeable_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 "POST" in class_methods:
                        dummy_object = gen_dummy_object(class_.title, self.doc)
                        # Test for writeable properties
                        post_response = self.client.post(
                            endpoints[endpoint], data=json.dumps(dummy_object))
                        assert post_response.status_code == 200
                        # Test for properties with writeable=False
                        non_writeable_prop = ""
                        for prop in class_.supportedProperty:
                            if prop.write is False:
                                non_writeable_prop = prop.title
                                break
                        if non_writeable_prop != "":
                            dummy_object[non_writeable_prop] = "xyz"
                            post_response = self.client.post(
                                endpoints[endpoint], data=json.dumps(dummy_object))
                            assert post_response.status_code == 405 
Example 57
Project: hydrus   Author: HTTP-APIs   File: test_app.py    MIT License 5 votes vote down vote up
def test_bad_objects(self):
        """Checks if bad objects are added or not."""
        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:
                bad_response_put = self.client.put(
                    endpoints[endpoint],
                    data=json.dumps(
                        dict(
                            foo='bar')))
                assert bad_response_put.status_code == 400 
Example 58
Project: hydrus   Author: HTTP-APIs   File: test_app.py    MIT License 5 votes vote down vote up
def test_bad_requests(self):
        """Checks if bad requests are handled or not."""
        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)
                initial_put_response = self.client.put(
                    endpoints[endpoint], data=json.dumps(dummy_object))
                assert initial_put_response.status_code == 201
                response = json.loads(
                    initial_put_response.data.decode('utf-8'))
                regex = r'(.*)ID (.{36})* (.*)'
                matchObj = re.match(regex, response["description"])
                assert matchObj is not None
                id_ = matchObj.group(2)
                if "POST" not in class_methods:
                    dummy_object = gen_dummy_object(
                        collection.class_.title, self.doc)
                    post_replace_response = self.client.post(
                        '{}/{}'.format(endpoints[endpoint], id_), data=json.dumps(dummy_object))
                    assert post_replace_response.status_code == 405
                if "DELETE" not in class_methods:
                    delete_response = self.client.delete(
                        '{}/{}'.format(endpoints[endpoint], id_))
                    assert delete_response.status_code == 405 
Example 59
Project: phrydy   Author: Josef-Friedrich   File: versioneer.py    MIT License 5 votes vote down vote up
def write_to_version_file(filename, versions):
    """Write the given version number to the given _version.py file."""
    os.unlink(filename)
    contents = json.dumps(versions, sort_keys=True,
                          indent=1, separators=(",", ": "))
    with open(filename, "w") as f:
        f.write(SHORT_VERSION_PY % contents)

    print("set %s to '%s'" % (filename, versions["version"])) 
Example 60
Project: pepper-robot-programming   Author: maverickjoy   File: asthama_search.py    MIT License 5 votes vote down vote up
def _isAsthamaPump(self, imageWidth, imageHeight, imageString):
        result = {}
        coordinates = {}
        metadata = {}
        isPresent = False

        try :
            self._printLogs("Sending Image To DL Server...", "NORMAL")

            url = DL_SERVER_URL
            payload = {
                        "imageWidth"   : imageWidth,
                        "imageHeight"  : imageHeight,
                        "image_string" : base64.b64encode(imageString),
                        "imageID"      : self.imageNo2d
                        }
            headers = {'content-type': 'application/json'}

            res = requests.post(url, data=json.dumps(payload), headers=headers)
            result = res.json()
            self._printLogs("[*] Sent to  : " + str(url), "OKBLUE")
            self._printLogs("[*] Response : " + str(result), "OKBLUE")

        except Exception, err:
            self._printLogs("Error Found on connecting to server : " + str(err), "FAIL")
            self._printLogs("+", "LINE") 
Example 61
Project: pyblish-win   Author: pyblish   File: regrtest.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def work():
            # A worker thread.
            try:
                while True:
                    try:
                        test, args_tuple = next(pending)
                    except StopIteration:
                        output.put((None, None, None, None))
                        return
                    # -E is needed by some tests, e.g. test_import
                    popen = Popen(base_cmd + ['--slaveargs', json.dumps(args_tuple)],
                                   stdout=PIPE, stderr=PIPE,
                                   universal_newlines=True,
                                   close_fds=(os.name != 'nt'))
                    stdout, stderr = popen.communicate()
                    # Strip last refcount output line if it exists, since it
                    # comes from the shutdown of the interpreter in the subcommand.
                    stderr = debug_output_pat.sub("", stderr)
                    stdout, _, result = stdout.strip().rpartition("\n")
                    if not result:
                        output.put((None, None, None, None))
                        return
                    result = json.loads(result)
                    output.put((test, stdout.rstrip(), stderr.rstrip(), result))
            except BaseException:
                output.put((None, None, None, None))
                raise 
Example 62
Project: pyblish-win   Author: pyblish   File: test_multiprocessing.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def run_in_child(cls):
        import json
        r, w = multiprocessing.Pipe(duplex=False)
        p = multiprocessing.Process(target=cls.run_in_grandchild, args=(w,))
        p.start()
        grandchild_flags = r.recv()
        p.join()
        r.close()
        w.close()
        flags = (tuple(sys.flags), grandchild_flags)
        print(json.dumps(flags)) 
Example 63
Project: hanuman   Author: sunlightlabs   File: extract.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def handle(self, *args, **options):
        from nanospider.spider import Spider
        self.scrape_dir = os.path.join(settings.MODEL_DIR, 'scrape')
        self.model_dir = os.path.join(settings.MODEL_DIR, 'model')

        for data_dir in (self.scrape_dir, self.model_dir):
            if not os.path.exists(data_dir):
                os.mkdir(data_dir)

        for firm_id in options['firm_id']:
            try:
                firm = Firm.objects.get(pk=firm_id)
            except Firm.DoesNotExist:
                raise CommandError('Firm "%s" does not exist' % firm_id)

            fts = FirmTrainingSet.get_for_firm(firm)

            self.stdout.write("Extracting bios for firm \"%s\"\n" % firm.name)

            if not all([fts.spider_complete, fts.page_classifier_trained, fts.element_classifier_trained]):
                raise CommandError('Spidering must be complete and both models must be trained for extraction to occur.')

            pages = self.identify_bio_pages(firm)
            elements = self.identify_bio_elements(firm, pages)

            print json.dumps(elements, indent=4)

            self.stdout.write('Successfully extracted data for firm "%s"' % firm.name) 
Example 64
Project: xadmin_bugfix   Author: vip68   File: models.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def set_json(self, obj):
        self.value = json.dumps(obj, cls=JSONEncoder, ensure_ascii=False) 
Example 65
Project: xadmin_bugfix   Author: vip68   File: models.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def set_value(self, obj):
        self.value = json.dumps(obj, cls=JSONEncoder, ensure_ascii=False) 
Example 66
Project: backtrader-cn   Author: pandalibin   File: sina.py    GNU General Public License v3.0 5 votes vote down vote up
def pretty_print(data):
    print(json.dumps(data, indent=4, ensure_ascii=False)) 
Example 67
Project: backtrader-cn   Author: pandalibin   File: sina.py    GNU General Public License v3.0 5 votes vote down vote up
def check_error(res_dict):
    """
    检测返回值中是否包含错误的返回码。
    如果返回码提示有错误,抛出一个异常
    """
    logger.error(json.dumps(res_dict, ensure_ascii=False))
    if "retcode" in res_dict:
        if res_dict['retcode'] == 1005:
            logger.warning("下单失败, 操作过快,即将重试!")
            raise HighFrequencyError("{}: {}".format(res_dict["retcode"], res_dict["msg"]))
        raise StockMatchError("{}: {}".format(res_dict["retcode"], res_dict["msg"]))
    raise StockMatchError(json.dumps(res_dict)) 
Example 68
Project: drydock   Author: airshipit   File: designs.py    Apache License 2.0 5 votes vote down vote up
def on_post(self, req, resp):
        """Method handler for POST requests.

        :param req: Falcon request object
        :param resp: Falcon response object
        """
        try:
            json_data = self.req_json(req)
            design = None
            if json_data is not None:
                base_design = json_data.get('base_design_id', None)

                if base_design is not None:
                    base_design = uuid.UUID(base_design)
                    design = hd_objects.SiteDesign(base_design_id=base_design)
            else:
                design = hd_objects.SiteDesign()
            design.assign_id()
            design.create(req.context, self.state_manager)

            resp.body = json.dumps(design.obj_to_simple())
            resp.status = falcon.HTTP_201
        except errors.StateError:
            self.error(req.context, "Error updating persistence")
            self.return_error(
                resp,
                falcon.HTTP_500,
                message="Error updating persistence",
                retry=True)
        except errors.InvalidFormat as fex:
            self.error(req.context, str(fex))
            self.return_error(
                resp, falcon.HTTP_400, message=str(fex), retry=False) 
Example 69
Project: drydock   Author: airshipit   File: base.py    Apache License 2.0 5 votes vote down vote up
def return_error(self, resp, status_code, message="", retry=False):
        resp.body = json.dumps({
            'type': 'error',
            'message': message,
            'retry': retry
        })
        resp.status = status_code 
Example 70
Project: drydock   Author: airshipit   File: base.py    Apache License 2.0 5 votes vote down vote up
def to_json(self, body_dict):
        """
        Thin wrapper around json.dumps, providing the default=str config
        """
        return json.dumps(body_dict, default=str) 
Example 71
Project: drydock   Author: airshipit   File: nodes.py    Apache License 2.0 5 votes vote down vote up
def on_get(self, req, resp):
        try:
            maas_client = MaasRequestFactory(
                config.config_mgr.conf.maasdriver.maas_api_url,
                config.config_mgr.conf.maasdriver.maas_api_key)

            machine_list = Machines(maas_client)
            machine_list.refresh()

            node_view = list()
            for m in machine_list:
                m.get_power_params()
                node_view.append(
                    dict(
                        hostname=m.hostname,
                        memory=m.memory,
                        cpu_count=m.cpu_count,
                        status_name=m.status_name,
                        boot_mac=m.boot_mac,
                        power_state=m.power_state,
                        power_address=m.power_parameters.get('power_address'),
                        boot_ip=m.boot_ip))

            resp.body = json.dumps(node_view)
            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 72
Project: flask-template   Author: pwgraham91   File: login_view.py    MIT License 4 votes vote down vote up
def callback():
    current_user = flask.g.user
    session = db.session

    # Redirect user to home page if already logged in.
    if current_user is not None and current_user.is_authenticated:
        return redirect(url_for('index'))
    if 'error' in request.args:
        if request.args.get('error') == 'access_denied':
            return 'You are denied access.'
        return 'Error encountered.'
    if 'code' not in request.args and 'state' not in request.args:
        return redirect(url_for('login'))
    else:
        # Execution reaches here when user has
        # successfully authenticated our app.
        google = get_google_auth(state=flask_session['oauth_state'])
        try:
            token = google.fetch_token(
                Auth.TOKEN_URI,
                client_secret=Auth.CLIENT_SECRET,
                authorization_response=request.url.replace('http://', 'https://'),
            )
        except HTTPError:
            return 'HTTPError occurred.'
        google = get_google_auth(token=token)
        # todo: cool stuff in here
        resp = google.get(Auth.USER_INFO)
        if resp.status_code == 200:
            user_data = resp.json()
            email = user_data['email']
            user = User.query.filter_by(email=email).first()
            if user is None:
                user = User(
                    name=user_data['name'] or user_data['email'].split('@')[0].capitalize(),
                    email=email,
                    avatar=user_data['picture']
                )
            user.tokens = json.dumps(token)
            session.add(user)
            session.commit()
            login_user(user)
            return redirect(url_for('index'))
        return 'Could not fetch your information.' 
Example 73
Project: fs_image   Author: facebookincubator   File: test_snapshot_repo.py    MIT License 4 votes vote down vote up
def test_snapshot(self):
        with temp_repos.temp_repos_steps(repo_change_steps=[{
            'dog': temp_repos.SAMPLE_STEPS[0]['dog'],
        }]) as repos_root, temp_dir() as td:
            with open(td / 'fake_gpg_key', 'w'):
                pass

            whitelist_dir = td / 'gpg_whitelist'
            os.mkdir(whitelist_dir)
            shutil.copy(td / 'fake_gpg_key', whitelist_dir)

            storage_dict = {
                'key': 'test',
                'kind': 'filesystem',
                'base_dir': (td / 'storage').decode(),
            }
            snapshot_repo([
                '--repo-name', 'dog',
                '--repo-url', (repos_root / '0/dog').file_url(),
                '--gpg-key-whitelist-dir', whitelist_dir.decode(),
                '--gpg-url', (td / 'fake_gpg_key').file_url(),
                '--snapshot-dir', (td / 'snap').decode(),
                '--storage', json.dumps(storage_dict),
                '--db', json.dumps({
                    'kind': 'sqlite',
                    'db_path': (td / 'db.sqlite3').decode(),
                }),
            ])
            # This test simply checks the overall integration, so we don't
            # bother looking inside the DB or Storage, or inspecting the
            # details of the snapshot -- those should all be covered 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({
                    '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";'
                    ).fetchall()
                }) 
Example 74
Project: fs_image   Author: facebookincubator   File: mount.py    MIT License 4 votes vote down vote up
def customize_fields(kwargs):  # noqa: B902
        target = kwargs.pop('target')
        cfg = kwargs.pop('mount_config')
        assert (target is None) ^ (cfg is None), \
            f'Exactly one of `target` or `mount_config` must be set in {kwargs}'
        if cfg is not None:
            cfg = cfg.copy()  # We must not mutate our input!
        else:
            with open(os.path.join(target, 'mountconfig.json')) as f:
                cfg = json.load(f)

        default_mountpoint = cfg.pop('default_mountpoint', None)
        if kwargs.get('mountpoint') is None:  # Missing or None => use default
            kwargs['mountpoint'] = default_mountpoint
            if kwargs['mountpoint'] is None:
                raise AssertionError(f'MountItem {kwargs} lacks mountpoint')
        coerce_path_field_normal_relative(kwargs, 'mountpoint')

        kwargs['is_directory'] = cfg.pop('is_directory')

        kwargs['build_source'] = _BuildSource(**cfg.pop('build_source'))
        if kwargs['build_source'].type == 'host' and not (
            kwargs['from_target'].startswith('//fs_image/features/host_mounts')
            or kwargs['from_target'].startswith('//fs_image/compiler/test')
        ):
            raise AssertionError(
                'Host mounts cause containers to be non-hermetic and fragile, '
                'so they must be located under `fs_image/features/host_mounts` '
                'to enable close review by the owners of `fs_image`.'
            )

        # This is supposed to be the run-time equivalent of `build_source`,
        # but for us it's just an opaque JSON blob that the runtime wants.
        # Hack: We serialize this back to JSON since the compiler expects
        # items to be hashable, and the source WILL contain dicts.
        runtime_source = cfg.pop('runtime_source', None)
        # Future: once runtime_source grows a schema, use it here?
        if (runtime_source and runtime_source.get('type') == 'host'):
            raise AssertionError(
                f'Only `build_source` may specify host mounts: {kwargs}'
            )
        kwargs['runtime_source'] = json.dumps(runtime_source, sort_keys=True)

        assert cfg == {}, f'Unparsed fields in {kwargs} mount_config: {cfg}'
        # These must be set to appease enriched_namedtuple
        kwargs['target'] = None
        kwargs['mount_config'] = None 
Example 75
Project: Coulomb   Author: DynamoDS   File: sessions_to_sorted_deduped_sessions.py    MIT License 4 votes vote down vote up
def sort_blob(blob_name, sorted_blob_name, temp_path):
    log("Sorting: {} => {}".format(blob_name, sorted_blob_name))

    blob_session_name = blob_name.split('/')[-1]
    blob_session_name_sorted = sorted_blob_name.split('/')[-1]

    blob_path_to_proc = os.path.join(temp_path, blob_session_name)
    out_path = os.path.join(temp_path, blob_session_name_sorted)
    
    log ("Downloading {} => {}".format(blob_name, blob_path_to_proc))
    download_blob(SESSIONS_BUCKET, blob_name, blob_path_to_proc)

    f = gzip.open(blob_path_to_proc)
    data_set = set()
    data = []
    byte_counter = 0
    skip_file = False

    for ln in f:
        data_set.add(ln)
        byte_counter += sys.getsizeof(ln)

        if byte_counter > MAX_MEM:
            skip_file = True
            log ("Skipped large file: " + blob_path_to_proc)
            break

    if skip_file:
        os.remove(blob_path_to_proc)
        return False

    for ln in data_set:
        data.append(json.loads(ln))

    data.sort(key=lambda x: int(x["MicroTime"]))
    sortedF = gzip.open(out_path, 'w')
    for d in data:
        sortedF.write((json.dumps(d) + "\n").encode('utf-8'))

    sortedF.flush()
    sortedF.close()
    log("Sorted: {} => {}".format(blob_session_name, blob_session_name_sorted))
    upload_blob(SESSIONS_BUCKET, out_path, sorted_blob_name)
    
    log("About to remove: {}".format(blob_path_to_proc))
    os.remove(blob_path_to_proc)
    
    log("About to remove: {}".format(out_path))
    os.remove(out_path)

    return True 
Example 76
Project: hanuman   Author: sunlightlabs   File: train.py    BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def train_page_classifier(self, firm):
        fts = FirmTrainingSet.get_for_firm(firm)
        spider = Spider(firm.domain, os.path.join(settings.MODEL_DIR, 'scrape', str(fts.id) + ".db"), workers=4, retry_attempts=2)

        self.stdout.write("Page classifier:\n")

        model = PageClassifier(os.path.join(settings.MODEL_DIR, 'model', str(fts.id) + "_page.tgm"))

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

        bio_pages = set()
        non_bio_pages = set()
        for view_log in ViewLog.objects.filter(firm=firm, session__user__collectionsettings__is_test_user=False):
            bio_pages.update(view_log.bio_pages)
            non_bio_pages.update(view_log.non_bio_pages)

        for cat, pages in (('bio', bio_pages), ('nonbio', non_bio_pages)):
            for url in pages:
                self.stdout.write(' * ' + url)
                try:
                    page = spider.get(url)
                except:
                    continue
                if (page.text.strip()):
                    model.add_page(page, cat)

        self.stdout.write('Done (processed %s pages).\n' % (len(bio_pages) + len(non_bio_pages)))

        self.stdout.write('Training... ')
        model.train()
        model.save()
        self.stdout.write('done.\n')

        self.stdout.write('Testing classifier on training data...\n')
        total_count = 0
        correct_count = 0
        for cat, pages in (('bio', bio_pages), ('nonbio', non_bio_pages)):
            for url in pages:
                try:
                    page = spider.get(url)
                except:
                    continue
                if not page.text.strip():
                    continue

                prediction = str(model.predict(page))
                print url, json.dumps(cat), json.dumps(prediction), "true" if cat == prediction else "false"

                total_count += 1
                correct_count += 1 if cat == prediction else 0

        self.stdout.write('Accuracy: %s%%; ' % int(round(100 * float(correct_count) / total_count)))
        if float(correct_count) / total_count >= 0.8:
            self.stdout.write('continuing...\n')
            fts.page_classifier_trained = True
            fts.save()
        else:
            self.stdout.write('giving up.\n')
            raise CommandError('Page classifier training failed.') 
Example 77
Project: backtrader-cn   Author: pandalibin   File: sina.py    GNU General Public License v3.0 4 votes vote down vote up
def get_today_orders(self, status=None):
        """
        获取当日委托,
        :param status: 委托状态: "0": 未成交委托, "1": 成交的委托, "2":已撤销委托,默认返回当天所有委托
        :return:
        [Order(og_id='120350', contest_id='10000', sid='5175517774', StockCode='sz000651', StockName='格力电器', SellBuy='0', OrderPrice='47.800', DealAmount='100', OrderAmount='100', IfDealt='2', OrderTime='2017-11-21 17:30:10', mtime='2017-11-21 17:33:40')]

        {
            "data": [
                {
                    "og_id": "120350",  # 委托ID
                    "contest_id": "10000", # 比赛ID
                    "sid": "5175517774",
                    "StockCode": "sz000651",
                    "StockName": "格力电器",
                    "SellBuy": "0",   # 0表示买入
                    "OrderPrice": "47.800",
                    "DealAmount": "100",
                    "OrderAmount": "100",
                    "IfDealt": "2", # 成交状态: 0: 未成交,2: 已撤销
                    "OrderTime": "2017-11-21 17:30:10", # 委托创建时间
                    "mtime": "2017-11-21 17:33:40"
                },
        """
        if isinstance(status, int):
            status = str(status)
        from_position = 0
        per_page = 10
        obj_stocks = []
        while True:
            json_stocks = self._query_orders(from_position, per_page)
            for stock in json_stocks['data']:
                stock['_class_name'] = 'Order'
                if status is None:
                    obj_stocks.append(json2obj(json.dumps(stock)))
                elif stock['IfDealt'] == status:
                    obj_stocks.append(json2obj(json.dumps(stock)))
            if from_position + per_page >= int(json_stocks['count']):
                break
            from_position += per_page

        return obj_stocks 
Example 78
Project: backtrader-cn   Author: pandalibin   File: sina.py    GNU General Public License v3.0 4 votes vote down vote up
def get_stock_hold(self):
        """
        获取当前持仓

        {
            "data": [
                {
                    "sg_id": "117989",
                    "contest_id": "10000",
                    "sid": "1768615155",
                    "zs_price": null,
                    "zy_price": null,
                    "HoldPercent": 0.6,   # 仓位
                    "StockCode": "sz000001",
                    "StockName": "平安银行",
                    "StockAmount": "200", # 当前持股
                    "AvailSell": "0",     # 可用股数
                    "TodayBuy": "200",
                    "TodaySell": "0",
                    "T4AvailSell": "0",
                    "T3AvailSell": "0",
                    "T2AvailSell": "0",
                    "T1AvailSell": "0",
                    "CurrentValue": null,
                    "cost": "14.920",    # 持仓成本
                    "StartDate": "2017-11-22 10:24:36",
                    "EndDate": "0000-00-00 00:00:00",
                    "mtime": "0000-00-00 00:00:00",
                    "CostFund": "2983.980", # 持仓花费金额
                    "newcost": 14.99,    # 最新价格
                    "dealvalue": "2983.980",
                    "newvalue": 2998,   # 持股市值
                    "profit": 14,       # 浮动盈亏
                    "profitRate": 0.47   # 盈亏比例
                }
            ],
            "count": "1",
            "status": "2"
        }
        """
        from_position = 0
        per_page = 10
        obj_stocks = []
        while True:
            json_stocks = self._query_stock_hold(from_position, per_page)
            for stock in json_stocks['data']:
                obj_stocks.append(json2obj(json.dumps(stock)))
            if from_position + per_page >= int(json_stocks['count']):
                break
            from_position += per_page

        return obj_stocks 
Example 79
Project: drydock   Author: airshipit   File: designs.py    Apache License 2.0 4 votes vote down vote up
def on_post(self, req, resp, design_id):
        ingester_name = req.params.get('ingester', None)

        if ingester_name is None:
            self.error(
                None,
                "DesignsPartsResource POST requires parameter 'ingester'")
            self.return_error(
                resp,
                falcon.HTTP_400,
                message="POST requires parameter 'ingester'",
                retry=False)
        else:
            try:
                raw_body = req.stream.read(req.content_length or 0)
                if raw_body is not None and len(raw_body) > 0:
                    parsed_items = self.ingester.ingest_data(
                        plugin_name=ingester_name,
                        design_state=self.state_manager,
                        content=raw_body,
                        design_id=design_id,
                        context=req.context)
                    resp.status = falcon.HTTP_201
                    resp.body = json.dumps(
                        [x.obj_to_simple() for x in parsed_items])
                else:
                    self.return_error(
                        resp,
                        falcon.HTTP_400,
                        message="Empty body not supported",
                        retry=False)
            except ValueError:
                self.return_error(
                    resp,
                    falcon.HTTP_500,
                    message="Error processing input",
                    retry=False)
            except LookupError:
                self.return_error(
                    resp,
                    falcon.HTTP_400,
                    message="Ingester %s not registered" % ingester_name,
                    retry=False) 
Example 80
Project: drydock   Author: airshipit   File: designs.py    Apache License 2.0 4 votes vote down vote up
def on_get(self, req, resp, design_id):
        try:
            design = self.state_manager.get_design(design_id)
        except errors.DesignError:
            self.return_error(
                resp,
                falcon.HTTP_404,
                message="Design %s nout found" % design_id,
                retry=False)

        part_catalog = []

        site = design.get_site()

        part_catalog.append({'kind': 'Region', 'key': site.get_id()})

        part_catalog.extend([{
            'kind': 'Network',
            'key': n.get_id()
        } for n in design.networks])

        part_catalog.extend([{
            'kind': 'NetworkLink',
            'key': l.get_id()
        } for l in design.network_links])

        part_catalog.extend([{
            'kind': 'HostProfile',
            'key': p.get_id()
        } for p in design.host_profiles])

        part_catalog.extend([{
            'kind': 'HardwareProfile',
            'key': p.get_id()
        } for p in design.hardware_profiles])

        part_catalog.extend([{
            'kind': 'BaremetalNode',
            'key': n.get_id()
        } for n in design.baremetal_nodes])

        resp.body = json.dumps(part_catalog)
        resp.status = falcon.HTTP_200
        return