def _test_get_console_locale_without_babel(self): os.environ['LANG'] = 'en_US.utf-8' os.environ['LC_MESSAGES'] = 'de_DE.utf-8' os.environ['LC_ALL'] = 'en_US.utf-8' os.environ['LANGUAGE'] = 'de_DE:en_US' self.assertIsNone(get_console_locale(None, 'en_US.UTF8')) self.env.config.set('trac', 'default_language', '') self.assertIsNone(get_console_locale(self.env, 'en_US.UTF8')) self.assertIsNone(get_console_locale(self.env)) self.env.config.set('trac', 'default_language', 'en_US') self.assertIsNone(get_console_locale(self.env, 'en_US.UTF8')) self.assertIsNone(get_console_locale(self.env))
def run(args=None): """Main entry point.""" if args is None: args = sys.argv[1:] if has_babel: translation.activate(get_console_locale()) warn_setuptools_issue() admin = TracAdmin() if len(args) > 0: if args[0] in ('-h', '--help', 'help'): return admin.onecmd(' '.join(['help'] + args[1:])) elif args[0] in ('-v','--version'): printout(os.path.basename(sys.argv[0]), TRAC_VERSION) else: env_path = os.path.abspath(args[0]) try: unicode(env_path, 'ascii') except UnicodeDecodeError: printerr(_("Non-ascii environment path '%(path)s' not " "supported.", path=to_unicode(env_path))) sys.exit(2) admin.env_set(env_path) if len(args) > 1: s_args = ' '.join(["'%s'" % c for c in args[2:]]) command = args[1] + ' ' + s_args return admin.onecmd(command) else: while True: try: admin.run() except KeyboardInterrupt: admin.do_quit('') else: return admin.onecmd("help")
def _do_add(self, name, due=None): milestone = model.Milestone(self.env) milestone.name = name if due is not None: milestone.due = parse_date(due, hint='datetime', locale=get_console_locale(self.env)) milestone.insert()
def run(args=None): """Main entry point.""" translation.activate(get_console_locale()) try: return _run(args) finally: translation.deactivate()
def _init_env(self): self.__env = env = Environment(self.envname) # fix language according to env settings if has_babel: negotiated = get_console_locale(env) if negotiated: translation.activate(negotiated)
def get_admin_commands(self): hints = { 'datetime': get_datetime_format_hint(get_console_locale(self.env)), 'iso8601': get_datetime_format_hint('iso8601'), } yield ('milestone list', '', "Show milestones", None, self._do_list) yield ('milestone add', '<name> [due]', "Add milestone", None, self._do_add) yield ('milestone rename', '<name> <newname>', "Rename milestone", self._complete_name, self._do_rename) yield ('milestone due', '<name> <due>', """Set milestone due date The <due> date must be specified in the "%(datetime)s" or "%(iso8601)s" (ISO 8601) format. Alternatively, "now" can be used to set the due date to the current time. To remove the due date from a milestone, specify an empty string (""). """ % hints, self._complete_name, self._do_due) yield ('milestone completed', '<name> <completed>', """Set milestone complete date The <completed> date must be specified in the "%(datetime)s" or "%(iso8601)s" (ISO 8601) format. Alternatively, "now" can be used to set the completion date to the current time. To remove the completion date from a milestone, specify an empty string (""). """ % hints, self._complete_name, self._do_completed) yield ('milestone remove', '<name>', "Remove milestone", self._complete_name, self._do_remove)
def get_admin_commands(self): locale = get_console_locale(self.env) hints = { 'datetime': get_datetime_format_hint(locale), 'iso8601': get_datetime_format_hint('iso8601'), } yield ('version list', '', "Show versions", None, self._do_list) yield ('version add', '<name> [time]', "Add version", None, self._do_add) yield ('version rename', '<name> <newname>', "Rename version", self._complete_name, self._do_rename) yield ('version remove', '<name>', "Remove version", self._complete_name, self._do_remove) yield ('version time', '<name> <time>', """Set version date The <time> must be specified in the "%(datetime)s" or "%(iso8601)s" (ISO 8601) format. Alternatively, "now" can be used to set the version date to the current time. To remove the date from a version, specify an empty string (""). """ % hints, self._complete_name, self._do_time)
def _do_add(self, name, time=None): version = model.Version(self.env) version.name = name version.time = parse_date(time, hint='datetime', locale=get_console_locale(self.env)) \ if time else None version.insert()
def get_admin_commands(self): hints = { 'datetime': get_datetime_format_hint(get_console_locale(self.env)), 'iso8601': get_datetime_format_hint('iso8601'), } yield ('version list', '', "Show versions", None, self._do_list) yield ('version add', '<name> [time]', "Add version", None, self._do_add) yield ('version rename', '<name> <newname>', "Rename version", self._complete_name, self._do_rename) yield ('version time', '<name> <time>', """Set version date The <time> must be specified in the "%(datetime)s" or "%(iso8601)s" (ISO 8601) format. Alternatively, "now" can be used to set the version date to the current time. To remove the date from a version, specify an empty string (""). """ % hints, self._complete_name, self._do_time) yield ('version remove', '<name>', "Remove version", self._complete_name, self._do_remove)
def _date_convert(self, date_str): try: locale = get_console_locale(self._env) return to_timestamp(parse_date(date_str, hint='date', locale=locale)) except Exception as excpt: self._env.log.exception(excpt) raise EmptyRangeError(excpt.message)
def _init_env(self): self.__env = env = Environment(self.envname) negotiated = None # fixup language according to env settings if has_babel: negotiated = get_console_locale(env) if negotiated: translation.activate(negotiated)
def _do_add(self, name, time=None): version = model.Version(self.env) version.name = name if time is not None: version.time = time and \ parse_date(time, hint='datetime', locale=get_console_locale(self.env)) version.insert()
def _do_add(self, name, time=None): version = model.Version(self.env) version.name = name if time is not None: version.time = parse_date(time, hint='datetime', locale=get_console_locale(self.env)) \ if time else None version.insert()
def _init_env(self): self.__env = env = Environment(self.envname) negotiated = None # fixup language according to env settings if has_babel: negotiated = get_console_locale(env) if negotiated: translation.activate(negotiated) self.cmd_mgr = AdminCommandManager(env)
def get_admin_commands(self): hints = { 'datetime': get_datetime_format_hint(get_console_locale(self.env)), 'iso8601': get_datetime_format_hint('iso8601'), } yield ('session list', '[sid[:0|1]] [...]', """List the name and email for the given sids Specifying the sid 'anonymous' lists all unauthenticated sessions, and 'authenticated' all authenticated sessions. '*' lists all sessions, and is the default if no sids are given. An sid suffix ':0' operates on an unauthenticated session with the given sid, and a suffix ':1' on an authenticated session (the default).""", self._complete_list, self._do_list) yield ('session add', '<sid[:0|1]> [name] [email]', """Create a session for the given sid Populates the name and email attributes for the given session. Adding a suffix ':0' to the sid makes the session unauthenticated, and a suffix ':1' makes it authenticated (the default if no suffix is specified).""", None, self._do_add) yield ('session set', '<name|email|default_handler> ' '<sid[:0|1]> <value>', """Set the name or email attribute of the given sid An sid suffix ':0' operates on an unauthenticated session with the given sid, and a suffix ':1' on an authenticated session (the default).""", self._complete_set, self._do_set) yield ('session delete', '<sid[:0|1]> [...]', """Delete the session of the specified sid An sid suffix ':0' operates on an unauthenticated session with the given sid, and a suffix ':1' on an authenticated session (the default). Specifying the sid 'anonymous' will delete all anonymous sessions.""", self._complete_delete, self._do_delete) yield ('session purge', '<age>', """Purge anonymous sessions older than given age or date Age may be specified as a relative time like "90 days ago", or as a date in the "%(datetime)s" or "%(iso8601)s" (ISO 8601) format.""" % hints, None, self._do_purge)
def get_admin_commands(self): hints = { 'datetime': get_datetime_format_hint(get_console_locale(self.env)), 'iso8601': get_datetime_format_hint('iso8601'), } yield ('session list', '[sid[:0|1]] [...]', """List the name and email for the given sids Specifying the sid 'anonymous' lists all unauthenticated sessions, and 'authenticated' all authenticated sessions. '*' lists all sessions, and is the default if no sids are given. An sid suffix ':0' operates on an unauthenticated session with the given sid, and a suffix ':1' on an authenticated session (the default).""", self._complete_list, self._do_list) yield ('session add', '<sid[:0|1]> [name] [email]', """Create a session for the given sid Populates the name and email attributes for the given session. Adding a suffix ':0' to the sid makes the session unauthenticated, and a suffix ':1' makes it authenticated (the default if no suffix is specified).""", None, self._do_add) yield ('session set', '<name|email> <sid[:0|1]> <value>', """Set the name or email attribute of the given sid An sid suffix ':0' operates on an unauthenticated session with the given sid, and a suffix ':1' on an authenticated session (the default).""", self._complete_set, self._do_set) yield ('session delete', '<sid[:0|1]> [...]', """Delete the session of the specified sid An sid suffix ':0' operates on an unauthenticated session with the given sid, and a suffix ':1' on an authenticated session (the default). Specifying the sid 'anonymous' will delete all anonymous sessions.""", self._complete_delete, self._do_delete) yield ('session purge', '<age>', """Purge anonymous sessions older than the given age or date Age may be specified as a relative time like "90 days ago", or as a date in the "%(datetime)s" or "%(iso8601)s" (ISO 8601) format.""" % hints, None, self._do_purge)
def _do_purge(self, age): when = parse_date(age, hint='datetime', locale=get_console_locale(self.env)) with self.env.db_transaction as db: ts = to_timestamp(when) db(""" DELETE FROM session WHERE authenticated=0 AND last_visit<%s """, (ts,)) db(""" DELETE FROM session_attribute WHERE authenticated=0 AND sid NOT IN (SELECT sid FROM session WHERE authenticated=0) """)
def get_admin_commands(self): locale = get_console_locale(self.env) hints = { 'datetime': get_datetime_format_hint(locale), 'iso8601': get_datetime_format_hint('iso8601'), } yield ('milestone list', '', "Show milestones", None, self._do_list) yield ('milestone add', '<name> [due]', "Add milestone", None, self._do_add) yield ('milestone rename', '<name> <newname>', "Rename milestone", self._complete_name, self._do_rename) yield ('milestone due', '<name> <due>', """Set milestone due date The <due> date must be specified in the "%(datetime)s" or "%(iso8601)s" (ISO 8601) format. Alternatively, "now" can be used to set the due date to the current time. To remove the due date from a milestone, specify an empty string (""). """ % hints, self._complete_name, self._do_due) yield ('milestone completed', '<name> <completed>', """Set milestone complete date The <completed> date must be specified in the "%(datetime)s" or "%(iso8601)s" (ISO 8601) format. Alternatively, "now" can be used to set the completion date to the current time. To remove the completion date from a milestone, specify an empty string (""). """ % hints, self._complete_name, self._do_completed) yield ('milestone remove', '<name>', "Remove milestone", self._complete_name, self._do_remove)
def _do_time(self, name, time): version = model.Version(self.env, name) version.time = parse_date(time, hint='datetime', locale=get_console_locale(self.env)) \ if time else None version.update()
def _do_completed(self, name, completed): milestone = model.Milestone(self.env, name) milestone.completed = parse_date(completed, hint='datetime', locale=get_console_locale(self.env)) \ if completed else None milestone.update()
def _do_due(self, name, due): milestone = model.Milestone(self.env, name) milestone.due = parse_date(due, hint='datetime', locale=get_console_locale(self.env)) \ if due else None milestone.update()
def datetime_format_hint(self): return get_datetime_format_hint(get_console_locale(self.env))
def _test_get_console_locale_with_babel(self): from babel.core import Locale, UnknownLocaleError locales = get_available_locales() en_US = Locale.parse('en_US') de = Locale.parse('de') def unset_locale_envs(): for name in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'): if name in os.environ: del os.environ[name] if 'de' in locales: unset_locale_envs() self.assertIsNone(get_console_locale(None, None)) self.assertEqual(de, get_console_locale(None, 'de_DE.UTF8')) self.env.config.set('trac', 'default_language', 'de') self.assertEqual(de, get_console_locale(self.env, None)) self.assertEqual(de, get_console_locale(self.env, 'C')) self.env.config.set('trac', 'default_language', 'en_US') self.assertEqual(en_US, get_console_locale(self.env, None)) self.assertEqual(en_US, get_console_locale(self.env, 'C')) self.assertEqual(de, get_console_locale(self.env, 'de_DE.UTF8')) self.env.config.set('trac', 'default_language', 'de') os.environ['LANG'] = 'POSIX' # unavailable locale in Trac self.assertIsNone(get_console_locale()) self.assertEqual(de, get_console_locale(self.env)) os.environ['LANG'] = '****' # invalid locale self.assertIsNone(get_console_locale()) self.assertEqual(de, get_console_locale(self.env)) os.environ['LANG'] = 'en_US.utf-8' self.assertEqual(en_US, get_console_locale()) self.assertEqual(en_US, get_console_locale(self.env)) os.environ['LC_MESSAGES'] = 'de_DE.utf-8' self.assertEqual(de, get_console_locale()) self.assertEqual(de, get_console_locale(self.env)) os.environ['LC_ALL'] = 'en_US.utf-8' self.assertEqual(en_US, get_console_locale()) self.assertEqual(en_US, get_console_locale(self.env)) os.environ['LANGUAGE'] = 'de_DE:en_US:en' self.assertEqual(de, get_console_locale()) self.assertEqual(de, get_console_locale(self.env)) if not locales: # compiled catalog is missing unset_locale_envs() self.assertIsNone(get_console_locale(None, 'de_DE.UTF8')) self.env.config.set('trac', 'default_language', 'de') self.assertIsNone(get_console_locale(self.env, None)) self.assertIsNone(get_console_locale(self.env, 'C')) self.assertIsNone(get_console_locale(self.env, 'de_DE.UTF8')) os.environ['LANG'] = 'en_US.utf-8' os.environ['LC_MESSAGES'] = 'de_DE.utf-8' os.environ['LC_ALL'] = 'en_US.utf-8' os.environ['LANGUAGE'] = 'de_DE:en_US' self.assertEqual(en_US, get_console_locale()) self.assertEqual(en_US, get_console_locale(self.env))