예제 #1
0
파일: oldformat.py 프로젝트: esheldon/espy
def PaperDataLumToAscii(band, shortnames=False, redgal=False):
    import records
    d=GetPaperDataLum(band, redgal=redgal)
    fold = GetPaperDataLumFiles(band, redgal=redgal)

    red=''
    if redgal:
        red='red-'

    num=len(d)
    if shortnames:
        dirold=os.path.dirname(fold[0])
        fnames = [red+'deltasigma-'+band+'-'+str(n+1)+'.csv' for n in range(num)]
        fnames = [os.path.join(dirold,f) for f in fnames]
    else:
        fnames = [f.replace('.fit','.rec') for f in fold]

    dt=[('r','f4'),('delta_sigma','f4'),('delta_sigma_err','f4')] 
    for i in range(num):
        sys.stdout.write('%s\n' % fnames[i])
        nrad = len(d[i]['MEANR_REBIN'])
        out=numpy.zeros(nrad, dtype=dt)
        out['r'] = d[i]['MEANR_REBIN']
        out['delta_sigma'] = d[i]['SIGMA_REBIN']
        out['delta_sigma_err'] = d[i]['SIGMAERR_REBIN']
        
        if not shortnames:
            hdr={}
            hdr['file_origin'] = os.path.basename(fold[0])
        else:
            hdr=None
        sfile.write(out, fnames[i], header=hdr, delim=',') 
예제 #2
0
def updateAdvancedSettings(input):
    try:
        filename = 'advancedsettings.xml'
        source   = os.path.join('special://userdata', filename)

        if sfile.exists(source):
            contents = sfile.read(source)
            if input in contents:
                Log('%s already in %s' % (input, filename))
                return True

            Log('Updating %s with %s' % (filename, input))
            start = input.replace('>', ' >').split(' ', 1)[0].strip()
            end   = '</' + input.rsplit('</', 1)[-1].strip()

            start = contents.split(start, 1)[0]
            end   = contents.split(end, 1)[-1]

            contents = start
            if start != end:
                contents += end

            contents = contents.replace('</advancedsettings>', '%s</advancedsettings>' % input)
        else:
            Log('Creating %s with %s' % (filename, input))
            contents = '<advancedsettings>%s</advancedsettings>' % input
    except:
        Log('Error updating %s with %s' % (filename, input))
        Log('Resetting %s with %s' % (filename, input))
        contents = '<advancedsettings>%s</advancedsettings>' % input

    sfile.write(source, contents)
    
    return False
예제 #3
0
def dump_query(q, fname):
    import sfile
    conn = oracle_util.Connection()
    stdout.write(q+'\n')
    res = conn.Execute(q)
    stdout.write("Writing to file: %s\n" % fname)
    sfile.write(res, fname)
    res=0
    return
예제 #4
0
    def SetSeriesImage(self, name, image):
        targetFile = self.GetSeriesImage(name)
        if not sfile.exists(targetFile):
	    import urllib2
            req = urllib2.Request(image)
            req.add_header('User-Agent', utils.getUserAgent())
            response = urllib2.urlopen(req)
            sfile.write(targetFile, response.read())
            response.close()
        return targetFile
예제 #5
0
 def SetSeriesImage(self, name, image):
     targetFile = self.GetSeriesImage(name)
     if not sfile.exists(targetFile):
         import urllib2
         req = urllib2.Request(image)
         req.add_header('User-Agent', utils.getUserAgent())
         response = urllib2.urlopen(req)
         sfile.write(targetFile, response.read())
         response.close()
     return targetFile
예제 #6
0
def verifySource():
    input  = '<source><name>Livebox Cache</name><path pathversion="1">special://userdata/addon_data/script.video.thelivebox/</path></source></video>'
    source = os.path.join('special://userdata', 'sources.xml')

    if sfile.exists(source):
        contents = sfile.read(source)
        if 'Livebox Cache' in contents:
            Log('Source already exists')
            return True
        Log('Updating sources')
        contents = contents.replace('</video>', input)
    else:
        Log('Creating sources.xml file')
        contents = '<sources><video><default pathversion="1"></default>%s</sources>' % input

    sfile.write(source, contents)
    
    return False
