Notice: This forum has been recovered from an old backup, so some content, links, and dates may be outdated. The forum is currently read-only while we restore sign-in and registration functionality. Details

If you find this forum valuable and would like to help keep it online, donations to help cover hosting and domain costs are greatly appreciated, but never expected. You can support the forum through Buy Me a Coffee or Ko-fi. Thank you for helping preserve the EventGhost community.

check if file exist

If you have a question or need help, this is the place to be.
Post Reply
Diabl0570
Posts: 30
Joined: Sun Feb 28, 2010 6:05 pm

check if file exist

Post by Diabl0570 »

hello,
i cannot post any topics inthe plugin support forum so i post this here....

i'm working on my fist plugin and i want to check if an file exist,
this is my code for now:

Code: Select all

import eg
import datetime
import time
import os

eg.RegisterPlugin(
    name = "My New Plugin",
    author = "Me",
    version = "0.0.1",
    kind = "other",
    description = "This is an example plugin."
)

eg.RegisterPlugin()


class MyNewPlugin(eg.PluginBase):

    def __init__(self):
        self.AddAction(HelloWorld)
        self.AddAction(Test)
        self.AddAction(LogFileHandler)
        print "MyNewPlugin is inited."

    def __start__(self, myString):
        print "MyNewPlugin is started with parameter: " + myString

    def __stop__(self):
        print "MyNewPlugin is stopped."

    def __close__(self):
        print "MyNewPlugin is closed."

    def Configure(self, myString=""):
        panel = eg.ConfigPanel()
        textControl = wx.TextCtrl(panel, -1, myString)
        panel.sizer.Add(textControl, 1, wx.EXPAND)
        while panel.Affirmed():
            panel.SetResult(textControl.GetValue())
		
class HelloWorld(eg.ActionBase):

    def __call__(self):
        print "Hello World!"
		
class Test(eg.ActionBase):

    def __call__(self):
        print "Test!"
        filename = "LogFile.txt"
        if os.path.exists(filename):
                print "file exists"
        else:
                print "file not exists"


class LogFileHandler(eg.ActionBase):

	def __call__(self, myString):
		now = datetime.datetime.now()
		TimeDateNow = now.ctime()

		newLine =TimeDateNow + " :        " +myString +".\n"
		today = datetime.date.today()
		file = open("LogFile.txt", "a")
		file.write(newLine)
		file.close()
		return
		def Configure(self, myString=""):
			panel = eg.ConfigPanel()
			textControl = wx.TextCtrl(panel, -1, myString)
			panel.sizer.Add(textControl, 1, wx.EXPAND)
			while panel.Affirmed():
				panel.SetResult(textControl.GetValue())

Code: Select all

   	
class Test(eg.ActionBase):

    def __call__(self):
        print "Test!"
        filename = "LogFile.txt"
        if os.path.exists(filename):
                print "file exists"
        else:
                print "file not exists"
The problem is the class Test, if i remove it that class i get no errors.
First i tried to run the code of the class test in a python decompiler and also gave me no errors.

When i try it in eventghost i get the following errors:

Code: Select all

Error while loading plugin-file KPI.
Traceback (most recent call last) (1462):
  File "C:\Program Files (x86)\EventGhost\eg\Classes\PluginManager.py", line 178, in LoadPluginInfo
  File "C:\Program Files (x86)\EventGhost\eg\Classes\PluginInfo.py", line 126, in ImportPlugin
  File "C:\Program Files (x86)\EventGhost\plugins\KPI\__init__.py", line 52
    filename = "LogFile.txt"
   ^
IndentationError: unexpected indent
KPI
Error while loading plugin-file KPI.
Traceback (most recent call last) (1462):
  File "C:\Program Files (x86)\EventGhost\eg\Classes\PluginManager.py", line 178, in LoadPluginInfo
  File "C:\Program Files (x86)\EventGhost\eg\Classes\PluginInfo.py", line 126, in ImportPlugin
  File "C:\Program Files (x86)\EventGhost\plugins\KPI\__init__.py", line 52
    filename = "LogFile.txt"
   ^
IndentationError: unexpected indent
KPI
---> Welcome to EventGhost <---
Can't find action: MyNewPlugin.PrintString(u'helloooooo')
Autostart
   Plugin: Task Create/Switch Events
   Python Script
   Plugin: KPI #
      Error starting plugin: KPI
      Plugin not found!
Main.OnInit
Task.Activated.EventGhost
anyone who knows what i'm doing wrong, or have any tips please replay.
THANKS in ADVANCED
Diabl0570
Posts: 30
Joined: Sun Feb 28, 2010 6:05 pm

Re: check if file exist FIXED

Post by Diabl0570 »

oke so i copied the code from an website,
all the TABS ware SPACES!

i figuered this out when i posted my topic, because the code didn't fit right

Code: Select all

class Test(eg.ActionBase):

    def __call__(self):
        print "Test!"
		filename = "LogFile.txt"
        if os.path.exists(filename):
			print "file exists"
		else:
			print "file not exists"
replaced all the spaces for tabs and the code looks like:

Code: Select all

class Test(eg.ActionBase):

    def __call__(self):
		print "Test!"
		filename = "LogFile.txt"
		if os.path.exists(filename):
			print "file exists"
		else:
			print "file not exists"
so this was a good lessen for me, if you copy tour code check if the tabs are really tabs and not spaces

anyway thanks because starting a topic did help to find my problem ^_^ :lol: !
stottle
Plugin Developer
Posts: 636
Joined: Sun Apr 26, 2009 10:59 pm

Re: check if file exist

Post by stottle »

Glad you found the solution! Yes, python is very finicky about whitespace and tab's are not the same as spaces (even if they look the same with a given font in an editor). Only thing I would add is that EG uses spaces over tabs. I do my editing in the free Notepad++, which has a setting that replaces tab entries with 4 spaces. This allows you to use one key (tab) for indenting, but consistently uses spaces so your plugin is formatted like EG and all of the other plugins.

Good luck with your plugin development.

Brett
Post Reply