Пример #1
0
def restore_junction(old_path, new_path):
    if os.path_exists(new_path):
        if os.path_exists(old_path):
            if junction.isjunction(old_path):
                print("Already junctioned: {}".format(old_path))
            else:
                print("Cannot junction; conflicting folder already exists: {}".format(old_path))
        else:
            link_save(old_path, new_path)
            hide_directory(old_path)
    def resume_download():
        file_exists = False
        Custom_URL_Class = CustomURLOpener(
        )  # create object from class to override it
        if os.path_exists(TARGET_FILE):
            out_file = open(TARGET_FILE, "ab")
            file_exists_size = os.path.getsize(TARGET_FILE)  # get file size
            ### if the file exists then only download the ubfinished part
            Custom_URL_Class.addheader("Download range",
                                       "bytes=%s-" % (file_exists_size))
        else:
            out_file = open(TARGET_FILE, "wb")

        web_page = Custome_URL_Class.open(TARGET_URL + TARGET_FILE)

        # if the file exists  but we already have the whole thing,don't download again

        if int(web_page.headers['Content-Length']) == file_exists_size:
            loop = 0
            print("File already downloaded ..")

        byte_count = 0
        while True:
            data = web_page.read(8192)
            if not data:
                break
            out_file.write(data)
            byte_count = byte_count + len(data)

        web_page.close()
        out_file.close()

        for k, v in web_page.headers.items():
            print(k, "=", v)
        print("file copied  ", byte_count, "byte from", web_page.url)
Пример #3
0
    def create(self, project_directory):
        """ Sets up what needs to be set up.
            Should only be called if current state is STATE_UNINITIALIZED
        """
        # redo setup based on passed project directory
        self.project_directory = project_directory
        self.directory = '{}{}.project'.format(project_directory, os.sep)
        self.conf_path = '{}{}book.cnf'.format(self.directory, os.sep)

        # make sure the project directory exists
        if not os.path_exists(self.directory):
            os.makedirs(self.directory)
        
        # make sure our config is loaded
        if not self.config:
            self.config = ConfigParser()
            if os.path_exists(self.conf_path):
                self.config.read(self.conf_path)

        # make sure all the sections of the config exist
        sections = (
            'extract_text',
            'metadata',
            'process',
        )   

        for section in sections:
            if not self.config.has_section(section):
                self.config.add_section(section)

        # set current status if it hasn't been set
        if not self.config.has_option('process', 'current_status'):
            self.config.set('process', 'current_status', STATE_NEEDS_METADATA)

        metadata = (
            'author',
            'title'
        )
        # set metadata if it hasn't been set
        for data in metadata:
            if not self.config.get('metadata', data):
                self.config.set('metadata', data, '')

        # write the config
        self.write_config()
Пример #4
0
def czytaj_dane(plik):
    if not os.path_exists(plik):
        print('Plik niedostepny!')
        return False
    teksty = []
    with open(plik, "r") as f:
        for wiersz in f:
            teksty.append(linia.strip())
    return teksty
Пример #5
0
    def run(self, context) :
        """
        Run Meshlab using selected filter
        """
        server = findmeshlab()                                                          # find meshlabserver executable
        if server is None :
            self.report({'ERROR'}, "Can't find Meshlab Server program. 'meshlabserver' must be in your PATH for command line programs.")
            return      
        #   Preliminary checks complete, OK to attempt operation   
        try :
            scriptfile = self.script_filename                                           # name of script we will run
            working_dir = tempfile.mkdtemp(prefix='Blender-Meshlab')                    # scratch file directory  
            #   Name export file from blender, and meshlab output file for re-import
            temp_ply_path = os.path.join(working_dir,"temp_mesh.ply")
            temp_o_ply_path = os.path.join(working_dir,"temp_mesh_o.ply")
            #   Export file
            bpy.ops.export_mesh.ply(filepath=temp_ply_path, check_existing = False)     # exports entire scene, revise
     
            #   Call Meshlabserver to do the work.
            result = subprocess.call([server,"-i",temp_ply_path,"-o",temp_o_ply_path, "-m","vcvnfnfcvt", "-s",scriptfile])

            if result != 0 :
                self.report({'ERROR'},"\"%s\" running script \"%s\" failed, status %d" % (server, scriptfile, result)) # trouble
                return
    
            #   Import file exported by Meshlab.
            bpy.ops.import_mesh.ply(filepath=temp_o_ply_path)
            
            #   Postprocessing after import
            new_obj = bpy.data.objects["temp_mesh_o"]
            ####new_obj.name = name + "_meshlab"
    
            ####bpy.ops.object.select_all(action = 'DESELECT')
            ####new_obj.select = True
            ####bpy.context.scene.objects.active = new_obj
            ####bpy.ops.transform.rotate(value = (math.pi/2,),axis = (1,0,0))
            ####new_obj.scale = scale
            
        finally:                                                                            # clean up, even if things went wrong
            assert(working_dir)
            if not KEEPTEMPDIR :                                                            # clean up, unless debugging
                if os.path.exists(temp_ply_path) :
                    os.remove(temp_ply_path)
                if os.path_exists(temp_o_ply_path) :
                    os.remove(temp_o_ply_path)
                os.rmdir(working_dir)                                                       # temp directory should always exist
