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.

Reading Text from a text file

If you have a question or need help, this is the place to be.
allalone747
Experienced User
Posts: 81
Joined: Sun May 03, 2015 2:17 pm

Re: Reading Text from a text file

Post by allalone747 »

Hello
Yes I change it back to c: it's , exactly as you wrote it ,
krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

Re: Reading Text from a text file

Post by krambriw »

@allalone747

This thread is maybe a good example for creating a tutorial on 'How to read text from a text file' ? You may start now...
User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: Reading Text from a text file

Post by Pako »

allalone747 wrote:When i do a (Execute item) i hear Speech Speak None. i attached a copy of my XML flie. please can some one have a look see what i am going wrong thanks a lot ,
allalone747 wrote:Hello
Yes I change it back to c: it's , exactly as you wrote it ,
I still do not see the information "it works" ...

Edit: ... or (of course it is also possible): "yet it does not work" (in this case, I would expect a more detailed report).

Pako
You know flattr ? You can Image
allalone747
Experienced User
Posts: 81
Joined: Sun May 03, 2015 2:17 pm

Re: Reading Text from a text file

Post by allalone747 »

Hi Krambriw
It would interested if i get this project to work, with some input from yourself that would good.
I would certainly create a Tutorial on the end product.

Thanks
allalone747
Experienced User
Posts: 81
Joined: Sun May 03, 2015 2:17 pm

Re: Reading Text from a text file

Post by allalone747 »

Hi Pako
Good evening, First may i just say that you have already done more than expected,i really appreciate the assistance i this project i am aiming to get working, so far i must tell you that it still only say None when i execute the second macro in this project,
My Eventghost keeps crashing (not Responding) recently and i am using the Oct 24 2015 issue,
running Windows 10
Please find my XML file attach
Thanks again
Attachments
myproject 2016.xml
(2.89 KiB) Downloaded 90 times
User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: Reading Text from a text file

Post by Pako »

I just do not understand.
I sent you an example of a working configuration.
Why am I doing this when you redo it completely differently?
This is an example functional configuration:
Diary.png
Diary.png (9.77 KiB) Viewed 4471 times
And this is your configuration:
DiaryBadExample.png
DiaryBadExample.png (14.94 KiB) Viewed 4471 times
Do you see the difference?
allalone747 wrote:My Eventghost keeps crashing (not Responding) recently and i am using the Oct 24 2015 issue,
running Windows 10
This sentence I have not understood.

Pako
Attachments
myproject 2016-fixed.xml
(2.1 KiB) Downloaded 91 times
You know flattr ? You can Image
allalone747
Experienced User
Posts: 81
Joined: Sun May 03, 2015 2:17 pm

Re: Reading Text from a text file

Post by allalone747 »

Hello Pako
Good evening, my apologies to you, I am now using your fixed version of the xml file, what i can now hear is the sound saying NONE.
This is my system setup
Platform is Windows 10.pro.
date format is dd/MM/yyyy
Time format is HH:MM
First day of week is Monday

Thank you for your help
Cheers
User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: Reading Text from a text file

Post by Pako »

In the current version, format of time can be completely arbitrary.
Time is not parsed.
It reads the entire file, valid for the current day.
The file is read each time, when you start EventGhost (it makes the event Main.OnInit).
It was your request.

If you want the file read again, you must run the macro "Python script Diary" ( never the macro "Speech: Speak: {eg.event.payload}" !!!).
If it does not work for you, unfortunately I can not see what you're doing wrong.

Note:
If a file, valid for current day, is found, then it is listed after start the macro "Python script Diary" in EventGhost log.
For example, today:

Code: Select all

19:52:54         File valid for today: C:\2016 Diary\January\Week 2\Sunday.txt
Pako
You know flattr ? You can Image
allalone747
Experienced User
Posts: 81
Joined: Sun May 03, 2015 2:17 pm

Re: Reading Text from a text file

Post by allalone747 »

Hello Pako
Thanks a million, Brilliant it works perfectly,You have created a beautiful Program
Everyone out there this is your Dairy.
One more thing I was wandering,could this be run on a hourly bases, could schedule ghost be added to control this loop?
Can it be done :)
Thanks
Cheers
User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: Reading Text from a text file

Post by Pako »

allalone747 wrote:One more thing I was wandering,could this be run on a hourly bases, could schedule ghost be added to control this loop?
Of course, you can add any other event (ie from SchedulGhost as well) to the macro "Python script Diary".
BTW: why is it suddenly works when the program is still the same?

Note:
I already have a functioning new version including parsing time. I have to write some more information to it.

Pako
You know flattr ? You can Image
allalone747
Experienced User
Posts: 81
Joined: Sun May 03, 2015 2:17 pm

Re: Reading Text from a text file

Post by allalone747 »

Hello Pako
The fault was my doing , it was simple nothing was in the Sunday text file that's why It was reporting (none )
I am very pleased that the idea of a Diary did make sense to you ;
I would be very great full if you send me a copy of your new version .
Thank you
User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: Reading Text from a text file

Post by Pako »

Here is an improved version of the script.

Code: Select all

diary_root = "C:"
code_page = eg.systemEncoding

