Beispiel #1
0
 def __init__(self, filename=None, configText=None, type='pw'):
     self.namelists = OrderedDict()
     self.cards = OrderedDict()
     self.attach = None
     self.filename = filename
     self.configText = configText
     self.namelistRef = None
     self.cardRef = None
     self.type = type
Beispiel #2
0
 def __init__(self, filename=None, config=None, type='pw'):
     self.filename   = filename
     self.config     = config
     self.parser     = QEParser(filename, config, type)
     self.type       = type
     self.namelists  = OrderedDict()
     self.cards      = OrderedDict()
     self.attach     = None          # Specific for 'matdyn'
     self.namelistRef    = None
     self.cardRef        = None
     self.qe         = [self.namelists, self.cards, self.attach]
Beispiel #3
0
    def _get_atomicSpecies(self):
        atomicSpecies = OrderedDict()
        for a in self:
            atomicSpecies[a.element] = AtomicSpecies(element = a.element, \
                                        mass = a.mass, potential = a.potential)

        return atomicSpecies
    def __init__(self, filename=None, config=None, parse=True):
        self._filename = filename
        self._config = config
        # OrderedDict?
        # Header
        self._headerstr = ""  # Non-parsed header
        self._header = OrderedDict()  # Parsed header
        self._inputparams = OrderedDict()  # Dictionary of input parameters
        self._outputparams = OrderedDict()  # Dictionary of output parameters

        # Body
        self._sections = {}  # Sections
        self._defs = {}  # Definitions

        if parse and (self._fileExists() or config):
            self.parse()
Beispiel #5
0
    def _getCards(self, rawlist):
        cards       = OrderedDict()
        cardName    = None
        for l in rawlist:
            isCardName  = False
            p   = re.compile(CARD)
            m   = p.match(l)
            if m is not None:       # If card name matches
                firstPart   = m.group(1).lower()
                secondPart  = m.group(2).strip().lower()    # Catch argument of the card
                if firstPart in self.cardRef:
                    isCardName  = True
                    cardName    = firstPart
                    cards[cardName]    = {}

                    if (secondPart != ''):
                        cards[cardName]["args"] = secondPart
                    else:
                        cards[cardName]["args"] = None
                    cards[cardName]["values"]   = []

            if cardName is not None and not isCardName:
                cards[cardName]["values"].append(l)

        return cards
Beispiel #6
0
 def __init__(self, filename = None, configText = None, type = 'pw'):
     """
         filename:   (str) -- Absolute or relative filename of file to be parsed
         configText: (str) -- Configuration text to be parsed
         type:       (str) -- Type of the simulation
     """
     
     self.header     = None
     self.namelists  = OrderedDict()     # Namelist dictionary
     self.cards      = OrderedDict()     # Cards dictionary
     self.attach     = None
     self.filename   = filename
     self.configText = configText
     self.namelistRef    = None
     self.cardRef        = None
     self.type           = type.lower()
Beispiel #7
0
 def __init__(self, filename = None, configText = None, type = 'pw'):
     """
     Parameters:
         filename    -- absolute or relative filename to be parsed
         configText  -- configuration text to be parsed
         type        -- type of the simulation
     """
     
     self.header     = None
     self.namelists  = OrderedDict()     # Namelist dictionary
     self.cards      = OrderedDict()     # Cards dictionary
     self.attach     = None
     self.filename   = filename
     self.configText = configText
     self.namelistRef    = None
     self.cardRef        = None
     self.type           = type
Beispiel #8
0
def returnError(metadataName, rootName, duration, message):
    metadatadict = OrderedDict([("duration", str(duration)),
                                ("error", message), ("idProperty", None),
                                ("successProperty", 'success'),
                                ("totalProperty", 'recordCount'),
                                ("success", False), ("recordCount", 0),
                                ("root", None), ("fields", None)])
    responsejson = json.dumps(dict([(metadataName, metadatadict),
                                    (rootName, None)]),
                              indent=1)
    return getJsonResponse(responsejson)
