sbws.util package

Submodules

sbws.util.config module

Util functions to manage sbws configuration files.

sbws.util.config.configure_logging(args, conf)[source]
sbws.util.config.get_config(args)[source]

Get ConfigParser interpolating all configuration files.

sbws.util.config.validate_config(conf)[source]

Checks the given conf for bad values or bad combinations of values. If there’s something wrong, returns False and a list of error messages. Otherwise, return True and an empty list

sbws.util.filelock module

class sbws.util.filelock.DirectoryLock(dname)[source]

Bases: _FLock

Holds a lock on a file in dname so that other sbws processes/threads won’t try to read/write while we are reading/writing in this directory.

>>> with DirectoryLock(dname):
>>>     # do some reading/writing in dname
>>> # no longer have the lock

Note: The directory must already exist.

Parameters:

dname (str) – Name of directory for which we want to obtain a lock

class sbws.util.filelock.FileLock(fname)[source]

Bases: _FLock

Holds a lock on fname so that other sbws processes/threads won’t try to read/write while we are reading/writing this file.

>>> with FileLock(fname):
>>>     # do some reading/writing of fname
>>> # no longer have the lock
Parameters:

fname (str) – Name of the file for which we want to obtain a lock

sbws.util.loghandler module

Log handlers.

class sbws.util.loghandler.GzipTimedRotatingFileHandler(filename, when='h', interval=1, backupCount=0, encoding=None, delay=False, utc=False, atTime=None, errors=None)[source]

Bases: TimedRotatingFileHandler

Compress all dayly log files, except the current day one.

doRollover()[source]

do a rollover; in this case, a date/time stamp is appended to the filename when the rollover happens. However, you want the file to be named for the start of the interval, not the current time. If there is a backup count, then we have to get a list of matching filenames, sort them and remove the one with the oldest suffix.

sbws.util.parser module

sbws.util.parser.create_parser()[source]

sbws.util.state module

class sbws.util.state.State(fname)[source]

Bases: object

json wrapper to read a json file every time it gets a key and to write to the file every time a key is set.

When reading a key, use load_ignore_errors to ignore not valid json that might exits due to power interruption or similar. When writing a key, use atomic_dump to atomically write the json file and do not write invalid json dut to power interruption or similar.

>>> state = State('foo.state')
>>> # state == {}
>>> state['linux'] = True
>>> # 'foo.state' now exists on disk with the JSON for {'linux': True}
>>> # We read 'foo.state' from disk in order to get the most up-to-date
>>> #     state info. Pretend another process has updated 'linux' to be
>>> #     False
>>> state['linux']
>>> # returns False
>>> # Pretend another process has added the user's age to the state file.
>>> #     As before, we read the state file from disk for the most
>>> #     up-to-date info.
>>> state['age']
>>> # Returns 14
>>> # We now set their name. We read the state file first, set the option,
>>> #     and then write it out.
>>> state['name'] = 'John'
>>> # We can do many of the same things with a State object as with a dict
>>> for key in state: print(key)
>>> # Prints 'linux', 'age', and 'name'
count(k)[source]

Returns the length if the key value is a list or the sum of number if the key value is a list of list or the key value or None if the state doesn’t have the key.

get(key, d=None)[source]

Implements a dictionary get method reading and locking a json file.

sbws.util.stem module

sbws.util.stem.add_event_listener(controller, func, event)[source]
sbws.util.stem.attach_stream_to_circuit_listener(controller, circ_id, bwscanner_cc)[source]

Returns a function that should be given to add_event_listener(). It looks for newly created streams and attaches them to the given circ_id

sbws.util.stem.circuit_str(controller, circ_id)[source]
sbws.util.stem.get_socks_info(controller)[source]

Returns the first SocksPort Tor is configured to listen on, in the form of an (address, port) tuple

sbws.util.stem.init_controller(conf)[source]
sbws.util.stem.is_bootstrapped(c)[source]
sbws.util.stem.is_torrc_starting_point_set(tor_controller)[source]

Verify that the tor controller has the correct configuration.

When connecting to a tor controller that has not been launched by sbws, it should have been configured to work with sbws.

sbws.util.stem.launch_or_connect_to_tor(conf)[source]
sbws.util.stem.launch_tor(conf)[source]
sbws.util.stem.only_relays_with_bandwidth(relays, min_bw=None, max_bw=None)[source]

Given a list of relays, only return those that optionally have above min_bw and optionally have below max_bw, inclusively. If neither min_bw nor max_bw are given, essentially just returns the input list of relays.

sbws.util.stem.parse_user_torrc_config(torrc, torrc_text)[source]

Parse the user configuration torrc text call extra_lines to a dictionary suitable to use with stem and return a new torrc dictionary that merges that dictionary with the existing torrc. Example:

[tor]
extra_lines =
    Log debug file /tmp/tor-debug.log
    NumCPUs 1
sbws.util.stem.remove_event_listener(controller, func)[source]
sbws.util.stem.set_torrc_options_can_fail(controller)[source]

Set options that can fail, at runtime.

They can be set at launch, but since the may fail because they are not supported in some Tor versions, it’s easier to try one by one at runtime and ignore the ones that fail.

sbws.util.stem.set_torrc_runtime_options(controller)[source]

Set torrc options at runtime.

sbws.util.stem.set_torrc_starting_point(controller)[source]

Set the torrc starting point options.

Module contents