#!/usr/bin/env python
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.

def firstLine(s):
    '''Returns the first line of any series of text with / without line breaks.'''
    return s.split('\n')[0]


def fuzzDice(filename):
    '''Returns the lines of the file, except for the one line containing DICE'''
    before = []
    after = []
    with open(filename, 'rb') as f:
        for line in f:
            if line.find("DICE") != -1:
                break
            before.append(line)
        for line in f:
            after.append(line)
    return [before, after]


def fuzzSplice(filename):
    '''Returns the lines of a file, minus the ones between the two lines containing SPLICE'''
    before = []
    after = []
    with open(filename, 'rb') as f:
        for line in f:
            before.append(line)
            if line.find("SPLICE") != -1:
                break
        for line in f:
            if line.find("SPLICE") != -1:
                after.append(line)
                break
        for line in f:
            after.append(line)
    return [before, after]


def linesWith(lines, searchFor):
    '''Returns the lines from an array that contain a given string'''
    matchingLines = []
    for line in lines:
        if line.find(searchFor) != -1:
            matchingLines.append(line)
    return matchingLines


def linesStartingWith(lines, searchFor):
    '''Returns the lines from an array that start with a given string'''
    matchingLines = []
    for line in lines:
        if line.startswith(searchFor):
            matchingLines.append(line)
    return matchingLines


def truncateMid(a, limitEachSide, insertIfTruncated):
    '''Returns a list with the middle portion removed, if it has more than limitEachSide*2 items.'''
    if len(a) <= limitEachSide + limitEachSide:
        return a
    return a[0:limitEachSide] + insertIfTruncated + a[-limitEachSide:]


def writeLinesToFile(lines, filename):
    '''Writes lines to a given filename.'''
    with open(filename, 'wb') as f:
        f.writelines(lines)

@@ -217,16 +217,17 @@
 /2.0/.%0A%0A
+%0A
 def firs
@@ -236,34 +236,33 @@
 ine(s):%0A    
-'''
+%22%22%22
 Return
-s
  the first l
@@ -314,19 +314,19 @@
  breaks.
-'''
+%22%22%22
 %0A    ret
@@ -372,34 +372,33 @@
 ename):%0A    
-'''
+%22%22%22
 Return
-s
  the lines o
@@ -448,19 +448,20 @@
 ing DICE
-'''
+.%22%22%22
 %0A    bef
@@ -746,34 +746,33 @@
 ename):%0A    
-'''
+%22%22%22
 Return
-s
  the lines o
@@ -835,19 +835,20 @@
 g SPLICE
-'''
+.%22%22%22
 %0A    bef
@@ -1264,34 +1264,33 @@
 chFor):%0A    
-'''
+%22%22%22
 Return
-s
  the lines f
@@ -1321,35 +1321,36 @@
 n a given string
-'''
+.%22%22%22
 %0A    matchingLin
@@ -1530,26 +1530,25 @@
 r):%0A    
-'''
+%22%22%22
 Return
-s
  the lin
@@ -1594,19 +1594,20 @@
 n string
-'''
+.%22%22%22
 %0A    mat
@@ -1812,18 +1812,17 @@
     
-'''
+%22%22%22
 Return
-s
  a l
@@ -1900,19 +1900,19 @@
 2 items.
-'''
+%22%22%22
 %0A    if 
@@ -2089,17 +2089,16 @@
     
-'''
+%22%22%22
 Write
-s
  lin
@@ -2124,11 +2124,11 @@
 ame.
-'''
+%22%22%22
 %0A   

Make linter happier with fileManipulation.