Пример #6
0
 def tiles(self, id):
     datatype = id
     x = request.params.get('x', None)
     y = request.params.get('y', None)
     z = request.params.get('z', None)
     id = request.params.get('id', None)
     
     overwrite = True if request.params.get('overwrite', None) is not None else False
     
     logging.info('Creating tileset type: ' + datatype)
     
     if datatype=="range":
         mapfile = os.path.join(app_globals.RANGESHP_DIR, id + '.mapfile.xml')  
         if not os.path_exists(mapfile):
             url = "http://mol.colorado.edu/layers/api/newtileset/range?id=%s&range_ids=%s" % (id,id)
             req = urllib2.Request(url)
             response = urllib2.urlopen(req)
         tile_dir = os.path.join(app_globals.TILE_DIR, id)    
         tile = os.path.join(tile_dir, z, x, "%s.png" % y)  
         null_tile = os.path.join(tile_dir, z, x, "%s.null" % y)  
         
     elif datatype=="ecoregion":
         mapfile = os.path.join(app_globals.ECOSHP_DIR, id + '.mapfile.xml')   
         tile_dir = os.path.join(app_globals.ECOTILE_DIR, id)   
         tile = os.path.join(tile_dir, z, x, "%s.png" % y)  
         null_tile = os.path.join(tile_dir, z, x, "%s.null" % y)  
         
     logging.info('Generating new ' + datatype +' tiles: ' + id)
     bbox = bboxfromxyz(int(x),int(y),int(z) )   
     GenerateTiles.render_tiles(bbox,
                                str(mapfile),
                                str(tile_dir),
                                int(z),
                                int(z+1),
                                str(id),
                                num_threads=app_globals.TILE_QUEUE_THREADS,
                                overwrite=overwrite)   
Пример #7
0
def create_dir(directory):
  if not os.path_exists(directory):
    os.makedir(directory)
