API¶
General API for working with the Student Robotics tools.
Bill of Materials¶
Modules for working with bills of materials.
Routines for extracting BOMs from schematics.
-
class
sr.tools.bom.bom.BoardBom(db, fname, name)[source]¶ Bases:
sr.tools.bom.bom.BomBOM object. Groups parts with the same asset code into PartGroups. Dictionary keys are asset codes.
Parameters: - db – A parts DB instance.
- fname – The schematic to load from.
- name – The name to give the schematic.
-
class
sr.tools.bom.bom.Bom[source]¶ Bases:
dictA bill of materials.
-
get_price()[source]¶ Get total price of all the items.
Returns: The total price. Return type: decimal.Decimal
-
-
class
sr.tools.bom.bom.MultiBoardBom(db)[source]¶ Bases:
sr.tools.bom.bom.BomA bill of materials with multiple boards.
Parameters: db – A parts DB instance. -
add_boards(board, num)[source]¶ Add boards to the collection.
Parameters: - board (BoardCom) – The board to add.
- num (int) – The number of times to add it.
-
load_boards_args(args, allow_multipliers=True)[source]¶ Load the BOM from board arguments, which is a list of arguments where each item is a string either starting with a ‘-’ and then a number, meaning it is a multiplier, or just a string which contains the schematic.
Parameters: - args – The board arguments.
- allow_multipliers (bool) – Whether or not to allow multipliers in the board arguments.
-
-
class
sr.tools.bom.bom.PartGroup(part, name='', designators=[])[source]¶ Bases:
listA set of parts. One might call this a “BOM line”.
Parameters: -
get_price()[source]¶ Returns the price of the group.
Returns: The price. Return type: decimal.Decimal
-
Routines for grabbing a page, with caching.
-
sr.tools.bom.cachedfetch.grab_url_cached(url)[source]¶ Download a possibly cached URL.
Returns: The contents of the page.
Routines for scraping data about parts from digikey.
-
class
sr.tools.bom.digikey.Item(partNumber)[source]¶ Bases:
sr.tools.bom.distpart.DistItemRepresents a Digikey item.
Parameters: partNumber – The number of the part.
Superclass for distributor items.
-
class
sr.tools.bom.distpart.DistItem(part_number)[source]¶ Bases:
objectA distributor item.
Parameters: part_number – The part number of the item.
-
exception
sr.tools.bom.distpart.UnsupportedFormat[source]¶ Bases:
ExceptionThe page returned by the distributor was of an unsupported format
Routines for scraping data about parts from Farnell.
-
class
sr.tools.bom.farnell.Item(part_number)[source]¶ Bases:
sr.tools.bom.distpart.DistItemA Farnell item.
Parameters: part_number – The number of the part.
-
class
sr.tools.bom.geda.GSchem(fname)[source]¶ Bases:
dictReads in gEDA file.
Parameters: fname (str) – The filename of the schematic.
-
class
sr.tools.bom.geda.PCB(fname)[source]¶ Bases:
objectRead in a gEDA PCB file.
Parameters: fname (str) – The filename of the PCB file.
Routines for scraping data about parts from Mouser.
-
class
sr.tools.bom.mouser.Item(part_number)[source]¶ Bases:
sr.tools.bom.distpart.DistItemAn item sold by Mouser.
Parameters: part_number – The number of the part.
Student Robotics parts database access library.
-
class
sr.tools.bom.parts_db.Db(file)[source]¶ Bases:
dictA parts database.
Parameters: file (file-like) – A file-like object to read the parts DB from.
-
class
sr.tools.bom.parts_db.Part(d)[source]¶ Bases:
dictRepresents a part.
Parameters: d (dict) – A part dictionary from the database.
Routines for scraping data about parts from RS.
-
class
sr.tools.bom.rs.Item(part_number)[source]¶ Bases:
sr.tools.bom.distpart.DistItemAn item sold by RS.
Parameters: part_number – The number of the part.
Routines for extracting information from schematics.
-
sr.tools.bom.schem.open_schem(fname)[source]¶ Open a schematics file.
Returns: The parsed schematic as an object. Raises: ValueError – If the file is not a gschem file.
-
class
sr.tools.bom.threadpool.ThreadPool(num_threads)[source]¶ Bases:
objectPool of threads consuming tasks from a queue.
-
class
sr.tools.bom.threadpool.Worker(tasks)[source]¶ Bases:
threading.ThreadThread executing tasks from a given tasks queue.
Budget¶
Library for accessing the budget files.
-
class
sr.tools.budget.AddedItem(a)[source]¶ Bases:
objectAn item that has been added to the budget.
-
class
sr.tools.budget.BudgetConfig(root)[source]¶ Bases:
objectA class representing a budget config.
Parameters: root (str) – The root path to the budget.
-
class
sr.tools.budget.BudgetItem(name, fname, conf)[source]¶ Bases:
objectA budget item.
Parameters:
-
class
sr.tools.budget.BudgetTree(name)[source]¶ Bases:
objectContainer for the BudgetItems and BudgetTrees below a certain point.
Parameters: name (str) – The name of the tree. -
add_child(child)[source]¶ Add a child to the tree.
Parameters: child ( BudgetTreeorBudgetItem.) – The child to add.Raises: ValueError – If the child is not a valid type.
-
draw(fd=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>, space=' ', prefix='')[source]¶ Draw a text-representation of the tree.
Parameters: - fd – Where to print the result.
- space – The string representing a space.
- prefix – The prefix for each line in the tree.
-
path(path)[source]¶ Get the object at the given path relative to this one.
Returns: The object at the path. Raises: InvalidPath – If the path is invalid.
-
total()[source]¶ Sum all children.
Returns: The total cost of all the children. Return type: decimal.Decimal
-
-
class
sr.tools.budget.ChangedItem(a, b)[source]¶ Bases:
objectAn item in the budget that has been changed.
-
exception
sr.tools.budget.InvalidPath[source]¶ Bases:
ExceptionAn exception representing an invalid budget path.
-
exception
sr.tools.budget.NoBudgetConfig[source]¶ Bases:
ExceptionAn exception representing that no config file for the budget has been found.
-
exception
sr.tools.budget.NotBudgetRepo[source]¶ Bases:
ExceptionAn exception raised if the repo is not a budget.
-
class
sr.tools.budget.RemovedItem(a)[source]¶ Bases:
objectAn item that has been removed from the budget.
-
class
sr.tools.budget.TmpBudgetExport(root, rev)[source]¶ Bases:
objectA class for temporarily exporting a budget.
Parameters: - root – The root path to the budget.
- rev – The revision to export.
-
sr.tools.budget.changes_to_tree(changes)[source]¶ Convert a list of changes into a tree.
Parameters: changes – A list of changes in the budget. Raises: ValueError – If the list of changes contains non-change objects. Returns: A new budget tree. Return type: BudgetTree
-
sr.tools.budget.dict_constructor(loader, node)[source]¶ Constructor for libyaml to use ordered dicts instead of dicts.
-
sr.tools.budget.diff_trees(a, b)[source]¶ Get the difference betwen two budget trees.
Returns: A sorted list of changes containing either AddedItem,RemovedItemorChangedItemobjects.Return type: list of AddedItem,RemovedItemandChangedItem
-
sr.tools.budget.find_root(path=None)[source]¶ Find the root directory of the budget repository.
Checks that the repository is budget.git too.
Parameters: path – if provided is a path within the budget.git repository (defaults to working directory)
-
sr.tools.budget.load_budget(root)[source]¶ Load the budget from a budget root.
Parameters: root – The root path to the budget.
-
sr.tools.budget.load_budget_rev(root, rev)[source]¶ Load a specific revision of the budget.
Parameters:
Configuration¶
Configuration for the tools.
-
class
sr.tools.config.Config[source]¶ Bases:
dictConfiguration reader for the tools.
-
get_password(*args, **kw)[source]¶ Get the user’s password.
Return the first non-None argument. If all of the arguments are None, and the user has enabled the use of the keyring, attempts to look-up password in the keyring. If that fails, resort to prompting the user for their password.
For keyring lookups, a username is required. If the caller does not want to use the username specified by the config files for this, then the desired username can be fed in through the ‘user’ keyword argument. This argument is ignored if its value is None.
-
Environment¶
A set of utilities that provide cross-platform support for a few basic features.
-
sr.tools.environment.get_cache_dir(*components)[source]¶ Return the cache directory for a particular component of the tools.
If the environment variable
SR_CACHE_DIRis set, that takes precedence as the root of the cache directory.This function will endeavour to create the cache directory for you.
Parameters: components (str) – Used to separate out different parts of cache. Generally these are combined with the platform path separator path. Returns: The path to the cache directory. Return type: str
-
sr.tools.environment.get_config_filename()[source]¶ Get the filename for the configuration file.
Returns: The filename to the config. Return type: str
-
sr.tools.environment.get_terminal_size()[source]¶ Get the size of the terminal window the user is working in.
The code for this is based on: https://gist.github.com/jtriley/1108174 .
Returns: A tuple containing the width and height as its elements respectively. Return type: (width, height) tuple
Inventory¶
Modules for working with the Student Robotics inventory system.
A set of functions for dealing with asset codes in the inventory.
Asset codes are constructed from part numbers and user numbers.
-
sr.tools.inventory.assetcode.code_to_num(asset_code)[source]¶ Convert an alphanumeric asset code to a user/part number combo.
Parameters: asset_code (str) – The asset code to convert. Returns: A tuple consisting of the user and part number. Return type: pair of ints
-
sr.tools.inventory.assetcode.is_valid(asset_code)[source]¶ Check if an asset code is valid.
Parameters: asset_code (str) – The asset code to check. Returns: True if valid, else False. Return type: bool
-
sr.tools.inventory.assetcode.normalise(asset_code)[source]¶ Normalise the given asset code to one that is compatible with the inventory API. Generally this just involves removing the ‘sr’ from the front and making the result all in uppercase.
Parameters: asset_code (str) – The asset code to normalise. Returns: A normalised part code. Return type: str
-
sr.tools.inventory.assetcode.num_to_code(user_number, part_number)[source]¶ Convert a user/part number combo to an alphanumeric asset code.
Parameters: - int (part_number) – The user number.
- int – The part number.
Returns: An asset code string.
Return type:
A set of classes and functions for working with The Inventory.
-
exception
sr.tools.inventory.inventory.InvalidFileError(path, comment)[source]¶ Bases:
ValueErrorRaised when an invalid file is found in the inventory.
Parameters: path – The path to the file, relative to the inventory. Also accessible as the pathattribute of this class.
-
class
sr.tools.inventory.inventory.Inventory(root_path)[source]¶ Bases:
objectAn inventory.
Parameters: root_path (str) – The root path to the inventory. -
asset_codes¶ Get all the asset codes.
Returns: An iteration over the codes.
-
current_user_number¶ Get the user ID of the currently configured Git user.
Returns: The current user number. Raises: KeyError – If the user doesn’t exist.
-
static
get_current_git_user()[source]¶ Get the currently configured Git user.
Returns: A tuple containing the name and email address. Return type: tuple
-
-
class
sr.tools.inventory.inventory.Item(path, parent=None)[source]¶ Bases:
objectAn item in the inventory.
Parameters: - path (str) – The path to the item.
- parent – The item parent.
-
class
sr.tools.inventory.inventory.ItemGroup(path, parent=None)[source]¶ Bases:
sr.tools.inventory.inventory.ItemTreeA group of items in the inventory.
Parameters: - path (str) – The path to the item group.
- parent – The parent item or tree.
-
ignore_fnames= ('info',)¶
-
class
sr.tools.inventory.inventory.ItemTree(path, parent=None)[source]¶ Bases:
objectA tree of items in the inventory.
Parameters: - path (str) – The path to the tree.
- parent – The parent item or tree.
-
ignore_fnames= ()¶
-
resolve(path)[source]¶ Resolve the given path into an object.
Parameters: path (str) – The path to resolve. Returns: The resolved items or self.
-
special_fnames= {'info': "group 'info' files may only exist within directories which are themselves assets"}¶
-
exception
sr.tools.inventory.inventory.NotAnInventoryError(directory)[source]¶ Bases:
OSErrorRaised when an inventory object is created for a directory that is not an inventory.
Parameters: directory – The directory that is not an inventory. Also accessible as the directoryattribute of this class.
-
sr.tools.inventory.inventory.cached_yaml_load(path)[source]¶ Load a pickled YAML file from cache.
Parameters: path (str) – The path to load. Returns: The loaded YAML file, possibly from cache. Return type: dict
-
sr.tools.inventory.inventory.find_top_level_dir(start_dir=None)[source]¶ Find the top level of the inventory repo.
Parameters: start_dir (str) – The working to start the search from. If this is None, the current working directory is used. Returns: The top level directory or None. Return type: str or None
-
sr.tools.inventory.inventory.get_inventory(directory=None)[source]¶ Get an
Inventoryobject for a directory.Parameters: directory (str) – The directory to find the inventory from. If this is left as None, the current working directory is used. Returns: An instance of an Inventoryobject pointing to the inventory in the directory specified.Return type: InventoryRaises: OSError – If the directory is not an inventory.
-
sr.tools.inventory.inventory.should_ignore(path)[source]¶ Check if the path should be ignored. A path that is deamed ignore-worthy starts with ‘.’ or ends with ‘~’.
Parameters: path (str) – The path to check. Returns: Trueif the path should be ignored, elseFalse.Return type: bool
Module for calculation and verifying the checksum of a number using the Luhn algorithm.
Validation can be done with is_valid() which validates that the calculated checksum is 0. A valid number can be made by calculating the check digit and appending it.
>>> is_valid('7894')
False
>>> checksum('7894')
6
>>> calc_check_digit('7894')
'9'
>>> is_valid('78949')
True
An alternative alphabet can be provided to use the Luhn mod N algorithm. The default alphabet is ‘0123456789’.
>>> is_valid('1234', alphabet='0123456789abcdef')
False
>>> checksum('1234', alphabet='0123456789abcdef')
14
-
sr.tools.inventory.luhn.calc_check_digit(number, alphabet='0123456789')[source]¶ With the provided number, calculate the extra digit that should be appended to make it pass the Luhn checksum.
Parameters: Returns: The check digit of the number.
Return type:
-
sr.tools.inventory.luhn.checksum(number, alphabet='0123456789')[source]¶ Calculate the Luhn checksum over the provided number. Valid numbers should have a checksum of 0.
Parameters: Returns: The checksum of the number.
Return type:
-
sr.tools.inventory.luhn.is_valid(number, alphabet='0123456789')[source]¶ Checks to see if the number provided passes the Luhn checksum.
Parameters: Returns: True if the number is valid.
Return type:
-
class
sr.tools.inventory.query_ast.And(left, right)[source]¶ Bases:
sr.tools.inventory.query_ast.NonTerminalAn AST node representing an ‘and’.
Parameters: - left – The left side of the operation.
- right – The right side of the operation.
-
class
sr.tools.inventory.query_ast.Assy(assy)[source]¶ Bases:
sr.tools.inventory.query_ast.TerminalAn AST node representing an ‘assy’ check.
Parameters: assy (A string representation of a bool ('true', '1', etc)) – Whether or not the asset has an assembly.
-
class
sr.tools.inventory.query_ast.Code(*codes)[source]¶ Bases:
sr.tools.inventory.query_ast.TerminalAn AST node representing a ‘code’ check.
Parameters: codes (list of str) – A list of valid codes.
-
class
sr.tools.inventory.query_ast.Condition(*conditions)[source]¶ Bases:
sr.tools.inventory.query_ast.TerminalAn AST node representing a ‘condition’ check operation.
Parameters: conditions (list of strs) – A list of conditions that should be matched.
-
class
sr.tools.inventory.query_ast.Function(func_name, node)[source]¶ Bases:
sr.tools.inventory.query_ast.NonTerminalAn AST node representing a function.
Params str func_name: The name of the function. Params node: The node to run the functions on.
-
class
sr.tools.inventory.query_ast.Labelled(labelled)[source]¶ Bases:
sr.tools.inventory.query_ast.TerminalAn AST node representing a ‘labelled’ check.
Parameters: labelled (A string representation of a bool ('true', '1', etc)) – Whether or not the asset is labelled.
-
class
sr.tools.inventory.query_ast.NonTerminal[source]¶ Bases:
sr.tools.inventory.query_ast.ASTNodeA non-terminal AST node.
-
class
sr.tools.inventory.query_ast.Not(node)[source]¶ Bases:
sr.tools.inventory.query_ast.NonTerminalAn AST node representing a ‘not’ operation.
Parameters: node – The node to be not-ed.
-
class
sr.tools.inventory.query_ast.Or(left, right)[source]¶ Bases:
sr.tools.inventory.query_ast.NonTerminalAn AST node representing an ‘or’ operation.
Parameters: - left – The left side of the operation.
- right – The right side of the operation.
-
class
sr.tools.inventory.query_ast.Path(*paths)[source]¶ Bases:
sr.tools.inventory.query_ast.TerminalAn AST node representing a ‘path’ check.
Parameters: paths (list of str) – A list of paths that are deamed valid.
-
class
sr.tools.inventory.query_ast.Serial(*serials)[source]¶ Bases:
sr.tools.inventory.query_ast.TerminalAn AST node representing a ‘serial’ check.
Parameters: serials (str) – A list of valid serials.
-
class
sr.tools.inventory.query_ast.Terminal[source]¶ Bases:
sr.tools.inventory.query_ast.ASTNodeA terminal AST node.
-
class
sr.tools.inventory.query_ast.TriState(key, desired_val)[source]¶ Bases:
sr.tools.inventory.query_ast.TerminalAn AST node representing a ‘tri’ check.
Parameters:
-
class
sr.tools.inventory.query_ast.Type(*types)[source]¶ Bases:
sr.tools.inventory.query_ast.TerminalAn AST node representing a ‘type’ check.
Parameters: types – A list of types that should be checked.
Spending¶
Library for accessing the spending files.
-
exception
sr.tools.spending.LedgerNotFound[source]¶ Bases:
ExceptionAn exception that occurs if ‘ledger’ cannot be found.
-
exception
sr.tools.spending.NotSpendingRepo[source]¶ Bases:
ExceptionAn exception that occurs if the repo is not a spending clone.
-
class
sr.tools.spending.Transaction(name, date, fname)[source]¶ Bases:
objectA spending transaction.
Parameters:
-
sr.tools.spending.account_to_budget_line(account)[source]¶ Convert an account name to a budget line name.
Parameters: account (str) – The account to convert. Returns: The budget line.
-
sr.tools.spending.budget_line_to_account(line)[source]¶ Convert a budget line to an account name.
Parameters: line (str) – The line to convert. Returns: The account.
-
sr.tools.spending.find_root(path=None)[source]¶ Find the root directory of the spending repository.
Checks that the repository is spending.git too.
Parameters: path – if provided, is a path within the spending.git repository (defaults to working directory)
-
sr.tools.spending.group_trans_by_budget_line(trans)[source]¶ Group transactions by the budget line.
Parameters: trans – The transactions to group. Returns: A dictionary mapping budget line to a list of transactions.
-
sr.tools.spending.load_budget_spends(root)[source]¶ Load budget spending data.
Parameters: root (str) – The root of the spending data. Returns: A list of budget lines.
Team Git Repositories¶
-
class
sr.tools.teamgit.Repo(path, server='studentrobotics.org')[source]¶ Bases:
objectRepresenting a repository on the IDE.
Parameters:
-
class
sr.tools.teamgit.Team(identifier, server='studentrobotics.org', reporoot='/var/www/html/ide/repos')[source]¶ Bases:
objectRepresenting a team in the IDE.
Parameters:
-
sr.tools.teamgit.list_teams(reporoot='/var/www/html/ide/repos', server='studentrobotics.org')[source]¶ Get a list of teams from the IDE.
Parameters: Returns: A list of teams.
Return type: list of str
Trac¶
-
class
sr.tools.trac.Ticket(num, proxy)[source]¶ Bases:
objectA ticket that may have dependencies.
Parameters: - num (int) – The ticket number.
- proxy – The XMLRPC proxy object.
-
cleanup(dry_run=False, msg='Synchronise dependency summaries with dependencies (automated edit)')[source]¶ Clean-up the ticket’s description.
Parameters: Returns: Whether or not a change has occurred.
Return type:
-
url¶
-
class
sr.tools.trac.TracProxy(user=None, password=None, server=None, port=None, anon=False)[source]¶ Bases:
xmlrpc.client.ServerProxyAn XML-RPC proxy for SR Trac.
Parameters: - user – The username. By default this is looked up in the config or the user is prompted for it.
- password – The password to use. If left as its default value of None, it may be looked up in the keyring, or the user may be prompted for it.
- server – The server hostname. Defaults to that found in the config.
- port – The HTTPS port of the server. Defaults to that found in the config.
- anon – Whether to use trac anonymously.