def createnormpath(self, epsilon=_marker): if epsilon is _marker: return normpath([normsubpath([normline_pt(self.x_pt, self.y_pt, self.x_pt, self.y_pt)])]) elif epsilon is None: return normpath([normsubpath([pdfmoveto_pt(self.x_pt, self.y_pt, self.x_pt, self.y_pt)], epsilon=epsilon)]) else: return normpath([normsubpath([normline_pt(self.x_pt, self.y_pt, self.x_pt, self.y_pt)], epsilon=epsilon)])
def createnormpath(self, epsilon=_marker): if epsilon is _marker: return normpath([ normsubpath( _arctobezierpath(self.x_pt, self.y_pt, self.r_pt, self.angle2, self.angle1)) ]).reversed() else: return normpath([ normsubpath(_arctobezierpath(self.x_pt, self.y_pt, self.r_pt, self.angle2, self.angle1), epsilon=epsilon) ]).reversed()
def acquire_image(self): self.acquire_image_started = time() self.acquire_image_setup() ccd.ignore_first_trigger = False ccd.acquire_images_triggered([normpath(self.settings.image_filename)]) show_images(normpath(self.settings.image_filename)) Ensemble_SAXS.acquire(delays=[0], laser_on=[False]) tmax = time() + self.settings.acquire_image_timeout while ccd.state() != "idle" and not self.cancelled and time() < tmax: sleep(0.05) ccd.abort() self.acquire_image_unsetup() self.acquire_image_started = 0 self.update()
def module_path(object): """full pathname of the current module""" from normpath import normpath global MODULE_PATH if "MODULE_PATH" in globals(): return MODULE_PATH from sys import path from os import getcwd from os.path import basename,exists from inspect import getmodulename,getfile # 'getfile' retreives the source file name name compiled into the .pyc file. try: pathname = getfile(object) except: pathname = getfile(lambda x:x) ##print("module_path: pathname: %r" % pathname) if not exists(pathname): # The module might have been compiled on a different machine or in a # different directory. pathname = pathname.replace("\\","/") filename = basename(pathname) ##print("module_path: filename: %r" % filename) dirs = [dir for dir in [getcwd()]+path if exists(dir+"/"+filename)] if len(dirs) == 0: print "pathname of file %r not found" % filename dir = dirs[0] if len(dirs) > 0 else "." pathname = dir+"/"+filename ##print("module_path: pathname: %r" % pathname) pathname = normpath(pathname) MODULE_PATH = pathname return pathname
def image_timestamp(self): """Full pathname of the last recorded image""" from os.path import getmtime from normpath import normpath filename = self.image_filename t = getmtime(normpath(filename)) if filename else 0 return t
def set_setup_filename(self, filename): from normpath import normpath filename = Windows_pathname(normpath(filename)) from os.path import dirname, basename dir, file = dirname(filename), basename(filename) self.setup_dirname = dir self.setup_basename = file
def logfile(): """Current collection logfile""" import lauecollect from normpath import normpath lauecollect.load_settings() file = normpath(lauecollect.logfile()) return file
def decorate(self, dp, texrunner): r_pt = 2 dp.ensurenormpath() for normsubpath in dp.path.normsubpaths: for i, normsubpathitem in enumerate(normsubpath.normsubpathitems): if isinstance(normsubpathitem, normpath.normcurve_pt): dp.ornaments.stroke( normpath.normpath( [normpath.normsubpath([normsubpathitem])]), [color.rgb.green]) else: dp.ornaments.stroke( normpath.normpath( [normpath.normsubpath([normsubpathitem])]), [color.rgb.blue]) for normsubpath in dp.path.normsubpaths: for i, normsubpathitem in enumerate(normsubpath.normsubpathitems): if isinstance(normsubpathitem, normpath.normcurve_pt): dp.ornaments.stroke( path.line_pt(normsubpathitem.x0_pt, normsubpathitem.y0_pt, normsubpathitem.x1_pt, normsubpathitem.y1_pt), [style.linestyle.dashed, color.rgb.red]) dp.ornaments.stroke( path.line_pt(normsubpathitem.x2_pt, normsubpathitem.y2_pt, normsubpathitem.x3_pt, normsubpathitem.y3_pt), [style.linestyle.dashed, color.rgb.red]) dp.ornaments.draw( path.circle_pt(normsubpathitem.x1_pt, normsubpathitem.y1_pt, r_pt), [filled([color.rgb.red])]) dp.ornaments.draw( path.circle_pt(normsubpathitem.x2_pt, normsubpathitem.y2_pt, r_pt), [filled([color.rgb.red])]) for normsubpath in dp.path.normsubpaths: for i, normsubpathitem in enumerate(normsubpath.normsubpathitems): if not i: x_pt, y_pt = normsubpathitem.atbegin_pt() dp.ornaments.draw(path.circle_pt(x_pt, y_pt, r_pt), [filled]) x_pt, y_pt = normsubpathitem.atend_pt() dp.ornaments.draw(path.circle_pt(x_pt, y_pt, r_pt), [filled])
def __init__(self): self.filename = normpath(default_logfile) self.loaded_filename = "" self.timestamp = 0 self.T = zeros(0) self.VALUE = zeros(0) from thread import allocate_lock self.lock = allocate_lock()
def image_timestamp(self): """Full pathname of the last recorded image""" from os.path import getmtime, dirname from os import listdir filename = normpath(self.settings.image_filename) if exists(filename): listdir(dirname(filename)) # for NFS attibute caching t = getmtime(filename) if exists(filename) else 0 return t
def add_view(app_root, file_path, app): path = file_path.replace(normpath(app_root, "templates"), "")[1:] if path[-5:] == ".html": url = path.replace(".html", "") if url[-5:] == "index": url = url[:-5] if url[-4:] == "home": url = url[:-4] else: url = path app.add_url_rule('/'+url, view_func=BaseView.as_view(path, template_name=path))
def content(self): from normpath import normpath filename = normpath(self.filename) from mmap import mmap, ACCESS_READ try: f = file(filename) content = mmap(f.fileno(), 0, access=ACCESS_READ) except IOError: content = "" return content
def image(self): from numimage import numimage filename = normpath(self.settings.image_filename) if exists(filename): image = numimage(filename) else: image = self.default_image # Needed for "BeamProfile" to detect image updates. # If a memory-mapped image would be chached by "BeamProfile", the # comparison with the new image would show no difference, since the # cached image dynamically updates. image = image.copy() return image
def SetState(self,state): ##debug("SetState %r" % state) for line in state.split("\n"): line = line.strip(" \n\r") if line != "": try: exec("self."+line) except: debug("ignoring "+line); pass if hasattr(self,"logfile"): debug("logfile %r" % self.logfile) self.logfile = normpath(self.logfile) debug("logfile %r" % self.logfile)
def normpath(self, epsilon=_marker): """convert the path into a normpath""" # use cached value if existent and epsilon is _marker if self._normpath is not None and epsilon is _marker: return self._normpath if self.pathitems: if epsilon is _marker: normpath = self.pathitems[0].createnormpath() else: normpath = self.pathitems[0].createnormpath(epsilon) context = self.pathitems[0].createcontext() for pathitem in self.pathitems[1:]: pathitem.updatenormpath(normpath, context) else: if epsilon is _marker: normpath = normpath([]) else: normpath = normpath(epsilon=epsilon) if epsilon is _marker: self._normpath = normpath return normpath
def save_traces_once(self): from os.path import exists, basename from normpath import normpath for count, i in self.files_to_save.keys(): source = self.trace_filename(i, count) if exists(source): destination = self.files_to_save[count, i] destination = normpath(destination) info("Saving %r as %r", basename(source), basename(destination)) ##info("Saving %r as %r",basename(source),destination) copy(source, destination) del self.files_to_save[count, i]
def decorate(self, dp, texrunner): r_pt = 2 dp.ensurenormpath() for normsubpath in dp.path.normsubpaths: for i, normsubpathitem in enumerate(normsubpath.normsubpathitems): if isinstance(normsubpathitem, normpath.normcurve_pt): dp.ornaments.stroke(normpath.normpath([normpath.normsubpath([normsubpathitem])]), [color.rgb.green]) else: dp.ornaments.stroke(normpath.normpath([normpath.normsubpath([normsubpathitem])]), [color.rgb.blue]) for normsubpath in dp.path.normsubpaths: for i, normsubpathitem in enumerate(normsubpath.normsubpathitems): if isinstance(normsubpathitem, normpath.normcurve_pt): dp.ornaments.stroke(path.line_pt(normsubpathitem.x0_pt, normsubpathitem.y0_pt, normsubpathitem.x1_pt, normsubpathitem.y1_pt), [style.linestyle.dashed, color.rgb.red]) dp.ornaments.stroke(path.line_pt(normsubpathitem.x2_pt, normsubpathitem.y2_pt, normsubpathitem.x3_pt, normsubpathitem.y3_pt), [style.linestyle.dashed, color.rgb.red]) dp.ornaments.draw(path.circle_pt(normsubpathitem.x1_pt, normsubpathitem.y1_pt, r_pt), [filled([color.rgb.red])]) dp.ornaments.draw(path.circle_pt(normsubpathitem.x2_pt, normsubpathitem.y2_pt, r_pt), [filled([color.rgb.red])]) for normsubpath in dp.path.normsubpaths: for i, normsubpathitem in enumerate(normsubpath.normsubpathitems): if not i: x_pt, y_pt = normsubpathitem.atbegin_pt() dp.ornaments.draw(path.circle_pt(x_pt, y_pt, r_pt), [filled]) x_pt, y_pt = normsubpathitem.atend_pt() dp.ornaments.draw(path.circle_pt(x_pt, y_pt, r_pt), [filled])
def files(self): """Contents of directory as relative filenames""" from time import time from normpath import normpath from os import listdir if time() - self.checktime > checktime: mtime = getmtime(self.pathname) if mtime != self.mtime: self.mtime = mtime ##debug("exists: reading dir %r" % self.pathname) pathname = normpath(self.pathname) if self.pathname else "." try: self.__files__ = set(listdir(pathname)) except OSError: self.__files__ = set() self.checktime = time() return self.__files__
def content(self): from os.path import exists, getsize from normpath import normpath filename = normpath(self.filename) if exists(filename): size_change = getsize(filename) - len(self.cached_content) if size_change > 0: ##debug("Logfile: Reading %d bytes" % size_change) f = file(filename) f.seek(len(self.cached_content)) self.cached_content += f.read() elif size_change < 0: ##debug("Logfile: Reading %d bytes" % getsize(filename)) self.cached_content = file(filename).read() else: self.cached_content = "" return self.cached_content
def exist_files(filenames): """filenames: list of pathnames return value: list of booleans""" from os import listdir from os.path import exists,dirname,basename from time import time from normpath import normpath from numpy import array exist_files = [] for f in filenames: d = dirname(f) if not d in directories or time() - directories[d]["time"] > timeout: try: files = listdir(normpath(d) if d else ".") except OSError: files = [] directories[d] = {"files":files,"time":time()} exist_files += [basename(f) in directories[d]["files"]] exist_files = array(exist_files) return exist_files
def OnBrowse(self, event): """Set destination folder for archive""" from os.path import exists, dirname from normpath import normpath pathname = channel_archiver.directory while pathname and not exists(pathname): pathname = dirname(pathname) dlg = wx.DirDialog(self, "Choose a directory:", style=wx.DD_DEFAULT_STYLE) # ShowModal pops up a dialog box and returns control only after the user # has selects OK or Cancel. dlg.Path = pathname if dlg.ShowModal() == wx.ID_OK: self.Directory.Value = normpath(str(dlg.Path)) dlg.Destroy() debug("channel_archiver.directory = %r" % str(self.Directory.Value)) channel_archiver.directory = str(self.Directory.Value)
def start_series(self,n_frames=None,first_frame_number=None, integration_time=None,interval_time=None,frame_trigger_type=None, series_trigger_type=None, filename_base=None,filename_suffix=None,number_field_width=None): """Start acqisition of image series.""" from normpath import normpath if n_frames is not None: self.n_frames = n_frames if first_frame_number is not None: self.first_frame_number = first_frame_number if filename_base is not None: self.filename_base = normpath(filename_base) if filename_suffix is not None: self.filename_suffix = filename_suffix if number_field_width is not None: self.number_field_width = number_field_width info("rayonix: Starting series of %d images..." % self.n_frames) self.frame_number = self.first_frame_number self.acquiring_series = True self.series_triggered = False # trigger pulse seen? from thread import start_new_thread start_new_thread(self.acquire_series,())
def log(self, *args, **kwargs): """Append to logfile time: time in seconds since 1970-01-01 00:00:00 UTC """ from time import time from time_string import date_time from normpath import normpath from os.path import exists, dirname from os import makedirs values = args if "time" in kwargs: timestamp = kwargs["time"] else: timestamp = time() with self.lock: # Allow only one thread at a time inside this function. filename = normpath(self.filename) if not exists(dirname(filename)): makedirs(dirname(filename)) if not exists(filename): header = "#" + "\t".join(self.columns) + "\n" else: header = "" fields = [date_time(timestamp)] + [str(v) for v in values] line = "\t".join(fields) + "\n" file(filename, "ab").write(header + line)
def get_file_tree(*args): for path, dirs, files in os.walk(normpath(*args)): for filename in files: yield os.path.join(path, filename)
def get_program_running(self): """Is the Aerobasic program executing the triggered motion loaded and running?""" from normpath import normpath return normpath(self.program_filename) == \ normpath(self.ensemble.program_directory+"/"+self.required_program_filenname)
def program_filename(self): """AeroBasic program""" from normpath import normpath filename = normpath(ensemble.program_directory) + "/image_scan.ab" return filename
def fixTagsOnly(relpath, data, kind): print("Fixing tags") if kind == 'xml': parser = etree.XMLParser(encoding='utf-8', strip_cdata=False, resolve_entities=True) root = etree.fromstring(data.encode(), parser) for el in root.xpath( '//*[self::link or self::url or local-name()="loc"]'): el.text = re.sub(source_url_regex, lambda _: args.target, el.text) # /favicon.png is a redirect in ghost, so no favicon.png exists. if el.text == args.target + '/favicon.png': el.text = args.target + '/favicon.ico' for el in root.xpath('//*[@href]'): el.attrib['href'] = re.sub(source_url_regex, lambda _: args.target, el.attrib['href']) for el in root.xpath( '//*[local-name()="link" and namespace-uri()="http://www.w3.org/2005/Atom" and @href and @rel="self" and @type="application/rss+xml"]' ): el.attrib['href'] = re.sub(r'/rss/$', '/rss/index.xml', el.attrib['href']) return etree.tostring(root, encoding='utf-8', pretty_print=True, xml_declaration=True).decode() elif kind == 'html': parser = etree.HTMLParser(encoding='utf-8') root = etree.fromstring(data.encode(), parser) # Remove ghost API javascript. Static files don't need this. for el in root.xpath( '//script[@type="text/javascript"][contains(@src,"public/ghost-sdk.")][contains(@src,".js")]' ): el.getparent().remove(el) for el in root.xpath( '//script[@type="text/javascript"][not(@src)]'): if re.match(r'\s*ghost\.init\(', el.text): el.getparent().remove(el) # Copy any remote static (has integrity attr) javascript files to be local for el in root.xpath( '//script[@integrity][starts-with(@src,"https:")]'): src = el.attrib['src'] integrity = el.attrib['integrity'] integrity_hash = integrity.split('-', maxsplit=1)[0] basename_split = os.path.splitext(os.path.basename(src)) destination = os.path.join( args.static_path, 'immutable', basename_split[0] + '-' + integrity + basename_split[1]) if (destination not in downloaded_external_scripts and (not os.path.isfile(destination) or integrity != calculate_sri(destination, integrity_hash))): destination_dirname = os.path.dirname(destination) if not os.path.isdir(destination_dirname): os.mkdir(destination_dirname) download_one_file_to_destination(src, destination) dl_integrity = calculate_sri(destination, integrity_hash) if integrity != dl_integrity: os.unlink(destination) print('Expected integrity', repr(integrity)) print('Found integrity', repr(dl_integrity)) raise Exception( 'Downloaded file had wrong integrity (' + destination + ')') downloaded_external_scripts.add(destination) el.attrib['src'] = os.path.relpath( destination, os.path.join(args.static_path, os.path.dirname(relpath))) del el.attrib['integrity'] if 'crossorigin' in el.attrib: del el.attrib['crossorigin'] for el in root.xpath( '/html/head//link[@rel="canonical" or @rel="amphtml"][@href]' ): if not abs_url_regex.search(el.attrib['href']): el.attrib['href'] = args.target + re.sub( r'(/|^)index\.html$', r'\1', normpath( PurePosixPath('/').joinpath( relpath.parent, el.attrib['href'])).as_posix()) for el in root.xpath( '/html/head//meta[@name or @property][@content]'): if re.search( ':url$', el.attrib['name'] if 'name' in el.attrib else el.attrib['property']): el.attrib['content'] = re.sub(source_url_regex, lambda _: args.target, el.attrib['content']) for el in root.xpath( '/html/head/script[@type="application/ld+json"]'): def urlFixer(o): for key, value in o.items(): if isinstance(value, str): o[key] = re.sub(source_url_regex, lambda _: args.target, o[key]) elif isinstance(value, dict): urlFixer(value) ld = json.loads(el.text) urlFixer(ld) el.text = "\n" + json.dumps(ld, sort_keys=True, indent=4) + "\n" for el in root.xpath('//*[@href]'): if not abs_url_regex.search(el.attrib['href']): new_href = re.sub(r'(/|^)rss/index\.html$', r'\1rss/index.xml', el.attrib['href']) new_href = re.sub(r'(/|^)index\.html$', r'\1', new_href) if el.attrib['href'] != new_href: el.attrib['href'] = new_href else: # Fix social sharing links el.attrib['href'] = re.sub(re.escape(args.source), lambda _: args.target, el.attrib['href']) # Fix feedly rss link el.attrib['href'] = re.sub( re.escape(args.target) + '((?:/[^?&]+)?)/rss/([?&]|$)', lambda m: args.target + m.group(1) + '/rss/index.xml' + m.group(2), el.attrib['href']) return etree.tostring(root, encoding='utf-8', pretty_print=True, method="html", doctype='<!DOCTYPE html>').decode() else: raise Exception("Unknown kind " + kind)
def __new__(subclass, arg=None, filename="", dtype=numpy.float32, shape=(0, 0), format="", array=None, pixelsize=nan): """filename: TIFF,PNG,JPEG or GIF image.""" ##print "numimage.__new__(%r,%r)" % (subclass,filename) from numpy import zeros, ndarray, nan import numpy if isinstance(arg, basestring): filename = arg elif isinstance(arg, ndarray): array = arg elif isinstance(arg, tuple) and len(arg) == 2: shape = arg else: raise (RuntimeError, "%s: expecting str,array or (w,h)" % type(arg)) info = {} self = None if filename: from normpath import normpath filename = normpath(filename) # A MAR CCD or Rayonix image is a TIFF image with NxN pixels, # depth 16 bit and a fixed-size 4096-byte TIFF header. # N = Nmax/bin_factor # Nmax = 7680 for MX340HS # Nmax = 4096 for MAR CCD image_sizes = [3840, 1920, 960, 480, 2048, 1024, 512] # pixels headersize = 4096 # bytes TIFF_header_size = 1024 from os.path import getsize filesize = getsize(filename) for image_size in image_sizes: image_nbytes = 2 * image_size**2 if filesize == headersize + image_nbytes: format = "RX" if DEBUG: debug("using memmap") from numpy import memmap, uint16, int32 self = memmap(filename, uint16, 'r', headersize, (image_size, image_size), 'F') # Read TIFF header. from struct import pack, unpack header = file(filename).read(headersize) offset, = unpack("I", header[4:8]) ntags, = unpack("h", header[offset:offset + 2]) offset = offset + 2 size = 12 class tag(): def __init__(self, type, dtype, length, data): self.type, self.dtype, self.length, self.data = type, dtype, length, data def __repr__(self): return "%r,%r,%r,%r" % (self.type, self.dtype, self.length, self.data) tags = {} for i in range(0, ntags): data = header[offset + i * size:offset + (i + 1) * size] type, dtype, length, data = unpack("<HHII", data) tags[type] = tag(type, dtype, length, data) if 283 in tags: # x resolution, type rational, data = pointer offset = tags[283].data num, den = unpack("II", header[offset:offset + 8]) res = float(num) / den # in dpi if DEBUG: debug("TIFF: 283: resolution num/den %r/%r=%g" % (num, den, res)) unit = nan if 296 in tags: # resolution unit, code 2 = inch, 3 = cm code = tags[296].data if code == 2: unit = 25.4 # inch elif code == 3: unit = 10 # cm if DEBUG: debug("TIFF: 296: resolution unit %r = %r mm" % (code, unit)) pixelsize = unit / res if DEBUG: debug("TIFF: pixelsize (%r mm)/%g = %.6f mm" % (unit, res, pixelsize)) # Rayonix High Speed Detector Manual v. 0.3, Ross Doyle, Justin Anderson # Chapter 8: Image Format (marccd) # Rayonix_HS_detector_manual-0.3a.pdf start = TIFF_header_size + 193 * 4 end = start + 4 if DEBUG: debug("RX: pixelsize [nm]: header[%r:%r] = %r" % (start, end, header[start:end])) frame_header = memmap(filename, int32, 'r', TIFF_header_size, (headersize - TIFF_header_size), 'F') pixelsize_nm = frame_header[193] pixelsize = pixelsize_nm * 1e-9 / 1e-3 # convert from nm to mm if DEBUG: debug("RX: int 193: pixelsize = %r nm = %.6f mm" % (pixelsize_nm, pixelsize)) if self is None: if filename.upper().endswith(".EDF"): header = file(filename).read(1024) headersize = header.find("}\n") + 2 header = header[0:headersize] lines = header.split("\n") for line in lines: line = line.strip(" ;") if line.startswith("Dim_1 = "): w = int(line.replace("Dim_1 = ", "")) if line.startswith("Dim_2 = "): h = int(line.replace("Dim_2 = ", "")) from numpy import memmap, uint16, int32 self = memmap(filename, uint16, 'r', headersize, (w, h), 'F') format = "EDF" else: from PIL import Image from numpy import uint8, uint16, uint32, float32 PIL_image = Image.open(filename) mode = PIL_image.mode ##PIL_image = PIL_image.convert("I") if mode == "1": self = numpy.array(PIL_image, bool).T elif mode == "I;8": self = numpy.array(PIL_image, uint8).T elif mode == "I;16": self = numpy.array(PIL_image, uint16).T elif mode == "I;32": self = numpy.array(PIL_image, uint32).T elif mode == "F;32": self = numpy.array(PIL_image, float32).T else: warn("Unknown data type %s" % mode) format = PIL_image.format info = PIL_image.info if "dpi" in info: pixelsize = 25.4 / info["dpi"][ 0] # convert from DPI to mm elif array is not None: self = array else: self = zeros(shape, dtype) self = self.view(subclass) self.filename = filename self.format = format self.info = info self.pixelsize = pixelsize return self
def parameter_filename(self): """AeroBasic program""" from normpath import normpath filename = normpath(ensemble.program_directory)+"/RasterScan_parameters.abi" return filename
import sys import os.path from normpath import normpath if __name__ == '__main__': print os.path.relpath(normpath(sys.argv[1]), normpath(sys.argv[2]))
def errorhandlers(app, app_root): if os.path.exists(normpath(app_root, "templates", "404.html")): @app.errorhandler(404) def not_found(error): return render_template('404.html'), 404 return app
def get_setup_filename(self): filename = self.setup_dirname + "/" + self.setup_basename from normpath import normpath filename = normpath(filename) return filename
def createnormpath(self, epsilon=_marker): if epsilon is _marker: return normpath([normsubpath(_arctobezierpath(self.x_pt, self.y_pt, self.r_pt, self.angle2, self.angle1))]).reversed() else: return normpath([normsubpath(_arctobezierpath(self.x_pt, self.y_pt, self.r_pt, self.angle2, self.angle1), epsilon=epsilon)]).reversed()
def set_logfile(self,new_logfile): log.filename = normpath(str(new_logfile))