from datetime import date as date, datetime as dt
from calendar import setfirstweekday, monthcalendar
from os.path import isfile
import codecs
import re

WEEK = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
YEAR = (
    "January",
    "February",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November",
    "December"
)
ptrn = '((?:(?:[0-1][0-9])|(?:[2][0-3])|(?:[0-9])):(?:[0-5][0-9])(?::[0-5][0-9])?(?:\\s?(?:am|pm))?)'
rg = re.compile(ptrn,re.IGNORECASE|re.DOTALL)
am = {'12':'00','01':'01','02':'02','03':'03','04':'04','05':'05','06':'06',
	      '07':'07','08':'08','09':'09','10':'10','11':'11'}
pm = {'12':'12','01':'13','02':'14','03':'15','04':'16','05':'17','06':'18',
	      '07':'19','08':'20','09':'21','10':'22','11':'23'}
def test(txt):
    m = rg.search(txt)
    if m:
        time1 = m.group(1)
    else:
        return (True, "No timestamp found !")
    t_lower = time1.strip().lower()
    a = time1.split(':')
    res = ""
    hr = a[0].zfill(2)
    if t_lower.endswith("am"):
        if hr in am:
            res = am[hr] + ":" + a[1][:2]
        else:
            return (True, time1 + " (wrong format)") # When an error occurs, it is preferable to return True
    elif t_lower.endswith("pm"):
        if hr in pm:
            res = pm[hr] + ":" + a[1][:2]
        else:
            return (True, time1 + " (wrong format)") # When an error occurs, it is preferable to return True
    if res == "":
        #res = time1
        return (True, time1 + " (wrong format)")     # When an error occurs, it is preferable to return True
    if len(a) == 3:
        res += ":" + a[2][:2]
    else:
        res += ":00"
    now = dt.now()
    ts = dt.now()
    slots = res.split(":")
    ts = ts.replace(hour = int(slots[0]),minute = int(slots[1]),second = int(slots[2]))
    return (now < ts, ts)

today = date.today()
day  = today.day
wd = today.isoweekday() - 1
setfirstweekday(0)
month = monthcalendar(today.year, today.month)
week = 1 + [w[wd] for w in month].index(day)
today_file = r"%s\%i Diary\%s\Week %i\%s.txt" % (diary_root, today.year, YEAR[today.month-1], week, WEEK[wd])

if not isfile(today_file):
    eg.PrintError("No file (valid for today) found !")
    eg.Exit()
eg.PrintNotice("File valid for today: " + today_file)

input = codecs.open(today_file, 'r', code_page, 'ignore')
data = input.readlines()
input.close()
speakData = ""
for line in data:
    flag, ts = test(line)
    if flag:
        print "Upcoming event: ",str(ts)
        speakData += line
    else:
        print "Missed event:   ",str(ts)
if speakData:
    eg.TriggerEvent("Speak", prefix = "Diary", payload = speakData)
It works as follows:
1) Find the file that is valid for the current day
2) Each line of the file is treated as a separate event
3) Tries to find the timestamp in format HH:MM P or HH:MM:SS P or HH:MMP or HH:MM:SSP (P = am or pm or AM or PM)
4) If the timestamp is smaller than the current time, the entire line is ignored
5) If the timestamp is greater than the current time, or timestamp is not the correct format or timestamp is missing, the line is sent to the plugin Speech.

Pako
You know flattr ? You can Image
allalone747
Experienced User
Posts: 81
Joined: Sun May 03, 2015 2:17 pm

Re: Reading Text from a text file

Post by allalone747 »

Hello Pako
Good evening,
Thanks again
What i find happening with both, your first script, and this latest script.
When I open ÔǬC:\2016 Diary\January\week 2\Monday.txt file, It read the content,i then close eventghost and did a re-start it would repeat the text again.
I then delete all the text from the monday.txt file, then re-type different text and the save it,I then close Evenyghost. then i did a restart, the text which i deleted was read again , the new text was not read.
I did a computer restart and i am getting the same result
Question Is the Text that i delete store in a log.

I also found that if three separate lines are entered in the text file ,it would only read from the first two lines.
I hope you understand.
Cheers
User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: Reading Text from a text file

Post by Pako »

allalone747 wrote:When I open ÔǬC:\2016 Diary\January\week 2\Monday.txt file, It read the content,i then close eventghost and did a re-start it would repeat the text again.
I then delete all the text from the monday.txt file, then re-type different text and the save it,I then close Evenyghost. then i did a restart, the text which i deleted was read again , the new text was not read.
I did a computer restart and i am getting the same result
I believe, PEBCAK (Problem Exists Between Chair And Keyboard) :) .
This week is the third in 2016.

Pako
You know flattr ? You can Image
allalone747
Experienced User
Posts: 81
Joined: Sun May 03, 2015 2:17 pm

Re: Reading Text from a text file

Post by allalone747 »

Hello Pako
A very good day to you, I am afraid to say that this week is not week three, it's week two
Please have a look at the web page in text that i attached
It is reading from week three, but we are still in week two.
Thanks
Cheers
Attachments
week with numbers.txt
(2.26 KiB) Downloaded 88 times
Post Reply