def test_twice(self): """Attributes are expensive to get so they are cached.""" class Class: pass old_attributes = get_attributes(Class) new_attributes = get_attributes(Class) self.assertEqual(old_attributes, new_attributes)
def check_attribute(option, opt, value): """Check that the given value is a valid attribute.""" if value not in get_attributes(Item): raise OptionValueError( "option %s: invalid value: %r, not found in available attributes." % (opt, value)) return value
def test_attribute(self): """ The L{get_attributes} function returns a dictionary with each class attribute. """ class Class: attr = String() attributes = get_attributes(Class) self.assertEqual(len(attributes), 1) self.assertTrue("attr" in attributes)
def get_variables(obj): from jiraban.attribute import get_attributes if "__variables__" in obj.__dict__: return obj.__dict__["__variables__"] else: variables = {} cls = type(obj) for attribute in get_attributes(cls).values(): variable = attribute.variable_factory(attribute=attribute) variables[attribute] = variable return obj.__dict__.setdefault("__variables__", variables)
class RunnerApplication(Application): # Application defaults usage = """\ Usage: %%prog [OPTIONS] USERNAME [PASSWORD] Warning, JIRA might limit the number of requests within a period of time. Attributes: %s\ """ % "\n ".join(sorted(get_attributes(Item).keys())) # Runner defaults default_output = "-" default_server = "http://localhost:8080" # Display defaults default_category = "fix_versions" default_identity = "assignee" default_story = "components" def add_options(self, parser): """See L{Application}.""" super(RunnerApplication, self).add_options(parser) runner_group = OptionGroup(parser, "Runner options") runner_group.add_option( "--cache", metavar="FILE", help=("""Cache to use instead of a request on the server.""")) runner_group.add_option( "-o", "--output", metavar="FILE", default=self.default_output, help=("""Output file, defaults to "%default".""")) runner_group.add_option( "-s", "--server", metavar="URL", default=self.default_server, help=("""JIRA server, defaults to "%default".""")) filter_group = OptionGroup(parser, "Filter options") filter_group.add_option( "-a", "--assignee", metavar="NAME", action="append", type="string", default=[], help=( """Assignees to filter, defaults to the current user """ """without other filters. More than one can be specified.""")) filter_group.add_option("-c", "--component", metavar="NAME", action="append", type="string", default=[], help=("""Components to filter. """ """More than one can be specified.""")) filter_group.add_option( "-j", "--jql", metavar="QUERY", help=("""JIRA query language. """ """Cannot be specified with assignee or component.""")) display_group = OptionGroup(parser, "Display options") display_group.option_class = AttributeOption display_group.add_option( "--category", metavar="ATTR", type="attribute", default=self.default_category, help=("""Category attribute to group items horizontally, """ """defaults to "%default".""")) display_group.add_option( "--identity", metavar="ATTR", type="attribute", default=self.default_identity, help=("""Identity attribute to group items by color, """ """defaults to "%default".""")) display_group.add_option( "--story", metavar="ATTR", type="attribute", default=self.default_story, help=("""Story attribute to group items vertically, """ """defaults to "%default".""")) parser.add_option_group(runner_group) parser.add_option_group(filter_group) parser.add_option_group(display_group) def parse_options(self, options, args): """See L{Application}.""" super(RunnerApplication, self).parse_options(options, args) if len(args) < 1: raise OptionValueError("Missing USERNAME.") if len(args) > 2: raise OptionValueError("Too many arguments.") username = args[0] password = args[1] if len(args) > 1 else getpass("Password: "******"Cannot use JQL with assignee or component") self.jql = options.jql else: if not options.assignee and not options.component: assignee = "currentUser()" else: assignee = options.assignee self.jql = kwargs_to_jql(resolution="unresolved", assignee=assignee, component=options.component) self.jira = JIRA(options.server, username, password) self.board = Board(self.jql, self.jira.query_html(self.jql).url, options.category, options.story, options.identity) self.cache = options.cache self.output = options.output def process(self): """See L{Application}.""" try: for item in self.jira.iter_items(self.jql, self.cache): self.board.add(item) except JIRAError, e: raise ApplicationError(e) html = generate_html(self.board, self.jira) if self.output != "-": output_file = open(self.output, "w") else: output_file = sys.stdout try: print >> output_file, html.encode('utf-8') finally: if self.output != "-": output_file.close()