Beispiel #9
0
def getValuesForPoint(sceneid, x, y, crs):
    try:
        authenticate()
        lng, lat = utilities.getPointLL(x, y, crs)
        scene = ee.Image(sceneid)
        collection = ee.ImageCollection(scene)
        data = collection.getRegion(ee.Geometry.Point(lng, lat), 30).getInfo()
        if len(data) == 1:
            raise GoogleEarthEngineError("No values for point x:" + str(x) + " y: " + str(y) + " for sceneid: " + sceneid)
        return OrderedDict([(data[0][i], data[1][i]) for i in range (len(data[0]))])
    
    except (EEException, GoogleEarthEngineError):
        return "Google Earth Engine Error: " + str(sys.exc_info())        
Beispiel #10
0
 def __init__(self, qeConf):
     """the structure is initialized from PWSCF config file
        'lattice' and 'structure' are automatically updated"""
     self.filename = qeConf.filename
     self.atomicSpecies = OrderedDict()
     self.formatString = '%# .8f %# .8f %# .8f'
     # optConstraints three 1/0 for each coordinate of each atom
     self.optConstraints = []
     self.qeConf = qeConf
     self.lattice = QELattice()
     self.structure = Structure(lattice=self.lattice.diffpy())
     self.nat = None
     self.ntyp = None
Beispiel #11
0
    def __init__(self, filename=None, config=None, type='pw', parse=True):
        """
        Initializes QEInput by passing either filename or config (not both)
        parameters
        
            filename:   (str) -- Absolute or relative filename of file to be parsed
            config:     (str) -- Configuration text to be parsed
            type:       (str) -- Type of the simulation
        """
        self.filename       = filename
        self.config         = config
        self._type          = type.lower()
        self.filters        = []
        self.header         = None
        self.namelists      = OrderedDict()
        self.cards          = OrderedDict()
        self.attach         = None          # Specific for 'matdyn', 'dynmat', etc.
        self.qe             = None          # DEPRICATED
        self.namelistRef    = None
        self.cardRef        = None

        self._read(filename, config, parse)
Beispiel #12
0
 def setStructureFromQEInput(self):
     """ Loads structure from PWSCF config file"""
     self.atomicSpecies = OrderedDict()
     self.lattice.setLatticeFromPWInput(self.qeConf)
     #self.lattice = QELattice(qeConf = self.qeConf)
     self.structure = Structure(lattice = self.lattice.diffpy())
     self.nat = self.ntyp = None
     self.filename = self.qeConf.filename
     self.optConstraints = []
     
     if 'system' in self.qeConf.namelists:        
         self.nat  = int(self.qeConf.namelist('system').param('nat'))
         self.ntyp  = int(self.qeConf.namelist('system').param('ntyp'))
     if 'atomic_positions' in self.qeConf.cards:        
         atomicLines = self.qeConf.card('atomic_positions').lines()
         self.atomicPositionsType = self.qeConf.card('atomic_positions').arg()
         if self.atomicPositionsType == 'bohr' or self.atomicPositionsType == 'angstrom':
             raise NotImplementedError
         if self.atomicPositionsType == None:
             self.atomicPositionsType = 'alat'
         for line in atomicLines:
             if '!' not in line:
                 words = line.split()
                 coords = [float(w) for w in words[1:4]]
                 constraint = []
                 if len(words) > 4:
                     constraint = [int(c) for c in words[4:7]]
                 self.optConstraints.append(numpy.array(constraint, dtype = int))
                 atomSymbol = words[0]
                 if self.atomicPositionsType == 'alat':
                     coords = self.lattice.diffpy().fractional(numpy.array(coords[0:3])*self.lattice.a0)
                 if self.atomicPositionsType == 'crystal':
                     coords = numpy.array(coords[0:3])
                 self.structure.addNewAtom(atomSymbol, xyz = numpy.array(coords[0:3]))
     # parse mass ATOMIC_SPECIES section:
      
     if 'atomic_species' in self.qeConf.cards:
         atomicSpeciesLines = self.qeConf.card('atomic_species').lines()
         for line in atomicSpeciesLines:
             if '!' not in line:
                 if line.strip() != '':                            
                     atomicSpeciesWords = line.split()
                     element = atomicSpeciesWords[0]
                     mass = 0
                     ps = ''
                     if len(atomicSpeciesWords) > 1 :
                         mass = float(atomicSpeciesWords[1])
                     if len(atomicSpeciesWords) > 2:
                         ps = atomicSpeciesWords[2]
                     self.atomicSpecies[element] =  AtomicSpecies(element, mass, ps)
