Safe Haskell | Safe-Infered |
---|
Utility functions.
- debug :: Show a => a -> a
- debugFn :: Show b => (a -> b) -> a -> a
- debugXy :: Show a => a -> b -> b
- applyIf :: Bool -> (a -> a) -> a -> a
- commaJoin :: [String] -> String
- sepSplit :: Eq a => a -> [a] -> [[a]]
- findFirst :: (Ord a, Enum a) => a -> Set a -> a
- plural :: Int -> String -> String -> String
- ensureQuoted :: String -> String
- threadDelaySeconds :: Int -> IO ()
- divideList :: [a] -> ([a], [a])
- balancedSum :: Num a => [a] -> a
- stdDev :: [Double] -> Double
- if' :: Bool -> a -> a -> a
- parseChoices :: Monad m => String -> String -> [(a, String)] -> m a
- tryRead :: (Monad m, Read a) => String -> String -> m a
- readMaybe :: Read a => String -> Maybe a
- formatTable :: [[String]] -> [Bool] -> [[String]]
- printTable :: String -> [String] -> [[String]] -> [Bool] -> String
- parseUnitValue :: Monad m => Bool -> String -> m Rational
- parseUnitEx :: (Monad m, Integral a, Read a) => Bool -> String -> m a
- parseUnit :: (Monad m, Integral a, Read a) => String -> m a
- parseUnitAssumeBinary :: (Monad m, Integral a, Read a) => String -> m a
- exitIfBad :: String -> Result a -> IO a
- exitErr :: String -> IO a
- exitWhen :: Bool -> String -> IO ()
- exitUnless :: Bool -> String -> IO ()
- logWarningIfBad :: String -> a -> Result a -> IO a
- logAndBad :: String -> IO (Result a)
- tryAndLogIOError :: IO a -> String -> (a -> Result b) -> IO (Result b)
- withDefaultOnIOError :: a -> IO a -> IO a
- warn :: String -> IO ()
- extractKey :: [Either Integer String] -> String -> ([Either Integer String], String)
- niceSort :: [String] -> [String]
- niceSortKey :: (a -> String) -> [a] -> [a]
- rStripSpace :: String -> String
- newUUID :: IO String
- uuidCheckParser :: Parser ()
- isUUID :: String -> Bool
- getCurrentTime :: IO Integer
- getCurrentTimeUSec :: IO Integer
- clockTimeToString :: ClockTime -> String
- clockTimeToCTime :: ClockTime -> EpochTime
- clockTimeToUSec :: ClockTime -> Integer
- cTimeToClockTime :: EpochTime -> ClockTime
- diffClockTimes :: ClockTime -> ClockTime -> TimeDiff
- chompPrefix :: String -> String -> Maybe String
- wrap :: Int -> String -> [String]
- trim :: String -> String
- defaultHead :: a -> [a] -> a
- exitIfEmpty :: String -> [a] -> IO a
- monadicThe :: (Eq a, Monad m) => String -> [a] -> m a
- splitEithers :: [Either a b] -> ([a], [b], [Bool])
- recombineEithers :: (Show a, Show b) => [a] -> [b] -> [Bool] -> Result [Either a b]
- resolveAddrHints :: Maybe AddrInfo
- resolveAddr :: Int -> String -> IO (Result (Family, SockAddr))
- setOwnerAndGroupFromNames :: FilePath -> GanetiDaemon -> GanetiGroup -> IO ()
- setOwnerWGroupR :: FilePath -> IO ()
- formatOrdinal :: (Integral a, Show a) => a -> String
- lockFile :: FilePath -> IO (Result Fd)
- type FStat = (EpochTime, FileID, FileOffset)
- nullFStat :: FStat
- buildFileStatus :: FileStatus -> FStat
- getFStat :: FilePath -> IO FStat
- getFStatSafe :: FilePath -> IO FStat
- needsReload :: FStat -> FilePath -> IO (Maybe FStat)
- watchFileEx :: Eq b => Integer -> b -> IORef b -> (a -> Bool) -> IO a -> IO a
- watchFileBy :: FilePath -> Int -> (a -> Bool) -> IO a -> IO a
- watchFile :: Eq a => FilePath -> Int -> a -> IO a -> IO a
- data FilePermissions = FilePermissions {
- fpOwner :: Maybe GanetiDaemon
- fpGroup :: Maybe GanetiGroup
- fpPermissions :: FileMode
- ensurePermissions :: FilePath -> FilePermissions -> IO (Result ())
- safeRenameFile :: FilePermissions -> FilePath -> FilePath -> IO (Result ())
- ordNub :: Ord a => [a] -> [a]
- isSubsequenceOf :: Eq a => [a] -> [a] -> Bool
- maxBy :: (a -> a -> Ordering) -> a -> a -> a
- monotoneFind :: ([a] -> Int) -> (a -> Bool) -> [a] -> Maybe a
- iterateJust :: (a -> Maybe a) -> a -> [a]
Debug functions
debugFn :: Show b => (a -> b) -> a -> aSource
Displays a modified form of the second parameter before returning it.
Miscellaneous
applyIf :: Bool -> (a -> a) -> a -> aSource
Apply the function if condition holds, otherwise use default value.
findFirst :: (Ord a, Enum a) => a -> Set a -> aSource
Finds the first unused element in a set starting from a given base.
ensureQuoted :: String -> StringSource
Ensure a value is quoted if needed.
threadDelaySeconds :: Int -> IO ()Source
Delay a thread for several seconds.
divideList :: [a] -> ([a], [a])Source
Split a list into two lists of approximately the same length.
Mathematical functions
balancedSum :: Num a => [a] -> aSource
Compute the sum of a list of numbers, all about the same value, and do so in a balanced way to avoid adding numbers of too different values (and thus too bad inaccuracies).
Logical functions
:: Bool | condition |
-> a | "then" result |
-> a | "else" result |
-> a | "then" or else result depending on the condition |
"if" as a function, rather than as syntactic sugar.
Parsing utility functions
parseChoices :: Monad m => String -> String -> [(a, String)] -> m aSource
tryRead :: (Monad m, Read a) => String -> String -> m aSource
Safe read
function returning data encapsulated in a Result.
readMaybe :: Read a => String -> Maybe aSource
Parse a string using the Read
instance.
Succeeds if there is exactly one valid result.
Backport from Text.Read introduced in base-4.6.0.0
formatTable :: [[String]] -> [Bool] -> [[String]]Source
Format a table of strings to maintain consistent length.
printTable :: String -> [String] -> [[String]] -> [Bool] -> StringSource
Constructs a printable table from given header and rows
parseUnitValue :: Monad m => Bool -> String -> m RationalSource
parseUnitEx :: (Monad m, Integral a, Read a) => Bool -> String -> m aSource
parseUnit :: (Monad m, Integral a, Read a) => String -> m aSource
Tries to extract number and scale from the given string.
Input must be in the format NUMBER+ SPACE* [UNIT]. If no unit is specified, it defaults to MiB. Return value is always an integral value in MiB.
parseUnitAssumeBinary :: (Monad m, Integral a, Read a) => String -> m aSource
Tries to extract a number and scale from a given string, taking all kilos to be binary.
exitWhen :: Bool -> String -> IO ()Source
Exits with an error message if the given boolean condition if true.
exitUnless :: Bool -> String -> IO ()Source
Exits with an error message unless the given boolean condition
if true, the opposite of exitWhen
.
logWarningIfBad :: String -> a -> Result a -> IO aSource
tryAndLogIOError :: IO a -> String -> (a -> Result b) -> IO (Result b)Source
Try an IO interaction, log errors and unfold as a Result
.
withDefaultOnIOError :: a -> IO a -> IO aSource
Try an IO interaction and return a default value if the interaction throws an IOError.
extractKey :: [Either Integer String] -> String -> ([Either Integer String], String)Source
niceSort :: [String] -> [String]Source
Sort a list of strings based on digit and non-digit groupings.
Given a list of names [
this function
will sort the list in the logical order a1
, a10
, a11
, a2
][
.
a1
, a2
, a10
, a11
]
The sort algorithm breaks each name in groups of either only-digits or no-digits, and sorts based on each group.
Internally, this is not implemented via regexes (like the Python
version), but via actual splitting of the string in sequences of
either digits or everything else, and converting the digit sequences
in Left Integer and the non-digit ones in Right String, at which
point sorting becomes trivial due to the built-in Either
ordering;
we only need one extra step of dropping the key at the end.
niceSortKey :: (a -> String) -> [a] -> [a]Source
Key-version of niceSort
. We use sortBy
and compare
since we don't want to add an ordering constraint on the a type,
hence the need to only compare the first element of the (key, a)
tuple.
on
fst
rStripSpace :: String -> StringSource
Strip space characthers (including newline). As this is expensive, should only be run on small strings.
Returns a random UUID. This is a Linux-specific method as it uses the /proc filesystem.
uuidCheckParser :: Parser ()Source
isUUID :: String -> BoolSource
Checks if the string is a valid UUID as in Ganeti.Constants.uuidRegex.
getCurrentTime :: IO IntegerSource
Returns the current time as an Integer
representing the number
of seconds from the Unix epoch.
getCurrentTimeUSec :: IO IntegerSource
Returns the current time as an Integer
representing the number
of microseconds from the Unix epoch (hence the need for Integer
).
clockTimeToString :: ClockTime -> StringSource
Convert a ClockTime into a (seconds-only) timestamp.
clockTimeToCTime :: ClockTime -> EpochTimeSource
Convert a ClockTime into a (seconds-only) EpochTime
(AKA time_t
).
clockTimeToUSec :: ClockTime -> IntegerSource
Convert a ClockTime the number of microseconds since the epoch.
cTimeToClockTime :: EpochTime -> ClockTimeSource
Convert a ClockTime into a (seconds-only) EpochTime
(AKA time_t
).
diffClockTimes :: ClockTime -> ClockTime -> TimeDiffSource
A version of diffClockTimes
that works around ghc bug #2519.
chompPrefix :: String -> String -> Maybe StringSource
Strip a prefix from a string, allowing the last character of the prefix (which is assumed to be a separator) to be absent from the string if the string terminates there.
>>> chompPrefix "foo:bar:" "a:b:c" Nothing
>>> chompPrefix "foo:bar:" "foo:bar:baz" Just "baz"
>>> chompPrefix "foo:bar:" "foo:bar:" Just ""
>>> chompPrefix "foo:bar:" "foo:bar" Just ""
>>> chompPrefix "foo:bar:" "foo:barbaz" Nothing
:: Int | maxWidth |
-> String | string that needs wrapping |
-> [String] | string "broken" in lines |
Breaks a string in lines with length <= maxWidth.
NOTE: The split is OK if:
- It doesn't break a word, i.e. the next line begins with space
(
isSpace . head $ rest
) or the current line ends with space (null revExtra
); - It breaks a very big word that doesn't fit anyway (
null revLine
).
defaultHead :: a -> [a] -> aSource
A safer head version, with a default value.
exitIfEmpty :: String -> [a] -> IO aSource
A head
version in the I/O monad, for validating parameters
without which we cannot continue.
monadicThe :: (Eq a, Monad m) => String -> [a] -> m aSource
Obtain the unique element of a list in an arbitrary monad.
splitEithers :: [Either a b] -> ([a], [b], [Bool])Source
Split an Either
list into two separate lists (containing the
Left
and Right
elements, plus a "trail" list that allows
recombination later.
This is splitter; for recombination, look at recombineEithers
.
The sum of "left" and "right" lists should be equal to the
original list length, and the trail list should be the same length
as well. The entries in the resulting lists are reversed in
comparison with the original list.
recombineEithers :: (Show a, Show b) => [a] -> [b] -> [Bool] -> Result [Either a b]Source
Recombines two "left" and "right" lists using a "trail"
list into a single Either
list.
This is the counterpart to splitEithers
. It does the opposite
transformation, and the output list will be the reverse of the
input lists. Since splitEithers
also reverses the lists, calling
these together will result in the original list.
Mismatches in the structure of the lists (e.g. inconsistent
lengths) are represented via Bad
; normally this function should
not fail, if lists are passed as generated by splitEithers
.
resolveAddrHints :: Maybe AddrInfoSource
resolveAddr :: Int -> String -> IO (Result (Family, SockAddr))Source
Resolves a numeric address.
setOwnerAndGroupFromNames :: FilePath -> GanetiDaemon -> GanetiGroup -> IO ()Source
Set the owner and the group of a file (given as names, not numeric id).
setOwnerWGroupR :: FilePath -> IO ()Source
Resets permissions so that the owner can read/write and the group only read. All other permissions are cleared.
formatOrdinal :: (Integral a, Show a) => a -> StringSource
Formats an integral number, appending a suffix.
lockFile :: FilePath -> IO (Result Fd)Source
Attempt, in a non-blocking way, to obtain a lock on a given file; report back success. Returns the file descriptor so that the lock can be released by closing
buildFileStatus :: FileStatus -> FStatSource
getFStat :: FilePath -> IO FStatSource
Wrapper over buildFileStatus
. This reads the data from the
filesystem and then builds our cache structure.
getFStatSafe :: FilePath -> IO FStatSource
Safe version of getFStat
, that ignores IOErrors.
needsReload :: FStat -> FilePath -> IO (Maybe FStat)Source
Check if the file needs reloading
watchFileEx :: Eq b => Integer -> b -> IORef b -> (a -> Bool) -> IO a -> IO aSource
watchFileBy :: FilePath -> Int -> (a -> Bool) -> IO a -> IO aSource
Within the given timeout (in seconds), wait for for the output of the given method to satisfy a given predicate and return the new value; make use of the promise that the method will only change its value, if the given file changes on disk. If the file does not exist on disk, return immediately.
watchFile :: Eq a => FilePath -> Int -> a -> IO a -> IO aSource
Within the given timeout (in seconds), wait for for the output of the given method to change and return the new value; make use of the promise that the method will only change its value, if the given file changes on disk. If the file does not exist on disk, return immediately.
data FilePermissions Source
Type describing ownership and permissions of newly generated directories and files. All parameters are optional, with nothing meaning that the default value should be left untouched.
FilePermissions | |
|
ensurePermissions :: FilePath -> FilePermissions -> IO (Result ())Source
Ensure that a given file or directory has the permissions, and possibly ownerships, as required.
safeRenameFile :: FilePermissions -> FilePath -> FilePath -> IO (Result ())Source
Safely rename a file, creating the target directory, if needed.
isSubsequenceOf :: Eq a => [a] -> [a] -> BoolSource
`isSubsequenceOf a b`: Checks if a is a subsequence of b.
maxBy :: (a -> a -> Ordering) -> a -> a -> aSource
Compute the maximum of two elements by a given order.
As opposed to using maximumBy
, is function is guaranteed
to be total, as the signature enforces a non-empty list of
arguments.
monotoneFind :: ([a] -> Int) -> (a -> Bool) -> [a] -> Maybe aSource
Given a predicate that is monotone on a list, find the first list entry where it holds, if any. Use the monotonicity property to evaluate the property at as few places as possible, guided by the heuristics provided.
iterateJust :: (a -> Maybe a) -> a -> [a]Source
Iterate a funtion as long as it returns Just values, collecting all the Justs that where obtained.