app.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. #------------------------------------------------------------------------------
  4. # Naked | A Python command line application framework
  5. # Copyright 2014 Christopher Simpkins
  6. # MIT License
  7. #------------------------------------------------------------------------------
  8. #------------------------------------------------------------------------------------
  9. # c.cmd = Primary command (<executable> <primary command>)
  10. # c.cmd2 = Secondary command (<executable> <primary command> <secondary command>)
  11. #
  12. # c.option(option_string, [bool argument_required]) = test for option with optional test for positional arg to the option
  13. # c.option_with_arg(option_string) = test for option and mandatory positional argument to option test
  14. # c.flag(flag_string) = test for presence of a "--option=argument" style flag
  15. #
  16. # c.arg(arg_string) = returns the next positional argument to the arg_string argument
  17. # c.flag_arg(flag_string) = returns the flag assignment for a "--option=argument" style flag
  18. #------------------------------------------------------------------------------------
  19. # Application start
  20. def main():
  21. import sys
  22. from Naked.commandline import Command
  23. #from Naked.toolshed.state import StateObject
  24. from Naked.toolshed.system import stderr
  25. #------------------------------------------------------------------------------------------
  26. # [ Instantiate command line object ]
  27. # used for all subsequent conditional logic in the CLI application
  28. #------------------------------------------------------------------------------------------
  29. c = Command(sys.argv[0], sys.argv[1:])
  30. #------------------------------------------------------------------------------
  31. # [ Instantiate state object ]
  32. #------------------------------------------------------------------------------
  33. #state = StateObject()
  34. #------------------------------------------------------------------------------------------
  35. # [ Command Suite Validation ] - early validation of appropriate command syntax
  36. # Test that user entered a primary command, print usage if not
  37. #------------------------------------------------------------------------------------------
  38. if not c.command_suite_validates():
  39. from Naked.commands.usage import Usage
  40. Usage().print_usage()
  41. sys.exit(1)
  42. #------------------------------------------------------------------------------------------
  43. # [ PRIMARY COMMAND LOGIC ]
  44. # Test for primary commands and handle them
  45. #------------------------------------------------------------------------------------------
  46. #------------------------------------------------------------------------------
  47. # [ args ] - identify the parsed arguments for a command string (2)= help
  48. #------------------------------------------------------------------------------
  49. if c.cmd == "args":
  50. if c.cmd2 == "help":
  51. from Naked.commands.args import help as args_help
  52. args_help()
  53. elif c.argc > 0: # there is an argument to where that is not help
  54. from Naked.commands.args import Args
  55. a = Args(c.arg_to_cmd)
  56. a.run()
  57. else:
  58. stderr("The args command requires an example command as an argument. Use 'naked args help' for more information.", 1)
  59. #------------------------------------------------------------------------------
  60. # [ build ] - build the C code in the Naked library (2)= help
  61. #------------------------------------------------------------------------------
  62. elif c.cmd == "build":
  63. if c.cmd2 == "help":
  64. from Naked.commands.build import help as build_help
  65. build_help()
  66. else:
  67. from Naked.commands.build import compile_c_code
  68. import os, inspect
  69. abs_dirpath = os.path.join(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))), "toolshed", "c")
  70. compile_c_code(abs_dirpath) # function calls exit status code
  71. #------------------------------------------------------------------------------
  72. # [ classify ] - search Python application classifiers and display to user (args)-search string
  73. #------------------------------------------------------------------------------
  74. elif c.cmd == "classify":
  75. if c.cmd2 == "help":
  76. from Naked.commands.classifier import help as classifier_help
  77. classifier_help()
  78. else:
  79. if c.second: # if search string was given
  80. search_string = c.second
  81. else:
  82. search_string = "" # absence of search string detected in Classifier, defaults to the entire list instead of search
  83. from Naked.commands.classifier import Classifier
  84. c = Classifier(search_string)
  85. c.run()
  86. #------------------------------------------------------------------------------
  87. # [ dist ] - distribute source files to PyPI (2)=register, sdist, swheel, wheel, win, all, help
  88. #------------------------------------------------------------------------------
  89. elif c.cmd == "dist":
  90. if c.argc > 1:
  91. from Naked.commands.dist import Dist
  92. d = Dist()
  93. if c.cmd2 == "register": # python setup.py register
  94. d.run('register')
  95. elif c.cmd2 == "sdist": # python setup.py sdist upload
  96. d.run('sdist')
  97. elif c.cmd2 == "swheel": # python setup.py sdist bdist_wheel upload
  98. d.run('swheel')
  99. elif c.cmd2 == "wheel": # python setup.py bdist_wheel upload
  100. d.run('wheel')
  101. elif c.cmd2 == "win": # python setup.py bdist_wininst upload
  102. d.run('win')
  103. elif c.cmd2 == "all": # python setup.py sdist bdist_wheel bdist_wininst upload
  104. d.run('all')
  105. elif c.cmd2 == "help": # help for command
  106. from Naked.commands.dist import help as dist_help
  107. dist_help()
  108. else:
  109. stderr("The naked dist secondary command was not recognized. Use 'naked dist help' for more information.", 1)
  110. else:
  111. stderr("Please enter a secondary command", 1)
  112. #------------------------------------------------------------------------------
  113. # [ locate ] - locate Naked project files (2)= main, settings, setup, help
  114. #------------------------------------------------------------------------------
  115. elif c.cmd == "locate":
  116. from Naked.commands.locate import Locator
  117. if c.cmd2 == "help":
  118. from Naked.commands.locate import help as locate_help
  119. locate_help()
  120. elif c.cmd2 == "main":
  121. l = Locator('main')
  122. elif c.cmd2 == "settings":
  123. l = Locator('settings')
  124. elif c.cmd2 == "setup":
  125. l = Locator('setup')
  126. else:
  127. l = Locator('') #handles error report to user
  128. #------------------------------------------------------------------------------
  129. # [ make ] - make a new Naked project (2)=help (args)=project name
  130. #------------------------------------------------------------------------------
  131. elif c.cmd == "make":
  132. from Naked.commands.make import MakeController
  133. if c.cmd2 == "help":
  134. from Naked.commands.make import help as make_help
  135. make_help()
  136. if c.arg1: # arg1 is not help so use it as the argument to the make command
  137. m = MakeController(c.arg1)
  138. else:
  139. m = MakeController(None)
  140. m.run()
  141. #------------------------------------------------------------------------------
  142. # [ profile ] - run the profiler.py file in the Naked project (2)=help
  143. #------------------------------------------------------------------------------
  144. elif c.cmd == "profile":
  145. if c.cmd2 == "help":
  146. from Naked.commands.profile import help as profile_help
  147. profile_help()
  148. else:
  149. from Naked.commands.profile import Profiler
  150. p = Profiler()
  151. p.run()
  152. #------------------------------------------------------------------------------
  153. # [ pyh ] - help for python built-in library modules, classes, methods, functions
  154. #------------------------------------------------------------------------------
  155. elif c.cmd == "pyh":
  156. if c.cmd2 == "help":
  157. from Naked.commands.pyh import pyh_help
  158. pyh_help()
  159. else:
  160. if c.argc > 1:
  161. from Naked.commands.pyh import python_help
  162. python_help(c.arg1)
  163. else:
  164. stderr("Please enter a query term with the pyh command. Use 'naked pyh help' for more information.", 1)
  165. #------------------------------------------------------------------------------
  166. # [ test ] - Run unit tests on the project (2)= help,nose,pytest,tox,unittest (see help for args)
  167. #------------------------------------------------------------------------------
  168. elif c.cmd == "test":
  169. if c.argc > 1:
  170. if c.cmd2 == "help":
  171. from Naked.commands.test import help as tox_help
  172. tox_help()
  173. elif c.cmd2 == "nose":
  174. from Naked.commands.test import NoseTester
  175. n = NoseTester()
  176. n.run()
  177. elif c.cmd2 == "pytest":
  178. from Naked.commands.test import PyTester
  179. p = PyTester()
  180. p.run()
  181. elif c.cmd2 == "tox":
  182. from Naked.commands.test import ToxTester
  183. if c.arg2: #user specified a python version to run with one of the tox version defs
  184. t = ToxTester(c.arg2) #instantiate with the python version
  185. else:
  186. t = ToxTester()
  187. t.run()
  188. elif c.cmd2 == "unittest":
  189. from Naked.commands.test import UnitTester
  190. if c.arg2:
  191. t = UnitTester(c.arg2)
  192. t.run()
  193. else:
  194. stderr("Please include a unit test file path. Use 'naked test help' for more information.", 1)
  195. else:
  196. stderr("The secondary command was not recognized. Use 'naked test help' for more information.", 1)
  197. else:
  198. stderr("Please include a secondary command with the 'naked test' command. Use 'naked dist help' for more information.", 1)
  199. #------------------------------------------------------------------------------------------
  200. # [ NAKED FRAMEWORK COMMANDS ]
  201. # Naked framework provides default help, usage, and version commands for all applications
  202. # --> settings for user messages are assigned in the lib/PROJECT/settings.py file
  203. #------------------------------------------------------------------------------------------
  204. elif c.help(): # User requested naked help (help.py module in commands directory)
  205. from Naked.commands.help import Help
  206. Help().print_help()
  207. elif c.usage(): # user requested naked usage info (usage.py module in commands directory)
  208. from Naked.commands.usage import Usage
  209. Usage().print_usage()
  210. elif c.version(): # user requested naked version (version.py module in commands directory)
  211. from Naked.commands.version import Version
  212. Version().print_version()
  213. #------------------------------------------------------------------------------------------
  214. # [ DEFAULT MESSAGE FOR MATCH FAILURE ]
  215. # Message to provide to the user when all above conditional logic fails to meet a true condition
  216. #------------------------------------------------------------------------------------------
  217. else:
  218. print("Could not complete the command that you entered. Please try again.")
  219. sys.exit(1) #exit
  220. if __name__ == '__main__':
  221. main()