def test_fs_data(self): self.assertTrue( issubclass(self.fs, vfs.VirtualFilePath), "OSFilePath subclass of VirtualFilePath abstract class") self.assertTrue(to_text(self.fs.curdir) == ".", "Current directory component") self.assertTrue(to_text(self.fs.pardir) == "..", "Parent directory component")
def run_constructor(self): path = self.fs() self.assertTrue(isinstance(path, vfs.VirtualFilePath), "VirtualFilePath abstract class") self.assertTrue(path.is_empty(), "Empty path creation") self.assertTrue(path.is_single_component(), "Empty path is a single component") self.assertFalse(path.is_dirlike(), "Empty path is not diretory like") self.assertFalse(path.is_root(), "Empty path is not the root") self.assertFalse(path, "Non-zero test of empty path") path = self.fs('hello') self.assertFalse(path.is_empty(), "Single path component") self.assertTrue(path.is_single_component(), "path is a single component") self.assertFalse(path.is_dirlike(), "path is not diretory like") self.assertFalse(path.is_root(), "path is not the root") self.assertTrue(path.to_bytes() == b"hello", "convert to binary str") self.assertTrue(to_text(path) == ul("hello"), "convert to text") self.assertTrue(path, "Non-zero test of non-empty path") # create a path from a path path = self.fs(path) self.assertTrue(to_text(path) == ul("hello"), "check path") # create a path from a string and instance mix hello_world = ul("hello") + to_text(self.fs.sep) + ul("world") path = self.fs(path, 'world') self.assertTrue(to_text(path) == hello_world) # create a path from a string ending with the separator path = self.fs(ul('hello') + to_text(self.fs.sep), 'world') self.assertTrue(to_text(path) == hello_world) self.assertTrue(str(path) == str(hello_world)) path = self.fs(ul('Caf\xe9')) self.assertTrue(path.to_bytes() == ul('Caf\xe9').encode(path.codec), "convert to binary string") self.assertTrue(to_text(path) == ul('Caf\xe9'), "convert to text") # create a path with a trailing sep hello = self.fs.path_str('hello') + self.fs.sep path = self.fs(hello) self.assertFalse(path.is_empty(), "Trailing slash non empty") self.assertFalse(path.is_single_component(), "trailing slash is a single component") self.assertTrue(path.is_dirlike(), "trailing slash diretory like") self.assertFalse(path.is_root(), "trailing slash not the root") self.assertTrue(to_text(path) == ul(hello), "convert to text") # create a path with a trailing sep and current dir indicator hello = self.fs.path_str('hello') + self.fs.sep + self.fs.curdir path = self.fs(hello) self.assertFalse(path.is_empty(), "Trailing dot-slash non empty") self.assertFalse(path.is_single_component(), "trailing dot-slash is a single component") self.assertTrue(path.is_dirlike(), "trailing dot-slash diretory like") self.assertFalse(path.is_root(), "trailing dot-slash not the root") self.assertTrue(to_text(path) == ul(hello), "convert to text") # bad argument types raise TypeError try: path = self.fs(45) self.fail("constructor requires string argument") except TypeError: pass
def test_char_range(self): """:: charRange ::= seRange | XmlCharIncDash """ p = xsi.RegularExpressionParser("^\\^-bxa-c-\\?-A") for match in ["\\^", "[_-b^]", "x", "[a-c]", "-", "[?-A]"]: cclass = p.require_char_range() self.assertTrue(to_text(cclass) == match, "Expected %s, found %s" % (match, to_text(cclass))) p = xsi.RegularExpressionParser("[") try: cclass = p.require_char_range() self.fail("Parsed [ as CharRange") except xsi.RegularExpressionError: pass
def test_unicode_relatives(self): """The idea of this test to ensure that UTF-8 relative paths work even if the underlying file system uses a different encoding.""" class LatinSystem(vfs.MemFilePath): fs_name = "latinfs.pyslet.org" codec = "latin-1" # must override these to prevent mixed instances _wd = None _fsdir = {} # need to register this file system to enable us to obtain # instances from FileURLs. vfs.register_file_system(LatinSystem) upath = ul(b"Caf\xe9") fpath = LatinSystem(LatinSystem.sep + upath) furl = uri.URI.from_virtual_path(fpath) # the native file system encoding is not UTF-8 so we might # expect the file name in the URL to be latin-1 encoded but # this would be madness as relative URLs would be completely # messed up and non-portable between systems (and we need # them to work in Pyslet). self.assertTrue(to_text(furl) == ul("file://latinfs.pyslet.org/Caf%C3%A9")) result = furl.get_file_name() self.assertTrue(result == upath, "file name in URL: %s" % repr(result)) # This is an absolute URL in a file system with a known non-UTF8 # path encoding so we should be able to transform to a locally # portable URL containing the native system encoding instead # this is a concession to working on Windows result = furl.to_local_text() self.assertTrue(result == ul("file://latinfs.pyslet.org/Caf%E9"), result) # now lets go back to the virtual file system to check there too result = furl.get_virtual_file_path() self.assertTrue(to_text(result) == ul(b"/Caf\xe9")) # here's the rub, we expect the resolution of relative URI to # work portably file_url = uri.URI.from_octets("Caf%C3%A9.txt").resolve(furl) self.assertTrue(to_text(file_url) == ul("file://latinfs.pyslet.org/Caf%C3%A9.txt")) result = file_url.get_file_name() self.assertTrue(result == upath + ul(".txt"), "relative file name in URL: %s" % repr(result)) self.assertTrue(file_url.to_local_text() == ul("file://latinfs.pyslet.org/Caf%E9.txt")) result = file_url.get_virtual_file_path() self.assertTrue(to_text(result) == ul(b"/Caf\xe9.txt"), result)
def load_notes(weather_notes, file_name, weather_data): with open(file_name, 'r') as f: id = 1 with weather_notes.open() as collection: with weather_data.open() as data: while True: line = f.readline() if len(line) == 0: break elif line[0] == '#': continue note_words = line.split() if note_words: note = collection.new_entity() note['ID'].set_from_value(id) start = iso.TimePoint( date=iso.Date.from_str(note_words[0]), time=iso.Time(hour=0, minute=0, second=0)) note['StartDate'].set_from_value(start) end = iso.TimePoint( date=iso.Date.from_str( note_words[1]).offset(days=1), time=iso.Time(hour=0, minute=0, second=0)) note['EndDate'].set_from_value(end) note['Details'].set_from_value( ' '.join(note_words[2:])) collection.insert_entity(note) # now find the data points that match data.set_filter( core.CommonExpression.from_str( "TimePoint ge datetime'%s' and " "TimePoint lt datetime'%s'" % (to_text(start), to_text(end)))) for data_point in data.values(): # use values, not itervalues to avoid this bug # in Python 2.7 http://bugs.python.org/issue10513 data_point['Note'].bind_entity(note) data.update_entity(data_point) id = id + 1 with weather_notes.open() as collection: collection.set_orderby( core.CommonExpression.orderby_from_str('StartDate desc')) for e in collection.itervalues(): with e['DataPoints'].open() as affectedData: output( "%s-%s: %s (%i data points affected)" % (to_text(e['StartDate'].value), to_text(e['EndDate'].value), e['Details'].value, len(affectedData)))
def test_char_range(self): """:: charRange ::= seRange | XmlCharIncDash """ p = xsi.RegularExpressionParser("^\\^-bxa-c-\\?-A") for match in ["\\^", "[_-b^]", "x", "[a-c]", "-", "[?-A]"]: cclass = p.require_char_range() self.assertTrue( to_text(cclass) == match, "Expected %s, found %s" % (match, to_text(cclass))) p = xsi.RegularExpressionParser("[") try: cclass = p.require_char_range() self.fail("Parsed [ as CharRange") except xsi.RegularExpressionError: pass
def test_constructor(self): u = uri.URI(SIMPLE_EXAMPLE) self.assertTrue(isinstance(u, uri.URI)) self.assertTrue(str(u) == SIMPLE_EXAMPLE) self.assertTrue(is_unicode(u.octets), "octets must be a character string") if py2: self.assertTrue(to_text(u) == SIMPLE_EXAMPLE) try: u = uri.URI(LIST_EXAMPLE) # we don't support this type of thing any more # self.assertTrue(str(u)==SIMPLE_EXAMPLE,"Simple from list") except uri.URIException: pass u = uri.URI.from_octets(u8(b'\xe8\x8b\xb1\xe5\x9b\xbd.xml')) self.assertTrue( str(u) == '%E8%8B%B1%E5%9B%BD.xml', "Unicode example: %s" % str(u)) self.assertTrue(is_unicode(u.octets), "octets must be a character string") try: u = uri.URI.from_octets(u8(b'\xe8\x8b\xb1\xe5\x9b\xbd.xml'), strict=True) self.fail("strict mode requires %-encoding") except uri.URIException: pass # binary string must be US-ASCII clean try: u = uri.URI.from_octets(b'Caf\xe9') self.fail("binary string must be US-ASCII") except UnicodeDecodeError: pass # but URI-encoded is OK even if it is binary u = uri.URI.from_octets(b'Caf%E9') self.assertTrue(is_unicode(u.octets), "octets must be a character string")
def test_getcroot(self): wd = self.fs.getcroot() self.assertTrue(isinstance(wd, self.fs)) self.assertTrue(isinstance(wd, vfs.VirtualFilePath)) self.assertTrue(to_text(wd) == ul('/')) self.assertTrue(wd.to_bytes() == b'/') self.assertTrue(isinstance(wd.to_bytes(), bytes))
def test_model(drop=False): """Read and write some key value pairs""" doc = load_metadata() make_container(doc, drop) weather_data = doc.root.DataServices[ 'WeatherSchema.CambridgeWeather.DataPoints'] weather_notes = doc.root.DataServices[ 'WeatherSchema.CambridgeWeather.Notes'] if drop: load_data(weather_data, SAMPLE_DIR) load_notes(weather_notes, 'weathernotes.txt', weather_data) with weather_data.open() as collection: collection.set_orderby( core.CommonExpression.orderby_from_str('WindSpeedMax desc')) collection.set_page(30) for e in collection.iterpage(): note = e['Note'].get_entity() if e['WindSpeedMax'] and e['Pressure']: output( "%s: Pressure %imb, max wind speed %0.1f knots " "(%0.1f mph); %s" % ( to_text(e['TimePoint'].value), e['Pressure'].value, e['WindSpeedMax'].value, e['WindSpeedMax'].value * 1.15078, note['Details'] if note is not None else ""))
def test_text(self): data = "hello" udata = u"hello" bdata = b"hello" xdata = ['hello'] self.assertTrue(py2.is_text(data)) self.assertTrue(py2.is_text(udata)) if sys.version_info[0] < 3: self.assertTrue(py2.is_text(bdata)) else: self.assertFalse(py2.is_text(bdata)) self.assertFalse(py2.is_text(xdata)) if sys.version_info[0] < 3: self.assertFalse(py2.is_unicode(data)) else: self.assertTrue(py2.is_unicode(data)) self.assertTrue(py2.is_unicode(udata)) self.assertFalse(py2.is_unicode(bdata)) self.assertFalse(py2.is_unicode(xdata)) # force text forces strings to be text self.assertTrue(data == py2.force_text(data)) self.assertTrue(isinstance(py2.force_text(data), type(u""))) if sys.version_info[0] < 3: self.assertFalse(isinstance(py2.force_text(data), type(""))) else: self.assertTrue(isinstance(py2.force_text(data), type(""))) self.assertTrue(isinstance(py2.force_text(data), type(u""))) self.assertTrue(data == py2.force_text(udata)) self.assertTrue(isinstance(py2.force_text(udata), type(u""))) if sys.version_info[0] < 3: # force_text will not throw an error in python 2 pass else: try: py2.force_text(bdata) self.fail("force_text(bytes)") except TypeError: pass # this must work in both python 2 and 3 to prevent accidental # conversion to string. try: py2.force_text(xdata) self.fail("force_text(object)") except TypeError: pass # conversion to text self.assertTrue(data == py2.to_text(data)) self.assertTrue(isinstance(py2.to_text(data), type(u""))) self.assertTrue(data == py2.to_text(udata)) self.assertTrue(isinstance(py2.to_text(udata), type(u""))) self.assertTrue(data == py2.to_text(bdata)) self.assertTrue(isinstance(py2.to_text(bdata), type(u""))) if sys.version_info[0] < 3: self.assertTrue(u"['hello']" == py2.to_text(xdata)) else: self.assertTrue("['hello']" == py2.to_text(xdata))
def run_abs(self): path = self.fs.getcwd() self.assertTrue(path.isabs(), "CWD not absolute:" + to_text(path)) self.assertTrue(self.fs.isabs(path), "CWD not absolute, alternative") self.assertTrue(path.abspath() == path, "Absolute path from cwd") path = self.fs('hello') self.assertFalse(path.isabs(), "path component not absolute") self.assertFalse( self.fs.isabs(path), "path component not absolute, alternative")
def run_split_ext(self): path = self.fs.getcwd().join('hello.txt') root, ext = path.splitext() self.assertTrue(isinstance(root, vfs.VirtualFilePath), "Splitext root is a virtual file path") self.assertTrue(is_text(ext), "extension returns a string type") self.assertTrue(str(root.split()[1]) == "hello", "root match") self.assertTrue(ext == ".txt", "ext match") path = self.fs.getcwd().join('hello') root, ext = path.splitext() self.assertTrue(root == path) self.assertTrue(to_text(ext) == to_text('')) # catch an odd case hello = ul("hello") + to_text(self.fs.sep) path = self.fs(hello) root, ext = path.splitext() self.assertTrue(root == path) self.assertTrue(to_text(ext) == to_text(''))
def visit_method(self, dirname, names): # Make d a directory like path by adding an empty component at the end d = uri.URI.from_virtual_path(dirname.join(dirname.curdir)) for name in names: if to_text(name).startswith('??') or \ name.to_bytes().startswith(b'??'): logging.warn("8-bit path tests limited to ASCII file names") continue join_match = dirname.join(name) seg_name = uri.escape_data( to_text(name).encode(name.codec), uri.is_path_segment_reserved) u = uri.URI(seg_name) u = u.resolve(d) self.assertTrue(isinstance(u, uri.FileURL)) joined = u.get_virtual_file_path() self.assertTrue(joined == join_match, "Joined pathnames mismatch:\n%s\n%s" % (joined, join_match))
def test_getcwd(self): wd = self.fs.getcwd() self.assertTrue(isinstance(wd, self.fs)) self.assertTrue(isinstance(wd, vfs.VirtualFilePath)) self.assertTrue(to_text(wd) == ul('C:\\home')) # the current drive letter is used to make a path absolute path = self.fs(ul('\\home')) self.assertTrue(path.isabs(), "Missing path letter still absolute") apath = path.abspath() self.assertTrue(apath != path, "Path should change for abspath") self.assertTrue(apath.splitdrive()[0] == ul('C:')) # check that the drive is not absolute self.assertFalse(apath.splitdrive()[0].isabs())
def test_text(self): data = "hello" udata = u"hello" bdata = b"hello" xdata = ['hello'] self.assertTrue(py2.is_string(data)) self.assertTrue(py2.is_string(udata)) self.assertTrue(py2.is_string(bdata)) self.assertFalse(py2.is_string(xdata)) self.assertTrue(py2.is_text(data)) self.assertTrue(py2.is_text(udata)) if sys.version_info[0] < 3: self.assertTrue(py2.is_text(bdata)) else: self.assertFalse(py2.is_text(bdata)) self.assertFalse(py2.is_text(xdata)) if sys.version_info[0] < 3: self.assertFalse(py2.is_unicode(data)) else: self.assertTrue(py2.is_unicode(data)) self.assertTrue(py2.is_unicode(udata)) self.assertFalse(py2.is_unicode(bdata)) self.assertFalse(py2.is_unicode(xdata)) # force text forces strings to be text self.assertTrue(data == py2.force_text(data)) self.assertTrue(isinstance(py2.force_text(data), type(u""))) if sys.version_info[0] < 3: self.assertFalse(isinstance(py2.force_text(data), type(""))) else: self.assertTrue(isinstance(py2.force_text(data), type(""))) self.assertTrue(data == py2.force_text(udata)) self.assertTrue(isinstance(py2.force_text(udata), type(u""))) if sys.version_info[0] < 3: self.assertFalse(isinstance(py2.force_text(udata), type(""))) else: self.assertTrue(isinstance(py2.force_text(udata), type(""))) if sys.version_info[0] < 3: # force_text will not throw an error in python 2 pass else: try: py2.force_text(bdata) self.fail("force_text(bytes)") except TypeError: pass # this must work in both python 2 and 3 to prevent accidental # conversion to string. try: py2.force_text(xdata) self.fail("force_text(object)") except TypeError: pass # force ascii forces strings to be ascii text self.assertTrue(data == py2.force_ascii(data)) if sys.version_info[0] < 3: self.assertFalse(isinstance(py2.force_ascii(data), type(u""))) else: self.assertTrue(isinstance(py2.force_ascii(data), type(u""))) self.assertTrue(isinstance(py2.force_ascii(data), type(""))) self.assertTrue(data == py2.force_ascii(udata)) if sys.version_info[0] < 3: self.assertFalse(isinstance(py2.force_ascii(udata), type(u""))) else: self.assertTrue(isinstance(py2.force_ascii(udata), type(u""))) self.assertTrue(isinstance(py2.force_ascii(udata), type(""))) if sys.version_info[0] < 3: self.assertTrue(bdata == py2.force_ascii(bdata)) self.assertFalse(isinstance(py2.force_ascii(bdata), type(u""))) else: # can't compare different types in Python 3 self.assertFalse(bdata == py2.force_ascii(bdata)) self.assertTrue(isinstance(py2.force_ascii(bdata), type(u""))) self.assertTrue(isinstance(py2.force_ascii(bdata), type(""))) # this must work in both python 2 and 3 to prevent accidental # conversion to string. try: py2.force_ascii(xdata) self.fail("force_ascii(object)") except TypeError: pass # conversion to text self.assertTrue(data == py2.to_text(data)) self.assertTrue(isinstance(py2.to_text(data), type(u""))) self.assertTrue(data == py2.to_text(udata)) self.assertTrue(isinstance(py2.to_text(udata), type(u""))) self.assertTrue(data == py2.to_text(bdata)) self.assertTrue(isinstance(py2.to_text(bdata), type(u""))) if sys.version_info[0] < 3: self.assertTrue(u"['hello']" == py2.to_text(xdata)) else: self.assertTrue("['hello']" == py2.to_text(xdata)) # check the empty text constant: self.assertTrue(isinstance(py2.uempty, type(u""))) self.assertFalse(py2.uempty) self.assertTrue(len(py2.uempty) == 0)
def test_getcroot(self): wd = self.fs.getcroot() self.assertTrue(isinstance(wd, self.fs)) self.assertTrue(isinstance(wd, vfs.VirtualFilePath)) self.assertTrue(to_text(wd) == ul('C:\\'))
def __unicode__(self): return py2.to_text(self.data)
def test_getcwd(self): wd = self.fs.getcwd() self.assertTrue(isinstance(wd, self.fs)) self.assertTrue(isinstance(wd, vfs.VirtualFilePath)) self.assertTrue(to_text(wd) == ul('\\home'))