예제 #7
0
def DoDownload(name, dst, src, image=None, orignalSrc=None, progressClose=True, silent=False):
    import download
    import s3
    import sfile

    dst = dst.replace(os.sep, DELIMETER)
    src = src.replace(os.sep, DELIMETER)

    if orignalSrc == None or len(orignalSrc) == 0:
        orignalSrc = src

    temp  = dst + '.temp'

    url = urllib.quote_plus(src)
    url = s3.getURL(url)
    url = s3.convertToCloud(url)

    #image no longer used
    #if image and image.startswith('http'):
    #    pass
    #else:
    #    image = None
    image = None

    resp = download.getResponse(url, 0, '')
    if not resp:
        return 1

    dp = None

    name = name.decode('utf-8')

    if not silent:
        dp = DialogProgress(GETTEXT(30079) % name)
        
    download.doDownload(url, temp, name, dp=dp, silent=silent)
    
    if dp and progressClose:
        dp.close()

    if sfile.exists(temp + '.part'): #if part file exists then download has failed
        return 1

    if not sfile.exists(temp): #download was cancelled
        return 2

    sfile.remove(dst)
    sfile.rename(temp, dst)

    src = orignalSrc

    #recursively get dsc files
    dscFile = removeExtension(dst) + '.%s' % DSC
    while len(src) > 0:
        try:
            plot = getAmazonContent(src, DSC)
            sfile.write(dscFile, plot)

            newSrc = src.rsplit(DELIMETER, 1)[0]
            if newSrc == src:
                break

            src     = newSrc 
            dscFile = dscFile.rsplit(DELIMETER, 1)[0] + '.%s' % DSC

        except:
            pass


    #original image handling - no longer used
    if image:            
        img   = image.rsplit('?', 1)[0]
        ext   = img.rsplit('.'  , 1)[-1]
        root  = dst.rsplit('.'  , 1)[0]
        jpg   = root + '.%s' %  ext
        gif   = root + '.%s' % 'gif'

        gifURL = s3.getURL(urllib.quote_plus(_src.rsplit('.', 1)[0] + '.gif'))
        
        DownloadIfExists(image,  jpg)
        DownloadIfExists(gifURL, gif)


    #recursively get image files
    src = orignalSrc.rsplit('.', 1)[0]
    dst = dst.rsplit('.', 1)[0]

    imageTypes = IMG_EXT
    imageTypes.append('.gif')

    while len(src) > 0:
        for ext in imageTypes: 
            image = src + ext

            image = s3.getURL(urllib.quote_plus(image))
            image = s3.convertToCloud(image)

            DownloadIfExists(image, dst+ext)

        newSrc = src.rsplit(DELIMETER, 1)[0]

        if newSrc == src:    
            break

        src = newSrc 
        dst = dst.rsplit(DELIMETER, 1)[0]

    return 0
예제 #8
0
def getFavourites(file,
                  limit=10000,
                  validate=True,
                  superSearch=False,
                  chooser=False):
    import xbmcgui

    prefix = ''
    if not chooser:
        prefix = HOME_INDICATOR if xbmcgui.getCurrentWindowId(
        ) == 10000 else ''

    xml = '<favourites></favourites>'
    if sfile.exists(file):
        xml = sfile.read(file)

    #fix files due to previous bug
    if HOME_INDICATOR in xml:
        xml = xml.replace(HOME_INDICATOR, '')
        sfile.write(file, xml)

    items = []

    faves = re.compile('<favourite(.+?)</favourite>').findall(xml)

    for fave in faves:
        fave = fave.replace('&quot;', '&_quot_;')
        fave = fave.replace('\'', '"')
        fave = utils.unescape(fave)

        fave = fave.replace('name=""', '')
        try:
            name = re.compile('name="(.+?)"').findall(fave)[0]
        except:
            name = ''

        try:
            thumb = re.compile('thumb="(.+?)"').findall(fave)[0]
        except:
            thumb = ''

        try:
            cmd = fave.split('>', 1)[-1]
        except:
            cmd = ''

        #name  = utils.Clean(name.replace( '&_quot_;', '"'))
        name = name.replace('&_quot_;', '"')
        thumb = thumb.replace('&_quot_;', '"')
        cmd = cmd.replace('&_quot_;', '"')

        add = False

        if superSearch:
            add = isValid(cmd)
        elif (SHOWUNAVAIL) or (not validate) or isValid(cmd):
            add = True

        if add:
            cmd = upgradeCmd(cmd)

            if cmd.startswith('PlayMedia'):
                option = 'mode'
                try:
                    mode = int(favourite.getOption(cmd, option))
                except:
                    win = xbmcgui.getCurrentWindowId()
                    cmd = updateSFOption(cmd, 'winID', win)

            name = resolve(name)
            cmd = patch(cmd)
            cmd = resolve(cmd)
            cmd = prefix + cmd

            items.append([name, thumb, cmd])
            if len(items) > limit:
                return items

    return items
