本文整理匯總了Python中zim.fs.File.exists方法的典型用法代碼示例。如果您正苦於以下問題:Python File.exists方法的具體用法?Python File.exists怎麽用?Python File.exists使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類zim.fs.File
的用法示例。
在下文中一共展示了File.exists方法的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: testAttachFileDialog
# 需要導入模塊: from zim.fs import File [as 別名]
# 或者: from zim.fs.File import exists [as 別名]
def testAttachFileDialog(self):
'''Test AttachFileDialog'''
tmp_dir = self.create_tmp_dir('testAttachFileDialog')
file = File((tmp_dir, 'file_to_be_attached'))
file.write('Test 1 2 3\n')
newfile = File((tmp_dir, 'attachments', 'Test', 'foo', 'file_to_be_attached'))
self.assertTrue(file.exists())
self.assertFalse(newfile.exists())
dialog = zim.gui.AttachFileDialog(self.ui, path=Path('Test:foo'))
dialog.set_file(file)
示例2: get_template
# 需要導入模塊: from zim.fs import File [as 別名]
# 或者: from zim.fs.File import exists [as 別名]
def get_template(format, template):
'''Returns a Template object for a template name, file path, or File object'''
# NOTE: here the "category" needs to be a format at the same time !
if isinstance(template, File):
file = template
else:
if not is_path_re.match(template):
file = None
path = list(data_dirs(('templates', format)))
path.reverse()
for dir in path:
for basename in dir.list():
name = basename.rsplit('.')[0] # robust if no '.' in basename
if name == template:
file = dir.file(basename)
if file.exists(): # is a file
break
if not file:
file = File(template)
else:
file = File(template)
logger.info('Loading template from: %s', file)
if not file.exists():
raise AssertionError, 'No such file: %s' % file
basename, ext = file.basename.rsplit('.', 1)
resources = file.dir.subdir(basename)
return Template(file.readlines(), format, name=file.path, resources_dir=resources)
示例3: testSaveCopyDialog
# 需要導入模塊: from zim.fs import File [as 別名]
# 或者: from zim.fs.File import exists [as 別名]
def testSaveCopyDialog(self):
'''Test SaveCopyDialog'''
tmp_dir = self.create_tmp_dir('testSaveCopyDialog')
file = File((tmp_dir, 'save_copy.txt'))
self.assertFalse(file.exists())
dialog = zim.gui.SaveCopyDialog(self.ui)
dialog.set_file(file)
示例4: testImportPageDialog
# 需要導入模塊: from zim.fs import File [as 別名]
# 或者: from zim.fs.File import exists [as 別名]
def testImportPageDialog(self):
'''Test ImportPageDialog'''
tmp_dir = self.create_tmp_dir('testImportPageDialog')
file = File((tmp_dir, 'import_page.txt'))
file.write('test 123\n')
self.assertTrue(file.exists())
dialog = zim.gui.ImportPageDialog(self.ui)
dialog.set_file(file)
示例5: runTest
# 需要導入模塊: from zim.fs import File [as 別名]
# 或者: from zim.fs.File import exists [as 別名]
def runTest(self):
tmp_dir = self.create_tmp_dir()
file = File((tmp_dir, 'test.txt'))
file.write('test 123')
self.assertTrue(file.exists())
dialog = FileDialog(None, 'Test')
dialog.set_file(file)
示例6: delete_file
# 需要導入模塊: from zim.fs import File [as 別名]
# 或者: from zim.fs.File import exists [as 別名]
def delete_file(self, file=None, refresh=False):
'''Deletes a file and refreshes the treeview if refresh == True'''
if not file:
file = self.selected_file
refresh = True
logger.debug('Deleting %s' % file)
file = File(file)
if file.exists():
file.cleanup()
if refresh:
self.treeview.model.remove(self.iter)
示例7: get_file
# 需要導入模塊: from zim.fs import File [as 別名]
# 或者: from zim.fs.File import exists [as 別名]
def get_file(self):
file = File(self.uistate['output_file'])
if file.exists():
ok = QuestionDialog(self, (
_('File exists'), # T: message heading
_('This file already exists.\n'
'Do you want to overwrite it?' ) # T: detailed message, answers are Yes and No
) ).run()
if not ok:
return None
return file
示例8: DiagramGenerator
# 需要導入模塊: from zim.fs import File [as 別名]
# 或者: from zim.fs.File import exists [as 別名]
class DiagramGenerator(ImageGeneratorClass):
uses_log_file = False
object_type = 'diagram'
scriptname = 'diagram.dot'
imagename = 'diagram.png'
def __init__(self, plugin):
ImageGeneratorClass.__init__(self, plugin)
self.dotfile = TmpFile(self.scriptname)
self.dotfile.touch()
self.pngfile = File(self.dotfile.path[:-4] + '.png') # len('.dot') == 4
def generate_image(self, text):
if isinstance(text, basestring):
text = text.splitlines(True)
# Write to tmp file
self.dotfile.writelines(text)
# Call GraphViz
try:
dot = Application(dotcmd)
dot.run((self.pngfile, self.dotfile))
except ApplicationError:
return None, None # Sorry, no log
else:
if self.pngfile.exists():
return self.pngfile, None
else:
# When supplying a dot file with a syntax error, the dot command
# doesn't return an error code (so we don't raise
# ApplicationError), but we still don't have a png file to
# return, so return None.
return None, None
def cleanup(self):
self.dotfile.remove()
self.pngfile.remove()
示例9: testError
# 需要導入模塊: from zim.fs import File [as 別名]
# 或者: from zim.fs.File import exists [as 別名]
def testError(self):
def creator_with_failure(*a):
raise ThumbnailCreatorFailure
def creator_with_error(*a):
raise ValueError
file = File('./data/zim.png')
self.assertTrue(file.exists())
self.assertTrue(file.isimage())
for creator in creator_with_failure, creator_with_error:
#~ print ">>", creator.__name__
queue = ThumbnailQueue(creator)
queue.queue_thumbnail_request(file, 64)
with tests.LoggingFilter('zim.plugins.attachmentbrowser', 'Exception'):
queue.start()
while not queue.queue_empty():
r = queue.get_ready_thumbnail()
self.assertIsNone(r[0], None)
示例10: set_basedirs
# 需要導入模塊: from zim.fs import File [as 別名]
# 或者: from zim.fs.File import exists [as 別名]
def set_basedirs():
'''This method sets the global configuration paths for according to the
freedesktop basedir specification.
Called automatically when module is first loaded, should be
called explicitly only when environment has changed.
'''
global ZIM_DATA_DIR
global XDG_DATA_HOME
global XDG_DATA_DIRS
global XDG_CONFIG_HOME
global XDG_CONFIG_DIRS
global XDG_CACHE_HOME
# Cast string to folder
import zim
zim_data_dir = File(zim.ZIM_EXECUTABLE).dir.subdir('data')
if zim_data_dir.exists():
ZIM_DATA_DIR = zim_data_dir
if os.name == 'nt':
APPDATA = environ['APPDATA']
XDG_DATA_HOME = Dir(
environ.get('XDG_DATA_HOME', APPDATA + r'\zim\data'))
XDG_DATA_DIRS = map(Dir,
environ.get_list('XDG_DATA_DIRS', '~/.local/share/')) # Backwards compatibility
XDG_CONFIG_HOME = Dir(
environ.get('XDG_CONFIG_HOME', APPDATA + r'\zim\config'))
XDG_CONFIG_DIRS = map(Dir,
environ.get_list('XDG_CONFIG_DIRS', '~/.config/')) # Backwards compatibility
try:
import _winreg as wreg
wreg_key = wreg.OpenKey(
wreg.HKEY_CURRENT_USER,
r'Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders')
cache_dir = str(wreg.QueryValueEx(wreg_key, "Cache")[0].replace(u'%USERPROFILE%', environ['USERPROFILE']))
wreg.CloseKey(wreg_key)
except:
cache_dir = APPDATA + r'\zim\cache'
# Not using TMP here because it is cleaned too often
XDG_CACHE_HOME = Dir(
environ.get('XDG_CACHE_HOME', cache_dir + r'\zim'))
else:
XDG_DATA_HOME = Dir(
environ.get('XDG_DATA_HOME', '~/.local/share/'))
XDG_DATA_DIRS = map(Dir,
environ.get_list('XDG_DATA_DIRS', ('/usr/share/', '/usr/local/share/')))
XDG_CONFIG_HOME = Dir(
environ.get('XDG_CONFIG_HOME', '~/.config/'))
XDG_CONFIG_DIRS = map(Dir,
environ.get_list('XDG_CONFIG_DIRS', ('/etc/xdg/',)))
XDG_CACHE_HOME = Dir(
environ.get('XDG_CACHE_HOME', '~/.cache'))
示例11: main
# 需要導入模塊: from zim.fs import File [as 別名]
# 或者: from zim.fs.File import exists [as 別名]
def main(argv):
"""Run the main program
Depending on the commandline given and whether or not there is
an instance of zim running already, this method may return
immediatly, or go into the mainloop untill the program is exitted.
@param argv: commandline arguments, e.g. from C{sys.argv}
@raises UsageError: when number of arguments is not correct
@raises GetOptError: when invalid options are found
"""
global ZIM_EXECUTABLE
# FIXME - this returns python.exe on my windows test
ZIM_EXECUTABLE = argv[0]
zim_exec_file = File(ZIM_EXECUTABLE)
if zim_exec_file.exists():
# We were given an absolute path, e.g. "python ./zim.py"
ZIM_EXECUTABLE = zim_exec_file.path
# Check for special commandline args for ipc, does not return
# if handled
import zim.ipc
zim.ipc.handle_argv()
# Let getopt parse the option list
short = "".join(shortopts.keys())
for s, l in shortopts.items():
if l.endswith("="):
short = short.replace(s, s + ":")
long = list(longopts) + list(commands)
for opts in commandopts.values():
long.extend(opts)
opts, args = gnu_getopt(argv[1:], short, long)
# First figure out which command to execute
cmd = "gui" # default
if opts:
o = opts[0][0].lstrip("-")
if o in shortopts:
o = shortopts[o].rstrip("=")
if o in commands:
opts.pop(0)
cmd = o
# If it is a simple command execute it and return
if cmd == "version":
print "zim %s\n" % __version__
print __copyright__, "\n"
print __license__
return
elif cmd == "help":
print usagehelp.replace("zim", argv[0])
print optionhelp
return
# Otherwise check the number of arguments
if cmd in maxargs and len(args) > maxargs[cmd]:
raise UsageError
# --manual is an alias for --gui /usr/share/zim/manual
if cmd == "manual":
cmd = "gui"
args.insert(0, data_dir("manual").path)
# Now figure out which options are allowed for this command
allowedopts = list(longopts)
allowedopts.extend(commandopts[cmd])
# Convert options into a proper dict
optsdict = {}
for o, a in opts:
o = str(o.lstrip("-")) # str() -> no unicode for keys
if o in shortopts:
o = shortopts[o].rstrip("=")
if o + "=" in allowedopts:
o = o.replace("-", "_")
optsdict[o] = a
elif o in allowedopts:
o = o.replace("-", "_")
optsdict[o] = True
else:
raise GetoptError, ("--%s is not allowed in combination with --%s" % (o, cmd), o)
# --port is the only option that is not of type string
if "port" in optsdict and not optsdict["port"] is None:
try:
optsdict["port"] = int(optsdict["port"])
except ValueError:
raise GetoptError, ("--port takes an integer argument", "port")
# set logging output level for logging root (format has been set in zim.py)
if not ZIM_EXECUTABLE[-4:].lower() == ".exe":
# for most platforms
level = logging.WARN
else:
#.........這裏部分代碼省略.........
示例12: ConfigFile
# 需要導入模塊: from zim.fs import File [as 別名]
# 或者: from zim.fs.File import exists [as 別名]
class ConfigFile(object):
'''Container object for a config file
Maps to a "base" file in the home folder, used to write new values,
and one or more default files, e.g. in C{/usr/share/zim}, which
are the fallback to get default values
@ivar file: the underlying file object for the base config file
in the home folder
@note: this class implement similar API to the L{File} class but
is explicitly not a sub-class of L{File} because config files should
typically not be moved, renamed, etc. It just implements the reading
and writing methods.
'''
def __init__(self, path, file=None):
'''Constructor
@param path: either basename as string or tuple with relative path,
is resolved relative to the default config dir for zim.
@param file: optional argument for some special case to
override the base file in the home folder.
'''
if isinstance(path, basestring):
path = (path,)
self._path = tuple(path)
if file:
self.file = file
else:
self.file = File((XDG_CONFIG_HOME, 'zim') + self._path)
def __repr__(self):
return '<%s: %s>' % (self.__class__.__name__, self.file.path)
def __eq__(self, other):
return isinstance(other, ConfigFile) \
and other._path == self._path \
and other.file == self.file
@property
def basename(self):
return self.file.basename
def default_files(self):
'''Generator that yields default config files (read-only) to
use instead of the standard file when it is still empty.
Typically only the first one is used.
'''
for dir in config_dirs():
default = dir.file(self._path)
if default.exists():
yield default
def touch(self):
'''Ensure the custom file in the home folder exists. Either by
copying a default config file, or touching an empty file.
Intended to be called before trying to edit the file with an
external editor.
'''
if not self.file.exists():
for file in self.default_files():
file.copyto(self.file)
break
else:
self.file.touch() # create empty file
def read(self, fail=False):
'''Read the base file or first default file
@param fail: if C{True} a L{FileNotFoundError} error is raised
when neither the base file or a default file are found. If
C{False} it will return C{''} for a non-existing file.
@returns: file content as a string
'''
try:
return self.file.read()
except FileNotFoundError:
for file in self.default_files():
return file.read()
else:
if fail:
raise
else:
return ''
def readlines(self, fail=False):
'''Read the base file or first default file
@param fail: if C{True} a L{FileNotFoundError} error is raised
when neither the base file or a default file are found. If
C{False} it will return C{[]} for a non-existing file.
@returns: file content as a list of lines
'''
try:
return self.file.readlines()
except FileNotFoundError:
for file in self.default_files():
return file.readlines()
else:
if fail:
raise
else:
#.........這裏部分代碼省略.........
示例13: runTest
# 需要導入模塊: from zim.fs import File [as 別名]
# 或者: from zim.fs.File import exists [as 別名]
def runTest(self):
plugins = PluginManager.list_installed_plugins()
self.assertTrue(len(plugins) > 10)
self.assertTrue('spell' in plugins)
self.assertTrue('linkmap' in plugins)
pluginindex = File('data/manual/Plugins.txt').read()
seen = {
'name': set(),
'description': set(),
'help': set(),
}
for name in plugins:
#~ print '>>', name
klass = PluginManager.get_plugin_class(name)
# test plugin info
for key in ('name', 'description', 'author'):
self.assertTrue(
klass.plugin_info.get(key),
'Plugin %s misses info field \'%s\'' % (name, key)
)
for key in ('name', 'description', 'help'):
self.assertIn(key, klass.plugin_info, 'Plugin %s missing "%s"' % (name, key))
value = klass.plugin_info[key]
self.assertFalse(
value in seen[key],
'Value for \'%s\' in %s seen before - copy-paste error ?' % (key, name)
)
seen[key].add(value)
# test manual page present and at least documents preferences
page = klass.plugin_info['help']
self.assertTrue(page.startswith('Plugins:'), 'Help page for %s not valid' % name)
rellink = "+%s" % page[8:]
self.assertIn(rellink, pluginindex, 'Missing links "%s" in manual/Plugins.txt' % rellink)
file = File('data/manual/' + page.replace(':', '/').replace(' ', '_') + '.txt')
self.assertTrue(file.exists(), 'Missing file: %s' % file)
manual = file.read()
for pref in klass.plugin_preferences:
label = pref[2]
if '\n' in label:
label, x = label.split('\n', 1)
label = label.rstrip(',')
self.assertIn(label, manual, 'Preference "%s" for %s plugin not documented in manual page' % (label, name))
# test dependencies data
dep = klass.check_dependencies()
self.assertTrue(isinstance(dep,tuple))
check, dep = dep
self.assertTrue(isinstance(check,bool))
self.assertTrue(isinstance(dep,list))
for i in range(len(dep)):
self.assertTrue(isinstance(dep[i],tuple))
self.assertTrue(isinstance(dep[i][0],str))
self.assertTrue(isinstance(dep[i][1],bool))
self.assertTrue(isinstance(dep[i][2],bool))