def on_query_completions(self, view, prefix, locations): window=sublime.active_window() view=window.active_view() self.clases=set() lang=utils.get_language() if lang=="html" or lang=="php": punto=view.sel()[0].a linea=view.substr(sublime.Region(view.line(punto).a, punto)).replace('"', "'") linea=linea[:linea.rfind("'")].strip() print("la linea es :"+linea) if linea.endswith("class="): print("en compass") cssFiles=utils.get_files({"ext":"css"}) self.clases=[] for cssFile in cssFiles: texto=open(cssFile).read() cssClases=re.findall("\.(?P<clase>[a-z][-\w]*)\s+", texto) self.clases=self.clases + cssClases self.clases=list(set(self.clases)) self.clases=[[clase + "\t(CSS)", clase] for clase in self.clases] return list(self.clases) linea=view.substr(sublime.Region(view.line(punto).a, punto)).replace('"', "'").strip() if linea.endswith("src='") and linea.startswith("<script"): path=view.file_name() path=path[:path.rfind("/")] if path.find("/")!=-1 else path[:path.rfind("\\")] RecursosHtml(path, "js").insertar() elif linea.endswith("href='") and linea.startswith("<link "): path=view.file_name() path=path[:path.rfind("/")] if path.find("/")!=-1 else path[:path.rfind("\\")] RecursosHtml(path, "css").insertar()
def execute(self): cf = self.fm.thisfile path = cf.relative_path.replace("%", "%%") if path.find('.') != 0 and path.rfind('.') != -1 and not cf.is_directory: self.fm.open_console('rename ' + path, position=(7 + path.rfind('.'))) else: self.fm.open_console('rename ' + path)
def readSvnFile(self): fp = open(self.txtPath, "r") list = fp.readlines() fp.close() #print(list) for path in list: #path.strip() if "/src/main/" in path and "." in path and "D /" not in path: path = path.replace("\n", "") if "(from" in path: index = path.rfind("(from") path = path[0:index] if "/main/java/" in path: if ".java" in path: path = path.replace(".java", ".class") index = path.rfind("/main/java/") path = "/WEB-INF/classes/"+path[index+11:] if "/main/resources/" in path: index = path.rfind("/resources/") path = "/WEB-INF/classes/"+path[index+11:] if "/main/webapp/" in path: index = path.rfind("/webapp/") path = path[index+7:] mavenCreater.copyList.append(path) print("读取>> ",path)
def writeTestCaseAlternatives(req, arguments): path = req.parsed_uri[-3] tc = path[path.rfind("/") + 1:] base_uri = req.construct_url( \ path[0:path.rfind("/test-cases/")] + "/test-cases/") filename = arguments.split("/")[-1] req.write("""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd"> <html version="XHTML+RDFa 1.0" xmlns="http://www.w3.org/1999/xhtml" xmlns:xhv="http://www.w3.org/1999/xhtml/vocab#" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:test="http://www.w3.org/2006/03/test-description#"> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> <title>JSON-LD Test Suite: Select a Test Case Document</title> </head> <body> <p> The following documents are associated with Test Case %s: <ul> <li><a href="%s%s.json">JSON-LD</li> <li><a href="%s%s.sparql">SPARQL for JSON-LD</li> </ul> </p> </body> </html>""" % (tc, base_uri, filename, base_uri, filename))
def __getitem__(self, index): """ Args: index (int): Index Returns: tuple: (image, target) """ path = self.imgs[index] target = [float(_) for _ in path[path.rfind('/')+1:path.rfind('.')].split(',')] try: target[1] = target[1] + math.pi if target[1] < 0 else target[1] - math.pi except: # real images target = [0, 0] target = torch.Tensor(target) img = self.loader(path) if self.transform is not None: img = self.transform(img) if self.augment: img = torch.from_numpy(data_augment(img.numpy().transpose([1,2,0])).transpose([2,0,1])) else: img = (img * 2) - 1 if self.target_transform is not None: target = self.target_transform(target) if self.return_path: return img, target, path else: return img, target
def get_properties_from_uri(uri): path = get_path_from_uri(uri) if path.endswith('/'): path = path[:-1] blob_name = path[path.rfind('/')+1:] path = path[:path.rfind('/')] container_name = path[path.rfind('/')+1:] return {'blob_name': blob_name, 'container_name': container_name}
def get_properties_from_uri(uri): path = get_path_from_uri(uri) if path.endswith('/'): path = path[:-1] blob_name = path[path.rfind('/') + 1:] path = path[:path.rfind('/')] container_name = path[path.rfind('/') + 1:] return {'blob_name': blob_name, 'container_name': container_name}
def is_path_exists(self, path, check_type = 'FILE'): """ @path[string]: the path you want to check exists or not @check_type[string]: the type you check, FILE(default) or FOLDER If the path exists in the VM, return True, else return False """ if self.__login_flag == False: print 'Path check error: Sorry, but please login the VM first.' return False if check_type not in ['FILE', 'FOLDER']: print "Check path error: check_type must be set as 'FILE' or 'FOLDER'" return False if path.endswith('\\'): path = path[:-1] if path.find('\\') != -1 and path.find('/') != -1: print "Check path error: path must be set as 'c:\\temp\\file.file' or 'c:/temp/file.file'" return False if path.find('\\') != -1: location = path[0:path.rfind('\\')] check_item = path[path.rfind('\\')+1:].lower() if (check_item is None) or (check_item == ""): return False try: tmp_list = self.list_files(location) except Exception as e: raise e if check_type == 'FOLDER': if tmp_list is not None: for item in tmp_list: if item['path'].lower() == check_item and item['type'] == 'directory': return True return False elif check_type == 'FILE': if tmp_list is not None: for item in tmp_list: if item['path'].lower() == check_item and item['type'] == 'file': return True return False elif path.find('/') != -1: location = path[0:path.rfind('/')] check_item = path[path.rfind('/')+1:].lower() if (check_item is None) or (check_item == ""): return False try: tmp_list = self.list_files(location) except Exception, e: raise e if check_type == 'FOLDER': if tmp_list is not None: for item in tmp_list: if item['path'].lower() == check_item and item['type'] == 'directory': return True return False elif check_type == 'FILE': if tmp_list is not None: print ".........not valid "
def is_path_exists(self, path, check_type = 'FILE'): """ @path[string]: the path you want to check exists or not @check_type[string]: the type you check, FILE(default) or FOLDER If the path exists in the VM, return True, else return False """ if self.__login_flag == False: print 'Path check error: Sorry, but please login the VM first.' return False if check_type not in ['FILE', 'FOLDER']: print "Check path error: check_type must be set as 'FILE' or 'FOLDER'" return False if path.endswith('\\'): path = path[:-1] if path.find('\\') != -1 and path.find('/') != -1: print "Check path error: path must be set as 'c:\\temp\\file.file' or 'c:/temp/file.file'" return False if path.find('\\') != -1: location = path[0:path.rfind('\\')] check_item = path[path.rfind('\\')+1:].lower() if (check_item is None) or (check_item == ""): return False try: tmp_list = self.list_files(location) except Exception as e: raise e if check_type == 'FOLDER': if tmp_list is not None: for item in tmp_list: if item['path'].lower() == check_item and item['type'] == 'directory': return True return False elif check_type == 'FILE': if tmp_list is not None: for item in tmp_list: if item['path'].lower() == check_item and item['type'] == 'file': return True return False elif path.find('/') != -1: location = path[0:path.rfind('/')] check_item = path[path.rfind('/')+1:].lower() if (check_item is None) or (check_item == ""): return False try: tmp_list = self.list_files(location) except Exception, e: raise e if check_type == 'FOLDER': if tmp_list is not None: for item in tmp_list: if item['path'].lower() == check_item and item['type'] == 'directory': return True return False elif check_type == 'FILE': if tmp_list is not None:
def GetPathNameWithExt(path): idx = path.rfind("/") if idx < 0: idx = path.rfind("\\") if idx < 0: return path ret = path[(idx + 1):] return ret
def _file_part(path): sep_pos = path.rfind("/") if sep_pos == -1: sep_pos = path.rfind("\\") if sep_pos == -1: return path return path[sep_pos+1:]
def findPath(self,rootpath): servers = ServerCodes if self.parent.euroServers: servers = EuroServerCodes self.model.removeRows(0, self.model.rowCount()) rootpath = unicode(rootpath) if os.path.isdir(rootpath): filelist = glob.glob(rootpath+'/*-*.ini') filelist.extend(glob.glob(rootpath+'/*/*-*.ini')) filelist.extend(glob.glob(rootpath+'/*/*/*-*.ini')) for file in filelist: path = file[len(rootpath)+1:] slash = path.rfind('/') slash = max(slash, path.rfind('\\')) m = self.reini.search(path[(slash+1):]) if slash < 0: path = '' else: path = path[:slash] if m is None or not servers.has_key(m.group(2)): continue # search the section(s) for the pattern # [Quickbar[2-3]*] # Hotkey_[0-9]*=44,13, # corresponding to an SC'er menu quickbar item f = open(file, 'r') find = 0 for txt in f: sec = self.resec.match(txt) if sec is not None: if sec.group(1)[:8] == 'Quickbar': find = 1 else: find = 0 continue if find == 1 and self.rectl.match(txt) is not None: find = 2 break f.close() if find < 2: continue server = servers[m.group(2)] self.model.insertRows(self.model.rowCount(), 1) index = self.model.index(self.model.rowCount()-1, 0, QModelIndex()) self.model.setData(index, QVariant(path), Qt.DisplayRole) self.model.setData(index, QVariant(file), Qt.UserRole) index = self.model.index(self.model.rowCount()-1, 1, QModelIndex()) self.model.setData(index, QVariant(server), Qt.DisplayRole) index = self.model.index(self.model.rowCount()-1, 2, QModelIndex()) self.model.setData(index, QVariant(m.group(1)), Qt.DisplayRole) if self.model.rowCount() == 1: self.CharList.selectRow(0) if len(filelist) > 0: ScOptions.instance().setOption('DaocIniPath', rootpath) self.CharList.resizeRowsToContents()
def findPath(self,rootpath): servers = ServerCodes self.model.removeRows(0, self.model.rowCount()) rootpath = unicode(rootpath) if os.path.isdir(rootpath): filelist = glob.glob(rootpath+'/*-*.ini') filelist.extend(glob.glob(rootpath+'/*/*-*.ini')) filelist.extend(glob.glob(rootpath+'/*/*/*-*.ini')) for file in filelist: path = file[len(rootpath)+1:] slash = path.rfind('/') slash = max(slash, path.rfind('\\')) m = self.reini.search(path[(slash+1):]) if slash < 0: path = '' else: path = path[:slash] if m is None or not servers.has_key(m.group(2)): continue # search the section(s) for the pattern # [Quickbar[2-3]*] # Hotkey_[0-9]*=44,13, # corresponding to an SC'er menu quickbar item f = open(file, 'r') find = 0 for txt in f: sec = self.resec.match(txt) if sec is not None: if sec.group(1)[:8] == 'Quickbar': find = 1 else: find = 0 continue if find == 1 and self.rectl.match(txt) is not None: find = 2 break f.close() if find < 2: continue server = servers[m.group(2)] self.model.insertRows(self.model.rowCount(), 1) index = self.model.index(self.model.rowCount()-1, 0, QModelIndex()) self.model.setData(index, QVariant(path), Qt.DisplayRole) self.model.setData(index, QVariant(file), Qt.UserRole) index = self.model.index(self.model.rowCount()-1, 1, QModelIndex()) self.model.setData(index, QVariant(server), Qt.DisplayRole) index = self.model.index(self.model.rowCount()-1, 2, QModelIndex()) self.model.setData(index, QVariant(m.group(1)), Qt.DisplayRole) if self.model.rowCount() == 1: self.CharList.selectRow(0) if len(filelist) > 0: ScOptions.instance().setOption('DaocIniPath', rootpath) self.CharList.resizeRowsToContents()
def execute(self): copy_buffer = tuple(self.fm.copy_buffer) if not copy_buffer or len(copy_buffer) != 1: return cf = copy_buffer[0] #path = cf.relative_path.replace("%", "%%") path = cf.relative_path if path.find('.') != 0 and path.rfind('.') != -1 and not cf.is_directory: self.fm.open_console('paste_as ' + path, position=(len("paste_as") + 1 + path.rfind('.'))) else: self.fm.open_console('paste_as ' + path)
def run(self, edit): self.ruta=sublime.packages_path()+os.sep+"Plugins"+os.sep+"historyFiles.json" window=sublime.active_window() self.archivos=sublime.decode_value(open(self.ruta).read()) if not self.archivos:return self.lista=[] self.archivos["archivos"]=list(reversed(self.archivos["archivos"])) for path in self.archivos["archivos"]: if not os.path.exists(path):continue if path.find("/")!=-1: self.lista.append([path[path.rfind("/")+1:], path]) else: self.lista.append([path[path.rfind("\\")+1:], path]) window.show_quick_panel(self.lista, self.seleccion)
def safePath(obj): """ Récupère de façon sûre le path d'une instance de UDisksObjectProxy @param obj instance de UDisksObjectProxy, ou simple chaine """ if type(obj)==type(""): path=obj else: path= obj.get_object_path() posUnderscore=path.rfind("_") posSlash=path.rfind("/") if posUnderscore > posSlash: # il faut retirer tout après l'underscore final path=path[:posUnderscore] return path
def execute(self): copy_buffer = tuple(self.fm.copy_buffer) if not copy_buffer or len(copy_buffer) != 1: return cf = copy_buffer[0] #path = cf.relative_path.replace("%", "%%") path = cf.relative_path if path.find('.') != 0 and path.rfind( '.') != -1 and not cf.is_directory: self.fm.open_console('paste_as ' + path, position=(len("paste_as") + 1 + path.rfind('.'))) else: self.fm.open_console('paste_as ' + path)
def list(): # Only allow tail/grep on files in the directory validfiles = {} # Filter log files for dirs specified in the config for dir in config['directories']: for ext in config['extensions']: # Glob for all files specified in the config paths = glob.glob(dir + "/*." + ext) for path in paths: # store dict of validfiles using the filename hindex = path.rfind('/') filename = path[hindex+1:] # Generate URLs if os.path.getsize(path) > 0: size = str(os.path.getsize(path)) uniquefilename = filename + "_" + str(uuid.uuid1()) print "Added " + path + " " + uniquefilename validfiles[uniquefilename] = [path, size] session['grepnumlines'] = str(config['grepnumlines']) session['searchbeforecontext'] = str(config['searchbeforecontext']) session['searchaftercontext'] = str(config['searchaftercontext']) session['validfiles'] = validfiles return render_template('list.html')
def GetFileExt(path): idx = path.rfind(".") + 1 slen = len(path) size = slen - idx ret = path[idx:] # print(path="+path+" idx="+str(idx)+" ret="+ret+" slen="+str(slen) return ret
def __call__(self, environ, start_response): """WSGI dispatch. Args: environ: environment list. start_response: WSGI response start. Returns: Iterable server result. """ path = environ.get('PATH_INFO', '') while path.startswith('/'): path = path[1:] filename = os.path.join(self._source_dir, path) if not os.path.exists(filename): logging.info('%s not found', filename) start_response('404 Not Found', [('Content-Type', 'text/html')]) return [ """<!DOCTYPE html> <html> <head> <title>Not Found</title> <body>%s not found</body> </html>""" % path ] logging.info('responding with %s', filename) suffix = path[path.rfind('.') + 1:] headers = [('Content-Type', _CONTENT_TYPE_FOR_SUFFIX[suffix])] if path in self._response_headers: for header in self._response_headers[path]: headers.append((str(header[0]), str(header[1]))) start_response('200 OK', headers) return [file(filename).read()]
def _transEffectInfo(self, transition, type): res = {} action = transition.getAction() res['sourcestate'] = transition.getSourceState() res['targetstate'] = transition.getTargetState() res['transition'] = transition.getName() res['workflow'] = transition.getParent().getCleanName() if type == 'before': res['effectname'] = action.getBeforeActionName() res['wfinterface'] = 'Products.DCWorkflow.interfaces.IBeforeTransitionEvent' else: res['effectname'] = action.getAfterActionName() res['wfinterface'] = 'Products.DCWorkflow.interfaces.IAfterTransitionEvent' klass = res['sourcestate'].getParent().getParent() if not isinstance(klass,XMIClass) or klass.hasStereoType(self.atgenerator.noncontentstereotype, umlprofile=self.atgenerator.uml_profile): defaultbinding = '*' else: # take the generated marker interface as defaultbinding path = klass.getQualifiedModuleName(klass.getPackage(), includeRoot=1) if path.startswith(self.package.getProduct().getName()): # strip product name - shouldnt be in there, xmiparser is here # a bit buggy :-( path = path[len(self.package.getProduct().getName()):] rdot = path.rfind('.') if rdot > 0: path = '.' + path[:rdot+1] else: path = '.' defaultbinding = '%sinterfaces.I%s' % (path, klass.getCleanName()) tag = '%s:binding' % type res['objinterface'] = action.getTaggedValue(tag, defaultbinding) return res
def translate_parameters(self): if "parameters" not in self: return config = OS2borgerPCConfig() admin_url = config.get_value("admin_url") local_params = [] self["local_parameters"] = local_params params = self["parameters"] del self["parameters"] for index, param in enumerate(params): if param["type"] == "FILE": # Make sure we have the directory if not os.path.isdir(self.attachments_path): os.mkdir(self.attachments_path) value = param["value"] _, _, path, _, _, _ = urllib.parse.urlparse(value) basename = path[path.rfind("/") + 1 :] or "file" local_filename = os.path.join( self.attachments_path, str(index) + "_" + basename ) # urljoin does the right thing for both relative and absolute # values of, er, value full_url = urllib.parse.urljoin(admin_url, value) remote_file = urllib.request.urlopen(full_url) with open(local_filename, "wb") as attachment_fh: attachment_fh.write(remote_file.read()) local_params.append(local_filename) else: local_params.append(param["value"])
def corrupt(path): phash = hash_(path) parent = path[:path.rfind('/')] # key for parent if len(parent) == 0: parent = '/' meta = get(parent) try: file_size = meta[path]["st_size"] except KeyError: print "File Not found" return total_blocks = (file_size - 1) // block_size + 1 if total_blocks > 2: blk = random.randint(0, total_blocks - 1 - 2) else: blk = random.randint(0, total_blocks - 1) ack1 = False ack2 = False ack1 = dataserv[(phash + blk) % len(dataserv)].corrupt_data( Binary(str(blk) + path)) if corruptNUM == str(2) and total_blocks > 2: ack2 = dataserv[(phash + blk + 2) % len(dataserv)].corrupt_data( Binary(str(blk + 2) + path)) if ack1 and ack2 == True: print "Corrupted block number: %d and %d of file: %s. Assoicated Server number are %d and %d:" % ( blk, blk + 2, path, (phash + blk) % len(dataserv), (phash + blk + 2) % len(dataserv)) else: if ack1 == True: print "Corrupted block number: %d of file: %s. Assoicated Server number are %d:" % ( blk, path, (phash + blk) % len(dataserv))
def _make_mode(vm_path, path, bc, verbosity, mk_fresh): # Try to work out a plausible cached path name. dp = path.rfind(os.extsep) if dp >= 0 and os.sep not in path[dp:]: cp = path[:dp] else: cp = None if not cp or mk_fresh: return _do_make_mode(vm_path, path, None, verbosity, mk_fresh) else: # There is a cached path, so now we try and load it and see if it is upto date. If any part # of this fails, we simply go straight to full make mode. try: st = os.stat(cp) except OSError: return _do_make_mode(vm_path, path, cp, verbosity, mk_fresh) cbc, start = _read_bc(cp, "CONVEXEC") if start == -1: return _do_make_mode(vm_path, path, cp, verbosity, mk_fresh) assert start >= 0 useful_bc = cbc[start:] if Bytecode.exec_upto_date(None, rffi.str2charp(useful_bc), st.st_mtime): return cbc, start, 0 return _do_make_mode(vm_path, path, cp, verbosity, mk_fresh)
def read_from_path(path): label = int(path[path.rfind('-') + 1:-4]) with FileSystems.open(path) as f: binary = f.read() base64_str = base64.urlsafe_b64encode(binary) row = {'label': label, 'image': base64_str} return row
def image_load(path): r""" Given a '.npy' or '.png' file, this function loads the file and returns its content as a two-dimensional :class:`numpy.ndarray` of :class:`float` values. For '.png' images, the pixel values are normalized to be between 0 and 1 (instead of between 0 and 255) and color images are converted to grey-scale. **Required parameter** :param string path: Path to the image to be converted, either of a '.png' or '.npy' file. **Return value** :return: The loaded image as a two-dimensional :class:`numpy.ndarray`. """ image_extension = path[path.rfind('.'):] if image_extension == '.npy': return np.array(np.load(path), dtype='float64') elif image_extension == '.png': return np.array(scipy.ndimage.imread(path, flatten=True) / 255.0, dtype='float64') else: raise ValueError("This function can only load .png or .npy files.")
def __call__(self, path: str = None, **kwargs): if path is not None: path = os.path.expanduser(path) if os.path.isdir(path): path = os.path.join(path, '.xtsenv.py') module_name = os.path.basename(path)[:path.rfind('.')] if module_name == '.xtsenv': module_name = '__hidden_xtsenv__' try: env_mod = importlib.util.spec_from_file_location( 'xts.env.' + module_name, path).loader.load_module() except ImportError: return else: for key in dir(env_mod): value = getattr(env_mod, key) if isinstance(value, DataRoot) or \ isinstance(value, DataSource): setattr(self, key, value) for key, value in kwargs.items(): setattr(self, key, value)
def list(): # Only allow tail/grep on files in the directory validfiles = {} # Filter log files for dirs specified in the config for dir in config['directories']: for ext in config['extensions']: # Glob for all files specified in the config paths = glob.glob(dir + "/*." + ext) for path in paths: # store dict of validfiles using the filename hindex = path.rfind('/') filename = path[hindex + 1:] # Generate URLs if os.path.getsize(path) > 0: size = str(os.path.getsize(path)) uniquefilename = filename + "_" + str(uuid.uuid1()) print "Added " + path + " " + uniquefilename validfiles[uniquefilename] = [path, size] session['grepnumlines'] = str(config['grepnumlines']) session['searchbeforecontext'] = str(config['searchbeforecontext']) session['searchaftercontext'] = str(config['searchaftercontext']) session['validfiles'] = validfiles return render_template('list.html')
def saveWand(self, path, compress=None, depth=None): ''' Vendor-specific :py:func:`~openlut.ColMap.save` function. See :py:func:`~openlut.ColMap.save` :param str path: The image path to save to. :param compress: Compression options passed to Wand. Currently broken. :param depth: You may override the ColMap's depth if you wish. :type depth: int or None **NOTE: EXRs are only saveable as 16-bit integer, with no compression options. This is an IM/Wand library limitation.** ''' if depth not in ColMap.DEPTHS.values(): raise ValueError( 'Bit depth not supported! Supported bit depths: {}'.format( ', '.join(ColMap.DEPTHS.values()))) data = self.apply(LUT.lutFunc( gamma.sRGB)) if path[path.rfind('.') + 1:] == 'dpx' else self i = data.asWandImg(depth) i.colorspace = 'srgb' #Make sure it saves without a colorspace transformation. #~ if compress : #~ library.MagickSetCompression(i.wand, 'rle') #~ i.compression = 'lzma' #~ i.compression_quality = 80 i.save(filename=path)
def _zip_dir(path): """ zips a path to /tmp/iapydependencies.zip. Please note that this zip file will be truncated every time this call is made. So to preserve the contents, read the file immediately or copy. Not thread-safe. """ # todo: fix such that this is thread-safe, and does not continue to zip and rezip _make_archive(_get_dir_entries(path, True), _zip_file_path, path[0:path.rfind('/')])
def getHref(self, path): "return an a href to this path for editing" if path: cdoc = self.getCompoundDoc() script = cdoc.restrictedTraverse(path, None) if script is not None: try: parent = script.aq_parent except AttributeError: parent = self.getCompoundDocContainer() try: return '<a href="%s/manage">Edit: %s</a> <a href="%s/manage">Parent Edit: %s</a>' % ( script.absolute_url_path(), script.title_or_id(), parent.absolute_url_path(), parent.title_or_id(), ) except AttributeError: return "" elif "/" in path: path = path[: path.rfind("/")] parent = cdoc.restrictedTraverse(path, None) if parent is not None: try: return '<a href="%s/manage">Parent Edit: %s</a>' % ( parent.absolute_url_path(), parent.title_or_id(), ) except AttributeError: return "" return ""
def _dirname(path): # An RPython compatible version since that in os.path currently isn't... i = path.rfind('/') + 1 assert i > 0 head = path[:i] if head and head != '/' * len(head): head = head.rstrip('/') return head
def __call__(self, environ, start_response): """WSGI dispatch. Args: environ: environment list. start_response: WSGI response start. Returns: Iterable server result. """ path = environ.get('PATH_INFO', '') while path.startswith('/'): path = path[1:] filename = os.path.join(self._source_dir, path) if not os.path.exists(filename): logging.info('%s not found', filename) start_response('404 Not Found', [('Content-Type', 'text/html')]) return ["""<!DOCTYPE html> <html> <head> <title>Not Found</title> <body>%s not found</body> </html>""" % path] logging.info('responding with %s', filename) suffix = path[path.rfind('.') + 1:] headers = [('Content-Type', _CONTENT_TYPE_FOR_SUFFIX[suffix])] if path in self._response_headers: for header in self._response_headers[path]: headers.append((str(header[0]), str(header[1]))) start_response('200 OK', headers) return [file(filename).read()]
def addnode(path,_GUI,ctype="",table="",qty=1,extra="",mult=1): newnode = GUITreeNode(path,_GUI) x = path.rfind("/") newnode.text = path[x+1:] newnode.ctype = ctype newnode.table = table newnode.qty = qty newnode.extra = extra if mult == -1: newnode.XMLFileListNo = newnode.XMLFileListNo * -1 if XMLPathList.count( path[:x] ) > 0: XMLPathList.append( path ) temp = XMLPathList.index( path[:x] ) parentID = NodeList[ temp ].ID newnode.makedisplay(_GUI,newnode) newnode.ID = _GUI.Tree.AppendItem( parentID , newnode.display ) _GUI.Tree.SetPyData(newnode.ID , None) _GUI.Tree.SetItemImage(newnode.ID , _GUI.cicon , wx.TreeItemIcon_Normal) NodeList.append(newnode) else: _GUI.NError("ERROR: could not find path:\n" + path) return newnode
def set_status(self, set_title=0): """ set_status(set_title=0) Check if the link target exists. Check if the target file is the current file. Could also parse <title> from the file and set it to title. """ if self.target.startswith('http://'): self.error = 0 return path = self.target tmp = path.rfind("#") if tmp != -1: path = path[0:tmp] if not os.path.isfile(path): self.error = 1 if not self.error and set_title: file = open(path) title = re.compile('<title>([^<]*)</title>') strip = re.compile('[a-zA-ZåäöÅÄÖ\-\_ ]*') title = title.findall(file.read()) file.close() if len(title) > 0: self.title = strip.findall(title[0])[0] if not self.error and os.path.abspath(path) \ == os.path.abspath(slashify(config.working_directory)+config.output_filename): self.focus = 1 self.target = '' self.title = ''
def get_action(self, req): """Take a given Request, parse url path to calculate action type. Depending on req.method: if POST: - path ends with 'action', read the body and use as action; - path ends with known custom_action, take action from config; - request ends with known path, assume is create action; - request ends with unknown path, assume is update action. if GET: - request ends with known path, assume is list action; - request ends with unknown path, assume is read action. if PUT, assume update action. if DELETE, assume delete action. if HEAD, assume read action. """ path = req.path[:-1] if req.path.endswith('/') else req.path url_ending = self._clean_path(path[path.rfind('/') + 1:]) method = req.method if url_ending + '/' + method.lower() in self._MAP.custom_actions: action = self._MAP.custom_actions[url_ending + '/' + method.lower()] elif url_ending in self._MAP.custom_actions: action = self._MAP.custom_actions[url_ending] elif method == 'POST': if url_ending == 'action': try: if req.json: body_action = list(req.json.keys())[0] action = taxonomy.ACTION_UPDATE + '/' + body_action else: action = taxonomy.ACTION_CREATE except ValueError: action = taxonomy.ACTION_CREATE elif url_ending not in self._MAP.path_kw: action = taxonomy.ACTION_UPDATE else: action = taxonomy.ACTION_CREATE elif method == 'GET': if url_ending in self._MAP.path_kw: action = taxonomy.ACTION_LIST else: action = taxonomy.ACTION_READ elif method == 'PUT' or method == 'PATCH': action = taxonomy.ACTION_UPDATE elif method == 'DELETE': action = taxonomy.ACTION_DELETE elif method == 'HEAD': action = taxonomy.ACTION_READ else: action = taxonomy.UNKNOWN return action
def readFile(path): s = path.rfind('/') name = path[s + 1:] s = name.find('.') name = name[0:s] with open(path, 'rb') as file: head = file.read(4) assert head == b'ZLB\0' version, decLen, compLen = struct.unpack('>3I', file.read(12)) assert version == 1 data = file.read(compLen) fileData = zlib.decompress(data) assert len(fileData) == decLen offs = 0 idx = 0 while offs < len(fileData): typ, size, flags = struct.unpack_from('>hBB', fileData, offs) unk4, unk5, unk6, map, x, y, z, id = struct.unpack_from( '>BBBBfffi', fileData, offs + 4) realId = objIndex.get(typ, typ) objName = objNames.get(realId, '?') if realId in objParamLen: if objParamLen[realId] != size: objParamLen[realId] = "varies" else: objParamLen[realId] = size if size > 6 and realId not in exampleParams: exampleParams[realId] = fileData[offs + 0x18:offs + (size * 4)] idx += 1 offs += size * 4
def get_baseinfo(selected_hash,selected_file,hash_list): #准备需要的数据,检查文件名字是否合法,获取改变的文件名字、路径、改变模式等等 #print selected_hash or "None" file_info=repo.git.show(r"--pretty=""","--name-status",selected_hash) file_info=file_info.split("\n") file_cmode=[ line[:line.find("\t")] for line in file_info if line ] file_path=[ line[line.find("\t")+1:].strip() for line in file_info if line] file_name=[ path[ path.rfind("/")+1:] for path in file_path if line ] file_path=[ project_path+"/"+path for path in file_path if line] file_path=dict(zip(file_name,file_path)) file_cmode=dict(zip(file_name,file_cmode)) selected_file = ( selected_file if selected_file in file_name else file_name[0] ) if file_name else None path_name=file_path[selected_file] if selected_file else None c_type=file_cmode[selected_file] if selected_file else None return selected_file,path_name,c_type,file_name
def test_get_anchorhub_path_directory(): """ getanchorhubpath.py: Make sure directory given is named 'anchorhub' """ path = get_anchorhub_path() dir = path[path.rfind(get_path_separator()) + 1:] assert dir == 'anchorhub'
def open(path): ''' Construct a ColMap from an image on the disk. :param str path: The image path to open. :return: The image, as a ColMat. :rtype: :py:class:`~openlut.ColMap` ColMap currently uses ImageMagick to open a wide range of formats, including: * **EXR**: The industry standard for HDR, wide-gamut, linear-encoded images. * **DPX**: An older production format. * **PNG**: Can store 16-bit images well. Usually quite slow. * *Any other IM-supported formats...* See https://www.imagemagick.org/script/formats.php ''' try: openFunction = { "exr": ColMap.openWand, "dpx": ColMap.openWand, }[path[path.rfind('.') + 1:]] return openFunction(path) #Any fancy formats will go here. except: #Fallback to opening using Wand. return ColMap.openWand(path)
def get_save_directory(multiple_files=False, save_as_image=False, default_ext=None): """ Show file dialog so user can choose where to save. :param multiple_files: boolean - whether more than one file is being saved :param save_as_image: boolean - whether to allow saving as a .png/pdf :param default_ext: file extension that is selected by default :return: path to save directory, name to save the file as, file format extension :raises: RuntimeError if dialog is cancelled """ if multiple_files: return QFileDialog.getExistingDirectory(), None, default_ext else: file_dialog = QFileDialog() file_dialog.setAcceptMode(QFileDialog.AcceptSave) filter = "Nexus (*.nxs);; Ascii (*.txt);; Matlab (*.mat)" if save_as_image: filter += ";; Image (*.png);; PDF (*.pdf)" file_dialog.setNameFilter(filter) if default_ext: ext_to_qtfilter = {'.nxs': 'Nexus (*.nxs)', '.txt': 'Ascii (*.txt)', '.mat': 'Matlab (*.mat)', } file_dialog.selectNameFilter(ext_to_qtfilter[default_ext]) if (file_dialog.exec_()): path = str(file_dialog.selectedFiles()[0]) if '.' not in path: # add extension unless there's one in the name path += file_dialog.selectedFilter()[-5:-1] ext = path[path.rfind('.'):] return os.path.dirname(path), os.path.basename(path), ext
def __call__(self, environ, start_response): """WSGI dispatch. Args: environ: environment list. start_response: WSGI response start. Returns: Iterable server result. """ path = environ.get('PATH_INFO', '') while path.startswith('/'): path = path[1:] filename = os.path.join(self._source_dir, path) if not os.path.exists(filename): logging.info('%s not found', filename) start_response('404 Not Found', [('Content-Type', 'text/html')]) return ["""<!DOCTYPE html> <html> <head> <title>Not Found</title> <body>%s not found</body> </html>""" % path] logging.info('responding with %s', filename) suffix = path[path.rfind('.') + 1:] start_response('200 OK', [('Content-Type', {'css': 'text/css', 'html': 'text/html', 'jpg': 'image/jpeg', 'js': 'text/javascript', 'png': 'image/png', 'ttf': 'font/ttf', }[suffix])]) return file(filename).read()
def readFile(path): s = path.rfind('/') name = path[s + 1:] s = name.find('.') name = name[0:s] try: mapId = mapIds[name] except KeyError: missingMaps.add(name) return with open(path, 'rb') as file: head = file.read(4) assert head == b'ZLB\0' version, decLen, compLen = struct.unpack('>3I', file.read(12)) assert version == 1 data = file.read(compLen) fileData = zlib.decompress(data) assert len(fileData) == decLen offs = 0 while offs < len(fileData): typ, size, flags = struct.unpack_from('>hBB', fileData, offs) unk4, unk5, unk6, map, x, y, z, id = struct.unpack_from( '>BBBBfffi', fileData, offs + 4) realId = objIndex.get(typ, typ) objects[realId][mapId] += 1 offs += size * 4
def getHref(self, path): "return an a href to this path for editing" if path: cdoc = self.getCompoundDoc() script = cdoc.restrictedTraverse(path, None) if script is not None: try: parent = script.aq_parent except AttributeError: parent = self.getCompoundDocContainer() try: return '<a href="%s/manage">Edit: %s</a> <a href="%s/manage">Parent Edit: %s</a>' % ( script.absolute_url_path(), script.title_or_id(), parent.absolute_url_path(), parent.title_or_id()) except AttributeError: return '' elif '/' in path: path = path[:path.rfind('/')] parent = cdoc.restrictedTraverse(path, None) if parent is not None: try: return '<a href="%s/manage">Parent Edit: %s</a>' % ( parent.absolute_url_path(), parent.title_or_id()) except AttributeError: return '' return ''
def findConfigFile(path: str, name: str) -> Optional[str]: """ Search for a config file. Search for a file of a given name from the directory specifyed by path through all parent directories. The first file found is selected. Parameters ---------- path : str The path where the search starts. name : str The file to be found. Returns ------- Optional[str] The path where the file has been found or None if no matching file has been found. """ while True: p = f"{path}/{name}" if os.path.isfile(p): return p else: loc = path.rfind("/") if loc == -1: return None path = path[:loc]
def make_train_data(target_dir, prepared_data_dir): train = open(prepared_data_dir + os.sep + 'train.txt', 'w') test = open(prepared_data_dir + os.sep + 'test.txt', 'w') labelsTxt = open(prepared_data_dir + os.sep + 'labels.txt', 'w') classNo = 0 count = 0 for path, dirs, files in os.walk(target_dir): if not dirs: start = path.rfind(os.sep) + 1 labelsTxt.write(path[start:].split(os.sep)[0] + "\n") startCount = count length = len(files) for f in files: if(f.split('.')[-1] not in ["jpg", "jpeg", "gif", "png"]): continue imagepath = prepared_data_dir + os.sep + "image%07d" %count + ".jpg" resize_image(os.path.join(path, f), imagepath) if count - startCount < length * 0.75: train.write(imagepath + " %d\n" % classNo) else: test.write(imagepath + " %d\n" % classNo) count += 1 classNo += 1 train.close() test.close() labelsTxt.close() return
def process_request(self, req): parent_id = None parent_realm = req.args.get('realm') path = req.args.get('path') filename = None if not parent_realm or not path: raise HTTPBadRequest(_('Bad request')) if parent_realm == 'attachment': raise TracError(tag_("%(realm)s is not a valid parent realm", realm=tag.code(parent_realm))) parent_realm = Resource(parent_realm) action = req.args.get('action', 'view') if action == 'new': parent_id = path.rstrip('/') else: last_slash = path.rfind('/') if last_slash == -1: parent_id, filename = path, '' else: parent_id, filename = path[:last_slash], path[last_slash + 1:] parent = parent_realm(id=parent_id) if not resource_exists(self.env, parent): raise ResourceNotFound( _("Parent resource %(parent)s doesn't exist", parent=get_resource_name(self.env, parent))) # Link the attachment page to parent resource parent_name = get_resource_name(self.env, parent) parent_url = get_resource_url(self.env, parent, req.href) add_link(req, 'up', parent_url, parent_name) add_ctxtnav(req, _('Back to %(parent)s', parent=parent_name), parent_url) if not filename: # there's a trailing '/' if req.args.get('format') == 'zip': self._download_as_zip(req, parent) elif action != 'new': return self._render_list(req, parent) attachment = Attachment(self.env, parent.child(self.realm, filename)) if req.method == 'POST': if action == 'new': data = self._do_save(req, attachment) elif action == 'delete': self._do_delete(req, attachment) else: raise HTTPBadRequest(_("Invalid request arguments.")) elif action == 'delete': data = self._render_confirm_delete(req, attachment) elif action == 'new': data = self._render_form(req, attachment) else: data = self._render_view(req, attachment) add_stylesheet(req, 'common/css/code.css') return 'attachment.html', data, None
def load_backend(self, path): i = path.rfind('.') module, attr = path[:i], path[i+1:] try: mod = import_module(module) except ImportError, e: raise ImproperlyConfigured('Error importing synchronization backend %s: "%s"' % (module, e))
def loadFTP(self, URL, localfile='', timeout=url_open_timeout, proxy="CACHING", \ content_type= '', retries=0): self.state = 0 #success #Parse URL according RFC 1738: ftp://user:password@host:port/path #There is no standard Python funcion to split these URL's. username='' password='' port=21 #check for username, password index = URL.find('@') if index != -1: index2 = URL.find(':',6,index) if index2 != -1: username = URL[6:index2] print 'user: '******'password: '******'/') if index != -1: host = URL[:index] path = URL[index:] else: host = URL path = '' #retrieve the port index = host.find(':') if index != -1: port = int(host[index+1:]) host = host[:index] print 'host: ' + host print 'port: ' + str(port) #split path and file index = path.rfind('/') if index != -1: file = path[index+1:] path = path[:index] else: file = '' print 'path: ' + path print 'file: ' + file try: self.f = ftplib.FTP() self.f.connect(host,port) except (socket.error, socket.gaierror), e: print 'ERROR: cannot reach "%s"' % host self.state = -1 #failed to download the file return
def _zip_dir(path): """ zips a path to /tmp/iapydependencies-<UUID>.zip and returns that path. Name is hardened to allow for concurrency. """ file_path = '/tmp/iapydependencies-' + uuid.uuid1().hex + ".zip" _make_archive(_get_dir_entries(path, True), file_path, path[0:path.rfind('/')]) return file_path
def load_old_learner(self,filepath=None,trainSet1=None,trainSet2=None,stage1=-1,stage2=-1): # if their is no trainSet1 and trainSet2, we suppose that their is one saved in the learner if not filepath: assert(not self.learner1.expdir.endswith("/learner1") or not self.learner2.expdir.endswith("/learner2")) path=self.learner1.expdir[:-9] assert(path==self.learner2.expdir[:-9]) i=path.rfind("-2007-") (subdir,subfile)=os.path.split(path[:i]) tmp=[x for x in os.listdir(subdir) if x.startswith(subpath)] assert(len(tmp)>0) filepath=max(tmp) #if stage=-1 we find the last one if stage1 == -1: s="learner1_stage=" lens=len(s) e=".psave" lene=len(e) tmp=[ x for x in os.listdir(filepath) if x.startswith(s) and x.endswith(".psave") ] for file in tmp: t=int(file[lens:-lene]) if t>stage1: stage1=t #We must split stage1 and stage2 as one learner can early stop. if stage2 == -1: s="learner2_stage=" lens=len(s) e=".psave" lene=len(e) tmp=[ x for x in os.listdir(filepath) if x.startswith(s) and x.endswith(e) ] for x in tmp: t=int(x[lens:-lene]) if t>stage2: stage2=t file1=os.path.join(filepath,"learner1_stage="+str(stage1)+".psave") file2=os.path.join(filepath,"learner2_stage="+str(stage2)+".psave") if (not os.path.exists(file1)) or (not os.path.exists(file2)): print "ERROR: no file to load in the gived directory" print "file",file1,"and file",file2, "do not exist" sys.exit(1) self.learner1=loadObject(file1) self.learner2=loadObject(file2) if not self.learner1.found_zero_error_weak_learner and not self.learner2.found_zero_error_weak_learner: assert(self.learner1.stage==self.learner2.stage) self.stage=self.learner1.stage self.nstages=self.learner1.nstages if trainSet1: self.learner1.setTrainingSet(trainSet1,False) if trainSet2: self.learner2.setTrainingSet(trainSet2,False) self.learner1.setTrainStatsCollector(VecStatsCollector()) self.learner2.setTrainStatsCollector(VecStatsCollector()) self.multi_class_adaboost.learner1=self.learner1 self.multi_class_adaboost.learner2=self.learner2 self.multi_class_adaboost.build() for (learner,trainSet) in [(self.learner1,trainSet1), (self.learner2,trainSet2)]: for weak in learner.weak_learners: weak.setTrainingSet(trainSet,False)
def get_matches(self, path, escaped_path): if escaped_path: path = remove_escape_spaces(path) pattern = path + '*' # print "pattern:", pattern matches = [] for fname in iglob(pattern): completion = os.path.basename(fname) if escaped_path: completion = escape_scapes(completion) text = '' if os.path.isdir(fname): text = '%s/\tDir' % completion else: text = '%s\tFile' % completion # FIXME: # this one is a bit weird # for example if there are three files: # /quick test # /quick test 1 # /quick test 2 # and user types 'quick' and asks for compliction # he will get all three choises, so far so good, # if he selects the first one, everything is fine, but # if he selects the second or third, the code completin will replace # /quick quick test 1 # /quick quick test 2 # I guess the problem is that the first item in the tuple is # what should be replaced, but it only works on words and # space will break the word boundary # last word that will be completed (is highlighted in the box) # if the last charcated is a space then it will be the whole word # from the last separator lastword = path[path.rfind(sep)+1:] # difference between the completion and the lastword rest = '' if path[-1] != ' ': lastword = lastword[lastword.rfind(' ')+1:] rest = completion[completion.find(lastword):] else: rest = completion[completion.find(lastword)+len(lastword):] if rest.find(' ') != -1: completion = rest matches.append((text, completion)) return matches
def get_source_info(self, source_spec): """ This turns an argv string into a (Local|Tahoe)(File|Directory)Source. """ precondition(isinstance(source_spec, unicode), source_spec) rootcap, path_utf8 = get_alias(self.aliases, source_spec, None) path = path_utf8.decode("utf-8") # any trailing slash is removed in abspath_expanduser_unicode(), so # make a note of it here, to throw an error later had_trailing_slash = path.endswith("/") if rootcap == DefaultAliasMarker: # no alias, so this is a local file pathname = abspath_expanduser_unicode(path) name = os.path.basename(pathname) if not os.path.exists(pathname): raise MissingSourceError(source_spec, quotefn=quote_local_unicode_path) if os.path.isdir(pathname): t = LocalDirectorySource(self.progress, pathname, name) else: if had_trailing_slash: raise FilenameWithTrailingSlashError(source_spec, quotefn=quote_local_unicode_path) if not os.path.isfile(pathname): raise WeirdSourceError(pathname) t = LocalFileSource(pathname, name) # non-empty else: # this is a tahoe object url = self.nodeurl + "uri/%s" % urllib.quote(rootcap) name = None if path: if path.endswith("/"): path = path[:-1] url += "/" + escape_path(path) last_slash = path.rfind(u"/") name = path if last_slash != -1: name = path[last_slash+1:] resp = do_http("GET", url + "?t=json") if resp.status == 404: raise MissingSourceError(source_spec) elif resp.status != 200: raise HTTPError("Error examining source %s" % quote_output(source_spec), resp) parsed = json.loads(resp.read()) nodetype, d = parsed if nodetype == "dirnode": t = TahoeDirectorySource(self.nodeurl, self.cache, self.progress, name) t.init_from_parsed(parsed) else: if had_trailing_slash: raise FilenameWithTrailingSlashError(source_spec) writecap = to_str(d.get("rw_uri")) readcap = to_str(d.get("ro_uri")) mutable = d.get("mutable", False) # older nodes don't provide it t = TahoeFileSource(self.nodeurl, mutable, writecap, readcap, name) return t