예제 #9
0
def AddPlaylistToPlaylist(title, image, url, mode, isFirst=False):
    src = url

    playlist = []
    root     = ''

    while not sfile.exists(src):
        loc = utils.getDownloadLocation()

        client = utils.GetClient() + s3.DELIMETER        
        dst    = os.path.join(loc, url.replace(client, ''))

        if sfile.exists(dst):
            if utils.DialogYesNo(GETTEXT(30101), GETTEXT(30100)):
                src = dst
                break
            else:
                utils.delete(dst, APPLICATION)

        src = urllib.quote_plus(src)
        src = s3.getURL(src)
        src = s3.convertToCloud(src)
        src = utils.GetHTML(src, maxAge=7*86400)

        src = src.replace('\r', '')
        src = src.split('\n')

        content = ''
        root    = 'AMAZON@'

        for video in src:
            if len(video.strip()) > 0:
                content += root
                content += video
                content += '\r\n'
        sfile.write(dst, content)

        src  = utils.removeExtension(url)
        root = utils.removeExtension(dst)

        plotFile = root + '.%s' % DSC
        plot     = utils.getAmazonContent(url, DSC)
        sfile.write(plotFile, plot)

        imageTypes = IMG_EXT
        imageTypes.append('.gif')

        for ext in imageTypes: 
            image = src + ext
            image = s3.getURL(urllib.quote_plus(image))
            image = s3.convertToCloud(image)

            utils.DownloadIfExists(image, root+ext)


        return AddPlaylistToPlaylist(title, image, dst, mode, isFirst)

    playlist = utils.getPlaylistFromLocalSrc(src)

    for video in playlist:
        utils.Log('Adding to playlist: %s' % video)
        title, thumb = utils.GetTitleAndImage(video)
        if not thumb:
            thumb = image

        try:    AddToPlaylist(title, thumb, video, mode=mode, isFirst=isFirst)
        except: pass

        isFirst = False
예제 #10
0
def dump_table_inchunks(tables_in, columns_in, fname, chunksize=1000000,
                        constraints='',
                        dryrun=False):
    import sfile
    conn = oracle_util.Connection()

    if isinstance(tables_in, str):
        tables = [tables_in]
    else:
        tables = tables_in
    if isinstance(columns_in, str):
        columns = [columns_in]
    else:
        columns = columns_in



    q="select count(*) from %s" % tables[0]
    stdout.write(q+'\n')
    res = conn.Execute(q)
    nrows = res['count(*)'][0]

    nchunk = nrows/chunksize
    nleft = nrows % chunksize

    stdout.write('Total number of rows: %s\n' % nrows)
    stdout.write('Chunksize: %s\n' % chunksize)
    stdout.write('    Main chunks: %s\n' % nchunk)
    stdout.write('    leftover:    %s\n' % nleft)
    stdout.write("Output file: '%s'\n" % fname)

    nget = nchunk
    if nleft > 0:
        nget += 1

    colstr = ','.join(columns)
    tabstr = ','.join(tables)
    for i in range(nget):

        rowmin = i*chunksize + 1
        rowmax = (i+1)*chunksize + 1
        q = """
        select 
            %s
        from
            %s
        where
            rownum >= %s
            AND rownum < %s
            %s
        """ % (colstr, tabstr, rowmin, rowmax, constraints)

        stdout.write(q+'\n')
        res = conn.Execute(q)

        if res is not None:
            stdout.write("Found: %s\n" % len(res))
        else:
            stderr.write("Found no rows\n");

        if not dryrun and res is not None:
            if i == 0:
                sfile.write(res, fname)
            else:
                sfile.write(res, fname, append=True)

    conn.Close()
예제 #11
0
def getFavourites(file, limit=10000, validate=True, superSearch=False, chooser=False):
    import xbmcgui

    prefix = ''
    if not chooser:
        prefix = HOME_INDICATOR if xbmcgui.getCurrentWindowId() == 10000 else ''    

    xml  = '<favourites></favourites>'
    if sfile.exists(file):
        xml = sfile.read(file)

    #fix files due to previous bug
    if HOME_INDICATOR in xml:
        xml = xml.replace(HOME_INDICATOR, '')
        sfile.write(file, xml)

    items = []

    faves = re.compile('<favourite(.+?)</favourite>').findall(xml)

    for fave in faves:
        fave = fave.replace('&quot;', '&_quot_;')
        fave = fave.replace('\'', '"')
        fave = utils.unescape(fave)

        fave = fave.replace('name=""', '')
        try:    name = re.compile('name="(.+?)"').findall(fave)[0]
        except: name = ''

        try:    thumb = re.compile('thumb="(.+?)"').findall(fave)[0]
        except: thumb = ''

        try:    cmd   = fave.split('>', 1)[-1]
        except: cmd = ''

        #name  = utils.Clean(name.replace( '&_quot_;', '"'))
        name  = name.replace( '&_quot_;', '"')
        thumb = thumb.replace('&_quot_;', '"')
        cmd   = cmd.replace(  '&_quot_;', '"')

        add = False

        if superSearch:
            add = isValid(cmd)
        elif (SHOWUNAVAIL) or (not validate) or isValid(cmd):
            add = True

        if add:
            cmd = upgradeCmd(cmd)

            if cmd.startswith('PlayMedia'):
                option = 'mode'
                try:                        
                    mode = int(favourite.getOption(cmd, option))
                except:
                    win  = xbmcgui.getCurrentWindowId()
                    cmd  = updateSFOption(cmd, 'winID', win)

            name = resolve(name)
            cmd  = patch(cmd)
            cmd  = resolve(cmd)
            cmd  = prefix + cmd

            items.append([name, thumb, cmd])
            if len(items) > limit:
                return items

    return items