Beispiel #13
0
    def _parseNamelists(self, text):
        namelists = OrderedDict()
        p = re.compile(COMMENT)
        s1 = re.sub(p, '', text)  # Remove comments
        p2 = re.compile(NAMELIST)
        matches = p2.findall(s1)  # Finds all namelist blocks
        for m in matches:
            name = m[0].lower()
            if name in self.namelistRef:
                params = self._parseParams(
                    m[1])  # Parse parameters from a namelist block
                namelists[name.lower()] = params

        self._convertNamelists(namelists)
Beispiel #14
0
    def _parseNamelists(self, text):
        """Parses text and populates namelist dictionary"""

        namelists   = OrderedDict()
        s1          = self._removeComments(text)
        p2          = re.compile(NAMELIST)
        matches     = p2.findall(s1)        # Finds all namelist blocks
        
        for m in matches:
            name    = m[0].lower()
            if name in self.namelistRef:
                params  = self._parseParams(m[1])     # Parse parameters from a namelist block
                namelists[name.lower()] = params

        self._convertNamelists(namelists)
Beispiel #15
0
def generate():
    inp = open("pseudo.list")
    # Generate structure
    pseudo = OrderedDict()
    str = inp.read()
    pseudolist = str.split()

    for name in pseudolist:
        l = label(name)
        if l not in pseudo.keys():  # key does not exist
            pseudo[l] = []

        pseudo[l].append(name)

    out = open("pseudo.py", "w")
    out.write(toString(pseudo))
    inp.close()
    out.close()
    print "Done!"
Beispiel #16
0
#!/usr/bin/env python
# Autogenerated script by pseudo-generator.py
from orderedDict import OrderedDict

PSEUDO = OrderedDict()
PSEUDO["Ag"] = (
    "Ag.pbe-d-rrkjus.UPF",
    "Ag.pz-d-rrkjus.UPF",
)

PSEUDO["Al"] = (
    "Al.blyp-n-van_ak.UPF",
    "Al.bp-n-van_ak.UPF",
    "Al.pbe-n-van.UPF",
    "Al.pbe-rrkj.UPF",
    "Al.pbe-sp-van.UPF",
    "Al.pw91-n-van.UPF",
    "Al.pz-vbc.UPF",
)

PSEUDO["Ar"] = ("Ar.pz-rrkj.UPF", )

PSEUDO["As"] = (
    "As.pbe-n-van.UPF",
    "As.pw91-n-van.UPF",
    "As.pz-bhs.UPF",
)