Пример #8
0
    def calculate_twi(self,
                      esfile,
                      save_path,
                      use_cache=True,
                      do_edges=False,
                      skip_uca_twi=False):
        """
        Calculates twi for supplied elevation file

        Parameters
        -----------
        esfile : str
            Path to elevation file to be processed
        save_path: str
            Root path to location where TWI will be saved. TWI will be saved in
            a subdirectory 'twi'.
        use_cache : bool (optional)
            Default True. If a temporary file exists (from a previous run),
            the cached file will be used. Otherwise, if False, existing files
            will be recomputed
        do_edges : bool (optional)
            See :py:func:`process_twi` for details on this argument.
        skip_uca_twi : bool (optional)
            Skips the calculation of the UCA and TWI (only calculates the
            magnitude and direction)
        """
        if os.path.exists(os.path.join(save_path, 'tile_edge.pkl')) and \
                self.tile_edge is None:
            with open(os.path.join(save_path, 'tile_edge.pkl'), 'r') as fid:
                self.tile_edge = cPickle.load(fid)
        elif self.tile_edge is None:
            self.tile_edge = TileEdgeFile(self.elev_source_files, save_path)
            with open(os.path.join(save_path, 'tile_edge.pkl'), 'wb') as fid:
                cPickle.dump(self.tile_edge, fid)

        status = 'Success'  # optimism
        # Check if file is locked
        lckfn = _get_lockfile_name(esfile)
        coords = parse_fn(esfile)
        fn = get_fn_from_coords(coords, 'twi')
        print '*' * 79
        if skip_uca_twi:
            print '*' * 10, fn, 'Slope Calculation starting...:', '*' * 10
        else:
            print '*' * 10, fn, 'TWI Calculation starting...:', '*' * 10
        print '*' * 79
        if os.path.exists(lckfn):  # another process is working on it
            print fn, 'is locked'
            return fn, "Locked"
        else:  # lock this tile
            fid = file(lckfn, 'w')
            fid.close()

        dem_proc = DEMProcessor(esfile)
        # check if the slope already exists for the file. If yes, we should
        # move on to the next tile without doing anything else
        if skip_uca_twi \
                and os.path.exists(dem_proc.get_full_fn('mag', save_path)
                                   + '.npz') \
                and os.path.exists(dem_proc.get_full_fn('ang', save_path)
                                   + '.npz'):
            print dem_proc.get_full_fn('mag',
                                       save_path) + '.npz', 'already exists'
            print dem_proc.get_full_fn('ang',
                                       save_path) + '.npz', 'already exists'
            # remove lock file
            os.remove(lckfn)
            return fn, 'Cached: Slope'
        # check if the twi already exists for the file. If not in the edge
        # resolution round, we should move on to the next tile
        if os.path.exists(dem_proc.get_full_fn('twi', save_path)) \
                and (do_edges is False):
            print dem_proc.get_full_fn('twi', save_path), 'already exists'
            # remove lock file
            os.remove(lckfn)
            return fn, 'Cached'

        # only calculate the slopes and direction if they do not exist in cache
        fn_ang = dem_proc.get_full_fn('ang', save_path)
        fn_mag = dem_proc.get_full_fn('mag', save_path)
        if os.path.exists(fn_ang + '.npz') and os.path.exists(fn_mag + '.npz')\
                and not self.overwrite_cache:
            dem_proc.load_direction(fn_ang)
            dem_proc.load_slope(fn_mag)
            dem_proc.find_flats()
        else:
            if os.path.exists(fn_ang + '.npz') and os.path_exists(fn_mag + '.npz')\
                    and self.overwrite_cache:
                os.remove(fn_ang)
                os.remove(fn_mag)
            dem_proc.calc_slopes_directions()
            dem_proc.save_slope(save_path, raw=True)
            dem_proc.save_direction(save_path, raw=True)
        if self._DEBUG:
            dem_proc.save_slope(save_path, as_int=False)
            dem_proc.save_direction(save_path, as_int=False)

        if skip_uca_twi:
            # remove lock file
            os.remove(lckfn)
            return fn, status + ":mag-dir-only"

        fn_uca = dem_proc.get_full_fn('uca', save_path)
        fn_uca_ec = dem_proc.get_full_fn('uca_edge_corrected', save_path)
        fn_twi = dem_proc.get_full_fn('twi', save_path)

        # check if edge structure exists for this tile and initialize
        edge_init_data, edge_init_done, edge_init_todo = \
            self.tile_edge.get_edge_init_data(esfile, save_path)

        # Check if uca data exists (if yes, we are in the
        # edge-resolution round)
        uca_init = None
        if os.path.exists(fn_uca + '.npz'):
            if os.path.exists(fn_uca_ec + '.npz'):
                dem_proc.load_uca(fn_uca_ec)
            else:
                dem_proc.load_uca(fn_uca)
            uca_init = dem_proc.uca

        if do_edges or uca_init is None:
            dem_proc.calc_uca(uca_init=uca_init,
                              edge_init_data=[
                                  edge_init_data, edge_init_done,
                                  edge_init_todo
                              ])

            if uca_init is None:
                dem_proc.save_uca(save_path, raw=True)
                if self._DEBUG:
                    # Also save a geotiff for debugging
                    dem_proc.save_uca(save_path, as_int=False)
            else:
                if os.path.exists(fn_uca_ec):
                    os.remove(fn_uca_ec)
                dem_proc.save_array(dem_proc.uca,
                                    None,
                                    'uca_edge_corrected',
                                    save_path,
                                    raw=True)
                if self._DEBUG:
                    dem_proc.save_array(dem_proc.uca,
                                        None,
                                        'uca_edge_corrected',
                                        save_path,
                                        as_int=False)
            # Saving Edge Data, and updating edges
            self.tile_edge.update_edges(esfile, dem_proc)

        dem_proc.calc_twi()
        if os.path.exists(fn_twi):
            os.remove(fn_twi)
        dem_proc.save_twi(save_path, raw=False)

        # clean up for in case
        gc.collect()

        # remove lock file
        os.remove(lckfn)
        # Save last-used dem_proc for debugging purposes
        if self._DEBUG:
            self.dem_proc = dem_proc
        return fn, status
Пример #9
0
	def get_price(self, tracker_file, stock):
		if os.path_exists(tracker_file):
			with open(tracker_file) as fp:
				json_data = json.load(fp)
		return json_data[stock]