def _get_object_spec(name, path): """ Given a summonable object's name, search for it on the given file and return its description. """ try: with builtin_open(path, "r") as fobj: content = SUMMON_FILE_SCHEMA(ruamel.yaml.safe_load(fobj.read())) objects = [x for x in content["objects"] if x["name"] == name] if not objects: raise SummonError("No object with name '{}'".format(name)) elif len(objects) >= 2: raise SummonError( "More than one object with name '{}'".format(name) ) return objects[0] except FileNotFoundError as exc: raise SummonError("Summon file not found") from exc except ruamel.yaml.YAMLError as exc: raise SummonError("Failed to parse summon file") from exc except Invalid as exc: raise SummonError(str(exc)) from exc
def get_mathics_threejs_backend_data(): """Load mathics-three-package.json. It contains version information.""" global mathics_threejs_backend_data if not mathics_threejs_backend_data: try: with builtin_open(settings.MATHICS_BACKEND_THREEJS_JSON_PATH, "rb") as version_json_fp: mathics_threejs_backend_data = json.load(version_json_fp) except: pass return mathics_threejs_backend_data
def __init__(self, filename, mode='rb', **compress_options): """Open a heatshrink LZSS encoded file. If filename is a str, bytes or unicode object, it gives the name of the file to be opened. Otherwise, it should be a file-like object, which will be used to read or write the compressed data. mode can be 'rb for reading (default) or 'wb' for (over)writing. 'r' and 'w' will be converted to to 'rb' and 'wb' respectively. """ self._lock = RLock() self._fp = None # Should the file be closed by us? self._close_fp = False self._mode = _MODE_CLOSED if mode in ('', 'r', 'rb'): self._mode_str = 'rb' self._mode = _MODE_READ elif mode in ('w', 'wb'): self._mode_str = 'wb' self._mode = _MODE_WRITE else: raise ValueError("Invalid mode: '{!r}'".format(mode)) if isinstance(filename, (str, bytes, unicode)): self._fp = builtin_open(filename, self._mode_str) # We opened the file, we need to close it self._close_fp = True elif hasattr(filename, 'read') or hasattr(filename, 'write'): # Implements the file protocol self._fp = filename else: msg = 'filename must be an str, bytes or a file-like object' raise TypeError(msg) if self._mode == _MODE_READ: raw = _DecompressReader(self._fp, core.Reader, **compress_options) self._buffer = io.BufferedReader(raw) else: writer = core.Writer(**compress_options) self._encoder = core.Encoder(writer) # File seek position self._pos = 0 # The file name. Defaults to None self.name = getattr(self._fp, 'name', None)
def get_MathJax_version(): """ Get the MathJax version the static and hacky way not involving javascript. """ three_file = osp.join( osp.normcase(osp.dirname(osp.abspath(__file__))), "media", "js", "mathjax", "MathJax.js", ) pattern = r'MathJax.version="(\d\.\d\.\d)"' match = re.search(pattern, builtin_open(three_file).read()) if match: return match.group(1) else: return "?.?.?"
def uopen(filename, mode='r', *args, **kwargs): """ Replacement for builtin open() that reads unicode even in ASCII environment In order to read unicode from text, python uses locale.getpreferredencoding to translate bytes to str. If the environment only provides ASCII encoding, this will fail since most office files contain unicode. Therefore, guess a good encoding here if necessary and open file with that. :returns: same type as the builtin :py:func:`open` """ # do not interfere if not necessary: if 'b' in mode: if DEBUG: print('Opening binary file, do not interfere') return builtin_open(filename, mode, *args, **kwargs) if 'encoding' in kwargs: if DEBUG: print('Opening file with encoding {!r}, do not interfere'.format( kwargs['encoding'])) return builtin_open(filename, mode, *args, **kwargs) if len(args) > 3: # "encoding" is the 4th arg if DEBUG: print('Opening file with encoding {!r}, do not interfere'.format( args[3])) return builtin_open(filename, mode, *args, **kwargs) # determine preferred encoding encoding = PREFERRED_ENCODING if DEBUG: print('preferred encoding is {}'.format(encoding)) if isinstance(encoding, str) and encoding.lower().startswith('utf'): if DEBUG: print('encoding is acceptable, open {} regularly'.format(filename)) return builtin_open(filename, mode, *args, **kwargs) # so we want to read text from a file but can probably only deal with ASCII # --> use fallback if DEBUG: print('Opening {} with fallback encoding {}'.format( filename, FALLBACK_ENCODING_OPEN)) if PY3: return builtin_open(filename, mode, *args, encoding=FALLBACK_ENCODING_OPEN, **kwargs) else: handle = builtin_open(filename, mode, *args, **kwargs) return codecs.EncodedFile(handle, FALLBACK_ENCODING_OPEN)
def upload_file(path, destination=None, options=None): if not isinstance(options, dict): options = {} pth = Path(path) stat = pth.stat() with builtin_open(path, 'rb') as local_file: if destination is None: destination = pth.name upload, etags, _bytes_written = upload_chunks(local_file, destination, options) params = { "action": "end", "etags": etags, "provided_mtime": datetime.utcfromtimestamp(stat.st_mtime).isoformat(), "ref": upload.ref, "size": stat.st_size } create(destination, params, options)
def download_file(self, output_file): with builtin_open(output_file, 'wb') as file: self.download_content(file)
def logged_open(filename, mode='r', *argv, **kwargs): from builtins import open as builtin_open logging.info("({}):\t{}".format(mode, filename)) return builtin_open(filename, mode, *argv, **kwargs)