PSEUDO["Au"] = (
    "Au.blyp-van_ak.UPF",
    "Au.bp-van_ak.UPF",
Beispiel #17
0
    def setStructureFromPWOutput(self, pwscfOutputFile):
        """
        Loads structure from PWSCF output file. If there was geometry
        optimization (relax or vc-relax), the structure will be reinitialized
        from the last step of the optimization
        """
        file = open(pwscfOutputFile)
        pwscfOut = file.readlines()
        pseudoList = []
        atomList = []
        massList = []
        self.atomicSpecies = OrderedDict()
        self.atomicPositionsType = 'alat'
        # parse beginning:
        for i, line in enumerate(pwscfOut):
            if 'lattice parameter (a_0)' in line:
                a_0 = float(line.split()[4])
            if 'bravais-lattice index' in line:
                ibrav = int(line.split('=')[1])
            if 'number of atoms/cell' in line:
                self.nat = int(line.split('=')[1])
            if 'number of atomic types' in line:
                self.ntyp = int(line.split('=')[1])
            if 'PseudoPot.' in line:
                pseudoList.append(line.split('read from file')[1].strip())
            if 'atomic species   valence    mass     pseudopotential' in line:
                for j in range(self.ntyp):
                    atomList.append(pwscfOut[i + j + 1].split()[0])
                    massList.append(float(pwscfOut[i + j + 1].split()[2]))
            if 'crystal axes: (cart. coord. in units of a_0)' in line:
                latticeVectors = [
                    [float(f) * a_0 for f in pwscfOut[i + 1].split()[3:6]],
                    [float(f) * a_0 for f in pwscfOut[i + 2].split()[3:6]],
                    [float(f) * a_0 for f in pwscfOut[i + 3].split()[3:6]]
                ]
                self.lattice.setLatticeFromQEVectors(ibrav, latticeVectors)
            if 'site n.     atom                  positions (a_0 units)' in line:
                self.structure = Structure(lattice=self.lattice.diffpy())
                for n in range(self.nat):
                    words = pwscfOut[i + n + 1].split()
                    atomSymbol = words[1]
                    coords = [float(w) for w in words[6:9]]
                    constraint = []
                    self.optConstraints.append(
                        numpy.array(constraint, dtype=int))
                    #print numpy.array(coords[0:3])*a_0
                    coords = self.lattice.diffpy().fractional(
                        numpy.array(coords[0:3]) * a_0)
                    self.structure.addNewAtom(atomSymbol,
                                              xyz=numpy.array(coords[0:3]))

        for a, m, p in zip(atomList, massList, pseudoList):
            self.atomicSpecies[a] = AtomicSpecies(a, m, p)

        #print 'Input structure from output file: ', self.toString()
        #Parse end:
        # Find all geometry optimization steps
        posList = [
            i for i, line in enumerate(pwscfOut) if '!    total energy' in line
        ]
        lastSection = pwscfOut[posList[-1]:]
        for i, line in enumerate(lastSection):
            if 'CELL_PARAMETERS (alat)' in line:
                latticeVectors = [
                    [float(f) * a_0 for f in lastSection[i + 1].split()],
                    [float(f) * a_0 for f in lastSection[i + 2].split()],
                    [float(f) * a_0 for f in lastSection[i + 3].split()]
                ]
                self.lattice.setLatticeFromQEVectors(ibrav, latticeVectors)
                #self.lattice = QELattice(ibrav = ibrav, base = latticeVectors)
                print self.lattice.diffpy().base
            if 'ATOMIC_POSITIONS (alat)' in line:
                self.structure = Structure(lattice=self.lattice.diffpy())
                for n in range(self.nat):
                    words = lastSection[i + n + 1].split()
                    atomSymbol = words[0]
                    coords = [float(w) for w in words[1:4]]
                    constraint = []
                    if len(words) > 4:
                        constraint = [int(c) for c in words[4:7]]
                    self.optConstraints.append(
                        numpy.array(constraint, dtype=int))
                    coords = self.lattice.diffpy().fractional(
                        numpy.array(coords[0:3]) * a_0)
                    self.structure.addNewAtom(atomSymbol,
                                              xyz=numpy.array(coords[0:3]))
Beispiel #18
0
 def __init__(self, name):
     """
         name: (str) -- Name of the namelist in lower case. Example: "control"
     """
     self._name = name.lower()  # keeps lower name
     self._params = OrderedDict()  # Replace dictionary by ordered dictionry
Beispiel #19
0
def callservice(conn, schemaname, servicename, querystring):
    try:
        t1 = datetime.datetime.now()
        # log the request
        logging.basicConfig(
            filename='../../htdocs/mstmp/REST_Services_Log.log',
            level=logging.INFO,
            format='%(asctime)s %(levelname)s %(message)s',
        )
        #         logging.info("REST REQUEST: " + web.ctx.home + web.ctx.path + web.ctx.query)
        # PARSE THE STANDARD OPTIONAL INPUT PARAMETERS
        # get the input parameters
        params = getQueryStringParams(
            querystring
        )  # the unquoting is to handle encoded parameters (like from extJS - 1,2,3 as a parameter becomes 1%2C2%2C3
        # get the standard optional parameters from the url
        format = params.setdefault('format', 'json')
        fields = params.setdefault('fields', '').split(
            ","
        )  # fields will be passed as an array, e.g. iucn_species_id,wdpa_id
        includemetadata = params.setdefault('includemetadata', 'true')
        metadataName = params.setdefault('metadataname', 'metadata')
        rootName = params.setdefault('rootname', 'records')
        parseparams = params.setdefault('parseparams', 'true')
        sortField = params.setdefault('sortfield', '')
        decimalPlaceLimit = params.setdefault('dplimit', '2')
        isHadoop = (
            'true' if (servicename[-2:] == '_h') else 'false'
        )  # if the service is a call to a hadoop method then set a flag

        # remove the standard optional parameters from the dictionary so we are left with just the parameters required for the function
        del (params['format'], params['fields'], params['includemetadata'],
             params['parseparams'], params['metadataname'], params['rootname'],
             params['sortfield'], params['dplimit'])
        if 'callback' in params.keys():
            del (params['callback'])
        # check if the service name is valid
        if not (isValidServiceName(servicename)):
            raise RESTServicesError('Invalid servicename')

        # authorise with ecas if needed


#         if requiresAuthentication(servicename):
#             if isAuthenticated() == False:
#                 web.ctx.status = '401 Unauthorized'
#                 web.header("Content-Type", "text/html")
#                 return "<html><head></html><body><h1>Authentication required</h1></body></html>"

# if it is a Hadoop query then we need to run if first before we actually use the values to get the data from postgresql
        if (isHadoop.lower() == 'true'):
            hadoopData = runHadoopQuery(conn, servicename, params)
            if hadoopData == '[]': hadoopData = '[-1]'
            servicename = "_" + servicename  # now call the postgresql function
            params.clear()
            params['species_ids'] = str(hadoopData)[1:-1]

        # PARSE AND CONVERT THE DATA TYPES OF THE OTHER INPUT PARAMETERS
        # get all the parameters for the function from postgresql
        conn.cur.callproc('utils.dopa_rest_getparams', [servicename])
        # get the function parameters as a string and split this into a list, e.g. wdpa_id integer, presence_id integer[] -->  ['wdpa_id integer', ' presence_id integer[]']
        functionparams = conn.cur.fetchone()
        hasparams = True if functionparams[0] else False
        if hasparams:
            functionparams = functionparams[0].split(',')
            # get the names of the function parameters which are array types
            arrayparamnames = [
                p.strip().split(" ")[0] for p in functionparams if '[' in p
            ]
            # convert the array values into lists
            for key in params.keys():
                if key in arrayparamnames:
                    strlist = params[key].split(",")
                    isnum = isNumeric(strlist[0])
                    if isnum:
                        params[key] = [int(s) for s in strlist]
                    else:
                        params[key] = strlist
            # get the full list of function parameter names
            functionparamnames = [
                p.strip().split(" ")[0] for p in functionparams
            ]
            # check that all parameters are correct
            invalidparamnames = [
                n for n in params.keys() if n not in functionparamnames
            ]
            if invalidparamnames and parseparams == 'true':
                raise RESTServicesError('Invalid parameters: ' +
                                        ",".join(invalidparamnames))
            # put the input parameters in the right order
            params = OrderedDict([(n, params[n]) for n in functionparamnames
                                  if n in params.keys()])

        # GET THE SORT CLAUSE
        if sortField != "":
            sortClause = ' ORDER BY "' + sortField + '"'
        else:
            sortClause = ""

        # GET THE FIELDS CLAUSE
        if fields != ['']:
            fieldsClause = ",".join(fields)
        else:
            fieldsClause = "*"

        # RUN THE QUERY
        if hasparams:
            sql = "SELECT " + fieldsClause + " from " + schemaname + "." + servicename + "(" + ",".join(
                [n + ":=%(" + n + ")s" for n in params]
            ) + ")" + sortClause + ";"  # run the query using named parameters
            conn.cur.execute(sql, params)
        else:
            sql = "SELECT * from " + schemaname + "." + servicename + "()" + sortClause + ";"
            conn.cur.execute(sql)
        rows = conn.cur.fetchall()

        # PROCESS THE ROWS AND WRITE THEM BACK TO THE CLIENT
        conn.cur.close()
        t2 = datetime.datetime.now()

        # METADATA SECTION OF RESPONSE
        allfields = [d.name for d in conn.cur.description]
        if (fields == ['']): fields = allfields
        fieldcount = len(fields)
        fieldsdict = [
            dict([("name", d.name),
                  ("type", gettypefromtypecode(d.type_code))])
            for d in conn.cur.description if (d.name in fields)
        ]
        if len(fieldsdict) != len(fields):
            raise RESTServicesError('Invalid output fields')
        metadatadict = OrderedDict([
            ("duration", str(t2 - t1)), ("error", None),
            ("idProperty", conn.cur.description[0].name),
            ("successProperty", 'success'), ("totalProperty", 'recordCount'),
            ("success", True), ("recordCount", int(conn.cur.rowcount)),
            ("root", rootName), ("fields", fieldsdict)
        ])

        # RECORDS SECTION OF THE RESPONSE
        # parse the float values and set the correct number of decimal places according to the decimalPlaceLimit variable - dont include lat/long fields as these must have more decimal places
        floatColumns = [
            i for i, d in enumerate(fieldsdict)
            if d['type'] == 'float' and d['name'] not in ['lat', 'lng']
        ]
        if len(floatColumns) > 0:
            for floatColumn in floatColumns:
                for row in rows:
                    if type(row[floatColumn]
                            ) != NoneType:  # check that the data is not null
                        row[floatColumn] = round(row[floatColumn],
                                                 int(decimalPlaceLimit))

        # return the data
        colsRequired = [allfields.index(field) for field in fields]
        if format in ['json', 'array']:
            if format == 'json':
                recordsdict = [
                    OrderedDict([(allfields[col], row[col])
                                 for col in range(fieldcount)
                                 if (col in colsRequired)]) for row in rows
                ]
            else:
                recordsdict = [[
                    row[col] for col in range(fieldcount)
                    if (col in colsRequired)
                ] for row in rows]
            json.encoder.FLOAT_REPR = lambda f: (
                "%.14g" % f
            )  # this specifies how many decimal places are returned in the json with float values - currently set to 14 - good enough for returning lat/long coordinates
            if (includemetadata.lower() == 'true'):
                responsejson = json.dumps(dict([(metadataName, metadatadict),
                                                (rootName, recordsdict)]),
                                          indent=1,
                                          cls=CustomJSONEncoder)
            else:
                responsejson = json.dumps(dict([(rootName, recordsdict)]),
                                          indent=1,
                                          cls=CustomJSONEncoder)
            return getJsonResponse(responsejson)

        elif format in ['xml', 'xmlverbose']:
            root = etree.Element('results')
            recordsnode = etree.Element(rootName)
            recordsdicts = [
                OrderedDict([
                    (allfields[col], str(row[col]).decode('utf-8'))
                    for col in range(fieldcount)
                    if (col in colsRequired) and str(row[col]) != 'None'
                ]) for row in rows
            ]  #
            if format == 'xml':
                recordselements = [
                    etree.Element('record', element)
                    for element in recordsdicts
                ]
                for recordelement in recordselements:
                    recordsnode.append(recordelement)
            else:
                for recordelement in recordsdicts:
                    record = etree.Element('record')
                    for (n, v) in recordelement.items():
                        el = etree.Element(n)
                        el.text = v
                        record.append(el)
                    recordsnode.append(record)
            root.append(recordsnode)
            web.header("Content-Type", "text/xml")
            #             web.header("Content-Type", "application/Excel") # doesnt work!
            #             web.header("Content-Disposition", "attachment; filename=test.xml")
            return etree.tostring(root)

        elif format == 'sms':
            _twilio = twilio()
            client = TwilioRestClient(
                _twilio.twilio_account_sid,
                _twilio.twilio_auth_token)  # use the twilio api account
            bodystr = 'Hi Andrew - test species data: '
            bodystr = bodystr + str(rows[0])[:160 - len(bodystr)]
            message = client.sms.messages.create(to="+393668084920",
                                                 from_="+19712647662",
                                                 body=bodystr)  # my mobile
            return message

        elif format == 'email':
            _amazon_ses = amazon_ses()
            amazonSes = AmazonSES(
                _amazon_ses.AccessKeyID, _amazon_ses.SecretAccessKey
            )  # use the amazon simple email service api account
            message = EmailMessage()
            message.subject = 'JRC REST Services Information Request'
            message.bodyHtml = getResultsAsHTML(rows, fieldcount, colsRequired,
                                                metadatadict)
            result = amazonSes.sendEmail('*****@*****.**',
                                         '*****@*****.**',
                                         message)  # to me
            return result

        elif format == 'html':
            htmlData = getResultsAsHTML(rows, fieldcount, colsRequired,
                                        metadatadict)
            web.header("Content-Type", "text/html")
            return "<html><head></head><body>" + htmlData + "</body></html>"

        elif format == 'csv':
            data = [[
                row[col] for col in range(fieldcount) if (col in colsRequired)
            ] for row in rows]
            colnames = ",".join([f["name"]
                                 for f in metadatadict["fields"]]) + "\n"
            output = colnames + "\n".join([
                p for p in [
                    ",".join(h) for h in [[getStringValue(col) for col in row]
                                          for row in data]
                ]
            ])
            filename = "dataDownload.csv"  #hardcoded for now
            f = open(r'../../htdocs/mstmp/' + filename, 'wb')
            f.write(output)
            f.close()
            web.header("Content-Type", "text/plain")
            web.header("Content-Disposition",
                       "attachment; filename=%s" % filename)
            return output

        elif format == 'pdf':
            config = pdfkit.configuration(
                wkhtmltopdf='/usr/local/bin/wkhtmltopdf')
            web.header("Content-Type", "application/pdf")
            htmlData = getResultsAsHTML(rows, fieldcount, colsRequired,
                                        metadatadict)
            return pdfkit.from_string(htmlData.decode('utf8'),
                                      False,
                                      configuration=config,
                                      options={
                                          'quiet': '',
                                          'encoding': "UTF-8"
                                      })

        else:
            raise RESTServicesError('Invalid response format: ' + format)

    except (RESTServicesError, DataError, ProgrammingError,
            exceptions.TypeError, IndexError, IntegrityError, AmazonError,
            OperationalError) as e:
        #        web.webapi.internalerror() #returns a internal server error 500
        t2 = datetime.datetime.now()
        msg = "There was an error sending the email. Make sure that the email address has been verified in Amazon Simple Email Services" if type(
            e) == AmazonError else e.message
        logging.error(msg + "\n")
        if type(e) == ProgrammingError:
            if ("column" in e.message) & ("does not exist"
                                          in e.message) & (sortField != ""):
                msg = "Invalid sortfield parameter: " + sortField
        return returnError(metadataName, rootName, t2 - t1, msg)
Beispiel #20
0
def getQueryStringParams(querystring):
    return OrderedDict([(q.split("=")[0], urllib.unquote(q.split("=")[1]))
                        for q in querystring.split("&")])
Beispiel #21
0
def xml(test_xml):
    global tests, teacher_tests, var_names, teacher_varnames, student_id, testid
    global title, names, questions, choices, id, teacher_questions, teacher_choices, teacher_section
    xmldoc = minidom.parseString(test_xml)
    test = xmldoc.firstChild
    global teacherNode
    teacherNode = None
    ids = xmldoc.getElementsByTagName('id')
    testid = ids[0].getAttribute('testid')
    teacher_tests = OrderedDict()
    tests = OrderedDict()
    names = OrderedDict()
    student_id = OrderedDict()
    #put student_names in a dict to match tests
    for singleid in ids:
        id = singleid.firstChild.nodeValue
        title = singleid.getElementsByTagName('title')[0].firstChild.data
        sections = singleid.getElementsByTagName('section')
        studentNode = sections[0]
        names[id] = studentNode.getElementsByTagName('name')[0].firstChild.data
        student_id[id] = studentNode.getAttribute("studentid")
        if len(sections) > 1:
            teacherNode = sections[1]
        #student = {dict of tests:{dict of questions:[list of choices]}{}}
        questions = OrderedDict()
        var_names = OrderedDict()
        questionNodes = studentNode.getElementsByTagName('question')
        for question in questionNodes:
            questiontemp = question.getElementsByTagName(
                'text')[0].firstChild.wholeText
            varname = question.getAttribute('varName')
            choicesNodes = question.getElementsByTagName('choice')
            choices = []
            for choice in choicesNodes:
                choicetemp = choice.firstChild.data
                value = choice.getElementsByTagName(
                    'value')[0].firstChild.wholeText
                choices.append((choicetemp, value))
            counter = 0
            temp = questiontemp
            while temp in questions:
                counter = counter + 1
                temp = questiontemp + ` counter ` + '.'
            if counter != 0:
                questiontemp += ` counter ` + '.'
            questions[questiontemp] = choices
            var_names[questiontemp] = varname

        tests[id] = [questions, var_names]

        #teacher = {{dict of essay questions:[list of point values]}{}}
        if teacherNode != None:
            teacher_section = teacherNode.getElementsByTagName(
                'name')[0].firstChild.data
            teacher_questions = OrderedDict()
            teacher_varnames = OrderedDict()
            teacher_questionNodes = teacherNode.getElementsByTagName(
                'question')
            for teacher_question in teacher_questionNodes:
                questiontemp = teacher_question.getElementsByTagName(
                    'text')[0].firstChild.wholeText
                varname = teacher_question.getAttribute('varName')
                choicesNodes = teacher_question.getElementsByTagName('choice')
                teacher_choices = []
                for choice in choicesNodes:
                    choicetemp = choice.firstChild.data
                    value = choice.getElementsByTagName(
                        'value')[0].firstChild.wholeText
                    teacher_choices.append((choicetemp, value))
                counter = 0
                temp = questiontemp
                while temp in teacher_questions:
                    counter = counter + 1
                    temp = questiontemp + ` counter ` + '.'
                if counter != 0:
                    questiontemp += ` counter ` + '.'
                teacher_questions[questiontemp] = teacher_choices
                teacher_varnames[questiontemp] = varname

            teacher_tests[id] = [teacher_questions, teacher_varnames]
Beispiel #22
0
 def __init__(self, name):
     self.__name = name.lower()  # keeps lower name
     self.params = OrderedDict()  # Replace dictionary by ordered dictionry
Beispiel #23
0
 def _get_atomicSpecies(self):
     atomicSpecies = OrderedDict()
     for a in self:
         atomicSpecies[a.symbol] = AtomicSpecies(symbol = a.symbol, \
                                     mass = a.mass, potential = a.potential)
     return atomicSpecies