<commit_before>"""
***************************
Synapse command line client
***************************

The Synapse client can be used from the command line via the **synapse**
command. For help, type::

    synapse -h.

For help on commands, type::

    synapse [command] -h


Optional arguments
==================

.. code-block:: shell

    -h, --help            show this help message and exit
    --version             show program's version number and exit
    -u SYNAPSEUSER, --username SYNAPSEUSER
                        Username used to connect to Synapse
    -p SYNAPSEPASSWORD, --password <PASSWORD>
                        Password used to connect to Synapse

Commands
========
  * **login**            - login to Synapse and (optionally) cache credentials
  * **get**              - download an entity and associated data
  * **store**            - create or update
  * **delete**           - removes a dataset from Synapse
  * **query**            - performs SQL like queries on Synapse
  * **submit**           - submit an entity for evaluation
  * **set-provenance**   - create provenance records
  * **get-provenance**   - show provenance records
  * **show**             - show metadata for an entity
  * **onweb**            - opens Synapse website for Entity

A few more commands (show, cat, add, create, update) exist, mostly for backwards compatibility.

"""

import argparse
import os
import shutil
import sys
import synapseclient
from synapseclient import Activity
import utils
import signal
import json


def query(args, syn):
    ## TODO: Should use loop over multiple returned values if return is too long
    results = syn.chunkedQuery(' '.join(args.queryString))

    headings = {}
    temp = [] # Since query returns a generator, the results must be stored locally
    for res in results:
        temp.append(res)
        for head in res:
            headings[head] = True
    if len(headings) == 0: # No results found
        return 
    print '\t'.join(headings)
    
    for res in temp:
        out = []
        for key in headings:
            out.append(str(res.get(key, "")))
        print "\t".join(out)

        
def get(args, syn):
    entity = syn.get(args.id)
    
    ## TODO: Is this part even necessary?
    ## (Other than the print statements)<commit_msg>Fixes IOError when piping query output<commit_after>"""
***************************
Synapse command line client
***************************

The Synapse client can be used from the command line via the **synapse**
command. For help, type::

    synapse -h.

For help on commands, type::

    synapse [command] -h


Optional arguments
==================

.. code-block:: shell

    -h, --help            show this help message and exit
    --version             show program's version number and exit
    -u SYNAPSEUSER, --username SYNAPSEUSER
                        Username used to connect to Synapse
    -p SYNAPSEPASSWORD, --password <PASSWORD>
                        Password used to connect to Synapse

Commands
========
  * **login**            - login to Synapse and (optionally) cache credentials
  * **get**              - download an entity and associated data
  * **store**            - create or update
  * **delete**           - removes a dataset from Synapse
  * **query**            - performs SQL like queries on Synapse
  * **submit**           - submit an entity for evaluation
  * **set-provenance**   - create provenance records
  * **get-provenance**   - show provenance records
  * **show**             - show metadata for an entity
  * **onweb**            - opens Synapse website for Entity

A few more commands (show, cat, add, create, update) exist, mostly for backwards compatibility.

"""

import argparse
import os
import shutil
import sys
import synapseclient
from synapseclient import Activity
import utils
import signal
import json


def query(args, syn):
    signal.signal(signal.SIGPIPE, signal.SIG_DFL)
    ## TODO: Should use loop over multiple returned values if return is too long
    results = syn.chunkedQuery(' '.join(args.queryString))

    headings = {}
    temp = [] # Since query returns a generator, the results must be stored locally
    for res in results:
        temp.append(res)
        for head in res:
            headings[head] = True
    if len(headings) == 0: # No results found
        return 
    sys.stdout.write('%s\n' %'\t'.join(headings))
    
    for res in temp:
        out = []
        for key in headings:
            out.append(str(res.get(key, "")))
        sys.stdout.write('%s\n' % "\t".join(out))

        
def get(args, syn):
    entity = syn.get(args.id)
    
    ## TODO: Is this part even necessary?
    ## (Other than the print statements)
{'fraction_non_alphanumeric': 0.125, 'fraction_numerical': 0.000423728813559322, 'mean_word_length': 2.376967095851216, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5493282', 'n_tokens_mistral': 1290, 'n_tokens_neox': 1246, 'n_words': 494}