def path(source, res=''): if source: res = url2pathname(source.url) + '>>' + res path(source.prev, res) else: res += url2pathname(philosophy) print(res)
def play(self, uri): try: winsound.PlaySound(None, 0) winsound.PlaySound( url2pathname(uri[5:]), winsound.SND_FILENAME | winsound.SND_ASYNC) except RuntimeError: log.error("ERROR: RuntimeError while playing %s." % url2pathname(uri[5:]))
def readMedia(self, media): conf = {} uuid = '' for opt in media: if(opt.attrib['name'] == 'uuid'): uuid = url.url2pathname(opt.attrib['value']) else: conf[opt.attrib['name']] = literal_eval(url.url2pathname(opt.attrib['value'])) return (uuid, conf)
def drag_data_received(self,widget, drag_context, x, y, selection_data, info, timestamp): model = self.treeview.get_model() for filename in selection_data.get_uris(): if len(filename)>8: filename = url2pathname(filename) filename = filename[7:] model.append([filename])
def identify(self, song): media = self.instance.media_new(song.path) media.parse() media.get_meta(vlc.Meta.Album) track_info = {} try: track_info = { 'track_nb': media.get_meta(vlc.Meta.TrackNumber), 'title': media.get_meta(vlc.Meta.Title), 'album': media.get_meta(vlc.Meta.Album), 'artist': media.get_meta(vlc.Meta.Artist), 'label': media.get_meta(vlc.Meta.Publisher), 'date': media.get_meta(vlc.Meta.Date), 'genre': media.get_meta(vlc.Meta.Genre), 'artwork': media.get_meta(vlc.Meta.ArtworkURL) } if track_info['artwork'] is not None: track_info['artwork'] = url2pathname(track_info['artwork'][7:]) print(track_info) except: # TODO: # - clean messed up tags # - create empty trackinfos pass # return track_info song.track_info = track_info
def run(node): """ Primary entry-point for running this module. :param node: dict { "url": "https://some-site.com" } :return: { document_url: metadata, ... } :rtype: dict """ mapper = lambda x: redis_load(x, r) url = node.get('url', 'http://www.cic.gc.ca') pool = ThreadPool(32) docs = redis_docs(url, r) metadata = pool.map(mapper, docs) return { url2pathname(k): v for k,v in metadata if v }
def get_filename(self, basename): """ Returns full path to a file, for example: get_filename('css/one.css') -> '/full/path/to/static/css/one.css' """ filename = None # first try finding the file in the root try: # call path first so remote storages don't make it to exists, # which would cause network I/O filename = self.storage.path(basename) if not self.storage.exists(basename): filename = None except NotImplementedError: # remote storages don't implement path, access the file locally if compressor_file_storage.exists(basename): filename = compressor_file_storage.path(basename) # secondly try to find it with staticfiles (in debug mode) if not filename and self.finders: filename = self.finders.find(url2pathname(basename)) if filename: return filename # or just raise an exception as the last resort raise UncompressableFileError( "'%s' could not be found in the COMPRESS_ROOT '%s'%s" % (basename, settings.COMPRESS_ROOT, self.finders and " or with staticfiles." or "."))
def drag_data_received(self, widget, context, x, y, sel_data, info, time): """ Handle the standard gtk interface for drag_data_received. If the selection data is define, extract the value from sel_data.data, and decide if this is a move or a reorder. The only data we accept on mediaview is dropping a file, so URI_LIST. We assume this is what we obtain """ if not sel_data: return files = sel_data.get_uris() for file in files: protocol, site, mfile, j, k, l = urlparse(file) if protocol == "file": name = url2pathname(mfile) mime = get_type(name) if not is_valid_type(mime): return photo = Media() self.uistate.set_busy_cursor(True) photo.set_checksum(create_checksum(name)) self.uistate.set_busy_cursor(False) base_dir = str(media_path(self.dbstate.db)) if os.path.exists(base_dir): name = relative_path(name, base_dir) photo.set_path(name) photo.set_mime_type(mime) basename = os.path.basename(name) (root, ext) = os.path.splitext(basename) photo.set_description(root) with DbTxn(_("Drag Media Object"), self.dbstate.db) as trans: self.dbstate.db.add_media(photo, trans) widget.emit_stop_by_name('drag_data_received')
def url_to_file_path(self, url): url_path = urlparse(url).path if url_path.endswith('/'): url_path += 'index.html' if url_path.startswith('/'): url_path = url_path[1:] return url2pathname(url_path)
def get_cover(self, db_entry=None): # Find cover in music dir if db_entry: print("dbentry") cover_dir = path.dirname(url2pathname(db_entry.get_playback_uri()).replace('file://', '')) if path.isdir(cover_dir): print(cover_dir) for f in listdir(cover_dir): file_name = path.join(cover_dir, f) mt = mimetypes.guess_type(file_name)[0] if mt and mt.startswith('image/'): if path.splitext(f)[0].lower() in IMAGE_NAMES: print("spli") print(file_name) return file_name # Find cover saved by artdisplay plugin song_info = self.get_song_info(db_entry) for rb_cover_path in ('~/.gnome2/rhythmbox/covers', '~/.cache/rhythmbox/covers/'): for file_type in ('jpg', 'png', 'jpeg', 'gif', 'svg'): cover_file = path.join(path.expanduser(rb_cover_path), '%s - %s.%s' % (song_info['artist'], song_info['album'], file_type)) if path.isfile(cover_file): print("Found image in cache folder") print(cover_file) return cover_file key = db_entry.create_ext_db_key(RB.RhythmDBPropType.ALBUM) cover_db = RB.ExtDB(name='album-art') art_location = cover_db.lookup(key) if art_location and path.exists(art_location): print(art_location) return art_location # Find the image from AlbumArt plugin #cover_art = self.db.entry_request_extra_metadata(db_entry, "rb:coverArt") # If image not found return #if cover_art==None: # print "Image not found, bloody timeouts" # return DesktopControl.UNKNOWN_COVER # Do the dirty work here cover_file = path.expanduser(STORAGE_LOC) + song_info['title'] + "-" + song_info['artist'] + ".jpg" print(cover_file) cover_art.save(cover_file, "jpeg", {"quality":"100"}) print("Returning cover file") print(cover_file) return cover_file # No cover found print("no cover") return DesktopControl.UNKNOWN_COVER # Not playing print("not playing") return None
def file_uri_to_path(uri): if PathCreateFromUrlW is not None: path_len = ctypes.c_uint32(260) path = ctypes.create_unicode_buffer(path_len.value) PathCreateFromUrlW(uri, path, ctypes.byref(path_len), 0) return path.value else: return url2pathname(urlparse(uri).path)
def _url_to_local_path(url, path): """Mirror a url path in a local destination (keeping folder structure).""" destination = parse.urlparse(url).path # First char should be '/', and it needs to be discarded if len(destination) < 2 or destination[0] != '/': raise ValueError('Invalid URL') destination = os.path.join(path, request.url2pathname(destination)[1:]) return destination
def nativejoin(base, path): """ Joins two paths - returning a native file path. Given a base path and a relative location, (in posix format) return a file path in a (relatively) OS native way. """ return url2pathname(pathjoin(base, path))
def testDatabase(self): """Verify that a Workspace requests a database file in the target directory, but not in any repository. """ root = self._testWorkspace self._assertInDir(self._testbed.dbLocation, root) for repo in WorkspaceTestSuite._allRepos(self._testbed): # Workspace spec allows these to be URIs or paths, whatever the Butler accepts self._assertNotInDir(self._testbed.dbLocation, url2pathname(repo))
def file_uri_to_path(file_uri: str) -> str: """Convert a file uri to absolute path.""" try: parsed = urlparse(file_uri) host = "{0}{0}{mnt}{0}".format(os.path.sep, mnt=parsed.netloc) return os.path.abspath(os.path.join(host, url2pathname(parsed.path))) except ValueError: logger.warning(f"Invalid file uri '{file_uri}'") return None
def process_args(args): """Process arguments sent to already running Deluge""" # Make sure args is a list args = list(args) log.debug('Processing args from other process: %s', args) if not client.connected(): # We're not connected so add these to the queue log.debug('Not connected to host.. Adding to queue.') component.get('QueuedTorrents').add_to_queue(args) return config = ConfigManager('gtkui.conf') for arg in args: if not arg.strip(): continue log.debug('arg: %s', arg) if is_url(arg): log.debug('Attempting to add url (%s) from external source...', arg) if config['interactive_add']: component.get('AddTorrentDialog').add_from_url(arg) component.get('AddTorrentDialog').show( config['focus_add_dialog']) else: client.core.add_torrent_url(arg, None) elif is_magnet(arg): log.debug('Attempting to add magnet (%s) from external source...', arg) if config['interactive_add']: component.get('AddTorrentDialog').add_from_magnets([arg]) component.get('AddTorrentDialog').show( config['focus_add_dialog']) else: client.core.add_torrent_magnet(arg, {}) else: log.debug('Attempting to add file (%s) from external source...', arg) if urlparse(arg).scheme == 'file': arg = url2pathname(urlparse(arg).path) path = os.path.abspath(decode_bytes(arg)) if not os.path.exists(path): log.error('No such file: %s', path) continue if config['interactive_add']: component.get('AddTorrentDialog').add_from_files([path]) component.get('AddTorrentDialog').show( config['focus_add_dialog']) else: with open(path, 'rb') as _file: filedump = b64encode(_file.read()) client.core.add_torrent_file( os.path.split(path)[-1], filedump, None)
def coordinates_path(url_parts): url = urls.reverse('openoni_page', kwargs=url_parts) path = url2pathname(url) if path.startswith("/"): path = path[1:] full_path = os.path.join(settings.COORD_STORAGE, path) if not os.path.exists(full_path): os.makedirs(full_path) return os.path.join(full_path, "coordinates.json.gz")
def get_os_filename(path): """Return filesystem path for given URL path.""" if os.name == 'nt': path = prepare_urlpath_for_nt(path) res = urlrequest.url2pathname(fileutil.path_safe(path)) if os.name == 'nt' and res.endswith(':') and len(res) == 2: # Work around http://bugs.python.org/issue11474 res += os.sep return res
def _get_filesystem_path(self, url_path, basedir=settings.MEDIA_ROOT): """ Makes a filesystem path from the specified URL path """ if url_path.startswith(settings.MEDIA_URL): # strip media root url url_path = url_path[len(settings.MEDIA_URL):] return os.path.normpath(os.path.join(basedir, url2pathname(url_path)))
def status(): t.write("status\n".encode("utf-8")) r = t.read_until(">".encode("utf-8")).decode("utf-8") file = re.search(r'(file://.+?) \)', r) if file: file = urlparse(file.group(1)) file = url2pathname(file.path) state = re.search(r'state (\w+)', r).group(1) return file, state
def repl_relative(m, base_path, relative_path): """ Replace path with relative path """ RE_WIN_DRIVE_PATH = re.compile(r"(^(?P<drive>[A-Za-z]{1}):(?:\\|/))") link = m.group(0) try: scheme, netloc, path, params, query, fragment, is_url, is_absolute = parse_url( m.group('path')[1:-1]) except: # Parsing crashed an burned; no need to continue. return link if not is_url: # Get the absolute path of the file or return # if we can't resolve the path path = url2pathname(path) abs_path = None if (not is_absolute): # Convert current relative path to absolute temp = os.path.normpath(os.path.join(base_path, path)) if os.path.exists(temp): abs_path = temp.replace("\\", "/") elif os.path.exists(path): abs_path = path if abs_path is not None: convert = False # Determine if we should convert the relative path # (or see if we can realistically convert the path) if (sublime.platform() == "windows"): # Make sure basepath starts with same drive location as target # If they don't match, we will stay with absolute path. if (base_path.startswith('//') and base_path.startswith('//')): convert = True else: base_drive = RE_WIN_DRIVE_PATH.match(base_path) path_drive = RE_WIN_DRIVE_PATH.match(abs_path) if ((base_drive and path_drive) and base_drive.group('drive').lower() == path_drive.group('drive').lower()): convert = True else: # OSX and Linux convert = True # Convert the path, url encode it, and format it as a link if convert: path = pathname2url( os.path.relpath(abs_path, relative_path).replace('\\', '/')) else: path = pathname2url(abs_path) link = '%s"%s"' % (m.group('name'), urlunparse((scheme, netloc, path, params, query, fragment))) return link
def parse_file_url(file_url): if file_url.scheme != 'file': raise ValueError("Invalid file URI") path = url2pathname(file_url.path) if file_url.netloc not in ('', 'localhost'): # Windows file share # TODO: Can't do anything useful on non-Windows... Return an error? path = '\\\\%s%s' % (file_url.netloc, path) return path
def uri_to_path(uri): # https://stackoverflow.com/a/61922504 # ~heavy import from urllib.parse import urlparse, unquote from urllib.request import url2pathname path = urlparse(uri).path return url2pathname(unquote(path))
def testAlerts(self): """Verify that a WorkspaceGen3 requests an alert dump in the target directory, but not in any repository. """ root = self._testWorkspace self._assertInDir(self._testbed.alertLocation, root) # Workspace spec allows these to be URIs or paths, whatever the Butler accepts self._assertNotInDir(self._testbed.alertLocation, url2pathname(self._testbed.repo))
def parse_uri(uri: str, encoding="utf-8"): '''Parse a path out of a given uri :uri: URI string to be parsed :encoding: (Optional) string encoding to parse with ''' if uri: url = urlsplit(unquote(uri, encoding=encoding)) return url2pathname(url.path)
def s3_open(self, req): # The implementation was inspired mainly by the code behind # urllib.request.FileHandler.file_open(). # # recipe copied from: # http://code.activestate.com/recipes/578957-urllib-handler-for-amazon-s3-buckets/ # converted to boto3 if version_info[0] < 3: bucket_name = req.get_host() key_name = url2pathname(req.get_selector())[1:] else: bucket_name = req.host key_name = url2pathname(req.selector)[1:] if not bucket_name or not key_name: raise URLError('url must be in the format s3://<bucket>/<key>') s3 = boto3.resource('s3') key = s3.Object(bucket_name, key_name) client = boto3.client('s3') obj = client.get_object(Bucket=bucket_name, Key=key_name) filelike = _FileLikeKey(obj['Body']) origurl = 's3://{}/{}'.format(bucket_name, key_name) if key is None: raise URLError('no such resource: {}'.format(origurl)) headers = [ ('Content-type', key.content_type), ('Content-encoding', key.content_encoding), ('Content-language', key.content_language), ('Content-length', key.content_length), ('Etag', key.e_tag), ('Last-modified', key.last_modified), ] headers = email.message_from_string('\n'.join( '{}: {}'.format(key, value) for key, value in headers if value is not None)) return addinfourl(filelike, headers, origurl)
def parseUrl(self): url = self.originalUrl.toPlainText() parsedurl = str(urlparse(url)) parsedurl = parsedurl.replace(',', '\n') parsedurl = parsedurl.replace('&', '\n') parsedurl = str(url2pathname(parsedurl)) parsedurl = parsedurl.replace('query=\'', 'query=\n') # report parsed url self.parsedUrl.setPlainText(parsedurl)
def _assign_unique_name(url, html_doc): """ uses the title tag in the html docunment as a folder name uses the url instead """ Threads.new_folder_lock.acquire() title = web.get_title_from_html(html_doc) if title: unique_name = url2pathname(title.text) else: unique_name = url2pathname(url) # remove any illegal characters # this function was taken from stackoverflow # assign to global unique_path_name settings = Settings.load() settings["unique_pathname"]["name"] = format_filename(unique_name) Settings.save(settings) Threads.new_folder_lock.release()
def format_uri(self, uri): path = url2pathname(uri) # escape special chars path = path.strip('\r\n\x00') # remove \r\n and NULL if path.startswith('file:\\\\\\'): # windows path = path[8:] # 8 is len('file:///') elif path.startswith('file://'): #nautilus, rox path = path[7:] # 7 is len('file://') elif path.startswith('file:'): # xffm path = path[5:] # 5 is len('file:') return path
def urlpath2path(url): """Like :func:`ur.url2pathname()`, but prefixing with UNC(\\\\?\\) long paths and preserving last slash.""" import urllib.request as ur p = ur.url2pathname(url) if _is_dir_regex.search(url) and p[-1] != os.sep: p = p + osp.sep if len(p) > 200: p += _unc_prefix return p
def decode_resource_url(url): """ URL decodes a full resource file/folder url. :param url: an encoded string url :return: url decoded string """ parsed_url = urllib.parse.urlparse(url) url_encoded_path = url2pathname(parsed_url.path) encoded_url = parsed_url._replace(path=url_encoded_path).geturl() return encoded_url
def dispatch_selected(*args): """BGG BSG Function to dispatch a single user hand""" document = XSCRIPTCONTEXT.getDocument() maindir = urllib2.url2pathname(os.path.dirname(document.Location.replace("file://", ""))) logfile = os.path.join(maindir, 'bsg-dispatcher-debug.log') sys.stdout = open(logfile, "ab", 0) # unbuffered # Useful variables so we don't need to do a lot of typing worksheet = document.getSheets().getByName('Game State') dispatcherinfo = document.getSheets().getByName('Posting Templates') # Find the selected char selected_char = worksheet.DrawPage.Forms.getByName('formGameState').getByName('lstPlayers').CurrentValue selected_player = '' if not selected_char: MessageBox(document, "Error: no player selected", "Invalid player", "warningbox") return False # Find out which player we're looking at for i in range(7): charname = worksheet.getCellByPosition(4, 2+i).getString() # Character name on Game State if charname == selected_char: selected_player = worksheet.getCellByPosition(1, 2+i).getString() # Player name on Game State player_id = i break else: MessageBox(document, "Error: player not found, maybe a bug?", "Invalid player", "warningbox") return False # Verify if file exists playerfile = os.path.join(maindir, selected_char + '.txt') if not os.path.exists(playerfile): MessageBox(document, "Error: file '%s' not found (use the 'Create Hand Lists' first)" % (selected_char + '.txt'), "File not found", "warningbox") return False # Verify if we already sent this file old_md5 = dispatcherinfo.getCellByPosition(player_id+4, 31).getString() current_md5 = get_md5(playerfile) if old_md5 == current_md5: # We DID send this already!!! confirm = MessageBox(document, "It seems we've already sent this file. Send again?", "File already sent", "querybox", YES_NO) if confirm == 3: # Pressed "No" return False # Now we finally try to send our files try: gm = GeekMail(workdir=maindir) gm.dispatch_file(selected_player, playerfile) # Set the current MD5 on the spreadsheet (so that we only send it again after something is changed) dispatcherinfo.getCellByPosition(player_id+4, 31).setString(current_md5) except Exception as e: MessageBox(document, str(e), "Alert!", "warningbox") return False MessageBox(document, "Successfully sent file to %s" % selected_player, "Success!", "infobox")
def uri2path(file_uri: str, windows_non_standard: bool = False) -> str: # there is lot of buggy regex based code out there which expects Windows file URIs as # file://C/... instead of standard file:///C/... # When passing file uri to such code, turn on windows_non_standard if windows_non_standard and is_windows(): file_uri = file_uri.replace('file://', 'file:///') parsed = urlparse(file_uri) host = "{0}{0}{mnt}{0}".format(os.path.sep, mnt=parsed.netloc) return os.path.normpath( os.path.join(host, url2pathname(unquote(parsed.path))))
def check_invalid_image(wgt_file, resource_info, key): image_url = resource_info[key] if image_url != '' and not image_url.startswith(('http://', 'https://')): image_path = url2pathname(image_url) try: wgt_file.read(image_path) except KeyError: raise InvalidContents('missing image file: %s' % image_path)
class ParsedURL(str): fragment = property(lambda self: self.__parsed__.fragment) netloc = property(lambda self: self.__parsed__.netloc) params = property(lambda self: self.__parsed__.params) path = property(lambda self: self.__parsed__.path if self.scheme != 'file' else url2pathname(self.__parsed__.path)) query = property(lambda self: self.__parsed__.query) scheme = property(lambda self: self.__parsed__.scheme) def __init__(self, value): self.__parsed__ = urlparse(self)
def testDirectories(self): """Verify that a Workspace creates repositories in the target directory. The exact repository locations are not tested, as they are likely to change. """ root = self._testWorkspace self.assertEqual(self._testbed.workDir, root) self._assertInDir(self._testbed.configDir, root) for repo in WorkspaceTestSuite._allRepos(self._testbed): # Workspace spec allows these to be URIs or paths, whatever the Butler accepts self._assertInDir(url2pathname(repo), root)
def read(self, request, vendor, name, version): from_version = request.GET.get('from') if from_version is not None: try: from_version = Version(from_version) except ValueError: return build_error_response( request, 422, _("Missing parameter: template_uri or file")) resource = get_object_or_404(CatalogueResource, vendor=vendor, short_name=name, version=version) resource_info = resource.get_processed_info(process_urls=False) if resource_info['changelog'] == '': raise Http404 doc_relative_path = url2pathname(resource_info['changelog']) doc_base_url = force_trailing_slash( urljoin(resource.get_template_url(request=request, for_base=True), pathname2url(os.path.dirname(doc_relative_path)))) doc_path = os.path.join( catalogue_utils.wgt_deployer.get_base_dir(vendor, name, version), doc_relative_path) (doc_filename_root, doc_filename_ext) = os.path.splitext(doc_path) localized_doc_path = doc_filename_root + '.' + get_language( ) + doc_filename_ext try: doc_code = download_local_file(localized_doc_path).decode('utf-8') except Exception: try: doc_code = download_local_file(doc_path).decode('utf-8') except Exception: msg = _('Error opening the changelog file') doc_code = '<div class="margin-top: 10px"><p>%s</p></div>' % msg doc_pre_html = markdown.markdown(doc_code, output_format='xhtml5', extensions=[ 'markdown.extensions.codehilite', 'markdown.extensions.fenced_code' ]) if from_version: doc_pre_html = filter_changelog(doc_pre_html, from_version) if doc_pre_html.strip() == '': raise Http404 doc = clean_html(doc_pre_html, base_url=doc_base_url) return HttpResponse( doc, content_type='application/xhtml+xml; charset=UTF-8')
def testDirectories(self): """Verify that a WorkspaceGen3 creates subdirectories in the target directory. The exact locations are not tested, as they are likely to change. """ # Workspace should report all paths as absolute root = os.path.abspath(os.path.realpath(self._testWorkspace)) self.assertEqual(self._testbed.workDir, root) self._assertInDir(self._testbed.configDir, root) # Workspace spec allows these to be URIs or paths, whatever the Butler accepts self._assertInDir(url2pathname(self._testbed.repo), root)
def normalize_uri(self, uri): scheme = _scheme_from_uri(uri) if scheme == 'file': path = url2pathname(uri[len('file:'):]) return self.normalize_local_path(path) if scheme: return self.normalize_remote_uri(uri) if uri.startswith(self.PATH_SEPARATOR): return self.normalize_absolute_path(uri) return self.normalize_relative_path(uri)
def checkout_file(self, item, dest, revnum=None, verbose=False): revnum_s = str(revnum) if revnum == None: revnum_s = 'head' url = self._svn_url d = '/'.join(item.split('/')[:-1]) path = os.path.join(dest, url2pathname(item)) fullurl = url+'/'+item if d: url = url + '/' + d dest = os.path.join(dest, url2pathname(d)) if urlparse(url).fragment: logger.warning('invalid url: {}'.format(url)) else: rev = None if revnum: rev = pysvn.Revision(pysvn.opt_revision_kind.number, revnum) else: rev = pysvn.Revision(pysvn.opt_revision_kind.head) if verbose: logger.info('checking out "{}@{}" to "{}"'.format(fullurl, revnum_s, path)) if os.path.exists(path): if os.path.isdir(path): pass else: logger.warning('already exist: "{}", removing..'.format(path)) os.unlink(path) if not os.path.exists(os.path.join(dest, '.svn')): self.svn_cli.checkout(url, dest, revision=rev, ignore_externals=True, depth=pysvn.depth.empty) self.svn_cli.update(path, revision=rev, ignore_externals=True, depth=pysvn.depth.files)
def open(self, url: str): """ Please use as a context manager like `with db.open("http://...") as f: ...` """ if url.startswith('http:') or url.startswith( 'https:') or url.startswith('ftp:'): with self.__cached_session.get(url) as resp: return nullcontext(resp.raw) if url.startswith('file:'): return open(url2pathname(url.lstrip('file:')), mode='rb') else: return urlopen(url)
def get_file_path_from_dnd_dropped_uri(self, uri): path = "" if uri.startswith('file:\\\\\\'): # windows path = uri[8:] # 8 is len('file:///') elif uri.startswith('file://'): # nautilus, rox path = uri[7:] # 7 is len('file://') elif uri.startswith('file:'): # xffm path = uri[5:] # 5 is len('file:') path = request.url2pathname(path) # escape special chars # path = path.strip('\r\n\x00') # remove \r\n and NULL return path
def _create_run_in_store(store): config = { "experiment_id": "0", "user_id": "Anderson", "start_time": int(time.time()), "tags": {}, } run = store.create_run(**config) artifact_path = url2pathname(unquote(urlparse(run.info.artifact_uri).path)) if not os.path.exists(artifact_path): os.makedirs(artifact_path) return run
def basename(url): try: parts = urllib.parse.urlsplit(url) path = url2pathname(parts.path) res = os.path.basename(path) except: global bad_url_counter bad_url_counter += 1 return 'bad_url_%d.html' % bad_url_counter if not os.path.splitext(res)[1]: return 'index.html' return res
def repl_relative(m, base_path, relative_path): """ Replace path with relative path """ RE_WIN_DRIVE_PATH = re.compile(r"(^(?P<drive>[A-Za-z]{1}):(?:\\|/))") link = m.group(0) try: scheme, netloc, path, params, query, fragment, is_url, is_absolute = parse_url(m.group('path')[1:-1]) if not is_url: # Get the absolute path of the file or return # if we can't resolve the path path = url2pathname(path) abs_path = None if (not is_absolute): # Convert current relative path to absolute temp = os.path.normpath(os.path.join(base_path, path)) if os.path.exists(temp): abs_path = temp.replace("\\", "/") elif os.path.exists(path): abs_path = path if abs_path is not None: convert = False # Determine if we should convert the relative path # (or see if we can realistically convert the path) if (sublime.platform() == "windows"): # Make sure basepath starts with same drive location as target # If they don't match, we will stay with absolute path. if (base_path.startswith('//') and base_path.startswith('//')): convert = True else: base_drive = RE_WIN_DRIVE_PATH.match(base_path) path_drive = RE_WIN_DRIVE_PATH.match(abs_path) if ( (base_drive and path_drive) and base_drive.group('drive').lower() == path_drive.group('drive').lower() ): convert = True else: # OSX and Linux convert = True # Convert the path, url encode it, and format it as a link if convert: path = pathname2url(os.path.relpath(abs_path, relative_path).replace('\\', '/')) else: path = pathname2url(abs_path) link = '%s"%s"' % (m.group('name'), urlunparse((scheme, netloc, path, params, query, fragment))) except: # Parsing crashed an burned; no need to continue. pass return link
def dispatch_all(*args): """BGG BSG Function to dispatch all player hands""" document = XSCRIPTCONTEXT.getDocument() maindir = urllib2.url2pathname(os.path.dirname(document.Location.replace("file://", ""))) logfile = os.path.join(maindir, 'bsg-dispatcher-debug.log') sys.stdout = open(logfile, "ab", 0) # unbuffered # Useful variables so we don't need to do a lot of typing worksheet = document.getSheets().getByName('Game State') dispatcherinfo = document.getSheets().getByName('Posting Templates') to_send = [] # Maximum of 7 players for i in range(7): playername = worksheet.getCellByPosition(1, 2+i).getString() # Player name on Game State charname = worksheet.getCellByPosition(4, 2+i).getString() # Character name on Game State if not playername: # If there isn't a player for this number, skip it continue # Verify if file exists playerfile = os.path.join(maindir, charname + '.txt') if not os.path.exists(playerfile): MessageBox(document, "Error: file '%s' not found (use the 'Create Hand Lists' first)" % (charname + '.txt'), "File not found", "warningbox") return False # Let's see if this file was modified old_md5 = dispatcherinfo.getCellByPosition(i+4, 31).getString() current_md5 = get_md5(playerfile) if old_md5 != current_md5: # File was modified. Set up to send it to_send.append({'player': playername, 'character': charname, 'playerfile': playerfile, 'md5': current_md5, 'player_id': i}) if not to_send: MessageBox(document, "Nothing new to send. Maybe you forgot to use Create Hand Lists?", "No files modified!", "infobox") else: def send(p): # Now we finally try to send our files try: gm = GeekMail(workdir=maindir) gm.dispatch_file(p['player'], p['playerfile']) # Set the current MD5 on the spreadsheet (so that we only send it again after something is changed) dispatcherinfo.getCellByPosition(p['player_id']+4, 31).setString(p['md5']) except Exception as e: MessageBox(document, e.message, "Alert!", "warningbox") processes = [] n = 0 for player in to_send: n += 1 processes.append(multiprocessing.Process(target=send, args=(player, ))) processes[-1].start() MessageBox(document, "Successfully sent the updated hands to: %s" % (", ".join([e['player'] for e in to_send])), "Success!", "infobox")
def local_get(url, *args, **kwargs): "Fetch a stream from local files." from requests import Response p_url = urlparse(url) if p_url.scheme != "file": raise ValueError("Expected file scheme") filename = url2pathname(p_url.path) response = Response() response.status_code = 200 response.raw = open(filename, "rb") return response
def get_locations(self, inputstring): """ Get file path for other file operations """ # Get source for comparison source = url2pathname(self.location).replace('file:///', '/') if inputstring == 'source': return source # Set Destination Directory targetdir = '/' + self.rbfo.configurator.get_val('layout-path') targetdir = tools.data_filler(self, targetdir, strip_ntfs=self.strip_ntfs) targetloc = self.rbfo.configurator.get_val('locations')[0] targetpath = url2pathname(targetloc).replace('file:///', '/') targetdir = tools.folderize(targetpath, targetdir) # Set Destination Filename targetname = self.rbfo.configurator.get_val('layout-filename') targetname = tools.data_filler(self, targetname, strip_ntfs=self.strip_ntfs) targetname += os.path.splitext(self.location)[1] # Join destination destin = (os.path.join(targetdir, targetname)) if inputstring == 'destin': return destin return
def repl_absolute(m, base_path): """ Replace path with absolute path """ link = m.group(0) scheme, netloc, path, params, query, fragment, is_url, is_absolute = parse_url(m.group("path")[1:-1]) path = url2pathname(path) if not is_absolute and not is_url: temp = os.path.normpath(os.path.join(base_path, path)) if os.path.exists(temp): path = pathname2url(temp.replace("\\", "/")) link = '%s"%s"' % (m.group("name"), urlunparse((scheme, netloc, path, params, query, fragment))) return link
def handle(self, options): self.console = component.get('ConsoleUI') t_options = {} if options.path: t_options['download_location'] = os.path.abspath(os.path.expanduser(options.path)) def on_success(result): if not result: self.console.write('{!error!}Torrent was not added: Already in session') else: self.console.write('{!success!}Torrent added!') def on_fail(result): self.console.write('{!error!}Torrent was not added: %s' % result) # Keep a list of deferreds to make a DeferredList deferreds = [] for torrent in options.torrents: if not torrent.strip(): continue if deluge.common.is_url(torrent): self.console.write('{!info!}Attempting to add torrent from url: %s' % torrent) deferreds.append(client.core.add_torrent_url(torrent, t_options).addCallback(on_success).addErrback( on_fail)) elif deluge.common.is_magnet(torrent): self.console.write('{!info!}Attempting to add torrent from magnet uri: %s' % torrent) deferreds.append(client.core.add_torrent_magnet(torrent, t_options).addCallback(on_success).addErrback( on_fail)) else: # Just a file if urlparse(torrent).scheme == 'file': torrent = url2pathname(urlparse(torrent).path) path = os.path.abspath(os.path.expanduser(torrent)) if not os.path.exists(path): self.console.write('{!error!}%s does not exist!' % path) continue if not os.path.isfile(path): self.console.write('{!error!}This is a directory!') continue self.console.write('{!info!}Attempting to add torrent: %s' % path) filename = os.path.split(path)[-1] with open(path, 'rb') as _file: filedump = base64.encodestring(_file.read()) deferreds.append(client.core.add_torrent_file(filename, filedump, t_options).addCallback( on_success).addErrback(on_fail)) return defer.DeferredList(deferreds)
def repl_absolute(m, base_path): """ Replace path with absolute path """ link = m.group(0) try: scheme, netloc, path, params, query, fragment, is_url, is_absolute = parse_url(m.group('path')[1:-1]) except Exception: return link if (not is_absolute and not is_url): path = url2pathname(path) temp = os.path.normpath(os.path.join(base_path, path)) if os.path.exists(temp): path = pathname2url(temp.replace("\\", "/")) link = '%s"%s"' % (m.group('name'), urlunparse((scheme, netloc, path, params, query, fragment))) return link
def get_url(self): """ :returns: BZR URL of the branch (output of bzr info command), or None if it cannot be determined """ result = None if self.detect_presence(): cmd = 'bzr info %s' % self._path _, output, _ = run_shell_command(cmd, shell=True, us_env=True) matches = [l for l in output.splitlines() if l.startswith(' parent branch: ')] if matches: ppath = url2pathname(matches[0][len(' parent branch: '):]) # when it can, bzr substitues absolute path for relative paths if (ppath is not None and os.path.isdir(ppath) and not os.path.isabs(ppath)): result = os.path.abspath(os.path.join(os.getcwd(), ppath)) else: result = ppath return result
def repl_absolute(m, base_path): """Replace path with absolute path.""" link = m.group(0) try: scheme, netloc, path, params, query, fragment, is_url, is_absolute = parse_url(m.group('path')[1:-1]) if (not is_absolute and not is_url): path = url2pathname(path) temp = normpath(join(base_path, path)) if exists(temp): path = pathname2url(temp.replace("\\", "/")) link = '%s"%s"' % (m.group('name'), urlunparse((scheme, netloc, path, params, query, fragment))) except Exception: # Parsing crashed and burned; no need to continue. pass return link
def playAction(cls, action): if not conf.get("useSounds", True): return if isinstance(action, str): key_no = cls.actionToKeyNo[action] else: key_no = action typ = conf.get("soundcombo%d" % key_no, SOUND_MUTE) if typ == SOUND_BEEP: sys.stdout.write("\a") sys.stdout.flush() elif typ == SOUND_URI: uri = conf.get("sounduri%d" % key_no, "") if not os.path.isfile(url2pathname(uri[5:])): conf.set("soundcombo%d" % key_no, SOUND_MUTE) return cls.getPlayer().play(uri)
def open_local_file(self, req): host = req.get_host() file = req.get_selector() localfile = url2pathname(file) stats = os.stat(localfile) size = stats[stat.ST_SIZE] modified = rfc822.formatdate(stats[stat.ST_MTIME]) mtype = mimetypes.guess_type(file)[0] stats = os.stat(localfile) headers = mimetools.Message(StringIO( 'Content-Type: %s\nContent-Length: %d\nLast-modified: %s\n' % (mtype or 'text/plain', size, modified))) if host: host, port = splitport(host) if not host or \ (not port and socket.gethostbyname(host) in self.get_names()): return addinfourl(open(localfile, 'rb'), headers, 'file:'+file) raise URLError('file not on local host')
def dispatcher_call(*args): """BGG BSG Function to dispatch a generic message via GeekMail""" document = XSCRIPTCONTEXT.getDocument() maindir = urllib2.url2pathname(os.path.dirname(document.Location.replace("file://",""))) logfile = os.path.join(maindir, 'bsg-dispatcher-debug.log') sys.stdout = open(logfile, "a", 0) # unbuffered # Useful variables so we don't need to do a lot of typing worksheet = document.getSheets().getByName('Game State') dispatcherinfo = document.getSheets().getByName('Posting Templates') dispatchersheet = document.getSheets().getByName('Dispatcher') playername = dispatchersheet.getCellByPosition(2, 1).getString() subject = dispatchersheet.getCellByPosition(2, 3).getString() body = dispatchersheet.getCellByPosition(2, 4).getString() oldhash = dispatchersheet.getCellByPosition(2, 5).getString() if not playername: MessageBox(document, "Error: Username can't be empty", "No username!", 'warningbox') return False if not subject: MessageBox(document, "Error: Subject not defined", "Subject not defined!", 'warningbox') return False if not body: MessageBox(document, "Error: Body is empty", "Empty body!", 'warningbox') return False hashish = calculate_md5("%s%s%s" % (playername, subject, body)) if oldhash == hashish: confirm = MessageBox(document, "It seems we've already sent this data. Send again?", "Data already sent", "querybox", YES_NO) if confirm == 3: # Pressed "No" return False try: gm = GeekMail(workdir=maindir) gm.dispatch_text(playername, subject, body) # Set the current MD5 on the spreadsheet (so that we only send it again after something is changed) dispatchersheet.getCellByPosition(2, 5).setString(hashish) except Exception as e: MessageBox(document, str(e), "Alert!", "warningbox") return False MessageBox(document, "Successfully sent the following message:\n\n%s" % (subject), "Success!", "infobox")
def b64(m): import base64 src = url2pathname(m.group("path")[1:-1]) data = m.group(0) base_path = self.settings.get("builtin").get("basepath") if base_path is None: base_path = "" # Format the link absolute = False if src.startswith("file://"): src = src.replace("file://", "", 1) if sublime.platform() == "windows" and not src.startswith("//"): src = src.lstrip("/") absolute = True elif sublime.platform() == "windows" and RE_WIN_DRIVE.match(src) is not None: absolute = True # Make sure we are working with an absolute path if not src.startswith(exclusion_list): if absolute: src = os.path.normpath(src) else: src = os.path.normpath(os.path.join(base_path, src)) if os.path.exists(src): ext = os.path.splitext(src)[1].lower() for b64_ext in file_types: if ext in b64_ext: try: with open(src, "rb") as f: data = ' src="data:%s;base64,%s"' % ( file_types[b64_ext], base64.b64encode(f.read()).decode("ascii"), ) except Exception: pass break return data