def find( regex="", url="uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext", paths=[]): """Do the real search. """ # See head of file for what's happening here. register_uno_import() from pyuno import systemPathToFileUrl, absolutize from com.sun.star.lang import XTypeProvider from com.sun.star.beans import PropertyValue from com.sun.star.uno import Exception as UnoException from com.sun.star.io import IOException, XOutputStream unregister_uno_import() # never shrinks ! _g_typeTable = {} def _unohelper_getHandle(self): """Helper function from unohelper.py. """ ret = None if _g_typeTable.has_key(self.__class__): ret = _g_typeTable[self.__class__] else: names = {} traverse = list(self.__class__.__bases__) while len(traverse) > 0: item = traverse.pop() bases = item.__bases__ if uno.isInterface(item): names[item.__pyunointerface__] = None elif len(bases) > 0: # the "else if", because we only need the most # derived interface traverse = traverse + list(bases)# lst = names.keys() types = [] for x in lst: t = uno.getTypeByName(x) types.append(t) ret = tuple(types) , uno.generateUuid() _g_typeTable[self.__class__] = ret return ret class Base(XTypeProvider): """Helper class from unohelper.py. """ def getTypes(self): return _unohelper_getHandle(self)[0] def getImplementationId(self): return _unohelper_getHandle(self)[1] class OutputStream(Base, XOutputStream): def __init__(self): self.closed = 0 def closeOutput(self): self.closed = 1 def writeBytes(self, seq): sys.stdout.write(seq.value) def flush(self): pass ret_val = 0 doc = None stdout = False filter_props = None dest_paths = [] matches = [] try: ctxLocal = uno.getComponentContext() smgrLocal = ctxLocal.ServiceManager resolver = smgrLocal.createInstanceWithContext( "com.sun.star.bridge.UnoUrlResolver", ctxLocal) ctx = resolver.resolve(url) smgr = ctx.ServiceManager desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop", ctx) cwd = systemPathToFileUrl(getcwd()) inProps = PropertyValue("Hidden" , 0 , True, 0), for path in paths: try: fileUrl = absolutize(cwd, systemPathToFileUrl(path)) doc = desktop.loadComponentFromURL( fileUrl , "_blank", 0, inProps) if not doc: raise UnoException( "Couldn't open stream for unknown reason", None) viewCursor = doc.getCurrentController().getViewCursor() #Create Search Descriptor search = doc.createSearchDescriptor() #What to search for search.SearchString = regex search.SearchRegularExpression = True #Found string matchingResult = doc.findAll( search ) nbOfMatches = matchingResult.getCount() matchingXTextRanges = [matchingResult.getByIndex(i) for i in range(nbOfMatches)] matches = [] for matchingXTextRange in matchingXTextRanges: viewCursor.gotoRange(matchingXTextRange, False) pageNumber = viewCursor.getPage() # or .page ? matches.append({'page':pageNumber}) except IOException, e: print "Error during conversion: ", e.Message ret_val = 1 except UnoException, e: print "UnoError during conversion: ", e.__class__, e.Message ret_val = 1
def systemPathToFileUrl( systemPath ): "returns a file-url for the given system path" return pyuno.systemPathToFileUrl( systemPath )
def systemPathToFileUrl(systemPath): return pyuno.systemPathToFileUrl(systemPath)
def convert( url="uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext", filter_name="Text (Encoded)", extension="txt", paths=[]): """Do the real conversion. """ # See head of file for what's happening here. register_uno_import() from pyuno import systemPathToFileUrl, absolutize from com.sun.star.lang import XTypeProvider from com.sun.star.beans import PropertyValue from com.sun.star.uno import Exception as UnoException from com.sun.star.io import IOException, XOutputStream unregister_uno_import() # never shrinks ! _g_typeTable = {} def _unohelper_getHandle(self): """Helper function from unohelper.py. """ ret = None if _g_typeTable.has_key(self.__class__): ret = _g_typeTable[self.__class__] else: names = {} traverse = list(self.__class__.__bases__) while len(traverse) > 0: item = traverse.pop() bases = item.__bases__ if uno.isInterface(item): names[item.__pyunointerface__] = None elif len(bases) > 0: # the "else if", because we only need the most # derived interface traverse = traverse + list(bases)# lst = names.keys() types = [] for x in lst: t = uno.getTypeByName(x) types.append(t) ret = tuple(types) , uno.generateUuid() _g_typeTable[self.__class__] = ret return ret class Base(XTypeProvider): """Helper class from unohelper.py. """ def getTypes(self): return _unohelper_getHandle(self)[0] def getImplementationId(self): return _unohelper_getHandle(self)[1] class OutputStream(Base, XOutputStream): def __init__(self): self.closed = 0 def closeOutput(self): self.closed = 1 def writeBytes(self, seq): sys.stdout.write(seq.value) def flush(self): pass ret_val = 0 doc = None stdout = False filter_props = None dest_paths = [] try: ctxLocal = uno.getComponentContext() smgrLocal = ctxLocal.ServiceManager resolver = smgrLocal.createInstanceWithContext( "com.sun.star.bridge.UnoUrlResolver", ctxLocal) ctx = resolver.resolve(url) smgr = ctx.ServiceManager desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop", ctx) cwd = systemPathToFileUrl(getcwd()) out_props = [ PropertyValue("FilterName" , 0, filter_name , 0), PropertyValue("Overwrite" , 0, True , 0), PropertyValue("OutputStream", 0, OutputStream(), 0), ] if filter_name == "writer_pdf_Export": # We have to pass the `FilterData` property very # carefully. It must be an `uno.Any` value. filter_props = PropertyValue( "FilterData", 0, uno.Any( "[]com.sun.star.beans.PropertyValue", ( #PropertyValue("SelectPdfVersion", 0, 1L, 0), #PropertyValue("UseTaggedPDF", 0, False, 0), )), 0) out_props.append(filter_props) inProps = PropertyValue("Hidden" , 0 , True, 0), for path in paths: try: fileUrl = absolutize(cwd, systemPathToFileUrl(path)) doc = desktop.loadComponentFromURL( fileUrl , "_blank", 0, inProps) if not doc: raise UnoException( "Couldn't open stream for unknown reason", None) if not stdout: (dest, ext) = splitext(path) dest = dest + "." + extension destUrl = absolutize(cwd, systemPathToFileUrl(dest)) doc.storeToURL(destUrl, tuple(out_props)) dest_paths.append(destUrl) else: doc.storeToURL("private:stream",out_props) except IOException, e: print "Error during conversion: ", e.Message ret_val = 1 except UnoException, e: print "UnoError during conversion: ", e.__class__, e.Message ret_val = 1
def systemPathToFileUrl(systemPath): """Returns a file URL for the given system path.""" return pyuno.systemPathToFileUrl(systemPath)
def systemPathToFileUrl(systemPath): "returns a file-url for the given system path" return pyuno.systemPathToFileUrl(systemPath)
def path_to_url(path): if not path.startswith('file://'): return pyuno.systemPathToFileUrl(os.path.realpath(path))