Differences

This shows you the differences between the selected revision and the current version of the page.

ideas:radioclient.py 2008/11/15 11:08 ideas:radioclient.py 2008/12/19 23:48 current
Line 11: Line 11:
import urllib, httplib, sys, time import urllib, httplib, sys, time
-old_last_line = "1" +def Tail(filepath, line_number, read_size=1024):
- + """
-def Tail(filepath, read_size=1024): + This function returns the last line of a file.
- """ + Args:
- This function returns the last line of a file. + filepath: path to file
- Args: + read_size:  data is read in chunks of this size (optional, default=1024)
-   filepath: path to file + Raises:
-   read_size:  data is read in chunks of this size (optional, default=1024) + IOError if file cannot be processed.
- Raises: +
-   IOError if file cannot be processed.+
From: http://manugarg.blogspot.com/2007/04/tailing-in-python.html From: http://manugarg.blogspot.com/2007/04/tailing-in-python.html
-  """ + """
- f = open(filepath, 'r')    # U is to open it with Universal newline support + f = open(filepath, 'r')    # U is to open it with Universal newline support
- offset = read_size + offset = read_size
- f.seek(0, 2) + f.seek(0, 2)
- file_size = f.tell() + file_size = f.tell()
- while 1: + while 1:
-   if file_size < offset: + if file_size < offset:
-     offset = file_size + offset = file_size
-   f.seek(-1*offset, 2) + f.seek(-1*offset, 2)
-   read_str = f.read(offset) + read_str = f.read(offset)
-   # Remove newline at the end + # Remove newline at the end
-   if read_str[offset - 1] == '\n': + if read_str[offset - 1] == '\n':
-     read_str = read_str[0:-1] + read_str = read_str[0:-1]
-   lines = read_str.split('\n') + lines = read_str.split('\n')
-   if len(lines) > 1:  # Got a line + if len(lines) > 1:  # Got a line
-     return lines[len(lines) - 1] + return lines[len(lines) - line_number]
-   if offset == file_size:  # Reached the beginning + if offset == file_size:  # Reached the beginning
-     return read_str + return read_str
-   offset += read_size + offset += read_size
- f.close() + f.close()
- +
 +old_last_line = "1"
cmd_arg = len(sys.argv) cmd_arg = len(sys.argv)
if cmd_arg > 2: if cmd_arg > 2:
Line 49: Line 48:
while 1: while 1:
- last_line = Tail(file_location)+ last_line = Tail(file_location, 2)
 + print last_line
if last_line != old_last_line: if last_line != old_last_line:
old_last_line = last_line old_last_line = last_line
Line 70: Line 70:
print last_line_field_count print last_line_field_count
print callsign_field_length print callsign_field_length
- if last_line_length < 100 and last_line_field_count >= 8 and callsign_field_length < 18 and field_too_big == 0:+ if last_line_length < 100 and last_line_field_count >= 8 and last_line_field_count < 14 and callsign_field_length < 30 and field_too_big == 0:
print user_identity + "," + last_line print user_identity + "," + last_line
#Uploads to server #Uploads to server
Line 87: Line 87:
#time.sleep(2) #time.sleep(2)
print "No new data" print "No new data"
- time.sleep(0.5)+ time.sleep(2)
else: else:
print "No arguments passed" print "No arguments passed"
print "radioclient.py $username $logfile" print "radioclient.py $username $logfile"
</code> </code>
 
ideas/radioclient.py.txt · Last modified: 2008/12/19 23:48 by jamescoxon
 
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki